| 1 |
#include <net-snmp/net-snmp-config.h> |
| 2 |
#include <net-snmp/net-snmp-features.h> |
| 3 |
#include <net-snmp/types.h> |
| 4 |
|
| 5 |
netsnmp_feature_require(cert_util); |
| 6 |
|
| 7 |
#ifdef HAVE_STRING_H |
| 8 |
#include <string.h> |
| 9 |
#else |
| 10 |
#include <strings.h> |
| 11 |
#endif |
| 12 |
#ifdef HAVE_SYS_SOCKET_H |
| 13 |
#include <sys/socket.h> |
| 14 |
#endif |
| 15 |
#ifdef HAVE_NETINET_IN_H |
| 16 |
#include <netinet/in.h> |
| 17 |
#endif |
| 18 |
#ifdef HAVE_ARPA_INET_H |
| 19 |
#include <arpa/inet.h> |
| 20 |
#endif |
| 21 |
#ifdef HAVE_NETDB_H |
| 22 |
#include <netdb.h> |
| 23 |
#endif |
| 24 |
#include <errno.h> |
| 25 |
#include <ctype.h> |
| 26 |
#include "../memcheck.h" |
| 27 |
|
| 28 |
/* OpenSSL Includes */ |
| 29 |
#include <openssl/bio.h> |
| 30 |
#include <openssl/ssl.h> |
| 31 |
#include <openssl/err.h> |
| 32 |
#include <openssl/x509.h> |
| 33 |
#include <openssl/x509_vfy.h> |
| 34 |
#include <openssl/x509v3.h> |
| 35 |
|
| 36 |
#include <net-snmp/config_api.h> |
| 37 |
#include <net-snmp/library/container.h> |
| 38 |
#include <net-snmp/library/cert_util.h> |
| 39 |
#include <net-snmp/library/snmp_openssl.h> |
| 40 |
#include <net-snmp/library/default_store.h> |
| 41 |
#include <net-snmp/library/callback.h> |
| 42 |
#include <net-snmp/library/snmp_logging.h> |
| 43 |
#include <net-snmp/library/snmp_api.h> |
| 44 |
#include <net-snmp/library/tools.h> |
| 45 |
#include <net-snmp/library/snmp_debug.h> |
| 46 |
#include <net-snmp/library/snmp_assert.h> |
| 47 |
#include <net-snmp/library/snmp_transport.h> |
| 48 |
#include <net-snmp/library/snmp_secmod.h> |
| 49 |
#include <net-snmp/library/read_config.h> |
| 50 |
#include <net-snmp/library/system.h> |
| 51 |
#include <net-snmp/library/snmpTLSBaseDomain.h> |
| 52 |
|
| 53 |
#ifndef X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS |
| 54 |
#define X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS 0 |
| 55 |
#endif |
| 56 |
|
| 57 |
#define LOGANDDIE(msg) do { snmp_log(LOG_ERR, "%s\n", msg); return 0; } while(0) |
| 58 |
|
| 59 |
int openssl_local_index; |
| 60 |
|
| 61 |
#ifndef HAVE_ERR_GET_ERROR_ALL |
| 62 |
/* |
| 63 |
* A backport of the OpenSSL 3.0 ERR_get_error_all() function. See also |
| 64 |
* OpenSSL commit b13342e933c5 ("Modernise the ERR functionality further (new |
| 65 |
* functions and deprecations)"). |
| 66 |
*/ |
| 67 |
static unsigned long ERR_get_error_all(const char **file, int *line, |
| 68 |
const char **func, |
| 69 |
const char **data, int *flags) |
| 70 |
{ |
| 71 |
*func = "(?)"; |
| 72 |
#ifdef HAVE_ERR_GET_ERROR_FUNC |
| 73 |
ERR_get_error_func(func); |
| 74 |
#endif |
| 75 |
return ERR_get_error_line_data(file, line, data, flags); |
| 76 |
} |
| 77 |
#endif |
| 78 |
|
| 79 |
/* this is called during negotiation */ |
| 80 |
int verify_callback(int ok, X509_STORE_CTX *ctx) { |
| 81 |
int err, depth; |
| 82 |
char buf[1024], *fingerprint; |
| 83 |
X509 *thecert; |
| 84 |
netsnmp_cert *cert; |
| 85 |
_netsnmp_verify_info *verify_info; |
| 86 |
SSL *ssl; |
| 87 |
|
| 88 |
thecert = X509_STORE_CTX_get_current_cert(ctx); |
| 89 |
err = X509_STORE_CTX_get_error(ctx); |
| 90 |
depth = X509_STORE_CTX_get_error_depth(ctx); |
| 91 |
|
| 92 |
/* things to do: */ |
| 93 |
|
| 94 |
X509_NAME_oneline(X509_get_subject_name(thecert), buf, sizeof(buf)); |
| 95 |
fingerprint = netsnmp_openssl_cert_get_fingerprint(thecert, NS_HASH_SHA1); |
| 96 |
DEBUGMSGTL(("tls_x509:verify", "Cert: %s\n", buf)); |
| 97 |
DEBUGMSGTL(("tls_x509:verify", " fp: %s\n", fingerprint ? |
| 98 |
fingerprint : "unknown")); |
| 99 |
|
| 100 |
ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx()); |
| 101 |
verify_info = SSL_get_ex_data(ssl, openssl_local_index); |
| 102 |
|
| 103 |
if (verify_info && ok && depth > 0) { |
| 104 |
/* remember that a parent certificate has been marked as trusted */ |
| 105 |
verify_info->flags |= VRFY_PARENT_WAS_OK; |
| 106 |
} |
| 107 |
|
| 108 |
/* this ensures for self-signed certificates we have a valid |
| 109 |
locally known fingerprint and then accept it */ |
| 110 |
if (!ok && |
| 111 |
(X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT == err || |
| 112 |
X509_V_ERR_CERT_UNTRUSTED == err || |
| 113 |
X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE == err || |
| 114 |
X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN == err)) { |
| 115 |
|
| 116 |
cert = netsnmp_cert_find(NS_CERT_REMOTE_PEER, NS_CERTKEY_FINGERPRINT, |
| 117 |
(void*)fingerprint); |
| 118 |
if (cert) |
| 119 |
DEBUGMSGTL(("tls_x509:verify", " Found locally: %s/%s\n", |
| 120 |
cert->info.dir, cert->info.filename)); |
| 121 |
|
| 122 |
|
| 123 |
if (cert) { |
| 124 |
DEBUGMSGTL(("tls_x509:verify", "verify_callback called with: ok=%d ctx=%p depth=%d err=%i:%s\n", ok, ctx, depth, err, X509_verify_cert_error_string(err))); |
| 125 |
DEBUGMSGTL(("tls_x509:verify", " accepting matching fp of self-signed certificate found in: %s\n", |
| 126 |
cert->info.filename)); |
| 127 |
SNMP_FREE(fingerprint); |
| 128 |
return 1; |
| 129 |
} else { |
| 130 |
DEBUGMSGTL(("tls_x509:verify", " no matching fp found\n")); |
| 131 |
/* log where we are and why called */ |
| 132 |
snmp_log(LOG_ERR, "tls verification failure: ok=%d ctx=%p depth=%d err=%i:%s\n", ok, ctx, depth, err, X509_verify_cert_error_string(err)); |
| 133 |
SNMP_FREE(fingerprint); |
| 134 |
return 0; |
| 135 |
} |
| 136 |
|
| 137 |
#if 0 |
| 138 |
/* |
| 139 |
* This code is unreachable because of the return statements above. |
| 140 |
* Comment it out to avoid that Coverity complains about this code. |
| 141 |
*/ |
| 142 |
if (0 == depth && verify_info && |
| 143 |
(verify_info->flags & VRFY_PARENT_WAS_OK)) { |
| 144 |
DEBUGMSGTL(("tls_x509:verify", "verify_callback called with: ok=%d ctx=%p depth=%d err=%i:%s\n", ok, ctx, depth, err, X509_verify_cert_error_string(err))); |
| 145 |
DEBUGMSGTL(("tls_x509:verify", " a parent was ok, so returning ok for this child certificate\n")); |
| 146 |
SNMP_FREE(fingerprint); |
| 147 |
return 1; /* we'll check the hostname later at this level */ |
| 148 |
} |
| 149 |
#endif |
| 150 |
} |
| 151 |
|
| 152 |
if (0 == ok) |
| 153 |
snmp_log(LOG_ERR, "tls verification failure: ok=%d ctx=%p depth=%d err=%i:%s\n", ok, ctx, depth, err, X509_verify_cert_error_string(err)); |
| 154 |
else |
| 155 |
DEBUGMSGTL(("tls_x509:verify", "verify_callback called with: ok=%d ctx=%p depth=%d err=%i:%s\n", ok, ctx, depth, err, X509_verify_cert_error_string(err))); |
| 156 |
DEBUGMSGTL(("tls_x509:verify", " returning the passed in value of %d\n", |
| 157 |
ok)); |
| 158 |
SNMP_FREE(fingerprint); |
| 159 |
return(ok); |
| 160 |
} |
| 161 |
|
| 162 |
#define VERIFIED_FINGERPRINT 0 |
| 163 |
#define NO_FINGERPRINT_AVAILABLE 1 |
| 164 |
#define FAILED_FINGERPRINT_VERIFY 2 |
| 165 |
|
| 166 |
static int |
| 167 |
_netsnmp_tlsbase_verify_remote_fingerprint(X509 *remote_cert, |
| 168 |
_netsnmpTLSBaseData *tlsdata, |
| 169 |
int try_default) { |
| 170 |
|
| 171 |
char *fingerprint; |
| 172 |
|
| 173 |
fingerprint = |
| 174 |
netsnmp_openssl_cert_get_fingerprint(remote_cert, NS_HASH_SHA1); |
| 175 |
|
| 176 |
if (!fingerprint) { |
| 177 |
/* no peer cert */ |
| 178 |
snmp_log(LOG_ERR, "failed to get fingerprint of remote certificate\n"); |
| 179 |
return FAILED_FINGERPRINT_VERIFY; |
| 180 |
} |
| 181 |
|
| 182 |
if (!tlsdata->their_fingerprint && tlsdata->their_identity) { |
| 183 |
/* we have an identity; try and find it's fingerprint */ |
| 184 |
netsnmp_cert *peer_cert; |
| 185 |
peer_cert = |
| 186 |
netsnmp_cert_find(NS_CERT_REMOTE_PEER, NS_CERTKEY_MULTIPLE, |
| 187 |
tlsdata->their_identity); |
| 188 |
|
| 189 |
if (peer_cert) |
| 190 |
tlsdata->their_fingerprint = |
| 191 |
netsnmp_openssl_cert_get_fingerprint(peer_cert->ocert, NS_HASH_SHA1); |
| 192 |
} |
| 193 |
|
| 194 |
if (!tlsdata->their_fingerprint && try_default) { |
| 195 |
/* try for the default instead */ |
| 196 |
netsnmp_cert *peer_cert; |
| 197 |
peer_cert = |
| 198 |
netsnmp_cert_find(NS_CERT_REMOTE_PEER, NS_CERTKEY_DEFAULT, |
| 199 |
NULL); |
| 200 |
|
| 201 |
if (peer_cert) |
| 202 |
tlsdata->their_fingerprint = |
| 203 |
netsnmp_openssl_cert_get_fingerprint(peer_cert->ocert, NS_HASH_SHA1); |
| 204 |
} |
| 205 |
|
| 206 |
if (tlsdata->their_fingerprint) { |
| 207 |
netsnmp_fp_lowercase_and_strip_colon(tlsdata->their_fingerprint); |
| 208 |
if (0 != strcmp(tlsdata->their_fingerprint, fingerprint)) { |
| 209 |
snmp_log(LOG_ERR, "The fingerprint from the remote side's certificate didn't match the expected\n"); |
| 210 |
snmp_log(LOG_ERR, " got %s, expected %s\n", |
| 211 |
fingerprint, tlsdata->their_fingerprint); |
| 212 |
free(fingerprint); |
| 213 |
return FAILED_FINGERPRINT_VERIFY; |
| 214 |
} |
| 215 |
} else { |
| 216 |
DEBUGMSGTL(("tls_x509:verify", "No fingerprint for the remote entity available to verify\n")); |
| 217 |
free(fingerprint); |
| 218 |
return NO_FINGERPRINT_AVAILABLE; |
| 219 |
} |
| 220 |
|
| 221 |
free(fingerprint); |
| 222 |
return VERIFIED_FINGERPRINT; |
| 223 |
} |
| 224 |
|
| 225 |
/* this is called after the connection on the client side by us to check |
| 226 |
other aspects about the connection */ |
| 227 |
int |
| 228 |
netsnmp_tlsbase_verify_server_cert(SSL *ssl, _netsnmpTLSBaseData *tlsdata) { |
| 229 |
/* XXX */ |
| 230 |
X509 *remote_cert; |
| 231 |
char *check_name; |
| 232 |
int ret; |
| 233 |
|
| 234 |
netsnmp_assert_or_return(ssl != NULL, SNMPERR_GENERR); |
| 235 |
netsnmp_assert_or_return(tlsdata != NULL, SNMPERR_GENERR); |
| 236 |
|
| 237 |
remote_cert = SSL_get_peer_certificate(ssl); |
| 238 |
if (!remote_cert) { |
| 239 |
/* no peer cert */ |
| 240 |
DEBUGMSGTL(("tls_x509:verify", |
| 241 |
"remote connection provided no certificate (yet)\n")); |
| 242 |
return SNMPERR_TLS_NO_CERTIFICATE; |
| 243 |
} |
| 244 |
|
| 245 |
/* make sure that the fingerprint matches */ |
| 246 |
ret = _netsnmp_tlsbase_verify_remote_fingerprint(remote_cert, tlsdata, 1); |
| 247 |
switch(ret) { |
| 248 |
case VERIFIED_FINGERPRINT: |
| 249 |
return SNMPERR_SUCCESS; |
| 250 |
|
| 251 |
case FAILED_FINGERPRINT_VERIFY: |
| 252 |
return SNMPERR_GENERR; |
| 253 |
|
| 254 |
case NO_FINGERPRINT_AVAILABLE: |
| 255 |
if (tlsdata->their_hostname && tlsdata->their_hostname[0] != '\0') { |
| 256 |
GENERAL_NAMES *onames; |
| 257 |
const GENERAL_NAME *oname = NULL; |
| 258 |
int i, j; |
| 259 |
int count; |
| 260 |
char buf[SPRINT_MAX_LEN]; |
| 261 |
int is_wildcarded = 0; |
| 262 |
char *compare_to; |
| 263 |
|
| 264 |
/* see if the requested hostname has a wildcard prefix */ |
| 265 |
if (strncmp(tlsdata->their_hostname, "*.", 2) == 0) { |
| 266 |
is_wildcarded = 1; |
| 267 |
compare_to = tlsdata->their_hostname + 2; |
| 268 |
} else { |
| 269 |
compare_to = tlsdata->their_hostname; |
| 270 |
} |
| 271 |
|
| 272 |
/* if the hostname we were expecting to talk to matches |
| 273 |
the cert, then we can accept this connection. */ |
| 274 |
|
| 275 |
/* check against the DNS subjectAltName */ |
| 276 |
onames = (GENERAL_NAMES *)X509_get_ext_d2i(remote_cert, |
| 277 |
NID_subject_alt_name, |
| 278 |
NULL, NULL ); |
| 279 |
if (NULL != onames) { |
| 280 |
count = sk_GENERAL_NAME_num(onames); |
| 281 |
|
| 282 |
for (i=0 ; i <count; ++i) { |
| 283 |
oname = sk_GENERAL_NAME_value(onames, i); |
| 284 |
if (GEN_DNS == oname->type) { |
| 285 |
|
| 286 |
/* get the value */ |
| 287 |
ASN1_STRING_to_UTF8((unsigned char**)&check_name, |
| 288 |
oname->d.ia5); |
| 289 |
|
| 290 |
/* convert to lowercase for comparisons */ |
| 291 |
for (j = 0; *check_name && j < sizeof(buf)-1; |
| 292 |
++check_name, ++j) { |
| 293 |
buf[j] = tolower(0xFF & *check_name); |
| 294 |
} |
| 295 |
if (j < sizeof(buf)) |
| 296 |
buf[j] = '\0'; |
| 297 |
check_name = buf; |
| 298 |
|
| 299 |
if (is_wildcarded) { |
| 300 |
/* we *only* allow passing till the first '.' */ |
| 301 |
/* ie *.example.com can't match a.b.example.com */ |
| 302 |
check_name = strchr(check_name, '.') + 1; |
| 303 |
} |
| 304 |
|
| 305 |
DEBUGMSGTL(("tls_x509:verify", "checking subjectAltname of dns:%s\n", check_name)); |
| 306 |
if (strcmp(compare_to, check_name) == 0) { |
| 307 |
|
| 308 |
DEBUGMSGTL(("tls_x509:verify", "Successful match on a subjectAltname of dns:%s\n", check_name)); |
| 309 |
return SNMPERR_SUCCESS; |
| 310 |
} |
| 311 |
} |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
/* check the common name for a match */ |
| 316 |
check_name = |
| 317 |
netsnmp_openssl_cert_get_commonName(remote_cert, NULL, NULL); |
| 318 |
|
| 319 |
if (is_wildcarded) { |
| 320 |
/* we *only* allow passing till the first '.' */ |
| 321 |
/* ie *.example.com can't match a.b.example.com */ |
| 322 |
if (check_name) |
| 323 |
check_name = strchr(check_name, '.'); |
| 324 |
if (check_name) |
| 325 |
check_name++; |
| 326 |
} |
| 327 |
|
| 328 |
if (check_name && strcmp(compare_to, check_name) == 0) { |
| 329 |
DEBUGMSGTL(("tls_x509:verify", "Successful match on a common name of %s\n", check_name)); |
| 330 |
return SNMPERR_SUCCESS; |
| 331 |
} |
| 332 |
|
| 333 |
snmp_log(LOG_ERR, "No matching names in the certificate to match the expected %s\n", tlsdata->their_hostname); |
| 334 |
return SNMPERR_GENERR; |
| 335 |
|
| 336 |
} |
| 337 |
/* XXX: check for hostname match instead */ |
| 338 |
snmp_log(LOG_ERR, "Can not verify a remote server identity without configuration\n"); |
| 339 |
return SNMPERR_GENERR; |
| 340 |
} |
| 341 |
DEBUGMSGTL(("tls_x509:verify", "shouldn't get here\n")); |
| 342 |
return SNMPERR_GENERR; |
| 343 |
} |
| 344 |
|
| 345 |
/* this is called after the connection on the server side by us to check |
| 346 |
the validity of the client's certificate */ |
| 347 |
int |
| 348 |
netsnmp_tlsbase_verify_client_cert(SSL *ssl, _netsnmpTLSBaseData *tlsdata) { |
| 349 |
/* XXX */ |
| 350 |
X509 *remote_cert; |
| 351 |
int ret; |
| 352 |
|
| 353 |
/* RFC5953: section 5.3.2, paragraph 1: |
| 354 |
A (D)TLS server should accept new session connections from any client |
| 355 |
that it is able to verify the client's credentials for. This is done |
| 356 |
by authenticating the client's presented certificate through a |
| 357 |
certificate path validation process (e.g. [RFC5280]) or through |
| 358 |
certificate fingerprint verification using fingerprints configured in |
| 359 |
the snmpTlstmCertToTSNTable. Afterward the server will determine the |
| 360 |
identity of the remote entity using the following procedures. |
| 361 |
*/ |
| 362 |
/* Implementation notes: |
| 363 |
+ path validation is taken care of during the openssl verify |
| 364 |
routines, our part of which is hanlded in verify_callback |
| 365 |
above. |
| 366 |
+ fingerprint verification happens below. |
| 367 |
*/ |
| 368 |
remote_cert = SSL_get_peer_certificate(ssl); |
| 369 |
if (!remote_cert) { |
| 370 |
/* no peer cert */ |
| 371 |
DEBUGMSGTL(("tls_x509:verify", |
| 372 |
"remote connection provided no certificate (yet)\n")); |
| 373 |
return SNMPERR_TLS_NO_CERTIFICATE; |
| 374 |
} |
| 375 |
|
| 376 |
/* we don't force a known remote fingerprint for a client since we |
| 377 |
will accept any certificate we know about (and later derive the |
| 378 |
securityName from it and apply access control) */ |
| 379 |
ret = _netsnmp_tlsbase_verify_remote_fingerprint(remote_cert, tlsdata, 0); |
| 380 |
switch(ret) { |
| 381 |
case FAILED_FINGERPRINT_VERIFY: |
| 382 |
DEBUGMSGTL(("tls_x509:verify", "failed to verify a client fingerprint\n")); |
| 383 |
return SNMPERR_GENERR; |
| 384 |
|
| 385 |
case NO_FINGERPRINT_AVAILABLE: |
| 386 |
DEBUGMSGTL(("tls_x509:verify", "no known fingerprint available (not a failure case)\n")); |
| 387 |
return SNMPERR_SUCCESS; |
| 388 |
|
| 389 |
case VERIFIED_FINGERPRINT: |
| 390 |
DEBUGMSGTL(("tls_x509:verify", "Verified client fingerprint\n")); |
| 391 |
tlsdata->flags |= NETSNMP_TLSBASE_CERT_FP_VERIFIED; |
| 392 |
return SNMPERR_SUCCESS; |
| 393 |
} |
| 394 |
|
| 395 |
DEBUGMSGTL(("tls_x509:verify", "shouldn't get here\n")); |
| 396 |
return SNMPERR_GENERR; |
| 397 |
} |
| 398 |
|
| 399 |
/* this is called after the connection on the server side by us to |
| 400 |
check other aspects about the connection and obtain the |
| 401 |
securityName from the remote certificate. */ |
| 402 |
int |
| 403 |
netsnmp_tlsbase_extract_security_name(SSL *ssl, _netsnmpTLSBaseData *tlsdata) { |
| 404 |
netsnmp_container *chain_maps; |
| 405 |
netsnmp_cert_map *cert_map, *peer_cert; |
| 406 |
netsnmp_iterator *itr; |
| 407 |
int rc; |
| 408 |
|
| 409 |
netsnmp_assert_or_return(ssl != NULL, SNMPERR_GENERR); |
| 410 |
netsnmp_assert_or_return(tlsdata != NULL, SNMPERR_GENERR); |
| 411 |
|
| 412 |
if (NULL == (chain_maps = netsnmp_openssl_get_cert_chain(ssl))) |
| 413 |
return SNMPERR_GENERR; |
| 414 |
/* |
| 415 |
* map fingerprints to mapping entries |
| 416 |
*/ |
| 417 |
rc = netsnmp_cert_get_secname_maps(chain_maps); |
| 418 |
if ((-1 == rc) || (CONTAINER_SIZE(chain_maps) == 0)) { |
| 419 |
netsnmp_cert_map_container_free(chain_maps); |
| 420 |
return SNMPERR_GENERR; |
| 421 |
} |
| 422 |
|
| 423 |
/* |
| 424 |
* change container to sorted (by clearing unsorted option), then |
| 425 |
* iterate over it until we find a map that returns a secname. |
| 426 |
*/ |
| 427 |
CONTAINER_SET_OPTIONS(chain_maps, 0, rc); |
| 428 |
itr = CONTAINER_ITERATOR(chain_maps); |
| 429 |
if (NULL == itr) { |
| 430 |
snmp_log(LOG_ERR, "could not get iterator for secname fingerprints\n"); |
| 431 |
netsnmp_cert_map_container_free(chain_maps); |
| 432 |
return SNMPERR_GENERR; |
| 433 |
} |
| 434 |
peer_cert = cert_map = ITERATOR_FIRST(itr); |
| 435 |
for( ; !tlsdata->securityName && cert_map; cert_map = ITERATOR_NEXT(itr)) |
| 436 |
tlsdata->securityName = |
| 437 |
netsnmp_openssl_extract_secname(cert_map, peer_cert); |
| 438 |
ITERATOR_RELEASE(itr); |
| 439 |
|
| 440 |
netsnmp_cert_map_container_free(chain_maps); |
| 441 |
|
| 442 |
return (tlsdata->securityName ? SNMPERR_SUCCESS : SNMPERR_GENERR); |
| 443 |
} |
| 444 |
|
| 445 |
int |
| 446 |
_trust_this_cert(SSL_CTX *the_ctx, char *certspec) { |
| 447 |
netsnmp_cert *trustcert; |
| 448 |
|
| 449 |
DEBUGMSGTL(("sslctx_client", "Trying to load a trusted certificate: %s\n", |
| 450 |
certspec)); |
| 451 |
|
| 452 |
/* load this identifier into the trust chain */ |
| 453 |
trustcert = netsnmp_cert_find(NS_CERT_CA, |
| 454 |
NS_CERTKEY_MULTIPLE, |
| 455 |
certspec); |
| 456 |
if (!trustcert) |
| 457 |
trustcert = netsnmp_cert_find(NS_CERT_REMOTE_PEER, |
| 458 |
NS_CERTKEY_MULTIPLE, |
| 459 |
certspec); |
| 460 |
if (!trustcert) |
| 461 |
LOGANDDIE("failed to find requested certificate to trust"); |
| 462 |
|
| 463 |
/* Add the certificate to the context */ |
| 464 |
if (netsnmp_cert_trust(the_ctx, trustcert) != SNMPERR_SUCCESS) |
| 465 |
LOGANDDIE("failed to load trust certificate"); |
| 466 |
|
| 467 |
return 1; |
| 468 |
} |
| 469 |
|
| 470 |
void |
| 471 |
_load_trusted_certs(SSL_CTX *the_ctx) { |
| 472 |
netsnmp_container *trusted_certs = NULL; |
| 473 |
netsnmp_iterator *trusted_cert_iterator = NULL; |
| 474 |
char *fingerprint; |
| 475 |
|
| 476 |
trusted_certs = netsnmp_cert_get_trustlist(); |
| 477 |
trusted_cert_iterator = CONTAINER_ITERATOR(trusted_certs); |
| 478 |
if (trusted_cert_iterator) { |
| 479 |
for (fingerprint = (char *) ITERATOR_FIRST(trusted_cert_iterator); |
| 480 |
fingerprint; fingerprint = ITERATOR_NEXT(trusted_cert_iterator)) { |
| 481 |
if (!_trust_this_cert(the_ctx, fingerprint)) |
| 482 |
snmp_log(LOG_ERR, "failed to load trust cert: %s\n", |
| 483 |
fingerprint); |
| 484 |
} |
| 485 |
ITERATOR_RELEASE(trusted_cert_iterator); |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
SSL_CTX * |
| 490 |
_sslctx_common_setup(SSL_CTX *the_ctx, _netsnmpTLSBaseData *tlsbase) { |
| 491 |
char *crlFile; |
| 492 |
char *cipherList; |
| 493 |
X509_LOOKUP *lookup; |
| 494 |
X509_STORE *cert_store = NULL; |
| 495 |
|
| 496 |
_load_trusted_certs(the_ctx); |
| 497 |
|
| 498 |
/* add in the CRLs if available */ |
| 499 |
|
| 500 |
crlFile = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, |
| 501 |
NETSNMP_DS_LIB_X509_CRL_FILE); |
| 502 |
if (NULL != crlFile) { |
| 503 |
cert_store = SSL_CTX_get_cert_store(the_ctx); |
| 504 |
DEBUGMSGTL(("sslctx_common", "loading CRL: %s\n", crlFile)); |
| 505 |
if (!cert_store) |
| 506 |
LOGANDDIE("failed to find certificate store"); |
| 507 |
if (!(lookup = X509_STORE_add_lookup(cert_store, X509_LOOKUP_file()))) |
| 508 |
LOGANDDIE("failed to create a lookup function for the CRL file"); |
| 509 |
if (X509_load_crl_file(lookup, crlFile, X509_FILETYPE_PEM) != 1) |
| 510 |
LOGANDDIE("failed to load the CRL file"); |
| 511 |
/* tell openssl to check CRLs */ |
| 512 |
X509_STORE_set_flags(cert_store, |
| 513 |
X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL); |
| 514 |
} |
| 515 |
|
| 516 |
cipherList = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, |
| 517 |
NETSNMP_DS_LIB_TLS_ALGORITMS); |
| 518 |
if (NULL != cipherList) { |
| 519 |
if (SSL_CTX_set_cipher_list(the_ctx, cipherList) != 1) |
| 520 |
LOGANDDIE("failed to set the cipher list to the requested value"); |
| 521 |
else |
| 522 |
snmp_log(LOG_INFO,"set SSL cipher list to '%s'\n", cipherList); |
| 523 |
} |
| 524 |
return the_ctx; |
| 525 |
} |
| 526 |
|
| 527 |
SSL_CTX * |
| 528 |
sslctx_client_setup(const SSL_METHOD *method, _netsnmpTLSBaseData *tlsbase) { |
| 529 |
netsnmp_cert *id_cert, *peer_cert; |
| 530 |
SSL_CTX *the_ctx; |
| 531 |
X509 *ocert; |
| 532 |
const char *msg; |
| 533 |
|
| 534 |
/*********************************************************************** |
| 535 |
* Set up the client context |
| 536 |
*/ |
| 537 |
the_ctx = SSL_CTX_new(NETSNMP_REMOVE_CONST(SSL_METHOD *, method)); |
| 538 |
if (!the_ctx) { |
| 539 |
snmp_log(LOG_ERR, "ack: %p\n", the_ctx); |
| 540 |
msg = "can't create a new context"; |
| 541 |
goto err; |
| 542 |
} |
| 543 |
MAKE_MEM_DEFINED(the_ctx, 256/*sizeof(*the_ctx)*/); |
| 544 |
SSL_CTX_set_read_ahead (the_ctx, 1); /* Required for DTLS */ |
| 545 |
|
| 546 |
SSL_CTX_set_verify(the_ctx, |
| 547 |
SSL_VERIFY_PEER| |
| 548 |
SSL_VERIFY_FAIL_IF_NO_PEER_CERT| |
| 549 |
SSL_VERIFY_CLIENT_ONCE, |
| 550 |
&verify_callback); |
| 551 |
|
| 552 |
if (tlsbase->our_identity) { |
| 553 |
DEBUGMSGTL(("sslctx_client", "looking for local id: %s\n", tlsbase->our_identity)); |
| 554 |
id_cert = netsnmp_cert_find(NS_CERT_IDENTITY, NS_CERTKEY_MULTIPLE, |
| 555 |
tlsbase->our_identity); |
| 556 |
} else { |
| 557 |
DEBUGMSGTL(("sslctx_client", "looking for default local id: %s\n", tlsbase->our_identity)); |
| 558 |
id_cert = netsnmp_cert_find(NS_CERT_IDENTITY, NS_CERTKEY_DEFAULT, NULL); |
| 559 |
} |
| 560 |
|
| 561 |
if (!id_cert) { |
| 562 |
msg = "error finding client identity keys"; |
| 563 |
goto err; |
| 564 |
} |
| 565 |
|
| 566 |
if (!id_cert->key || !id_cert->key->okey) { |
| 567 |
msg = "failed to load private key"; |
| 568 |
goto err; |
| 569 |
} |
| 570 |
|
| 571 |
DEBUGMSGTL(("sslctx_client", "using public key: %s\n", |
| 572 |
id_cert->info.filename)); |
| 573 |
DEBUGMSGTL(("sslctx_client", "using private key: %s\n", |
| 574 |
id_cert->key->info.filename)); |
| 575 |
|
| 576 |
if (SSL_CTX_use_certificate(the_ctx, id_cert->ocert) <= 0) { |
| 577 |
msg = "failed to set the client certificate to use"; |
| 578 |
goto err; |
| 579 |
} |
| 580 |
|
| 581 |
if (SSL_CTX_use_PrivateKey(the_ctx, id_cert->key->okey) <= 0) { |
| 582 |
msg = "failed to set the client private key to use"; |
| 583 |
goto err; |
| 584 |
} |
| 585 |
|
| 586 |
if (!SSL_CTX_check_private_key(the_ctx)) { |
| 587 |
msg = "client public and private keys incompatible"; |
| 588 |
goto err; |
| 589 |
} |
| 590 |
|
| 591 |
while (id_cert->issuer_cert) { |
| 592 |
id_cert = id_cert->issuer_cert; |
| 593 |
if (id_cert->ocert) |
| 594 |
{ |
| 595 |
ocert = X509_dup(id_cert->ocert); |
| 596 |
DEBUGMSGTL(("sslctx_client", "adding cert-chain certificate %p", ocert)); |
| 597 |
if (!ocert || !SSL_CTX_add_extra_chain_cert(the_ctx, ocert)) { |
| 598 |
msg = "failed to add intermediate client certificate"; |
| 599 |
goto err; |
| 600 |
} |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
if (tlsbase->their_identity) |
| 605 |
peer_cert = netsnmp_cert_find(NS_CERT_REMOTE_PEER, |
| 606 |
NS_CERTKEY_MULTIPLE, |
| 607 |
tlsbase->their_identity); |
| 608 |
else |
| 609 |
peer_cert = netsnmp_cert_find(NS_CERT_REMOTE_PEER, NS_CERTKEY_DEFAULT, |
| 610 |
NULL); |
| 611 |
if (peer_cert) { |
| 612 |
DEBUGMSGTL(("sslctx_client", "server's expected public key: %s\n", |
| 613 |
peer_cert ? peer_cert->info.filename : "none")); |
| 614 |
|
| 615 |
/* Trust the expected certificate */ |
| 616 |
if (netsnmp_cert_trust(the_ctx, peer_cert) != SNMPERR_SUCCESS) { |
| 617 |
msg = "failed to set verify paths"; |
| 618 |
goto err; |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
/* trust a certificate (possibly a CA) specifically passed in */ |
| 623 |
if (tlsbase->trust_cert) { |
| 624 |
if (!_trust_this_cert(the_ctx, tlsbase->trust_cert)) { |
| 625 |
SSL_CTX_free(the_ctx); |
| 626 |
return 0; |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
return _sslctx_common_setup(the_ctx, tlsbase); |
| 631 |
|
| 632 |
err: |
| 633 |
snmp_log(LOG_ERR, "%s\n", msg); |
| 634 |
SSL_CTX_free(the_ctx); |
| 635 |
return 0; |
| 636 |
} |
| 637 |
|
| 638 |
SSL_CTX * |
| 639 |
sslctx_server_setup(const SSL_METHOD *method) { |
| 640 |
netsnmp_cert *id_cert; |
| 641 |
X509 *ocert; |
| 642 |
const char *msg; |
| 643 |
|
| 644 |
/*********************************************************************** |
| 645 |
* Set up the server context |
| 646 |
*/ |
| 647 |
/* setting up for ssl */ |
| 648 |
SSL_CTX *the_ctx = SSL_CTX_new(NETSNMP_REMOVE_CONST(SSL_METHOD *, method)); |
| 649 |
if (!the_ctx) { |
| 650 |
msg = "can't create a new server context"; |
| 651 |
goto err; |
| 652 |
} |
| 653 |
MAKE_MEM_DEFINED(the_ctx, 256/*sizeof(*the_ctx)*/); |
| 654 |
|
| 655 |
id_cert = netsnmp_cert_find(NS_CERT_IDENTITY, NS_CERTKEY_DEFAULT, NULL); |
| 656 |
if (!id_cert) { |
| 657 |
msg = "error finding server identity keys"; |
| 658 |
goto err; |
| 659 |
} |
| 660 |
|
| 661 |
if (!id_cert->key || !id_cert->key->okey) { |
| 662 |
msg = "failed to load server private key"; |
| 663 |
goto err; |
| 664 |
} |
| 665 |
|
| 666 |
DEBUGMSGTL(("sslctx_server", "using public key: %s\n", |
| 667 |
id_cert->info.filename)); |
| 668 |
DEBUGMSGTL(("sslctx_server", "using private key: %s\n", |
| 669 |
id_cert->key->info.filename)); |
| 670 |
|
| 671 |
if (SSL_CTX_use_certificate(the_ctx, id_cert->ocert) <= 0) { |
| 672 |
msg = "failed to set the server certificate to use"; |
| 673 |
goto err; |
| 674 |
} |
| 675 |
|
| 676 |
if (SSL_CTX_use_PrivateKey(the_ctx, id_cert->key->okey) <= 0) { |
| 677 |
msg = "failed to set the server private key to use"; |
| 678 |
goto err; |
| 679 |
} |
| 680 |
|
| 681 |
if (!SSL_CTX_check_private_key(the_ctx)) { |
| 682 |
msg ="server public and private keys incompatible"; |
| 683 |
goto err; |
| 684 |
} |
| 685 |
|
| 686 |
while (id_cert->issuer_cert) { |
| 687 |
id_cert = id_cert->issuer_cert; |
| 688 |
if (id_cert->ocert) |
| 689 |
{ |
| 690 |
ocert = X509_dup(id_cert->ocert); |
| 691 |
DEBUGMSGTL(("sslctx_server", "adding cert-chain certificate %p", ocert)); |
| 692 |
if (!ocert || !SSL_CTX_add_extra_chain_cert(the_ctx, ocert)) { |
| 693 |
msg = "failed to add intermediate server certificate"; |
| 694 |
goto err; |
| 695 |
} |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
SSL_CTX_set_read_ahead(the_ctx, 1); /* XXX: DTLS only? */ |
| 700 |
|
| 701 |
SSL_CTX_set_verify(the_ctx, |
| 702 |
SSL_VERIFY_PEER| |
| 703 |
SSL_VERIFY_FAIL_IF_NO_PEER_CERT| |
| 704 |
SSL_VERIFY_CLIENT_ONCE, |
| 705 |
&verify_callback); |
| 706 |
|
| 707 |
SSL_CTX_set_options(the_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE); |
| 708 |
|
| 709 |
return _sslctx_common_setup(the_ctx, NULL); |
| 710 |
|
| 711 |
err: |
| 712 |
snmp_log(LOG_ERR, "%s\n", msg); |
| 713 |
SSL_CTX_free(the_ctx); |
| 714 |
return NULL; |
| 715 |
} |
| 716 |
|
| 717 |
int |
| 718 |
netsnmp_tlsbase_config(struct netsnmp_transport_s *t, const char *token, const char *value) { |
| 719 |
_netsnmpTLSBaseData *tlsdata; |
| 720 |
|
| 721 |
netsnmp_assert_or_return(t != NULL, -1); |
| 722 |
netsnmp_assert_or_return(t->data != NULL, -1); |
| 723 |
|
| 724 |
tlsdata = t->data; |
| 725 |
|
| 726 |
if ((strcmp(token, "localCert") == 0) || |
| 727 |
(strcmp(token, "our_identity") == 0)) { |
| 728 |
SNMP_FREE(tlsdata->our_identity); |
| 729 |
tlsdata->our_identity = strdup(value); |
| 730 |
DEBUGMSGT(("tls:config","our identity %s\n", value)); |
| 731 |
} |
| 732 |
|
| 733 |
if ((strcmp(token, "peerCert") == 0) || |
| 734 |
(strcmp(token, "their_identity") == 0)) { |
| 735 |
SNMP_FREE(tlsdata->their_identity); |
| 736 |
tlsdata->their_identity = strdup(value); |
| 737 |
DEBUGMSGT(("tls:config","their identity %s\n", value)); |
| 738 |
} |
| 739 |
|
| 740 |
if ((strcmp(token, "peerHostname") == 0) || |
| 741 |
(strcmp(token, "their_hostname") == 0)) { |
| 742 |
SNMP_FREE(tlsdata->their_hostname); |
| 743 |
tlsdata->their_hostname = strdup(value); |
| 744 |
} |
| 745 |
|
| 746 |
if ((strcmp(token, "trust_cert") == 0) || |
| 747 |
(strcmp(token, "trustCert") == 0)) { |
| 748 |
SNMP_FREE(tlsdata->trust_cert); |
| 749 |
tlsdata->trust_cert = strdup(value); |
| 750 |
} |
| 751 |
|
| 752 |
return SNMPERR_SUCCESS; |
| 753 |
} |
| 754 |
|
| 755 |
int |
| 756 |
netsnmp_tlsbase_session_init(struct netsnmp_transport_s *transport, |
| 757 |
struct snmp_session *sess) { |
| 758 |
/* the default security model here should be TSM; most other |
| 759 |
things won't work with TLS because we'll throw out the packet |
| 760 |
if it doesn't have a proper tmStateRef (and onyl TSM generates |
| 761 |
this at the moment */ |
| 762 |
if (!(transport->flags & NETSNMP_TRANSPORT_FLAG_LISTEN)) { |
| 763 |
if (sess->securityModel == SNMP_DEFAULT_SECMODEL) { |
| 764 |
sess->securityModel = SNMP_SEC_MODEL_TSM; |
| 765 |
} else if (sess->securityModel != SNMP_SEC_MODEL_TSM) { |
| 766 |
sess->securityModel = SNMP_SEC_MODEL_TSM; |
| 767 |
snmp_log(LOG_ERR, "A security model other than TSM is being used with (D)TLS; using TSM anyways\n"); |
| 768 |
} |
| 769 |
|
| 770 |
if (NULL == sess->securityName) { |
| 771 |
/* client side doesn't need a real securityName */ |
| 772 |
/* XXX: call-home issues require them to set one for VACM; but |
| 773 |
we don't do callhome yet */ |
| 774 |
sess->securityName = strdup("__BOGUS__"); |
| 775 |
sess->securityNameLen = strlen(sess->securityName); |
| 776 |
} |
| 777 |
|
| 778 |
if (sess->version != SNMP_VERSION_3) { |
| 779 |
sess->version = SNMP_VERSION_3; |
| 780 |
snmp_log(LOG_ERR, "A SNMP version other than 3 was requested with (D)TLS; using 3 anyways\n"); |
| 781 |
} |
| 782 |
|
| 783 |
if (sess->securityLevel <= 0) { |
| 784 |
sess->securityLevel = SNMP_SEC_LEVEL_AUTHPRIV; |
| 785 |
} |
| 786 |
} |
| 787 |
return SNMPERR_SUCCESS; |
| 788 |
} |
| 789 |
|
| 790 |
static int have_done_bootstrap = 0; |
| 791 |
|
| 792 |
static int |
| 793 |
tls_bootstrap(int majorid, int minorid, void *serverarg, void *clientarg) { |
| 794 |
char indexname[] = "_netsnmp_verify_info"; |
| 795 |
|
| 796 |
/* don't do this more than once */ |
| 797 |
if (have_done_bootstrap) |
| 798 |
return 0; |
| 799 |
have_done_bootstrap = 1; |
| 800 |
|
| 801 |
netsnmp_certs_load(); |
| 802 |
|
| 803 |
openssl_local_index = |
| 804 |
SSL_get_ex_new_index(0, indexname, NULL, NULL, NULL); |
| 805 |
|
| 806 |
return 0; |
| 807 |
} |
| 808 |
|
| 809 |
int |
| 810 |
tls_get_verify_info_index(void) { |
| 811 |
return openssl_local_index; |
| 812 |
} |
| 813 |
|
| 814 |
static void _parse_client_cert(const char *tok, char *line) |
| 815 |
{ |
| 816 |
config_pwarn("clientCert is deprecated. Clients should use localCert, servers should use peerCert"); |
| 817 |
if (*line == '"') { |
| 818 |
char buf[SNMP_MAXBUF]; |
| 819 |
copy_nword(line, buf, sizeof(buf)); |
| 820 |
netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID, |
| 821 |
NETSNMP_DS_LIB_X509_CLIENT_PUB, buf); |
| 822 |
} else |
| 823 |
netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID, |
| 824 |
NETSNMP_DS_LIB_X509_CLIENT_PUB, line); |
| 825 |
} |
| 826 |
|
| 827 |
static void _parse_server_cert(const char *tok, char *line) |
| 828 |
{ |
| 829 |
config_pwarn("serverCert is deprecated. Clients should use peerCert, servers should use localCert."); |
| 830 |
if (*line == '"') { |
| 831 |
char buf[SNMP_MAXBUF]; |
| 832 |
copy_nword(line, buf, sizeof(buf)); |
| 833 |
netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID, |
| 834 |
NETSNMP_DS_LIB_X509_CLIENT_PUB, buf); |
| 835 |
} else |
| 836 |
netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID, |
| 837 |
NETSNMP_DS_LIB_X509_SERVER_PUB, line); |
| 838 |
} |
| 839 |
|
| 840 |
void |
| 841 |
netsnmp_tlsbase_ctor(void) { |
| 842 |
|
| 843 |
/* bootstrap ssl since we'll need it */ |
| 844 |
netsnmp_init_openssl(); |
| 845 |
|
| 846 |
/* the private client cert to authenticate with */ |
| 847 |
netsnmp_ds_register_config(ASN_OCTET_STR, "snmp", "extraX509SubDir", |
| 848 |
NETSNMP_DS_LIBRARY_ID, |
| 849 |
NETSNMP_DS_LIB_CERT_EXTRA_SUBDIR); |
| 850 |
|
| 851 |
/* Do we have a CRL list? */ |
| 852 |
netsnmp_ds_register_config(ASN_OCTET_STR, "snmp", "x509CRLFile", |
| 853 |
NETSNMP_DS_LIBRARY_ID, |
| 854 |
NETSNMP_DS_LIB_X509_CRL_FILE); |
| 855 |
|
| 856 |
/* What TLS algorithms should be use */ |
| 857 |
netsnmp_ds_register_config(ASN_OCTET_STR, "snmp", "tlsAlgorithms", |
| 858 |
NETSNMP_DS_LIBRARY_ID, |
| 859 |
NETSNMP_DS_LIB_TLS_ALGORITMS); |
| 860 |
|
| 861 |
/* |
| 862 |
* for the client |
| 863 |
*/ |
| 864 |
|
| 865 |
/* the public client cert to authenticate with */ |
| 866 |
register_config_handler("snmp", "clientCert", _parse_client_cert, NULL, |
| 867 |
NULL); |
| 868 |
|
| 869 |
/* |
| 870 |
* for the server |
| 871 |
*/ |
| 872 |
|
| 873 |
/* The X509 server key to use */ |
| 874 |
register_config_handler("snmp", "serverCert", _parse_server_cert, NULL, |
| 875 |
NULL); |
| 876 |
/* |
| 877 |
* remove cert config ambiguity: localCert, peerCert |
| 878 |
*/ |
| 879 |
netsnmp_ds_register_config(ASN_OCTET_STR, "snmp", "localCert", |
| 880 |
NETSNMP_DS_LIBRARY_ID, |
| 881 |
NETSNMP_DS_LIB_TLS_LOCAL_CERT); |
| 882 |
netsnmp_ds_register_config(ASN_OCTET_STR, "snmp", "peerCert", |
| 883 |
NETSNMP_DS_LIBRARY_ID, |
| 884 |
NETSNMP_DS_LIB_TLS_PEER_CERT); |
| 885 |
|
| 886 |
/* |
| 887 |
* register our boot-strapping needs |
| 888 |
*/ |
| 889 |
snmp_register_callback(SNMP_CALLBACK_LIBRARY, |
| 890 |
SNMP_CALLBACK_POST_PREMIB_READ_CONFIG, |
| 891 |
tls_bootstrap, NULL); |
| 892 |
|
| 893 |
} |
| 894 |
|
| 895 |
_netsnmpTLSBaseData * |
| 896 |
netsnmp_tlsbase_allocate_tlsdata(netsnmp_transport *t, int isserver) { |
| 897 |
|
| 898 |
_netsnmpTLSBaseData *tlsdata; |
| 899 |
|
| 900 |
if (NULL == t) |
| 901 |
return NULL; |
| 902 |
|
| 903 |
/* allocate our TLS specific data */ |
| 904 |
tlsdata = SNMP_MALLOC_TYPEDEF(_netsnmpTLSBaseData); |
| 905 |
if (NULL == tlsdata) { |
| 906 |
SNMP_FREE(t); |
| 907 |
return NULL; |
| 908 |
} |
| 909 |
|
| 910 |
if (!isserver) |
| 911 |
tlsdata->flags |= NETSNMP_TLSBASE_IS_CLIENT; |
| 912 |
|
| 913 |
return tlsdata; |
| 914 |
} |
| 915 |
|
| 916 |
void netsnmp_tlsbase_free_tlsdata(_netsnmpTLSBaseData *tlsbase) { |
| 917 |
if (!tlsbase) |
| 918 |
return; |
| 919 |
|
| 920 |
DEBUGMSGTL(("tlsbase","Freeing TLS Base data for a session\n")); |
| 921 |
|
| 922 |
if (tlsbase->ssl) |
| 923 |
SSL_free(tlsbase->ssl); |
| 924 |
|
| 925 |
if (tlsbase->ssl_context) |
| 926 |
SSL_CTX_free(tlsbase->ssl_context); |
| 927 |
/* don't free the accept_bio since it's the parent bio */ |
| 928 |
/* |
| 929 |
if (tlsbase->accept_bio) |
| 930 |
BIO_free(tlsbase->accept_bio); |
| 931 |
*/ |
| 932 |
/* and this is freed by the SSL shutdown */ |
| 933 |
/* |
| 934 |
if (tlsbase->accepted_bio) |
| 935 |
BIO_free(tlsbase->accept_bio); |
| 936 |
*/ |
| 937 |
|
| 938 |
/* free the config data */ |
| 939 |
SNMP_FREE(tlsbase->securityName); |
| 940 |
SNMP_FREE(tlsbase->addr_string); |
| 941 |
SNMP_FREE(tlsbase->our_identity); |
| 942 |
SNMP_FREE(tlsbase->their_identity); |
| 943 |
SNMP_FREE(tlsbase->their_fingerprint); |
| 944 |
SNMP_FREE(tlsbase->their_hostname); |
| 945 |
SNMP_FREE(tlsbase->trust_cert); |
| 946 |
|
| 947 |
/* free the base itself */ |
| 948 |
SNMP_FREE(tlsbase); |
| 949 |
} |
| 950 |
|
| 951 |
int netsnmp_tlsbase_wrapup_recv(netsnmp_tmStateReference *tmStateRef, |
| 952 |
_netsnmpTLSBaseData *tlsdata, |
| 953 |
void **opaque, int *olength) { |
| 954 |
int no_auth, no_priv; |
| 955 |
|
| 956 |
if (NULL == tlsdata) |
| 957 |
return SNMPERR_GENERR; |
| 958 |
|
| 959 |
/* RFC5953 Section 5.1.2 step 2: tmSecurityLevel */ |
| 960 |
/* |
| 961 |
* Don't accept null authentication. Null encryption ok. |
| 962 |
* |
| 963 |
* XXX: this should actually check for a configured list of encryption |
| 964 |
* algorithms to map to NOPRIV, but for the moment we'll |
| 965 |
* accept any encryption alogrithms that openssl is using. |
| 966 |
*/ |
| 967 |
netsnmp_openssl_null_checks(tlsdata->ssl, &no_auth, &no_priv); |
| 968 |
if (no_auth == 1) { /* null/unknown authentication */ |
| 969 |
/* xxx-rks: snmp_increment_statistic(STAT_???); */ |
| 970 |
snmp_log(LOG_ERR, "tls connection with NULL authentication\n"); |
| 971 |
SNMP_FREE(tmStateRef); |
| 972 |
return SNMPERR_GENERR; |
| 973 |
} |
| 974 |
else if (no_priv == 1) /* null/unknown encryption */ |
| 975 |
tmStateRef->transportSecurityLevel = SNMP_SEC_LEVEL_AUTHNOPRIV; |
| 976 |
else |
| 977 |
tmStateRef->transportSecurityLevel = SNMP_SEC_LEVEL_AUTHPRIV; |
| 978 |
DEBUGMSGTL(("tls:secLevel", "SecLevel %d\n", |
| 979 |
tmStateRef->transportSecurityLevel)); |
| 980 |
|
| 981 |
/* use x509 cert to do lookup to secname if DNE in cachep yet */ |
| 982 |
|
| 983 |
/* RFC5953: section 5.3.2, paragraph 2: |
| 984 |
The (D)TLS server identifies the authenticated identity from the |
| 985 |
(D)TLS client's principal certificate using configuration information |
| 986 |
from the snmpTlstmCertToTSNTable mapping table. The (D)TLS server |
| 987 |
MUST request and expect a certificate from the client and MUST NOT |
| 988 |
accept SNMP messages over the (D)TLS connection until the client has |
| 989 |
sent a certificate and it has been authenticated. The resulting |
| 990 |
derived tmSecurityName is recorded in the tmStateReference cache as |
| 991 |
tmSecurityName. The details of the lookup process are fully |
| 992 |
described in the DESCRIPTION clause of the snmpTlstmCertToTSNTable |
| 993 |
MIB object. If any verification fails in any way (for example |
| 994 |
because of failures in cryptographic verification or because of the |
| 995 |
lack of an appropriate row in the snmpTlstmCertToTSNTable) then the |
| 996 |
session establishment MUST fail, and the |
| 997 |
snmpTlstmSessionInvalidClientCertificates object is incremented. If |
| 998 |
the session can not be opened for any reason at all, including |
| 999 |
cryptographic verification failures, then the |
| 1000 |
snmpTlstmSessionOpenErrors counter is incremented and processing |
| 1001 |
stops. |
| 1002 |
*/ |
| 1003 |
|
| 1004 |
if (!tlsdata->securityName) { |
| 1005 |
netsnmp_tlsbase_extract_security_name(tlsdata->ssl, tlsdata); |
| 1006 |
if (NULL != tlsdata->securityName) { |
| 1007 |
DEBUGMSGTL(("tls", "set SecName to: %s\n", tlsdata->securityName)); |
| 1008 |
} else { |
| 1009 |
snmp_increment_statistic(STAT_TLSTM_SNMPTLSTMSESSIONINVALIDCLIENTCERTIFICATES); |
| 1010 |
snmp_increment_statistic(STAT_TLSTM_SNMPTLSTMSESSIONOPENERRORS); |
| 1011 |
SNMP_FREE(tmStateRef); |
| 1012 |
return SNMPERR_GENERR; |
| 1013 |
} |
| 1014 |
} |
| 1015 |
|
| 1016 |
/* RFC5953 Section 5.1.2 step 2: tmSecurityName */ |
| 1017 |
/* XXX: detect and throw out overflow secname sizes rather |
| 1018 |
than truncating. */ |
| 1019 |
strlcpy(tmStateRef->securityName, tlsdata->securityName, |
| 1020 |
sizeof(tmStateRef->securityName)); |
| 1021 |
tmStateRef->securityNameLen = strlen(tmStateRef->securityName); |
| 1022 |
|
| 1023 |
/* RFC5953 Section 5.1.2 step 2: tmSessionID */ |
| 1024 |
/* use our TLSData pointer as the session ID */ |
| 1025 |
memcpy(tmStateRef->sessionID, &tlsdata, sizeof(netsnmp_tmStateReference *)); |
| 1026 |
|
| 1027 |
/* save the tmStateRef in our special pointer */ |
| 1028 |
*opaque = tmStateRef; |
| 1029 |
*olength = sizeof(netsnmp_tmStateReference); |
| 1030 |
|
| 1031 |
return SNMPERR_SUCCESS; |
| 1032 |
} |
| 1033 |
|
| 1034 |
netsnmp_feature_child_of(_x509_get_error, netsnmp_unused); |
| 1035 |
#ifndef NETSNMP_FEATURE_REMOVE__X509_GET_ERROR |
| 1036 |
const char * _x509_get_error(int x509failvalue, const char *location) { |
| 1037 |
static const char *reason = NULL; |
| 1038 |
|
| 1039 |
/* XXX: use this instead: X509_verify_cert_error_string(err) */ |
| 1040 |
|
| 1041 |
switch (x509failvalue) { |
| 1042 |
case X509_V_OK: |
| 1043 |
reason = "X509_V_OK"; |
| 1044 |
break; |
| 1045 |
case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT: |
| 1046 |
reason = "X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT"; |
| 1047 |
break; |
| 1048 |
case X509_V_ERR_UNABLE_TO_GET_CRL: |
| 1049 |
reason = "X509_V_ERR_UNABLE_TO_GET_CRL"; |
| 1050 |
break; |
| 1051 |
case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE: |
| 1052 |
reason = "X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE"; |
| 1053 |
break; |
| 1054 |
case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE: |
| 1055 |
reason = "X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE"; |
| 1056 |
break; |
| 1057 |
case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY: |
| 1058 |
reason = "X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY"; |
| 1059 |
break; |
| 1060 |
case X509_V_ERR_CERT_SIGNATURE_FAILURE: |
| 1061 |
reason = "X509_V_ERR_CERT_SIGNATURE_FAILURE"; |
| 1062 |
break; |
| 1063 |
case X509_V_ERR_CRL_SIGNATURE_FAILURE: |
| 1064 |
reason = "X509_V_ERR_CRL_SIGNATURE_FAILURE"; |
| 1065 |
break; |
| 1066 |
case X509_V_ERR_CERT_NOT_YET_VALID: |
| 1067 |
reason = "X509_V_ERR_CERT_NOT_YET_VALID"; |
| 1068 |
break; |
| 1069 |
case X509_V_ERR_CERT_HAS_EXPIRED: |
| 1070 |
reason = "X509_V_ERR_CERT_HAS_EXPIRED"; |
| 1071 |
break; |
| 1072 |
case X509_V_ERR_CRL_NOT_YET_VALID: |
| 1073 |
reason = "X509_V_ERR_CRL_NOT_YET_VALID"; |
| 1074 |
break; |
| 1075 |
case X509_V_ERR_CRL_HAS_EXPIRED: |
| 1076 |
reason = "X509_V_ERR_CRL_HAS_EXPIRED"; |
| 1077 |
break; |
| 1078 |
case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD: |
| 1079 |
reason = "X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD"; |
| 1080 |
break; |
| 1081 |
case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD: |
| 1082 |
reason = "X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD"; |
| 1083 |
break; |
| 1084 |
case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD: |
| 1085 |
reason = "X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD"; |
| 1086 |
break; |
| 1087 |
case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD: |
| 1088 |
reason = "X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD"; |
| 1089 |
break; |
| 1090 |
case X509_V_ERR_OUT_OF_MEM: |
| 1091 |
reason = "X509_V_ERR_OUT_OF_MEM"; |
| 1092 |
break; |
| 1093 |
case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: |
| 1094 |
reason = "X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT"; |
| 1095 |
break; |
| 1096 |
case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN: |
| 1097 |
reason = "X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN"; |
| 1098 |
break; |
| 1099 |
case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY: |
| 1100 |
reason = "X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY"; |
| 1101 |
break; |
| 1102 |
case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE: |
| 1103 |
reason = "X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE"; |
| 1104 |
break; |
| 1105 |
case X509_V_ERR_CERT_CHAIN_TOO_LONG: |
| 1106 |
reason = "X509_V_ERR_CERT_CHAIN_TOO_LONG"; |
| 1107 |
break; |
| 1108 |
case X509_V_ERR_CERT_REVOKED: |
| 1109 |
reason = "X509_V_ERR_CERT_REVOKED"; |
| 1110 |
break; |
| 1111 |
case X509_V_ERR_INVALID_CA: |
| 1112 |
reason = "X509_V_ERR_INVALID_CA"; |
| 1113 |
break; |
| 1114 |
case X509_V_ERR_PATH_LENGTH_EXCEEDED: |
| 1115 |
reason = "X509_V_ERR_PATH_LENGTH_EXCEEDED"; |
| 1116 |
break; |
| 1117 |
case X509_V_ERR_INVALID_PURPOSE: |
| 1118 |
reason = "X509_V_ERR_INVALID_PURPOSE"; |
| 1119 |
break; |
| 1120 |
case X509_V_ERR_CERT_UNTRUSTED: |
| 1121 |
reason = "X509_V_ERR_CERT_UNTRUSTED"; |
| 1122 |
break; |
| 1123 |
case X509_V_ERR_CERT_REJECTED: |
| 1124 |
reason = "X509_V_ERR_CERT_REJECTED"; |
| 1125 |
break; |
| 1126 |
case X509_V_ERR_SUBJECT_ISSUER_MISMATCH: |
| 1127 |
reason = "X509_V_ERR_SUBJECT_ISSUER_MISMATCH"; |
| 1128 |
break; |
| 1129 |
case X509_V_ERR_AKID_SKID_MISMATCH: |
| 1130 |
reason = "X509_V_ERR_AKID_SKID_MISMATCH"; |
| 1131 |
break; |
| 1132 |
case X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH: |
| 1133 |
reason = "X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH"; |
| 1134 |
break; |
| 1135 |
case X509_V_ERR_KEYUSAGE_NO_CERTSIGN: |
| 1136 |
reason = "X509_V_ERR_KEYUSAGE_NO_CERTSIGN"; |
| 1137 |
break; |
| 1138 |
case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER: |
| 1139 |
reason = "X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER"; |
| 1140 |
break; |
| 1141 |
case X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION: |
| 1142 |
reason = "X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION"; |
| 1143 |
break; |
| 1144 |
case X509_V_ERR_KEYUSAGE_NO_CRL_SIGN: |
| 1145 |
reason = "X509_V_ERR_KEYUSAGE_NO_CRL_SIGN"; |
| 1146 |
break; |
| 1147 |
case X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION: |
| 1148 |
reason = "X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION"; |
| 1149 |
break; |
| 1150 |
case X509_V_ERR_INVALID_NON_CA: |
| 1151 |
reason = "X509_V_ERR_INVALID_NON_CA"; |
| 1152 |
break; |
| 1153 |
case X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED: |
| 1154 |
reason = "X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED"; |
| 1155 |
break; |
| 1156 |
case X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE: |
| 1157 |
reason = "X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE"; |
| 1158 |
break; |
| 1159 |
case X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED: |
| 1160 |
reason = "X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED"; |
| 1161 |
break; |
| 1162 |
#ifdef X509_V_ERR_INVALID_EXTENSION /* not avail on darwin */ |
| 1163 |
case X509_V_ERR_INVALID_EXTENSION: |
| 1164 |
reason = "X509_V_ERR_INVALID_EXTENSION"; |
| 1165 |
break; |
| 1166 |
#endif |
| 1167 |
#ifdef X509_V_ERR_INVALID_POLICY_EXTENSION /* not avail on darwin */ |
| 1168 |
case X509_V_ERR_INVALID_POLICY_EXTENSION: |
| 1169 |
reason = "X509_V_ERR_INVALID_POLICY_EXTENSION"; |
| 1170 |
break; |
| 1171 |
#endif |
| 1172 |
#ifdef X509_V_ERR_NO_EXPLICIT_POLICY /* not avail on darwin */ |
| 1173 |
case X509_V_ERR_NO_EXPLICIT_POLICY: |
| 1174 |
reason = "X509_V_ERR_NO_EXPLICIT_POLICY"; |
| 1175 |
break; |
| 1176 |
#endif |
| 1177 |
#ifdef X509_V_ERR_UNNESTED_RESOURCE /* not avail on darwin */ |
| 1178 |
case X509_V_ERR_UNNESTED_RESOURCE: |
| 1179 |
reason = "X509_V_ERR_UNNESTED_RESOURCE"; |
| 1180 |
break; |
| 1181 |
#endif |
| 1182 |
case X509_V_ERR_APPLICATION_VERIFICATION: |
| 1183 |
reason = "X509_V_ERR_APPLICATION_VERIFICATION"; |
| 1184 |
break; |
| 1185 |
default: |
| 1186 |
reason = "unknown failure code"; |
| 1187 |
} |
| 1188 |
|
| 1189 |
return reason; |
| 1190 |
} |
| 1191 |
#endif /* NETSNMP_FEATURE_REMOVE__X509_GET_ERROR */ |
| 1192 |
|
| 1193 |
void _openssl_log_error(int rc, SSL *con, const char *location) { |
| 1194 |
const char *reason, *file, *func, *data; |
| 1195 |
unsigned long numerical_reason; |
| 1196 |
int flags, line; |
| 1197 |
|
| 1198 |
snmp_log(LOG_ERR, "---- OpenSSL Related Errors: ----\n"); |
| 1199 |
|
| 1200 |
/* SSL specific errors */ |
| 1201 |
if (con) { |
| 1202 |
|
| 1203 |
int sslnum = SSL_get_error(con, rc); |
| 1204 |
|
| 1205 |
switch(sslnum) { |
| 1206 |
case SSL_ERROR_NONE: |
| 1207 |
reason = "SSL_ERROR_NONE"; |
| 1208 |
break; |
| 1209 |
|
| 1210 |
case SSL_ERROR_SSL: |
| 1211 |
reason = "SSL_ERROR_SSL"; |
| 1212 |
break; |
| 1213 |
|
| 1214 |
case SSL_ERROR_WANT_READ: |
| 1215 |
reason = "SSL_ERROR_WANT_READ"; |
| 1216 |
break; |
| 1217 |
|
| 1218 |
case SSL_ERROR_WANT_WRITE: |
| 1219 |
reason = "SSL_ERROR_WANT_WRITE"; |
| 1220 |
break; |
| 1221 |
|
| 1222 |
case SSL_ERROR_WANT_X509_LOOKUP: |
| 1223 |
reason = "SSL_ERROR_WANT_X509_LOOKUP"; |
| 1224 |
break; |
| 1225 |
|
| 1226 |
case SSL_ERROR_SYSCALL: |
| 1227 |
reason = "SSL_ERROR_SYSCALL"; |
| 1228 |
snmp_log(LOG_ERR, "TLS error: %s: rc=%d, sslerror = %d (%s): system_error=%d (%s)\n", |
| 1229 |
location, rc, sslnum, reason, errno, strerror(errno)); |
| 1230 |
snmp_log(LOG_ERR, "TLS Error: %s\n", |
| 1231 |
ERR_reason_error_string(ERR_get_error())); |
| 1232 |
return; |
| 1233 |
|
| 1234 |
case SSL_ERROR_ZERO_RETURN: |
| 1235 |
reason = "SSL_ERROR_ZERO_RETURN"; |
| 1236 |
break; |
| 1237 |
|
| 1238 |
case SSL_ERROR_WANT_CONNECT: |
| 1239 |
reason = "SSL_ERROR_WANT_CONNECT"; |
| 1240 |
break; |
| 1241 |
|
| 1242 |
case SSL_ERROR_WANT_ACCEPT: |
| 1243 |
reason = "SSL_ERROR_WANT_ACCEPT"; |
| 1244 |
break; |
| 1245 |
|
| 1246 |
default: |
| 1247 |
reason = "unknown"; |
| 1248 |
} |
| 1249 |
|
| 1250 |
snmp_log(LOG_ERR, " TLS error: %s: rc=%d, sslerror = %d (%s)\n", |
| 1251 |
location, rc, sslnum, reason); |
| 1252 |
|
| 1253 |
snmp_log(LOG_ERR, " TLS Error: %s\n", |
| 1254 |
ERR_reason_error_string(ERR_get_error())); |
| 1255 |
|
| 1256 |
} |
| 1257 |
|
| 1258 |
/* other errors */ |
| 1259 |
while ((numerical_reason = |
| 1260 |
ERR_get_error_all(&file, &line, &func, &data, &flags)) != 0) { |
| 1261 |
snmp_log(LOG_ERR, "%s (file %s, func %s, line %d)\n", |
| 1262 |
ERR_error_string(numerical_reason, NULL), file, func, line); |
| 1263 |
|
| 1264 |
/* if we have a text translation: */ |
| 1265 |
if (data && (flags & ERR_TXT_STRING)) { |
| 1266 |
snmp_log(LOG_ERR, " Textual Error: %s\n", data); |
| 1267 |
} |
| 1268 |
} |
| 1269 |
/* clear openssl error ring buffer */ |
| 1270 |
ERR_clear_error(); |
| 1271 |
|
| 1272 |
snmp_log(LOG_ERR, "---- End of OpenSSL Errors ----\n"); |
| 1273 |
} |