| 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/webcrypto_impl.h" |     5 #include "content/renderer/webcrypto/webcrypto_impl.h" | 
|     6  |     6  | 
|     7 #include <cryptohi.h> |     7 #include <cryptohi.h> | 
|     8 #include <pk11pub.h> |     8 #include <pk11pub.h> | 
|     9 #include <sechash.h> |     9 #include <sechash.h> | 
|    10  |    10  | 
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   195   switch (algorithm.id()) { |   195   switch (algorithm.id()) { | 
|   196     case blink::WebCryptoAlgorithmIdAesCbc: |   196     case blink::WebCryptoAlgorithmIdAesCbc: | 
|   197       return CKM_AES_KEY_GEN; |   197       return CKM_AES_KEY_GEN; | 
|   198     case blink::WebCryptoAlgorithmIdHmac: |   198     case blink::WebCryptoAlgorithmIdHmac: | 
|   199       return HmacAlgorithmToGenMechanism(algorithm); |   199       return HmacAlgorithmToGenMechanism(algorithm); | 
|   200     default: |   200     default: | 
|   201       return CKM_INVALID_MECHANISM; |   201       return CKM_INVALID_MECHANISM; | 
|   202   } |   202   } | 
|   203 } |   203 } | 
|   204  |   204  | 
|   205 unsigned int WebCryptoHmacAlgorithmToBlockSize( |   205 // TODO(eroman): This is duplicated in OpenSSL version. | 
 |   206 unsigned int WebCryptoHmacAlgorithmToBlockSizeBits( | 
|   206     const blink::WebCryptoAlgorithm& algorithm) { |   207     const blink::WebCryptoAlgorithm& algorithm) { | 
|   207   DCHECK_EQ(algorithm.id(), blink::WebCryptoAlgorithmIdHmac); |   208   DCHECK_EQ(algorithm.id(), blink::WebCryptoAlgorithmIdHmac); | 
|   208   const blink::WebCryptoHmacKeyParams* params = algorithm.hmacKeyParams(); |   209   const blink::WebCryptoHmacKeyParams* params = algorithm.hmacKeyParams(); | 
|   209   DCHECK(params); |   210   DCHECK(params); | 
|   210   switch (params->hash().id()) { |   211   switch (params->hash().id()) { | 
|   211     case blink::WebCryptoAlgorithmIdSha1: |   212     case blink::WebCryptoAlgorithmIdSha1: | 
|   212       return 512; |   213       return 512; | 
|   213     case blink::WebCryptoAlgorithmIdSha256: |   214     case blink::WebCryptoAlgorithmIdSha256: | 
|   214       return 512; |   215       return 512; | 
|   215     default: |   216     default: | 
| (...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   661  |   662  | 
|   662   if (mech == CKM_INVALID_MECHANISM) { |   663   if (mech == CKM_INVALID_MECHANISM) { | 
|   663     return false; |   664     return false; | 
|   664   } |   665   } | 
|   665  |   666  | 
|   666   switch (algorithm.id()) { |   667   switch (algorithm.id()) { | 
|   667     case blink::WebCryptoAlgorithmIdAesCbc: { |   668     case blink::WebCryptoAlgorithmIdAesCbc: { | 
|   668       const blink::WebCryptoAesKeyGenParams* params = |   669       const blink::WebCryptoAesKeyGenParams* params = | 
|   669           algorithm.aesKeyGenParams(); |   670           algorithm.aesKeyGenParams(); | 
|   670       DCHECK(params); |   671       DCHECK(params); | 
|   671       keylen_bytes = params->length() / 8; |   672       if (params->lengthBits() % 8) | 
|   672       if (params->length() % 8) |  | 
|   673         return false; |   673         return false; | 
 |   674       keylen_bytes = params->lengthBits() / 8; | 
|   674       key_type = blink::WebCryptoKeyTypeSecret; |   675       key_type = blink::WebCryptoKeyTypeSecret; | 
|   675       break; |   676       break; | 
|   676     } |   677     } | 
|   677     case blink::WebCryptoAlgorithmIdHmac: { |   678     case blink::WebCryptoAlgorithmIdHmac: { | 
|   678       const blink::WebCryptoHmacKeyParams* params = algorithm.hmacKeyParams(); |   679       const blink::WebCryptoHmacKeyParams* params = algorithm.hmacKeyParams(); | 
|   679       DCHECK(params); |   680       DCHECK(params); | 
|   680       if (!params->getLength(keylen_bytes)) { |   681       if (params->hasLengthBytes()) { | 
|   681         keylen_bytes = WebCryptoHmacAlgorithmToBlockSize(algorithm) / 8; |   682         keylen_bytes = params->optionalLengthBytes(); | 
 |   683       } else { | 
 |   684         keylen_bytes = WebCryptoHmacAlgorithmToBlockSizeBits(algorithm) / 8; | 
|   682       } |   685       } | 
|   683  |   686  | 
|   684       key_type = blink::WebCryptoKeyTypeSecret; |   687       key_type = blink::WebCryptoKeyTypeSecret; | 
|   685       break; |   688       break; | 
|   686     } |   689     } | 
|   687  |   690  | 
|   688     default: { |   691     default: { | 
|   689       return false; |   692       return false; | 
|   690     } |   693     } | 
|   691   } |   694   } | 
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   723   switch (algorithm.id()) { |   726   switch (algorithm.id()) { | 
|   724     case blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5: |   727     case blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5: | 
|   725     case blink::WebCryptoAlgorithmIdRsaOaep: |   728     case blink::WebCryptoAlgorithmIdRsaOaep: | 
|   726     case blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5: { |   729     case blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5: { | 
|   727       const blink::WebCryptoRsaKeyGenParams* const params = |   730       const blink::WebCryptoRsaKeyGenParams* const params = | 
|   728           algorithm.rsaKeyGenParams(); |   731           algorithm.rsaKeyGenParams(); | 
|   729       DCHECK(params); |   732       DCHECK(params); | 
|   730  |   733  | 
|   731       crypto::ScopedPK11Slot slot(PK11_GetInternalKeySlot()); |   734       crypto::ScopedPK11Slot slot(PK11_GetInternalKeySlot()); | 
|   732       unsigned long public_exponent; |   735       unsigned long public_exponent; | 
|   733       if (!slot || !params->modulusLength() || |   736       if (!slot || !params->modulusLengthBits() || | 
|   734           !BigIntegerToLong(params->publicExponent().data(), |   737           !BigIntegerToLong(params->publicExponent().data(), | 
|   735                             params->publicExponent().size(), |   738                             params->publicExponent().size(), | 
|   736                             &public_exponent) || |   739                             &public_exponent) || | 
|   737           !public_exponent) { |   740           !public_exponent) { | 
|   738         return false; |   741         return false; | 
|   739       } |   742       } | 
|   740  |   743  | 
|   741       PK11RSAGenParams rsa_gen_params; |   744       PK11RSAGenParams rsa_gen_params; | 
|   742       rsa_gen_params.keySizeInBits = params->modulusLength(); |   745       rsa_gen_params.keySizeInBits = params->modulusLengthBits(); | 
|   743       rsa_gen_params.pe = public_exponent; |   746       rsa_gen_params.pe = public_exponent; | 
|   744  |   747  | 
|   745       // Flags are verified at the Blink layer; here the flags are set to all |   748       // Flags are verified at the Blink layer; here the flags are set to all | 
|   746       // possible operations for the given key type. |   749       // possible operations for the given key type. | 
|   747       CK_FLAGS operation_flags; |   750       CK_FLAGS operation_flags; | 
|   748       switch (algorithm.id()) { |   751       switch (algorithm.id()) { | 
|   749         case blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5: |   752         case blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5: | 
|   750         case blink::WebCryptoAlgorithmIdRsaOaep: |   753         case blink::WebCryptoAlgorithmIdRsaOaep: | 
|   751           operation_flags = CKF_ENCRYPT | CKF_DECRYPT | CKF_WRAP | CKF_UNWRAP; |   754           operation_flags = CKF_ENCRYPT | CKF_DECRYPT | CKF_WRAP | CKF_UNWRAP; | 
|   752           break; |   755           break; | 
| (...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  1004  |  1007  | 
|  1005   *key = blink::WebCryptoKey::create(new PublicKeyHandle(pubkey.Pass()), |  1008   *key = blink::WebCryptoKey::create(new PublicKeyHandle(pubkey.Pass()), | 
|  1006                                      blink::WebCryptoKeyTypePublic, |  1009                                      blink::WebCryptoKeyTypePublic, | 
|  1007                                      extractable, |  1010                                      extractable, | 
|  1008                                      algorithm, |  1011                                      algorithm, | 
|  1009                                      usage_mask); |  1012                                      usage_mask); | 
|  1010   return true; |  1013   return true; | 
|  1011 } |  1014 } | 
|  1012  |  1015  | 
|  1013 }  // namespace content |  1016 }  // namespace content | 
| OLD | NEW |