pkix-verify.cpp revision 13493ab7596e827b8d0caab2c89e635dd65f78f9
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IPRT - Crypto - Public Key Infrastructure API, Verification.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Copyright (C) 2006-2014 Oracle Corporation
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * available from http://www.virtualbox.org. This file is free software;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * you can redistribute it and/or modify it under the terms of the GNU
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * General Public License (GPL) as published by the Free Software
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * The contents of this file may alternatively be used under the terms
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * of the Common Development and Distribution License Version 1.0
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * VirtualBox OSE distribution, in which case the provisions of the
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * CDDL are applicable instead of those of the GPL.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * You may elect to license modified versions of this file under the
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * terms and conditions of either the GPL or the CDDL or both.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/*******************************************************************************
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync* Header Files *
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync*******************************************************************************/
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(int) RTCrPkixPubKeyVerifySignature(PCRTASN1OBJID pAlgorithm, PCRTASN1DYNTYPE pParameters, PCRTASN1BITSTRING pPublicKey,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync PCRTASN1BITSTRING pSignatureValue, const void *pvData, size_t cbData,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Valid input.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync AssertReturn(RTAsn1ObjId_IsPresent(pAlgorithm), VERR_INVALID_POINTER);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync AssertPtrReturn(pParameters, VERR_INVALID_POINTER);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync AssertReturn(RTAsn1BitString_IsPresent(pPublicKey), VERR_INVALID_POINTER);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync AssertPtrReturn(pSignatureValue, VERR_INVALID_POINTER);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync AssertReturn(RTAsn1BitString_IsPresent(pSignatureValue), VERR_INVALID_POINTER);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Parameters are not currently supported (openssl code path).
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync return RTErrInfoSet(pErrInfo, VERR_CR_PKIX_CIPHER_ALGO_PARAMS_NOT_IMPL,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync "Cipher algorithm parameters are not yet supported.");
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Validate using IPRT.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync int rcIprt = RTCrPkixSignatureCreateByObjId(&hSignature, pAlgorithm, false /*fSigning*/, pPublicKey, pParameters);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync return RTErrInfoSetF(pErrInfo, VERR_CR_PKIX_CIPHER_ALGO_NOT_KNOWN,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync "Unknown public key algorithm [IPRT]: %s", pAlgorithm->szObjId);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rcIprt = RTCrDigestCreateByObjId(&hDigest, pAlgorithm);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /* Calculate the digest. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rcIprt = RTCrDigestUpdate(hDigest, pvData, cbData);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rcIprt = RTCrPkixSignatureVerifyBitString(hSignature, hDigest, pSignatureValue);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync RTErrInfoSet(pErrInfo, rcIprt, "RTCrPkixSignatureVerifyBitString failed");
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync RTErrInfoSet(pErrInfo, rcIprt, "RTCrDigestUpdate failed");
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync RTErrInfoSetF(pErrInfo, rcIprt, "Unknown digest algorithm [IPRT]: %s", pAlgorithm->szObjId);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Validate using OpenSSL EVP.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /* Translate the algorithm ID into a EVP message digest type pointer. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync return RTErrInfoSetF(pErrInfo, VERR_CR_PKIX_OSSL_CIPHER_ALGO_NOT_KNOWN,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync "Unknown public key algorithm [OpenSSL]: %s", pAlgorithm->szObjId);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync const EVP_MD *pEvpMdType = EVP_get_digestbyname(pszAlogSn);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync return RTErrInfoSetF(pErrInfo, VERR_CR_PKIX_OSSL_CIPHER_ALGO_NOT_KNOWN_EVP,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync "EVP_get_digestbyname failed on %s (%s)", pszAlogSn, pAlgorithm->szObjId);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /* Initialize the EVP message digest context. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if (!EVP_VerifyInit_ex(&EvpMdCtx, pEvpMdType, NULL /*engine*/))
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync return RTErrInfoSetF(pErrInfo, VERR_CR_PKIX_OSSL_CIPHER_ALOG_INIT_FAILED,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync "EVP_VerifyInit_ex failed (algorithm type is %s / %s)", pszAlogSn, pAlgorithm->szObjId);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /* Create an EVP public key. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pEvpPublicKey->type = EVP_PKEY_type(pEvpMdType->required_pkey_type[0]);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync const unsigned char *puchPublicKey = RTASN1BITSTRING_GET_BIT0_PTR(pPublicKey);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if (d2i_PublicKey(pEvpPublicKey->type, &pEvpPublicKey, &puchPublicKey, RTASN1BITSTRING_GET_BYTE_SIZE(pPublicKey)))
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /* Digest the data. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /* Verify the signature. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rcOssl = RTErrInfoSet(pErrInfo, VERR_CR_PKIX_OSSL_VERIFY_FINAL_FAILED, "EVP_VerifyFinal failed");
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rcOssl = RTErrInfoSet(pErrInfo, VERR_CR_PKIX_OSSL_D2I_PUBLIC_KEY_FAILED, "d2i_PublicKey failed");
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rcOssl = RTErrInfoSetF(pErrInfo, VERR_CR_PKIX_OSSL_EVP_PKEY_TYPE_ERROR,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync "EVP_PKEY_type(%d) failed", pEvpMdType->required_pkey_type[0]);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /* Cleanup and return.*/
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Check the result.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if (RT_FAILURE_NP(rcIprt) && RT_FAILURE_NP(rcOssl))
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync AssertMsgFailed(("rcIprt=%Rrc rcOssl=%Rrc\n", rcIprt, rcOssl));
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync#endif /* IPRT_WITH_OPENSSL */