PEM encoder supports encoding from RSA components directly, allowing gcrypt plugin...
authorMartin Willi <martin@revosec.ch>
Thu, 29 Apr 2010 07:36:45 +0000 (09:36 +0200)
committerMartin Willi <martin@revosec.ch>
Thu, 29 Apr 2010 07:36:45 +0000 (09:36 +0200)
src/libstrongswan/plugins/gmp/gmp_rsa_private_key.c
src/libstrongswan/plugins/gmp/gmp_rsa_public_key.c
src/libstrongswan/plugins/pem/pem_encoder.c

index 0d7ec48..f70b0b5 100644 (file)
@@ -406,13 +406,7 @@ static bool get_encoding(private_gmp_rsa_private_key_t *this,
                                                 key_encoding_type_t type, chunk_t *encoding)
 {
        chunk_t n, e, d, p, q, exp1, exp2, coeff;
-       bool success, pem = FALSE;
-
-       if (type == KEY_PRIV_PEM)
-       {
-               pem = TRUE;
-               type = KEY_PRIV_ASN1_DER;
-       }
+       bool success;
 
        n = gmp_mpz_to_chunk(this->n);
        e = gmp_mpz_to_chunk(this->e);
@@ -438,15 +432,6 @@ static bool get_encoding(private_gmp_rsa_private_key_t *this,
        chunk_clear(&exp2);
        chunk_clear(&coeff);
 
-       if (pem && success)
-       {
-               chunk_t asn1_encoding = *encoding;
-
-               success = lib->encoding->encode(lib->encoding, KEY_PRIV_PEM, NULL,
-                                                                       encoding, KEY_PART_RSA_PRIV_ASN1_DER,
-                                                                       asn1_encoding, KEY_PART_END);
-               chunk_clear(&asn1_encoding);
-       }
        return success;
 }
 
index 93eef06..98dbb19 100644 (file)
@@ -399,13 +399,7 @@ static bool get_encoding(private_gmp_rsa_public_key_t *this,
                                                 key_encoding_type_t type, chunk_t *encoding)
 {
        chunk_t n, e;
-       bool success, pem = FALSE;
-
-       if (type == KEY_PUB_PEM)
-       {
-               pem = TRUE;
-               type = KEY_PUB_SPKI_ASN1_DER;
-       }
+       bool success;
 
        n = gmp_mpz_to_chunk(this->n);
        e = gmp_mpz_to_chunk(this->e);
@@ -415,15 +409,6 @@ static bool get_encoding(private_gmp_rsa_public_key_t *this,
        chunk_free(&n);
        chunk_free(&e);
 
-       if (pem && success)
-       {
-               chunk_t asn1_encoding = *encoding;
-
-               success = lib->encoding->encode(lib->encoding, KEY_PUB_PEM, NULL,
-                                                                       encoding, KEY_PART_RSA_PUB_ASN1_DER,
-                                                                       asn1_encoding, KEY_PART_END);
-               chunk_clear(&asn1_encoding);
-       }
        return success;
 }
 
index 48a2674..13c99a9 100644 (file)
@@ -27,26 +27,59 @@ bool pem_encoder_encode(key_encoding_type_t type, chunk_t *encoding,
        char *label;
        u_char *pos;
        size_t len, written, pem_chars, pem_lines;
+       chunk_t n, e, d, p, q, exp1, exp2, coeff, to_free = chunk_empty;
 
        switch (type)
        {
                case KEY_PUB_PEM:
+                       label ="PUBLIC KEY";
+                       /* direct PKCS#1 PEM encoding */
                        if (key_encoding_args(args, KEY_PART_RSA_PUB_ASN1_DER,
-                                                                  &asn1, KEY_PART_END) ||
+                                                                       &asn1, KEY_PART_END) ||
                                key_encoding_args(args, KEY_PART_ECDSA_PUB_ASN1_DER,
-                                                                  &asn1, KEY_PART_END))
+                                                                       &asn1, KEY_PART_END))
                        {
-                               label ="PUBLIC KEY";
                                break;
                        }
+                       /* indirect PEM encoding from components */
+                       if (key_encoding_args(args, KEY_PART_RSA_MODULUS, &n,
+                                                                       KEY_PART_RSA_PUB_EXP, &e, KEY_PART_END))
+                       {
+                               if (lib->encoding->encode(lib->encoding, KEY_PUB_SPKI_ASN1_DER,
+                                                                       NULL, &asn1, KEY_PART_RSA_MODULUS, n,
+                                                                       KEY_PART_RSA_PUB_EXP, e, KEY_PART_END))
+                               {
+                                       to_free = asn1;
+                                       break;
+                               }
+                       }
                        return FALSE;
                case KEY_PRIV_PEM:
+                       label ="RSA PRIVATE KEY";
+                       /* direct PKCS#1 PEM encoding */
                        if (key_encoding_args(args, KEY_PART_RSA_PRIV_ASN1_DER,
-                                                                  &asn1, KEY_PART_END))
+                                                                       &asn1, KEY_PART_END))
                        {
-                               label ="RSA PRIVATE KEY";
                                break;
                        }
+                       /* indirect PEM encoding from components */
+                       if (key_encoding_args(args, KEY_PART_RSA_MODULUS, &n,
+                                                       KEY_PART_RSA_PUB_EXP, &e, KEY_PART_RSA_PRIV_EXP, &d,
+                                                       KEY_PART_RSA_PRIME1, &p, KEY_PART_RSA_PRIME2, &q,
+                                                       KEY_PART_RSA_EXP1, &exp1, KEY_PART_RSA_EXP2, &exp2,
+                                                       KEY_PART_RSA_COEFF, &coeff, KEY_PART_END))
+                       {
+                               if (lib->encoding->encode(lib->encoding, KEY_PRIV_ASN1_DER, NULL,
+                                                       &asn1, KEY_PART_RSA_MODULUS, n,
+                                                       KEY_PART_RSA_PUB_EXP, e, KEY_PART_RSA_PRIV_EXP, d,
+                                                       KEY_PART_RSA_PRIME1, p, KEY_PART_RSA_PRIME2, q,
+                                                       KEY_PART_RSA_EXP1, exp1, KEY_PART_RSA_EXP2, exp2,
+                                                       KEY_PART_RSA_COEFF, coeff, KEY_PART_END))
+                               {
+                                       to_free = asn1;
+                                       break;
+                               }
+                       }
                        if (key_encoding_args(args, KEY_PART_ECDSA_PRIV_ASN1_DER,
                                                                   &asn1, KEY_PART_END))
                        {
@@ -86,6 +119,8 @@ bool pem_encoder_encode(key_encoding_type_t type, chunk_t *encoding,
                len--;
        }
 
+       chunk_clear(&to_free);
+
        /* write PEM trailer */
        written = snprintf(pos, len, "-----END %s-----", label);
        pos += written;