Getting CRYPT_ERROR_BADDATA on certImport

Ed Curren <[email protected]>
Newsgroups gmane.comp.encryption.cryptlib
Message-ID <[email protected]>
Hello all,
I am generating an RSA keypair, placing the public key in a certificate and self signing it then placing that in a key file along with the private key.  


When I export the public key as a base64 encoded certificate, and then try to import it back into cryptlib I am getting CRYPT_ERROR_BADDATA.


Below I have the code that I am using to create, export and import the keys and certs.

 

I would appreciate any help in diagonosing this.  I am at a loss

Thanks very much
Ed

 

 

/************************************************************************************************************************************/
*  The source code                                       *
/************************************************************************************************************************************/


int main(int argc, char *argv[])
{
 CCryptography::Instance().GenerateCryptographicKeyPair("keystore", 1024, "cryptKeys", "password1");
 ...
 string serverPublicKey = CCryptography::Instance().GetLocalPublicKey("keystore", "cryptKeys");
 CCryptography::Instance().AddServerKey("PublicKeys", serverPublicKey);
 ...
}

 

void CCryptography::GenerateCryptographicKeyPair(string keyStoreFileName, int keySize, string keySetName, string password)
{
   int result = 0;
   CRYPT_CONTEXT cryptContext;
   CRYPT_KEYSET cryptKeyset;
   CRYPT_CERTIFICATE cryptCert;

   result = cryptCreateContext(&cryptContext, CRYPT_UNUSED, CRYPT_ALGO_RSA);
   result = cryptSetAttributeString( cryptContext, CRYPT_CTXINFO_LABEL, keySetName.c_str(), keySetName.length() );
   result = cryptGenerateKey( cryptContext );

   result = cryptCreateCert(&cryptCert, CRYPT_UNUSED, CRYPT_CERTTYPE_CERTIFICATE);
   result = cryptSetAttribute(cryptCert, CRYPT_CERTINFO_XYZZY, 1);
   result = cryptSetAttribute(cryptCert, CRYPT_CERTINFO_SUBJECTPUBLICKEYINFO, cryptContext);
   result = cryptSetAttributeString(cryptCert, CRYPT_CERTINFO_COMMONNAME, CConfigData::Instance().GetServerName().c_str

              (),CConfigData::Instance().GetServerName().length());
   result = cryptSignCert(cryptCert, cryptContext);

   result = cryptKeysetOpen(&cryptKeyset, CRYPT_UNUSED, CRYPT_KEYSET_FILE, keyStoreFileName.c_str(), 

                                      CRYPT_KEYOPT_NONE);
   if( result == CRYPT_ERROR_NOTFOUND )
   {
      result = cryptKeysetOpen(&cryptKeyset, CRYPT_UNUSED, CRYPT_KEYSET_FILE, keyStoreFileName.c_str(),   

      CRYPT_KEYOPT_CREATE);
   }
   result = cryptAddPrivateKey(cryptKeyset, cryptContext, password.c_str());
   result = cryptAddPublicKey(cryptKeyset, cryptCert);

   result = cryptKeysetClose(cryptKeyset);
   result = cryptDestroyContext(cryptContext);
   result = cryptDestroyCert(cryptCert);
}

string CCryptography::GetLocalPublicKey(string keyStoreFileName, string keySetName)
{
   int result = 0;
   int certLength = 0;

   CRYPT_KEYSET cryptKeyset;
   CRYPT_CERTIFICATE cryptCert;

 

   result = cryptKeysetOpen(&cryptKeyset, CRYPT_UNUSED, CRYPT_KEYSET_FILE, keyStoreFileName.c_str(), 

                                      CRYPT_KEYOPT_NONE);
   result = cryptGetPublicKey(cryptKeyset, &cryptCert, CRYPT_KEYID_NAME, keySetName.c_str());
   result = cryptExportCert(NULL, 10240, &certLength, CRYPT_CERTFORMAT_TEXT_CERTIFICATE, cryptCert);

 

   char *buffer = new char[10240];
   memset(buffer, '\0', 10240);
   result = cryptExportCert(buffer, 10240, &certLength, CRYPT_CERTFORMAT_TEXT_CERTIFICATE, cryptCert);

 

   result = cryptDestroyCert(cryptCert);
   result = cryptKeysetClose(cryptKeyset);

 

   string keyCert(buffer);
   delete[] buffer;

   

    return keyCert;
}

 

void CCryptography::AddServerKey(string keyStoreFileName, string key)
{
   CRYPT_KEYSET cryptKeyset;
   CRYPT_CERTIFICATE cryptCertificate;

 

   int result = 0;
   result = cryptKeysetOpen(&cryptKeyset, CRYPT_UNUSED, CRYPT_KEYSET_ODBC, keyStoreFileName.c_str(), 

                                      CRYPT_KEYOPT_NONE);
   result = cryptImportCert(key.c_str(), key.length(), CRYPT_UNUSED, &cryptCertificate);  // <-- This function returns -32
   result = cryptAddPublicKey(cryptKeyset, cryptCertificate);
   result = cryptKeysetClose(cryptKeyset);
   result = cryptDestroyCert(cryptCertificate);
}

_______________________________________________
Cryptlib mailing list
[email protected] via Mail: [email protected]
Archive: ftp://ftp.franken.de/pub/crypt/cryptlib/archives/
http://news.gmane.org/gmane.comp.encryption.cryptlib
Posts from non-subscribed addresses are blocked to prevent spam, please
subscribe in order to post messages.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.