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 webcrypto { |
| 15 |
| 16 namespace { |
| 17 |
| 18 blink::WebCryptoAlgorithm CreateAesKeyGenAlgorithm( |
| 19 blink::WebCryptoAlgorithmId aes_alg_id, |
| 20 unsigned short length) { |
| 21 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 22 aes_alg_id, new blink::WebCryptoAesKeyGenParams(length)); |
| 23 } |
| 24 |
| 25 bool IsHashAlgorithm(blink::WebCryptoAlgorithmId alg_id) { |
| 26 return alg_id == blink::WebCryptoAlgorithmIdSha1 || |
| 27 alg_id == blink::WebCryptoAlgorithmIdSha224 || |
| 28 alg_id == blink::WebCryptoAlgorithmIdSha256 || |
| 29 alg_id == blink::WebCryptoAlgorithmIdSha384 || |
| 30 alg_id == blink::WebCryptoAlgorithmIdSha512; |
| 31 } |
| 32 |
| 33 } // namespace |
| 34 |
| 35 const uint8* Uint8VectorStart(const std::vector<uint8>& data) { |
| 36 if (data.empty()) |
| 37 return NULL; |
| 38 return &data[0]; |
| 39 } |
| 40 |
| 41 void ShrinkBuffer(blink::WebArrayBuffer* buffer, unsigned new_size) { |
| 42 DCHECK_LE(new_size, buffer->byteLength()); |
| 43 |
| 44 if (new_size == buffer->byteLength()) |
| 45 return; |
| 46 |
| 47 blink::WebArrayBuffer new_buffer = blink::WebArrayBuffer::create(new_size, 1); |
| 48 DCHECK(!new_buffer.isNull()); |
| 49 memcpy(new_buffer.data(), buffer->data(), new_size); |
| 50 *buffer = new_buffer; |
| 51 } |
| 52 |
| 53 bool Base64DecodeUrlSafe(const std::string& input, std::string* output) { |
| 54 std::string base64EncodedText(input); |
| 55 std::replace(base64EncodedText.begin(), base64EncodedText.end(), '-', '+'); |
| 56 std::replace(base64EncodedText.begin(), base64EncodedText.end(), '_', '/'); |
| 57 base64EncodedText.append((4 - base64EncodedText.size() % 4) % 4, '='); |
| 58 return base::Base64Decode(base64EncodedText, output); |
| 59 } |
| 60 |
| 61 blink::WebCryptoAlgorithm GetInnerHashAlgorithm( |
| 62 const blink::WebCryptoAlgorithm& algorithm) { |
| 63 if (algorithm.hmacParams()) |
| 64 return algorithm.hmacParams()->hash(); |
| 65 if (algorithm.hmacKeyParams()) |
| 66 return algorithm.hmacKeyParams()->hash(); |
| 67 if (algorithm.rsaSsaParams()) |
| 68 return algorithm.rsaSsaParams()->hash(); |
| 69 if (algorithm.rsaOaepParams()) |
| 70 return algorithm.rsaOaepParams()->hash(); |
| 71 return blink::WebCryptoAlgorithm::createNull(); |
| 72 } |
| 73 |
| 74 blink::WebCryptoAlgorithm CreateAlgorithm(blink::WebCryptoAlgorithmId id) { |
| 75 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(id, NULL); |
| 76 } |
| 77 |
| 78 blink::WebCryptoAlgorithm CreateHmacAlgorithmByHashOutputLen( |
| 79 unsigned short hash_output_length_bits) { |
| 80 blink::WebCryptoAlgorithmId hash_id; |
| 81 switch (hash_output_length_bits) { |
| 82 case 160: |
| 83 hash_id = blink::WebCryptoAlgorithmIdSha1; |
| 84 break; |
| 85 case 224: |
| 86 hash_id = blink::WebCryptoAlgorithmIdSha224; |
| 87 break; |
| 88 case 256: |
| 89 hash_id = blink::WebCryptoAlgorithmIdSha256; |
| 90 break; |
| 91 case 384: |
| 92 hash_id = blink::WebCryptoAlgorithmIdSha384; |
| 93 break; |
| 94 case 512: |
| 95 hash_id = blink::WebCryptoAlgorithmIdSha512; |
| 96 break; |
| 97 default: |
| 98 NOTREACHED(); |
| 99 return blink::WebCryptoAlgorithm::createNull(); |
| 100 } |
| 101 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 102 blink::WebCryptoAlgorithmIdHmac, |
| 103 new blink::WebCryptoHmacParams(CreateAlgorithm(hash_id))); |
| 104 } |
| 105 |
| 106 blink::WebCryptoAlgorithm CreateHmacAlgorithmByHashId( |
| 107 blink::WebCryptoAlgorithmId hash_id) { |
| 108 DCHECK(IsHashAlgorithm(hash_id)); |
| 109 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 110 blink::WebCryptoAlgorithmIdHmac, |
| 111 new blink::WebCryptoHmacParams(CreateAlgorithm(hash_id))); |
| 112 } |
| 113 |
| 114 blink::WebCryptoAlgorithm CreateHmacKeyGenAlgorithm( |
| 115 blink::WebCryptoAlgorithmId hash_id, |
| 116 unsigned key_length_bytes) { |
| 117 DCHECK(IsHashAlgorithm(hash_id)); |
| 118 // key_length_bytes == 0 means unspecified |
| 119 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 120 blink::WebCryptoAlgorithmIdHmac, |
| 121 new blink::WebCryptoHmacKeyParams( |
| 122 CreateAlgorithm(hash_id), (key_length_bytes != 0), key_length_bytes)); |
| 123 } |
| 124 |
| 125 blink::WebCryptoAlgorithm CreateRsaSsaAlgorithm( |
| 126 blink::WebCryptoAlgorithmId hash_id) { |
| 127 DCHECK(IsHashAlgorithm(hash_id)); |
| 128 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 129 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, |
| 130 new blink::WebCryptoRsaSsaParams(CreateAlgorithm(hash_id))); |
| 131 } |
| 132 |
| 133 blink::WebCryptoAlgorithm CreateRsaOaepAlgorithm( |
| 134 blink::WebCryptoAlgorithmId hash_id) { |
| 135 DCHECK(IsHashAlgorithm(hash_id)); |
| 136 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 137 blink::WebCryptoAlgorithmIdRsaOaep, |
| 138 new blink::WebCryptoRsaOaepParams( |
| 139 CreateAlgorithm(hash_id), false, NULL, 0)); |
| 140 } |
| 141 |
| 142 blink::WebCryptoAlgorithm CreateRsaKeyGenAlgorithm( |
| 143 blink::WebCryptoAlgorithmId algorithm_id, |
| 144 unsigned modulus_length, |
| 145 const std::vector<uint8>& public_exponent) { |
| 146 DCHECK(algorithm_id == blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5 || |
| 147 algorithm_id == blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 || |
| 148 algorithm_id == blink::WebCryptoAlgorithmIdRsaOaep); |
| 149 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 150 algorithm_id, |
| 151 new blink::WebCryptoRsaKeyGenParams( |
| 152 modulus_length, |
| 153 webcrypto::Uint8VectorStart(public_exponent), |
| 154 public_exponent.size())); |
| 155 } |
| 156 |
| 157 blink::WebCryptoAlgorithm CreateAesCbcAlgorithm(const std::vector<uint8>& iv) { |
| 158 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 159 blink::WebCryptoAlgorithmIdAesCbc, |
| 160 new blink::WebCryptoAesCbcParams(Uint8VectorStart(iv), iv.size())); |
| 161 } |
| 162 |
| 163 blink::WebCryptoAlgorithm CreateAesGcmAlgorithm( |
| 164 const std::vector<uint8>& iv, |
| 165 const std::vector<uint8>& additional_data, |
| 166 uint8 tag_length_bytes) { |
| 167 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 168 blink::WebCryptoAlgorithmIdAesCbc, |
| 169 new blink::WebCryptoAesGcmParams(Uint8VectorStart(iv), |
| 170 iv.size(), |
| 171 additional_data.size() != 0, |
| 172 Uint8VectorStart(additional_data), |
| 173 additional_data.size(), |
| 174 tag_length_bytes != 0, |
| 175 tag_length_bytes)); |
| 176 } |
| 177 |
| 178 blink::WebCryptoAlgorithm CreateAesCbcKeyGenAlgorithm( |
| 179 unsigned short key_length_bits) { |
| 180 return CreateAesKeyGenAlgorithm(blink::WebCryptoAlgorithmIdAesCbc, |
| 181 key_length_bits); |
| 182 } |
| 183 |
| 184 blink::WebCryptoAlgorithm CreateAesGcmKeyGenAlgorithm( |
| 185 unsigned short key_length_bits) { |
| 186 return CreateAesKeyGenAlgorithm(blink::WebCryptoAlgorithmIdAesGcm, |
| 187 key_length_bits); |
| 188 } |
| 189 |
| 190 } // namespace webcrypto |
| 191 |
| 192 } // namespace content |
OLD | NEW |