]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
Added gnutls_pubkey_import_privkey(), that will copy the public key from a gnutls_pri...
authorNikos Mavrogiannopoulos <nmav@gnutls.org>
Thu, 16 Dec 2010 16:06:45 +0000 (17:06 +0100)
committerNikos Mavrogiannopoulos <nmav@gnutls.org>
Thu, 16 Dec 2010 16:06:45 +0000 (17:06 +0100)
lib/abstract_int.h [new file with mode: 0644]
lib/gnutls_privkey.c
lib/gnutls_pubkey.c
lib/includes/gnutls/abstract.h
lib/libgnutls.map

diff --git a/lib/abstract_int.h b/lib/abstract_int.h
new file mode 100644 (file)
index 0000000..70fc880
--- /dev/null
@@ -0,0 +1,9 @@
+#ifndef _ABSTRACT_INT_H
+# define _ABSTRACT_INT_H
+
+#include <gnutls/abstract.h>
+
+int _gnutls_privkey_get_public_mpis (gnutls_privkey_t key, 
+  bigint_t * params, int *params_size);
+  
+#endif
index 86e45edd436eb221cf953d3fb1c2670f5c95fb4c..4dbd85f99d6358cbcfeffc309c8a656e042ec6d5 100644 (file)
@@ -35,6 +35,7 @@
 #include <openpgp/openpgp_int.h>
 #include <openpgp/gnutls_openpgp.h>
 #include <gnutls_sig.h>
+#include <abstract_int.h>
 
 struct gnutls_privkey_st
 {
@@ -103,6 +104,95 @@ gnutls_privkey_get_pk_algorithm (gnutls_privkey_t key, unsigned int *bits)
 
 }
 
