Subject: bug#50507: New function in Emacs GnuTLS implementation

From: Nikolaos Chatzikonstantinou <[email-masked]>
Date: Fri, 30 Sep 2022 06:04:30 -0400

On Thu, Sep 29, 2022 at 10:08 AM Robert Pluim <[email-masked]> wrote:
>
> >>>>> On Thu, 29 Sep 2022 09:44:09 -0400, Nikolaos Chatzikonstantinou <[email-masked]> said:
>     Nikolaos> +The :pass and :flags keys are ignored with old versions of GnuTLS, and
>     Nikolaos> +:flags is ignored if :pass is not specified.
>     Nikolaos> +
>     >>
>     >> Maybe mention that not specifying :flags or passing :flags nil means
>     >> passing '0' to the GnuTLS function?
>
>     Nikolaos> Yes, and on that note, I discovered two things. One, the value 0 is
>     Nikolaos> special; it has meaning but it is not an enumeration constant. I
>     Nikolaos> documented this appropriately. Two, the password may be NULL instead
>     Nikolaos> of a string.
>
> OK. I guess youʼre mapping ':pass nil' to that?

Yes.

>     Nikolaos> +  DEFSYM (Qgnutls_pkcs_plain, "GNUTLS_PKCS_PLAIN");
>     Nikolaos> <removed a few more such lines>
>     Nikolaos> +  DEFSYM (Qgnutls_pkcs_pbes2_gost_cpd, "GNUTLS_PKCS_PBES2_GOST_CPD");
>     >>
>     >> All this is kind of awkward, but apart from doing DEFVAR_LISP Iʼm not
>     >> aware of how to define a lisp level symbol with a value (it would
>     >> allow you to simplify `key_file2_aux', since you could just extract
>     >> the values directly from the symbols).
>
>     Nikolaos> I am now comparing against intern("GNUTLS_PKCS_PLAIN") and so on.
>
> I guess thatʼs another option, but itʼs not the preferred
> solution. Anyway, letʼs not let the perfect be the enemy of the good.

I went with intern. There were some additional #if checks to avoid
dynamically loading the symbol on library Windows if it is not
available. I used plist_member() to differentiate between `:pass nil`
and not specifying `:pass`, and I documented this in the docstrings.

Regards,
Nikolaos Chatzikonstantinou
From 3100c17f8455a3894ca27c9872548daa1a1fb905 Mon Sep 17 00:00:00 2001
From: Nikolaos Chatzikonstantinou <[email-masked]>
Date: Mon, 26 Sep 2022 11:08:18 -0400
Subject: [PATCH] add :pass and :flags to gnutls-boot for :keylist

* lisp/net/gnutls.el (gnutls-boot-parameters): Add the keys :pass and
:flags, and update the documentation.
* src/gnutls.c (gnutls-boot): Add the keys :pass and :flags, and
update the documentation.
(syms_of_gnutls): Add the symbols :pass, :flags, and the symbols that
correspond to the enumeration constants of the GnuTLS enum
`gnutls_pkcs_encrypt_flags_t`.
(key_file2_aux): Private helper function that translates a list of
symbols to its corresponding `unsigned int` value of the GnuTLS C enum
`gnutls_pkcs_encrypt_flags_t`.
---
 lisp/net/gnutls.el |  10 +++++
 src/gnutls.c       | 105 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 115 insertions(+)

diff --git a/lisp/net/gnutls.el b/lisp/net/gnutls.el
index 6e3845aec1..eef6559a95 100644
--- a/lisp/net/gnutls.el
+++ b/lisp/net/gnutls.el
@@ -265,6 +265,7 @@ gnutls-boot-parameters
            &key type hostname priority-string
            trustfiles crlfiles keylist min-prime-bits
            verify-flags verify-error verify-hostname-error
+           pass flags
            &allow-other-keys)
   "Return a keyword list of parameters suitable for passing to `gnutls-boot'.
 
@@ -281,6 +282,13 @@ gnutls-boot-parameters
 VERIFY-HOSTNAME-ERROR is a backwards compatibility option for
 putting `:hostname' in VERIFY-ERROR.
 
+PASS is a string, the password of the key.  It may also be nil,
+for a NULL password.
+
+FLAGS is a list of symbols corresponding to the equivalent ORed
+bitflag of the gnutls_pkcs_encrypt_flags_t enum of GnuTLS.  The
+empty list corresponds to the bitflag with value 0.
+
 When VERIFY-ERROR is t or a list containing `:trustfiles', an
 error will be raised when the peer certificate verification fails
 as per GnuTLS' gnutls_certificate_verify_peers2.  Otherwise, only
@@ -358,6 +366,8 @@ gnutls-boot-parameters
                 :keylist ,keylist
                 :verify-flags ,verify-flags
                 :verify-error ,verify-error
+                :pass ,pass
+                :flags ,flags
                 :callbacks nil)))
 
 (defun gnutls--get-files (files)
diff --git a/src/gnutls.c b/src/gnutls.c
index a0de0238c4..ccfbb58881 100644
--- a/src/gnutls.c
+++ b/src/gnutls.c
@@ -34,6 +34,7 @@
 # endif
 
 # if GNUTLS_VERSION_NUMBER >= 0x030200
+#  define HAVE_GNUTLS_CERTIFICATE_SET_X509_KEY_FILE2
 #  define HAVE_GNUTLS_CIPHER_GET_IV_SIZE
 # endif
 
@@ -121,6 +122,11 @@ DEF_DLL_FN (int, gnutls_certificate_set_x509_crl_file,
 DEF_DLL_FN (int, gnutls_certificate_set_x509_key_file,
 	    (gnutls_certificate_credentials_t, const char *, const char *,
 	     gnutls_x509_crt_fmt_t));
+#  ifdef HAVE_GNUTLS_CERTIFICATE_SET_X509_KEY_FILE2
+DEF_DLL_FN (int, gnutls_certificate_set_x509_key_file2,
+	    (gnutls_certificate_credentials_t, const char *, const char *,
+	     gnutls_x509_crt_fmt_t, const char *, unsigned int));
+#  endif
 #  ifdef HAVE_GNUTLS_X509_SYSTEM_TRUST
 DEF_DLL_FN (int, gnutls_certificate_set_x509_system_trust,
 	    (gnutls_certificate_credentials_t));
@@ -314,6 +320,9 @@ init_gnutls_functions (void)
   LOAD_DLL_FN (library, gnutls_certificate_set_verify_flags);
   LOAD_DLL_FN (library, gnutls_certificate_set_x509_crl_file);
   LOAD_DLL_FN (library, gnutls_certificate_set_x509_key_file);
+#  ifdef HAVE_GNUTLS_CERTIFICATE_SET_X509_KEY_FILE2
+  LOAD_DLL_FN (library, gnutls_certificate_set_x509_key_file2);
+#  endif
 #  ifdef HAVE_GNUTLS_X509_SYSTEM_TRUST
   LOAD_DLL_FN (library, gnutls_certificate_set_x509_system_trust);
 #  endif
@@ -455,6 +464,9 @@ init_gnutls_functions (void)
 #  define gnutls_certificate_set_verify_flags fn_gnutls_certificate_set_verify_flags
 #  define gnutls_certificate_set_x509_crl_file fn_gnutls_certificate_set_x509_crl_file
 #  define gnutls_certificate_set_x509_key_file fn_gnutls_certificate_set_x509_key_file
+#  ifdef HAVE_GNUTLS_CERTIFICATE_SET_X509_KEY_FILE2
+#   define gnutls_certificate_set_x509_key_file2 fn_gnutls_certificate_set_x509_key_file2
+#  endif
 #  define gnutls_certificate_set_x509_system_trust fn_gnutls_certificate_set_x509_system_trust
 #  define gnutls_certificate_set_x509_trust_file fn_gnutls_certificate_set_x509_trust_file
 #  define gnutls_certificate_type_get fn_gnutls_certificate_type_get
@@ -1774,6 +1786,61 @@ gnutls_verify_boot (Lisp_Object proc, Lisp_Object proplist)
   return gnutls_make_error (ret);
 }
 
+#ifdef HAVE_GNUTLS_CERTIFICATE_SET_X509_KEY_FILE2
+
+/* Helper function for gnutls-boot.
+
+   The key :flags receives a list of symbols, each of which
+   corresponds to a GnuTLS C flag, the ORed result is to be passed to
+   the function gnutls_certificate_set_x509_key_file2() as its last
+   argument.
+*/
+static unsigned int
+key_file2_aux (Lisp_Object flags)
+{
+  unsigned int rv = 0;
+  Lisp_Object tail = flags;
+  FOR_EACH_TAIL_SAFE (tail)
+    {
+      Lisp_Object flag = XCAR (tail);
+      if (EQ (flag, intern ("GNUTLS_PKCS_PLAIN")))
+	rv |= GNUTLS_PKCS_PLAIN;
+      else if(EQ (flag, intern ("GNUTLS_PKCS_PKCS12_3DES")))
+	rv |= GNUTLS_PKCS_PKCS12_3DES;
+      else if(EQ (flag, intern ("GNUTLS_PKCS_PKCS12_ARCFOUR")))
+	rv |= GNUTLS_PKCS_PKCS12_ARCFOUR;
+      else if(EQ (flag, intern ("GNUTLS_PKCS_PKCS12_RC2_40")))
+	rv |= GNUTLS_PKCS_PKCS12_RC2_40;
+      else if(EQ (flag, intern ("GNUTLS_PKCS_PBES2_3DES")))
+	rv |= GNUTLS_PKCS_PBES2_3DES;
+      else if(EQ (flag, intern ("GNUTLS_PKCS_PBES2_AES_128")))
+	rv |= GNUTLS_PKCS_PBES2_AES_128;
+      else if(EQ (flag, intern ("GNUTLS_PKCS_PBES2_AES_192")))
+	rv |= GNUTLS_PKCS_PBES2_AES_192;
+      else if(EQ (flag, intern ("GNUTLS_PKCS_PBES2_AES_256")))
+	rv |= GNUTLS_PKCS_PBES2_AES_256;
+      else if(EQ (flag, intern ("GNUTLS_PKCS_NULL_PASSWORD")))
+	rv |= GNUTLS_PKCS_NULL_PASSWORD;
+      else if(EQ (flag, intern ("GNUTLS_PKCS_PBES2_DES")))
+	rv |= GNUTLS_PKCS_PBES2_DES;
+      else if(EQ (flag, intern ("GNUTLS_PKCS_PBES1_DES_MD5")))
+	rv |= GNUTLS_PKCS_PBES1_DES_MD5;
+      else if(EQ (flag, intern ("gnutls_pkcs_pbes2_gost_TC26Z")))
+	rv |= GNUTLS_PKCS_PBES2_GOST_TC26Z;
+      else if(EQ (flag, intern ("GNUTLS_PKCS_PBES2_GOST_CPA")))
+	rv |= GNUTLS_PKCS_PBES2_GOST_CPA;
+      else if(EQ (flag, intern ("GNUTLS_PKCS_PBES2_GOST_CPB")))
+	rv |= GNUTLS_PKCS_PBES2_GOST_CPB;
+      else if(EQ (flag, intern ("GNUTLS_PKCS_PBES2_GOST_CPC")))
+	rv |= GNUTLS_PKCS_PBES2_GOST_CPC;
+      else if(EQ (flag, intern ("GNUTLS_PKCS_PBES2_GOST_CPD")))
+	rv |= GNUTLS_PKCS_PBES2_GOST_CPD;
+    }
+  return rv;
+}
+
+#endif /* HAVE_GNUTLS_CERTIFICATE_SET_X509_KEY_FILE2 */
+
 DEFUN ("gnutls-boot", Fgnutls_boot, Sgnutls_boot, 3, 3, 0,
        doc: /* Initialize GnuTLS client for process PROC with TYPE+PROPLIST.
 Currently only client mode is supported.  Return a success/failure
@@ -1813,6 +1880,21 @@ DEFUN ("gnutls-boot", Fgnutls_boot, Sgnutls_boot, 3, 3, 0,
 :complete-negotiation, if non-nil, will make negotiation complete
 before returning even on non-blocking sockets.
 
+:pass, the password of the private key as per GnuTLS'
+gnutls_certificate_set_x509_key_file2.  Specify as nil to have a NULL
+password.
+
+:flags, a list of symbols relating to :pass, each specifying a flag:
+GNUTLS_PKCS_PLAIN, GNUTLS_PKCS_PKCS12_3DES,
+GNUTLS_PKCS_PKCS12_ARCFOUR, GNUTLS_PKCS_PKCS12_RC2_40,
+GNUTLS_PKCS_PBES2_3DES, GNUTLS_PKCS_PBES2_AES_128,
+GNUTLS_PKCS_PBES2_AES_192, GNUTLS_PKCS_PBES2_AES_256,
+GNUTLS_PKCS_NULL_PASSWORD, GNUTLS_PKCS_PBES2_DES,
+GNUTLS_PKCS_PBES2_DES_MD5, GNUTLS_PKCS_PBES2_GOST_TC26Z,
+GNUTLS_PKCS_PBES2_GOST_CPA, GNUTLS_PKCS_PBES2_GOST_CPB,
+GNUTLS_PKCS_PBES2_GOST_CPC, GNUTLS_PKCS_PBES2_GOST_CPD.  If not
+specified, or if nil, the bitflag with value 0 is used.
+
 The debug level will be set for this process AND globally for GnuTLS.
 So if you set it higher or lower at any point, it affects global
 debugging.
@@ -1825,6 +1907,9 @@ DEFUN ("gnutls-boot", Fgnutls_boot, Sgnutls_boot, 3, 3, 0,
 functions are used.  This function allocates resources which can only
 be deallocated by calling `gnutls-deinit' or by calling it again.
 
+The :pass and :flags keys are ignored with old versions of GnuTLS, and
+:flags is ignored if :pass is not specified.
+
 The callbacks alist can have a `verify' key, associated with a
 verification function (UNUSED).
 
@@ -1848,6 +1933,8 @@ DEFUN ("gnutls-boot", Fgnutls_boot, Sgnutls_boot, 3, 3, 0,
   Lisp_Object trustfiles;
   Lisp_Object crlfiles;
   Lisp_Object keylist;
+  Lisp_Object pass;
+  Lisp_Object flags;
   /* Lisp_Object callbacks; */
   Lisp_Object loglevel;
   Lisp_Object hostname;
@@ -1877,6 +1964,8 @@ DEFUN ("gnutls-boot", Fgnutls_boot, Sgnutls_boot, 3, 3, 0,
   crlfiles              = plist_get (proplist, QCcrlfiles);
   loglevel              = plist_get (proplist, QCloglevel);
   prime_bits            = plist_get (proplist, QCmin_prime_bits);
+  pass                  = plist_get (proplist, QCpass);
+  flags                 = plist_get (proplist, QCflags);
 
   if (!STRINGP (hostname))
     {
@@ -2038,8 +2127,22 @@ DEFUN ("gnutls-boot", Fgnutls_boot, Sgnutls_boot, 3, 3, 0,
 	      keyfile = ansi_encode_filename (keyfile);
 	      certfile = ansi_encode_filename (certfile);
 # endif
+# ifdef HAVE_GNUTLS_CERTIFICATE_SET_X509_KEY_FILE2
+	      if (STRINGP (pass))
+		ret = gnutls_certificate_set_x509_key_file2
+		  (x509_cred, SSDATA (certfile), SSDATA (keyfile), file_format,
+		   SSDATA (pass), key_file2_aux (flags));
+	      else if (NILP (pass) && plist_member (proplist, QCpass))
+		ret = gnutls_certificate_set_x509_key_file2
+		  (x509_cred, SSDATA (certfile), SSDATA (keyfile), file_format,
+		   NULL, key_file2_aux (flags));
+	      else
+		ret = gnutls_certificate_set_x509_key_file
+		  (x509_cred, SSDATA (certfile), SSDATA (keyfile), file_format);
+# else
 	      ret = gnutls_certificate_set_x509_key_file
 		(x509_cred, SSDATA (certfile), SSDATA (keyfile), file_format);
+# endif
 
 	      if (ret < GNUTLS_E_SUCCESS)
 		return gnutls_make_error (ret);
@@ -2860,6 +2963,8 @@ syms_of_gnutls (void)
   DEFSYM (QCmin_prime_bits, ":min-prime-bits");
   DEFSYM (QCloglevel, ":loglevel");
   DEFSYM (QCcomplete_negotiation, ":complete-negotiation");
+  DEFSYM (QCpass, ":pass");
+  DEFSYM (QCflags, ":flags");
   DEFSYM (QCverify_flags, ":verify-flags");
   DEFSYM (QCverify_error, ":verify-error");
 
-- 
2.37.3