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 <openssl/hmac.h> | 5 #include <openssl/hmac.h> |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/numerics/safe_math.h" | 8 #include "base/numerics/safe_math.h" |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "content/child/webcrypto/algorithm_implementation.h" | 10 #include "content/child/webcrypto/algorithm_implementation.h" |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 | 65 |
66 return Status::Success(); | 66 return Status::Success(); |
67 } | 67 } |
68 | 68 |
69 class HmacImplementation : public AlgorithmImplementation { | 69 class HmacImplementation : public AlgorithmImplementation { |
70 public: | 70 public: |
71 HmacImplementation() {} | 71 HmacImplementation() {} |
72 | 72 |
73 Status GenerateKey(const blink::WebCryptoAlgorithm& algorithm, | 73 Status GenerateKey(const blink::WebCryptoAlgorithm& algorithm, |
74 bool extractable, | 74 bool extractable, |
75 blink::WebCryptoKeyUsageMask usage_mask, | 75 blink::WebCryptoKeyUsageMask usages, |
76 GenerateKeyResult* result) const override { | 76 GenerateKeyResult* result) const override { |
77 Status status = CheckKeyCreationUsages(kAllKeyUsages, usage_mask); | 77 Status status = CheckKeyCreationUsages(kAllKeyUsages, usages); |
78 if (status.IsError()) | 78 if (status.IsError()) |
79 return status; | 79 return status; |
80 | 80 |
81 const blink::WebCryptoHmacKeyGenParams* params = | 81 const blink::WebCryptoHmacKeyGenParams* params = |
82 algorithm.hmacKeyGenParams(); | 82 algorithm.hmacKeyGenParams(); |
83 | 83 |
84 unsigned int keylen_bits = 0; | 84 unsigned int keylen_bits = 0; |
85 status = GetHmacKeyGenLengthInBits(params, &keylen_bits); | 85 status = GetHmacKeyGenLengthInBits(params, &keylen_bits); |
86 if (status.IsError()) | 86 if (status.IsError()) |
87 return status; | 87 return status; |
88 | 88 |
89 return GenerateSecretKeyOpenSsl(blink::WebCryptoKeyAlgorithm::createHmac( | 89 return GenerateSecretKeyOpenSsl(blink::WebCryptoKeyAlgorithm::createHmac( |
90 params->hash().id(), keylen_bits), | 90 params->hash().id(), keylen_bits), |
91 extractable, | 91 extractable, |
92 usage_mask, | 92 usages, |
93 keylen_bits / 8, | 93 keylen_bits / 8, |
94 result); | 94 result); |
95 } | 95 } |
96 | 96 |
97 Status VerifyKeyUsagesBeforeImportKey( | 97 Status VerifyKeyUsagesBeforeImportKey( |
98 blink::WebCryptoKeyFormat format, | 98 blink::WebCryptoKeyFormat format, |
99 blink::WebCryptoKeyUsageMask usage_mask) const override { | 99 blink::WebCryptoKeyUsageMask usages) const override { |
100 switch (format) { | 100 switch (format) { |
101 case blink::WebCryptoKeyFormatRaw: | 101 case blink::WebCryptoKeyFormatRaw: |
102 case blink::WebCryptoKeyFormatJwk: | 102 case blink::WebCryptoKeyFormatJwk: |
103 return CheckKeyCreationUsages(kAllKeyUsages, usage_mask); | 103 return CheckKeyCreationUsages(kAllKeyUsages, usages); |
104 default: | 104 default: |
105 return Status::ErrorUnsupportedImportKeyFormat(); | 105 return Status::ErrorUnsupportedImportKeyFormat(); |
106 } | 106 } |
107 } | 107 } |
108 | 108 |
109 Status ImportKeyRaw(const CryptoData& key_data, | 109 Status ImportKeyRaw(const CryptoData& key_data, |
110 const blink::WebCryptoAlgorithm& algorithm, | 110 const blink::WebCryptoAlgorithm& algorithm, |
111 bool extractable, | 111 bool extractable, |
112 blink::WebCryptoKeyUsageMask usage_mask, | 112 blink::WebCryptoKeyUsageMask usages, |
113 blink::WebCryptoKey* key) const override { | 113 blink::WebCryptoKey* key) const override { |
114 const blink::WebCryptoAlgorithm& hash = | 114 const blink::WebCryptoAlgorithm& hash = |
115 algorithm.hmacImportParams()->hash(); | 115 algorithm.hmacImportParams()->hash(); |
116 | 116 |
117 base::CheckedNumeric<unsigned int> keylen_bits(key_data.byte_length()); | 117 base::CheckedNumeric<unsigned int> keylen_bits(key_data.byte_length()); |
118 keylen_bits *= 8; | 118 keylen_bits *= 8; |
119 | 119 |
120 if (!keylen_bits.IsValid()) | 120 if (!keylen_bits.IsValid()) |
121 return Status::ErrorDataTooLarge(); | 121 return Status::ErrorDataTooLarge(); |
122 | 122 |
123 return ImportKeyRawOpenSsl(key_data, | 123 return ImportKeyRawOpenSsl(key_data, |
124 blink::WebCryptoKeyAlgorithm::createHmac( | 124 blink::WebCryptoKeyAlgorithm::createHmac( |
125 hash.id(), keylen_bits.ValueOrDie()), | 125 hash.id(), keylen_bits.ValueOrDie()), |
126 extractable, | 126 extractable, |
127 usage_mask, | 127 usages, |
128 key); | 128 key); |
129 } | 129 } |
130 | 130 |
131 Status ImportKeyJwk(const CryptoData& key_data, | 131 Status ImportKeyJwk(const CryptoData& key_data, |
132 const blink::WebCryptoAlgorithm& algorithm, | 132 const blink::WebCryptoAlgorithm& algorithm, |
133 bool extractable, | 133 bool extractable, |
134 blink::WebCryptoKeyUsageMask usage_mask, | 134 blink::WebCryptoKeyUsageMask usages, |
135 blink::WebCryptoKey* key) const override { | 135 blink::WebCryptoKey* key) const override { |
136 const char* algorithm_name = | 136 const char* algorithm_name = |
137 GetJwkHmacAlgorithmName(algorithm.hmacImportParams()->hash().id()); | 137 GetJwkHmacAlgorithmName(algorithm.hmacImportParams()->hash().id()); |
138 if (!algorithm_name) | 138 if (!algorithm_name) |
139 return Status::ErrorUnexpected(); | 139 return Status::ErrorUnexpected(); |
140 | 140 |
141 std::vector<uint8_t> raw_data; | 141 std::vector<uint8_t> raw_data; |
142 Status status = ReadSecretKeyJwk( | 142 Status status = ReadSecretKeyJwk( |
143 key_data, algorithm_name, extractable, usage_mask, &raw_data); | 143 key_data, algorithm_name, extractable, usages, &raw_data); |
144 if (status.IsError()) | 144 if (status.IsError()) |
145 return status; | 145 return status; |
146 | 146 |
147 return ImportKeyRaw( | 147 return ImportKeyRaw( |
148 CryptoData(raw_data), algorithm, extractable, usage_mask, key); | 148 CryptoData(raw_data), algorithm, extractable, usages, key); |
149 } | 149 } |
150 | 150 |
151 Status ExportKeyRaw(const blink::WebCryptoKey& key, | 151 Status ExportKeyRaw(const blink::WebCryptoKey& key, |
152 std::vector<uint8_t>* buffer) const override { | 152 std::vector<uint8_t>* buffer) const override { |
153 *buffer = SymKeyOpenSsl::Cast(key)->raw_key_data(); | 153 *buffer = SymKeyOpenSsl::Cast(key)->raw_key_data(); |
154 return Status::Success(); | 154 return Status::Success(); |
155 } | 155 } |
156 | 156 |
157 Status ExportKeyJwk(const blink::WebCryptoKey& key, | 157 Status ExportKeyJwk(const blink::WebCryptoKey& key, |
158 std::vector<uint8_t>* buffer) const override { | 158 std::vector<uint8_t>* buffer) const override { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 | 207 |
208 } // namespace | 208 } // namespace |
209 | 209 |
210 AlgorithmImplementation* CreatePlatformHmacImplementation() { | 210 AlgorithmImplementation* CreatePlatformHmacImplementation() { |
211 return new HmacImplementation; | 211 return new HmacImplementation; |
212 } | 212 } |
213 | 213 |
214 } // namespace webcrypto | 214 } // namespace webcrypto |
215 | 215 |
216 } // namespace content | 216 } // namespace content |
OLD | NEW |