]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
Added functions to directly load a private key.
authorNikos Mavrogiannopoulos <nmav@gnutls.org>
Thu, 21 Jun 2012 20:09:16 +0000 (22:09 +0200)
committerNikos Mavrogiannopoulos <nmav@gnutls.org>
Thu, 21 Jun 2012 20:19:29 +0000 (22:19 +0200)
They allow loading a data buffer into a gnutls_privkey_t without
going through cumbersome convertions.

NEWS
doc/cha-cert-auth2.texi
lib/gnutls_privkey.c
lib/gnutls_ui.c
lib/includes/gnutls/abstract.h
lib/libgnutls.map
src/cli.c

diff --git a/NEWS b/NEWS
index 5977a21427c9d8fa787f7b331b6f56962c38bf3c..d505f3c72d8b726ee75a564222654c6ab7650751 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -40,6 +40,9 @@ by Alexandre Bique.
 
 ** API and ABI modifications:
 GNUTLS_CERT_SIGNATURE_FAILURE: Added
+gnutls_privkey_import_pkcs11_url: Added
+gnutls_privkey_import_openpgp_raw: Added
+gnutls_privkey_import_x509_raw: Added
 gnutls_load_file: Added
 gnutls_pubkey_verify_hash2: Added
 gnutls_pkcs12_simple_parse: Added
index 3a2e67cc4b904ee9fd67a83e701eabef0a602caf..efc44d76a07778b29d6faaa258755bf709bad417 100644 (file)
@@ -585,8 +585,10 @@ an existing structure like @code{gnutls_x509_crt_t},
 or through an ASN.1 encoding of the X.509 @code{SubjectPublicKeyInfo}
 sequence.
 
-@showfuncdesc{gnutls_pubkey_import_x509}
-@showfuncE{gnutls_pubkey_import_openpgp,gnutls_pubkey_import_pkcs11,gnutls_pubkey_import_pkcs11_url,gnutls_pubkey_import_privkey,gnutls_pubkey_import}
+@showfuncC{gnutls_pubkey_import_x509,gnutls_pubkey_import_openpgp,gnutls_pubkey_import_pkcs11}
+@showfuncC{gnutls_pubkey_import_pkcs11_url,gnutls_pubkey_import_privkey,gnutls_pubkey_import}
+
+@showfuncC{gnutls_pubkey_import_x509_raw,gnutls_pubkey_import_openpgp_raw,gnutls_pubkey_import_pkcs11_url}
 @showfuncdesc{gnutls_pubkey_export}
 
 Additional functions are available that will return
index 3b4446350b43bbfdc7d6e5459d0ac8560b9a799c..8a99ec7db64579a4519562c7b4b19d6973eef66b 100644 (file)
@@ -760,3 +760,175 @@ gnutls_privkey_decrypt_data (gnutls_privkey_t key,
       return GNUTLS_E_INVALID_REQUEST;
     }
 }
