27#define X509_cert_digest X509_digest
33 if (SSL_CTX_use_certificate_chain_file(
ctx,cert) != 1)
38 if (ppwd) SSL_CTX_set_default_passwd_cb_userdata(
ctx,ppwd);
40 if (SSL_CTX_use_PrivateKey_file(
ctx,
key,SSL_FILETYPE_PEM) != 1)
43 if (SSL_CTX_check_private_key(
ctx) != 1)
51 SSL_set_options(
ssl,SSL_OP_NO_SSLv2);
52 SSL_set_options(
ssl,SSL_OP_NO_SSLv3);
68 if (flag > 20) fflag = flag - 20;
69 if (flag > 10) fflag = flag - 10;
73 if (
host.len && fflag > 4) {
74 GENERAL_NAMES *extensions = X509_get_ext_d2i(cert,NID_subject_alt_name,0,0);
75 num = sk_GENERAL_NAME_num(extensions);
77 for (i = 0; i <
num; ++i) {
78 const GENERAL_NAME *
ext = sk_GENERAL_NAME_value(extensions,i);
79 if (
ext->type == GEN_DNS) {
80 if (OBJ_sn2nid((
const char*)
ext->d.ia5) != V_ASN1_IA5STRING)
continue;
81 dnsname = (
char *)ASN1_STRING_get0_data(
ext->d.ia5);
82 len = ASN1_STRING_length(
ext->d.ia5);
88 X509_NAME_get_text_by_NID(X509_get_subject_name(cert),NID_commonName,
buf,
sizeof(
buf));
106 if (fflag > 3 && verify > -2) {
107 if (SSL_get_verify_result(
ssl) != X509_V_OK)
return -2;
120int dig_ascii(
char *digascii,
const char *digest,
const int len)
122 static const char hextab[] =
"0123456789abcdef";
125 for (
j = 0;
j < len;
j++) {
126 digascii[2 *
j] = hextab[(
unsigned char)digest[
j] >> 4];
127 digascii[2 *
j + 1] = hextab[(
unsigned char)digest[
j] & 0x0f];
129 digascii[2 * len] =
'\0';
141 unsigned int len = 0;
142 unsigned int size = 2048;
145 unsigned char buffer[
size];
149 if (!X509_get0_pubkey_bitstr(cert))
return 0;
151 len = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(cert),0);
152 if (len >
size)
return 0;
154 i2d_X509_PUBKEY(X509_get_X509_PUBKEY(cert),(
unsigned char **)&
buf2);
155 if (
buf2 -
buf != len)
return 0;
157 if (!EVP_Digest(
buf,len,md,
dlen,type,0))
return 0;
170 const EVP_MD *methodsha256 = EVP_sha256();
171 const EVP_MD *methodsha512 = EVP_sha512();
175 unsigned char digest[EVP_MAX_MD_SIZE];
176 unsigned int dlen = 0;
180 char port[FMT_ULONG];
187 if (
host.len < 2)
return 0;
194 if (dns_cname(&cn,&
sa) > 0)
198 if (
out.len < 5)
return -1;
205 if (!byte_diff(
tlsa0,
out.len - i - 3,
out.s + i + 3))
return -5;
208 type = (
unsigned char)
out.s[i + 2];
210 unsigned len = sk_X509_num(certs);
211 for (n = 0; n < len; n++) {
212 X509 *cert = sk_X509_value(certs,n);
216 }
else if (type == 2) {
222 if (!byte_diff(digest,
dlen,
out.s + i + 3))
return ++
usage;
226 }
while (i <
out.len - 4);
233 const EVP_MD *methodsha1 = EVP_sha1();
234 const EVP_MD *methodsha224 = EVP_sha224();
235 const EVP_MD *methodsha256 = EVP_sha256();
236 const EVP_MD *methodsha512 = EVP_sha512();
237 unsigned char digest[EVP_MAX_MD_SIZE];
238 unsigned char digascii[257];
242 case 40:
if (!X509_digest(cert,methodsha1,digest,&len))
return -2;
243 case 56:
if (!X509_digest(cert,methodsha224,digest,&len))
return -2;
244 case 64:
if (!X509_digest(cert,methodsha256,digest,&len))
return -2;
245 case 128:
if (!X509_digest(cert,methodsha512,digest,&len))
return -2;
250 if (!str_diffn(digascii,fingerprint,len))
return 1;
257 if (SSL_shutdown(
ssl) == 0)
279 stralloc tlshost = {0};
313 for (i = 0; i < tlshost.len; ++i)
314 if ((i == 0) || (tlshost.s[i] ==
'.')) {
320 for (i = 0; i < tlshost.len; ++i)
321 if ((i == 0) || (tlshost.s[i] ==
'.')) {
327 for (i = 0; i < tlshost.len; ++i)
328 if ((i == 0) || (tlshost.s[i] ==
'.')) {
334 for (i = 0; i < tlshost.len; ++i)
335 if ((i == 0) || (tlshost.s[i] ==
'.')) {
341 for (i = 0; i < tlshost.len; ++i)
342 if ((i == 0) || (tlshost.s[i] ==
'.')) {
374 for (i = 0; i < domainname.len; ++i)
375 if ((i == 0) || (domainname.s[i] ==
'.'))
int stralloc_copys(stralloc *, char const *)
int dns_tlsa(stralloc *out, const stralloc *fqdn)
void p(char *, char *, int, int, int)
int tls_domaincerts(const stralloc domainname)
int tls_fingerprint(X509 *cert, const char *fingerprint, int dlen)
int tls_checkcrl(SSL *ssl)
int X509_pkey_digest(const X509 *cert, const EVP_MD *type, unsigned char *md, unsigned int *dlen)
int tls_conn(SSL *ssl, int smtpfd)
int tls_destination(const stralloc hostname)
tls_destination
int tlsa_check(const STACK_OF(X509) *certs, const stralloc host, const unsigned long p)
int dig_ascii(char *digascii, const char *digest, const int len)
int tls_certkey(SSL_CTX *ctx, const char *cert, const char *key, char *ppwd)
int tls_checkpeer(SSL *ssl, X509 *cert, const stralloc host, const int flag, const int verify)
struct constmap maptlsdestinations
struct constmap mapdomaincerts