VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/crypto/x509-create-sign.cpp@ 104755

Last change on this file since 104755 was 104755, checked in by vboxsync, 12 months ago

IPRT: RTCrX509Certificate_GenerateSelfSignedRsa build fix for OpenSSL 1.0.x. bugref:10310

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.7 KB
Line 
1/* $Id: x509-create-sign.cpp 104755 2024-05-22 11:32:11Z vboxsync $ */
2/** @file
3 * IPRT - Crypto - X.509, Certificate Creation.
4 */
5
6/*
7 * Copyright (C) 2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.215389.xyz.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include "internal/iprt.h"
42#include <iprt/crypto/x509.h>
43
44#ifdef IPRT_WITH_OPENSSL
45# include <iprt/err.h>
46# include <iprt/file.h>
47# include <iprt/rand.h>
48
49# include "internal/iprt-openssl.h"
50# include "internal/openssl-pre.h"
51# include <openssl/evp.h>
52# include <openssl/pem.h>
53# include <openssl/x509.h>
54# include <openssl/bio.h>
55# include "internal/openssl-post.h"
56
57
58
59RTDECL(int) RTCrX509Certificate_GenerateSelfSignedRsa(RTDIGESTTYPE enmDigestType, uint32_t cBits, uint32_t cSecsValidFor,
60 uint32_t fKeyUsage, uint64_t fExtKeyUsage, void *pvSubjectTodo,
61 const char *pszCertFile, const char *pszPrivateKeyFile, PRTERRINFO pErrInfo)
62{
63 AssertReturn(cSecsValidFor <= (uint32_t)INT32_MAX, VERR_OUT_OF_RANGE); /* larger values are not portable (win) */
64 AssertReturn(!fKeyUsage, VERR_NOT_IMPLEMENTED);
65 AssertReturn(!fExtKeyUsage, VERR_NOT_IMPLEMENTED);
66 AssertReturn(pvSubjectTodo == NULL, VERR_NOT_IMPLEMENTED);
67
68 /*
69 * Translate enmDigestType.
70 */
71 const EVP_MD * const pEvpDigest = (const EVP_MD *)rtCrOpenSslConvertDigestType(enmDigestType, pErrInfo);
72 AssertReturn(pEvpDigest, pErrInfo ? pErrInfo->rc : VERR_CR_DIGEST_NOT_SUPPORTED);
73
74 /*
75 * Create a new RSA private key.
76 */
77# if OPENSSL_VERSION_NUMBER >= 0x30000000 /* RSA_generate_key is depreated in v3 */
78 EVP_PKEY * const pPrivateKey = EVP_RSA_gen(cBits);
79 if (!pPrivateKey)
80 return RTErrInfoSetF(pErrInfo, VERR_CR_KEY_GEN_FAILED_RSA, "EVP_RSA_gen(%u) failed", cBits);
81# else
82 RSA * const pRsaKey = RSA_generate_key(
83 cBits, /* Number of bits for the key */
84 RSA_F4, /* Exponent - RSA_F4 is defined as 0x10001L */
85 NULL, /* Callback */
86 NULL /* Callback argument */
87 );
88 if (!pRsaKey)
89 return RTErrInfoSetF(pErrInfo, VERR_CR_KEY_GEN_FAILED_RSA, "RSA_generate_key(%u,RSA_F4,,) failed", cBits);
90
91 EVP_PKEY * const pPrivateKey = EVP_PKEY_new();
92 if (pPrivateKey)
93 EVP_PKEY_assign_RSA(pPrivateKey, pRsaKey); /* Takes ownership of pRsaKey. */
94 else
95 {
96 RSA_free(pRsaKey);
97 return RTErrInfoSet(pErrInfo, VERR_NO_MEMORY, "EVP_PKEY_new failed");
98 }
99# endif
100
101 /*
102 * Construct the certificate.
103 */
104 int rc = VINF_SUCCESS;
105 X509 *pNewCert = X509_new();
106 if (pNewCert)
107 {
108 int rcOssl;
109
110 /* Set to X509 version 1: */
111# if 0
112 if (fKeyUsage || fExtKeyUsage)
113 rcOssl = X509_set_version(pNewCert, RTCRX509TBSCERTIFICATE_V3);
114 else
115# endif
116 rcOssl = X509_set_version(pNewCert, RTCRX509TBSCERTIFICATE_V1);
117 AssertStmt(rcOssl > 0, rc = RTErrInfoSet(pErrInfo, VERR_GENERAL_FAILURE, "X509_set_version failed"));
118
119 /* Set the serial number to a random number in the 1 - 1G range: */
120 rcOssl = ASN1_INTEGER_set(X509_get_serialNumber(pNewCert), RTRandU32Ex(1, UINT32_MAX / 4));
121 AssertStmt(rcOssl > 0, rc = RTErrInfoSet(pErrInfo, VERR_GENERAL_FAILURE, "X509_set_version failed"));
122
123 /* The certificate is valid from now and the specifice number of seconds forwards: */
124# if OPENSSL_VERSION_NUMBER >= 0x1010000f
125 AssertStmt(X509_gmtime_adj(X509_getm_notBefore(pNewCert), 0),
126 rc = RTErrInfoSet(pErrInfo, VERR_GENERAL_FAILURE, "X509_gmtime_adj/before failed"));
127 AssertStmt(X509_gmtime_adj(X509_getm_notAfter(pNewCert), cSecsValidFor),
128 rc = RTErrInfoSet(pErrInfo, VERR_GENERAL_FAILURE, "X509_gmtime_adj/after failed"));
129# else
130 AssertStmt(X509_gmtime_adj(X509_get_notBefore(pNewCert), 0),
131 rc = RTErrInfoSet(pErrInfo, VERR_GENERAL_FAILURE, "X509_gmtime_adj/before failed"));
132 AssertStmt(X509_gmtime_adj(X509_get_notAfter(pNewCert), cSecsValidFor),
133 rc = RTErrInfoSet(pErrInfo, VERR_GENERAL_FAILURE, "X509_gmtime_adj/after failed"));
134# endif
135
136 /* Set the public key (part of the private): */
137 rcOssl = X509_set_pubkey(pNewCert, pPrivateKey);
138 AssertStmt(rcOssl > 0, rc = RTErrInfoSet(pErrInfo, VERR_GENERAL_FAILURE, "X509_set_pubkey failed"));
139
140# if 0
141 /* Set key usage. */
142 if (fKeyUsage)
143 {
144 }
145 /* Set extended key usage. */
146 if (fExtKeyUsage)
147 {
148 }
149# endif
150 /** @todo set other certificate attributes? */
151
152
153 /** @todo check what the subject name is... Offer way to specify it? */
154
155 /* Make it self signed: */
156 X509_NAME *pX509Name = X509_get_subject_name(pNewCert);
157 rcOssl = X509_set_issuer_name(pNewCert, pX509Name);
158 AssertStmt(rcOssl > 0, rc = RTErrInfoSet(pErrInfo, VERR_GENERAL_FAILURE, "X509_set_issuer_name failed"));
159
160 if (RT_SUCCESS(rc))
161 {
162 /*
163 * Sign the certificate.
164 */
165 rcOssl = X509_sign(pNewCert, pPrivateKey, pEvpDigest);
166 if (rcOssl > 0)
167 {
168 /*
169 * Write out the result to the two files.
170 */
171 /* The certificate (not security sensitive). */
172# if OPENSSL_VERSION_NUMBER >= 0x1010000f
173 BIO * const pCertBio = BIO_new(BIO_s_mem());
174# else
175 BIO * const pCertBio = BIO_new(BIO_s_mem());
176# endif
177 if (pCertBio)
178 {
179 rcOssl = PEM_write_bio_X509(pCertBio, pNewCert);
180 if (rcOssl > 0)
181 rc = rtCrOpenSslWriteMemBioToNewFile(pCertBio, pszCertFile, pErrInfo);
182 else
183 rc = RTErrInfoSet(pErrInfo, VERR_CR_KEY_GEN_FAILED_RSA, "PEM_write_bio_X509 failed");
184 BIO_free(pCertBio);
185 }
186 else
187 rc = VERR_NO_MEMORY;
188
189 if (RT_SUCCESS(rc))
190 {
191 /* The private key as plain text (security sensitive, thus last). */
192 BIO * const pPkBio = BIO_new(BIO_s_secmem());
193 if (pPkBio)
194 {
195 rcOssl = PEM_write_bio_PrivateKey(pPkBio, pPrivateKey,
196 NULL /*enc*/, NULL /*kstr*/, 0 /*klen*/, NULL /*cb*/, NULL /*u*/);
197 if (rcOssl > 0)
198 rc = rtCrOpenSslWriteMemBioToNewFile(pPkBio, pszPrivateKeyFile, pErrInfo);
199 else
200 rc = RTErrInfoSet(pErrInfo, VERR_CR_KEY_GEN_FAILED_RSA, "PEM_write_bio_PrivateKey failed");
201 BIO_free(pPkBio);
202 }
203 else
204 rc = VERR_NO_MEMORY;
205 if (RT_FAILURE(rc))
206 RTFileDelete(pszCertFile);
207 }
208 }
209 else
210 rc = RTErrInfoSet(pErrInfo, VERR_CR_KEY_GEN_FAILED_RSA, "X509_sign failed");
211 }
212
213 X509_free(pNewCert);
214 }
215 else
216 rc = RTErrInfoSet(pErrInfo, VERR_NO_MEMORY, "X509_new failed");
217
218 EVP_PKEY_free(pPrivateKey);
219 return rc;
220}
221
222#endif /* IPRT_WITH_OPENSSL */
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette