I am trying to get the RSA public key info in WinHTTP. So far I've got the certificate info in CERT_CONTEXT
structure. I can get encryption algorithm and others as follows:
PCCERT_CONTEXT cert;
DWORD certLen = sizeof(PCCERT_CONTEXT);
WinHttpQueryOption(hRequest, WINHTTP_OPTION_SERVER_CERT_CONTEXT, &cert, &certLen);
The encryption algorithm is got by
LPSTR pubKeyAlgo = cert->pCertInfo->SubjectPublicKeyInfo.Algorithm.pszObjId;
And we might get the public key as follows:
CRYPT_BIT_BLOB pubKey = cert->pCertInfo->SubjectPublicKeyInfo.PublicKey;
BYTE *p = pKey.pbData;
But as per the documentation, this is an encoded form:
PublicKey
BLOB containing an encoded public key.
So how to get the actual RSA public key parameters like modulus and exponent?
Here's an example of extracting the modulus and exponent of a RSA Public Key using Wincrypt. The example certificate is a SSH client cert from RFC 6187. The certificate can be used for SSH's
x509v3-rsa2048-sha256
signature encoding method. The OpenSSL conf file is at the end of the answer, but was adapted from How to create a self-signed certificate with OpenSSL.RSA_PUBLIC_KEY_XX
is used to cast the blob returned fromCryptDecodeObject
to the packed RSA structures the API returns. The name*_XX
was used to avoid colliding with a future Microsoft structure name.Compile the program using a Visual Studio Developer command prompt.
And running the program results in the following.
Here is a dump of the certificate using OpenSSL's
x509
subcommand.Here is the OpenSSL configuration file from How to create a self-signed certificate with OpenSSL tweaked for a SSH client.