Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to convert RSA public key XML format to PEM or ASCII format

3,173 views
Skip to first unread message

Panikulam Vivek

unread,
Sep 17, 2010, 9:19:05 PM9/17/10
to
Hi All 
 
I have a RSA public key provided in the below format and would like to know how to convert it into a format like PEM or any other format which can be read by openssl. I didnt find any conclusive solutions for this on www. Will the application which generated this key format be capable of generating the same key in PEM or ASCII format?
 
  <?xml version="1.0" encoding="UTF-8" ?>
- <RSAKeyValue>
  <Modulus>dhjffljkglejDHKJFHkjhhhhhSLWSKWLlkNKMNCKJBCKJFKJFBNCJKNLKNCLKMNDLKJSLKWJLJSjsSJJSDDDDDDDDDddddkjswlqqq</Modulus>
  <Exponent>AQAB</Exponent>
  </RSAKeyValue>


Regards
Vivek Panikulam
 

Mounir IDRASSI

unread,
Sep 17, 2010, 11:07:10 PM9/17/10
to
Hi,

To perform the conversion, use your favorite XML library to extract the
BASE64 values in the Modulus and Exponent nodes, then create an EVP_PKEY
structure from these using the functions I'm pasting below. From here,
call PEM_write_PUBKEY to create a PEM file that will contain your RSA
public key and that can be used later by OpenSSL.

<CODE>
unsigned char *fromBase64(const char* szInput, int* pLen)
{
BIO *b64, *bmem;
size_t length = strlen(szInput);
// The length of BASE64 representation is always bigger
// than the actual data length, so the size given to
// the malloc below is sufficient to hold all the
// decoded data
unsigned char *buffer = (unsigned char *)malloc(length);

b64 = BIO_new(BIO_f_base64());
// No LF on the input string
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
bmem = BIO_new_mem_buf((void*)szInput, length);
bmem = BIO_push(b64, bmem);

*pLen = BIO_read(bmem, buffer, length);
BIO_free_all(bmem);

return buffer;
}

BIGNUM* BN_fromBase64(const char* szBase64)
{
BIGNUM* bn = NULL;
int iLen;
unsigned char* pbData = fromBase64(szBase64, &iLen);
if (iLen)
{
bn = BN_bin2bn(pbData, iLen, NULL);
}
free(pbData);
return bn;
}

EVP_PKEY* RSA_fromBase64(const char* szModulus, const char* szExp)
{
BIGNUM *n = BN_fromBase64(szModulus);
BIGNUM *e = BN_fromBase64(szExp);

if (!n) printf("Invalid encoding for modulus\n");
if (!e) printf("Invalid encoding for public exponent\n");

if (e && n)
{
EVP_PKEY* pRsaKey = EVP_PKEY_new();
RSA* rsa = RSA_new();
rsa->e = e;
rsa->n = n;
EVP_PKEY_assign_RSA(pRsaKey, rsa);
return pRsaKey;
}
else
{
if (n) BN_free(n);
if (e) BN_free(e);
return NULL;
}
}
</CODE>

Cheers,
--
Mounir IDRASSI
IDRIX
http://www.idrix.fr


______________________________________________________________________
OpenSSL Project http://www.openssl.org
User Support Mailing List openss...@openssl.org
Automated List Manager majo...@openssl.org

Panikulam Vivek

unread,
Sep 18, 2010, 12:06:37 PM9/18/10
to
Hi
 
Thanks for your response. In which platform do I compile/execute the below CODE? I only have UNIX command line and Windows available.
 
Regards
Vivek Panikulam


From: Mounir IDRASSI <mounir....@idrix.net>
To: openss...@openssl.org
Sent: Fri, September 17, 2010 10:07:10 PM
Subject: Re: How to convert RSA public key XML format to PEM or ASCII format

Mounir IDRASSI

unread,
Sep 18, 2010, 1:12:55 PM9/18/10
to
Hi,

The code is a generic OpenSSL C source that will compile using the
favorite compiler of your platform.
Did I understand your question correctly?

Mounir IDRASSI

unread,
Sep 18, 2010, 1:08:40 PM9/18/10
to
Hi,

The code is a generic OpenSSL C source that will compile using the
favorite compiler of your platform.
Did I understand your question correctly?

--
Mounir IDRASSI
IDRIX
http://www.idrix.fr

> Hi
>
> Thanks for your response. In which lplatform do I compile/execute the below

0 new messages