OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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 #include "content/renderer/webcrypto/webcrypto_util.h" |
| 6 |
| 7 #include "base/base64.h" |
| 8 #include "base/logging.h" |
| 9 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
| 10 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
| 11 |
| 12 namespace content { |
| 13 |
| 14 namespace { |
| 15 |
| 16 WebKit::WebCryptoAlgorithm CreateAesKeyGenAlgorithm( |
| 17 WebKit::WebCryptoAlgorithmId aes_alg_id, |
| 18 unsigned short length) { |
| 19 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 20 aes_alg_id, new WebKit::WebCryptoAesKeyGenParams(length)); |
| 21 } |
| 22 |
| 23 } // namespace |
| 24 |
| 25 const uint8* Start(const std::vector<uint8>& data) { |
| 26 if (data.empty()) |
| 27 return NULL; |
| 28 return &data[0]; |
| 29 } |
| 30 |
| 31 void ShrinkBuffer(WebKit::WebArrayBuffer* buffer, unsigned new_size) { |
| 32 DCHECK_LE(new_size, buffer->byteLength()); |
| 33 |
| 34 if (new_size == buffer->byteLength()) |
| 35 return; |
| 36 |
| 37 WebKit::WebArrayBuffer new_buffer = |
| 38 WebKit::WebArrayBuffer::create(new_size, 1); |
| 39 DCHECK(!new_buffer.isNull()); |
| 40 memcpy(new_buffer.data(), buffer->data(), new_size); |
| 41 *buffer = new_buffer; |
| 42 } |
| 43 |
| 44 bool Base64DecodeUrlSafe(const std::string& input, std::string* output) { |
| 45 std::string base64EncodedText(input); |
| 46 std::replace(base64EncodedText.begin(), base64EncodedText.end(), '-', '+'); |
| 47 std::replace(base64EncodedText.begin(), base64EncodedText.end(), '_', '/'); |
| 48 base64EncodedText.append((4 - base64EncodedText.size() % 4) % 4, '='); |
| 49 return base::Base64Decode(base64EncodedText, output); |
| 50 } |
| 51 |
| 52 WebKit::WebCryptoAlgorithm GetInnerHashAlgorithm( |
| 53 const WebKit::WebCryptoAlgorithm& algorithm) { |
| 54 if (algorithm.hmacParams()) |
| 55 return algorithm.hmacParams()->hash(); |
| 56 if (algorithm.hmacKeyParams()) |
| 57 return algorithm.hmacKeyParams()->hash(); |
| 58 if (algorithm.rsaSsaParams()) |
| 59 return algorithm.rsaSsaParams()->hash(); |
| 60 if (algorithm.rsaOaepParams()) |
| 61 return algorithm.rsaOaepParams()->hash(); |
| 62 return WebKit::WebCryptoAlgorithm::createNull(); |
| 63 } |
| 64 |
| 65 WebKit::WebCryptoAlgorithm CreateAlgorithm(WebKit::WebCryptoAlgorithmId id) { |
| 66 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate(id, NULL); |
| 67 } |
| 68 |
| 69 WebKit::WebCryptoAlgorithm CreateHmacAlgorithmByDigestLen( |
| 70 unsigned short digest_length_bits) { |
| 71 WebKit::WebCryptoAlgorithmId hash_id; |
| 72 switch (digest_length_bits) { |
| 73 case 160: |
| 74 hash_id = WebKit::WebCryptoAlgorithmIdSha1; |
| 75 break; |
| 76 case 224: |
| 77 hash_id = WebKit::WebCryptoAlgorithmIdSha224; |
| 78 break; |
| 79 case 256: |
| 80 hash_id = WebKit::WebCryptoAlgorithmIdSha256; |
| 81 break; |
| 82 case 384: |
| 83 hash_id = WebKit::WebCryptoAlgorithmIdSha384; |
| 84 break; |
| 85 case 512: |
| 86 hash_id = WebKit::WebCryptoAlgorithmIdSha512; |
| 87 break; |
| 88 default: |
| 89 NOTREACHED(); |
| 90 return WebKit::WebCryptoAlgorithm::createNull(); |
| 91 } |
| 92 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 93 WebKit::WebCryptoAlgorithmIdHmac, |
| 94 new WebKit::WebCryptoHmacParams(CreateAlgorithm(hash_id))); |
| 95 } |
| 96 |
| 97 WebKit::WebCryptoAlgorithm CreateHmacAlgorithmByHashId( |
| 98 WebKit::WebCryptoAlgorithmId hash_id) { |
| 99 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 100 WebKit::WebCryptoAlgorithmIdHmac, |
| 101 new WebKit::WebCryptoHmacParams(CreateAlgorithm(hash_id))); |
| 102 } |
| 103 |
| 104 WebKit::WebCryptoAlgorithm CreateHmacKeyGenAlgorithm( |
| 105 WebKit::WebCryptoAlgorithmId hash_id, |
| 106 unsigned hash_length) { |
| 107 // hash_length < 0 means unspecified |
| 108 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 109 WebKit::WebCryptoAlgorithmIdHmac, |
| 110 new WebKit::WebCryptoHmacKeyParams( |
| 111 CreateAlgorithm(hash_id), (hash_length != 0), hash_length)); |
| 112 } |
| 113 |
| 114 WebKit::WebCryptoAlgorithm CreateRsaEsAlgorithm() { |
| 115 return CreateAlgorithm(WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5); |
| 116 } |
| 117 |
| 118 WebKit::WebCryptoAlgorithm CreateRsaSsaAlgorithm( |
| 119 WebKit::WebCryptoAlgorithmId hash_algorithm_id) { |
| 120 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 121 WebKit::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, |
| 122 new WebKit::WebCryptoRsaSsaParams(CreateAlgorithm(hash_algorithm_id))); |
| 123 } |
| 124 |
| 125 WebKit::WebCryptoAlgorithm CreateRsaOaepAlgorithm( |
| 126 WebKit::WebCryptoAlgorithmId hash_algorithm_id) { |
| 127 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 128 WebKit::WebCryptoAlgorithmIdRsaOaep, |
| 129 new WebKit::WebCryptoRsaOaepParams( |
| 130 CreateAlgorithm(hash_algorithm_id), false, NULL, 0)); |
| 131 } |
| 132 |
| 133 WebKit::WebCryptoAlgorithm CreateAesCbcAlgorithm(const std::vector<uint8>& iv) { |
| 134 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 135 WebKit::WebCryptoAlgorithmIdAesCbc, |
| 136 new WebKit::WebCryptoAesCbcParams(Start(iv), iv.size())); |
| 137 } |
| 138 |
| 139 WebKit::WebCryptoAlgorithm CreateAesGcmAlgorithm( |
| 140 const std::vector<uint8>& iv, |
| 141 const std::vector<uint8>& additional_data, |
| 142 unsigned char tag_length) { |
| 143 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 144 WebKit::WebCryptoAlgorithmIdAesCbc, |
| 145 new WebKit::WebCryptoAesGcmParams(Start(iv), |
| 146 iv.size(), |
| 147 additional_data.size(), |
| 148 Start(additional_data), |
| 149 additional_data.size(), |
| 150 tag_length != 0, |
| 151 tag_length)); |
| 152 } |
| 153 |
| 154 WebKit::WebCryptoAlgorithm CreateAesCbcKeyGenAlgorithm(unsigned short length) { |
| 155 return CreateAesKeyGenAlgorithm(WebKit::WebCryptoAlgorithmIdAesCbc, length); |
| 156 } |
| 157 |
| 158 WebKit::WebCryptoAlgorithm CreateAesGcmKeyGenAlgorithm(unsigned short length) { |
| 159 return CreateAesKeyGenAlgorithm(WebKit::WebCryptoAlgorithmIdAesGcm, length); |
| 160 } |
| 161 |
| 162 } // namespace content |
OLD | NEW |