pkix-signature-rsa.cpp revision 13493ab7596e827b8d0caab2c89e635dd65f78f9
/* $Id$ */
/** @file
* IPRT - Crypto - Public Key Signature Schema Algorithm, RSA Providers.
*/
/*
* Copyright (C) 2006-2014 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include "rsa-internal.h"
#include "pkix-signature-builtin.h"
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* RSA signature provider instance.
*/
typedef struct RTCRPKIXSIGNATURERSA
{
/** Set if we're signing, clear if verifying. */
bool fSigning;
/** The modulus. */
/** The exponent. */
/** Temporary big number for use when signing or verifiying. */
/** Temporary big number for use when signing or verifiying. */
/** Scratch space for decoding the key. */
union
{
/** Public key. */
/** Private key. */
/** Scratch area where we assemble the signature. */
} Scratch;
/** Pointer to an RSA signature provider instance. */
typedef RTCRPKIXSIGNATURERSA *PRTCRPKIXSIGNATURERSA;
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** @name Pre-encoded DigestInfo DER sequences.
* @{ */
{/* { { 1.2.840.113549.2.2 (MD2), NULL }, hash octet-string } */
0x30,0x20, 0x30,0x0c, 0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x02, 0x05,0x00, 0x04,0x10
};
{/* { { 1.2.840.113549.2.4 (MD4), NULL }, hash octet-string } */
0x30,0x20, 0x30,0x0c, 0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x04, 0x05,0x00, 0x04,0x10
};
{/* { { 1.2.840.113549.2.5 (MD5), NULL }, hash octet-string } */
0x30,0x20, 0x30,0x0c, 0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05, 0x05,0x00, 0x04,0x10
};
{/* { { 1.3.14.3.2.26 (SHA-1), NULL }, hash octet-string } */
0x30,0x21, 0x30,0x09, 0x06,0x05,0x2b,0x0e,0x03,0x02,0x1a, 0x05,0x00, 0x04,0x14
};
static const uint8_t g_abSha256[] =
{/* { { 2.16.840.1.101.3.4.2.1 (SHA-256), NULL }, hash octet-string } */
0x30,0x31, 0x30,0x0d, 0x06,0x09,0x60,0x86,0x48,0x01,0x65,0x03,0x04,0x02,0x01, 0x05,0x00, 0x04,0x20
};
static const uint8_t g_abSha384[] =
{/* { { 2.16.840.1.101.3.4.2.2 (SHA-384), NULL }, hash octet-string } */
0x30,0x41, 0x30,0x0d, 0x06,0x09,0x60,0x86,0x48,0x01,0x65,0x03,0x04,0x02,0x02, 0x05,0x00, 0x04,0x30
};
static const uint8_t g_abSha512[] =
{/* { { 2.16.840.1.101.3.4.2.3 (SHA-512), NULL }, hash octet-string } */
0x30,0x51, 0x30,0x0d, 0x06,0x09,0x60,0x86,0x48,0x01,0x65,0x03,0x04,0x02,0x03, 0x05,0x00, 0x04,0x40
};
static const uint8_t g_abSha224[] =
{/* { { 2.16.840.1.101.3.4.2.4 (SHA-224), NULL }, hash octet-string } */
0x30,0x21, 0x30,0x0d, 0x06,0x09,0x60,0x86,0x48,0x01,0x65,0x03,0x04,0x02,0x04, 0x05,0x00, 0x04,0x14
};
/** @} */
/** Lookup array for the pre-encoded DigestInfo DER sequences. */
static struct
{
} const g_aDigestInfos[] =
{
};
/** @impl_interface_method{RTCRPKIXSIGNATUREDESC,pfnInit} */
static DECLCALLBACK(int) rtCrPkixSignatureRsa_Init(PCRTCRPKIXSIGNATUREDESC pDesc, void *pvState, void *pvOpaque,
{
if (pParams)
/*
* Decode the key and pick the bits we really need from it.
*/
RTAsn1CursorInitPrimary(&PrimaryCursor, RTASN1BITSTRING_GET_BIT0_PTR(pKey), RTASN1BITSTRING_GET_BYTE_SIZE(pKey),
int rc;
if (!fSigning)
{
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
return VINF_SUCCESS;
}
}
}
}
else
{
rc = RTCrRsaPrivateKey_DecodeAsn1(&PrimaryCursor.Cursor, 0, &pThis->Scratch.PrivateKey, "PrivateKey");
if (RT_SUCCESS(rc))
{
rc = RTAsn1Integer_ToBigNum(&pThis->Scratch.PrivateKey.Modulus, &pThis->Modulus, RTBIGNUMINIT_F_SENSITIVE);
if (RT_SUCCESS(rc))
{
rc = RTAsn1Integer_ToBigNum(&pThis->Scratch.PrivateKey.PublicExponent, &pThis->Exponent, RTBIGNUMINIT_F_SENSITIVE);
if (RT_SUCCESS(rc))
{
return VINF_SUCCESS;
}
}
}
}
return rc;
}
/** @impl_interface_method{RTCRPKIXSIGNATUREDESC,pfnReset} */
static DECLCALLBACK(int) rtCrPkixSignatureRsa_Reset(PCRTCRPKIXSIGNATUREDESC pDesc, void *pvState, bool fSigning)
{
return VINF_SUCCESS;
}
/** @impl_interface_method{RTCRPKIXSIGNATUREDESC,pfnDelete} */
static DECLCALLBACK(void) rtCrPkixSignatureRsa_Delete(PCRTCRPKIXSIGNATUREDESC pDesc, void *pvState, bool fSigning)
{
}
/**
* Common worker for rtCrPkixSignatureRsa_Verify and
* rtCrPkixSignatureRsa_Sign that encodes an EMSA-PKCS1-V1_5 signature in
* the scratch area.
*
* This function is referred to as EMSA-PKCS1-v1_5-ENCODE(M,k) in RFC-3447 and
* is described in section 9.2
*
* @returns IPRT status code.
* @param pThis The RSA signature provider instance.
* @param hDigest The digest which hash to turn into a signature.
* @param cbEncodedMsg The desired encoded message length.
*/
static int rtCrPkixSignatureRsa_EmsaPkcs1V15Encode(PRTCRPKIXSIGNATURERSA pThis, RTCRDIGEST hDigest, size_t cbEncodedMsg)
{
/*
* Figure out which hash and select the associate prebaked DigestInfo.
*/
AssertReturn(enmDigest != RTDIGESTTYPE_INVALID && enmDigest != RTDIGESTTYPE_UNKNOWN, VERR_CR_PKIX_UNKNOWN_DIGEST_TYPE);
size_t cbDigestInfoStart = 0;
{
break;
}
if (!pbDigestInfoStart)
return VERR_CR_PKIX_UNKNOWN_DIGEST_TYPE;
/*
* Get the hash size and verify that it matches what we've got in the
* precooked DigestInfo. ASSUMES less that 256 bytes of hash.
*/
/*
* Encode the message the first part of the scratch area.
*/
pbDst[0] = 0x00;
*pbDst++ = 0x00;
if (RT_FAILURE(rc))
return rc;
return VINF_SUCCESS;
}
/** @impl_interface_method{RTCRPKIXSIGNATUREDESC,pfnVerify} */
{
return VERR_CR_PKIX_SIGNATURE_TOO_LONG;
/*
* 8.2.2.1 - Length check.
*/
/*
* 8.2.2.2 - RSA verification / Decrypt the signature.
*/
/* a) s = OS2IP(S) -- Convert signature to integer. */
if (RT_FAILURE(rc))
return rc;
/* b) RSAVP1 - 5.2.2.2: Range check (0 <= s < n). */
{
{
/* b) RSAVP1 - 5.2.2.3: s^e mod n */
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
/* c) EM' = I2OSP(m, k) -- Convert the result to bytes. */
{
if (RT_SUCCESS(rc))
{
/*
* 8.2.2.3 - Build a hopefully identical signature using hDigest.
*/
if (RT_SUCCESS(rc))
{
/*
* 8.2.2.4 - Compare the two.
*/
rc = VINF_SUCCESS;
else
}
}
}
else
}
}
}
else
}
else
return rc;
}
/** @impl_interface_method{RTCRPKIXSIGNATUREDESC,pfnSign} */
{
return VERR_NOT_IMPLEMENTED;
}
/** RSA alias ODIs. */
static const char * const g_apszHashWithRsaAliases[] =
{
/* Note: Note quite sure about these OIW oddballs. */
"1.3.14.3.2.11" /* OIW rsaSignature */,
"1.3.14.3.2.14" /* OIW mdc2WithRSASignature */,
"1.3.14.3.2.15" /* OIW shaWithRSASignature */,
"1.3.14.3.2.24" /* OIW md2WithRSASignature */,
"1.3.14.3.2.25" /* OIW md5WithRSASignature */,
"1.3.14.3.2.29" /* OIW sha1WithRSASignature */,
};
/** RSA descriptor. */
{
"RSASSA-PKCS1-v1_5",
sizeof(RTCRPKIXSIGNATURERSA),
0,
0,
};