Home | History | Annotate | Download | only in webcrypto
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef CONTENT_CHILD_WEBCRYPTO_WEBCRYPTO_UTIL_H_
      6 #define CONTENT_CHILD_WEBCRYPTO_WEBCRYPTO_UTIL_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 #include "base/basictypes.h"
     11 #include "base/strings/string_piece.h"
     12 #include "base/values.h"
     13 #include "content/common/content_export.h"
     14 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h"
     15 #include "third_party/WebKit/public/platform/WebCryptoKey.h"
     16 
     17 namespace content {
     18 
     19 namespace webcrypto {
     20 
     21 class Status;
     22 
     23 // Returns a pointer to the start of |data|, or NULL if it is empty. This is a
     24 // convenience function for getting the pointer, and should not be used beyond
     25 // the expected lifetime of |data|.
     26 CONTENT_EXPORT const uint8* Uint8VectorStart(const std::vector<uint8>& data);
     27 CONTENT_EXPORT uint8* Uint8VectorStart(std::vector<uint8>* data);
     28 
     29 // This function decodes unpadded 'base64url' encoded data, as described in
     30 // RFC4648 (http://www.ietf.org/rfc/rfc4648.txt) Section 5.
     31 // In Web Crypto, this type of encoding is only used inside JWK.
     32 CONTENT_EXPORT bool Base64DecodeUrlSafe(const std::string& input,
     33                                         std::string* output);
     34 
     35 // Returns an unpadded 'base64url' encoding of the input data, the opposite of
     36 // Base64DecodeUrlSafe() above.
     37 CONTENT_EXPORT std::string Base64EncodeUrlSafe(const base::StringPiece& input);
     38 CONTENT_EXPORT std::string Base64EncodeUrlSafe(const std::vector<uint8>& input);
     39 
     40 // Composes a Web Crypto usage mask from an array of JWK key_ops values.
     41 CONTENT_EXPORT Status GetWebCryptoUsagesFromJwkKeyOps(
     42     const base::ListValue* jwk_key_ops_value,
     43     blink::WebCryptoKeyUsageMask* jwk_key_ops_mask);
     44 
     45 // Composes a JWK key_ops array from a Web Crypto usage mask.
     46 base::ListValue* CreateJwkKeyOpsFromWebCryptoUsages(
     47     blink::WebCryptoKeyUsageMask usage_mask);
     48 
     49 // Returns the "hash" param for an algorithm if it exists, otherwise returns
     50 // a null algorithm.
     51 blink::WebCryptoAlgorithm GetInnerHashAlgorithm(
     52     const blink::WebCryptoAlgorithm& algorithm);
     53 
     54 // Creates a WebCryptoAlgorithm without any parameters.
     55 CONTENT_EXPORT blink::WebCryptoAlgorithm CreateAlgorithm(
     56     blink::WebCryptoAlgorithmId id);
     57 
     58 // Creates an HMAC import algorithm whose inner hash algorithm is determined by
     59 // the specified algorithm ID. It is an error to call this method with a hash
     60 // algorithm that is not SHA*.
     61 CONTENT_EXPORT blink::WebCryptoAlgorithm CreateHmacImportAlgorithm(
     62     blink::WebCryptoAlgorithmId hash_id);
     63 
     64 // Creates an import algorithm for RSA algorithms that take a hash.
     65 // It is an error to call this with a hash_id that is not a SHA*.
     66 CONTENT_EXPORT blink::WebCryptoAlgorithm CreateRsaHashedImportAlgorithm(
     67     blink::WebCryptoAlgorithmId id,
     68     blink::WebCryptoAlgorithmId hash_id);
     69 
     70 bool CreateSecretKeyAlgorithm(const blink::WebCryptoAlgorithm& algorithm,
     71                               unsigned int keylen_bytes,
     72                               blink::WebCryptoKeyAlgorithm* key_algorithm);
     73 
     74 // Returns true if the set bits in b make up a subset of the set bits in a.
     75 bool ContainsKeyUsages(blink::WebCryptoKeyUsageMask a,
     76                        blink::WebCryptoKeyUsageMask b);
     77 
     78 bool IsAlgorithmRsa(blink::WebCryptoAlgorithmId alg_id);
     79 bool IsAlgorithmAsymmetric(blink::WebCryptoAlgorithmId alg_id);
     80 
     81 }  // namespace webcrypto
     82 
     83 }  // namespace content
     84 
     85 #endif  // CONTENT_CHILD_WEBCRYPTO_WEBCRYPTO_UTIL_H_
     86