Skip to content

Commit

Permalink
Shim builds direct, RSA key generation uses EVP
Browse files Browse the repository at this point in the history
  • Loading branch information
bartonjs committed Jan 12, 2021
1 parent d84755a commit 459d274
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,20 @@ private static extern int RsaVerificationPrimitive(
internal static extern int RsaGenerateKeyEx(SafeRsaHandle rsa, int bits, SafeBignumHandle e);

[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_RsaGenerateKey")]
internal static extern SafeEvpPKeyHandle RsaGenerateKey(int keySize);
private static extern SafeEvpPKeyHandle CryptoNative_RsaGenerateKey(int keySize);

internal static SafeEvpPKeyHandle RsaGenerateKey(int keySize)
{
SafeEvpPKeyHandle pkey = CryptoNative_RsaGenerateKey(keySize);

if (pkey.IsInvalid)
{
pkey.Dispose();
throw CreateOpenSslCryptographicException();
}

return pkey;
}

internal static bool RsaSign(int type, ReadOnlySpan<byte> m, int m_len, Span<byte> sigret, out int siglen, SafeRsaHandle rsa) =>
RsaSign(type, ref MemoryMarshal.GetReference(m), m_len, ref MemoryMarshal.GetReference(sigret), out siglen, rsa);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ static const Entry s_cryptoNative[] =
DllImportEntry(CryptoNative_RecursiveFreeX509Stack)
DllImportEntry(CryptoNative_RsaCreate)
DllImportEntry(CryptoNative_RsaDestroy)
DllImportEntry(CryptoNative_RsaGenerateKey)
DllImportEntry(CryptoNative_RsaGenerateKeyEx)
DllImportEntry(CryptoNative_RsaPrivateDecrypt)
DllImportEntry(CryptoNative_RsaPublicEncrypt)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,31 @@ int32_t CryptoNative_RsaGenerateKeyEx(RSA* rsa, int32_t bits, BIGNUM* e)
return RSA_generate_key_ex(rsa, bits, e, NULL);
}

EVP_PKEY* CryptoNative_RsaGenerateKey(int32_t keySize)
{
EVP_PKEY_CTX* ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);

if (ctx == NULL)
{
return NULL;
}

EVP_PKEY* pkey = NULL;
int success = 1;
success = success && (1 == EVP_PKEY_keygen_init(ctx));
success = success && (1 == EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, keySize));
success = success && (1 == EVP_PKEY_keygen(ctx, &pkey));

if (pkey != NULL && !success)
{
EVP_PKEY_free(pkey);
pkey = NULL;
}

EVP_PKEY_CTX_free(ctx);
return pkey;
}

int32_t
CryptoNative_RsaSign(int32_t type, const uint8_t* m, int32_t mlen, uint8_t* sigret, int32_t* siglen, RSA* rsa)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ Returns 1 upon success, otherwise 0.
*/
PALEXPORT int32_t CryptoNative_RsaGenerateKeyEx(RSA* rsa, int32_t bits, BIGNUM* e);

PALEXPORT EVP_PKEY* CryptoNative_RsaGenerateKey(int32_t keySize);

/*
Shims the RSA_sign method.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ c_static_assert(PAL_X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY == X509_V_ERR_U
c_static_assert(PAL_X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE);
c_static_assert(PAL_X509_V_ERR_CERT_CHAIN_TOO_LONG == X509_V_ERR_CERT_CHAIN_TOO_LONG);
c_static_assert(PAL_X509_V_ERR_CERT_REVOKED == X509_V_ERR_CERT_REVOKED);
c_static_assert(PAL_X509_V_ERR_INVALID_CA == X509_V_ERR_INVALID_CA);
//c_static_assert(PAL_X509_V_ERR_INVALID_CA == X509_V_ERR_INVALID_CA);
c_static_assert(PAL_X509_V_ERR_PATH_LENGTH_EXCEEDED == X509_V_ERR_PATH_LENGTH_EXCEEDED);
c_static_assert(PAL_X509_V_ERR_INVALID_PURPOSE == X509_V_ERR_INVALID_PURPOSE);
c_static_assert(PAL_X509_V_ERR_CERT_UNTRUSTED == X509_V_ERR_CERT_UNTRUSTED);
Expand Down Expand Up @@ -1115,7 +1115,10 @@ CryptoNative_X509ChainVerifyOcsp(X509_STORE_CTX* storeCtx, OCSP_REQUEST* req, OC

if (bio != NULL)
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wcast-qual"
if (i2d_OCSP_RESPONSE_bio(bio, resp))
#pragma clang diagnostic pop
{
clearErr = 0;
}
Expand Down

0 comments on commit 459d274

Please sign in to comment.