VirtualBox

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

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

IPRT/RTAsn1,RTCrPkcs7,RTCrSpc: Generate setters for dynamic members. bugref:8691

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 33.4 KB
Line 
1/** @file
2 * IPRT - PKCS \#7, Cryptographic Message Syntax Standard (aka CMS).
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_pkcs7_h
27#define IPRT_INCLUDED_crypto_pkcs7_h
28#ifndef RT_WITHOUT_PRAGMA_ONCE
29# pragma once
30#endif
31
32#include <iprt/asn1.h>
33#include <iprt/crypto/x509.h>
34
35
36RT_C_DECLS_BEGIN
37
38struct RTCRPKCS7CONTENTINFO;
39
40
41/** @defgroup grp_rt_crpkcs7 RTCrPkcs7 - PKCS \#7, Cryptographic Message Syntax Standard (aka CMS).
42 * @ingroup grp_rt_crypto
43 * @{
44 */
45
46/** PKCS \#7 data object ID.*/
47#define RTCR_PKCS7_DATA_OID "1.2.840.113549.1.7.1"
48/** PKCS \#7 signedData object ID. */
49#define RTCR_PKCS7_SIGNED_DATA_OID "1.2.840.113549.1.7.2"
50/** PKCS \#7 envelopedData object ID. */
51#define RTCR_PKCS7_ENVELOPED_DATA_OID "1.2.840.113549.1.7.3"
52/** PKCS \#7 signedAndEnvelopedData object ID. */
53#define RTCR_PKCS7_SIGNED_AND_ENVELOPED_DATA_OID "1.2.840.113549.1.7.4"
54/** PKCS \#7 digestedData object ID. */
55#define RTCR_PKCS7_DIGESTED_DATA_OID "1.2.840.113549.1.7.5"
56/** PKCS \#7 encryptedData object ID. */
57#define RTCR_PKCS7_ENCRYPTED_DATA_OID "1.2.840.113549.1.7.6"
58
59
60/**
61 * PKCS \#7 IssuerAndSerialNumber (IPRT representation).
62 */
63typedef struct RTCRPKCS7ISSUERANDSERIALNUMBER
64{
65 /** Sequence core. */
66 RTASN1SEQUENCECORE SeqCore;
67 /** The certificate name. */
68 RTCRX509NAME Name;
69 /** The certificate serial number. */
70 RTASN1INTEGER SerialNumber;
71} RTCRPKCS7ISSUERANDSERIALNUMBER;
72/** Pointer to the IPRT representation of a PKCS \#7 IssuerAndSerialNumber. */
73typedef RTCRPKCS7ISSUERANDSERIALNUMBER *PRTCRPKCS7ISSUERANDSERIALNUMBER;
74/** Pointer to the const IPRT representation of a PKCS \#7
75 * IssuerAndSerialNumber. */
76typedef RTCRPKCS7ISSUERANDSERIALNUMBER const *PCRTCRPKCS7ISSUERANDSERIALNUMBER;
77RTASN1TYPE_STANDARD_PROTOTYPES(RTCRPKCS7ISSUERANDSERIALNUMBER, RTDECL, RTCrPkcs7IssuerAndSerialNumber, SeqCore.Asn1Core);
78
79
80/** Pointer to the IPRT representation of a PKCS \#7 SignerInfo. */
81typedef struct RTCRPKCS7SIGNERINFO *PRTCRPKCS7SIGNERINFO;
82/** Pointer to the const IPRT representation of a PKCS \#7 SignerInfo. */
83typedef struct RTCRPKCS7SIGNERINFO const *PCRTCRPKCS7SIGNERINFO;
84RTASN1_IMPL_GEN_SET_OF_TYPEDEFS_AND_PROTOS(RTCRPKCS7SIGNERINFOS, RTCRPKCS7SIGNERINFO, RTDECL, RTCrPkcs7SignerInfos);
85
86
87/**
88 * Attribute value type (for the union).
89 */
90typedef enum RTCRPKCS7ATTRIBUTETYPE
91{
92 /** Zero is invalid. */
93 RTCRPKCS7ATTRIBUTETYPE_INVALID = 0,
94 /** Not present, union is NULL. */
95 RTCRPKCS7ATTRIBUTETYPE_NOT_PRESENT,
96 /** Unknown values, pCores. */
97 RTCRPKCS7ATTRIBUTETYPE_UNKNOWN,
98 /** Object IDs, use pObjIds. */
99 RTCRPKCS7ATTRIBUTETYPE_OBJ_IDS,
100 /** Octet strings, use pOctetStrings. */
101 RTCRPKCS7ATTRIBUTETYPE_OCTET_STRINGS,
102 /** Counter signatures (PKCS \#9), use pCounterSignatures. */
103 RTCRPKCS7ATTRIBUTETYPE_COUNTER_SIGNATURES,
104 /** Signing time (PKCS \#9), use pSigningTime. */
105 RTCRPKCS7ATTRIBUTETYPE_SIGNING_TIME,
106 /** Microsoft timestamp info (RFC-3161) signed data, use pContentInfo. */
107 RTCRPKCS7ATTRIBUTETYPE_MS_TIMESTAMP,
108 /** Microsoft nested PKCS\#7 signature (signtool /as). */
109 RTCRPKCS7ATTRIBUTETYPE_MS_NESTED_SIGNATURE,
110 /** Microsoft statement type, use pObjIdSeqs. */
111 RTCRPKCS7ATTRIBUTETYPE_MS_STATEMENT_TYPE,
112 /** Apple plist with the all code directory digests, use pOctetStrings. */
113 RTCRPKCS7ATTRIBUTETYPE_APPLE_MULTI_CD_PLIST,
114 /** Blow the type up to 32-bits. */
115 RTCRPKCS7ATTRIBUTETYPE_32BIT_HACK = 0x7fffffff
116} RTCRPKCS7ATTRIBUTETYPE;
117
118/**
119 * PKCS \#7 Attribute (IPRT representation).
120 */
121typedef struct RTCRPKCS7ATTRIBUTE
122{
123 /** Sequence core. */
124 RTASN1SEQUENCECORE SeqCore;
125 /** The attribute type (object ID). */
126 RTASN1OBJID Type;
127 /** The type of data found in the values union. */
128 RTCRPKCS7ATTRIBUTETYPE enmType;
129 /** Value allocation. */
130 RTASN1ALLOCATION Allocation;
131 /** Values. */
132 union
133 {
134 /** ASN.1 cores (RTCRPKCS7ATTRIBUTETYPE_UNKNOWN). */
135 PRTASN1SETOFCORES pCores;
136 /** ASN.1 object identifiers (RTCRPKCS7ATTRIBUTETYPE_OBJ_IDS). */
137 PRTASN1SETOFOBJIDS pObjIds;
138 /** Sequence of ASN.1 object identifiers (RTCRPKCS7ATTRIBUTETYPE_MS_STATEMENT_TYPE). */
139 PRTASN1SETOFOBJIDSEQS pObjIdSeqs;
140 /** ASN.1 octet strings (RTCRPKCS7ATTRIBUTETYPE_OCTET_STRINGS). */
141 PRTASN1SETOFOCTETSTRINGS pOctetStrings;
142 /** Counter signatures RTCRPKCS7ATTRIBUTETYPE_COUNTER_SIGNATURES(). */
143 PRTCRPKCS7SIGNERINFOS pCounterSignatures;
144 /** Signing time(s) (RTCRPKCS7ATTRIBUTETYPE_SIGNING_TIME). */
145 PRTASN1SETOFTIMES pSigningTime;
146 /** Microsoft timestamp (RFC-3161 signed data, RTCRPKCS7ATTRIBUTETYPE_MS_TIMESTAMP),
147 * Microsoft nested signature (RTCRPKCS7ATTRIBUTETYPE_MS_NESTED_SIGNATURE). */
148 struct RTCRPKCS7SETOFCONTENTINFOS *pContentInfos;
149 } uValues;
150} RTCRPKCS7ATTRIBUTE;
151/** Pointer to the IPRT representation of a PKCS \#7 Attribute. */
152typedef RTCRPKCS7ATTRIBUTE *PRTCRPKCS7ATTRIBUTE;
153/** Pointer to the const IPRT representation of a PKCS \#7 Attribute. */
154typedef RTCRPKCS7ATTRIBUTE const *PCRTCRPKCS7ATTRIBUTE;
155RTASN1TYPE_STANDARD_PROTOTYPES(RTCRPKCS7ATTRIBUTE, RTDECL, RTCrPkcs7Attribute, SeqCore.Asn1Core);
156
157RTDECL(int) RTCrPkcs7Attribute_SetAppleMultiCdPlist(PRTCRPKCS7ATTRIBUTE pThis, PCRTASN1SETOFOCTETSTRINGS pToClone,
158 PCRTASN1ALLOCATORVTABLE pAllocator);
159RTDECL(int) RTCrPkcs7Attribute_SetContentType(PRTCRPKCS7ATTRIBUTE pThis, PCRTASN1SETOFOBJIDS pToClone,
160 PCRTASN1ALLOCATORVTABLE pAllocator);
161RTDECL(int) RTCrPkcs7Attribute_SetCounterSignatures(PRTCRPKCS7ATTRIBUTE pThis, PCRTCRPKCS7SIGNERINFOS pToClone,
162 PCRTASN1ALLOCATORVTABLE pAllocator);
163RTDECL(int) RTCrPkcs7Attribute_SetMessageDigest(PRTCRPKCS7ATTRIBUTE pThis, PCRTASN1SETOFOCTETSTRINGS pToClone,
164 PCRTASN1ALLOCATORVTABLE pAllocator);
165RTDECL(int) RTCrPkcs7Attribute_SetMsStatementType(PRTCRPKCS7ATTRIBUTE pThis, PCRTASN1SETOFOBJIDSEQS pToClone,
166 PCRTASN1ALLOCATORVTABLE pAllocator);
167RTDECL(int) RTCrPkcs7Attribute_SetMsNestedSignature(PRTCRPKCS7ATTRIBUTE pThis, struct RTCRPKCS7SETOFCONTENTINFOS const *pToClone,
168 PCRTASN1ALLOCATORVTABLE pAllocator);
169RTDECL(int) RTCrPkcs7Attribute_SetMsTimestamp(PRTCRPKCS7ATTRIBUTE pThis, struct RTCRPKCS7SETOFCONTENTINFOS const *pToClone,
170 PCRTASN1ALLOCATORVTABLE pAllocator);
171RTDECL(int) RTCrPkcs7Attribute_SetSigningTime(PRTCRPKCS7ATTRIBUTE pThis, PCRTASN1SETOFTIMES pToClone,
172 PCRTASN1ALLOCATORVTABLE pAllocator);
173
174RTASN1_IMPL_GEN_SET_OF_TYPEDEFS_AND_PROTOS(RTCRPKCS7ATTRIBUTES, RTCRPKCS7ATTRIBUTE, RTDECL, RTCrPkcs7Attributes);
175
176
177/**
178 * One PKCS \#7 SignerInfo (IPRT representation).
179 */
180typedef struct RTCRPKCS7SIGNERINFO
181{
182 /** Sequence core. */
183 RTASN1SEQUENCECORE SeqCore;
184 /** The structure version (RTCRPKCS7SIGNERINFO_V1). */
185 RTASN1INTEGER Version;
186 /** The issuer and serial number of the certificate used to produce the
187 * encrypted digest below. */
188 RTCRPKCS7ISSUERANDSERIALNUMBER IssuerAndSerialNumber;
189 /** The digest algorithm use to digest the signed content. */
190 RTCRX509ALGORITHMIDENTIFIER DigestAlgorithm;
191 /** Authenticated attributes, optional [0].
192 * @todo Check how other producers formats this. The microsoft one does not
193 * have explicit tags, but combines it with the SET OF. */
194 RTCRPKCS7ATTRIBUTES AuthenticatedAttributes;
195 /** The digest encryption algorithm use to encrypt the digest of the signed
196 * content. */
197 RTCRX509ALGORITHMIDENTIFIER DigestEncryptionAlgorithm;
198 /** The encrypted digest. */
199 RTASN1OCTETSTRING EncryptedDigest;
200 /** Unauthenticated attributes, optional [1].
201 * @todo Check how other producers formats this. The microsoft one does not
202 * have explicit tags, but combines it with the SET OF. */
203 RTCRPKCS7ATTRIBUTES UnauthenticatedAttributes;
204} RTCRPKCS7SIGNERINFO;
205RTASN1TYPE_STANDARD_PROTOTYPES(RTCRPKCS7SIGNERINFO, RTDECL, RTCrPkcs7SignerInfo, SeqCore.Asn1Core);
206
207RTDECL(int) RTCrPkcs7SignerInfo_SetAuthenticatedAttributes(PRTCRPKCS7SIGNERINFO pThis, PCRTCRPKCS7ATTRIBUTES pAttributes,
208 PCRTASN1ALLOCATORVTABLE pAllocator);
209RTDECL(int) RTCrPkcs7SignerInfo_SetUnauthenticatedAttributes(PRTCRPKCS7SIGNERINFO pThis, PCRTCRPKCS7ATTRIBUTES pAttributes,
210 PCRTASN1ALLOCATORVTABLE pAllocator);
211
212/** RTCRPKCS7SIGNERINFO::Version value. */
213#define RTCRPKCS7SIGNERINFO_V1 1
214
215/** @name PKCS \#9 Attribute IDs
216 * @{ */
217/** Content type (RFC-2630 11.1).
218 * Value: Object Identifier */
219#define RTCR_PKCS9_ID_CONTENT_TYPE_OID "1.2.840.113549.1.9.3"
220/** Message digest (RFC-2630 11.2).
221 * Value: Octet string. */
222#define RTCR_PKCS9_ID_MESSAGE_DIGEST_OID "1.2.840.113549.1.9.4"
223/** Signing time (RFC-2630 11.3).
224 * Value: Octet string. */
225#define RTCR_PKCS9_ID_SIGNING_TIME_OID "1.2.840.113549.1.9.5"
226/** Counter signature (RFC-2630 11.4).
227 * Value: SignerInfo. */
228#define RTCR_PKCS9_ID_COUNTER_SIGNATURE_OID "1.2.840.113549.1.9.6"
229/** Microsoft timestamp (RTF-3161) counter signature (SignedData).
230 * @remarks This isn't defined by PKCS \#9, but lumped in here for convenience. It's actually listed as SPC by MS. */
231#define RTCR_PKCS9_ID_MS_TIMESTAMP "1.3.6.1.4.1.311.3.3.1"
232/** Microsoft nested PKCS\#7 signature.
233 * @remarks This isn't defined by PKCS \#9, but lumped in here for convenience. */
234#define RTCR_PKCS9_ID_MS_NESTED_SIGNATURE "1.3.6.1.4.1.311.2.4.1"
235/** Microsoft statement type.
236 * @remarks This isn't defined by PKCS \#9, but lumped in here for convenience. It's actually listed as SPC by MS. */
237#define RTCR_PKCS9_ID_MS_STATEMENT_TYPE "1.3.6.1.4.1.311.2.1.11"
238/** Microsoft opus info.
239 * @remarks This isn't defined by PKCS \#9, but lumped in here for convenience. It's actually listed as SPC by MS. */
240#define RTCR_PKCS9_ID_MS_SP_OPUS_INFO "1.3.6.1.4.1.311.2.1.12"
241/** Apple code signing multi-code-directory plist.
242 * @remarks This isn't defined by PKCS \#9, but lumped in here for convenience. */
243#define RTCR_PKCS9_ID_APPLE_MULTI_CD_PLIST "1.2.840.113635.100.9.1"
244/** @} */
245
246
247/**
248 * Get the (next) signing time attribute from the specfied SignerInfo or one of
249 * the immediate counter signatures.
250 *
251 * @returns Pointer to the signing time if found, NULL if not.
252 * @param pThis The SignerInfo to search.
253 * @param ppSignerInfo Pointer to variable keeping track of the
254 * enumeration, optional.
255 *
256 * If specified the input value is taken to the be
257 * SignerInfo of the previously returned signing
258 * time. The value pointed to is NULL, the
259 * search/enum restarts.
260 *
261 * On successful return this is set to the
262 * SignerInfo which we found the signing time in.
263 */
264RTDECL(PCRTASN1TIME) RTCrPkcs7SignerInfo_GetSigningTime(PCRTCRPKCS7SIGNERINFO pThis, PCRTCRPKCS7SIGNERINFO *ppSignerInfo);
265
266
267/**
268 * Get the (first) timestamp from within a Microsoft timestamp server counter
269 * signature.
270 *
271 * @returns Pointer to the signing time if found, NULL if not.
272 * @param pThis The SignerInfo to search.
273 * @param ppContentInfoRet Where to return the pointer to the counter
274 * signature, optional.
275 */
276RTDECL(PCRTASN1TIME) RTCrPkcs7SignerInfo_GetMsTimestamp(PCRTCRPKCS7SIGNERINFO pThis,
277 struct RTCRPKCS7CONTENTINFO const **ppContentInfoRet);
278
279
280
281/**
282 * PKCS \#7 ContentInfo (IPRT representation).
283 */
284typedef struct RTCRPKCS7CONTENTINFO
285{
286 /** Sequence core. */
287 RTASN1SEQUENCECORE SeqCore;
288 /** Object ID identifying the content below. */
289 RTASN1OBJID ContentType;
290 /** Content, optional, explicit tag 0.
291 *
292 * Hack alert! This should've been an explict context tag 0 structure with a
293 * type selected according to ContentType. However, it's simpler to replace the
294 * explicit context with an OCTET STRING with implict tag 0. Then we can tag
295 * along on the encapsulation logic RTASN1OCTETSTRING provides for the dynamic
296 * inner type. The default decoder code will detect known structures as
297 * outlined in the union below, and decode the octet string content as an
298 * anonymous RTASN1CORE if not known.
299 *
300 * If the user want to decode the octet string content differently, it can do so
301 * by destroying and freeing the current encapsulated pointer, replacing it with
302 * it's own. (Of course following the RTASN1OCTETSTRING rules.) Just remember
303 * to also update the value in the union.
304 *
305 * @remarks What's signed and verified is Content.pEncapsulated->uData.pv.
306 */
307 RTASN1OCTETSTRING Content;
308 /** Pointer to the CMS octet string that's inside the Content, NULL if PKCS \#7.
309 *
310 * Hack alert! When transitioning from PKCS \#7 to CMS, the designers decided to
311 * change things and add another wrapper. This time we're talking about a real
312 * octet string, not like the one above which is really an explicit content tag.
313 * When constructing or decoding CMS content, this will be the same pointer as
314 * Content.pEncapsulated, while the union below will be holding the same pointer
315 * as pCmsContent->pEncapsulated.
316 */
317 PRTASN1OCTETSTRING pCmsContent;
318 /** Same as Content.pEncapsulated, except a choice of known types. */
319 union
320 {
321 /** ContentType is RTCRPKCS7SIGNEDDATA_OID. */
322 struct RTCRPKCS7SIGNEDDATA *pSignedData;
323 /** ContentType is RTCRSPCINDIRECTDATACONTENT_OID. */
324 struct RTCRSPCINDIRECTDATACONTENT *pIndirectDataContent;
325 /** ContentType is RTCRTSPTSTINFO_OID. */
326 struct RTCRTSPTSTINFO *pTstInfo;
327 /** Generic / Unknown / User. */
328 PRTASN1CORE pCore;
329 } u;
330} RTCRPKCS7CONTENTINFO;
331/** Pointer to the IPRT representation of a PKCS \#7 ContentInfo. */
332typedef RTCRPKCS7CONTENTINFO *PRTCRPKCS7CONTENTINFO;
333/** Pointer to the const IPRT representation of a PKCS \#7 ContentInfo. */
334typedef RTCRPKCS7CONTENTINFO const *PCRTCRPKCS7CONTENTINFO;
335RTASN1TYPE_STANDARD_PROTOTYPES(RTCRPKCS7CONTENTINFO, RTDECL, RTCrPkcs7ContentInfo, SeqCore.Asn1Core);
336RTASN1_IMPL_GEN_SET_OF_TYPEDEFS_AND_PROTOS(RTCRPKCS7SETOFCONTENTINFOS, RTCRPKCS7CONTENTINFO, RTDECL, RTCrPkcs7SetOfContentInfos);
337
338RTDECL(bool) RTCrPkcs7ContentInfo_IsSignedData(PCRTCRPKCS7CONTENTINFO pThis);
339
340
341/**
342 * PKCS \#7 Certificate choice.
343 */
344typedef enum RTCRPKCS7CERTCHOICE
345{
346 RTCRPKCS7CERTCHOICE_INVALID = 0,
347 RTCRPKCS7CERTCHOICE_X509,
348 RTCRPKCS7CERTCHOICE_EXTENDED_PKCS6,
349 RTCRPKCS7CERTCHOICE_AC_V1,
350 RTCRPKCS7CERTCHOICE_AC_V2,
351 RTCRPKCS7CERTCHOICE_OTHER,
352 RTCRPKCS7CERTCHOICE_END,
353 RTCRPKCS7CERTCHOICE_32BIT_HACK = 0x7fffffff
354} RTCRPKCS7CERTCHOICE;
355
356
357/**
358 * Common representation for PKCS \#7 ExtendedCertificateOrCertificate and the
359 * CMS CertificateChoices types.
360 */
361typedef struct RTCRPKCS7CERT
362{
363 /** Dummy ASN.1 record, not encoded. */
364 RTASN1DUMMY Dummy;
365 /** The value allocation. */
366 RTASN1ALLOCATION Allocation;
367 /** The choice of value. */
368 RTCRPKCS7CERTCHOICE enmChoice;
369 /** The value union. */
370 union
371 {
372 /** Standard X.509 certificate (RTCRCMSCERTIFICATECHOICE_X509). */
373 PRTCRX509CERTIFICATE pX509Cert;
374 /** Extended PKCS \#6 certificate (RTCRCMSCERTIFICATECHOICE_EXTENDED_PKCS6). */
375 PRTASN1CORE pExtendedCert;
376 /** Attribute certificate version 1 (RTCRCMSCERTIFICATECHOICE_AC_V1). */
377 PRTASN1CORE pAcV1;
378 /** Attribute certificate version 2 (RTCRCMSCERTIFICATECHOICE_AC_V2). */
379 PRTASN1CORE pAcV2;
380 /** Other certificate (RTCRCMSCERTIFICATECHOICE_OTHER). */
381 PRTASN1CORE pOtherCert;
382 } u;
383} RTCRPKCS7CERT;
384/** Pointer to the IPRT representation of PKCS \#7 or CMS certificate. */
385typedef RTCRPKCS7CERT *PRTCRPKCS7CERT;
386/** Pointer to the const IPRT representation of PKCS \#7 or CMS certificate. */
387typedef RTCRPKCS7CERT const *PCRTCRPKCS7CERT;
388RTASN1TYPE_STANDARD_PROTOTYPES(RTCRPKCS7CERT, RTDECL, RTCrPkcs7Cert, Dummy.Asn1Core);
389RTASN1_IMPL_GEN_SET_OF_TYPEDEFS_AND_PROTOS(RTCRPKCS7SETOFCERTS, RTCRPKCS7CERT, RTDECL, RTCrPkcs7SetOfCerts);
390
391RTDECL(int) RTCrPkcs7Cert_SetX509Cert(PRTCRPKCS7CERT pThis, PCRTCRX509CERTIFICATE pToClone, PCRTASN1ALLOCATORVTABLE pAllocator);
392RTDECL(int) RTCrPkcs7Cert_SetExtendedCert(PRTCRPKCS7CERT pThis, PCRTASN1CORE pToClone, PCRTASN1ALLOCATORVTABLE pAllocator);
393RTDECL(int) RTCrPkcs7Cert_SetAcV1(PRTCRPKCS7CERT pThis, PCRTASN1CORE pToClone, PCRTASN1ALLOCATORVTABLE pAllocator);
394RTDECL(int) RTCrPkcs7Cert_SetAcV2(PRTCRPKCS7CERT pThis, PCRTASN1CORE pToClone, PCRTASN1ALLOCATORVTABLE pAllocator);
395RTDECL(int) RTCrPkcs7Cert_SetOtherCert(PRTCRPKCS7CERT pThis, PCRTASN1CORE pToClone, PCRTASN1ALLOCATORVTABLE pAllocator);
396
397RTDECL(PCRTCRX509CERTIFICATE) RTCrPkcs7SetOfCerts_FindX509ByIssuerAndSerialNumber(PCRTCRPKCS7SETOFCERTS pCertificates,
398 PCRTCRX509NAME pIssuer,
399 PCRTASN1INTEGER pSerialNumber);
400
401
402/**
403 * PKCS \#7 SignedData (IPRT representation).
404 */
405typedef struct RTCRPKCS7SIGNEDDATA
406{
407 /** Sequence core. */
408 RTASN1SEQUENCECORE SeqCore;
409 /** The structure version value (1). */
410 RTASN1INTEGER Version;
411 /** The digest algorithms that are used to signed the content (ContentInfo). */
412 RTCRX509ALGORITHMIDENTIFIERS DigestAlgorithms;
413 /** The content that's being signed. */
414 RTCRPKCS7CONTENTINFO ContentInfo;
415 /** Certificates, optional, implicit tag 0. (Required by Authenticode.) */
416 RTCRPKCS7SETOFCERTS Certificates;
417 /** Certificate revocation lists, optional, implicit tag 1.
418 * Not used by Authenticode, so currently stubbed. */
419 RTASN1CORE Crls;
420 /** Signer infos. */
421 RTCRPKCS7SIGNERINFOS SignerInfos;
422} RTCRPKCS7SIGNEDDATA;
423/** Pointer to the IPRT representation of a PKCS \#7 SignedData. */
424typedef RTCRPKCS7SIGNEDDATA *PRTCRPKCS7SIGNEDDATA;
425/** Pointer to the const IPRT representation of a PKCS \#7 SignedData. */
426typedef RTCRPKCS7SIGNEDDATA const *PCRTCRPKCS7SIGNEDDATA;
427RTASN1TYPE_STANDARD_PROTOTYPES(RTCRPKCS7SIGNEDDATA, RTDECL, RTCrPkcs7SignedData, SeqCore.Asn1Core);
428RTASN1_IMPL_GEN_SET_OF_TYPEDEFS_AND_PROTOS(RTCRPKCS7SETOFSIGNEDDATA, RTCRPKCS7SIGNEDDATA, RTDECL, RTCrPkcs7SetOfSignedData);
429
430/** PKCS \#7 SignedData object ID. */
431#define RTCRPKCS7SIGNEDDATA_OID RTCR_PKCS7_SIGNED_DATA_OID
432
433/** PKCS \#7 SignedData version number 1. */
434#define RTCRPKCS7SIGNEDDATA_V1 1
435/* No version 2 seems to exist. */
436/** CMS SignedData version number 3.
437 * This should only be used if there are version 1 attribute certificates
438 * present, or if there are version 3 SignerInfo items present, or if
439 * enmcCountInfo is not id-data (RFC-5652, section 5.1). */
440#define RTCRPKCS7SIGNEDDATA_V3 3
441/** CMS SignedData version number 4.
442 * This should only be used if there are version 2 attribute certificates
443 * present (RFC-5652, section 5.1). */
444#define RTCRPKCS7SIGNEDDATA_V4 4
445/** CMS SignedData version number 5.
446 * This should only be used if there are certificates or/and CRLs of the
447 * OTHER type present (RFC-5652, section 5.1). */
448#define RTCRPKCS7SIGNEDDATA_V5 5
449
450RTDECL(int) RTCrPkcs7SignedData_SetCertificates(PRTCRPKCS7SIGNEDDATA pThis, PCRTCRPKCS7SETOFCERTS pCerts, PCRTASN1ALLOCATORVTABLE pAllocator);
451RTDECL(int) RTCrPkcs7SignedData_SetCrls(PRTCRPKCS7SIGNEDDATA pThis, PCRTASN1CORE pCerts, PCRTASN1ALLOCATORVTABLE pAllocator);
452
453/** @name RTCRPKCS7SIGNEDDATA_SANITY_F_XXX - Flags for RTPkcs7SignedDataCheckSantiy.
454 * @{ */
455/** Check for authenticode restrictions. */
456#define RTCRPKCS7SIGNEDDATA_SANITY_F_AUTHENTICODE RT_BIT_32(0)
457/** Check that all the hash algorithms are known to IPRT. */
458#define RTCRPKCS7SIGNEDDATA_SANITY_F_ONLY_KNOWN_HASH RT_BIT_32(1)
459/** Require signing certificate to be present. */
460#define RTCRPKCS7SIGNEDDATA_SANITY_F_SIGNING_CERT_PRESENT RT_BIT_32(2)
461/** @} */
462
463/** PKCS\#7/CMS (content info) markers. */
464extern RTDATADECL(RTCRPEMMARKER const) g_aRTCrPkcs7Markers[];
465/** Number of entries in g_aRTCrPkcs7Markers. */
466extern RTDATADECL(uint32_t const) g_cRTCrPkcs7Markers;
467
468/** @name Flags for RTCrPkcs7ContentInfo_ReadFromBuffer
469 * @{ */
470/** Only allow PEM certificates, not binary ones.
471 * @sa RTCRPEMREADFILE_F_ONLY_PEM */
472#define RTCRPKCS7_READ_F_PEM_ONLY RT_BIT(1)
473/** @} */
474
475RTDECL(int) RTCrPkcs7_ReadFromBuffer(PRTCRPKCS7CONTENTINFO pContentInfo, const void *pvBuf, size_t cbBuf,
476 uint32_t fFlags, PCRTASN1ALLOCATORVTABLE pAllocator,
477 bool *pfCmsLabeled, PRTERRINFO pErrInfo, const char *pszErrorTag);
478
479
480/**
481 * PKCS \#7 DigestInfo (IPRT representation).
482 */
483typedef struct RTCRPKCS7DIGESTINFO
484{
485 /** Sequence core. */
486 RTASN1SEQUENCECORE SeqCore;
487 /** The digest algorithm use to digest the signed content. */
488 RTCRX509ALGORITHMIDENTIFIER DigestAlgorithm;
489 /** The digest. */
490 RTASN1OCTETSTRING Digest;
491} RTCRPKCS7DIGESTINFO;
492/** Pointer to the IPRT representation of a PKCS \#7 DigestInfo object. */
493typedef RTCRPKCS7DIGESTINFO *PRTCRPKCS7DIGESTINFO;
494/** Pointer to the const IPRT representation of a PKCS \#7 DigestInfo object. */
495typedef RTCRPKCS7DIGESTINFO const *PCRTCRPKCS7DIGESTINFO;
496RTASN1TYPE_STANDARD_PROTOTYPES(RTCRPKCS7DIGESTINFO, RTDECL, RTCrPkcs7DigestInfo, SeqCore.Asn1Core);
497
498
499/**
500 * Callback function for use with RTCrPkcs7VerifySignedData.
501 *
502 * @returns IPRT status code.
503 * @param pCert The certificate to verify.
504 * @param hCertPaths Unless the certificate is trusted directly, this
505 * is a reference to the certificate path builder
506 * and verifier instance that we used to establish
507 * at least valid trusted path to @a pCert. The
508 * callback can use this to enforce additional
509 * certificate lineage requirements, effective
510 * policy checks and whatnot.
511 * This is NIL_RTCRX509CERTPATHS if the certificate
512 * is directly trusted.
513 * @param fFlags Mix of the RTCRPKCS7VCC_F_XXX flags.
514 * @param pvUser The user argument.
515 * @param pErrInfo Optional error info buffer.
516 */
517typedef DECLCALLBACKTYPE(int, FNRTCRPKCS7VERIFYCERTCALLBACK,(PCRTCRX509CERTIFICATE pCert, RTCRX509CERTPATHS hCertPaths,
518 uint32_t fFlags, void *pvUser, PRTERRINFO pErrInfo));
519/** Pointer to a FNRTCRPKCS7VERIFYCERTCALLBACK callback. */
520typedef FNRTCRPKCS7VERIFYCERTCALLBACK *PFNRTCRPKCS7VERIFYCERTCALLBACK;
521
522/** @name RTCRPKCS7VCC_F_XXX - Flags for FNRTCRPKCS7VERIFYCERTCALLBACK.
523 * @{ */
524/** Normal callback for a direct signatory of the signed data. */
525#define RTCRPKCS7VCC_F_SIGNED_DATA RT_BIT_32(0)
526/** Check that the signatory can be trusted for timestamps. */
527#define RTCRPKCS7VCC_F_TIMESTAMP RT_BIT_32(1)
528/** @} */
529
530/**
531 * @callback_method_impl{FNRTCRPKCS7VERIFYCERTCALLBACK,
532 * Default implementation that checks for the DigitalSignature KeyUsage bit.}
533 */
534RTDECL(int) RTCrPkcs7VerifyCertCallbackDefault(PCRTCRX509CERTIFICATE pCert, RTCRX509CERTPATHS hCertPaths, uint32_t fFlags,
535 void *pvUser, PRTERRINFO pErrInfo);
536
537/**
538 * @callback_method_impl{FNRTCRPKCS7VERIFYCERTCALLBACK,
539 * Standard code signing. Use this for Microsoft SPC.}
540 */
541RTDECL(int) RTCrPkcs7VerifyCertCallbackCodeSigning(PCRTCRX509CERTIFICATE pCert, RTCRX509CERTPATHS hCertPaths, uint32_t fFlags,
542 void *pvUser, PRTERRINFO pErrInfo);
543
544/**
545 * Verifies PKCS \#7 SignedData.
546 *
547 * For compatability with alternative crypto providers, the user must work on
548 * the top level PKCS \#7 structure instead directly on the SignedData.
549 *
550 * @returns IPRT status code.
551 * @param pContentInfo PKCS \#7 content info structure.
552 * @param fFlags RTCRPKCS7VERIFY_SD_F_XXX.
553 * @param hAdditionalCerts Store containing additional certificates to
554 * supplement those mentioned in the signed data.
555 * @param hTrustedCerts Store containing trusted certificates.
556 * @param pValidationTime The time we're supposed to validate the
557 * certificates chains at. Ignored for signatures
558 * with valid signing time attributes.
559 * When RTCRPKCS7VERIFY_SD_F_UPDATE_VALIDATION_TIME
560 * is set, this is updated to the actual validation
561 * time used.
562 * @param pfnVerifyCert Callback for checking that a certificate used
563 * for signing the data is suitable.
564 * @param pvUser User argument for the callback.
565 * @param pErrInfo Optional error info buffer.
566 * @sa RTCrPkcs7VerifySignedDataWithExternalData
567 */
568RTDECL(int) RTCrPkcs7VerifySignedData(PCRTCRPKCS7CONTENTINFO pContentInfo, uint32_t fFlags,
569 RTCRSTORE hAdditionalCerts, RTCRSTORE hTrustedCerts,
570 PCRTTIMESPEC pValidationTime, PFNRTCRPKCS7VERIFYCERTCALLBACK pfnVerifyCert, void *pvUser,
571 PRTERRINFO pErrInfo);
572
573
574/**
575 * Verifies PKCS \#7 SignedData with external data.
576 *
577 * For compatability with alternative crypto providers, the user must work on
578 * the top level PKCS \#7 structure instead directly on the SignedData.
579 *
580 * @returns IPRT status code.
581 * @param pContentInfo PKCS \#7 content info structure.
582 * @param fFlags RTCRPKCS7VERIFY_SD_F_XXX.
583 * @param hAdditionalCerts Store containing additional certificates to
584 * supplement those mentioned in the signed data.
585 * @param hTrustedCerts Store containing trusted certificates.
586 * @param pValidationTime The time we're supposed to validate the
587 * certificates chains at. Ignored for signatures
588 * with valid signing time attributes.
589 * When RTCRPKCS7VERIFY_SD_F_UPDATE_VALIDATION_TIME
590 * is set, this is updated to the actual validation
591 * time used.
592 * @param pfnVerifyCert Callback for checking that a certificate used
593 * for signing the data is suitable.
594 * @param pvUser User argument for the callback.
595 * @param pvData The signed external data.
596 * @param cbData The size of the signed external data.
597 * @param pErrInfo Optional error info buffer.
598 * @sa RTCrPkcs7VerifySignedData
599 */
600RTDECL(int) RTCrPkcs7VerifySignedDataWithExternalData(PCRTCRPKCS7CONTENTINFO pContentInfo, uint32_t fFlags,
601 RTCRSTORE hAdditionalCerts, RTCRSTORE hTrustedCerts,
602 PCRTTIMESPEC pValidationTime,
603 PFNRTCRPKCS7VERIFYCERTCALLBACK pfnVerifyCert, void *pvUser,
604 void const *pvData, size_t cbData, PRTERRINFO pErrInfo);
605
606/** @name RTCRPKCS7VERIFY_SD_F_XXX - Flags for RTCrPkcs7VerifySignedData and
607 * RTCrPkcs7VerifySignedDataWithExternalData
608 * @{ */
609/** Always use the signing time attribute if present, requiring it to be
610 * verified as valid. The default behavior is to ignore unverifiable
611 * signing time attributes and use the @a pValidationTime instead. */
612#define RTCRPKCS7VERIFY_SD_F_ALWAYS_USE_SIGNING_TIME_IF_PRESENT RT_BIT_32(0)
613/** Same as RTCRPKCS7VERIFY_SD_F_ALWAYS_USE_SIGNING_TIME_IF_PRESENT for the MS
614 * timestamp counter signatures. */
615#define RTCRPKCS7VERIFY_SD_F_ALWAYS_USE_MS_TIMESTAMP_IF_PRESENT RT_BIT_32(1)
616/** Only use signing time attributes from counter signatures. */
617#define RTCRPKCS7VERIFY_SD_F_COUNTER_SIGNATURE_SIGNING_TIME_ONLY RT_BIT_32(2)
618/** Don't validate the counter signature containing the signing time, just use
619 * it unverified. This is useful if we don't necessarily have the root
620 * certificates for the timestamp server handy, but use with great care.
621 * @sa RTCRPKCS7VERIFY_SD_F_USE_MS_TIMESTAMP_UNVERIFIED */
622#define RTCRPKCS7VERIFY_SD_F_USE_SIGNING_TIME_UNVERIFIED RT_BIT_32(3)
623/** Don't validate the MS counter signature containing the signing timestamp.
624 * @sa RTCRPKCS7VERIFY_SD_F_USE_SIGNING_TIME_UNVERIFIED */
625#define RTCRPKCS7VERIFY_SD_F_USE_MS_TIMESTAMP_UNVERIFIED RT_BIT_32(4)
626/** Do not consider timestamps in microsoft counter signatures. */
627#define RTCRPKCS7VERIFY_SD_F_IGNORE_MS_TIMESTAMP RT_BIT_32(5)
628/** The signed data requires certificates to have the timestamp extended
629 * usage bit present. This is used for recursivly verifying MS timestamp
630 * signatures. */
631#define RTCRPKCS7VERIFY_SD_F_USAGE_TIMESTAMPING RT_BIT_32(6)
632/** Skip the verification of the certificate trust paths, taking all
633 * certificates to be trustworthy. */
634#define RTCRPKCS7VERIFY_SD_F_TRUST_ALL_CERTS RT_BIT_32(7)
635/** Update @a pValidationTime with the actual validation time used.
636 * This requires RTCRPKCS7VERIFY_SD_F_HAS_SIGNER_INDEX to get a consistent
637 * result. And yeah, it unconst the parameter, which is patently ugly. */
638#define RTCRPKCS7VERIFY_SD_F_UPDATE_VALIDATION_TIME RT_BIT_32(8)
639/** Check trust anchors (@sa RTCrX509CertPathsSetTrustAnchorChecks). */
640#define RTCRPKCS7VERIFY_SD_F_CHECK_TRUST_ANCHORS RT_BIT_32(9)
641
642/** This can be used to only verify one given signer info.
643 * Max index value is 15. */
644#define RTCRPKCS7VERIFY_SD_F_SIGNER_INDEX(a_idxSignerInfo) \
645 ( RTCRPKCS7VERIFY_SD_F_HAS_SIGNER_INDEX \
646 | (((a_idxSignerInfo) & RTCRPKCS7VERIFY_SD_F_SIGNER_INDEX_MAX) << RTCRPKCS7VERIFY_SD_F_SIGNER_INDEX_SHIFT) )
647/** Has a valid value in RTCRPKCS7VERIFY_SD_F_SIGNER_INDEX_MASK. */
648#define RTCRPKCS7VERIFY_SD_F_HAS_SIGNER_INDEX RT_BIT_32(23)
649/** Signer index shift value. */
650#define RTCRPKCS7VERIFY_SD_F_SIGNER_INDEX_SHIFT 24
651/** Signer index mask. */
652#define RTCRPKCS7VERIFY_SD_F_SIGNER_INDEX_MASK UINT32_C(0x0f000000)
653/** Max signer index value (inclusive). */
654#define RTCRPKCS7VERIFY_SD_F_SIGNER_INDEX_MAX \
655 (RTCRPKCS7VERIFY_SD_F_SIGNER_INDEX_MASK >> RTCRPKCS7VERIFY_SD_F_SIGNER_INDEX_SHIFT)
656
657/** Indicates internally that we're validating a counter signature and should
658 * use different rules when checking out the authenticated attributes.
659 * @internal */
660#define RTCRPKCS7VERIFY_SD_F_COUNTER_SIGNATURE RT_BIT_32(31)
661/** @} */
662
663
664RTDECL(int) RTCrPkcs7SimpleSignSignedData(uint32_t fFlags, PCRTCRX509CERTIFICATE pSigner, RTCRKEY hPrivateKey,
665 void const *pvData, size_t cbData, RTDIGESTTYPE enmDigestType,
666 RTCRSTORE hAdditionalCerts, PCRTCRPKCS7ATTRIBUTES pAdditionalAuthenticatedAttribs,
667 void *pvResult, size_t *pcbResult, PRTERRINFO pErrInfo);
668
669/** @name RTCRPKCS7SIGN_SD_F_XXX - Flags for RTCrPkcs7SimpleSign.
670 * @{ */
671/** Detached data. */
672#define RTCRPKCS7SIGN_SD_F_DEATCHED RT_BIT_32(0)
673/** No SMIME capabilities attribute. */
674#define RTCRPKCS7SIGN_SD_F_NO_SMIME_CAP RT_BIT_32(1)
675/** Valid flag mask. */
676#define RTCRPKCS7SIGN_SD_F_VALID_MASK UINT32_C(0x00000003)
677/** @} */
678
679/** @} */
680
681RT_C_DECLS_END
682
683#endif /* !IPRT_INCLUDED_crypto_pkcs7_h */
684
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