cryptui: Fix destination store for self-signed certs.
[wine/multimedia.git] / dlls / cryptui / main.c
blobafb84912f7a308acacaa360bc8a323ad47cc025d
1 /*
2 * Copyright 2008 Juan Lang
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.
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.
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
19 #include "config.h"
21 #include <stdarg.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winnls.h"
26 #include "winuser.h"
27 #include "cryptuiapi.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(cryptui);
32 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
34 TRACE("(0x%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
36 switch (fdwReason)
38 case DLL_WINE_PREATTACH:
39 return FALSE; /* prefer native version */
40 case DLL_PROCESS_ATTACH:
41 DisableThreadLibraryCalls(hinstDLL);
42 break;
43 case DLL_PROCESS_DETACH:
44 break;
45 default:
46 break;
48 return TRUE;
51 /***********************************************************************
52 * CryptUIDlgCertMgr (CRYPTUI.@)
54 BOOL WINAPI CryptUIDlgCertMgr(PCCRYPTUI_CERT_MGR_STRUCT pCryptUICertMgr)
56 FIXME("(%p): stub\n", pCryptUICertMgr);
57 return FALSE;
60 BOOL WINAPI CryptUIDlgViewCertificateA(
61 PCCRYPTUI_VIEWCERTIFICATE_STRUCTA pCertViewInfo, BOOL *pfPropertiesChanged)
63 CRYPTUI_VIEWCERTIFICATE_STRUCTW viewInfo;
64 LPWSTR title = NULL;
65 BOOL ret;
67 TRACE("(%p, %p)\n", pCertViewInfo, pfPropertiesChanged);
69 memcpy(&viewInfo, pCertViewInfo, sizeof(viewInfo));
70 if (pCertViewInfo->szTitle)
72 int len = MultiByteToWideChar(CP_ACP, 0, pCertViewInfo->szTitle, -1,
73 NULL, 0);
75 title = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
76 if (title)
78 MultiByteToWideChar(CP_ACP, 0, pCertViewInfo->szTitle, -1, title,
79 len);
80 viewInfo.szTitle = title;
82 else
84 ret = FALSE;
85 goto error;
88 ret = CryptUIDlgViewCertificateW(&viewInfo, pfPropertiesChanged);
89 HeapFree(GetProcessHeap(), 0, title);
90 error:
91 return ret;
94 BOOL WINAPI CryptUIDlgViewCertificateW(PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo,
95 BOOL *pfPropertiesChanged)
97 FIXME("(%p, %p): stub\n", pCertViewInfo, pfPropertiesChanged);
98 if (pfPropertiesChanged) *pfPropertiesChanged = FALSE;
99 return TRUE;
102 static PCCERT_CONTEXT make_cert_from_file(LPCWSTR fileName)
104 HANDLE file;
105 DWORD size, encoding = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
106 BYTE *buffer;
107 PCCERT_CONTEXT cert;
109 file = CreateFileW(fileName, GENERIC_READ, FILE_SHARE_READ, NULL,
110 OPEN_EXISTING, 0, NULL);
111 if (file == INVALID_HANDLE_VALUE)
113 WARN("can't open certificate file %s\n", debugstr_w(fileName));
114 return NULL;
116 if ((size = GetFileSize(file, NULL)))
118 if ((buffer = HeapAlloc(GetProcessHeap(), 0, size)))
120 DWORD read;
121 if (!ReadFile(file, buffer, size, &read, NULL) || read != size)
123 WARN("can't read certificate file %s\n", debugstr_w(fileName));
124 HeapFree(GetProcessHeap(), 0, buffer);
125 CloseHandle(file);
126 return NULL;
130 else
132 WARN("empty file %s\n", debugstr_w(fileName));
133 CloseHandle(file);
134 return NULL;
136 CloseHandle(file);
137 cert = CertCreateCertificateContext(encoding, buffer, size);
138 HeapFree(GetProcessHeap(), 0, buffer);
139 return cert;
142 /* Decodes a cert's basic constraints extension (either szOID_BASIC_CONSTRAINTS
143 * or szOID_BASIC_CONSTRAINTS2, whichever is present) to determine if it
144 * should be a CA. If neither extension is present, returns
145 * defaultIfNotSpecified.
147 static BOOL is_ca_cert(PCCERT_CONTEXT cert, BOOL defaultIfNotSpecified)
149 BOOL isCA = defaultIfNotSpecified;
150 PCERT_EXTENSION ext = CertFindExtension(szOID_BASIC_CONSTRAINTS,
151 cert->pCertInfo->cExtension, cert->pCertInfo->rgExtension);
153 if (ext)
155 CERT_BASIC_CONSTRAINTS_INFO *info;
156 DWORD size = 0;
158 if (CryptDecodeObjectEx(X509_ASN_ENCODING, szOID_BASIC_CONSTRAINTS,
159 ext->Value.pbData, ext->Value.cbData, CRYPT_DECODE_ALLOC_FLAG,
160 NULL, (LPBYTE)&info, &size))
162 if (info->SubjectType.cbData == 1)
163 isCA = info->SubjectType.pbData[0] & CERT_CA_SUBJECT_FLAG;
164 LocalFree(info);
167 else
169 ext = CertFindExtension(szOID_BASIC_CONSTRAINTS2,
170 cert->pCertInfo->cExtension, cert->pCertInfo->rgExtension);
171 if (ext)
173 CERT_BASIC_CONSTRAINTS2_INFO info;
174 DWORD size = sizeof(CERT_BASIC_CONSTRAINTS2_INFO);
176 if (CryptDecodeObjectEx(X509_ASN_ENCODING,
177 szOID_BASIC_CONSTRAINTS2, ext->Value.pbData, ext->Value.cbData,
178 0, NULL, &info, &size))
179 isCA = info.fCA;
182 return isCA;
185 static HCERTSTORE choose_store_for_cert(PCCERT_CONTEXT cert)
187 static const WCHAR AddressBook[] = { 'A','d','d','r','e','s','s',
188 'B','o','o','k',0 };
189 static const WCHAR CA[] = { 'C','A',0 };
190 LPCWSTR storeName;
192 if (is_ca_cert(cert, TRUE))
193 storeName = CA;
194 else
195 storeName = AddressBook;
196 return CertOpenStore(CERT_STORE_PROV_SYSTEM_W, 0, 0,
197 CERT_SYSTEM_STORE_CURRENT_USER, storeName);
200 BOOL WINAPI CryptUIWizImport(DWORD dwFlags, HWND hwndParent, LPCWSTR pwszWizardTitle,
201 PCCRYPTUI_WIZ_IMPORT_SRC_INFO pImportSrc, HCERTSTORE hDestCertStore)
203 BOOL ret;
204 HCERTSTORE store;
205 const CERT_CONTEXT *cert;
206 BOOL freeCert = FALSE;
208 TRACE("(0x%08x, %p, %s, %p, %p)\n", dwFlags, hwndParent, debugstr_w(pwszWizardTitle),
209 pImportSrc, hDestCertStore);
211 if (!(dwFlags & CRYPTUI_WIZ_NO_UI)) FIXME("UI not implemented\n");
213 if (!pImportSrc ||
214 pImportSrc->dwSize != sizeof(CRYPTUI_WIZ_IMPORT_SRC_INFO))
216 SetLastError(E_INVALIDARG);
217 return FALSE;
220 switch (pImportSrc->dwSubjectChoice)
222 case CRYPTUI_WIZ_IMPORT_SUBJECT_FILE:
223 if (!(cert = make_cert_from_file(pImportSrc->pwszFileName)))
225 WARN("unable to create certificate context\n");
226 return FALSE;
228 else
229 freeCert = TRUE;
230 break;
231 case CRYPTUI_WIZ_IMPORT_SUBJECT_CERT_CONTEXT:
232 cert = pImportSrc->pCertContext;
233 if (!cert)
235 SetLastError(E_INVALIDARG);
236 return FALSE;
238 break;
239 default:
240 FIXME("source type not implemented: %u\n", pImportSrc->dwSubjectChoice);
241 SetLastError(E_INVALIDARG);
242 return FALSE;
244 if (hDestCertStore) store = hDestCertStore;
245 else
247 if (!(store = choose_store_for_cert(cert)))
249 WARN("unable to open certificate store\n");
250 CertFreeCertificateContext(cert);
251 return FALSE;
254 ret = CertAddCertificateContextToStore(store, cert, CERT_STORE_ADD_REPLACE_EXISTING, NULL);
256 if (!hDestCertStore) CertCloseStore(store, 0);
257 if (freeCert)
258 CertFreeCertificateContext(cert);
259 return ret;