[Win][Clang] Fix warning -Wmissing-field-initializers
[WebKit-https.git] / Source / WebCore / platform / win / SSLKeyGeneratorWin.cpp
1 /*
2  * Copyright (C) 2008-2009 Torch Mobile Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include "config.h"
21 #include "SSLKeyGenerator.h"
22
23 #include <wtf/text/Base64.h>
24 #include <wtf/text/CString.h>
25 #include <wtf/text/win/WCharStringExtras.h>
26
27 #include <windows.h>
28 #include <wincrypt.h>
29
30 namespace WebCore {
31
32 void WebCore::getSupportedKeySizes(Vector<String>& v)
33 {
34     // FIXME: Strings should be localizable.
35     v.append("High Grade");
36 }
37
38 String WebCore::signedPublicKeyAndChallengeString(unsigned index, const String& challenge, const URL& url)
39 {
40     String keyString;
41
42     HCRYPTPROV hContext = 0;
43     HCRYPTKEY hKey = 0;
44     PCERT_PUBLIC_KEY_INFO pPubInfo = 0;
45
46     // Try to delete it if it exists already
47     CryptAcquireContextW(&hContext, L"keygen_container", MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_DELETEKEYSET);
48
49     do {
50         if (!CryptAcquireContextW(&hContext, L"keygen_container", MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET))
51             break;
52
53         DWORD dwPubInfoLength = 0;
54         if (!CryptGenKey(hContext, AT_KEYEXCHANGE, 0, &hKey) || !CryptExportPublicKeyInfo(hContext, AT_KEYEXCHANGE, X509_ASN_ENCODING, 0, &dwPubInfoLength))
55             break;
56
57         // Use malloc instead of new, because malloc guarantees to return a pointer aligned for all data types.
58         pPubInfo = reinterpret_cast<PCERT_PUBLIC_KEY_INFO>(fastMalloc(dwPubInfoLength));
59
60         if (!CryptExportPublicKeyInfo(hContext, AT_KEYEXCHANGE, X509_ASN_ENCODING, pPubInfo, &dwPubInfoLength))
61             break;
62
63         CERT_KEYGEN_REQUEST_INFO requestInfo { };
64         requestInfo.dwVersion = CERT_KEYGEN_REQUEST_V1;
65         requestInfo.pwszChallengeString = L"";
66         requestInfo.SubjectPublicKeyInfo = *pPubInfo;
67
68         String localChallenge = challenge;
69
70         // Windows API won't write to our buffer, although it's not declared with const.
71         auto localChallengeWide = stringToNullTerminatedWChar(localChallenge);
72         requestInfo.pwszChallengeString = const_cast<wchar_t*>(localChallengeWide.data());
73
74         CRYPT_ALGORITHM_IDENTIFIER signAlgo { };
75         signAlgo.pszObjId = szOID_RSA_SHA1RSA;
76
77         DWORD dwEncodedLength;
78         if (!CryptSignAndEncodeCertificate(hContext, AT_KEYEXCHANGE, X509_ASN_ENCODING, X509_KEYGEN_REQUEST_TO_BE_SIGNED, &requestInfo, &signAlgo, 0, 0, &dwEncodedLength))
79             break;
80
81         Vector<char> binary(dwEncodedLength);
82         if (!CryptSignAndEncodeCertificate(hContext, AT_KEYEXCHANGE, X509_ASN_ENCODING, X509_KEYGEN_REQUEST_TO_BE_SIGNED, &requestInfo, &signAlgo, 0, reinterpret_cast<LPBYTE>(binary.data()), &dwEncodedLength))
83             break;
84
85         keyString = base64Encode(binary);
86     } while(0);
87
88     if (pPubInfo)
89         fastFree(pPubInfo);
90
91     if (hKey)
92         CryptDestroyKey(hKey);
93
94     if (hContext)
95         CryptReleaseContext(hContext, 0);
96
97     return keyString;
98 }
99
100 } // namespace WebCore