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 <cryptohi.h> | 5 #include <cryptohi.h> |
6 #include <pk11pub.h> | 6 #include <pk11pub.h> |
7 #include <secerr.h> | 7 #include <secerr.h> |
8 #include <sechash.h> | 8 #include <sechash.h> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 return true; | 48 return true; |
49 default: | 49 default: |
50 return false; | 50 return false; |
51 } | 51 } |
52 } | 52 } |
53 | 53 |
54 class HmacImplementation : public AlgorithmImplementation { | 54 class HmacImplementation : public AlgorithmImplementation { |
55 public: | 55 public: |
56 HmacImplementation() {} | 56 HmacImplementation() {} |
57 | 57 |
58 virtual Status GenerateSecretKey(const blink::WebCryptoAlgorithm& algorithm, | 58 virtual Status GenerateKey(const blink::WebCryptoAlgorithm& algorithm, |
59 bool extractable, | 59 bool extractable, |
60 blink::WebCryptoKeyUsageMask usage_mask, | 60 blink::WebCryptoKeyUsageMask usage_mask, |
61 blink::WebCryptoKey* key) const override { | 61 GenerateKeyResult* result) const override { |
| 62 Status status = CheckKeyCreationUsages(kAllKeyUsages, usage_mask); |
| 63 if (status.IsError()) |
| 64 return status; |
| 65 |
62 const blink::WebCryptoHmacKeyGenParams* params = | 66 const blink::WebCryptoHmacKeyGenParams* params = |
63 algorithm.hmacKeyGenParams(); | 67 algorithm.hmacKeyGenParams(); |
64 | 68 |
65 const blink::WebCryptoAlgorithm& hash = params->hash(); | 69 const blink::WebCryptoAlgorithm& hash = params->hash(); |
66 CK_MECHANISM_TYPE mechanism = CKM_INVALID_MECHANISM; | 70 CK_MECHANISM_TYPE mechanism = CKM_INVALID_MECHANISM; |
67 if (!WebCryptoHashToHMACMechanism(hash, &mechanism)) | 71 if (!WebCryptoHashToHMACMechanism(hash, &mechanism)) |
68 return Status::ErrorUnsupported(); | 72 return Status::ErrorUnsupported(); |
69 | 73 |
70 unsigned int keylen_bits = 0; | 74 unsigned int keylen_bits = 0; |
71 Status status = GetHmacKeyGenLengthInBits(params, &keylen_bits); | 75 status = GetHmacKeyGenLengthInBits(params, &keylen_bits); |
72 if (status.IsError()) | 76 if (status.IsError()) |
73 return status; | 77 return status; |
74 | 78 |
75 return GenerateSecretKeyNss( | 79 return GenerateSecretKeyNss( |
76 blink::WebCryptoKeyAlgorithm::createHmac(hash.id(), keylen_bits), | 80 blink::WebCryptoKeyAlgorithm::createHmac(hash.id(), keylen_bits), |
77 extractable, | 81 extractable, |
78 usage_mask, | 82 usage_mask, |
79 keylen_bits / 8, | 83 keylen_bits / 8, |
80 mechanism, | 84 mechanism, |
81 key); | 85 result); |
82 } | 86 } |
83 | 87 |
84 virtual Status VerifyKeyUsagesBeforeImportKey( | 88 virtual Status VerifyKeyUsagesBeforeImportKey( |
85 blink::WebCryptoKeyFormat format, | 89 blink::WebCryptoKeyFormat format, |
86 blink::WebCryptoKeyUsageMask usage_mask) const override { | 90 blink::WebCryptoKeyUsageMask usage_mask) const override { |
87 switch (format) { | 91 switch (format) { |
88 case blink::WebCryptoKeyFormatRaw: | 92 case blink::WebCryptoKeyFormatRaw: |
89 case blink::WebCryptoKeyFormatJwk: | 93 case blink::WebCryptoKeyFormatJwk: |
90 return CheckKeyCreationUsages(kAllKeyUsages, usage_mask); | 94 return CheckKeyCreationUsages(kAllKeyUsages, usage_mask); |
91 default: | 95 default: |
92 return Status::ErrorUnsupportedImportKeyFormat(); | 96 return Status::ErrorUnsupportedImportKeyFormat(); |
93 } | 97 } |
94 } | 98 } |
95 | 99 |
96 virtual Status VerifyKeyUsagesBeforeGenerateKey( | |
97 blink::WebCryptoKeyUsageMask usage_mask) const override { | |
98 return CheckKeyCreationUsages(kAllKeyUsages, usage_mask); | |
99 } | |
100 | |
101 virtual Status ImportKeyRaw(const CryptoData& key_data, | 100 virtual Status ImportKeyRaw(const CryptoData& key_data, |
102 const blink::WebCryptoAlgorithm& algorithm, | 101 const blink::WebCryptoAlgorithm& algorithm, |
103 bool extractable, | 102 bool extractable, |
104 blink::WebCryptoKeyUsageMask usage_mask, | 103 blink::WebCryptoKeyUsageMask usage_mask, |
105 blink::WebCryptoKey* key) const override { | 104 blink::WebCryptoKey* key) const override { |
106 const blink::WebCryptoAlgorithm& hash = | 105 const blink::WebCryptoAlgorithm& hash = |
107 algorithm.hmacImportParams()->hash(); | 106 algorithm.hmacImportParams()->hash(); |
108 | 107 |
109 CK_MECHANISM_TYPE mechanism = CKM_INVALID_MECHANISM; | 108 CK_MECHANISM_TYPE mechanism = CKM_INVALID_MECHANISM; |
110 if (!WebCryptoHashToHMACMechanism(hash, &mechanism)) | 109 if (!WebCryptoHashToHMACMechanism(hash, &mechanism)) |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 | 231 |
233 } // namespace | 232 } // namespace |
234 | 233 |
235 AlgorithmImplementation* CreatePlatformHmacImplementation() { | 234 AlgorithmImplementation* CreatePlatformHmacImplementation() { |
236 return new HmacImplementation; | 235 return new HmacImplementation; |
237 } | 236 } |
238 | 237 |
239 } // namespace webcrypto | 238 } // namespace webcrypto |
240 | 239 |
241 } // namespace content | 240 } // namespace content |
OLD | NEW |