VirtualBox

source: vbox/trunk/include/iprt/crypto/store.h@ 95681

Last change on this file since 95681 was 95681, checked in by vboxsync, 2 years ago

IPRT/RTCrStoreCreateSnapshotById: Added two more store IDs for intermediate CAs so RTSignTool can search these when signing, only implemented for windows. Fixed an incorrect handle check in worker (windows). bugref:8691

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.3 KB
Line 
1/** @file
2 * IPRT - Cryptographic (Certificate) Store.
3 */
4
5/*
6 * Copyright (C) 2006-2022 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef IPRT_INCLUDED_crypto_store_h
27#define IPRT_INCLUDED_crypto_store_h
28#ifndef RT_WITHOUT_PRAGMA_ONCE
29# pragma once
30#endif
31
32#include <iprt/crypto/x509.h>
33#include <iprt/crypto/taf.h>
34#include <iprt/sha.h>
35
36
37RT_C_DECLS_BEGIN
38
39/** @defgroup grp_rt_crstore RTCrStore - Crypotgraphic (Certificate) Store.
40 * @ingroup grp_rt_crypto
41 * @{
42 */
43
44
45/**
46 * A certificate store search.
47 *
48 * Used by the store provider to keep track of the current location of a
49 * certificate search.
50 */
51typedef struct RTCRSTORECERTSEARCH
52{
53 /** Opaque provider specific storage.
54 *
55 * Provider restriction: The provider is only allowed to use the two first
56 * entries for the find-all searches, because the front-end API may want the
57 * last two for implementing specific searches on top of it. */
58 uintptr_t auOpaque[4];
59} RTCRSTORECERTSEARCH;
60/** Pointer to a certificate store search. */
61typedef RTCRSTORECERTSEARCH *PRTCRSTORECERTSEARCH;
62
63
64/**
65 * Info about a wanted certificate.
66 *
67 * All the search criteria are optional, but for a safe and efficient search
68 * it's recommended to specify all possible ones. If none are given, the search
69 * function will fail.
70 *
71 * For use with RTCrStoreCertAddFromFishingExpedition and others.
72 */
73typedef struct RTCRCERTWANTED
74{
75 /** The certificate subject name, optional.
76 * The format is: "C=US, ST=California, L=Redwood Shores, O=Oracle Corporation" */
77 const char *pszSubject;
78 /** The size of the DER (ASN.1) encoded certificate, optional (0). */
79 uint16_t cbEncoded;
80 /** Set if abSha1 contains a valid SHA-1 fingerprint. */
81 bool fSha1Fingerprint;
82 /** Set if abSha512 contains a valid SHA-512 fingerprint. */
83 bool fSha512Fingerprint;
84 /** The SHA-1 fingerprint (of the encoded data). */
85 uint8_t abSha1[RTSHA1_HASH_SIZE];
86 /** The SHA-512 fingerprint (of the encoded data). */
87 uint8_t abSha512[RTSHA512_HASH_SIZE];
88 /** User pointer for directly associating other data with the entry.
89 * Subclassing the structure isn't possible because it's passed as an array. */
90 void const *pvUser;
91} RTCRCERTWANTED;
92/** Pointer to a const certificat wanted structure. */
93typedef RTCRCERTWANTED const *PCRTCRCERTWANTED;
94
95
96/**
97 * Standard store identifiers.
98 *
99 * This is a least common denominator approach to system specific certificate
100 * stores, could be extended to include things other than certificates later if
101 * we need it.
102 *
103 * Windows has lots of different stores, they'll be combined by the
104 * implementation, possibly leading to duplicates. The user stores on Windows
105 * seems to be unioned with the system (machine) stores.
106 *
107 * Linux may have different stores depending on the distro/version/installation,
108 * in which case we'll combine them, which will most likely lead to
109 * duplicates just like on windows. Haven't found any easily accessible
110 * per-user certificate stores on linux yet, so they'll all be empty.
111 *
112 * Mac OS X seems a lot simpler, at least from the GUI point of view. Each
113 * keychains as a "Certificates" folder (the "My Certificates" folder seems to
114 * only be a matching of "Keys" and "Certificates"). However, there are two
115 * system keychains that we need to combine, "System" and "System Roots". As
116 * with Windows and Linux, there is a possibility for duplicates here.
117 *
118 * On solaris we have currently no idea where to look for a certificate store,
119 * so that doesn't yet work.
120 *
121 * Because of the OS X setup, we do not provide any purpose specific
122 */
123typedef enum RTCRSTOREID
124{
125 /** Mandatory invalid zero value. */
126 RTCRSTOREID_INVALID = 0,
127 /** Open the certificate store of the current user containing trusted
128 * CAs and certificates.
129 * @remarks This may or may not include all the certificates in the system
130 * store, that's host dependent. So, you better look in both. */
131 RTCRSTOREID_USER_TRUSTED_CAS_AND_CERTIFICATES,
132 /** Open the certificate store of the system containg trusted CAs
133 * and certificates. */
134 RTCRSTOREID_SYSTEM_TRUSTED_CAS_AND_CERTIFICATES,
135 /** Open the certificate store of the current user containing intermediate CAs.
136 * @remarks This may or may not include all the certificates in the system
137 * store, that's host dependent. So, you better look in both. */
138 RTCRSTOREID_USER_INTERMEDIATE_CAS,
139 /** Open the certificate store of the system containg intermediate CAs. */
140 RTCRSTOREID_SYSTEM_INTERMEDIATE_CAS,
141 /** End of valid values. */
142 RTCRSTOREID_END,
143 /** Traditional enum type compression prevention hack. */
144 RTCRSTOREID_32BIT_HACK = 0x7fffffff
145} RTCRSTOREID;
146
147/**
148 * Creates a snapshot of a standard store.
149 *
150 * This will return an in-memory store containing all data from the given store.
151 * There will be no duplicates in this one.
152 *
153 * @returns IPRT status code.
154 * @param phStore Where to return the store handle. Use
155 * RTCrStoreRelease to release it.
156 * @param enmStoreId The store to snapshot.
157 * @param pErrInfo Where to return additional error/warning info.
158 * Optional.
159 */
160RTDECL(int) RTCrStoreCreateSnapshotById(PRTCRSTORE phStore, RTCRSTOREID enmStoreId, PRTERRINFO pErrInfo);
161
162RTDECL(int) RTCrStoreCreateSnapshotOfUserAndSystemTrustedCAsAndCerts(PRTCRSTORE phStore, PRTERRINFO pErrInfo);
163
164RTDECL(int) RTCrStoreCreateInMem(PRTCRSTORE phStore, uint32_t cSizeHint);
165RTDECL(int) RTCrStoreCreateInMemEx(PRTCRSTORE phStore, uint32_t cSizeHint, RTCRSTORE hParentStore);
166
167RTDECL(uint32_t) RTCrStoreRetain(RTCRSTORE hStore);
168RTDECL(uint32_t) RTCrStoreRelease(RTCRSTORE hStore);
169RTDECL(PCRTCRCERTCTX) RTCrStoreCertByIssuerAndSerialNo(RTCRSTORE hStore, PCRTCRX509NAME pIssuer, PCRTASN1INTEGER pSerialNo);
170
171/**
172 * Add a certificate to the store.
173 *
174 * @returns IPRT status code.
175 * @retval VWRN_ALREADY_EXISTS if the certificate is already present and
176 * RTCRCERTCTX_F_ADD_IF_NOT_FOUND was specified.
177 * @retval VERR_WRITE_PROTECT if the store doesn't support adding.
178 * @param hStore The store to add the certificate to.
179 * @param fFlags RTCRCERTCTX_F_XXX. Encoding must be specified.
180 * RTCRCERTCTX_F_ADD_IF_NOT_FOUND is supported.
181 * @param pvSrc The encoded certificate bytes.
182 * @param cbSrc The size of the encoded certificate.
183 * @param pErrInfo Where to return additional error/warning info.
184 * Optional.
185 */
186RTDECL(int) RTCrStoreCertAddEncoded(RTCRSTORE hStore, uint32_t fFlags, void const *pvSrc, size_t cbSrc, PRTERRINFO pErrInfo);
187
188/**
189 * Add an X.509 packaged certificate to the store.
190 *
191 * @returns IPRT status code.
192 * @retval VWRN_ALREADY_EXISTS if the certificate is already present and
193 * RTCRCERTCTX_F_ADD_IF_NOT_FOUND was specified.
194 * @retval VERR_WRITE_PROTECT if the store doesn't support adding.
195 * @param hStore The store to add the certificate to.
196 * @param fFlags RTCRCERTCTX_F_XXX. Encoding must is optional,
197 * but must be RTCRCERTCTX_F_ENC_X509_DER if given.
198 * RTCRCERTCTX_F_ADD_IF_NOT_FOUND is supported.
199 * @param pCertificate The certificate to add. We may have to encode
200 * it, thus not const.
201 * @param pErrInfo Where to return additional error/warning info.
202 * Optional.
203 */
204RTDECL(int) RTCrStoreCertAddX509(RTCRSTORE hStore, uint32_t fFlags, PRTCRX509CERTIFICATE pCertificate, PRTERRINFO pErrInfo);
205
206/**
207 * Adds certificates from files in the specified directory.
208 *
209 * @returns IPRT status code. Even when RTCRCERTCTX_F_ADD_CONTINUE_ON_ERROR is
210 * used, an error is returned as an error (and not a warning).
211 *
212 * @param hStore The store to add the certificate(s) to.
213 * @param fFlags RTCRCERTCTX_F_ADD_IF_NOT_FOUND and/or
214 * RTCRCERTCTX_F_ADD_CONTINUE_ON_ERROR.
215 * @param pszDir The path to the directory.
216 * @param paSuffixes List of suffixes of files to process.
217 * @param cSuffixes Number of suffixes. If this is 0, all files are
218 * processed.
219 * @param pErrInfo Where to return additional error/warning info.
220 * Optional.
221 */
222RTDECL(int) RTCrStoreCertAddFromDir(RTCRSTORE hStore, uint32_t fFlags, const char *pszDir,
223 PCRTSTRTUPLE paSuffixes, size_t cSuffixes, PRTERRINFO pErrInfo);
224
225RTDECL(int) RTCrStoreCertAddWantedFromDir(RTCRSTORE hStore, uint32_t fFlags,
226 const char *pszDir, PCRTSTRTUPLE paSuffixes, size_t cSuffixes,
227 PCRTCRCERTWANTED paWanted, size_t cWanted, bool *pafFound, PRTERRINFO pErrInfo);
228
229/**
230 * Adds certificates from the specified file.
231 *
232 * The supported file formats are:
233 * - PEM (base 64 blobs wrapped in -----BEGIN / END----). Support multiple
234 * certificates in one file.
235 * - Binary DER ASN.1 certificate. Only one per file.
236 * - Java key store version 2.
237 *
238 * @returns IPRT status code. Even when RTCRCERTCTX_F_ADD_CONTINUE_ON_ERROR is
239 * used, an error is returned as an error (and not a warning).
240 *
241 * @param hStore The store to add the certificate(s) to.
242 * @param fFlags RTCRCERTCTX_F_ADD_IF_NOT_FOUND and/or
243 * RTCRCERTCTX_F_ADD_CONTINUE_ON_ERROR.
244 * @param pszFilename The filename.
245 * @param pErrInfo Where to return additional error/warning info.
246 * Optional.
247 */
248RTDECL(int) RTCrStoreCertAddFromFile(RTCRSTORE hStore, uint32_t fFlags, const char *pszFilename, PRTERRINFO pErrInfo);
249
250RTDECL(int) RTCrStoreCertAddWantedFromFile(RTCRSTORE hStore, uint32_t fFlags, const char *pszFilename,
251 PCRTCRCERTWANTED paWanted, size_t cWanted, bool *pafFound, PRTERRINFO pErrInfo);
252
253/**
254 * Adds certificates from the specified java key store file.
255 *
256 * @returns IPRT status code. Even when RTCRCERTCTX_F_ADD_CONTINUE_ON_ERROR is
257 * used, an error is returned as an error (and not a warning).
258 *
259 * @param hStore The store to add the certificate(s) to.
260 * @param fFlags RTCRCERTCTX_F_ADD_IF_NOT_FOUND and/or
261 * RTCRCERTCTX_F_ADD_CONTINUE_ON_ERROR.
262 * @param pszFilename The path to the JKS file.
263 * @param pErrInfo Where to return additional error/warning info.
264 * Optional.
265 */
266RTDECL(int) RTCrStoreCertAddFromJavaKeyStore(RTCRSTORE hStore, uint32_t fFlags, const char *pszFilename, PRTERRINFO pErrInfo);
267
268/**
269 * Adds certificates from an in-memory java key store.
270 *
271 * @returns IPRT status code. Even when RTCRCERTCTX_F_ADD_CONTINUE_ON_ERROR is
272 * used, an error is returned as an error (and not a warning).
273 *
274 * @param hStore The store to add the certificate(s) to.
275 * @param fFlags RTCRCERTCTX_F_ADD_IF_NOT_FOUND and/or
276 * RTCRCERTCTX_F_ADD_CONTINUE_ON_ERROR.
277 * @param pvContent Pointer to the key store bytes.
278 * @param cbContent The size of the key store.
279 * @param pszErrorName The file name or whatever helpful indicator the
280 * caller want in the error messages.
281 * @param pErrInfo Where to return additional error/warning info.
282 * Optional.
283 */
284RTDECL(int) RTCrStoreCertAddFromJavaKeyStoreInMem(RTCRSTORE hStore, uint32_t fFlags, void const *pvContent, size_t cbContent,
285 const char *pszErrorName, PRTERRINFO pErrInfo);
286
287/**
288 * Adds all certificates from @a hStoreSrc into @a hStore.
289 *
290 * @returns IPRT status code. Even when RTCRCERTCTX_F_ADD_CONTINUE_ON_ERROR is
291 * used, an error is returned as an error (and not a warning).
292 *
293 * @param hStore The destination store.
294 * @param fFlags RTCRCERTCTX_F_ADD_IF_NOT_FOUND and/or
295 * RTCRCERTCTX_F_ADD_CONTINUE_ON_ERROR.
296 * @param hStoreSrc The source store.
297 */
298RTDECL(int) RTCrStoreCertAddFromStore(RTCRSTORE hStore, uint32_t fFlags, RTCRSTORE hStoreSrc);
299
300RTDECL(int) RTCrStoreCertAddWantedFromStore(RTCRSTORE hStore, uint32_t fFlags, RTCRSTORE hSrcStore,
301 PCRTCRCERTWANTED paWanted, size_t cWanted, bool *pafFound);
302
303RTDECL(int) RTCrStoreCertCheckWanted(RTCRSTORE hStore, PCRTCRCERTWANTED paWanted, size_t cWanted, bool *pafFound);
304
305
306RTDECL(int) RTCrStoreCertAddWantedFromFishingExpedition(RTCRSTORE hStore, uint32_t fFlags,
307 PCRTCRCERTWANTED paWanted, size_t cWanted,
308 bool *pafFound, PRTERRINFO pErrInfo);
309
310/**
311 * Exports the certificates in the store to a PEM file
312 *
313 * @returns IPRT status code.
314 * @param hStore The store which certificates should be exported.
315 * @param fFlags Reserved for the future, MBZ.
316 * @param pszFilename The name of the destination PEM file. This will
317 * be truncated.
318 */
319RTDECL(int) RTCrStoreCertExportAsPem(RTCRSTORE hStore, uint32_t fFlags, const char *pszFilename);
320
321/**
322 * Counts the number of certificates in the store.
323 *
324 * @returns Certificate count on success, UINT32_MAX on failure.
325 * @param hStore The store which certificates should be counted.
326 */
327RTDECL(uint32_t) RTCrStoreCertCount(RTCRSTORE hStore);
328
329RTDECL(int) RTCrStoreCertFindAll(RTCRSTORE hStore, PRTCRSTORECERTSEARCH pSearch);
330RTDECL(int) RTCrStoreCertFindBySubjectOrAltSubjectByRfc5280(RTCRSTORE hStore, PCRTCRX509NAME pSubject,
331 PRTCRSTORECERTSEARCH pSearch);
332RTDECL(PCRTCRCERTCTX) RTCrStoreCertSearchNext(RTCRSTORE hStore, PRTCRSTORECERTSEARCH pSearch);
333RTDECL(int) RTCrStoreCertSearchDestroy(RTCRSTORE hStore, PRTCRSTORECERTSEARCH pSearch);
334
335RTDECL(int) RTCrStoreConvertToOpenSslCertStore(RTCRSTORE hStore, uint32_t fFlags, void **ppvOpenSslStore, PRTERRINFO pErrInfo);
336RTDECL(int) RTCrStoreConvertToOpenSslCertStack(RTCRSTORE hStore, uint32_t fFlags, void **ppvOpenSslStack, PRTERRINFO pErrInfo);
337
338
339/** @} */
340
341
342/** @defgroup grp_rt_crcertctx RTCrCertCtx - (Store) Certificate Context.
343 * @{ */
344
345
346/**
347 * Certificate context.
348 *
349 * This is returned by the certificate store APIs and is part of a larger
350 * reference counted structure. All the data is read only.
351 */
352typedef struct RTCRCERTCTX
353{
354 /** Flags, RTCRCERTCTX_F_XXX. */
355 uint32_t fFlags;
356 /** The size of the (DER) encoded certificate. */
357 uint32_t cbEncoded;
358 /** Pointer to the (DER) encoded certificate. */
359 uint8_t const *pabEncoded;
360 /** Pointer to the decoded X.509 representation of the certificate.
361 * This can be NULL when pTaInfo is present. */
362 PCRTCRX509CERTIFICATE pCert;
363 /** Pointer to the decoded TrustAnchorInfo for the certificate. This can be
364 * NULL, even for trust anchors, as long as pCert isn't. */
365 PCRTCRTAFTRUSTANCHORINFO pTaInfo;
366 /** Reserved for future use. */
367 void *paReserved[2];
368} RTCRCERTCTX;
369
370/** @name RTCRCERTCTX_F_XXX.
371 * @{ */
372/** Encoding mask. */
373#define RTCRCERTCTX_F_ENC_MASK UINT32_C(0x000000ff)
374/** X.509 certificate, DER encoded. */
375#define RTCRCERTCTX_F_ENC_X509_DER UINT32_C(0x00000000)
376/** RTF-5914 trust anchor info, DER encoded. */
377#define RTCRCERTCTX_F_ENC_TAF_DER UINT32_C(0x00000001)
378#if 0
379/** Extended certificate, DER encoded. */
380#define RTCRCERTCTX_F_ENC_PKCS6_DER UINT32_C(0x00000002)
381#endif
382/** Mask containing the flags that ends up in the certificate context. */
383#define RTCRCERTCTX_F_MASK UINT32_C(0x000000ff)
384
385/** Add APIs: Add the certificate if not found. */
386#define RTCRCERTCTX_F_ADD_IF_NOT_FOUND UINT32_C(0x00010000)
387/** Add APIs: Continue on error when possible. */
388#define RTCRCERTCTX_F_ADD_CONTINUE_ON_ERROR UINT32_C(0x00020000)
389/** @} */
390
391
392RTDECL(uint32_t) RTCrCertCtxRetain(PCRTCRCERTCTX pCertCtx);
393RTDECL(uint32_t) RTCrCertCtxRelease(PCRTCRCERTCTX pCertCtx);
394
395/** @} */
396
397RT_C_DECLS_END
398
399#endif /* !IPRT_INCLUDED_crypto_store_h */
400
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