VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/crypto/pkix-util.cpp@ 84379

Last change on this file since 84379 was 84248, checked in by vboxsync, 5 years ago

IPRT/crypto: Adding functions for checking whether a key or certificate can handle a given digest (size wise). Also, added OIDs, padding variants and stuff for sha512-224WithRSAEncryption and sha512-256WithRSAEncryption (RFC-8017). Note that OpenSSL does not implement these yet. bugref:9699

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.9 KB
Line 
1/* $Id: pkix-util.cpp 84248 2020-05-11 11:46:40Z vboxsync $ */
2/** @file
3 * IPRT - Crypto - Public Key Infrastructure API, Utilities.
4 */
5
6/*
7 * Copyright (C) 2006-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include "internal/iprt.h"
32#include <iprt/crypto/pkix.h>
33
34#include <iprt/asn1.h>
35#include <iprt/assert.h>
36#include <iprt/err.h>
37#include <iprt/string.h>
38#include <iprt/crypto/rsa.h>
39
40#ifdef IPRT_WITH_OPENSSL
41# include "internal/iprt-openssl.h"
42# include "openssl/evp.h"
43#endif
44
45
46
47
48RTDECL(const char *) RTCrPkixGetCiperOidFromSignatureAlgorithm(PCRTASN1OBJID pAlgorithm)
49{
50 /*
51 * This is all hardcoded, at least for the time being.
52 */
53 if (RTAsn1ObjId_StartsWith(pAlgorithm, RTCR_PKCS1_OID))
54 {
55 if (RTAsn1ObjIdCountComponents(pAlgorithm) == 7)
56 switch (RTAsn1ObjIdGetLastComponentsAsUInt32(pAlgorithm))
57 {
58 case 2:
59 case 3:
60 case 4:
61 case 5:
62 case 11:
63 case 12:
64 case 13:
65 case 14:
66 return RTCR_PKCS1_RSA_OID;
67 case 1: AssertFailed();
68 RT_FALL_THRU();
69 default:
70 return NULL;
71 }
72 }
73 /*
74 * OIW oddballs.
75 */
76 else if (RTAsn1ObjId_StartsWith(pAlgorithm, "1.3.14.3.2"))
77 {
78 if (RTAsn1ObjIdCountComponents(pAlgorithm) == 6)
79 switch (RTAsn1ObjIdGetLastComponentsAsUInt32(pAlgorithm))
80 {
81 case 11:
82 case 14:
83 case 15:
84 case 24:
85 case 25:
86 case 29:
87 return RTCR_PKCS1_RSA_OID;
88 default:
89 return NULL;
90 }
91 }
92
93
94 return NULL;
95}
96
97
98RTDECL(bool) RTCrPkixPubKeyCanHandleDigestType(PCRTCRX509SUBJECTPUBLICKEYINFO pPublicKeyInfo, RTDIGESTTYPE enmDigestType,
99 PRTERRINFO pErrInfo)
100{
101 bool fRc = false;
102 if (RTCrX509SubjectPublicKeyInfo_IsPresent(pPublicKeyInfo))
103 {
104 void const * const pvKeyBits = RTASN1BITSTRING_GET_BIT0_PTR(&pPublicKeyInfo->SubjectPublicKey);
105 uint32_t const cbKeyBits = RTASN1BITSTRING_GET_BYTE_SIZE(&pPublicKeyInfo->SubjectPublicKey);
106 RTASN1CURSORPRIMARY PrimaryCursor;
107 union
108 {
109 RTCRRSAPUBLICKEY RsaPublicKey;
110 } u;
111
112 if (RTAsn1ObjId_CompareWithString(&pPublicKeyInfo->Algorithm.Algorithm, RTCR_PKCS1_RSA_OID) == 0)
113 {
114 /*
115 * RSA.
116 */
117 RTAsn1CursorInitPrimary(&PrimaryCursor, pvKeyBits, cbKeyBits, pErrInfo, &g_RTAsn1DefaultAllocator,
118 RTASN1CURSOR_FLAGS_DER, "rsa");
119
120 RT_ZERO(u.RsaPublicKey);
121 int rc = RTCrRsaPublicKey_DecodeAsn1(&PrimaryCursor.Cursor, 0, &u.RsaPublicKey, "PublicKey");
122 if (RT_SUCCESS(rc))
123 fRc = RTCrRsaPublicKey_CanHandleDigestType(&u.RsaPublicKey, enmDigestType, pErrInfo);
124 RTCrRsaPublicKey_Delete(&u.RsaPublicKey);
125 }
126 else
127 {
128 RTErrInfoSetF(pErrInfo, VERR_CR_PKIX_CIPHER_ALGO_NOT_KNOWN, "%s", pPublicKeyInfo->Algorithm.Algorithm.szObjId);
129 AssertMsgFailed(("unknown key algorithm: %s\n", pPublicKeyInfo->Algorithm.Algorithm.szObjId));
130 fRc = true;
131 }
132 }
133 return fRc;
134}
135
136
137RTDECL(bool) RTCrPkixCanCertHandleDigestType(PCRTCRX509CERTIFICATE pCertificate, RTDIGESTTYPE enmDigestType, PRTERRINFO pErrInfo)
138{
139 if (RTCrX509Certificate_IsPresent(pCertificate))
140 return RTCrPkixPubKeyCanHandleDigestType(&pCertificate->TbsCertificate.SubjectPublicKeyInfo, enmDigestType, pErrInfo);
141 return false;
142}
143
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