VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/crypto/pkcs7-sign.cpp@ 84256

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

IPRT/pkcs7-sign.cpp: scm fix. bugref:9699

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.8 KB
Line 
1/* $Id: pkcs7-sign.cpp 84256 2020-05-11 15:15:35Z vboxsync $ */
2/** @file
3 * IPRT - Crypto - PKCS \#7, Signing
4 */
5
6/*
7 * Copyright (C) 2006-2020 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 "internal/iprt.h"
32#include <iprt/crypto/pkcs7.h>
33
34#include <iprt/errcore.h>
35#include <iprt/string.h>
36#include <iprt/crypto/digest.h>
37#include <iprt/crypto/key.h>
38#include <iprt/crypto/pkix.h>
39#include <iprt/crypto/store.h>
40#include <iprt/crypto/x509.h>
41
42#ifdef IPRT_WITH_OPENSSL
43# include "internal/iprt-openssl.h"
44# include <openssl/pkcs7.h>
45# include <openssl/cms.h>
46# include <openssl/x509.h>
47# include <openssl/err.h>
48#endif
49
50
51/*********************************************************************************************************************************
52* Structures and Typedefs *
53*********************************************************************************************************************************/
54/**
55 * PKCS\#7 / CMS signing operation instance.
56 */
57typedef struct RTCRPKCS7SIGNINGJOBINT
58{
59 /** Magic value (RTCRPKCS7SIGNINGJOBINT). */
60 uint32_t u32Magic;
61 /** Reference counter. */
62 uint32_t volatile cRefs;
63 /** RTCRPKCS7SIGN_F_XXX. */
64 uint64_t fFlags;
65 /** Set if finalized. */
66 bool fFinallized;
67
68 //....
69} RTCRPKCS7SIGNINGJOBINT;
70
71/** Magic value for RTCRPKCS7SIGNINGJOBINT (Jonathan Lethem). */
72#define RTCRPKCS7SIGNINGJOBINT_MAGIC UINT32_C(0x19640219)
73
74/** Handle to PKCS\#7/CMS signing operation. */
75typedef struct RTCRPKCS7SIGNINGJOBINT *RTCRPKCS7SIGNINGJOB;
76/** Pointer to a PKCS\#7/CMS signing operation handle. */
77typedef RTCRPKCS7SIGNINGJOB *PRTCRPKCS7SIGNINGJOB;
78
79//// CMS_sign
80//RTDECL(int) RTCrPkcs7Sign(PRTCRPKCS7SIGNINGJOB *phJob, uint64_t fFlags, PCRTCRX509CERTIFICATE pSigner, RTCRKEY hPrivateKey,
81// RTCRSTORE hAdditionalCerts,
82//
83
84
85
86RTDECL(int) RTCrPkcs7SimpleSignSignedData(uint32_t fFlags, PCRTCRX509CERTIFICATE pSigner, RTCRKEY hPrivateKey,
87 void const *pvData, size_t cbData, RTDIGESTTYPE enmDigestType,
88 RTCRSTORE hAdditionalCerts, void *pvResult, size_t *pcbResult, PRTERRINFO pErrInfo)
89{
90 size_t const cbResultBuf = *pcbResult;
91 *pcbResult = 0;
92 AssertReturn(!(fFlags & ~RTCRPKCS7SIGN_SD_F_VALID_MASK), VERR_INVALID_FLAGS);
93#ifdef IPRT_WITH_OPENSSL
94 AssertReturn((int)cbData >= 0 && (unsigned)cbData == cbData, VERR_TOO_MUCH_DATA);
95
96 /*
97 * Resolve the digest type.
98 */
99 const EVP_MD *pEvpMd = NULL;
100 if (enmDigestType != RTDIGESTTYPE_UNKNOWN)
101 {
102 pEvpMd = (const EVP_MD *)rtCrOpenSslConvertDigestType(enmDigestType, pErrInfo);
103 AssertReturn(pEvpMd, pErrInfo ? pErrInfo->rc : VERR_INVALID_PARAMETER);
104 }
105
106 /*
107 * Convert the private key.
108 */
109 EVP_PKEY *pEvpPrivateKey = NULL;
110 int rc = rtCrKeyToOpenSslKey(hPrivateKey, false /*fNeedPublic*/, (void **)&pEvpPrivateKey, pErrInfo);
111 if (RT_SUCCESS(rc))
112 {
113 /*
114 * Convert the signing certificate.
115 */
116 X509 *pOsslSigner = NULL;
117 rc = rtCrOpenSslConvertX509Cert((void **)&pOsslSigner, pSigner, pErrInfo);
118 if (RT_SUCCESS(rc))
119 {
120 /*
121 * Convert any additional certificates.
122 */
123 STACK_OF(X509) *pOsslAdditionalCerts = NULL;
124 if (hAdditionalCerts != NIL_RTCRSTORE)
125 rc = RTCrStoreConvertToOpenSslCertStack(hAdditionalCerts, 0 /*fFlags*/, (void **)&pOsslAdditionalCerts, pErrInfo);
126 if (RT_SUCCESS(rc))
127 {
128 /*
129 * Create a BIO for the data buffer.
130 */
131 BIO *pOsslData = BIO_new_mem_buf((void *)pvData, (int)cbData);
132 if (pOsslData)
133 {
134 /*
135 * Do the signing.
136 */
137 unsigned int fOsslSign = CMS_BINARY | CMS_PARTIAL;
138 if (fFlags & RTCRPKCS7SIGN_SD_F_DEATCHED)
139 fOsslSign |= CMS_DETACHED;
140 if (fFlags & RTCRPKCS7SIGN_SD_F_NO_SMIME_CAP)
141 fOsslSign |= CMS_NOSMIMECAP;
142 CMS_ContentInfo *pCms = CMS_sign(NULL, NULL, pOsslAdditionalCerts, NULL, fOsslSign);
143 if (pCms != NULL)
144 {
145 if (CMS_add1_signer(pCms, pOsslSigner, pEvpPrivateKey, pEvpMd, fOsslSign) != NULL)
146 {
147 rc = CMS_final(pCms, pOsslData, NULL /*dcont*/, fOsslSign);
148 if (rc > 0)
149 {
150 /*
151 * Get the output and copy it into the result buffer.
152 */
153 BIO *pOsslResult = BIO_new(BIO_s_mem());
154 if (pOsslResult)
155 {
156 rc = i2d_CMS_bio(pOsslResult, pCms);
157 if (rc > 0)
158 {
159 BUF_MEM *pBuf = NULL;
160 rc = (int)BIO_get_mem_ptr(pOsslResult, &pBuf);
161 if (rc > 0)
162 {
163 AssertPtr(pBuf);
164 size_t const cbResult = pBuf->length;
165 if ( cbResultBuf >= cbResult
166 && pvResult != NULL)
167 {
168 memcpy(pvResult, pBuf->data, cbResult);
169 rc = VINF_SUCCESS;
170 }
171 else
172 rc = VERR_BUFFER_OVERFLOW;
173 *pcbResult = cbResult;
174 }
175 else
176 rc = RTErrInfoSet(pErrInfo, VERR_GENERAL_FAILURE, "BIO_get_mem_ptr");
177 }
178 else
179 rc = RTErrInfoSet(pErrInfo, VERR_GENERAL_FAILURE, "i2d_CMS_bio");
180 BIO_free(pOsslResult);
181 }
182 else
183 rc = RTErrInfoSet(pErrInfo, VERR_NO_MEMORY, "BIO_new/BIO_s_mem");
184 }
185 else
186 rc = RTErrInfoSet(pErrInfo, VERR_GENERAL_FAILURE, "CMS_final");
187 }
188 else
189 rc = RTErrInfoSet(pErrInfo, VERR_GENERAL_FAILURE, "CMS_add1_signer");
190 CMS_ContentInfo_free(pCms);
191 }
192 else
193 rc = RTErrInfoSet(pErrInfo, VERR_GENERAL_FAILURE, "CMS_sign");
194 BIO_free(pOsslData);
195 }
196 }
197 rtCrOpenSslFreeConvertedX509Cert(pOsslSigner);
198 }
199 EVP_PKEY_free(pEvpPrivateKey);
200 }
201 return rc;
202#else
203 RT_NOREF(fFlags, pSigner, hPrivateKey, pvData, cbData, enmDigestType, hAdditionalCerts, pvResult, pErrInfo, cbResultBuf);
204 *pcbResult = 0;
205 return VERR_NOT_IMPLEMENTED;
206#endif
207}
208
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