webcrypto_util.h revision effb81e5f8246d0db0270817048dc992db66e9fb
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/WebArrayBuffer.h" 15#include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" 16#include "third_party/WebKit/public/platform/WebCryptoKey.h" 17 18namespace content { 19 20namespace webcrypto { 21 22class Status; 23 24// Returns a pointer to the start of |data|, or NULL if it is empty. This is a 25// convenience function for getting the pointer, and should not be used beyond 26// the expected lifetime of |data|. 27CONTENT_EXPORT const uint8* Uint8VectorStart(const std::vector<uint8>& data); 28 29// Shrinks a WebArrayBuffer to a new size. 30// TODO(eroman): This works by re-allocating a new buffer. It would be better if 31// the WebArrayBuffer could just be truncated instead. 32void ShrinkBuffer(blink::WebArrayBuffer* buffer, unsigned int new_size); 33 34// Creates a WebArrayBuffer from a uint8 byte array 35blink::WebArrayBuffer CreateArrayBuffer(const uint8* data, 36 unsigned int data_size); 37 38// This function decodes unpadded 'base64url' encoded data, as described in 39// RFC4648 (http://www.ietf.org/rfc/rfc4648.txt) Section 5. 40// In Web Crypto, this type of encoding is only used inside JWK. 41CONTENT_EXPORT bool Base64DecodeUrlSafe(const std::string& input, 42 std::string* output); 43 44// Returns an unpadded 'base64url' encoding of the input data, the opposite of 45// Base64DecodeUrlSafe() above. 46std::string Base64EncodeUrlSafe(const base::StringPiece& input); 47std::string Base64EncodeUrlSafe(const std::vector<uint8>& input); 48 49// Composes a Web Crypto usage mask from an array of JWK key_ops values. 50CONTENT_EXPORT Status GetWebCryptoUsagesFromJwkKeyOps( 51 const base::ListValue* jwk_key_ops_value, 52 blink::WebCryptoKeyUsageMask* jwk_key_ops_mask); 53 54// Composes a JWK key_ops array from a Web Crypto usage mask. 55base::ListValue* CreateJwkKeyOpsFromWebCryptoUsages( 56 blink::WebCryptoKeyUsageMask usage_mask); 57 58CONTENT_EXPORT bool IsHashAlgorithm(blink::WebCryptoAlgorithmId alg_id); 59 60// Returns the "hash" param for an algorithm if it exists, otherwise returns 61// a null algorithm. 62blink::WebCryptoAlgorithm GetInnerHashAlgorithm( 63 const blink::WebCryptoAlgorithm& algorithm); 64 65// Creates a WebCryptoAlgorithm without any parameters. 66CONTENT_EXPORT blink::WebCryptoAlgorithm CreateAlgorithm( 67 blink::WebCryptoAlgorithmId id); 68 69// Creates an HMAC import algorithm whose inner hash algorithm is determined by 70// the specified algorithm ID. It is an error to call this method with a hash 71// algorithm that is not SHA*. 72CONTENT_EXPORT blink::WebCryptoAlgorithm CreateHmacImportAlgorithm( 73 blink::WebCryptoAlgorithmId hash_id); 74 75// Creates an import algorithm for RSA algorithms that take a hash. 76// It is an error to call this with a hash_id that is not a SHA*. 77CONTENT_EXPORT blink::WebCryptoAlgorithm CreateRsaHashedImportAlgorithm( 78 blink::WebCryptoAlgorithmId id, 79 blink::WebCryptoAlgorithmId hash_id); 80 81bool CreateSecretKeyAlgorithm(const blink::WebCryptoAlgorithm& algorithm, 82 unsigned int keylen_bytes, 83 blink::WebCryptoKeyAlgorithm* key_algorithm); 84 85} // namespace webcrypto 86 87} // namespace content 88 89#endif // CONTENT_CHILD_WEBCRYPTO_WEBCRYPTO_UTIL_H_ 90