1 | /* $Id: key-create-rsa-openssl.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Crypto - RSA Key Creation using OpenSSL.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2018-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.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/key.h>
|
---|
43 |
|
---|
44 | #ifdef IPRT_WITH_OPENSSL /* Whole file. */
|
---|
45 | # include <iprt/err.h>
|
---|
46 | # include <iprt/string.h>
|
---|
47 |
|
---|
48 | # include "internal/iprt-openssl.h"
|
---|
49 | # include "internal/openssl-pre.h"
|
---|
50 | # include <openssl/rsa.h>
|
---|
51 | # include <openssl/err.h>
|
---|
52 | # include "internal/openssl-post.h"
|
---|
53 |
|
---|
54 | # include "key-internal.h"
|
---|
55 |
|
---|
56 |
|
---|
57 |
|
---|
58 | RTDECL(int) RTCrKeyCreateNewRsa(PRTCRKEY phKey, uint32_t cBits, uint32_t uPubExp, uint32_t fFlags)
|
---|
59 | {
|
---|
60 | AssertPtrReturn(phKey, VERR_INVALID_POINTER);
|
---|
61 | AssertMsgReturn(cBits >= 128 && cBits <= _64K, ("cBits=%u\n", cBits), VERR_OUT_OF_RANGE);
|
---|
62 | AssertReturn(uPubExp > 0, VERR_OUT_OF_RANGE);
|
---|
63 | AssertReturn(!fFlags, VERR_INVALID_FLAGS);
|
---|
64 |
|
---|
65 | rtCrOpenSslInit();
|
---|
66 |
|
---|
67 | /*
|
---|
68 | * Do the key generation first.
|
---|
69 | */
|
---|
70 | int rc = VERR_NO_MEMORY;
|
---|
71 | RSA *pRsa = RSA_new();
|
---|
72 | if (pRsa)
|
---|
73 | {
|
---|
74 | BIGNUM *pPubExp = BN_new();
|
---|
75 | if (pPubExp)
|
---|
76 | {
|
---|
77 | if (BN_set_word(pPubExp, uPubExp) != 0)
|
---|
78 | {
|
---|
79 | if (RSA_generate_key_ex(pRsa, cBits, pPubExp, NULL))
|
---|
80 | {
|
---|
81 | /*
|
---|
82 | * Create a IPRT key for it by encoding it as a private key.
|
---|
83 | */
|
---|
84 | unsigned char *pbRsaPrivateKey = NULL;
|
---|
85 | int cbRsaPrivateKey = i2d_RSAPrivateKey(pRsa, &pbRsaPrivateKey);
|
---|
86 | if (cbRsaPrivateKey)
|
---|
87 | {
|
---|
88 | rc = rtCrKeyCreateRsaPrivate(phKey, pbRsaPrivateKey, cbRsaPrivateKey, NULL, NULL);
|
---|
89 | OPENSSL_free(pbRsaPrivateKey);
|
---|
90 | }
|
---|
91 | /* else: VERR_NO_MEMORY */
|
---|
92 | }
|
---|
93 | else
|
---|
94 | rc = VERR_CR_KEY_GEN_FAILED_RSA;
|
---|
95 | }
|
---|
96 | BN_free(pPubExp);
|
---|
97 | }
|
---|
98 | RSA_free(pRsa);
|
---|
99 | }
|
---|
100 | return rc;
|
---|
101 | }
|
---|
102 |
|
---|
103 | #endif /* IPRT_WITH_OPENSSL */
|
---|
104 |
|
---|