VirtualBox

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

最後變更 在這個檔案從104745是 104745,由 vboxsync 提交於 10 月 前

IPRT,Main: Reworked the newly introduced RTCrX509Certificate_Generate function. It's now called RTCrX509Certificate_GenerateSelfSignedRsa and takes a few more parameters. We still can't read the output it creates. Added a create-self-signed-rsa-cert command to RTSignTool for easy testing. bugref:10310

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 8.6 KB
 
1/* $Id: x509-create-sign.cpp 104745 2024-05-21 12:52:09Z 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.alldomusa.eu.org.
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 >= 0x30000000
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 BIO * const pCertBio = BIO_new(BIO_s_mem());
173 if (pCertBio)
174 {
175 rcOssl = PEM_write_bio_X509(pCertBio, pNewCert);
176 if (rcOssl > 0)
177 rc = rtCrOpenSslWriteMemBioToNewFile(pCertBio, pszCertFile, pErrInfo);
178 else
179 rc = RTErrInfoSet(pErrInfo, VERR_CR_KEY_GEN_FAILED_RSA, "PEM_write_bio_X509 failed");
180 BIO_free(pCertBio);
181 }
182 else
183 rc = VERR_NO_MEMORY;
184
185 if (RT_SUCCESS(rc))
186 {
187 /* The private key as plain text (security sensitive, thus last). */
188 BIO * const pPkBio = BIO_new(BIO_s_secmem());
189 if (pPkBio)
190 {
191 rcOssl = PEM_write_bio_PrivateKey(pPkBio, pPrivateKey,
192 NULL /*enc*/, NULL /*kstr*/, 0 /*klen*/, NULL /*cb*/, NULL /*u*/);
193 if (rcOssl > 0)
194 rc = rtCrOpenSslWriteMemBioToNewFile(pPkBio, pszPrivateKeyFile, pErrInfo);
195 else
196 rc = RTErrInfoSet(pErrInfo, VERR_CR_KEY_GEN_FAILED_RSA, "PEM_write_bio_PrivateKey failed");
197 BIO_free(pPkBio);
198 }
199 else
200 rc = VERR_NO_MEMORY;
201 if (RT_FAILURE(rc))
202 RTFileDelete(pszCertFile);
203 }
204 }
205 else
206 rc = RTErrInfoSet(pErrInfo, VERR_CR_KEY_GEN_FAILED_RSA, "X509_sign failed");
207 }
208
209 X509_free(pNewCert);
210 }
211 else
212 rc = RTErrInfoSet(pErrInfo, VERR_NO_MEMORY, "X509_new failed");
213
214 EVP_PKEY_free(pPrivateKey);
215 return rc;
216}
217
218#endif /* IPRT_WITH_OPENSSL */
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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