OLD | NEW |
(Empty) | |
| 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 #include "content/child/webcrypto/openssl/aes_key_openssl.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "content/child/webcrypto/crypto_data.h" |
| 9 #include "content/child/webcrypto/jwk.h" |
| 10 #include "content/child/webcrypto/openssl/key_openssl.h" |
| 11 #include "content/child/webcrypto/openssl/sym_key_openssl.h" |
| 12 #include "content/child/webcrypto/status.h" |
| 13 #include "content/child/webcrypto/webcrypto_util.h" |
| 14 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" |
| 15 |
| 16 namespace content { |
| 17 |
| 18 namespace webcrypto { |
| 19 |
| 20 AesAlgorithm::AesAlgorithm(blink::WebCryptoKeyUsageMask all_key_usages, |
| 21 const std::string& jwk_suffix) |
| 22 : all_key_usages_(all_key_usages), jwk_suffix_(jwk_suffix) { |
| 23 } |
| 24 |
| 25 AesAlgorithm::AesAlgorithm(const std::string& jwk_suffix) |
| 26 : all_key_usages_(blink::WebCryptoKeyUsageEncrypt | |
| 27 blink::WebCryptoKeyUsageDecrypt | |
| 28 blink::WebCryptoKeyUsageWrapKey | |
| 29 blink::WebCryptoKeyUsageUnwrapKey), |
| 30 jwk_suffix_(jwk_suffix) { |
| 31 } |
| 32 |
| 33 Status AesAlgorithm::VerifyKeyUsagesBeforeGenerateKey( |
| 34 blink::WebCryptoKeyUsageMask usage_mask) const { |
| 35 return CheckKeyCreationUsages(all_key_usages_, usage_mask); |
| 36 } |
| 37 |
| 38 Status AesAlgorithm::GenerateSecretKey( |
| 39 const blink::WebCryptoAlgorithm& algorithm, |
| 40 bool extractable, |
| 41 blink::WebCryptoKeyUsageMask usage_mask, |
| 42 blink::WebCryptoKey* key) const { |
| 43 unsigned int keylen_bits; |
| 44 Status status = GetAesKeyGenLength(algorithm.aesKeyGenParams(), &keylen_bits); |
| 45 if (status.IsError()) |
| 46 return status; |
| 47 |
| 48 return GenerateSecretKeyOpenSsl( |
| 49 blink::WebCryptoKeyAlgorithm::createAes(algorithm.id(), keylen_bits), |
| 50 extractable, |
| 51 usage_mask, |
| 52 keylen_bits / 8, |
| 53 key); |
| 54 } |
| 55 |
| 56 Status AesAlgorithm::VerifyKeyUsagesBeforeImportKey( |
| 57 blink::WebCryptoKeyFormat format, |
| 58 blink::WebCryptoKeyUsageMask usage_mask) const { |
| 59 switch (format) { |
| 60 case blink::WebCryptoKeyFormatRaw: |
| 61 case blink::WebCryptoKeyFormatJwk: |
| 62 return CheckKeyCreationUsages(all_key_usages_, usage_mask); |
| 63 default: |
| 64 return Status::ErrorUnsupportedImportKeyFormat(); |
| 65 } |
| 66 } |
| 67 |
| 68 Status AesAlgorithm::ImportKeyRaw(const CryptoData& key_data, |
| 69 const blink::WebCryptoAlgorithm& algorithm, |
| 70 bool extractable, |
| 71 blink::WebCryptoKeyUsageMask usage_mask, |
| 72 blink::WebCryptoKey* key) const { |
| 73 const unsigned int keylen_bytes = key_data.byte_length(); |
| 74 Status status = VerifyAesKeyLengthForImport(keylen_bytes); |
| 75 if (status.IsError()) |
| 76 return status; |
| 77 |
| 78 // No possibility of overflow. |
| 79 unsigned int keylen_bits = keylen_bytes * 8; |
| 80 |
| 81 return ImportKeyRawOpenSsl( |
| 82 key_data, |
| 83 blink::WebCryptoKeyAlgorithm::createAes(algorithm.id(), keylen_bits), |
| 84 extractable, |
| 85 usage_mask, |
| 86 key); |
| 87 } |
| 88 |
| 89 Status AesAlgorithm::ImportKeyJwk(const CryptoData& key_data, |
| 90 const blink::WebCryptoAlgorithm& algorithm, |
| 91 bool extractable, |
| 92 blink::WebCryptoKeyUsageMask usage_mask, |
| 93 blink::WebCryptoKey* key) const { |
| 94 std::vector<uint8> raw_data; |
| 95 Status status = ReadAesSecretKeyJwk( |
| 96 key_data, jwk_suffix_, extractable, usage_mask, &raw_data); |
| 97 if (status.IsError()) |
| 98 return status; |
| 99 |
| 100 return ImportKeyRaw( |
| 101 CryptoData(raw_data), algorithm, extractable, usage_mask, key); |
| 102 } |
| 103 |
| 104 Status AesAlgorithm::ExportKeyRaw(const blink::WebCryptoKey& key, |
| 105 std::vector<uint8>* buffer) const { |
| 106 *buffer = SymKeyOpenSsl::Cast(key)->raw_key_data(); |
| 107 return Status::Success(); |
| 108 } |
| 109 |
| 110 Status AesAlgorithm::ExportKeyJwk(const blink::WebCryptoKey& key, |
| 111 std::vector<uint8>* buffer) const { |
| 112 const std::vector<uint8>& raw_data = SymKeyOpenSsl::Cast(key)->raw_key_data(); |
| 113 |
| 114 WriteSecretKeyJwk(CryptoData(raw_data), |
| 115 MakeJwkAesAlgorithmName(jwk_suffix_, raw_data.size()), |
| 116 key.extractable(), |
| 117 key.usages(), |
| 118 buffer); |
| 119 |
| 120 return Status::Success(); |
| 121 } |
| 122 |
| 123 } // namespace webcrypto |
| 124 |
| 125 } // namespace content |
OLD | NEW |