| 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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 GenerateKey(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 usages, |
| 61 GenerateKeyResult* result) const override { | 61 GenerateKeyResult* result) const override { |
| 62 Status status = CheckKeyCreationUsages(kAllKeyUsages, usage_mask); | 62 Status status = CheckKeyCreationUsages(kAllKeyUsages, usages); |
| 63 if (status.IsError()) | 63 if (status.IsError()) |
| 64 return status; | 64 return status; |
| 65 | 65 |
| 66 const blink::WebCryptoHmacKeyGenParams* params = | 66 const blink::WebCryptoHmacKeyGenParams* params = |
| 67 algorithm.hmacKeyGenParams(); | 67 algorithm.hmacKeyGenParams(); |
| 68 | 68 |
| 69 const blink::WebCryptoAlgorithm& hash = params->hash(); | 69 const blink::WebCryptoAlgorithm& hash = params->hash(); |
| 70 CK_MECHANISM_TYPE mechanism = CKM_INVALID_MECHANISM; | 70 CK_MECHANISM_TYPE mechanism = CKM_INVALID_MECHANISM; |
| 71 if (!WebCryptoHashToHMACMechanism(hash, &mechanism)) | 71 if (!WebCryptoHashToHMACMechanism(hash, &mechanism)) |
| 72 return Status::ErrorUnsupported(); | 72 return Status::ErrorUnsupported(); |
| 73 | 73 |
| 74 unsigned int keylen_bits = 0; | 74 unsigned int keylen_bits = 0; |
| 75 status = GetHmacKeyGenLengthInBits(params, &keylen_bits); | 75 status = GetHmacKeyGenLengthInBits(params, &keylen_bits); |
| 76 if (status.IsError()) | 76 if (status.IsError()) |
| 77 return status; | 77 return status; |
| 78 | 78 |
| 79 return GenerateSecretKeyNss( | 79 return GenerateSecretKeyNss( |
| 80 blink::WebCryptoKeyAlgorithm::createHmac(hash.id(), keylen_bits), | 80 blink::WebCryptoKeyAlgorithm::createHmac(hash.id(), keylen_bits), |
| 81 extractable, | 81 extractable, |
| 82 usage_mask, | 82 usages, |
| 83 keylen_bits / 8, | 83 keylen_bits / 8, |
| 84 mechanism, | 84 mechanism, |
| 85 result); | 85 result); |
| 86 } | 86 } |
| 87 | 87 |
| 88 virtual Status VerifyKeyUsagesBeforeImportKey( | 88 virtual Status VerifyKeyUsagesBeforeImportKey( |
| 89 blink::WebCryptoKeyFormat format, | 89 blink::WebCryptoKeyFormat format, |
| 90 blink::WebCryptoKeyUsageMask usage_mask) const override { | 90 blink::WebCryptoKeyUsageMask usages) const override { |
| 91 switch (format) { | 91 switch (format) { |
| 92 case blink::WebCryptoKeyFormatRaw: | 92 case blink::WebCryptoKeyFormatRaw: |
| 93 case blink::WebCryptoKeyFormatJwk: | 93 case blink::WebCryptoKeyFormatJwk: |
| 94 return CheckKeyCreationUsages(kAllKeyUsages, usage_mask); | 94 return CheckKeyCreationUsages(kAllKeyUsages, usages); |
| 95 default: | 95 default: |
| 96 return Status::ErrorUnsupportedImportKeyFormat(); | 96 return Status::ErrorUnsupportedImportKeyFormat(); |
| 97 } | 97 } |
| 98 } | 98 } |
| 99 | 99 |
| 100 virtual Status ImportKeyRaw(const CryptoData& key_data, | 100 virtual Status ImportKeyRaw(const CryptoData& key_data, |
| 101 const blink::WebCryptoAlgorithm& algorithm, | 101 const blink::WebCryptoAlgorithm& algorithm, |
| 102 bool extractable, | 102 bool extractable, |
| 103 blink::WebCryptoKeyUsageMask usage_mask, | 103 blink::WebCryptoKeyUsageMask usages, |
| 104 blink::WebCryptoKey* key) const override { | 104 blink::WebCryptoKey* key) const override { |
| 105 const blink::WebCryptoAlgorithm& hash = | 105 const blink::WebCryptoAlgorithm& hash = |
| 106 algorithm.hmacImportParams()->hash(); | 106 algorithm.hmacImportParams()->hash(); |
| 107 | 107 |
| 108 CK_MECHANISM_TYPE mechanism = CKM_INVALID_MECHANISM; | 108 CK_MECHANISM_TYPE mechanism = CKM_INVALID_MECHANISM; |
| 109 if (!WebCryptoHashToHMACMechanism(hash, &mechanism)) | 109 if (!WebCryptoHashToHMACMechanism(hash, &mechanism)) |
| 110 return Status::ErrorUnsupported(); | 110 return Status::ErrorUnsupported(); |
| 111 | 111 |
| 112 base::CheckedNumeric<unsigned int> keylen_bits(key_data.byte_length()); | 112 base::CheckedNumeric<unsigned int> keylen_bits(key_data.byte_length()); |
| 113 keylen_bits *= 8; | 113 keylen_bits *= 8; |
| 114 | 114 |
| 115 if (!keylen_bits.IsValid()) | 115 if (!keylen_bits.IsValid()) |
| 116 return Status::ErrorDataTooLarge(); | 116 return Status::ErrorDataTooLarge(); |
| 117 | 117 |
| 118 return ImportKeyRawNss(key_data, | 118 return ImportKeyRawNss(key_data, |
| 119 blink::WebCryptoKeyAlgorithm::createHmac( | 119 blink::WebCryptoKeyAlgorithm::createHmac( |
| 120 hash.id(), keylen_bits.ValueOrDie()), | 120 hash.id(), keylen_bits.ValueOrDie()), |
| 121 extractable, | 121 extractable, |
| 122 usage_mask, | 122 usages, |
| 123 mechanism, | 123 mechanism, |
| 124 CKF_SIGN | CKF_VERIFY, | 124 CKF_SIGN | CKF_VERIFY, |
| 125 key); | 125 key); |
| 126 } | 126 } |
| 127 | 127 |
| 128 virtual Status ImportKeyJwk(const CryptoData& key_data, | 128 virtual Status ImportKeyJwk(const CryptoData& key_data, |
| 129 const blink::WebCryptoAlgorithm& algorithm, | 129 const blink::WebCryptoAlgorithm& algorithm, |
| 130 bool extractable, | 130 bool extractable, |
| 131 blink::WebCryptoKeyUsageMask usage_mask, | 131 blink::WebCryptoKeyUsageMask usages, |
| 132 blink::WebCryptoKey* key) const override { | 132 blink::WebCryptoKey* key) const override { |
| 133 const char* algorithm_name = | 133 const char* algorithm_name = |
| 134 GetJwkHmacAlgorithmName(algorithm.hmacImportParams()->hash().id()); | 134 GetJwkHmacAlgorithmName(algorithm.hmacImportParams()->hash().id()); |
| 135 if (!algorithm_name) | 135 if (!algorithm_name) |
| 136 return Status::ErrorUnexpected(); | 136 return Status::ErrorUnexpected(); |
| 137 | 137 |
| 138 std::vector<uint8_t> raw_data; | 138 std::vector<uint8_t> raw_data; |
| 139 Status status = ReadSecretKeyJwk( | 139 Status status = ReadSecretKeyJwk( |
| 140 key_data, algorithm_name, extractable, usage_mask, &raw_data); | 140 key_data, algorithm_name, extractable, usages, &raw_data); |
| 141 if (status.IsError()) | 141 if (status.IsError()) |
| 142 return status; | 142 return status; |
| 143 | 143 |
| 144 return ImportKeyRaw( | 144 return ImportKeyRaw( |
| 145 CryptoData(raw_data), algorithm, extractable, usage_mask, key); | 145 CryptoData(raw_data), algorithm, extractable, usages, key); |
| 146 } | 146 } |
| 147 | 147 |
| 148 virtual Status ExportKeyRaw(const blink::WebCryptoKey& key, | 148 virtual Status ExportKeyRaw(const blink::WebCryptoKey& key, |
| 149 std::vector<uint8_t>* buffer) const override { | 149 std::vector<uint8_t>* buffer) const override { |
| 150 *buffer = SymKeyNss::Cast(key)->raw_key_data(); | 150 *buffer = SymKeyNss::Cast(key)->raw_key_data(); |
| 151 return Status::Success(); | 151 return Status::Success(); |
| 152 } | 152 } |
| 153 | 153 |
| 154 virtual Status ExportKeyJwk(const blink::WebCryptoKey& key, | 154 virtual Status ExportKeyJwk(const blink::WebCryptoKey& key, |
| 155 std::vector<uint8_t>* buffer) const override { | 155 std::vector<uint8_t>* buffer) const override { |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 | 231 |
| 232 } // namespace | 232 } // namespace |
| 233 | 233 |
| 234 AlgorithmImplementation* CreatePlatformHmacImplementation() { | 234 AlgorithmImplementation* CreatePlatformHmacImplementation() { |
| 235 return new HmacImplementation; | 235 return new HmacImplementation; |
| 236 } | 236 } |
| 237 | 237 |
| 238 } // namespace webcrypto | 238 } // namespace webcrypto |
| 239 | 239 |
| 240 } // namespace content | 240 } // namespace content |
| OLD | NEW |