CryptImportPublicKeyInfo fails with CRYPT_E_ASN1_BADTAG

From: Shan (anonymous_at_discussions.microsoft.com)
Date: 02/11/04


Date: Wed, 11 Feb 2004 05:36:04 -0800

Hi,
I encode a public key in java (Using PublicKey.getEncoding() ) and transport that public key to another system running on Win32 thru sockets. I'm trying to decode this public key using CryptDecodeObjects (which succeeds and promptly gives me the right Alg Id), and then I'm trying to get a handle to the public key by using CryptImportPublicKeyInfo, which fails with CRYPT_E_ASN1_BADTAG. Any idea?
( Extra info: I'm using Bouncy Castle Crypto provider on java side with RSA algo. )
                HCRYPTPROV hCryptProv;
        if(! CryptAcquireContext(
                &hCryptProv, // Handle to the CSP
                NULL, // Container name
                NULL, // Use the default provider
                PROV_RSA_FULL, // Provider type
                0) ) // Flag values
        {
                printf("\nCryptAcquireContext failed: %lx", GetLastError() );
                closesocket(s);
                return 0;
        }

        if( CryptDecodeObject(
                X509_ASN_ENCODING,
                X509_PUBLIC_KEY_INFO,
                publicKeyEnc,
                n,
                0,
                NULL,
                &keySize) )
        {}

        pubKeyInfo.PublicKey.cbData = keySize;
        pubKeyInfo.PublicKey.pbData = (BYTE*) malloc(keySize);

        if( !CryptDecodeObject(
                X509_ASN_ENCODING,
                X509_PUBLIC_KEY_INFO,
                publicKeyEnc,
                n,
                0,
                (void *)&pubKeyInfo,
                &keySize) )
        {
                printf("\nCryptDecodeObject failed: %lx", GetLastError() );
                closesocket(s);
                return 0;
        }
        
        printf("\n Alg Id: %s", pubKeyInfo.Algorithm.pszObjId ); // Prints 1.2.840.113549.1.1.1

        HCRYPTKEY hKey;
        if( !CryptImportPublicKeyInfo(
                        hCryptProv,
                        X509_ASN_ENCODING,
                        &pubKeyInfo,
                        &hKey) )
        {
                printf("\CryptImportPublicKeyInfo failed: %lx", GetLastError() );
                closesocket(s);
                return 0;
        }


Loading