OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/webcrypto/algorithms/util_openssl.h" | 5 #include "components/webcrypto/algorithms/util_openssl.h" |
6 | 6 |
7 #include <openssl/evp.h> | 7 #include <openssl/evp.h> |
8 #include <openssl/pkcs12.h> | 8 #include <openssl/pkcs12.h> |
9 #include <openssl/rand.h> | 9 #include <openssl/rand.h> |
10 | 10 |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 data.byte_length(), additional_data.bytes(), | 120 data.byte_length(), additional_data.bytes(), |
121 additional_data.byte_length()); | 121 additional_data.byte_length()); |
122 } | 122 } |
123 | 123 |
124 if (!ok) | 124 if (!ok) |
125 return Status::OperationError(); | 125 return Status::OperationError(); |
126 buffer->resize(len); | 126 buffer->resize(len); |
127 return Status::Success(); | 127 return Status::Success(); |
128 } | 128 } |
129 | 129 |
130 Status GenerateWebCryptoSecretKey(const blink::WebCryptoKeyAlgorithm& algorithm, | |
131 bool extractable, | |
132 blink::WebCryptoKeyUsageMask usages, | |
133 unsigned int keylen_bits, | |
134 GenerateKeyResult* result) { | |
135 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | |
136 | |
137 unsigned int keylen_bytes = NumBitsToBytes(keylen_bits); | |
138 std::vector<unsigned char> random_bytes(keylen_bytes, 0); | |
139 | |
140 if (keylen_bytes > 0) { | |
141 if (!(RAND_bytes(&random_bytes[0], keylen_bytes))) | |
142 return Status::OperationError(); | |
143 TruncateToBitLength(keylen_bits, &random_bytes); | |
144 } | |
145 | |
146 result->AssignSecretKey(blink::WebCryptoKey::create( | |
147 CreateSymmetricKeyHandle(CryptoData(random_bytes)), | |
148 blink::WebCryptoKeyTypeSecret, extractable, algorithm, usages)); | |
149 | |
150 return Status::Success(); | |
151 } | |
152 | |
153 Status CreateWebCryptoSecretKey(const CryptoData& key_data, | |
154 const blink::WebCryptoKeyAlgorithm& algorithm, | |
155 bool extractable, | |
156 blink::WebCryptoKeyUsageMask usages, | |
157 blink::WebCryptoKey* key) { | |
158 *key = blink::WebCryptoKey::create(CreateSymmetricKeyHandle(key_data), | |
159 blink::WebCryptoKeyTypeSecret, extractable, | |
160 algorithm, usages); | |
161 return Status::Success(); | |
162 } | |
163 | |
164 Status CreateWebCryptoPublicKey(crypto::ScopedEVP_PKEY public_key, | 130 Status CreateWebCryptoPublicKey(crypto::ScopedEVP_PKEY public_key, |
165 const blink::WebCryptoKeyAlgorithm& algorithm, | 131 const blink::WebCryptoKeyAlgorithm& algorithm, |
166 bool extractable, | 132 bool extractable, |
167 blink::WebCryptoKeyUsageMask usages, | 133 blink::WebCryptoKeyUsageMask usages, |
168 blink::WebCryptoKey* key) { | 134 blink::WebCryptoKey* key) { |
169 // Serialize the key at creation time so that if structured cloning is | 135 // Serialize the key at creation time so that if structured cloning is |
170 // requested it can be done synchronously from the Blink thread. | 136 // requested it can be done synchronously from the Blink thread. |
171 std::vector<uint8_t> spki_data; | 137 std::vector<uint8_t> spki_data; |
172 Status status = ExportPKeySpki(public_key.get(), &spki_data); | 138 Status status = ExportPKeySpki(public_key.get(), &spki_data); |
173 if (status.IsError()) | 139 if (status.IsError()) |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 return BN_bin2bn(reinterpret_cast<const uint8_t*>(n.data()), n.size(), NULL); | 204 return BN_bin2bn(reinterpret_cast<const uint8_t*>(n.data()), n.size(), NULL); |
239 } | 205 } |
240 | 206 |
241 std::vector<uint8_t> BIGNUMToVector(const BIGNUM* n) { | 207 std::vector<uint8_t> BIGNUMToVector(const BIGNUM* n) { |
242 std::vector<uint8_t> v(BN_num_bytes(n)); | 208 std::vector<uint8_t> v(BN_num_bytes(n)); |
243 BN_bn2bin(n, vector_as_array(&v)); | 209 BN_bn2bin(n, vector_as_array(&v)); |
244 return v; | 210 return v; |
245 } | 211 } |
246 | 212 |
247 } // namespace webcrypto | 213 } // namespace webcrypto |
OLD | NEW |