winhttp: Reimplement WinHttpCrackUrl.
[wine] / dlls / cryptui / main.c
1 /*
2  * Copyright 2008 Juan Lang
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 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  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include "config.h"
20
21 #include <stdarg.h>
22
23 #define NONAMELESSUNION
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winnls.h"
28 #include "winuser.h"
29 #include "cryptuiapi.h"
30 #include "wine/debug.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(cryptui);
33
34 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
35 {
36     TRACE("(0x%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
37
38     switch (fdwReason)
39     {
40         case DLL_WINE_PREATTACH:
41             return FALSE;    /* prefer native version */
42         case DLL_PROCESS_ATTACH:
43             DisableThreadLibraryCalls(hinstDLL);
44             break;
45         case DLL_PROCESS_DETACH:
46             break;
47         default:
48             break;
49     }
50     return TRUE;
51 }
52
53 /***********************************************************************
54  *              CryptUIDlgCertMgr (CRYPTUI.@)
55  */
56 BOOL WINAPI CryptUIDlgCertMgr(PCCRYPTUI_CERT_MGR_STRUCT pCryptUICertMgr)
57 {
58     FIXME("(%p): stub\n", pCryptUICertMgr);
59     return FALSE;
60 }
61
62 BOOL WINAPI CryptUIDlgViewCertificateA(
63  PCCRYPTUI_VIEWCERTIFICATE_STRUCTA pCertViewInfo, BOOL *pfPropertiesChanged)
64 {
65     CRYPTUI_VIEWCERTIFICATE_STRUCTW viewInfo;
66     LPWSTR title = NULL;
67     BOOL ret;
68
69     TRACE("(%p, %p)\n", pCertViewInfo, pfPropertiesChanged);
70
71     memcpy(&viewInfo, pCertViewInfo, sizeof(viewInfo));
72     if (pCertViewInfo->szTitle)
73     {
74         int len = MultiByteToWideChar(CP_ACP, 0, pCertViewInfo->szTitle, -1,
75          NULL, 0);
76
77         title = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
78         if (title)
79         {
80             MultiByteToWideChar(CP_ACP, 0, pCertViewInfo->szTitle, -1, title,
81              len);
82             viewInfo.szTitle = title;
83         }
84         else
85         {
86             ret = FALSE;
87             goto error;
88         }
89     }
90     ret = CryptUIDlgViewCertificateW(&viewInfo, pfPropertiesChanged);
91     HeapFree(GetProcessHeap(), 0, title);
92 error:
93     return ret;
94 }
95
96 BOOL WINAPI CryptUIDlgViewCertificateW(PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo,
97                                        BOOL *pfPropertiesChanged)
98 {
99     FIXME("(%p, %p): stub\n", pCertViewInfo, pfPropertiesChanged);
100     if (pfPropertiesChanged) *pfPropertiesChanged = FALSE;
101     return TRUE;
102 }
103
104 static PCCERT_CONTEXT make_cert_from_file(LPCWSTR fileName)
105 {
106     HANDLE file;
107     DWORD size, encoding = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
108     BYTE *buffer;
109     PCCERT_CONTEXT cert;
110
111     file = CreateFileW(fileName, GENERIC_READ, FILE_SHARE_READ, NULL,
112      OPEN_EXISTING, 0, NULL);
113     if (file == INVALID_HANDLE_VALUE)
114     {
115         WARN("can't open certificate file %s\n", debugstr_w(fileName));
116         return NULL;
117     }
118     if ((size = GetFileSize(file, NULL)))
119     {
120         if ((buffer = HeapAlloc(GetProcessHeap(), 0, size)))
121         {
122             DWORD read;
123             if (!ReadFile(file, buffer, size, &read, NULL) || read != size)
124             {
125                 WARN("can't read certificate file %s\n", debugstr_w(fileName));
126                 HeapFree(GetProcessHeap(), 0, buffer);
127                 CloseHandle(file);
128                 return NULL;
129             }
130         }
131     }
132     else
133     {
134         WARN("empty file %s\n", debugstr_w(fileName));
135         CloseHandle(file);
136         return NULL;
137     }
138     CloseHandle(file);
139     cert = CertCreateCertificateContext(encoding, buffer, size);
140     HeapFree(GetProcessHeap(), 0, buffer);
141     return cert;
142 }
143
144 /* Decodes a cert's basic constraints extension (either szOID_BASIC_CONSTRAINTS
145  * or szOID_BASIC_CONSTRAINTS2, whichever is present) to determine if it
146  * should be a CA.  If neither extension is present, returns
147  * defaultIfNotSpecified.
148  */
149 static BOOL is_ca_cert(PCCERT_CONTEXT cert, BOOL defaultIfNotSpecified)
150 {
151     BOOL isCA = defaultIfNotSpecified;
152     PCERT_EXTENSION ext = CertFindExtension(szOID_BASIC_CONSTRAINTS,
153      cert->pCertInfo->cExtension, cert->pCertInfo->rgExtension);
154
155     if (ext)
156     {
157         CERT_BASIC_CONSTRAINTS_INFO *info;
158         DWORD size = 0;
159
160         if (CryptDecodeObjectEx(X509_ASN_ENCODING, szOID_BASIC_CONSTRAINTS,
161          ext->Value.pbData, ext->Value.cbData, CRYPT_DECODE_ALLOC_FLAG,
162          NULL, (LPBYTE)&info, &size))
163         {
164             if (info->SubjectType.cbData == 1)
165                 isCA = info->SubjectType.pbData[0] & CERT_CA_SUBJECT_FLAG;
166             LocalFree(info);
167         }
168     }
169     else
170     {
171         ext = CertFindExtension(szOID_BASIC_CONSTRAINTS2,
172          cert->pCertInfo->cExtension, cert->pCertInfo->rgExtension);
173         if (ext)
174         {
175             CERT_BASIC_CONSTRAINTS2_INFO info;
176             DWORD size = sizeof(CERT_BASIC_CONSTRAINTS2_INFO);
177
178             if (CryptDecodeObjectEx(X509_ASN_ENCODING,
179              szOID_BASIC_CONSTRAINTS2, ext->Value.pbData, ext->Value.cbData,
180              0, NULL, &info, &size))
181                 isCA = info.fCA;
182         }
183     }
184     return isCA;
185 }
186
187 static HCERTSTORE choose_store_for_cert(PCCERT_CONTEXT cert)
188 {
189     static const WCHAR AddressBook[] = { 'A','d','d','r','e','s','s',
190      'B','o','o','k',0 };
191     static const WCHAR CA[] = { 'C','A',0 };
192     LPCWSTR storeName;
193
194     if (is_ca_cert(cert, TRUE))
195         storeName = CA;
196     else
197         storeName = AddressBook;
198     return CertOpenStore(CERT_STORE_PROV_SYSTEM_W, 0, 0,
199      CERT_SYSTEM_STORE_CURRENT_USER, storeName);
200 }
201
202 BOOL WINAPI CryptUIWizImport(DWORD dwFlags, HWND hwndParent, LPCWSTR pwszWizardTitle,
203                              PCCRYPTUI_WIZ_IMPORT_SRC_INFO pImportSrc, HCERTSTORE hDestCertStore)
204 {
205     BOOL ret;
206     HCERTSTORE store;
207     const CERT_CONTEXT *cert;
208     BOOL freeCert = FALSE;
209
210     TRACE("(0x%08x, %p, %s, %p, %p)\n", dwFlags, hwndParent, debugstr_w(pwszWizardTitle),
211           pImportSrc, hDestCertStore);
212
213     if (!(dwFlags & CRYPTUI_WIZ_NO_UI)) FIXME("UI not implemented\n");
214
215     if (!pImportSrc ||
216      pImportSrc->dwSize != sizeof(CRYPTUI_WIZ_IMPORT_SRC_INFO))
217     {
218         SetLastError(E_INVALIDARG);
219         return FALSE;
220     }
221
222     switch (pImportSrc->dwSubjectChoice)
223     {
224     case CRYPTUI_WIZ_IMPORT_SUBJECT_FILE:
225         if (!(cert = make_cert_from_file(pImportSrc->u.pwszFileName)))
226         {
227             WARN("unable to create certificate context\n");
228             return FALSE;
229         }
230         else
231             freeCert = TRUE;
232         break;
233     case CRYPTUI_WIZ_IMPORT_SUBJECT_CERT_CONTEXT:
234         cert = pImportSrc->u.pCertContext;
235         if (!cert)
236         {
237             SetLastError(E_INVALIDARG);
238             return FALSE;
239         }
240         break;
241     default:
242         FIXME("source type not implemented: %u\n", pImportSrc->dwSubjectChoice);
243         SetLastError(E_INVALIDARG);
244         return FALSE;
245     }
246     if (hDestCertStore) store = hDestCertStore;
247     else
248     {
249         if (!(store = choose_store_for_cert(cert)))
250         {
251             WARN("unable to open certificate store\n");
252             CertFreeCertificateContext(cert);
253             return FALSE;
254         }
255     }
256     ret = CertAddCertificateContextToStore(store, cert, CERT_STORE_ADD_REPLACE_EXISTING, NULL);
257
258     if (!hDestCertStore) CertCloseStore(store, 0);
259     if (freeCert)
260         CertFreeCertificateContext(cert);
261     return ret;
262 }