openssl: Add generic RSA public key encoding
authorTobias Brunner <tobias@strongswan.org>
Wed, 14 Aug 2013 16:23:00 +0000 (18:23 +0200)
committerTobias Brunner <tobias@strongswan.org>
Fri, 13 Sep 2013 13:23:49 +0000 (15:23 +0200)
src/libstrongswan/plugins/openssl/openssl_rsa_public_key.c

index 48beede..f0c1726 100644 (file)
@@ -19,6 +19,7 @@
 #ifndef OPENSSL_NO_RSA
 
 #include "openssl_rsa_public_key.h"
+#include "openssl_util.h"
 
 #include <utils/debug.h>
 
@@ -248,6 +249,7 @@ METHOD(public_key_t, get_encoding, bool,
        private_openssl_rsa_public_key_t *this, cred_encoding_type_t type,
        chunk_t *encoding)
 {
+       bool success = FALSE;
        u_char *p;
 
        switch (type)
@@ -255,11 +257,10 @@ METHOD(public_key_t, get_encoding, bool,
                case PUBKEY_SPKI_ASN1_DER:
                case PUBKEY_PEM:
                {
-                       bool success = TRUE;
-
                        *encoding = chunk_alloc(i2d_RSA_PUBKEY(this->rsa, NULL));
                        p = encoding->ptr;
                        i2d_RSA_PUBKEY(this->rsa, &p);
+                       success = TRUE;
 
                        if (type == PUBKEY_PEM)
                        {
@@ -280,7 +281,20 @@ METHOD(public_key_t, get_encoding, bool,
                        return TRUE;
                }
                default:
-                       return FALSE;
+               {
+                       chunk_t n = chunk_empty, e = chunk_empty;
+
+                       if (openssl_bn2chunk(this->rsa->n, &n) &&
+                               openssl_bn2chunk(this->rsa->e, &e))
+                       {
+                               success = lib->encoding->encode(lib->encoding, type, NULL,
+                                                                       encoding, CRED_PART_RSA_MODULUS, n,
+                                                                       CRED_PART_RSA_PUB_EXP, e, CRED_PART_END);
+                       }
+                       chunk_free(&n);
+                       chunk_free(&e);
+                       return success;
+               }
        }
 }