VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/crypto/pkix-verify.cpp@ 57444

Last change on this file since 57444 was 57358, checked in by vboxsync, 9 years ago

*: scm cleanup run.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.2 KB
Line 
1/* $Id: pkix-verify.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
2/** @file
3 * IPRT - Crypto - Public Key Infrastructure API, Verification.
4 */
5
6/*
7 * Copyright (C) 2006-2015 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/err.h>
35#include <iprt/string.h>
36#include <iprt/crypto/digest.h>
37
38#ifdef IPRT_WITH_OPENSSL
39# include "internal/iprt-openssl.h"
40# include "openssl/evp.h"
41#endif
42
43
44RTDECL(int) RTCrPkixPubKeyVerifySignature(PCRTASN1OBJID pAlgorithm, PCRTASN1DYNTYPE pParameters, PCRTASN1BITSTRING pPublicKey,
45 PCRTASN1BITSTRING pSignatureValue, const void *pvData, size_t cbData,
46 PRTERRINFO pErrInfo)
47{
48 /*
49 * Valid input.
50 */
51 AssertPtrReturn(pAlgorithm, VERR_INVALID_POINTER);
52 AssertReturn(RTAsn1ObjId_IsPresent(pAlgorithm), VERR_INVALID_POINTER);
53
54 if (pParameters)
55 {
56 AssertPtrReturn(pParameters, VERR_INVALID_POINTER);
57 if (pParameters->enmType == RTASN1TYPE_NULL)
58 pParameters = NULL;
59 }
60
61 AssertPtrReturn(pPublicKey, VERR_INVALID_POINTER);
62 AssertReturn(RTAsn1BitString_IsPresent(pPublicKey), VERR_INVALID_POINTER);
63
64 AssertPtrReturn(pSignatureValue, VERR_INVALID_POINTER);
65 AssertReturn(RTAsn1BitString_IsPresent(pSignatureValue), VERR_INVALID_POINTER);
66
67 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
68 AssertReturn(cbData > 0, VERR_INVALID_PARAMETER);
69
70 /*
71 * Parameters are not currently supported (openssl code path).
72 */
73 if (pParameters)
74 return RTErrInfoSet(pErrInfo, VERR_CR_PKIX_CIPHER_ALGO_PARAMS_NOT_IMPL,
75 "Cipher algorithm parameters are not yet supported.");
76
77 /*
78 * Validate using IPRT.
79 */
80 RTCRPKIXSIGNATURE hSignature;
81 int rcIprt = RTCrPkixSignatureCreateByObjId(&hSignature, pAlgorithm, false /*fSigning*/, pPublicKey, pParameters);
82 if (RT_FAILURE(rcIprt))
83 return RTErrInfoSetF(pErrInfo, VERR_CR_PKIX_CIPHER_ALGO_NOT_KNOWN,
84 "Unknown public key algorithm [IPRT]: %s", pAlgorithm->szObjId);
85
86 RTCRDIGEST hDigest;
87 rcIprt = RTCrDigestCreateByObjId(&hDigest, pAlgorithm);
88 if (RT_SUCCESS(rcIprt))
89 {
90 /* Calculate the digest. */
91 rcIprt = RTCrDigestUpdate(hDigest, pvData, cbData);
92 if (RT_SUCCESS(rcIprt))
93 {
94 rcIprt = RTCrPkixSignatureVerifyBitString(hSignature, hDigest, pSignatureValue);
95 if (RT_FAILURE(rcIprt))
96 RTErrInfoSet(pErrInfo, rcIprt, "RTCrPkixSignatureVerifyBitString failed");
97 }
98 else
99 RTErrInfoSet(pErrInfo, rcIprt, "RTCrDigestUpdate failed");
100 RTCrDigestRelease(hDigest);
101 }
102 else
103 RTErrInfoSetF(pErrInfo, rcIprt, "Unknown digest algorithm [IPRT]: %s", pAlgorithm->szObjId);
104 RTCrPkixSignatureRelease(hSignature);
105
106#ifdef IPRT_WITH_OPENSSL
107 /*
108 * Validate using OpenSSL EVP.
109 */
110 rtCrOpenSslInit();
111
112 /* Translate the algorithm ID into a EVP message digest type pointer. */
113 int iAlgoNid = OBJ_txt2nid(pAlgorithm->szObjId);
114 if (iAlgoNid == NID_undef)
115 return RTErrInfoSetF(pErrInfo, VERR_CR_PKIX_OSSL_CIPHER_ALGO_NOT_KNOWN,
116 "Unknown public key algorithm [OpenSSL]: %s", pAlgorithm->szObjId);
117 const char *pszAlogSn = OBJ_nid2sn(iAlgoNid);
118 const EVP_MD *pEvpMdType = EVP_get_digestbyname(pszAlogSn);
119 if (!pEvpMdType)
120 return RTErrInfoSetF(pErrInfo, VERR_CR_PKIX_OSSL_CIPHER_ALGO_NOT_KNOWN_EVP,
121 "EVP_get_digestbyname failed on %s (%s)", pszAlogSn, pAlgorithm->szObjId);
122
123 /* Initialize the EVP message digest context. */
124 EVP_MD_CTX EvpMdCtx;
125 EVP_MD_CTX_init(&EvpMdCtx);
126 if (!EVP_VerifyInit_ex(&EvpMdCtx, pEvpMdType, NULL /*engine*/))
127 return RTErrInfoSetF(pErrInfo, VERR_CR_PKIX_OSSL_CIPHER_ALOG_INIT_FAILED,
128 "EVP_VerifyInit_ex failed (algorithm type is %s / %s)", pszAlogSn, pAlgorithm->szObjId);
129
130 /* Create an EVP public key. */
131 int rcOssl;
132 EVP_PKEY *pEvpPublicKey = EVP_PKEY_new();
133 if (pEvpPublicKey)
134 {
135 pEvpPublicKey->type = EVP_PKEY_type(pEvpMdType->required_pkey_type[0]);
136 if (pEvpPublicKey->type != NID_undef)
137 {
138 const unsigned char *puchPublicKey = RTASN1BITSTRING_GET_BIT0_PTR(pPublicKey);
139 if (d2i_PublicKey(pEvpPublicKey->type, &pEvpPublicKey, &puchPublicKey, RTASN1BITSTRING_GET_BYTE_SIZE(pPublicKey)))
140 {
141 /* Digest the data. */
142 EVP_VerifyUpdate(&EvpMdCtx, pvData, cbData);
143
144 /* Verify the signature. */
145 if (EVP_VerifyFinal(&EvpMdCtx,
146 RTASN1BITSTRING_GET_BIT0_PTR(pSignatureValue),
147 RTASN1BITSTRING_GET_BYTE_SIZE(pSignatureValue),
148 pEvpPublicKey) > 0)
149 rcOssl = VINF_SUCCESS;
150 else
151 rcOssl = RTErrInfoSet(pErrInfo, VERR_CR_PKIX_OSSL_VERIFY_FINAL_FAILED, "EVP_VerifyFinal failed");
152 }
153 else
154 rcOssl = RTErrInfoSet(pErrInfo, VERR_CR_PKIX_OSSL_D2I_PUBLIC_KEY_FAILED, "d2i_PublicKey failed");
155 }
156 else
157 rcOssl = RTErrInfoSetF(pErrInfo, VERR_CR_PKIX_OSSL_EVP_PKEY_TYPE_ERROR,
158 "EVP_PKEY_type(%d) failed", pEvpMdType->required_pkey_type[0]);
159 /* Cleanup and return.*/
160 EVP_PKEY_free(pEvpPublicKey);
161 }
162 else
163 rcOssl = RTErrInfoSetF(pErrInfo, VERR_NO_MEMORY, "EVP_PKEY_new(%d) failed", pEvpMdType->required_pkey_type[0]);
164 EVP_MD_CTX_cleanup(&EvpMdCtx);
165
166 /*
167 * Check the result.
168 */
169 if (RT_SUCCESS(rcIprt) && RT_SUCCESS(rcOssl))
170 return VINF_SUCCESS;
171 if (RT_FAILURE_NP(rcIprt) && RT_FAILURE_NP(rcOssl))
172 return rcIprt;
173 AssertMsgFailed(("rcIprt=%Rrc rcOssl=%Rrc\n", rcIprt, rcOssl));
174 if (RT_FAILURE_NP(rcOssl))
175 return rcOssl;
176#endif /* IPRT_WITH_OPENSSL */
177
178 return rcIprt;
179}
180
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