VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/crypto/x509-verify.cpp@ 96763

Last change on this file since 96763 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.6 KB
Line 
1/* $Id: x509-verify.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * IPRT - Crypto - X.509, Signature verification.
4 */
5
6/*
7 * Copyright (C) 2006-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/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 if (pParameters)
65 {
66 AssertPtrReturn(pParameters, VERR_INVALID_POINTER);
67 if (pParameters->enmType == RTASN1TYPE_NULL)
68 pParameters = NULL;
69 }
70
71 AssertPtrReturn(pPublicKey, VERR_INVALID_POINTER);
72 AssertReturn(RTAsn1BitString_IsPresent(pPublicKey), VERR_INVALID_POINTER);
73
74 /*
75 * Check if the algorithm matches.
76 */
77 const char *pszCipherOid = RTCrPkixGetCiperOidFromSignatureAlgorithm(&pThis->SignatureAlgorithm.Algorithm);
78 if (!pszCipherOid)
79 return RTErrInfoSetF(pErrInfo, VERR_CR_X509_UNKNOWN_CERT_SIGN_ALGO,
80 "Certificate signature algorithm not known: %s",
81 pThis->SignatureAlgorithm.Algorithm.szObjId);
82
83 if (RTAsn1ObjId_CompareWithString(pAlgorithm, pszCipherOid) != 0)
84 return RTErrInfoSetF(pErrInfo, VERR_CR_X509_CERT_SIGN_ALGO_MISMATCH,
85 "Certificate signature cipher algorithm mismatch: cert uses %s (%s) while key uses %s",
86 pszCipherOid, pThis->SignatureAlgorithm.Algorithm.szObjId, pAlgorithm->szObjId);
87
88 /*
89 * Wrap up the public key.
90 */
91 RTCRKEY hPubKey;
92 int rc = RTCrKeyCreateFromPublicAlgorithmAndBits(&hPubKey, pAlgorithm, pPublicKey, pErrInfo, NULL);
93 if (RT_FAILURE(rc))
94 return rc;
95
96 /*
97 * Here we should recode the to-be-signed part as DER, but we'll ASSUME
98 * that it's already in DER encoding and only does this if there the
99 * encoded bits are missing.
100 */
101 const uint8_t *pbRaw;
102 uint32_t cbRaw;
103 void *pvFree = NULL;
104 rc = RTAsn1EncodeQueryRawBits(RTCrX509TbsCertificate_GetAsn1Core(&pThis->TbsCertificate), &pbRaw, &cbRaw, &pvFree, pErrInfo);
105 if (RT_SUCCESS(rc))
106 {
107 rc = RTCrPkixPubKeyVerifySignature(&pThis->SignatureAlgorithm.Algorithm, hPubKey, pParameters, &pThis->SignatureValue,
108 pbRaw, cbRaw, pErrInfo);
109 RTMemTmpFree(pvFree);
110 }
111
112 /* Free the public key. */
113 uint32_t cRefs = RTCrKeyRelease(hPubKey);
114 Assert(cRefs == 0); NOREF(cRefs);
115
116 return rc;
117}
118
119
120RTDECL(int) RTCrX509Certificate_VerifySignatureSelfSigned(PCRTCRX509CERTIFICATE pThis, PRTERRINFO pErrInfo)
121{
122 /*
123 * Validate the input a little.
124 */
125 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
126 AssertReturn(RTCrX509Certificate_IsPresent(pThis), VERR_INVALID_PARAMETER);
127
128 /*
129 * Assemble parameters for the generic verification call.
130 */
131 PCRTCRX509TBSCERTIFICATE const pTbsCert = &pThis->TbsCertificate;
132 PCRTASN1DYNTYPE pParameters = NULL;
133 if ( RTASN1CORE_IS_PRESENT(&pTbsCert->SubjectPublicKeyInfo.Algorithm.Parameters.u.Core)
134 && pTbsCert->SubjectPublicKeyInfo.Algorithm.Parameters.enmType != RTASN1TYPE_NULL)
135 pParameters = &pTbsCert->SubjectPublicKeyInfo.Algorithm.Parameters;
136 return RTCrX509Certificate_VerifySignature(pThis, &pTbsCert->SubjectPublicKeyInfo.Algorithm.Algorithm, pParameters,
137 &pTbsCert->SubjectPublicKeyInfo.SubjectPublicKey, pErrInfo);
138}
139
Note: See TracBrowser for help on using the repository browser.

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