VirtualBox

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

Last change on this file since 86065 was 84329, checked in by vboxsync, 5 years ago

IPRT: Adding RTCrStoreCreateInMemEx that allows specifying a parent store to provide a kind of union store. bugref:9699

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.0 KB
Line 
1/** @file
2 * IPRT - Cryptographic (Certificate) Store.
3 */
4
5/*
6 * Copyright (C) 2006-2020 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 /** End of valid values. */
136 RTCRSTOREID_END,
137 /** Traditional enum type compression prevention hack. */
138 RTCRSTOREID_32BIT_HACK = 0x7fffffff
139} RTCRSTOREID;
140
141/**
142 * Creates a snapshot of a standard store.
143 *
144 * This will return an in-memory store containing all data from the given store.
145 * There will be no duplicates in this one.
146 *
147 * @returns IPRT status code.
148 * @retval VWRN_ALREADY_EXISTS if the certificate is already present and
149 * RTCRCERTCTX_F_ADD_IF_NOT_FOUND was specified.
150 * @param phStore Where to return the store handle. Use
151 * RTCrStoreRelease to release it.
152 * @param enmStoreId The store to snapshot.
153 * @param pErrInfo Where to return additional error/warning info.
154 * Optional.
155 */
156RTDECL(int) RTCrStoreCreateSnapshotById(PRTCRSTORE phStore, RTCRSTOREID enmStoreId, PRTERRINFO pErrInfo);
157
158RTDECL(int) RTCrStoreCreateSnapshotOfUserAndSystemTrustedCAsAndCerts(PRTCRSTORE phStore, PRTERRINFO pErrInfo);
159
160RTDECL(int) RTCrStoreCreateInMem(PRTCRSTORE phStore, uint32_t cSizeHint);
161RTDECL(int) RTCrStoreCreateInMemEx(PRTCRSTORE phStore, uint32_t cSizeHint, RTCRSTORE hParentStore);
162
163RTDECL(uint32_t) RTCrStoreRetain(RTCRSTORE hStore);
164RTDECL(uint32_t) RTCrStoreRelease(RTCRSTORE hStore);
165RTDECL(PCRTCRCERTCTX) RTCrStoreCertByIssuerAndSerialNo(RTCRSTORE hStore, PCRTCRX509NAME pIssuer, PCRTASN1INTEGER pSerialNo);
166
167/**
168 * Add a certificate to the store.
169 *
170 * @returns IPRT status code.
171 * @retval VWRN_ALREADY_EXISTS if the certificate is already present and
172 * RTCRCERTCTX_F_ADD_IF_NOT_FOUND was specified.
173 * @retval VERR_WRITE_PROTECT if the store doesn't support adding.
174 * @param hStore The store to add the certificate to.
175 * @param fFlags RTCRCERTCTX_F_XXX. Encoding must be specified.
176 * RTCRCERTCTX_F_ADD_IF_NOT_FOUND is supported.
177 * @param pvSrc The encoded certificate bytes.
178 * @param cbSrc The size of the encoded certificate.
179 * @param pErrInfo Where to return additional error/warning info.
180 * Optional.
181 */
182RTDECL(int) RTCrStoreCertAddEncoded(RTCRSTORE hStore, uint32_t fFlags, void const *pvSrc, size_t cbSrc, PRTERRINFO pErrInfo);
183
184/**
185 * Add an X.509 packaged certificate to the store.
186 *
187 * @returns IPRT status code.
188 * @retval VWRN_ALREADY_EXISTS if the certificate is already present and
189 * RTCRCERTCTX_F_ADD_IF_NOT_FOUND was specified.
190 * @retval VERR_WRITE_PROTECT if the store doesn't support adding.
191 * @param hStore The store to add the certificate to.
192 * @param fFlags RTCRCERTCTX_F_XXX. Encoding must is optional,
193 * but must be RTCRCERTCTX_F_ENC_X509_DER if given.
194 * RTCRCERTCTX_F_ADD_IF_NOT_FOUND is supported.
195 * @param pCertificate The certificate to add. We may have to encode
196 * it, thus not const.
197 * @param pErrInfo Where to return additional error/warning info.
198 * Optional.
199 */
200RTDECL(int) RTCrStoreCertAddX509(RTCRSTORE hStore, uint32_t fFlags, PRTCRX509CERTIFICATE pCertificate, PRTERRINFO pErrInfo);
201
202/**
203 * Adds certificates from files in the specified directory.
204 *
205 * @returns IPRT status code. Even when RTCRCERTCTX_F_ADD_CONTINUE_ON_ERROR is
206 * used, an error is returned as an error (and not a warning).
207 *
208 * @param hStore The store to add the certificate(s) to.
209 * @param fFlags RTCRCERTCTX_F_ADD_IF_NOT_FOUND and/or
210 * RTCRCERTCTX_F_ADD_CONTINUE_ON_ERROR.
211 * @param pszDir The path to the directory.
212 * @param paSuffixes List of suffixes of files to process.
213 * @param cSuffixes Number of suffixes. If this is 0, all files are
214 * processed.
215 * @param pErrInfo Where to return additional error/warning info.
216 * Optional.
217 */
218RTDECL(int) RTCrStoreCertAddFromDir(RTCRSTORE hStore, uint32_t fFlags, const char *pszDir,
219 PCRTSTRTUPLE paSuffixes, size_t cSuffixes, PRTERRINFO pErrInfo);
220
221RTDECL(int) RTCrStoreCertAddWantedFromDir(RTCRSTORE hStore, uint32_t fFlags,
222 const char *pszDir, PCRTSTRTUPLE paSuffixes, size_t cSuffixes,
223 PCRTCRCERTWANTED paWanted, size_t cWanted, bool *pafFound, PRTERRINFO pErrInfo);
224
225/**
226 * Adds certificates from the specified file.
227 *
228 * The supported file formats are:
229 * - PEM (base 64 blobs wrapped in -----BEGIN / END----). Support multiple
230 * certificates in one file.
231 * - Binary DER ASN.1 certificate. Only one per file.
232 * - Java key store version 2.
233 *
234 * @returns IPRT status code. Even when RTCRCERTCTX_F_ADD_CONTINUE_ON_ERROR is
235 * used, an error is returned as an error (and not a warning).
236 *
237 * @param hStore The store to add the certificate(s) to.
238 * @param fFlags RTCRCERTCTX_F_ADD_IF_NOT_FOUND and/or
239 * RTCRCERTCTX_F_ADD_CONTINUE_ON_ERROR.
240 * @param pszFilename The filename.
241 * @param pErrInfo Where to return additional error/warning info.
242 * Optional.
243 */
244RTDECL(int) RTCrStoreCertAddFromFile(RTCRSTORE hStore, uint32_t fFlags, const char *pszFilename, PRTERRINFO pErrInfo);
245
246RTDECL(int) RTCrStoreCertAddWantedFromFile(RTCRSTORE hStore, uint32_t fFlags, const char *pszFilename,
247 PCRTCRCERTWANTED paWanted, size_t cWanted, bool *pafFound, PRTERRINFO pErrInfo);
248
249/**
250 * Adds certificates from the specified java key store file.
251 *
252 * @returns IPRT status code. Even when RTCRCERTCTX_F_ADD_CONTINUE_ON_ERROR is
253 * used, an error is returned as an error (and not a warning).
254 *
255 * @param hStore The store to add the certificate(s) to.
256 * @param fFlags RTCRCERTCTX_F_ADD_IF_NOT_FOUND and/or
257 * RTCRCERTCTX_F_ADD_CONTINUE_ON_ERROR.
258 * @param pszFilename The path to the JKS file.
259 * @param pErrInfo Where to return additional error/warning info.
260 * Optional.
261 */
262RTDECL(int) RTCrStoreCertAddFromJavaKeyStore(RTCRSTORE hStore, uint32_t fFlags, const char *pszFilename, PRTERRINFO pErrInfo);
263
264/**
265 * Adds certificates from an in-memory java key store.
266 *
267 * @returns IPRT status code. Even when RTCRCERTCTX_F_ADD_CONTINUE_ON_ERROR is
268 * used, an error is returned as an error (and not a warning).
269 *
270 * @param hStore The store to add the certificate(s) to.
271 * @param fFlags RTCRCERTCTX_F_ADD_IF_NOT_FOUND and/or
272 * RTCRCERTCTX_F_ADD_CONTINUE_ON_ERROR.
273 * @param pvContent Pointer to the key store bytes.
274 * @param cbContent The size of the key store.
275 * @param pszErrorName The file name or whatever helpful indicator the
276 * caller want in the error messages.
277 * @param pErrInfo Where to return additional error/warning info.
278 * Optional.
279 */
280RTDECL(int) RTCrStoreCertAddFromJavaKeyStoreInMem(RTCRSTORE hStore, uint32_t fFlags, void const *pvContent, size_t cbContent,
281 const char *pszErrorName, PRTERRINFO pErrInfo);
282
283/**
284 * Adds all certificates from @a hStoreSrc into @a hStore.
285 *
286 * @returns IPRT status code. Even when RTCRCERTCTX_F_ADD_CONTINUE_ON_ERROR is
287 * used, an error is returned as an error (and not a warning).
288 *
289 * @param hStore The destination store.
290 * @param fFlags RTCRCERTCTX_F_ADD_IF_NOT_FOUND and/or
291 * RTCRCERTCTX_F_ADD_CONTINUE_ON_ERROR.
292 * @param hStoreSrc The source store.
293 */
294RTDECL(int) RTCrStoreCertAddFromStore(RTCRSTORE hStore, uint32_t fFlags, RTCRSTORE hStoreSrc);
295
296RTDECL(int) RTCrStoreCertAddWantedFromStore(RTCRSTORE hStore, uint32_t fFlags, RTCRSTORE hSrcStore,
297 PCRTCRCERTWANTED paWanted, size_t cWanted, bool *pafFound);
298
299RTDECL(int) RTCrStoreCertCheckWanted(RTCRSTORE hStore, PCRTCRCERTWANTED paWanted, size_t cWanted, bool *pafFound);
300
301
302RTDECL(int) RTCrStoreCertAddWantedFromFishingExpedition(RTCRSTORE hStore, uint32_t fFlags,
303 PCRTCRCERTWANTED paWanted, size_t cWanted,
304 bool *pafFound, PRTERRINFO pErrInfo);
305
306/**
307 * Exports the certificates in the store to a PEM file
308 *
309 * @returns IPRT status code.
310 * @param hStore The store which certificates should be exported.
311 * @param fFlags Reserved for the future, MBZ.
312 * @param pszFilename The name of the destination PEM file. This will
313 * be truncated.
314 */
315RTDECL(int) RTCrStoreCertExportAsPem(RTCRSTORE hStore, uint32_t fFlags, const char *pszFilename);
316
317/**
318 * Counts the number of certificates in the store.
319 *
320 * @returns Certificate count on success, UINT32_MAX on failure.
321 * @param hStore The store which certificates should be counted.
322 */
323RTDECL(uint32_t) RTCrStoreCertCount(RTCRSTORE hStore);
324
325RTDECL(int) RTCrStoreCertFindAll(RTCRSTORE hStore, PRTCRSTORECERTSEARCH pSearch);
326RTDECL(int) RTCrStoreCertFindBySubjectOrAltSubjectByRfc5280(RTCRSTORE hStore, PCRTCRX509NAME pSubject,
327 PRTCRSTORECERTSEARCH pSearch);
328RTDECL(PCRTCRCERTCTX) RTCrStoreCertSearchNext(RTCRSTORE hStore, PRTCRSTORECERTSEARCH pSearch);
329RTDECL(int) RTCrStoreCertSearchDestroy(RTCRSTORE hStore, PRTCRSTORECERTSEARCH pSearch);
330
331RTDECL(int) RTCrStoreConvertToOpenSslCertStore(RTCRSTORE hStore, uint32_t fFlags, void **ppvOpenSslStore, PRTERRINFO pErrInfo);
332RTDECL(int) RTCrStoreConvertToOpenSslCertStack(RTCRSTORE hStore, uint32_t fFlags, void **ppvOpenSslStack, PRTERRINFO pErrInfo);
333
334
335/** @} */
336
337
338/** @defgroup grp_rt_crcertctx RTCrCertCtx - (Store) Certificate Context.
339 * @{ */
340
341
342/**
343 * Certificate context.
344 *
345 * This is returned by the certificate store APIs and is part of a larger
346 * reference counted structure. All the data is read only.
347 */
348typedef struct RTCRCERTCTX
349{
350 /** Flags, RTCRCERTCTX_F_XXX. */
351 uint32_t fFlags;
352 /** The size of the (DER) encoded certificate. */
353 uint32_t cbEncoded;
354 /** Pointer to the (DER) encoded certificate. */
355 uint8_t const *pabEncoded;
356 /** Pointer to the decoded X.509 representation of the certificate.
357 * This can be NULL when pTaInfo is present. */
358 PCRTCRX509CERTIFICATE pCert;
359 /** Pointer to the decoded TrustAnchorInfo for the certificate. This can be
360 * NULL, even for trust anchors, as long as pCert isn't. */
361 PCRTCRTAFTRUSTANCHORINFO pTaInfo;
362 /** Reserved for future use. */
363 void *paReserved[2];
364} RTCRCERTCTX;
365
366/** @name RTCRCERTCTX_F_XXX.
367 * @{ */
368/** Encoding mask. */
369#define RTCRCERTCTX_F_ENC_MASK UINT32_C(0x000000ff)
370/** X.509 certificate, DER encoded. */
371#define RTCRCERTCTX_F_ENC_X509_DER UINT32_C(0x00000000)
372/** RTF-5914 trust anchor info, DER encoded. */
373#define RTCRCERTCTX_F_ENC_TAF_DER UINT32_C(0x00000001)
374#if 0
375/** Extended certificate, DER encoded. */
376#define RTCRCERTCTX_F_ENC_PKCS6_DER UINT32_C(0x00000002)
377#endif
378/** Mask containing the flags that ends up in the certificate context. */
379#define RTCRCERTCTX_F_MASK UINT32_C(0x000000ff)
380
381/** Add APIs: Add the certificate if not found. */
382#define RTCRCERTCTX_F_ADD_IF_NOT_FOUND UINT32_C(0x00010000)
383/** Add APIs: Continue on error when possible. */
384#define RTCRCERTCTX_F_ADD_CONTINUE_ON_ERROR UINT32_C(0x00020000)
385/** @} */
386
387
388RTDECL(uint32_t) RTCrCertCtxRetain(PCRTCRCERTCTX pCertCtx);
389RTDECL(uint32_t) RTCrCertCtxRelease(PCRTCRCERTCTX pCertCtx);
390
391/** @} */
392
393RT_C_DECLS_END
394
395#endif /* !IPRT_INCLUDED_crypto_store_h */
396
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