VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/crypto/x509-verify.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: 5.3 KB
Line 
1/* $Id: x509-verify.cpp 100442 2023-07-08 11:10:51Z vboxsync $ */
2/** @file
3 * IPRT - Crypto - X.509, Signature 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/x509.h>
43#include <iprt/crypto/pkix.h>
44#include <iprt/crypto/key.h>
45
46#include <iprt/err.h>
47#include <iprt/mem.h>
48#include <iprt/string.h>
49
50
51RTDECL(int) RTCrX509Certificate_VerifySignature(PCRTCRX509CERTIFICATE pThis, PCRTASN1OBJID pAlgorithm,
52 PCRTASN1DYNTYPE pParameters, PCRTASN1BITSTRING pPublicKey,
53 PRTERRINFO pErrInfo)
54{
55 /*
56 * Validate the input a little.
57 */
58 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
59 AssertReturn(RTCrX509Certificate_IsPresent(pThis), VERR_INVALID_PARAMETER);
60
61 AssertPtrReturn(pAlgorithm, VERR_INVALID_POINTER);
62 AssertReturn(RTAsn1ObjId_IsPresent(pAlgorithm), VERR_INVALID_POINTER);
63
64 AssertPtrReturn(pPublicKey, VERR_INVALID_POINTER);
65 AssertReturn(RTAsn1BitString_IsPresent(pPublicKey), VERR_INVALID_POINTER);
66
67 /*
68 * Check if the algorithm matches.
69 */
70 const char * const pszCipherOid = RTCrX509AlgorithmIdentifier_GetEncryptionOid(&pThis->SignatureAlgorithm,
71 true /*fMustIncludeHash*/);
72 if (!pszCipherOid)
73 return RTErrInfoSetF(pErrInfo, VERR_CR_X509_UNKNOWN_CERT_SIGN_ALGO,
74 "Certificate signature algorithm not known: %s",
75 pThis->SignatureAlgorithm.Algorithm.szObjId);
76
77 if (RTAsn1ObjId_CompareWithString(pAlgorithm, pszCipherOid) != 0)
78 return RTErrInfoSetF(pErrInfo, VERR_CR_X509_CERT_SIGN_ALGO_MISMATCH,
79 "Certificate signature cipher algorithm mismatch: cert uses %s (%s) while key uses %s",
80 pszCipherOid, pThis->SignatureAlgorithm.Algorithm.szObjId, pAlgorithm->szObjId);
81
82 /*
83 * Wrap up the public key.
84 */
85 RTCRKEY hPubKey;
86 int rc = RTCrKeyCreateFromPublicAlgorithmAndBits(&hPubKey, pAlgorithm, pParameters, pPublicKey, pErrInfo, NULL);
87 if (RT_FAILURE(rc))
88 return rc;
89
90 /*
91 * Here we should recode the to-be-signed part as DER, but we'll ASSUME
92 * that it's already in DER encoding and only does this if there the
93 * encoded bits are missing.
94 */
95 const uint8_t *pbRaw;
96 uint32_t cbRaw;
97 void *pvFree = NULL;
98 rc = RTAsn1EncodeQueryRawBits(RTCrX509TbsCertificate_GetAsn1Core(&pThis->TbsCertificate), &pbRaw, &cbRaw, &pvFree, pErrInfo);
99 if (RT_SUCCESS(rc))
100 {
101 rc = RTCrPkixPubKeyVerifySignature(&pThis->SignatureAlgorithm.Algorithm, hPubKey, &pThis->SignatureAlgorithm.Parameters,
102 &pThis->SignatureValue, pbRaw, cbRaw, pErrInfo);
103 RTMemTmpFree(pvFree);
104 }
105
106 /* Free the public key. */
107 uint32_t cRefs = RTCrKeyRelease(hPubKey);
108 Assert(cRefs == 0); NOREF(cRefs);
109
110 return rc;
111}
112
113
114RTDECL(int) RTCrX509Certificate_VerifySignatureSelfSigned(PCRTCRX509CERTIFICATE pThis, PRTERRINFO pErrInfo)
115{
116 /*
117 * Validate the input a little.
118 */
119 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
120 AssertReturn(RTCrX509Certificate_IsPresent(pThis), VERR_INVALID_PARAMETER);
121
122 /*
123 * Call generic verification function.
124 */
125 PCRTCRX509TBSCERTIFICATE const pTbsCert = &pThis->TbsCertificate;
126 return RTCrX509Certificate_VerifySignature(pThis,
127 &pTbsCert->SubjectPublicKeyInfo.Algorithm.Algorithm,
128 &pTbsCert->SubjectPublicKeyInfo.Algorithm.Parameters,
129 &pTbsCert->SubjectPublicKeyInfo.SubjectPublicKey, pErrInfo);
130}
131
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use