1 | /* $Id: pkix-util.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Crypto - Public Key Infrastructure API, Utilities.
|
---|
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/pkix.h>
|
---|
43 |
|
---|
44 | #include <iprt/asn1.h>
|
---|
45 | #include <iprt/assert.h>
|
---|
46 | #include <iprt/err.h>
|
---|
47 | #include <iprt/string.h>
|
---|
48 | #include <iprt/crypto/rsa.h>
|
---|
49 |
|
---|
50 | #ifdef IPRT_WITH_OPENSSL
|
---|
51 | # include "internal/iprt-openssl.h"
|
---|
52 | # include "internal/openssl-pre.h"
|
---|
53 | # include <openssl/evp.h>
|
---|
54 | # include "internal/openssl-post.h"
|
---|
55 | #endif
|
---|
56 |
|
---|
57 |
|
---|
58 |
|
---|
59 |
|
---|
60 | RTDECL(const char *) RTCrPkixGetCiperOidFromSignatureAlgorithm(PCRTASN1OBJID pAlgorithm)
|
---|
61 | {
|
---|
62 | /*
|
---|
63 | * This is all hardcoded, at least for the time being.
|
---|
64 | */
|
---|
65 | if (RTAsn1ObjId_StartsWith(pAlgorithm, RTCR_PKCS1_OID))
|
---|
66 | {
|
---|
67 | if (RTAsn1ObjIdCountComponents(pAlgorithm) == 7)
|
---|
68 | switch (RTAsn1ObjIdGetLastComponentsAsUInt32(pAlgorithm))
|
---|
69 | {
|
---|
70 | case 2:
|
---|
71 | case 3:
|
---|
72 | case 4:
|
---|
73 | case 5:
|
---|
74 | case 11:
|
---|
75 | case 12:
|
---|
76 | case 13:
|
---|
77 | case 14:
|
---|
78 | return RTCR_PKCS1_RSA_OID;
|
---|
79 | case 1: AssertFailed();
|
---|
80 | RT_FALL_THRU();
|
---|
81 | default:
|
---|
82 | return NULL;
|
---|
83 | }
|
---|
84 | }
|
---|
85 | /*
|
---|
86 | * OIW oddballs.
|
---|
87 | */
|
---|
88 | else if (RTAsn1ObjId_StartsWith(pAlgorithm, "1.3.14.3.2"))
|
---|
89 | {
|
---|
90 | if (RTAsn1ObjIdCountComponents(pAlgorithm) == 6)
|
---|
91 | switch (RTAsn1ObjIdGetLastComponentsAsUInt32(pAlgorithm))
|
---|
92 | {
|
---|
93 | case 11:
|
---|
94 | case 14:
|
---|
95 | case 15:
|
---|
96 | case 24:
|
---|
97 | case 25:
|
---|
98 | case 29:
|
---|
99 | return RTCR_PKCS1_RSA_OID;
|
---|
100 | default:
|
---|
101 | return NULL;
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 |
|
---|
106 | return NULL;
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | RTDECL(bool) RTCrPkixPubKeyCanHandleDigestType(PCRTCRX509SUBJECTPUBLICKEYINFO pPublicKeyInfo, RTDIGESTTYPE enmDigestType,
|
---|
111 | PRTERRINFO pErrInfo)
|
---|
112 | {
|
---|
113 | bool fRc = false;
|
---|
114 | if (RTCrX509SubjectPublicKeyInfo_IsPresent(pPublicKeyInfo))
|
---|
115 | {
|
---|
116 | void const * const pvKeyBits = RTASN1BITSTRING_GET_BIT0_PTR(&pPublicKeyInfo->SubjectPublicKey);
|
---|
117 | uint32_t const cbKeyBits = RTASN1BITSTRING_GET_BYTE_SIZE(&pPublicKeyInfo->SubjectPublicKey);
|
---|
118 | RTASN1CURSORPRIMARY PrimaryCursor;
|
---|
119 | union
|
---|
120 | {
|
---|
121 | RTCRRSAPUBLICKEY RsaPublicKey;
|
---|
122 | } u;
|
---|
123 |
|
---|
124 | if (RTAsn1ObjId_CompareWithString(&pPublicKeyInfo->Algorithm.Algorithm, RTCR_PKCS1_RSA_OID) == 0)
|
---|
125 | {
|
---|
126 | /*
|
---|
127 | * RSA.
|
---|
128 | */
|
---|
129 | RTAsn1CursorInitPrimary(&PrimaryCursor, pvKeyBits, cbKeyBits, pErrInfo, &g_RTAsn1DefaultAllocator,
|
---|
130 | RTASN1CURSOR_FLAGS_DER, "rsa");
|
---|
131 |
|
---|
132 | RT_ZERO(u.RsaPublicKey);
|
---|
133 | int rc = RTCrRsaPublicKey_DecodeAsn1(&PrimaryCursor.Cursor, 0, &u.RsaPublicKey, "PublicKey");
|
---|
134 | if (RT_SUCCESS(rc))
|
---|
135 | fRc = RTCrRsaPublicKey_CanHandleDigestType(&u.RsaPublicKey, enmDigestType, pErrInfo);
|
---|
136 | RTCrRsaPublicKey_Delete(&u.RsaPublicKey);
|
---|
137 | }
|
---|
138 | else
|
---|
139 | {
|
---|
140 | RTErrInfoSetF(pErrInfo, VERR_CR_PKIX_CIPHER_ALGO_NOT_KNOWN, "%s", pPublicKeyInfo->Algorithm.Algorithm.szObjId);
|
---|
141 | AssertMsgFailed(("unknown key algorithm: %s\n", pPublicKeyInfo->Algorithm.Algorithm.szObjId));
|
---|
142 | fRc = true;
|
---|
143 | }
|
---|
144 | }
|
---|
145 | return fRc;
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|
149 | RTDECL(bool) RTCrPkixCanCertHandleDigestType(PCRTCRX509CERTIFICATE pCertificate, RTDIGESTTYPE enmDigestType, PRTERRINFO pErrInfo)
|
---|
150 | {
|
---|
151 | if (RTCrX509Certificate_IsPresent(pCertificate))
|
---|
152 | return RTCrPkixPubKeyCanHandleDigestType(&pCertificate->TbsCertificate.SubjectPublicKeyInfo, enmDigestType, pErrInfo);
|
---|
153 | return false;
|
---|
154 | }
|
---|
155 |
|
---|