ucspi-ssl 0.13.03
ucspi-ssl
Loading...
Searching...
No Matches
ssl_verify.c
Go to the documentation of this file.
1
6#include "ucspissl.h"
7#include "case.h"
8#include "str.h"
9
10int 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 cert = SSL_get_peer_certificate(ssl); // deprecated, but working everywhere => SSL_get1_peer_certificate()
23 if (!cert) return -1;
24
25 if (SSL_get_verify_result(ssl) != X509_V_OK) return -2;
26
27 if (hostname) {
28 if (!stralloc_copys(dnsout,"")) return 1;
29 extensions = X509_get_ext_d2i(cert,NID_subject_alt_name,0,0);
30 num = sk_GENERAL_NAME_num(extensions); /* num = 0, if no SAN extensions */
31
32 for (i = 0; i < num; ++i) {
33 ext = sk_GENERAL_NAME_value(extensions,i);
34 if (ext->type == GEN_DNS) {
35 if (ASN1_STRING_type(ext->d.dNSName) != V_ASN1_IA5STRING) continue;
36 dnsname = (char *)ASN1_STRING_get0_data(ext->d.dNSName);
37 len = ASN1_STRING_length(ext->d.dNSName);
38 if (len != str_len(dnsname)) continue;
39 if (!stralloc_copyb(dnsout,dnsname,len)) return 1;
40 if (case_diffs((char *)hostname,dnsname) == 0) return 0;
41 dname = 1;
42 }
43 }
44
45 if (!dname) {
46 X509_NAME_get_text_by_NID(X509_get_subject_name(cert),NID_commonName,buf,sizeof(buf));
47 buf[SSL_NAME_LEN - 1] = 0;
48 if (!stralloc_copyb(dnsout,buf,str_len(buf))) return 1;
49 if (case_diffs((char *)hostname,buf) == 0) return 0;
50 }
51
52 return -3;
53 }
54 return 0;
55}
int ssl_verify(SSL *ssl, const char *hostname, stralloc *dnsout)
Definition: ssl_verify.c:10
const char * hostname
Definition: sslclient.c:88
X509 * cert
Definition: sslhandle.c:126
char buf[SSL_NAME_LEN]
Definition: sslhandle.c:127
Header file to be used with sqmail; previously called ssl.h. (name clash)
#define SSL_NAME_LEN
Definition: ucspissl.h:36