+
+/**
+ * gnutls_privkey_import_x509_raw:
+ * @pkey: The private key
+ * @data: The private key data to be imported
+ * @format: The format of the private key
+ * @password: A password (optional)
+ *
+ * This function will import the given private key to the abstract
+ * #gnutls_privkey_t structure. 
+ *
+ * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
+ *   negative error value.
+ *
+ * Since: 3.1
+ **/
+int gnutls_privkey_import_x509_raw (gnutls_privkey_t pkey,
+                                    const gnutls_datum_t * data,
+                                    gnutls_x509_crt_fmt_t format,
+                                    const char* password)
+{
+  gnutls_x509_privkey_t xpriv;
+  int ret;
+  
+  ret = gnutls_x509_privkey_init(&xpriv);
+  if (ret < 0)
+    return gnutls_assert_val(ret);
+
+  if (password == NULL)
+    {
+      ret = gnutls_x509_privkey_import(xpriv, data, format);
+      if (ret < 0)
+        {
+          gnutls_assert();
+          goto cleanup;
+        }
+    }
+  else
+    {
+      ret = gnutls_x509_privkey_import_pkcs8(xpriv, data, format, password, 0);
+      if (ret < 0)
+        {
+          gnutls_assert();
+          goto cleanup;
+        }
+    
+    }
+
+  ret = gnutls_privkey_import_x509(pkey, xpriv, GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE);
+  if (ret < 0)
+    {
+      gnutls_assert();
+      goto cleanup;
+    }
+
+  return 0;
+  
+cleanup:
+  gnutls_x509_privkey_deinit(xpriv);
+  
+  return ret;
+}
+
+/**
+ * gnutls_privkey_import_openpgp_raw:
+ * @pkey: The private key
+ * @data: The private key data to be imported
+ * @format: The format of the private key
+ * @keyid: The key id to use (optional)
+ * @password: A password (optional)
+ *
+ * This function will import the given private key to the abstract
+ * #gnutls_privkey_t structure. 
+ *
+ * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
+ *   negative error value.
+ *
+ * Since: 3.1
+ **/
+int gnutls_privkey_import_openpgp_raw (gnutls_privkey_t pkey,
+                                    const gnutls_datum_t * data,
+                                    gnutls_openpgp_crt_fmt_t format,
+                                    const gnutls_openpgp_keyid_t keyid,
+                                    const char* password)
+{
+  gnutls_openpgp_privkey_t xpriv;
+  int ret;
+  
+  ret = gnutls_openpgp_privkey_init(&xpriv);
+  if (ret < 0)
+    return gnutls_assert_val(ret);
+
+  ret = gnutls_openpgp_privkey_import(xpriv, data, format, password, 0);
+  if (ret < 0)
+    {
+      gnutls_assert();
+      goto cleanup;
+    }
+
+  if(keyid)
+    {
+      ret = gnutls_openpgp_privkey_set_preferred_key_id(xpriv, keyid);
+      if (ret < 0)
+        {
+          gnutls_assert();
+          goto cleanup;
+        }
+    }
+
+  ret = gnutls_privkey_import_openpgp(pkey, xpriv, GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE);
+  if (ret < 0)
+    {
+      gnutls_assert();
+      goto cleanup;
+    }
+    
+  ret = 0;
+  
+cleanup:
+  gnutls_openpgp_privkey_deinit(xpriv);
+  
+  return ret;
+}
+
+/**
+ * gnutls_privkey_import_pkcs11_url:
+ * @key: A key of type #gnutls_pubkey_t
+ * @url: A PKCS 11 url
+ * @flags: One of GNUTLS_PKCS11_OBJ_* flags
+ *
+ * This function will import a PKCS 11 certificate to a #gnutls_pubkey_t
+ * structure.
+ *
+ * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
+ *   negative error value.
+ *
+ * Since: 3.1
+ **/
+int
+gnutls_privkey_import_pkcs11_url (gnutls_privkey_t key, const char *url)
+{
+  gnutls_pkcs11_privkey_t pkey;
+  int ret;
+
+  ret = gnutls_pkcs11_privkey_init (&pkey);
+  if (ret < 0)
+    {
+      gnutls_assert ();
+      return ret;
+    }
+
+  ret = gnutls_pkcs11_privkey_import_url (pkey, url, 0);
+  if (ret < 0)
+    {
+      gnutls_assert ();
+      goto cleanup;
+    }
+
+  ret = gnutls_privkey_import_pkcs11 (key, pkey, GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE);
+  if (ret < 0)
+    {
+      gnutls_assert ();
+      goto cleanup;
+    }
+
+  return 0;
+
+cleanup:
+  gnutls_pkcs11_privkey_deinit (pkey);
+
+  return ret;
+}
index e254d228f4fc2538f85700af0f67195b42d6274d..6ff667afe624c2050469bea147aad614f5f9513b 100644 (file)
@@ -748,6 +748,7 @@ gnutls_anon_set_params_function (gnutls_anon_server_credentials_t res,
  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise
  *   an error code is returned.
  *
+ * Since 3.1
  **/
 int gnutls_load_file(const char* filename, gnutls_datum_t * data)
 {
index ad6624661333bceba24f9d0ce94953bda8520a81..26a3d140f5dc88154febc13a0dbf5b5f95fa16fc 100644 (file)
@@ -174,6 +174,20 @@ int gnutls_privkey_import_x509 (gnutls_privkey_t pkey,
 int gnutls_privkey_import_openpgp (gnutls_privkey_t pkey,
                                    gnutls_openpgp_privkey_t key,
                                    unsigned int flags);
+
+int gnutls_privkey_import_openpgp_raw (gnutls_privkey_t pkey,
+                                    const gnutls_datum_t * data,
+                                    gnutls_openpgp_crt_fmt_t format,
+                                    const gnutls_openpgp_keyid_t keyid,
+                                    const char* password);
+
+int gnutls_privkey_import_x509_raw (gnutls_privkey_t pkey,
+                                    const gnutls_datum_t * data,
+                                    gnutls_x509_crt_fmt_t format,
+                                    const char* password);
+
+int gnutls_privkey_import_pkcs11_url (gnutls_privkey_t key, const char *url);
+
 int
 gnutls_privkey_import_ext (gnutls_privkey_t pkey,
                            gnutls_pk_algorithm_t pk,
index eefae386117e5f1cd6e98b9a69d253232237a59f..c32fc0cd58348595f6bfb2b82605a19e2d9026f7 100644 (file)
@@ -798,6 +798,10 @@ GNUTLS_3_1_0 {
        gnutls_x509_trust_list_add_trust_file;
        gnutls_x509_trust_list_add_trust_mem;
        gnutls_pkcs12_simple_parse;
+       gnutls_privkey_import_openpgp_raw;
+       gnutls_privkey_import_x509_raw;
+       gnutls_privkey_import_pkcs11_url;
+       gnutls_load_file;
 } GNUTLS_3_0_0;
 
 GNUTLS_PRIVATE {
index 864a5379b7fb85dee64fd6efe7bdbfc13a513422..980c50527dfc4e87c933cadb17f2768aeb20ae16 100644 (file)
--- a/src/cli.c
+++ b/src/cli.c
@@ -103,28 +103,6 @@ static int do_handshake (socket_st * socket);
 static void init_global_tls_stuff (void);
 static int cert_verify_ocsp (gnutls_session_t session);
 
-/* Helper functions to load a certificate and key
- * files into memory.
- */
-static gnutls_datum_t
-load_file (const char *file)
-{
-  gnutls_datum_t loaded_file = { NULL, 0 };
-  size_t length;
-
-  loaded_file.data = (void*)read_binary_file (file, &length);
-  if (loaded_file.data)
-    loaded_file.size = (unsigned int) length;
-
-  return loaded_file;
-}
-
-static void
-unload_file (gnutls_datum_t* data)
-{
-  free (data->data);
-}
-
 #define MAX_CRT 6
 static unsigned int x509_crt_size;
 static gnutls_pcert_st x509_crt[MAX_CRT];
@@ -167,7 +145,6 @@ load_keys (void)
 #ifdef ENABLE_PKCS11
   gnutls_pkcs11_privkey_t pkcs11_key;
 #endif
-  gnutls_x509_privkey_t tmp_key;
   unsigned char keyid[GNUTLS_OPENPGP_KEYID_SIZE];
 
   if (x509_certfile != NULL && x509_keyfile != NULL)
@@ -197,8 +174,8 @@ load_keys (void)
 #endif /* ENABLE_PKCS11 */
         {
 
-          data = load_file (x509_certfile);
-          if (data.data == NULL)
+          ret = gnutls_load_file (x509_certfile, &data);
+          if (ret < 0)
             {
               fprintf (stderr, "*** Error loading cert file.\n");
               exit (1);
@@ -241,7 +218,7 @@ load_keys (void)
           gnutls_x509_crt_deinit(crt_list[i]);
         }
 
-      unload_file (&data);
+      gnutls_free (data.data);
 
       ret = gnutls_privkey_init(&x509_key);
       if (ret < 0)
@@ -254,18 +231,8 @@ load_keys (void)
 #ifdef ENABLE_PKCS11
       if (strncmp (x509_keyfile, "pkcs11:", 7) == 0)
         {
-          gnutls_pkcs11_privkey_init (&pkcs11_key);
-
           ret =
-            gnutls_pkcs11_privkey_import_url (pkcs11_key, x509_keyfile, 0);
-          if (ret < 0)
-            {
-              fprintf (stderr, "*** Error loading url: %s\n",
-                       gnutls_strerror (ret));
-              exit (1);
-            }
-
-          ret = gnutls_privkey_import_pkcs11( x509_key, pkcs11_key, GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE);
+            gnutls_privkey_import_pkcs11_url (x509_key, x509_keyfile);
           if (ret < 0)
             {
               fprintf (stderr, "*** Error loading url: %s\n",
@@ -276,25 +243,14 @@ load_keys (void)
       else
 #endif /* ENABLE_PKCS11 */
         {
-          data = load_file (x509_keyfile);
-          if (data.data == NULL)
-            {
-              fprintf (stderr, "*** Error loading key file.\n");
-              exit (1);
-            }
-
-          gnutls_x509_privkey_init (&tmp_key);
-
-          ret =
-            gnutls_x509_privkey_import (tmp_key, &data, x509ctype);
+          ret = gnutls_load_file (x509_keyfile, &data);
           if (ret < 0)
             {
-              fprintf (stderr, "*** Error loading key file: %s\n",
-                       gnutls_strerror (ret));
+              fprintf (stderr, "*** Error loading key file.\n");
               exit (1);
             }
 
-          ret = gnutls_privkey_import_x509( x509_key, tmp_key, GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE);
+          ret = gnutls_privkey_import_x509_raw( x509_key, &data, x509ctype, NULL);
           if (ret < 0)
             {
               fprintf (stderr, "*** Error loading url: %s\n",
@@ -302,7 +258,7 @@ load_keys (void)
               exit (1);
             }
 
-          unload_file (&data);
+          gnutls_free(data.data);
         }
 
       fprintf (stdout, "Processed %d client X.509 certificates...\n",
@@ -320,8 +276,8 @@ load_keys (void)
     {
       gnutls_openpgp_crt_t tmp_pgp_crt;
 
-      data = load_file (pgp_certfile);
-      if (data.data == NULL)
+      ret = gnutls_load_file (pgp_certfile, &data);
+      if (ret < 0)
         {
           fprintf (stderr, "*** Error loading PGP cert file.\n");
           exit (1);
@@ -339,7 +295,7 @@ load_keys (void)
           exit (1);
         }
  
-      unload_file (&data);
+      gnutls_free (data.data);
 
       ret = gnutls_privkey_init(&pgp_key);
       if (ret < 0)
@@ -373,43 +329,17 @@ load_keys (void)
       else
 #endif /* ENABLE_PKCS11 */
         {
-          gnutls_openpgp_privkey_t tmp_pgp_key;
-
-          data = load_file (pgp_keyfile);
-          if (data.data == NULL)
-            {
-              fprintf (stderr, "*** Error loading PGP key file.\n");
-              exit (1);
-            }
-
-          gnutls_openpgp_privkey_init (&tmp_pgp_key);
-
-          ret =
-            gnutls_openpgp_privkey_import (tmp_pgp_key, &data,
-                                           GNUTLS_OPENPGP_FMT_BASE64, NULL,
-                                           0);
+          ret = gnutls_load_file (pgp_keyfile, &data);
           if (ret < 0)
             {
-              fprintf (stderr,
-                       "*** Error loading PGP key file: %s\n",
-                       gnutls_strerror (ret));
+              fprintf (stderr, "*** Error loading key file.\n");
               exit (1);
             }
 
           if (HAVE_OPT(PGPSUBKEY))
-            {
-              ret =
-                gnutls_openpgp_privkey_set_preferred_key_id (tmp_pgp_key, keyid);
-              if (ret < 0)
-                {
-                  fprintf (stderr,
-                      "*** Error setting preferred sub key id (%s): %s\n",
-                      OPT_ARG(PGPSUBKEY), gnutls_strerror (ret));
-                  exit (1);
-                }
-            }
-
-          ret = gnutls_privkey_import_openpgp( pgp_key, tmp_pgp_key, GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE);
+            ret = gnutls_privkey_import_openpgp_raw( pgp_key, &data, x509ctype, keyid, NULL);
+          else
+            ret = gnutls_privkey_import_openpgp_raw( pgp_key, &data, x509ctype, NULL, NULL);
           if (ret < 0)
             {
               fprintf (stderr, "*** Error loading url: %s\n",
@@ -417,7 +347,7 @@ load_keys (void)
               exit (1);
             }
 
-          unload_file (&data);
+          gnutls_free(data.data);
         }