| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "net/base/keygen_handler.h" | 5 #include "net/base/keygen_handler.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 #include <wincrypt.h> | 8 #include <wincrypt.h> |
| 9 #pragma comment(lib, "crypt32.lib") | 9 #pragma comment(lib, "crypt32.lib") |
| 10 #include <rpc.h> | 10 #include <rpc.h> |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 // Assigns the contents of a CERT_PUBLIC_KEY_INFO structure for the signing | 29 // Assigns the contents of a CERT_PUBLIC_KEY_INFO structure for the signing |
| 30 // key in |prov| to |output|. Returns true if encoding was successful. | 30 // key in |prov| to |output|. Returns true if encoding was successful. |
| 31 bool GetSubjectPublicKeyInfo(HCRYPTPROV prov, std::vector<BYTE>* output) { | 31 bool GetSubjectPublicKeyInfo(HCRYPTPROV prov, std::vector<BYTE>* output) { |
| 32 BOOL ok; | 32 BOOL ok; |
| 33 DWORD size = 0; | 33 DWORD size = 0; |
| 34 | 34 |
| 35 // From the private key stored in HCRYPTPROV, obtain the public key, stored | 35 // From the private key stored in HCRYPTPROV, obtain the public key, stored |
| 36 // as a CERT_PUBLIC_KEY_INFO structure. Currently, only RSA public keys are | 36 // as a CERT_PUBLIC_KEY_INFO structure. Currently, only RSA public keys are |
| 37 // supported. | 37 // supported. |
| 38 ok = CryptExportPublicKeyInfoEx(prov, AT_KEYEXCHANGE, X509_ASN_ENCODING, | 38 ok = CryptExportPublicKeyInfoEx(prov, AT_KEYEXCHANGE, X509_ASN_ENCODING, |
| 39 szOID_RSA_RSA, 0, NULL, NULL, &size); | 39 const_cast<char*>(szOID_RSA_RSA), 0, NULL, |
| 40 NULL, &size); |
| 40 DCHECK(ok); | 41 DCHECK(ok); |
| 41 if (!ok) | 42 if (!ok) |
| 42 return false; | 43 return false; |
| 43 | 44 |
| 44 output->resize(size); | 45 output->resize(size); |
| 45 | 46 |
| 46 PCERT_PUBLIC_KEY_INFO public_key_casted = | 47 PCERT_PUBLIC_KEY_INFO public_key_casted = |
| 47 reinterpret_cast<PCERT_PUBLIC_KEY_INFO>(&(*output)[0]); | 48 reinterpret_cast<PCERT_PUBLIC_KEY_INFO>(&(*output)[0]); |
| 48 ok = CryptExportPublicKeyInfoEx(prov, AT_KEYEXCHANGE, X509_ASN_ENCODING, | 49 ok = CryptExportPublicKeyInfoEx(prov, AT_KEYEXCHANGE, X509_ASN_ENCODING, |
| 49 szOID_RSA_RSA, 0, NULL, public_key_casted, | 50 const_cast<char*>(szOID_RSA_RSA), 0, NULL, |
| 50 &size); | 51 public_key_casted, &size); |
| 51 DCHECK(ok); | 52 DCHECK(ok); |
| 52 if (!ok) | 53 if (!ok) |
| 53 return false; | 54 return false; |
| 54 | 55 |
| 55 output->resize(size); | 56 output->resize(size); |
| 56 | 57 |
| 57 return true; | 58 return true; |
| 58 } | 59 } |
| 59 | 60 |
| 60 // Generates a DER encoded SignedPublicKeyAndChallenge structure from the | 61 // Generates a DER encoded SignedPublicKeyAndChallenge structure from the |
| (...skipping 14 matching lines...) Expand all Loading... |
| 75 // challenge IA5STRING | 76 // challenge IA5STRING |
| 76 // } | 77 // } |
| 77 CERT_KEYGEN_REQUEST_INFO pkac; | 78 CERT_KEYGEN_REQUEST_INFO pkac; |
| 78 pkac.dwVersion = CERT_KEYGEN_REQUEST_V1; | 79 pkac.dwVersion = CERT_KEYGEN_REQUEST_V1; |
| 79 pkac.SubjectPublicKeyInfo = | 80 pkac.SubjectPublicKeyInfo = |
| 80 *reinterpret_cast<PCERT_PUBLIC_KEY_INFO>(&spki[0]); | 81 *reinterpret_cast<PCERT_PUBLIC_KEY_INFO>(&spki[0]); |
| 81 pkac.pwszChallengeString = const_cast<wchar_t*>(wide_challenge.c_str()); | 82 pkac.pwszChallengeString = const_cast<wchar_t*>(wide_challenge.c_str()); |
| 82 | 83 |
| 83 CRYPT_ALGORITHM_IDENTIFIER sig_alg; | 84 CRYPT_ALGORITHM_IDENTIFIER sig_alg; |
| 84 memset(&sig_alg, 0, sizeof(sig_alg)); | 85 memset(&sig_alg, 0, sizeof(sig_alg)); |
| 85 sig_alg.pszObjId = szOID_RSA_MD5RSA; | 86 sig_alg.pszObjId = const_cast<char*>(szOID_RSA_MD5RSA); |
| 86 | 87 |
| 87 BOOL ok; | 88 BOOL ok; |
| 88 DWORD size = 0; | 89 DWORD size = 0; |
| 89 std::vector<BYTE> signed_pkac; | 90 std::vector<BYTE> signed_pkac; |
| 90 ok = CryptSignAndEncodeCertificate(prov, AT_KEYEXCHANGE, X509_ASN_ENCODING, | 91 ok = CryptSignAndEncodeCertificate(prov, AT_KEYEXCHANGE, X509_ASN_ENCODING, |
| 91 X509_KEYGEN_REQUEST_TO_BE_SIGNED, | 92 X509_KEYGEN_REQUEST_TO_BE_SIGNED, |
| 92 &pkac, &sig_alg, NULL, | 93 &pkac, &sig_alg, NULL, |
| 93 NULL, &size); | 94 NULL, &size); |
| 94 DCHECK(ok); | 95 DCHECK(ok); |
| 95 if (!ok) | 96 if (!ok) |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 | 213 |
| 213 std::string result; | 214 std::string result; |
| 214 base::Base64Encode(spkac, &result); | 215 base::Base64Encode(spkac, &result); |
| 215 | 216 |
| 216 VLOG(1) << "Keygen succeeded"; | 217 VLOG(1) << "Keygen succeeded"; |
| 217 return result; | 218 return result; |
| 218 } | 219 } |
| 219 } | 220 } |
| 220 | 221 |
| 221 } // namespace net | 222 } // namespace net |
| OLD | NEW |