VirtualBox

source: vbox/trunk/include/iprt/crypto/pkcs7.h@ 52503

Last change on this file since 52503 was 52503, checked in by vboxsync, 10 years ago

Started working on PKCS #7 attributes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.0 KB
Line 
1/** @file
2 * IPRT - PKCS \#7, Cryptographic Message Syntax Standard (aka CMS).
3 */
4
5/*
6 * Copyright (C) 2006-2014 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_crypto_pkcs7_h
27#define ___iprt_crypto_pkcs7_h
28
29#include <iprt/asn1.h>
30#include <iprt/crypto/x509.h>
31
32
33RT_C_DECLS_BEGIN
34
35/** @defgroup grp_rt_crpkcs7 RTCrPkcs7 - PKCS \#7, Cryptographic Message Syntax Standard (aka CMS).
36 * @ingroup grp_rt_crypto
37 * @{
38 */
39
40
41/**
42 * PKCS \#7 IssuerAndSerialNumber (IPRT representation).
43 */
44typedef struct RTCRPKCS7ISSUERANDSERIALNUMBER
45{
46 /** Sequence core. */
47 RTASN1SEQUENCECORE SeqCore;
48 /** The certificate name. */
49 RTCRX509NAME Name;
50 /** The certificate serial number. */
51 RTASN1INTEGER SerialNumber;
52} RTCRPKCS7ISSUERANDSERIALNUMBER;
53/** Pointer to the IPRT representation of a PKCS \#7 IssuerAndSerialNumber. */
54typedef RTCRPKCS7ISSUERANDSERIALNUMBER *PRTCRPKCS7ISSUERANDSERIALNUMBER;
55/** Pointer to the const IPRT representation of a PKCS \#7
56 * IssuerAndSerialNumber. */
57typedef RTCRPKCS7ISSUERANDSERIALNUMBER const *PCRTCRPKCS7ISSUERANDSERIALNUMBER;
58RTASN1TYPE_STANDARD_PROTOTYPES(RTCRPKCS7ISSUERANDSERIALNUMBER, RTDECL, RTCrPkcs7IssuerAndSerialNumber, SeqCore.Asn1Core);
59
60
61/** Pointer to the IPRT representation of a PKCS \#7 SignerInfo. */
62typedef struct RTCRPKCS7SIGNERINFO *PRTCRPKCS7SIGNERINFO;
63/** Pointer to the const IPRT representation of a PKCS \#7 SignerInfo. */
64typedef struct RTCRPKCS7SIGNERINFO const *PCRTCRPKCS7SIGNERINFO;
65RTASN1_IMPL_GEN_SET_OF_TYPEDEFS_AND_PROTOS(RTCRPKCS7SIGNERINFOS, RTCRPKCS7SIGNERINFO, RTDECL, RTCrPkcs7SignerInfos);
66
67
68/**
69 * Attribute value type (for the union).
70 */
71typedef enum RTCRPKCS7ATTRIBUTETYPE
72{
73 /** Zero is invalid. */
74 RTCRPKCS7ATTRIBUTETYPE_INVALID = 0,
75 /** Not present, union is NULL. */
76 RTCRPKCS7ATTRIBUTETYPE_NOT_PRESENT,
77 /** Unknown values, pCores. */
78 RTCRPKCS7ATTRIBUTETYPE_UNKNOWN,
79 /** Object IDs, use pObjIds. */
80 RTCRPKCS7ATTRIBUTETYPE_OBJ_IDS,
81 /** Octet strings, use pOctetStrings. */
82 RTCRPKCS7ATTRIBUTETYPE_OCTET_STRINGS,
83 /** Counter signatures (PKCS \#9), use pCounterSignatures. */
84 RTCRPKCS7ATTRIBUTETYPE_COUNTER_SIGNATURES,
85 /** Signing time (PKCS \#9), use pSigningTime. */
86 RTCRPKCS7ATTRIBUTETYPE_SIGNING_TIME,
87 /** Blow the type up to 32-bits. */
88 RTCRPKCS7ATTRIBUTETYPE_32BIT_HACK = 0x7fffffff
89} RTCRPKCS7ATTRIBUTETYPE;
90
91/**
92 * PKCS \#7 Attribute (IPRT representation).
93 */
94typedef struct RTCRPKCS7ATTRIBUTE
95{
96 /** Sequence core. */
97 RTASN1SEQUENCECORE SeqCore;
98 /** The attribute type (object ID). */
99 RTASN1OBJID Type;
100 /** The type of data found in the values union. */
101 RTCRPKCS7ATTRIBUTETYPE enmType;
102 /** Value allocation. */
103 RTASN1ALLOCATION Allocation;
104 /** Values. */
105 union
106 {
107 /** ASN.1 cores (RTCRPKCS7ATTRIBUTETYPE_UNKNOWN). */
108 PRTASN1SETOFCORES pCores;
109 /** ASN.1 object identifiers (RTCRPKCS7ATTRIBUTETYPE_OBJ_IDS). */
110 PRTASN1SETOFOBJIDS pObjIds;
111 /** ASN.1 octet strings (RTCRPKCS7ATTRIBUTETYPE_OCTET_STRINGS). */
112 PRTASN1SETOFOCTETSTRINGS pOctetStrings;
113 /** Counter signatures RTCRPKCS7ATTRIBUTETYPE_COUNTER_SIGNATURES(). */
114 PRTCRPKCS7SIGNERINFOS pCounterSignatures;
115 /** Signing time(s) (RTCRPKCS7ATTRIBUTETYPE_SIGNING_TIME). */
116 PRTASN1SETOFTIMES pSigningTime;
117 } uValues;
118} RTCRPKCS7ATTRIBUTE;
119/** Pointer to the IPRT representation of a PKCS \#7 Attribute. */
120typedef RTCRPKCS7ATTRIBUTE *PRTCRPKCS7ATTRIBUTE;
121/** Pointer to the const IPRT representation of a PKCS \#7 Attribute. */
122typedef RTCRPKCS7ATTRIBUTE const *PCRTCRPKCS7ATTRIBUTE;
123RTASN1TYPE_STANDARD_PROTOTYPES(RTCRPKCS7ATTRIBUTE, RTDECL, RTCrPkcs7Attribute, SeqCore.Asn1Core);
124
125RTASN1_IMPL_GEN_SET_OF_TYPEDEFS_AND_PROTOS(RTCRPKCS7ATTRIBUTES, RTCRPKCS7ATTRIBUTE, RTDECL, RTCrPkcs7Attributes);
126
127
128/**
129 * One PKCS \#7 SignerInfo (IPRT representation).
130 */
131typedef struct RTCRPKCS7SIGNERINFO
132{
133 /** Sequence core. */
134 RTASN1SEQUENCECORE SeqCore;
135 /** The structure version (RTCRPKCS7SIGNERINFO_V1). */
136 RTASN1INTEGER Version;
137 /** The issuer and serial number of the certificate used to produce the
138 * encrypted digest below. */
139 RTCRPKCS7ISSUERANDSERIALNUMBER IssuerAndSerialNumber;
140 /** The digest algorithm use to digest the signed content. */
141 RTCRX509ALGORITHMIDENTIFIER DigestAlgorithm;
142 /** Authenticated attributes, optional [0].
143 * @todo Check how other producers formats this. The microsoft one does not
144 * have explicit tags, but combines it with the SET OF. */
145 RTCRPKCS7ATTRIBUTES AuthenticatedAttributes;
146 /** The digest encryption algorithm use to encrypt the digest of the signed
147 * content. */
148 RTCRX509ALGORITHMIDENTIFIER DigestEncryptionAlgorithm;
149 /** The encrypted digest. */
150 RTASN1OCTETSTRING EncryptedDigest;
151 /** Unauthenticated attributes, optional [1].
152 * @todo Check how other producers formats this. The microsoft one does not
153 * have explicit tags, but combines it with the SET OF. */
154 RTCRPKCS7ATTRIBUTES UnauthenticatedAttributes;
155} RTCRPKCS7SIGNERINFO;
156RTASN1TYPE_STANDARD_PROTOTYPES(RTCRPKCS7SIGNERINFO, RTDECL, RTCrPkcs7SignerInfo, SeqCore.Asn1Core);
157
158/** RTCRPKCS7SIGNERINFO::Version value. */
159#define RTCRPKCS7SIGNERINFO_V1 1
160
161/** @name PKCS \#9 Attribute IDs
162 * @{ */
163/** Content type (RFC-2630 11.1).
164 * Value: Object Identifier */
165#define RTCR_PKCS9_ID_CONTENT_TYPE_OID "1.2.840.113549.1.9.3"
166/** Message digest (RFC-2630 11.2).
167 * Value: Octet string. */
168#define RTCR_PKCS9_ID_MESSAGE_DIGEST_OID "1.2.840.113549.1.9.4"
169/** Signing time (RFC-2630 11.3).
170 * Value: Octet string. */
171#define RTCR_PKCS9_ID_SIGNING_TIME_OID "1.2.840.113549.1.9.5"
172/** Counter signature (RFC-2630 11.4).
173 * Value: SignerInfo. */
174#define RTCR_PKCS9_ID_COUNTER_SIGNATURE_OID "1.2.840.113549.1.9.6"
175/** @} */
176
177
178/**
179 * PKCS \#7 ContentInfo (IPRT representation).
180 */
181typedef struct RTCRPKCS7CONTENTINFO
182{
183 /** Sequence core. */
184 RTASN1SEQUENCECORE SeqCore;
185 /** Object ID identifying the content below. */
186 RTASN1OBJID ContentType;
187 /** Content, optional, explicit tag 0.
188 *
189 * Hack alert! This should've been an explict context tag 0 structure with a
190 * type selected according to ContentType. However, it's simpler to replace the
191 * explicit context with an OCTET STRING with implict tag 0. Then we can tag
192 * along on the encapsulation logic RTASN1OCTETSTRING provides for the dynamic
193 * inner type. The default decoder code will detect known structures as
194 * outlined in the union below, and decode the octet string content as an
195 * anonymous RTASN1CORE if not known.
196 *
197 * If the user want to decode the octet string content differently, it can do so
198 * by destroying and freeing the current encapsulated pointer, replacing it with
199 * it's own. (Of course following the RTASN1OCTETSTRING rules.) Just remember
200 * to also update the value in the union.
201 *
202 * @remarks What's signed and verified is Content.pEncapsulated->uData.pv.
203 */
204 RTASN1OCTETSTRING Content;
205 /** Same as Content.pEncapsulated, except a choice of known types. */
206 union
207 {
208 /** ContentType is RTCRPKCS7SIGNEDDATA_OID. */
209 struct RTCRPKCS7SIGNEDDATA *pSignedData;
210 /** ContentType is RTCRSPCINDIRECTDATACONTENT_OID. */
211 struct RTCRSPCINDIRECTDATACONTENT *pIndirectDataContent;
212 /** Generic / Unknown / User. */
213 PRTASN1CORE pCore;
214 } u;
215} RTCRPKCS7CONTENTINFO;
216/** Pointer to the IPRT representation of a PKCS \#7 ContentInfo. */
217typedef RTCRPKCS7CONTENTINFO *PRTCRPKCS7CONTENTINFO;
218/** Pointer to the const IPRT representation of a PKCS \#7 ContentInfo. */
219typedef RTCRPKCS7CONTENTINFO const *PCRTCRPKCS7CONTENTINFO;
220
221RTASN1TYPE_STANDARD_PROTOTYPES(RTCRPKCS7CONTENTINFO, RTDECL, RTCrPkcs7ContentInfo, SeqCore.Asn1Core);
222
223RTDECL(bool) RTCrPkcs7ContentInfo_IsSignedData(PCRTCRPKCS7CONTENTINFO pThis);
224
225
226/**
227 * PKCS \#7 SignedData (IPRT representation).
228 */
229typedef struct RTCRPKCS7SIGNEDDATA
230{
231 /** Sequence core. */
232 RTASN1SEQUENCECORE SeqCore;
233 /** The structure version value (1). */
234 RTASN1INTEGER Version;
235 /** The digest algorithms that are used to signed the content (ContentInfo). */
236 RTCRX509ALGORITHMIDENTIFIERS DigestAlgorithms;
237 /** The content that's being signed. */
238 RTCRPKCS7CONTENTINFO ContentInfo;
239 /** Certificates, optional, implicit tag 0. (Required by Authenticode.) */
240 RTCRX509CERTIFICATES Certificates;
241 /** Certificate revocation lists, optional, implicit tag 1.
242 * Not used by Authenticode, so currently stubbed. */
243 RTASN1CORE Crls;
244 /** Signer infos. */
245 RTCRPKCS7SIGNERINFOS SignerInfos;
246} RTCRPKCS7SIGNEDDATA;
247/** Pointer to the IPRT representation of a PKCS \#7 SignedData. */
248typedef RTCRPKCS7SIGNEDDATA *PRTCRPKCS7SIGNEDDATA;
249/** Pointer to the const IPRT representation of a PKCS \#7 SignedData. */
250typedef RTCRPKCS7SIGNEDDATA const *PCRTCRPKCS7SIGNEDDATA;
251RTASN1TYPE_STANDARD_PROTOTYPES(RTCRPKCS7SIGNEDDATA, RTDECL, RTCrPkcs7SignedData, SeqCore.Asn1Core);
252
253/** PKCS \#7 SignedData object ID. */
254#define RTCRPKCS7SIGNEDDATA_OID "1.2.840.113549.1.7.2"
255
256/** PKCS \#7 SignedData version number 1. */
257#define RTCRPKCS7SIGNEDDATA_V1 1
258
259
260/** @name RTCRPKCS7SIGNEDDATA_SANITY_F_XXX - Flags for RTPkcs7SignedDataCheckSantiy.
261 * @{ */
262/** Check for authenticode restrictions. */
263#define RTCRPKCS7SIGNEDDATA_SANITY_F_AUTHENTICODE RT_BIT_32(0)
264/** Check that all the hash algorithms are known to IPRT. */
265#define RTCRPKCS7SIGNEDDATA_SANITY_F_ONLY_KNOWN_HASH RT_BIT_32(1)
266/** Require signing certificate to be present. */
267#define RTCRPKCS7SIGNEDDATA_SANITY_F_SIGNING_CERT_PRESENT RT_BIT_32(2)
268/** @} */
269
270
271/**
272 * PKCS \#7 DigestInfo (IPRT representation).
273 */
274typedef struct RTCRPKCS7DIGESTINFO
275{
276 /** Sequence core. */
277 RTASN1SEQUENCECORE SeqCore;
278 /** The digest algorithm use to digest the signed content. */
279 RTCRX509ALGORITHMIDENTIFIER DigestAlgorithm;
280 /** The digest. */
281 RTASN1OCTETSTRING Digest;
282} RTCRPKCS7DIGESTINFO;
283/** Pointer to the IPRT representation of a PKCS \#7 DigestInfo object. */
284typedef RTCRPKCS7DIGESTINFO *PRTCRPKCS7DIGESTINFO;
285/** Pointer to the const IPRT representation of a PKCS \#7 DigestInfo object. */
286typedef RTCRPKCS7DIGESTINFO const *PCRTCRPKCS7DIGESTINFO;
287RTASN1TYPE_STANDARD_PROTOTYPES(RTCRPKCS7DIGESTINFO, RTDECL, RTCrPkcs7DigestInfo, SeqCore.Asn1Core);
288
289
290/**
291 * Callback function for use with RTCrPkcs7VerifySignedData.
292 *
293 * @returns IPRT status code.
294 * @param pCert The certificate to verify.
295 * @param hCertPaths Unless the certificate is trusted directly, this
296 * is a reference to the certificate path builder
297 * and verifier instance that we used to establish
298 * at least valid trusted path to @a pCert. The
299 * callback can use this to enforce additional
300 * certificate lineage requirements, effective
301 * policy checks and whatnot.
302 * This is NIL_RTCRX509CERTPATHS if the certificate
303 * is directly trusted.
304 * @param pvUser The user argument.
305 * @param pErrInfo Optional error info buffer.
306 */
307typedef DECLCALLBACK(int) RTCRPKCS7VERIFYCERTCALLBACK(PCRTCRX509CERTIFICATE pCert, RTCRX509CERTPATHS hCertPaths,
308 void *pvUser, PRTERRINFO pErrInfo);
309/** Pointer to a RTCRPKCS7VERIFYCERTCALLBACK callback. */
310typedef RTCRPKCS7VERIFYCERTCALLBACK *PRTCRPKCS7VERIFYCERTCALLBACK;
311
312/**
313 * @callback_method_impl{RTCRPKCS7VERIFYCERTCALLBACK,
314 * Default implementation that checks for the DigitalSignature KeyUsage bit.}
315 */
316RTDECL(int) RTCrPkcs7VerifyCertCallbackDefault(PCRTCRX509CERTIFICATE pCert, RTCRX509CERTPATHS hCertPaths,
317 void *pvUser, PRTERRINFO pErrInfo);
318
319/**
320 * @callback_method_impl{RTCRPKCS7VERIFYCERTCALLBACK,
321 * Standard code signing. Use this for Microsoft SPC.}
322 */
323RTDECL(int) RTCrPkcs7VerifyCertCallbackCodeSigning(PCRTCRX509CERTIFICATE pCert, RTCRX509CERTPATHS hCertPaths,
324 void *pvUser, PRTERRINFO pErrInfo);
325
326/**
327 * Verifies PKCS \#7 SignedData.
328 *
329 * For compatability with alternative crypto providers, the user must work on
330 * the top level PKCS \#7 structure instead directly on the SignedData.
331 *
332 * @returns IPRT status code.
333 * @param pContentInfo PKCS \#7 content info structure.
334 * @param fFlags RTCRPKCS7VERIFY_SD_F_XXX.
335 * @param hAdditionalCerts Store containing additional certificates to
336 * supplement those mentioned in the signed data.
337 * @param hTrustedCerts Store containing trusted certificates.
338 * @param pValidationTime The time we're supposed to validate the
339 * certificates chains at.
340 * @param pfnVerifyCert Callback for checking that a certificate used
341 * for signing the data is suitable.
342 * @param pvUser User argument for the callback.
343 * @param pErrInfo Optional error info buffer.
344 */
345RTDECL(int) RTCrPkcs7VerifySignedData(PCRTCRPKCS7CONTENTINFO pContentInfo, uint32_t fFlags,
346 RTCRSTORE hAdditionalCerts, RTCRSTORE hTrustedCerts,
347 PCRTTIMESPEC pValidationTime, PRTCRPKCS7VERIFYCERTCALLBACK pfnVerifyCert, void *pvUser,
348 PRTERRINFO pErrInfo);
349
350/** @name RTCRPKCS7VERIFY_SD_F_XXX - Flags for RTCrPkcs7VerifySignedData
351 * @{ */
352/** @} */
353
354/** @} */
355
356RT_C_DECLS_END
357
358#endif
359
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