used rsa coeff field in OpenPGP secret key payload
[strongswan.git] / src / libstrongswan / plugins / pubkey / pubkey_public_key.c
index 1291b6e..6d3ae66 100644 (file)
  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  * for more details.
- *
- * $Id$
  */
 
 #include "pubkey_public_key.h"
 
 #include <debug.h>
+#include <asn1/pem.h>
 #include <asn1/oid.h>
 #include <asn1/asn1.h>
 #include <asn1/asn1_parser.h>
@@ -38,9 +37,10 @@ static const asn1Object_t pkinfoObjects[] = {
 
 
 /**
- * Load a public key from an ASN1 encoded blob
+ * Load a public key from an ASN.1 encoded blob
+ * Also used by pubkey_cert.c
  */
-static public_key_t *load(chunk_t blob)
+public_key_t *pubkey_public_key_load(chunk_t blob)
 {
        asn1_parser_t *parser;
        chunk_t object;
@@ -66,9 +66,8 @@ static public_key_t *load(chunk_t blob)
                                else if (oid == OID_EC_PUBLICKEY)
                                {
                                        /* we need the whole subjectPublicKeyInfo for EC public keys */
-                                       key = lib->creds->create(lib->creds,
-                                                               CRED_PUBLIC_KEY, KEY_ECDSA, BUILD_BLOB_ASN1_DER,
-                                                               chunk_clone(blob), BUILD_END);
+                                       key = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, 
+                                                               KEY_ECDSA, BUILD_BLOB_ASN1_DER, blob, BUILD_END);
                                        goto end;
                                }
                                else
@@ -85,8 +84,7 @@ static public_key_t *load(chunk_t blob)
                                        object = chunk_skip(object, 1);
                                }
                                key = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, type,
-                                                                                BUILD_BLOB_ASN1_DER, chunk_clone(object),
-                                                                                BUILD_END);
+                                                                                BUILD_BLOB_ASN1_DER, object, BUILD_END);
                                break;
                }
        } 
@@ -124,26 +122,43 @@ static public_key_t *build(private_builder_t *this)
  */
 static void add(private_builder_t *this, builder_part_t part, ...)
 {
-       va_list args;
-       
-       if (this->key)
-       {
-               DBG1("ignoring surplus build part %N", builder_part_names, part);
-               return;
-       }
-       switch (part)
+       if (!this->key)
        {
-               case BUILD_BLOB_ASN1_DER:
+               va_list args;
+               chunk_t blob;
+               
+               switch (part)
                {
-                       va_start(args, part);
-                       this->key = load(va_arg(args, chunk_t));
-                       va_end(args);
-                       break;
+                       case BUILD_BLOB_ASN1_DER:
+                       {
+                               va_start(args, part);
+                               blob = va_arg(args, chunk_t);
+                               this->key = pubkey_public_key_load(chunk_clone(blob));
+                               va_end(args);
+                               return;
+                       }
+                       case BUILD_BLOB_ASN1_PEM:
+                       {
+                               bool pgp;
+                               char *pem;
+                       
+                               va_start(args, part);
+                               pem = va_arg(args, char *);
+                               blob = chunk_clone(chunk_create(pem, strlen(pem)));
+                               if (pem_to_bin(&blob, chunk_empty, &pgp) == SUCCESS)
+                               {
+                                       this->key = pubkey_public_key_load(chunk_clone(blob));
+                               }
+                               free(blob.ptr);
+                               va_end(args);
+                               return;
+                       }
+                       default:
+                               break;
                }
-               default:
-                       DBG1("ignoring unsupported build part %N", builder_part_names, part);
-                       break;
        }
+       DESTROY_IF(this->key);
+       builder_cancel(&this->public);
 }
 
 /**