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