VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/crypto/pkix-sign.cpp

Last change on this file was 100442, checked in by vboxsync, 11 months ago

IPRT,OpenSSL: Support ECDSA for verficiation purposes when IPRT links with OpenSSL. This required quite a bit of cleanups, so not entirely no-risk. bugref:10479 ticketref:21621

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.2 KB
Line 
1/* $Id: pkix-sign.cpp 100442 2023-07-08 11:10:51Z vboxsync $ */
2/** @file
3 * IPRT - Crypto - Public Key Infrastructure API, Verification.
4 */
5
6/*
7 * Copyright (C) 2006-2023 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/pkix.h>
43
44#include <iprt/alloca.h>
45#include <iprt/err.h>
46#include <iprt/mem.h>
47#include <iprt/string.h>
48#include <iprt/crypto/digest.h>
49#include <iprt/crypto/key.h>
50
51#ifdef IPRT_WITH_OPENSSL
52# include "internal/iprt-openssl.h"
53# include "internal/openssl-pre.h"
54# include <openssl/evp.h>
55# include <openssl/rsa.h>
56# include "internal/openssl-post.h"
57# ifndef OPENSSL_VERSION_NUMBER
58# error "Missing OPENSSL_VERSION_NUMBER!"
59# endif
60#endif
61
62
63#if 0
64RTDECL(int) RTCrPkixPubKeySignData(PCRTASN1OBJID pAlgorithm, PCRTASN1DYNTYPE pParameters, PCRTASN1BITSTRING pPrivateKey,
65 PCRTASN1BITSTRING pSignatureValue, const void *pvData, size_t cbData, PRTERRINFO pErrInfo)
66{
67 /*
68 * Validate the digest related inputs.
69 */
70 AssertPtrReturn(pAlgorithm, VERR_INVALID_POINTER);
71 AssertReturn(RTAsn1ObjId_IsPresent(pAlgorithm), VERR_INVALID_POINTER);
72
73 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
74 AssertReturn(cbData > 0, VERR_INVALID_PARAMETER);
75
76 /*
77 * Digest the data and call the other API.
78 */
79 RTCRDIGEST hDigest;
80 int rc = RTCrDigestCreateByObjId(&hDigest, pAlgorithm);
81 if (RT_SUCCESS(rcIprt))
82 {
83 rc = RTCrDigestUpdate(hDigest, pvData, cbData);
84 if (RT_SUCCESS(rcIprt))
85 rc = RTCrPkixPubKeySignDigest(pAlgorithm, pParameters, pPrivateKey, pvSignedDigest, cbSignedDigest, hDigest, pErrInfo);
86 else
87 RTErrInfoSet(pErrInfo, rcIprt, "RTCrDigestUpdate failed");
88 RTCrDigestRelease(hDigest);
89 }
90 else
91 RTErrInfoSetF(pErrInfo, rcIprt, "Unknown digest algorithm [IPRT]: %s", pAlgorithm->szObjId);
92 return rc;
93}
94#endif
95
96
97RTDECL(int) RTCrPkixPubKeySignDigest(PCRTASN1OBJID pAlgorithm, RTCRKEY hPrivateKey, PCRTASN1DYNTYPE pParameters,
98 RTCRDIGEST hDigest, uint32_t fFlags,
99 void *pvSignature, size_t *pcbSignature, PRTERRINFO pErrInfo)
100{
101 /*
102 * Valid input.
103 */
104 AssertPtrReturn(pAlgorithm, VERR_INVALID_POINTER);
105 AssertReturn(RTAsn1ObjId_IsPresent(pAlgorithm), VERR_INVALID_POINTER);
106
107 if (pParameters)
108 {
109 AssertPtrReturn(pParameters, VERR_INVALID_POINTER);
110 if (pParameters->enmType == RTASN1TYPE_NULL)
111 pParameters = NULL;
112 }
113
114 AssertPtrReturn(hPrivateKey, VERR_INVALID_POINTER);
115 Assert(RTCrKeyHasPrivatePart(hPrivateKey));
116
117 AssertPtrReturn(pcbSignature, VERR_INVALID_PARAMETER);
118 size_t cbSignature = *pcbSignature;
119 if (cbSignature)
120 AssertPtrReturn(pvSignature, VERR_INVALID_POINTER);
121 else
122 pvSignature = NULL;
123
124 AssertPtrReturn(hDigest, VERR_INVALID_HANDLE);
125
126 AssertReturn(fFlags == 0, VERR_INVALID_FLAGS);
127
128 /*
129 * Parameters are not currently supported (openssl code path).
130 */
131 if (pParameters)
132 return RTErrInfoSet(pErrInfo, VERR_CR_PKIX_CIPHER_ALGO_PARAMS_NOT_IMPL,
133 "Cipher algorithm parameters are not yet supported.");
134
135 /*
136 * Sign using IPRT.
137 */
138 RTCRPKIXSIGNATURE hSignature;
139 int rcIprt = RTCrPkixSignatureCreateByObjId(&hSignature, pAlgorithm, hPrivateKey, pParameters, true /*fSigning*/);
140 if (RT_FAILURE(rcIprt))
141 return RTErrInfoSetF(pErrInfo, VERR_CR_PKIX_CIPHER_ALGO_NOT_KNOWN,
142 "Unknown private key algorithm [IPRT %Rrc]: %s", rcIprt, pAlgorithm->szObjId);
143
144 rcIprt = RTCrPkixSignatureSign(hSignature, hDigest, pvSignature, pcbSignature);
145 if (RT_FAILURE(rcIprt))
146 RTErrInfoSet(pErrInfo, rcIprt, "RTCrPkixSignatureSign failed");
147
148 RTCrPkixSignatureRelease(hSignature);
149
150 /*
151 * Sign using OpenSSL EVP if we can.
152 */
153#if defined(IPRT_WITH_OPENSSL) \
154 && (OPENSSL_VERSION_NUMBER > 0x10000000L) /* 0.9.8 doesn't seem to have EVP_PKEY_CTX_set_signature_md. */
155
156 /* Make sure the algorithm includes the digest and isn't just RSA, ECDSA or similar. */
157 const char *pszAlgObjId = RTCrX509AlgorithmIdentifier_CombineEncryptionOidAndDigestOid(pAlgorithm->szObjId,
158 RTCrDigestGetAlgorithmOid(hDigest));
159 AssertMsgStmt(pszAlgObjId, ("enc=%s hash=%s\n", pAlgorithm->szObjId, RTCrDigestGetAlgorithmOid(hDigest)),
160 pszAlgObjId = RTCrDigestGetAlgorithmOid(hDigest));
161
162 /* Create an EVP private key. */
163 EVP_PKEY *pEvpPrivateKey = NULL;
164 const EVP_MD *pEvpMdType = NULL;
165 int rcOssl = rtCrKeyToOpenSslKeyEx(hPrivateKey, false /*fNeedPublic*/, pszAlgObjId,
166 (void **)&pEvpPrivateKey, (const void **)&pEvpMdType, pErrInfo);
167 if (RT_SUCCESS(rcOssl))
168 {
169 /* Create an EVP Private key context we can use to validate the digest. */
170 EVP_PKEY_CTX *pEvpPKeyCtx = EVP_PKEY_CTX_new(pEvpPrivateKey, NULL);
171 if (pEvpPKeyCtx)
172 {
173 rcOssl = EVP_PKEY_sign_init(pEvpPKeyCtx);
174 if (rcOssl > 0)
175 {
176 rcOssl = EVP_PKEY_CTX_set_rsa_padding(pEvpPKeyCtx, RSA_PKCS1_PADDING);
177 if (rcOssl > 0)
178 {
179 rcOssl = EVP_PKEY_CTX_set_signature_md(pEvpPKeyCtx, pEvpMdType);
180 if (rcOssl > 0)
181 {
182 /* Allocate a signature buffer. */
183 unsigned char *pbOsslSignature = NULL;
184 void *pvOsslSignatureFree = NULL;
185 size_t cbOsslSignature = cbSignature;
186 if (cbOsslSignature > 0)
187 {
188 if (cbOsslSignature < _1K)
189 pbOsslSignature = (unsigned char *)alloca(cbOsslSignature);
190 else
191 {
192 pbOsslSignature = (unsigned char *)RTMemTmpAlloc(cbOsslSignature);
193 pvOsslSignatureFree = pbOsslSignature;
194 }
195 }
196 if (cbOsslSignature == 0 || pbOsslSignature != NULL)
197 {
198 /* Get the digest from hDigest and sign it. */
199 rcOssl = EVP_PKEY_sign(pEvpPKeyCtx,
200 pbOsslSignature,
201 &cbOsslSignature,
202 (const unsigned char *)RTCrDigestGetHash(hDigest),
203 RTCrDigestGetHashSize(hDigest));
204 if (rcOssl > 0)
205 {
206 /* Compare the result. The memcmp assums no random padding bits. */
207 rcOssl = VINF_SUCCESS;
208 AssertMsgStmt(cbOsslSignature == *pcbSignature,
209 ("cbOsslSignature=%#x, iprt %#x\n", cbOsslSignature, *pcbSignature),
210 rcOssl = VERR_CR_PKIX_OSSL_VS_IPRT_SIGNATURE_SIZE);
211 AssertMsgStmt( pbOsslSignature == NULL
212 || rcOssl != VINF_SUCCESS
213 || memcmp(pbOsslSignature, pvSignature, cbOsslSignature) == 0,
214 ("OpenSSL: %.*Rhxs\n"
215 "IPRT: %.*Rhxs\n",
216 cbOsslSignature, pbOsslSignature, *pcbSignature, pvSignature),
217 rcOssl = VERR_CR_PKIX_OSSL_VS_IPRT_SIGNATURE);
218 if (!pbOsslSignature && rcOssl == VINF_SUCCESS)
219 rcOssl = VERR_BUFFER_OVERFLOW;
220 }
221 else
222 rcOssl = RTErrInfoSetF(pErrInfo, VERR_CR_PKIX_OSSL_SIGN_FINAL_FAILED,
223 "EVP_PKEY_sign failed (%d)", rcOssl);
224 if (pvOsslSignatureFree)
225 RTMemTmpFree(pvOsslSignatureFree);
226 }
227 else
228 rcOssl = VERR_NO_TMP_MEMORY;
229 }
230 else
231 rcOssl = RTErrInfoSetF(pErrInfo, VERR_CR_PKIX_OSSL_EVP_PKEY_TYPE_ERROR,
232 "EVP_PKEY_CTX_set_signature_md failed (%d)", rcOssl);
233 }
234 else
235 rcOssl = RTErrInfoSetF(pErrInfo, VERR_CR_PKIX_OSSL_EVP_PKEY_RSA_PAD_ERROR,
236 "EVP_PKEY_CTX_set_rsa_padding failed (%d)", rcOssl);
237 }
238 else
239 rcOssl = RTErrInfoSetF(pErrInfo, VERR_CR_PKIX_OSSL_EVP_PKEY_TYPE_ERROR,
240 "EVP_PKEY_verify_init failed (%d)", rcOssl);
241 EVP_PKEY_CTX_free(pEvpPKeyCtx);
242 }
243 else
244 rcOssl = RTErrInfoSet(pErrInfo, VERR_CR_PKIX_OSSL_EVP_PKEY_TYPE_ERROR, "EVP_PKEY_CTX_new failed");
245 EVP_PKEY_free(pEvpPrivateKey);
246 }
247
248 /*
249 * Check the result.
250 */
251 if ( (RT_SUCCESS(rcIprt) && RT_SUCCESS(rcOssl))
252 || (RT_FAILURE_NP(rcIprt) && RT_FAILURE_NP(rcOssl))
253 || (RT_SUCCESS(rcIprt) && rcOssl == VERR_CR_PKIX_OSSL_CIPHER_ALGO_NOT_KNOWN_EVP) )
254 return rcIprt;
255 AssertMsgFailed(("rcIprt=%Rrc rcOssl=%Rrc\n", rcIprt, rcOssl));
256 if (RT_FAILURE_NP(rcOssl))
257 return rcOssl;
258#endif /* IPRT_WITH_OPENSSL */
259
260 return rcIprt;
261}
262
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use