Search code examples
c++winapicode-signingdigital-certificate

How to convert OID of a code-signing algorithm from CRYPT_ALGORITHM_IDENTIFIER to a human readable string?


When I'm retrieving a code signing signature from an executable file on Windows, the CERT_CONTEXT of the certificate points to the CERT_INFO, that has CRYPT_ALGORITHM_IDENTIFIER SignatureAlgorithm member that contains the algorithm used for signing.

How do I convert that to a human readable form as such?

enter image description here

For instance, SignatureAlgorithm.pszObjId may be set to "1.2.840.113549.1.1.11" string, which is szOID_RSA_SHA256RSA according to this long list. I guess I can make a very long switch statement for it, and link it to "sha256", but I'd rather avoid it since I don't know what most of those values are. Is there an API that can do all that for me?


Solution

  • Use CryptFindOIDInfo to get information about a OID including the display name and the CNG algorithm identifier string:

    void PrintSigAlgoName(CRYPT_ALGORITHM_IDENTIFIER* pSigAlgo)
    {
        if(pSigAlgo && pSigAlgo->pszObjId)
        {
            PCCRYPT_OID_INFO pCOI = CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY, pSigAlgo->pszObjId, 0);
            if(pCOI && pCOI->pwszName)
            {
                _tprintf(_T("%ls"), pCOI->pwszName);
            }
            else
            {
                _tprintf(_T("%hs"), pSigAlgo->pszObjId);
            }
        }
    }