[Botan-devel] Import Private key

Jack Lloyd lloyd at randombit.net
Fri Sep 1 16:00:17 EDT 2006


Yes, you can load that format using Botan, it just takes a little
effort. Here is a simple implementation, there are certainly some
places that could be improved (handling both raw DER and PEM formats,
better error checking, it leaks memory if the load fails, etc), but it
works for expository purposes. Making it a function template and
giving it an extra argument for the label would let it support DSA and
DH keys as well (though I've never seen those in the wild not encoded
as PKCS #8, so that's probably just overengineering).

-Jack

RSA_PrivateKey* load_pkcs1_rsa(const std::string& file)
   {
   DataSource_Stream input(file);
   std::string pem_label;
   SecureVector<byte> key_data = PEM_Code::decode(input, pem_label);
   if(pem_label != "RSA PRIVATE KEY")
      return 0;

   DataSource_Memory key_source(key_data);

   // an "empty" RSA object
   PKCS8_PrivateKey* key = get_private_key("RSA");
   key->BER_decode_priv(key_source);
   return dynamic_cast<RSA_PrivateKey*>(key);
   }

On Thu, Aug 31, 2006 at 03:51:59PM +0200, Kunze, Marcus wrote:
> Hallo,
> i have not found a way to import a private in this format
> 
> -----BEGIN RSA PRIVATE KEY-----
> MGtba7g63pR0Jrrmzzldi7cNxZUekUl2s8G55PrusxoDlfqPLGzbCBrDtZUaMvvW
> ....
> MGtba7g63pR0Jrrmzzldi7cNxZUekUl2s8G55PrusxoDlfqPLGzbCBrDtZUaMvvW
> -----END RSA PRIVATE KEY-----
> 
> Can I import this key without convert with openssl to pkcs8?
> 
> 
> Marcus
> _______________________________________________
> botan-devel mailing list
> botan-devel at randombit.net
> http://www.randombit.net/mailman/listinfo/botan-devel


More information about the botan-devel mailing list