ucspi-ssl  0.12.7
ucspi-ssl
ssl_verify.c
Go to the documentation of this file.
1 
6 #include "ucspissl.h"
7 #include "case.h"
8 #include "str.h"
9 
10 int ssl_verify(SSL *ssl,const char *hostname,stralloc *dnsout)
11 {
12  X509 *cert;
13  STACK_OF(GENERAL_NAME) *extensions;
14  const GENERAL_NAME *ext;
15  char buf[SSL_NAME_LEN];
16  char *dnsname = 0;
17  int i;
18  int num;
19  int len;
20  int dname = 0;
21 
22 #if (OPENSSL_VERSION_NUMBER > 0x30000000L) // 0xmnnffppsL
23  cert = SSL_get_peer_cert_chain(ssl);
24 #else
25  cert = SSL_get_peer_certificate(ssl);
26 #endif
27  if (!cert) return -1;
28 
29  if (SSL_get_verify_result(ssl) != X509_V_OK) return -2;
30 
31  if (hostname) {
32  if (!stralloc_copys(dnsout,"")) return 1;
33  extensions = X509_get_ext_d2i(cert,NID_subject_alt_name,0,0);
34  num = sk_GENERAL_NAME_num(extensions); /* num = 0, if no SAN extensions */
35 
36  for (i = 0; i < num; ++i) {
37  ext = sk_GENERAL_NAME_value(extensions,i);
38  if (ext->type == GEN_DNS) {
39  if (ASN1_STRING_type(ext->d.dNSName) != V_ASN1_IA5STRING) continue;
40 #if ((OPENSSL_VERSION_NUMBER < 0x10100000L) || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L))
41  dnsname = (char *)ASN1_STRING_data(ext->d.dNSName);
42 #else
43  dnsname = (char *)ASN1_STRING_get0_data(ext->d.dNSName);
44 #endif
45  len = ASN1_STRING_length(ext->d.dNSName);
46  if (len != str_len(dnsname)) continue;
47  if (!stralloc_copyb(dnsout,dnsname,len)) return 1;
48  if (case_diffs((char *)hostname,dnsname) == 0) return 0;
49  dname = 1;
50  }
51  }
52 
53  if (!dname) {
54  X509_NAME_get_text_by_NID(X509_get_subject_name(cert),NID_commonName,buf,sizeof(buf));
55  buf[SSL_NAME_LEN - 1] = 0;
56  if (!stralloc_copyb(dnsout,buf,str_len(buf))) return 1;
57  if (case_diffs((char *)hostname,buf) == 0) return 0;
58  }
59 
60  return -3;
61  }
62  return 0;
63 }
int ssl_verify(SSL *ssl, const char *hostname, stralloc *dnsout)
Definition: ssl_verify.c:10
const char * hostname
Definition: sslclient.c:86
X509 * cert
Definition: sslhandle.c:125
char buf[SSL_NAME_LEN]
Definition: sslhandle.c:126
Header file to be used with sqmail; previously called ssl.h. (name clash)
#define SSL_NAME_LEN
Definition: ucspissl.h:31