VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/win/RTCrStoreCreateSnapshotById-win.cpp@ 76409

Last change on this file since 76409 was 69111, checked in by vboxsync, 7 years ago

(C) year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.5 KB
Line 
1/* $Id: RTCrStoreCreateSnapshotById-win.cpp 69111 2017-10-17 14:26:02Z vboxsync $ */
2/** @file
3 * IPRT - RTCrStoreCreateSnapshotById, Windows.
4 */
5
6/*
7 * Copyright (C) 2006-2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/crypto/store.h>
32#include "internal/iprt.h"
33
34#include <iprt/assert.h>
35#include <iprt/err.h>
36#include <iprt/once.h>
37#include <iprt/ldr.h>
38
39#include <iprt/win/windows.h>
40
41
42/*********************************************************************************************************************************
43* Structures and Typedefs *
44*********************************************************************************************************************************/
45typedef HCERTSTORE (WINAPI *PFNCERTOPENSTORE)(PCSTR pszStoreProvider, DWORD dwEncodingType, HCRYPTPROV_LEGACY hCryptProv,
46 DWORD dwFlags, const void *pvParam);
47typedef BOOL (WINAPI *PFNCERTCLOSESTORE)(HCERTSTORE hCertStore, DWORD dwFlags);
48typedef PCCERT_CONTEXT (WINAPI *PFNCERTENUMCERTIFICATESINSTORE)(HCERTSTORE hCertStore, PCCERT_CONTEXT pPrevCertContext);
49
50
51
52static int rtCrStoreAddCertsFromNative(RTCRSTORE hStore, DWORD fStore, PCRTUTF16 pwszStoreName,
53 PFNCERTOPENSTORE pfnOpenStore, PFNCERTCLOSESTORE pfnCloseStore,
54 PFNCERTENUMCERTIFICATESINSTORE pfnEnumCerts, int rc, PRTERRINFO pErrInfo)
55{
56 DWORD fOpenStore = CERT_STORE_OPEN_EXISTING_FLAG | CERT_STORE_READONLY_FLAG;
57 HCERTSTORE hNativeStore = pfnOpenStore(CERT_STORE_PROV_SYSTEM_W, PKCS_7_ASN_ENCODING | X509_ASN_ENCODING,
58 NULL /* hCryptProv = default */, fStore | fOpenStore, pwszStoreName);
59 if (hStore)
60 {
61 PCCERT_CONTEXT pCurCtx = NULL;
62 while ((pCurCtx = pfnEnumCerts(hNativeStore, pCurCtx)) != NULL)
63 {
64 if (pCurCtx->dwCertEncodingType & X509_ASN_ENCODING)
65 {
66 RTERRINFOSTATIC StaticErrInfo;
67 RTASN1CURSORPRIMARY PrimaryCursor;
68 RTAsn1CursorInitPrimary(&PrimaryCursor, pCurCtx->pbCertEncoded, pCurCtx->cbCertEncoded,
69 RTErrInfoInitStatic(&StaticErrInfo),
70 &g_RTAsn1DefaultAllocator, RTASN1CURSOR_FLAGS_DER, "CurCtx");
71 RTCRX509CERTIFICATE MyCert;
72 int rc2 = RTCrX509Certificate_DecodeAsn1(&PrimaryCursor.Cursor, 0, &MyCert, "Cert");
73 if (RT_SUCCESS(rc2))
74 {
75 rc2 = RTCrStoreCertAddEncoded(hStore, RTCRCERTCTX_F_ENC_X509_DER | RTCRCERTCTX_F_ADD_IF_NOT_FOUND,
76 pCurCtx->pbCertEncoded, pCurCtx->cbCertEncoded,
77 RTErrInfoInitStatic(&StaticErrInfo));
78 RTCrX509Certificate_Delete(&MyCert);
79 }
80 if (RT_FAILURE(rc2))
81 {
82 if (RTErrInfoIsSet(&StaticErrInfo.Core))
83 RTErrInfoAddF(pErrInfo, -rc2, " %s", StaticErrInfo.Core.pszMsg);
84 else
85 RTErrInfoAddF(pErrInfo, -rc2, " %Rrc adding cert", rc2);
86 rc = -rc2;
87 }
88 }
89 }
90 pfnCloseStore(hNativeStore, CERT_CLOSE_STORE_CHECK_FLAG);
91 }
92 else
93 {
94 DWORD uLastErr = GetLastError();
95 if (uLastErr != ERROR_FILE_NOT_FOUND)
96 rc = RTErrInfoAddF(pErrInfo, -RTErrConvertFromWin32(uLastErr),
97 " CertOpenStore(%#x,'%ls') failed: %u", fStore, pwszStoreName);
98 }
99 return rc;
100}
101
102
103
104RTDECL(int) RTCrStoreCreateSnapshotById(PRTCRSTORE phStore, RTCRSTOREID enmStoreId, PRTERRINFO pErrInfo)
105{
106 AssertReturn(enmStoreId > RTCRSTOREID_INVALID && enmStoreId < RTCRSTOREID_END, VERR_INVALID_PARAMETER);
107
108 /*
109 * Create an empty in-memory store.
110 */
111 RTCRSTORE hStore;
112 int rc = RTCrStoreCreateInMem(&hStore, 128);
113 if (RT_SUCCESS(rc))
114 {
115 *phStore = hStore;
116
117 /*
118 * Resolve the APIs we need to do this job.
119 */
120 RTLDRMOD hLdrMod;
121 int rc2 = RTLdrLoadSystem("crypt32.dll", false /*NoUnload*/, &hLdrMod);
122 if (RT_SUCCESS(rc2))
123 {
124 PFNCERTOPENSTORE pfnOpenStore = NULL;
125 rc2 = RTLdrGetSymbol(hLdrMod, "CertOpenStore", (void **)&pfnOpenStore);
126
127 PFNCERTCLOSESTORE pfnCloseStore = NULL;
128 if (RT_SUCCESS(rc2))
129 rc2 = RTLdrGetSymbol(hLdrMod, "CertCloseStore", (void **)&pfnCloseStore);
130
131 PFNCERTENUMCERTIFICATESINSTORE pfnEnumCerts = NULL;
132 if (RT_SUCCESS(rc2))
133 rc2 = RTLdrGetSymbol(hLdrMod, "CertEnumCertificatesInStore", (void **)&pfnEnumCerts);
134 if (RT_SUCCESS(rc2))
135 {
136 /*
137 * Do the work.
138 */
139 switch (enmStoreId)
140 {
141 case RTCRSTOREID_USER_TRUSTED_CAS_AND_CERTIFICATES:
142 case RTCRSTOREID_SYSTEM_TRUSTED_CAS_AND_CERTIFICATES:
143 {
144 DWORD fStore = enmStoreId == RTCRSTOREID_USER_TRUSTED_CAS_AND_CERTIFICATES
145 ? CERT_SYSTEM_STORE_CURRENT_USER : CERT_SYSTEM_STORE_LOCAL_MACHINE;
146 static PCRTUTF16 const s_apwszStores[] = { L"AuthRoot", L"CA", L"MY", L"Root" };
147 for (uint32_t i = 0; i < RT_ELEMENTS(s_apwszStores); i++)
148 rc = rtCrStoreAddCertsFromNative(hStore, fStore, s_apwszStores[i], pfnOpenStore, pfnCloseStore,
149 pfnEnumCerts, rc, pErrInfo);
150 break;
151 }
152
153 default:
154 AssertFailed(); /* implement me */
155 }
156 }
157 else
158 rc = RTErrInfoSetF(pErrInfo, -rc2, "Error resolving crypt32.dll APIs");
159 RTLdrClose(hLdrMod);
160 }
161 else
162 rc = RTErrInfoSetF(pErrInfo, -rc2, "Error loading crypt32.dll");
163 }
164 else
165 RTErrInfoSet(pErrInfo, rc, "RTCrStoreCreateInMem failed");
166 return rc;
167}
168RT_EXPORT_SYMBOL(RTCrStoreCreateSnapshotById);
169
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette