[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] Code to get nicknames from peer certs



Update of /home/or/cvsroot/src/common
In directory moria.mit.edu:/tmp/cvs-serv10695/common

Modified Files:
	tortls.c tortls.h 
Log Message:
Code to get nicknames from peer certs

Index: tortls.c
===================================================================
RCS file: /home/or/cvsroot/src/common/tortls.c,v
retrieving revision 1.24
retrieving revision 1.25
diff -u -d -r1.24 -r1.25
--- tortls.c	19 Oct 2003 00:39:48 -0000	1.24
+++ tortls.c	19 Oct 2003 00:46:51 -0000	1.25
@@ -12,6 +12,9 @@
 #include "./util.h"
 #include "./log.h"
 
+/* Copied from or.h */
+#define LEGAL_NICKNAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
+
 #include <assert.h>
 #include <openssl/ssl.h>
 #include <openssl/err.h>
@@ -132,7 +135,6 @@
   EVP_PKEY *pkey = NULL;
   X509 *x509 = NULL;
   X509_NAME *name = NULL;
-  BIO *out = NULL;
   int nid;
   int err;
   
@@ -178,8 +180,6 @@
  error:
   err = 1;
  done:
-  if (out)
-    BIO_free(out);
   if (x509 && err)
     X509_free(x509);
   if (pkey)
@@ -459,6 +459,36 @@
     return 0;
   X509_free(cert);
   return 1;
+}
+
+int
+tor_tls_get_peer_cert_nickname(tor_tls *tls, char *buf, int buflen)
+{
+  X509 *cert = NULL;
+  X509_NAME *name = NULL;
+  int nid;
+  int lenout;
+  int i;
+  
+  if (!(cert = SSL_get_peer_certificate(tls->ssl))) {
+    log_fn(LOG_ERR, "Peer has no certificate");
+    return -1;
+  }
+  if (!(name = X509_get_subject_name(cert))) {
+    log_fn(LOG_ERR, "Peer certificate has no subject name");
+    return -1;
+  }
+  if ((nid = OBJ_txt2nid("commonName")) == NID_undef)
+    return -1;
+  
+  lenout = X509_NAME_get_text_by_NID(name, nid, buf, buflen);
+  if (lenout == -1)
+    return -1;
+  if (strspn(buf, LEGAL_NICKNAME_CHARACTERS) != lenout) {
+    log_fn(LOG_ERR, "Peer certificate nickname has illegal characters.");
+    return -1;
+  }
+  return 0;
 }
 
 /* If the provided tls connection is authenticated and has a

Index: tortls.h
===================================================================
RCS file: /home/or/cvsroot/src/common/tortls.h,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -d -r1.8 -r1.9
--- tortls.h	28 Sep 2003 06:47:29 -0000	1.8
+++ tortls.h	19 Oct 2003 00:46:51 -0000	1.9
@@ -21,6 +21,7 @@
 tor_tls *tor_tls_new(int sock, int isServer);
 void tor_tls_free(tor_tls *tls);
 int tor_tls_peer_has_cert(tor_tls *tls);
+int tor_tls_get_peer_cert_nickname(tor_tls *tls, char *buf, int buflen);
 crypto_pk_env_t *tor_tls_verify(tor_tls *tls);
 int tor_tls_read(tor_tls *tls, char *cp, int len);
 int tor_tls_write(tor_tls *tls, char *cp, int n);