OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/renderer/webcrypto_impl.h" | 5 #include "content/renderer/webcrypto_impl.h" |
6 | 6 |
| 7 #include <pk11pub.h> |
7 #include <sechash.h> | 8 #include <sechash.h> |
8 | 9 |
9 #include "base/logging.h" | 10 #include "base/logging.h" |
10 #include "crypto/nss_util.h" | 11 #include "crypto/nss_util.h" |
| 12 #include "crypto/scoped_nss_types.h" |
11 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" | 13 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" |
12 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | 14 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
| 15 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
13 | 16 |
14 namespace content { | 17 namespace content { |
15 | 18 |
| 19 namespace { |
| 20 |
| 21 class SymKeyHandle : public WebKit::WebCryptoKeyHandle { |
| 22 public: |
| 23 explicit SymKeyHandle(crypto::ScopedPK11SymKey key) { |
| 24 DCHECK(!key_.get()); |
| 25 key_ = key.Pass(); |
| 26 } |
| 27 |
| 28 PK11SymKey* key() { return key_.get(); } |
| 29 |
| 30 private: |
| 31 crypto::ScopedPK11SymKey key_; |
| 32 |
| 33 DISALLOW_COPY_AND_ASSIGN(SymKeyHandle); |
| 34 }; |
| 35 |
| 36 HASH_HashType WebCryptoAlgorithmToNSSHashType( |
| 37 const WebKit::WebCryptoAlgorithm& algorithm) { |
| 38 switch (algorithm.id()) { |
| 39 case WebKit::WebCryptoAlgorithmIdSha1: |
| 40 return HASH_AlgSHA1; |
| 41 case WebKit::WebCryptoAlgorithmIdSha224: |
| 42 return HASH_AlgSHA224; |
| 43 case WebKit::WebCryptoAlgorithmIdSha256: |
| 44 return HASH_AlgSHA256; |
| 45 case WebKit::WebCryptoAlgorithmIdSha384: |
| 46 return HASH_AlgSHA384; |
| 47 case WebKit::WebCryptoAlgorithmIdSha512: |
| 48 return HASH_AlgSHA512; |
| 49 default: |
| 50 // Not a digest algorithm. |
| 51 return HASH_AlgNULL; |
| 52 } |
| 53 } |
| 54 |
| 55 CK_MECHANISM_TYPE WebCryptoAlgorithmToHMACMechanism( |
| 56 const WebKit::WebCryptoAlgorithm& algorithm) { |
| 57 switch (algorithm.id()) { |
| 58 case WebKit::WebCryptoAlgorithmIdSha1: |
| 59 return CKM_SHA_1_HMAC; |
| 60 case WebKit::WebCryptoAlgorithmIdSha256: |
| 61 return CKM_SHA256_HMAC; |
| 62 default: |
| 63 // Not a supported algorithm. |
| 64 return CKM_INVALID_MECHANISM; |
| 65 } |
| 66 } |
| 67 |
| 68 } // namespace |
| 69 |
16 void WebCryptoImpl::Init() { | 70 void WebCryptoImpl::Init() { |
17 crypto::EnsureNSSInit(); | 71 crypto::EnsureNSSInit(); |
18 } | 72 } |
19 | 73 |
20 bool WebCryptoImpl::DigestInternal( | 74 bool WebCryptoImpl::DigestInternal( |
21 const WebKit::WebCryptoAlgorithm& algorithm, | 75 const WebKit::WebCryptoAlgorithm& algorithm, |
22 const unsigned char* data, | 76 const unsigned char* data, |
23 unsigned data_size, | 77 unsigned data_size, |
24 WebKit::WebArrayBuffer* buffer) { | 78 WebKit::WebArrayBuffer* buffer) { |
25 HASH_HashType hash_type = HASH_AlgNULL; | 79 HASH_HashType hash_type = WebCryptoAlgorithmToNSSHashType(algorithm); |
26 | 80 if (hash_type == HASH_AlgNULL) { |
27 switch (algorithm.id()) { | 81 return false; |
28 case WebKit::WebCryptoAlgorithmIdSha1: | |
29 hash_type = HASH_AlgSHA1; | |
30 break; | |
31 case WebKit::WebCryptoAlgorithmIdSha224: | |
32 hash_type = HASH_AlgSHA224; | |
33 break; | |
34 case WebKit::WebCryptoAlgorithmIdSha256: | |
35 hash_type = HASH_AlgSHA256; | |
36 break; | |
37 case WebKit::WebCryptoAlgorithmIdSha384: | |
38 hash_type = HASH_AlgSHA384; | |
39 break; | |
40 case WebKit::WebCryptoAlgorithmIdSha512: | |
41 hash_type = HASH_AlgSHA512; | |
42 break; | |
43 default: | |
44 // Not a digest algorithm. | |
45 return false; | |
46 } | 82 } |
47 | 83 |
48 HASHContext* context = HASH_Create(hash_type); | 84 HASHContext* context = HASH_Create(hash_type); |
49 if (!context) { | 85 if (!context) { |
50 return false; | 86 return false; |
51 } | 87 } |
52 | 88 |
53 HASH_Begin(context); | 89 HASH_Begin(context); |
54 | 90 |
55 HASH_Update(context, data, data_size); | 91 HASH_Update(context, data, data_size); |
56 | 92 |
57 size_t hash_result_length = HASH_ResultLenContext(context); | 93 unsigned hash_result_length = HASH_ResultLenContext(context); |
58 DCHECK_LE(hash_result_length, static_cast<size_t>(HASH_LENGTH_MAX)); | 94 DCHECK_LE(hash_result_length, static_cast<size_t>(HASH_LENGTH_MAX)); |
59 | 95 |
60 *buffer = WebKit::WebArrayBuffer::create(hash_result_length, 1); | 96 *buffer = WebKit::WebArrayBuffer::create(hash_result_length, 1); |
61 | 97 |
62 unsigned char* digest = reinterpret_cast<unsigned char*>(buffer->data()); | 98 unsigned char* digest = reinterpret_cast<unsigned char*>(buffer->data()); |
63 | 99 |
64 uint32 result_length = 0; | 100 unsigned result_length = 0; |
65 HASH_End(context, digest, &result_length, hash_result_length); | 101 HASH_End(context, digest, &result_length, hash_result_length); |
66 | 102 |
67 HASH_Destroy(context); | 103 HASH_Destroy(context); |
68 | 104 |
69 return result_length == hash_result_length; | 105 return result_length == hash_result_length; |
70 } | 106 } |
71 | 107 |
| 108 bool WebCryptoImpl::ImportKeyInternal( |
| 109 WebKit::WebCryptoKeyFormat format, |
| 110 const unsigned char* key_data, |
| 111 unsigned key_data_size, |
| 112 const WebKit::WebCryptoAlgorithm& algorithm, |
| 113 WebKit::WebCryptoKeyUsageMask usage_mask, |
| 114 scoped_ptr<WebKit::WebCryptoKeyHandle>* handle, |
| 115 WebKit::WebCryptoKeyType* type) { |
| 116 switch (algorithm.id()) { |
| 117 case WebKit::WebCryptoAlgorithmIdHmac: |
| 118 *type = WebKit::WebCryptoKeyTypeSecret; |
| 119 break; |
| 120 // TODO(bryaneyler): Support more key types. |
| 121 default: |
| 122 return false; |
| 123 } |
| 124 |
| 125 // TODO(bryaneyler): Need to split handling for symmetric and asymmetric keys. |
| 126 // Currently only supporting symmetric. |
| 127 CK_MECHANISM_TYPE mechanism = CKM_INVALID_MECHANISM; |
| 128 // Flags are verified at the Blink layer; here the flags are set to all |
| 129 // possible operations for this key type. |
| 130 CK_FLAGS flags = 0; |
| 131 |
| 132 switch(algorithm.id()) { |
| 133 case WebKit::WebCryptoAlgorithmIdHmac: { |
| 134 const WebKit::WebCryptoHmacParams* params = algorithm.hmacParams(); |
| 135 if (!params) { |
| 136 return false; |
| 137 } |
| 138 |
| 139 mechanism = WebCryptoAlgorithmToHMACMechanism(params->hash()); |
| 140 if (mechanism == CKM_INVALID_MECHANISM) { |
| 141 return false; |
| 142 } |
| 143 |
| 144 flags |= CKF_SIGN | CKF_VERIFY; |
| 145 |
| 146 break; |
| 147 } |
| 148 default: |
| 149 return false; |
| 150 } |
| 151 |
| 152 DCHECK_NE(CKM_INVALID_MECHANISM, mechanism); |
| 153 DCHECK_NE(0ul, flags); |
| 154 |
| 155 SECItem key_item = { siBuffer, NULL, 0 }; |
| 156 |
| 157 switch (format) { |
| 158 case WebKit::WebCryptoKeyFormatRaw: |
| 159 key_item.data = const_cast<unsigned char*>(key_data); |
| 160 key_item.len = key_data_size; |
| 161 break; |
| 162 // TODO(bryaneyler): Handle additional formats. |
| 163 default: |
| 164 return false; |
| 165 } |
| 166 |
| 167 crypto::ScopedPK11SymKey pk11_sym_key( |
| 168 PK11_ImportSymKeyWithFlags(PK11_GetInternalSlot(), |
| 169 mechanism, |
| 170 PK11_OriginUnwrap, |
| 171 CKA_FLAGS_ONLY, |
| 172 &key_item, |
| 173 flags, |
| 174 false, |
| 175 NULL)); |
| 176 if (!pk11_sym_key.get()) { |
| 177 NOTREACHED(); |
| 178 return false; |
| 179 } |
| 180 |
| 181 scoped_ptr<SymKeyHandle> sym_key(new SymKeyHandle(pk11_sym_key.Pass())); |
| 182 *handle = sym_key.Pass(); |
| 183 |
| 184 return true; |
| 185 } |
| 186 |
| 187 bool WebCryptoImpl::SignInternal( |
| 188 const WebKit::WebCryptoAlgorithm& algorithm, |
| 189 const WebKit::WebCryptoKey& key, |
| 190 const unsigned char* data, |
| 191 unsigned data_size, |
| 192 WebKit::WebArrayBuffer* buffer) { |
| 193 WebKit::WebArrayBuffer result; |
| 194 |
| 195 switch (algorithm.id()) { |
| 196 case WebKit::WebCryptoAlgorithmIdHmac: { |
| 197 const WebKit::WebCryptoHmacParams* params = algorithm.hmacParams(); |
| 198 if (!params) { |
| 199 return false; |
| 200 } |
| 201 |
| 202 SymKeyHandle* sym_key = reinterpret_cast<SymKeyHandle*>(key.handle()); |
| 203 |
| 204 DCHECK_EQ(PK11_GetMechanism(sym_key->key()), |
| 205 WebCryptoAlgorithmToHMACMechanism(params->hash())); |
| 206 DCHECK_NE(0, key.usages() & WebKit::WebCryptoKeyUsageSign); |
| 207 |
| 208 SECItem param_item = { siBuffer, NULL, 0 }; |
| 209 SECItem data_item = { |
| 210 siBuffer, |
| 211 const_cast<unsigned char*>(data), |
| 212 data_size |
| 213 }; |
| 214 // First call is to figure out the length. |
| 215 SECItem signature_item = { siBuffer, NULL, 0 }; |
| 216 |
| 217 if (PK11_SignWithSymKey(sym_key->key(), |
| 218 PK11_GetMechanism(sym_key->key()), |
| 219 ¶m_item, |
| 220 &signature_item, |
| 221 &data_item) != SECSuccess) { |
| 222 NOTREACHED(); |
| 223 return false; |
| 224 } |
| 225 |
| 226 DCHECK_NE(0u, signature_item.len); |
| 227 |
| 228 result = WebKit::WebArrayBuffer::create(signature_item.len, 1); |
| 229 signature_item.data = reinterpret_cast<unsigned char*>(result.data()); |
| 230 |
| 231 if (PK11_SignWithSymKey(sym_key->key(), |
| 232 PK11_GetMechanism(sym_key->key()), |
| 233 ¶m_item, |
| 234 &signature_item, |
| 235 &data_item) != SECSuccess) { |
| 236 NOTREACHED(); |
| 237 return false; |
| 238 } |
| 239 |
| 240 DCHECK_EQ(result.byteLength(), signature_item.len); |
| 241 |
| 242 break; |
| 243 } |
| 244 default: |
| 245 return false; |
| 246 } |
| 247 |
| 248 *buffer = result; |
| 249 return true; |
| 250 } |
| 251 |
72 } // namespace content | 252 } // namespace content |
OLD | NEW |