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