Extractor for named RDN element types from a certificate DN field.
[exim.git] / src / src / tls.c
index 0625c48b84d93dda9c3f08aa852b9a4b876f3385..75c46e9e4560b65f641718bc319cfa15ba5bc3a3 100644 (file)
@@ -17,6 +17,7 @@ functions from the OpenSSL or GNU TLS libraries. */
 
 
 #include "exim.h"
+#include "transports/smtp.h"
 
 /* This module is compiled only when it is specifically requested in the
 build-time configuration. However, some compilers don't like compiling empty
@@ -85,6 +86,7 @@ return TRUE;
 
 #ifdef USE_GNUTLS
 #include "tls-gnu.c"
+#include "tlscert-gnu.c"
 
 #define ssl_xfer_buffer (state_server.xfer_buffer)
 #define ssl_xfer_buffer_lwm (state_server.xfer_buffer_lwm)
@@ -94,6 +96,7 @@ return TRUE;
 
 #else
 #include "tls-openssl.c"
+#include "tlscert-openssl.c"
 #endif
 
 
@@ -181,4 +184,78 @@ return ssl_xfer_buffer_lwm < ssl_xfer_buffer_hwm;
 
 #endif  /* SUPPORT_TLS */
 
+void
+tls_modify_variables(tls_support * dest_tsp)
+{
+modify_variable(US"tls_bits",                 &dest_tsp->bits);
+modify_variable(US"tls_certificate_verified", &dest_tsp->certificate_verified);
+modify_variable(US"tls_cipher",               &dest_tsp->cipher);
+modify_variable(US"tls_peerdn",               &dest_tsp->peerdn);
+#if defined(SUPPORT_TLS) && !defined(USE_GNUTLS)
+modify_variable(US"tls_sni",                  &dest_tsp->sni);
+#endif
+}
+
+/************************************************
+*      TLS certificate name operations         *
+************************************************/
+
+/* Convert an rfc4514 DN to an exim comma-sep list.
+Backslashed commas need to be replaced by doublecomma
+for Exim's list quoting.  We modify the given string
+inplace.
+*/
+
+static void
+dn_to_list(uschar * dn)
+{
+uschar * cp;
+for (cp = dn; *cp; cp++)
+  if (cp[0] == '\\' && cp[1] == ',')
+    *cp++ = ',';
+}
+
+
+/* Extract fields of a given type from an RFC4514-
+format Distinguished Name.  Return an Exim list.
+NOTE: We modify the supplied dn string during operation.
+
+Arguments:
+       dn      Distinguished Name string
+       mod     string containing optional list-sep and
+               field selector match, comma-separated
+Return:
+       allocated string with list of matching fields,
+       field type stripped
+*/
+
+uschar *
+tls_field_from_dn(uschar * dn, uschar * mod)
+{
+int insep = ',';
+uschar outsep = '\n';
+uschar * ele;
+uschar * match = NULL;
+int len;
+uschar * list = NULL;
+
+while (ele = string_nextinlist(&mod, &insep, NULL, 0))
+  if (ele[0] != '>')
+    match = ele;       /* field tag to match */
+  else if (ele[1])
+    outsep = ele[1];   /* nondefault separator */
+
+dn_to_list(dn);
+insep = ',';
+len = Ustrlen(match);
+while (ele = string_nextinlist(&dn, &insep, NULL, 0))
+  if (Ustrncmp(ele, match, len) == 0 && ele[len] == '=')
+    list = string_append_listele(list, outsep, ele+len+1);
+return list;
+}
+
+
+
+/* vi: aw ai sw=2
+*/
 /* End of tls.c */