+static int privkey_to_pubkey(gnutls_pk_algorithm_t pk, 
+  const bigint_t * params, int params_size,
+  bigint_t *new_params, int* new_params_size)
+{
+  int ret, i;
+
+  switch(pk) {
+    case GNUTLS_PK_RSA:
+      if (*new_params_size < RSA_PUBLIC_PARAMS || params_size < RSA_PRIVATE_PARAMS)
+        {
+          gnutls_assert ();
+          return GNUTLS_E_INVALID_REQUEST;
+        }
+      new_params[0] = _gnutls_mpi_copy(params[0]);
+      new_params[1] = _gnutls_mpi_copy(params[1]);
+      
+      *new_params_size = RSA_PUBLIC_PARAMS;
+      
+      if (new_params[0] == NULL || new_params[1] == NULL)
+        {
+          gnutls_assert();
+          ret = GNUTLS_E_MEMORY_ERROR;
+          goto cleanup;
+        }
+        
+      break;
+    case GNUTLS_PK_DSA:
+      if (*new_params_size < DSA_PUBLIC_PARAMS || params_size < DSA_PRIVATE_PARAMS)
+        {
+          gnutls_assert ();
+          return GNUTLS_E_INVALID_REQUEST;
+        }
+      new_params[0] = _gnutls_mpi_copy(params[0]);
+      new_params[1] = _gnutls_mpi_copy(params[1]);
+      new_params[2] = _gnutls_mpi_copy(params[2]);
+      new_params[3] = _gnutls_mpi_copy(params[3]);
+
+      *new_params_size = DSA_PUBLIC_PARAMS;
+      
+      if (new_params[0] == NULL || new_params[1] == NULL || 
+        new_params[2] == NULL || new_params[3] == NULL)
+        {
+          gnutls_assert();
+          ret = GNUTLS_E_MEMORY_ERROR;
+          goto cleanup;
+        }
+        
+      break;
+    default:
+      gnutls_assert();
+      return GNUTLS_E_INVALID_REQUEST;
+  }
+  
+  return 0;
+cleanup:
+  for (i=0;i<*new_params_size;i++)
+    _gnutls_mpi_release(new_params[i]);
+  return ret;
+}
+  
+
+/* Returns the public key of the private key (if possible)
+ */
+int _gnutls_privkey_get_public_mpis (gnutls_privkey_t key, 
+  bigint_t * params, int *params_size)
+{
+  int ret;
+  
+  switch (key->type)
+    {
+    case GNUTLS_PRIVKEY_X509:
+      ret = privkey_to_pubkey( gnutls_privkey_get_pk_algorithm(key, NULL),
+        key->key.x509->params, key->key.x509->params_size, 
+        params, params_size);
+      if (ret < 0)
+        {
+          gnutls_assert();
+          return ret;
+        }
+    default:
+      gnutls_assert ();
+      return GNUTLS_E_INVALID_REQUEST;
+    }
+  
+  return 0;
+}
+
 /**
  * gnutls_privkey_init:
  * @key: The structure to be initialized
index 7169aecb80481d70456bd7d39dc77fe1b800e852..113d5a0da0a29b0539b85e810461e30255b730f3 100644 (file)
@@ -37,6 +37,7 @@
 #include <gnutls_num.h>
 #include <x509/common.h>
 #include <x509_b64.h>
+#include <abstract_int.h>
 
 #define PK_PEM_HEADER "PUBLIC KEY"
 
@@ -187,6 +188,33 @@ gnutls_pubkey_import_x509 (gnutls_pubkey_t key, gnutls_x509_crt_t crt,
   return 0;
 }
 
+/**
+ * gnutls_pubkey_import_privkey:
+ * @key: The public key
+ * @pkey: The private key
+ * @usage: GNUTLS_KEY_* key usage flags.
+ * @flags: should be zero
+ *
+ * This function will import the given public key to the abstract
+ * #gnutls_pubkey_t structure.
+ *
+ * Returns: On success, %GNUTLS_E_SUCCESS is returned, otherwise a
+ *   negative error value.
+ **/
+int
+gnutls_pubkey_import_privkey (gnutls_pubkey_t key, gnutls_privkey_t pkey,
+                          unsigned int usage, unsigned int flags)
+{
+  key->pk_algorithm = gnutls_privkey_get_pk_algorithm (pkey, &key->bits);
+
+  key->key_usage = usage;
+
+  key->params_size = sizeof (key->params) / sizeof (key->params[0]);
+
+  return _gnutls_privkey_get_public_mpis (pkey, key->params,
+    &key->params_size);
+}
+
 /**
  * gnutls_pubkey_get_preferred_hash_algorithm:
  * @key: Holds the certificate
index 2e68632b23ea235939b95449e280d3e30a5c92d8..5fcbc3cd894d3315d2f883c91167b56e038e5ae2 100644 (file)
@@ -12,6 +12,9 @@
 struct gnutls_pubkey_st;
 typedef struct gnutls_pubkey_st *gnutls_pubkey_t;
 
+struct gnutls_privkey_st;
+typedef struct gnutls_privkey_st *gnutls_privkey_t;
+
 int gnutls_pubkey_init (gnutls_pubkey_t * key);
 void gnutls_pubkey_deinit (gnutls_pubkey_t key);
 int gnutls_pubkey_get_pk_algorithm (gnutls_pubkey_t key, unsigned int *bits);
@@ -24,6 +27,10 @@ int gnutls_pubkey_import_openpgp (gnutls_pubkey_t pkey,
                                  gnutls_openpgp_crt_t crt,
                                  gnutls_openpgp_keyid_t keyid,
                                  unsigned int flags);
+int
+gnutls_pubkey_import_privkey (gnutls_pubkey_t key, gnutls_privkey_t pkey,
+                          unsigned int usage, unsigned int flags);
+
 
 int gnutls_pubkey_get_preferred_hash_algorithm (gnutls_pubkey_t key,
                                                gnutls_digest_algorithm_t *
@@ -78,9 +85,6 @@ gnutls_pubkey_get_verify_algorithm (gnutls_pubkey_t key,
 
 /* Private key operations */
 
-struct gnutls_privkey_st;
-typedef struct gnutls_privkey_st *gnutls_privkey_t;
-
 int gnutls_privkey_init (gnutls_privkey_t * key);
 void gnutls_privkey_deinit (gnutls_privkey_t key);
 int gnutls_privkey_get_pk_algorithm (gnutls_privkey_t key,
index 908da1585bebb438a4497a2ed47290da712e5e81..4ad0ab32b49cde16871781ed668a5b26cf24609d 100644 (file)
@@ -698,6 +698,7 @@ GNUTLS_2_12
        gnutls_openpgp_crt_verify_hash;
        gnutls_x509_privkey_sign_data2;
        gnutls_pkcs11_privkey_sign_hash2;
+       gnutls_pubkey_import_privkey;
 } GNUTLS_2_10;
 
 GNUTLS_PRIVATE {