John Rainey
John Rainey

Reputation: 3

How to Decrypt Public Key using CryptDecodeObjectEx function

I am having an issue with the CryptDecodeObjectEx function on a X509 certificate, using the RSA algorithm "1.2.840.113549.1.1.1". I retrieve 276 BYTEs and convert it to a string of chars, of hexadecimal digits separated by spaces. i.e. ...5d a4 7b f6 4a 35...

The result is 6 BYTEs longer (18 chars) than the public key displayed in cert manager, and the digits do not resemble the public key displayed in cert manager.

BOOL ByteToStr(DWORD cb, void* pv, LPSTR sz,int* size_sz,BOOL addSpaces,BOOL upperCaseHexDigits)

//-------------------------------------------------------------------
// Parameters passed are:
//    pv -- the Array of BYTES to be converted.
//    cb -- the number of BYTEs in the array.
//    sz -- a pointer to the string to be returned.

{
  //-------------------------------------------------------------------
  //  Declare and initialize local variables.

  BYTE* pb = (BYTE*)pv;  // local pointer to a BYTE in the BYTE array
  DWORD i;               // local loop counter
  int b;                 // local variable
  int outCharCount = 0;

  //LPSTR psz = (char*)malloc((cb * 3 + 1) * sizeof(char));

  //  Ensure that sz is large enough to hold pv.
  if (strlen(sz) < cb) {
    MyHandleError(L"The array of bytes is too long for the "
                  "allocated string.");
  }

  //-------------------------------------------------------------------
  //  Begin processing loop.

  for (i = 0; i < cb; i++) {
    b = (*pb & 0xF0) >> 4;
    if (upperCaseHexDigits == TRUE)
    {
      *sz++ = (b <= 9) ? b + '0' : (b - 10) + 'A';
    }
    else
    {
      *sz++ = (b <= 9) ? b + '0' : (b - 10) + 'a';
    }
    
    outCharCount++;
    //outCharCount = outCharCount+((b <= 9) ? 1 : 2);
    b = *pb & 0x0F;
    if (upperCaseHexDigits == TRUE)
    {
      *sz++ = (b <= 9) ? b + '0' : (b - 10) + 'A';
    }
    else
    {
      *sz++ = (b <= 9) ? b + '0' : (b - 10) + 'a';
    }
    
    outCharCount++;
    //outCharCount = outCharCount + ((b <= 9) ? 1 : 2);
    pb++;
    if (addSpaces)
    {
      *sz++ = ' ';
      outCharCount++;
    }
  }
  *sz++ = 0;
  outCharCount++;
  *size_sz = outCharCount;

  return TRUE;
}  // end of ByteToStr

// Decrypt the public key
publicKeyInfo = pCertContext->pCertInfo->SubjectPublicKeyInfo;

PBYTE pbPKEY = NULL;
//PBYTE pbPKEYdata = NULL;
DWORD iPKEYSize=0;

CryptDecodeObjectEx(
    X509_ASN_ENCODING  ,//(PKCS_7_ASN_ENCODING | X509_ASN_ENCODING),
    RSA_CSP_PUBLICKEYBLOB,
    pCertContext->pCertInfo->SubjectPublicKeyInfo.PublicKey.pbData,
    pCertContext->pCertInfo->SubjectPublicKeyInfo.PublicKey.cbData,
    CRYPT_ENCODE_ALLOC_FLAG,//The called decoding function allocates memory for the decoded structure. A pointer to the allocated structure is returned in pvStructInfo.
    NULL,//If CRYPT_ENCODE_ALLOC_FLAG then pDecodePara is set to NULL, then LocalAlloc and LocalFree are used to allocate and free memory
    &pbPKEY, //This parameter can be NULL to retrieve the size of this information for memory allocation purposes
    &iPKEYSize);


  LPSTR sz = (char*)malloc((iPKEYSize * 3 + 1) * sizeof(char));
  int size_sz = 0;
  BOOL succes=ByteToStr(iPKEYSize, pbPKEY, sz, &size_sz, TRUE, FALSE);  

  free(sz);
  LocalFree((HANDLE)pbPKEY);

Upvotes: 0

Views: 123

Answers (0)

Related Questions