0

I need to find the Public key specified in certificate details. I have used WinHttpQuery option and provided WINHTTP_OPTION_SERVER_CERT_CONTEXT as the option flag.

bRet = WinHttpQueryOption(
hRequest,
WINHTTP_OPTION_SERVER_CERT_CONTEXT,
&pCert,
&dwLen

);

I found the public key encryption type using the structure returned from WinhttpQueryOption. Now I need to find the size of the public key

Example : RSA(2048 bits)

Is there a way to find the size of the public key using this method or is there any other way?

Sample of certificate details

Keshav
  • 47
  • 1
  • 7
  • I think, `pCert` variable contains a pointer to a `CERT_CONTEXT` structure. You can go through this object (there is `pCertInfo` field which contains a pointer to a `CERT_INFO` structure). Use this to narrow down to `SubjectPublicKeyInfo`. – Crypt32 Jan 08 '18 at 08:22
  • Yeah I did that and thats how I found the encryption algorithm. I can get the algorithm name and the public key (in encoded form). Now how do I get the size of the key? Note : Public key is of CRYPT_BIT_BLOB type – Keshav Jan 08 '18 at 09:05
  • The size of the public key is clearly stated as 256 bits. Don't post pictures of text here, and especially not links to pictures of text. Unclear what you're asking. – user207421 Jan 08 '18 at 09:09
  • Umm, I posted it so that I can be clear in what I want from certificate details. Thats why I mentioned it as **Sample of certificate details** . – Keshav Jan 08 '18 at 09:17

1 Answers1

1

After hours of searching, I finally came up with the solution.

With WinHttpQueryOption, use WINHTTP_OPTION_SERVER_CERT_CONTEXT as the option flag and get the structure pCert(CERT_CONTEXT) . Now get the PCERT_INFO member of the structure to get details about the certificate. In PCERT_INFO use the SubjectPublicKeyInfo member, use the the function CertGetPublicKeyLength() and pass the SubjectPublicKeyInfo member as an argument to it. That function returns the length of the public key.

Code :

bRet = WinHttpQueryOption(
hRequest,
WINHTTP_OPTION_SERVER_CERT_CONTEXT,
&pCert,
&dwLen
);

cout<<"Alg Name : "<<pCert->pCertInfo-
>SubjectPublicKeyInfo.Algorithm.pszObjId<<endl;
CRYPT_BIT_BLOB pubKey = pCert->pCertInfo->SubjectPublicKeyInfo.PublicKey;

DWORD pLength = CertGetPublicKeyLength(X509_ASN_ENCODING | 
PKCS_7_ASN_ENCODING,&pCert->pCertInfo->SubjectPublicKeyInfo);
cout<<"Length of public key : "<<pLength<<endl;
Keshav
  • 47
  • 1
  • 7