| 1 |
/* nsd_mbedtls.c |
| 2 |
* |
| 3 |
* An implementation of the nsd interface for Mbed TLS. |
| 4 |
* |
| 5 |
* Copyright (C) 2007-2023 Rainer Gerhards and Adiscon GmbH. |
| 6 |
* Copyright (C) 2023 CS Group. |
| 7 |
* |
| 8 |
* This file is part of the rsyslog runtime library. |
| 9 |
* |
| 10 |
* The rsyslog runtime library is free software: you can redistribute it and/or modify |
| 11 |
* it under the terms of the GNU Lesser General Public License as published by |
| 12 |
* the Free Software Foundation, either version 3 of the License, or |
| 13 |
* (at your option) any later version. |
| 14 |
* |
| 15 |
* The rsyslog runtime library is distributed in the hope that it will be useful, |
| 16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
* GNU Lesser General Public License for more details. |
| 19 |
* |
| 20 |
* You should have received a copy of the GNU Lesser General Public License |
| 21 |
* along with the rsyslog runtime library. If not, see <http://www.gnu.org/licenses/>. |
| 22 |
* |
| 23 |
* A copy of the GPL can be found in the file "COPYING" in this distribution. |
| 24 |
* A copy of the LGPL can be found in the file "COPYING.LESSER" in this distribution. |
| 25 |
*/ |
| 26 |
#include "config.h" |
| 27 |
#include <stdio.h> |
| 28 |
#include <stdlib.h> |
| 29 |
#include <assert.h> |
| 30 |
#include <string.h> |
| 31 |
#include <errno.h> |
| 32 |
#include <sys/stat.h> |
| 33 |
#include <unistd.h> |
| 34 |
#include <fcntl.h> |
| 35 |
#include <sys/time.h> |
| 36 |
#include <mbedtls/debug.h> |
| 37 |
#include <mbedtls/error.h> |
| 38 |
#include <mbedtls/oid.h> |
| 39 |
#include <mbedtls/version.h> |
| 40 |
|
| 41 |
#include "rsyslog.h" |
| 42 |
#include "syslogd-types.h" |
| 43 |
#include "module-template.h" |
| 44 |
#include "obj.h" |
| 45 |
#include "nsd_ptcp.h" |
| 46 |
#include "nsd_mbedtls.h" |
| 47 |
#include "rsconf.h" |
| 48 |
#include "net.h" |
| 49 |
|
| 50 |
MODULE_TYPE_LIB |
| 51 |
MODULE_TYPE_KEEP |
| 52 |
|
| 53 |
/* static data */ |
| 54 |
DEFobjStaticHelpers DEFobjCurrIf(glbl) DEFobjCurrIf(net) DEFobjCurrIf(nsd_ptcp) |
| 55 |
/* Mbed TLS debug level (0..5) |
| 56 |
* 5 is the most logs. |
| 57 |
*/ |
| 58 |
#define MBEDTLS_DEBUG_LEVEL 0 |
| 59 |
|
| 60 |
#define DEFAULT_MAX_DEPTH 5 |
| 61 |
|
| 62 |
#if MBEDTLS_DEBUG_LEVEL > 0 |
| 63 |
static void debug(void __attribute__((unused)) * ctx, |
| 64 |
int __attribute__((unused)) level, |
| 65 |
const char *file, |
| 66 |
int line, |
| 67 |
const char *str) { |
| 68 |
dbgprintf("%s:%04d: %s", file, line, str); |
| 69 |
} |
| 70 |
#endif |
| 71 |
|
| 72 |
#define logMbedtlsError(err, mbedtls_err) \ |
| 73 |
do { \ |
| 74 |
char error_buf[128]; \ |
| 75 |
int aux = (mbedtls_err < 0 ? -mbedtls_err : mbedtls_err); \ |
| 76 |
const char *sig = (mbedtls_err < 0 ? "-" : ""); \ |
| 77 |
mbedtls_strerror(mbedtls_err, error_buf, 100); \ |
| 78 |
LogError(0, err, "Mbed TLS Error: %s0x%04X - %s", sig, aux, error_buf); \ |
| 79 |
} while (0) |
| 80 |
|
| 81 |
/* initialize Mbed TLS credential structure */ |
| 82 |
static rsRetVal mbedtlsInitCred(nsd_mbedtls_t *const pThis) { |
| 83 |
DEFiRet; |
| 84 |
const uchar *cafile; |
| 85 |
const uchar *crlfile; |
| 86 |
const uchar *keyFile; |
| 87 |
const uchar *certFile; |
| 88 |
int r; |
| 89 |
|
| 90 |
keyFile = (pThis->pszKeyFile == NULL) ? glbl.GetDfltNetstrmDrvrKeyFile(runConf) : pThis->pszKeyFile; |
| 91 |
if (keyFile != NULL) { |
| 92 |
mbedtls_pk_free(&(pThis->pkey)); |
| 93 |
mbedtls_pk_init(&(pThis->pkey)); |
| 94 |
#if MBEDTLS_VERSION_MAJOR >= 3 |
| 95 |
if ((r = mbedtls_pk_parse_keyfile(&(pThis->pkey), (const char *)keyFile, NULL, mbedtls_ctr_drbg_random, |
| 96 |
&(pThis->ctr_drbg))) != 0) { |
| 97 |
#else |
| 98 |
if ((r = mbedtls_pk_parse_keyfile(&(pThis->pkey), (const char *)keyFile, NULL)) != 0) { |
| 99 |
#endif |
| 100 |
logMbedtlsError(RS_RET_NO_ERRCODE, r); |
| 101 |
ABORT_FINALIZE(RS_RET_CONFIG_ERROR); |
| 102 |
} |
| 103 |
pThis->bHaveKey = 1; |
| 104 |
} |
| 105 |
|
| 106 |
certFile = (pThis->pszCertFile == NULL) ? glbl.GetDfltNetstrmDrvrCertFile(runConf) : pThis->pszCertFile; |
| 107 |
if (certFile != NULL) { |
| 108 |
mbedtls_x509_crt_free(&(pThis->srvcert)); |
| 109 |
mbedtls_x509_crt_init(&(pThis->srvcert)); |
| 110 |
if ((r = mbedtls_x509_crt_parse_file(&(pThis->srvcert), (const char *)certFile)) != 0) { |
| 111 |
logMbedtlsError(RS_RET_NO_ERRCODE, r); |
| 112 |
ABORT_FINALIZE(RS_RET_CONFIG_ERROR); |
| 113 |
} |
| 114 |
pThis->bHaveCert = 1; |
| 115 |
} |
| 116 |
|
| 117 |
cafile = (pThis->pszCAFile == NULL) ? glbl.GetDfltNetstrmDrvrCAF(runConf) : pThis->pszCAFile; |
| 118 |
if (cafile != NULL) { |
| 119 |
mbedtls_x509_crt_free(&(pThis->cacert)); |
| 120 |
mbedtls_x509_crt_init(&(pThis->cacert)); |
| 121 |
if ((r = mbedtls_x509_crt_parse_file(&(pThis->cacert), (const char *)cafile)) != 0) { |
| 122 |
logMbedtlsError(RS_RET_NO_ERRCODE, r); |
| 123 |
ABORT_FINALIZE(RS_RET_CONFIG_ERROR); |
| 124 |
} |
| 125 |
pThis->bHaveCaCert = 1; |
| 126 |
} |
| 127 |
|
| 128 |
crlfile = (pThis->pszCRLFile == NULL) ? glbl.GetDfltNetstrmDrvrCRLF(runConf) : pThis->pszCRLFile; |
| 129 |
if (crlfile != NULL) { |
| 130 |
mbedtls_x509_crl_free(&(pThis->crl)); |
| 131 |
mbedtls_x509_crl_init(&(pThis->crl)); |
| 132 |
if ((r = mbedtls_x509_crl_parse_file(&(pThis->crl), (const char *)crlfile)) != 0) { |
| 133 |
logMbedtlsError(RS_RET_NO_ERRCODE, r); |
| 134 |
ABORT_FINALIZE(RS_RET_CONFIG_ERROR); |
| 135 |
} |
| 136 |
pThis->bHaveCrl = 1; |
| 137 |
} |
| 138 |
|
| 139 |
finalize_it: |
| 140 |
if (iRet) { |
| 141 |
LogMsg(0, RS_RET_ERR, LOG_ERR, "nsd mbedtls: error parsing crypto config"); |
| 142 |
} |
| 143 |
|
| 144 |
RETiRet; |
| 145 |
} |
| 146 |
|
| 147 |
/* globally initialize Mbed TLS */ |
| 148 |
static rsRetVal mbedtlsGlblInit(void) { |
| 149 |
DEFiRet; |
| 150 |
|
| 151 |
dbgprintf("mbedtlsGlblInit: Running Version: '%#010x'\n", MBEDTLS_VERSION_NUMBER); |
| 152 |
|
| 153 |
RETiRet; |
| 154 |
} |
| 155 |
|
| 156 |
static rsRetVal get_custom_string(char **out) { |
| 157 |
DEFiRet; |
| 158 |
struct timeval tv; |
| 159 |
struct tm tm; |
| 160 |
|
| 161 |
CHKiRet(gettimeofday(&tv, NULL)); |
| 162 |
if (localtime_r(&(tv.tv_sec), &tm) == NULL) { |
| 163 |
ABORT_FINALIZE(RS_RET_NO_ERRCODE); |
| 164 |
} |
| 165 |
if (asprintf(out, "nsd_mbedtls-%04d-%02d-%02d %02d:%02d:%02d:%08ld", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, |
| 166 |
tm.tm_hour, tm.tm_min, tm.tm_sec, tv.tv_usec) == -1) { |
| 167 |
*out = NULL; |
| 168 |
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 169 |
} |
| 170 |
|
| 171 |
finalize_it: |
| 172 |
RETiRet; |
| 173 |
} |
| 174 |
|
| 175 |
static rsRetVal mbedtlsInitSession(nsd_mbedtls_t *pThis) { |
| 176 |
DEFiRet; |
| 177 |
char *cust = NULL; |
| 178 |
|
| 179 |
CHKiRet(get_custom_string(&cust)); |
| 180 |
CHKiRet(mbedtls_ctr_drbg_seed(&(pThis->ctr_drbg), mbedtls_entropy_func, &(pThis->entropy), |
| 181 |
(const unsigned char *)cust, strlen(cust))); |
| 182 |
|
| 183 |
finalize_it: |
| 184 |
if (iRet != RS_RET_OK) { |
| 185 |
LogError(0, iRet, "mbedtlsInitSession failed to INIT Session"); |
| 186 |
} |
| 187 |
free(cust); |
| 188 |
|
| 189 |
RETiRet; |
| 190 |
} |
| 191 |
|
| 192 |
/* globally de-initialize Mbed TLS */ |
| 193 |
static rsRetVal mbedtlsGlblExit(void) { |
| 194 |
DEFiRet; |
| 195 |
RETiRet; |
| 196 |
} |
| 197 |
|
| 198 |
/* end a Mbed TLS session |
| 199 |
* The function checks if we have a session and ends it only if so. So it can |
| 200 |
* always be called, even if there currently is no session. |
| 201 |
*/ |
| 202 |
static rsRetVal mbedtlsEndSess(nsd_mbedtls_t *pThis) { |
| 203 |
DEFiRet; |
| 204 |
int mbedtlsRet; |
| 205 |
|
| 206 |
if (pThis->bHaveSess) { |
| 207 |
while ((mbedtlsRet = mbedtls_ssl_close_notify(&(pThis->ssl))) != 0) { |
| 208 |
if (mbedtlsRet != MBEDTLS_ERR_SSL_WANT_READ && mbedtlsRet != MBEDTLS_ERR_SSL_WANT_WRITE) break; |
| 209 |
} |
| 210 |
pThis->bHaveSess = 0; |
| 211 |
} |
| 212 |
|
| 213 |
RETiRet; |
| 214 |
} |
| 215 |
|
| 216 |
/* Standard-Constructor */ |
| 217 |
BEGINobjConstruct(nsd_mbedtls) /* be sure to specify the object type also in END macro! */ |
| 218 |
iRet = nsd_ptcp.Construct(&pThis->pTcp); |
| 219 |
|
| 220 |
mbedtls_ssl_init(&(pThis->ssl)); |
| 221 |
mbedtls_ssl_config_init(&(pThis->conf)); |
| 222 |
mbedtls_x509_crt_init(&(pThis->cacert)); |
| 223 |
mbedtls_x509_crl_init(&(pThis->crl)); |
| 224 |
mbedtls_pk_init(&(pThis->pkey)); |
| 225 |
mbedtls_x509_crt_init(&(pThis->srvcert)); |
| 226 |
mbedtls_ctr_drbg_init(&(pThis->ctr_drbg)); |
| 227 |
mbedtls_entropy_init(&(pThis->entropy)); |
| 228 |
pThis->bReportAuthErr = 1; |
| 229 |
|
| 230 |
#if MBEDTLS_DEBUG_LEVEL > 0 |
| 231 |
mbedtls_debug_set_threshold(MBEDTLS_DEBUG_LEVEL); |
| 232 |
#endif |
| 233 |
|
| 234 |
ENDobjConstruct(nsd_mbedtls) |
| 235 |
|
| 236 |
/* destructor for the nsd_mbedtls object */ |
| 237 |
BEGINobjDestruct(nsd_mbedtls) /* be sure to specify the object type also in END and CODESTART macros! */ |
| 238 |
CODESTARTobjDestruct(nsd_mbedtls) if (pThis->iMode == 1) { |
| 239 |
mbedtlsEndSess(pThis); |
| 240 |
} |
| 241 |
|
| 242 |
if (pThis->pTcp != NULL) { |
| 243 |
nsd_ptcp.Destruct(&pThis->pTcp); |
| 244 |
} |
| 245 |
|
| 246 |
free((void *)pThis->pszCAFile); |
| 247 |
free((void *)pThis->pszCRLFile); |
| 248 |
free((void *)pThis->pszKeyFile); |
| 249 |
free((void *)pThis->pszCertFile); |
| 250 |
free((void *)pThis->anzCipherSuites); |
| 251 |
free((void *)pThis->pszConnectHost); |
| 252 |
|
| 253 |
mbedtls_entropy_free(&(pThis->entropy)); |
| 254 |
mbedtls_ctr_drbg_free(&(pThis->ctr_drbg)); |
| 255 |
mbedtls_x509_crt_free(&(pThis->srvcert)); |
| 256 |
mbedtls_pk_free(&(pThis->pkey)); |
| 257 |
mbedtls_x509_crl_free(&(pThis->crl)); |
| 258 |
mbedtls_x509_crt_free(&(pThis->cacert)); |
| 259 |
mbedtls_ssl_config_free(&(pThis->conf)); |
| 260 |
mbedtls_ssl_free(&(pThis->ssl)); |
| 261 |
|
| 262 |
ENDobjDestruct(nsd_mbedtls) |
| 263 |
|
| 264 |
/* Set the driver mode. For us, this has the following meaning: |
| 265 |
* 0 - work in plain tcp mode, without tls (e.g. before a STARTTLS) |
| 266 |
* 1 - work in TLS mode |
| 267 |
* rgerhards, 2008-04-28 |
| 268 |
*/ |
| 269 |
static rsRetVal SetMode(nsd_t *const pNsd, const int mode) { |
| 270 |
DEFiRet; |
| 271 |
nsd_mbedtls_t *pThis = (nsd_mbedtls_t *)pNsd; |
| 272 |
|
| 273 |
ISOBJ_TYPE_assert((pThis), nsd_mbedtls); |
| 274 |
dbgprintf("(tls) mode: %d\n", mode); |
| 275 |
if (mode != 0 && mode != 1) { |
| 276 |
LogError(0, RS_RET_INVALID_DRVR_MODE, |
| 277 |
"error: driver mode %d not supported by " |
| 278 |
"mbedtls netstream driver", |
| 279 |
mode); |
| 280 |
ABORT_FINALIZE(RS_RET_INVALID_DRVR_MODE); |
| 281 |
} |
| 282 |
|
| 283 |
pThis->iMode = mode; |
| 284 |
|
| 285 |
finalize_it: |
| 286 |
RETiRet; |
| 287 |
} |
| 288 |
|
| 289 |
/* Set the authentication mode. For us, the following is supported: |
| 290 |
* anon - no certificate checks whatsoever (discouraged, but supported) |
| 291 |
* x509/certvalid - (just) check certificate validity |
| 292 |
* x509/fingerprint - certificate fingerprint |
| 293 |
* x509/name - cerfificate name check |
| 294 |
* mode == NULL is valid and defaults to x509/name |
| 295 |
* rgerhards, 2008-05-16 |
| 296 |
*/ |
| 297 |
static rsRetVal SetAuthMode(nsd_t *pNsd, uchar *mode) { |
| 298 |
DEFiRet; |
| 299 |
nsd_mbedtls_t *pThis = (nsd_mbedtls_t *)pNsd; |
| 300 |
|
| 301 |
ISOBJ_TYPE_assert((pThis), nsd_mbedtls); |
| 302 |
if (mode == NULL || !strcasecmp((char *)mode, "x509/name")) { |
| 303 |
pThis->authMode = MBEDTLS_AUTH_CERTNAME; |
| 304 |
} else if (!strcasecmp((char *)mode, "x509/fingerprint")) { |
| 305 |
pThis->authMode = MBEDTLS_AUTH_CERTFINGERPRINT; |
| 306 |
} else if (!strcasecmp((char *)mode, "x509/certvalid")) { |
| 307 |
pThis->authMode = MBEDTLS_AUTH_CERTVALID; |
| 308 |
} else if (!strcasecmp((char *)mode, "anon")) { |
| 309 |
pThis->authMode = MBEDTLS_AUTH_CERTANON; |
| 310 |
} else { |
| 311 |
LogError(0, RS_RET_VALUE_NOT_SUPPORTED, |
| 312 |
"error: authentication mode '%s' not supported by " |
| 313 |
"mbedtls netstream driver", |
| 314 |
mode); |
| 315 |
ABORT_FINALIZE(RS_RET_VALUE_NOT_SUPPORTED); |
| 316 |
} |
| 317 |
|
| 318 |
dbgprintf("SetAuthMode to %s\n", (mode != NULL ? (char *)mode : "NULL")); |
| 319 |
|
| 320 |
finalize_it: |
| 321 |
RETiRet; |
| 322 |
} |
| 323 |
|
| 324 |
/* Set the PermitExpiredCerts mode. For us, the following is supported: |
| 325 |
* on - fail if certificate is expired |
| 326 |
* off - ignore expired certificates |
| 327 |
* warn - warn if certificate is expired |
| 328 |
* alorbach, 2018-12-20 |
| 329 |
*/ |
| 330 |
static rsRetVal SetPermitExpiredCerts(nsd_t *pNsd, uchar *mode) { |
| 331 |
DEFiRet; |
| 332 |
nsd_mbedtls_t *pThis = (nsd_mbedtls_t *)pNsd; |
| 333 |
|
| 334 |
ISOBJ_TYPE_assert((pThis), nsd_mbedtls); |
| 335 |
/* default is set to off! */ |
| 336 |
if (mode == NULL || !strcasecmp((char *)mode, "off")) { |
| 337 |
pThis->permitExpiredCerts = MBEDTLS_EXPIRED_DENY; |
| 338 |
} else if (!strcasecmp((char *)mode, "warn")) { |
| 339 |
pThis->permitExpiredCerts = MBEDTLS_EXPIRED_WARN; |
| 340 |
} else if (!strcasecmp((char *)mode, "on")) { |
| 341 |
pThis->permitExpiredCerts = MBEDTLS_EXPIRED_PERMIT; |
| 342 |
} else { |
| 343 |
LogError(0, RS_RET_VALUE_NOT_SUPPORTED, |
| 344 |
"error: permitexpiredcerts mode '%s' not supported by " |
| 345 |
"mbedtls netstream driver", |
| 346 |
mode); |
| 347 |
ABORT_FINALIZE(RS_RET_VALUE_NOT_SUPPORTED); |
| 348 |
} |
| 349 |
|
| 350 |
dbgprintf("SetPermitExpiredCerts: Set Mode %s/%d\n", (mode != NULL ? (char *)mode : "NULL"), |
| 351 |
pThis->permitExpiredCerts); |
| 352 |
|
| 353 |
/* TODO: clear stored IDs! */ |
| 354 |
|
| 355 |
finalize_it: |
| 356 |
RETiRet; |
| 357 |
} |
| 358 |
|
| 359 |
/* Set permitted peers. It is depending on the auth mode if this are |
| 360 |
* fingerprints or names. -- rgerhards, 2008-05-19 |
| 361 |
*/ |
| 362 |
static rsRetVal SetPermPeers(nsd_t *pNsd, permittedPeers_t *pPermPeers) { |
| 363 |
DEFiRet; |
| 364 |
nsd_mbedtls_t *pThis = (nsd_mbedtls_t *)pNsd; |
| 365 |
|
| 366 |
ISOBJ_TYPE_assert((pThis), nsd_mbedtls); |
| 367 |
if (pPermPeers == NULL) FINALIZE; |
| 368 |
|
| 369 |
if (pThis->authMode != MBEDTLS_AUTH_CERTFINGERPRINT && pThis->authMode != MBEDTLS_AUTH_CERTNAME) { |
| 370 |
LogError(0, RS_RET_VALUE_NOT_IN_THIS_MODE, |
| 371 |
"authentication not supported by " |
| 372 |
"mbedtls netstream driver in the configured authentication mode - ignored"); |
| 373 |
ABORT_FINALIZE(RS_RET_VALUE_NOT_IN_THIS_MODE); |
| 374 |
} |
| 375 |
|
| 376 |
pThis->pPermPeers = pPermPeers; |
| 377 |
|
| 378 |
finalize_it: |
| 379 |
RETiRet; |
| 380 |
} |
| 381 |
|
| 382 |
/* Set the priority string, aka the cipher suite list in |
| 383 |
* decreasing priority order (most important priority first) |
| 384 |
*/ |
| 385 |
static rsRetVal SetGnutlsPriorityString(nsd_t *pNsd, uchar *gnutlsPriorityString) { |
| 386 |
DEFiRet; |
| 387 |
nsd_mbedtls_t *pThis = (nsd_mbedtls_t *)pNsd; |
| 388 |
int nCipherSuiteId; |
| 389 |
char *pCurrentPos; |
| 390 |
char *pSave; |
| 391 |
int count = 0; |
| 392 |
char *str = NULL; |
| 393 |
int *tmp; |
| 394 |
const char *separators = ",; \n"; |
| 395 |
|
| 396 |
ISOBJ_TYPE_assert((pThis), nsd_mbedtls); |
| 397 |
|
| 398 |
if (gnutlsPriorityString != NULL) { |
| 399 |
CHKmalloc(str = strdup((const char *)gnutlsPriorityString)); |
| 400 |
|
| 401 |
for (pCurrentPos = strtok_r(str, separators, &pSave); pCurrentPos != NULL; |
| 402 |
pCurrentPos = strtok_r(NULL, separators, &pSave)) { |
| 403 |
if (*pCurrentPos == '\0') { |
| 404 |
continue; |
| 405 |
} |
| 406 |
nCipherSuiteId = mbedtls_ssl_get_ciphersuite_id(pCurrentPos); |
| 407 |
|
| 408 |
if (nCipherSuiteId == 0) { |
| 409 |
LogError(0, RS_RET_VALUE_NOT_SUPPORTED, |
| 410 |
"SetGnutlsPriorityString: %s " |
| 411 |
"is not supported", |
| 412 |
pCurrentPos); |
| 413 |
ABORT_FINALIZE(RS_RET_VALUE_NOT_SUPPORTED); |
| 414 |
} |
| 415 |
CHKmalloc(tmp = realloc(pThis->anzCipherSuites, (count + 2) * sizeof(int))); |
| 416 |
pThis->anzCipherSuites = tmp; |
| 417 |
pThis->anzCipherSuites[count] = nCipherSuiteId; |
| 418 |
pThis->anzCipherSuites[count + 1] = 0; |
| 419 |
++count; |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
finalize_it: |
| 424 |
free(str); |
| 425 |
|
| 426 |
RETiRet; |
| 427 |
} |
| 428 |
|
| 429 |
/* Set the driver cert extended key usage check setting |
| 430 |
* 0 - ignore contents of extended key usage |
| 431 |
* 1 - verify that cert contents is compatible with appropriate OID |
| 432 |
* jvymazal, 2019-08-16 |
| 433 |
*/ |
| 434 |
static rsRetVal SetCheckExtendedKeyUsage(nsd_t *pNsd, int ChkExtendedKeyUsage) { |
| 435 |
DEFiRet; |
| 436 |
nsd_mbedtls_t *pThis = (nsd_mbedtls_t *)pNsd; |
| 437 |
|
| 438 |
ISOBJ_TYPE_assert((pThis), nsd_mbedtls); |
| 439 |
if (ChkExtendedKeyUsage != 0 && ChkExtendedKeyUsage != 1) { |
| 440 |
LogError(0, RS_RET_VALUE_NOT_SUPPORTED, |
| 441 |
"error: driver ChkExtendedKeyUsage %d " |
| 442 |
"not supported by mbedtls netstream driver", |
| 443 |
ChkExtendedKeyUsage); |
| 444 |
ABORT_FINALIZE(RS_RET_VALUE_NOT_SUPPORTED); |
| 445 |
} |
| 446 |
|
| 447 |
dbgprintf("SetCheckExtendedKeyUsage: %d\n", ChkExtendedKeyUsage); |
| 448 |
pThis->dataTypeCheck = ChkExtendedKeyUsage; |
| 449 |
|
| 450 |
finalize_it: |
| 451 |
RETiRet; |
| 452 |
} |
| 453 |
|
| 454 |
/* Set the driver name checking strictness |
| 455 |
* 0 - less strict per RFC 5280, section 4.1.2.6 - either SAN or CN match is good |
| 456 |
* 1 - more strict per RFC 6125 - if any SAN present it must match (CN is ignored) |
| 457 |
* jvymazal, 2019-08-16 |
| 458 |
*/ |
| 459 |
static rsRetVal SetPrioritizeSAN(nsd_t *pNsd, int prioritizeSan) { |
| 460 |
DEFiRet; |
| 461 |
nsd_mbedtls_t *pThis = (nsd_mbedtls_t *)pNsd; |
| 462 |
|
| 463 |
ISOBJ_TYPE_assert((pThis), nsd_mbedtls); |
| 464 |
if (prioritizeSan != 0 && prioritizeSan != 1) { |
| 465 |
LogError(0, RS_RET_VALUE_NOT_SUPPORTED, |
| 466 |
"error: driver prioritizeSan %d " |
| 467 |
"not supported by mbedtls netstream driver", |
| 468 |
prioritizeSan); |
| 469 |
ABORT_FINALIZE(RS_RET_VALUE_NOT_SUPPORTED); |
| 470 |
} |
| 471 |
|
| 472 |
pThis->bSANpriority = prioritizeSan; |
| 473 |
|
| 474 |
finalize_it: |
| 475 |
RETiRet; |
| 476 |
} |
| 477 |
|
| 478 |
/* Set the driver tls verifyDepth |
| 479 |
* alorbach, 2019-12-20 |
| 480 |
*/ |
| 481 |
static rsRetVal SetTlsVerifyDepth(nsd_t *pNsd, int verifyDepth) { |
| 482 |
DEFiRet; |
| 483 |
nsd_mbedtls_t *pThis = (nsd_mbedtls_t *)pNsd; |
| 484 |
|
| 485 |
ISOBJ_TYPE_assert((pThis), nsd_mbedtls); |
| 486 |
pThis->DrvrVerifyDepth = verifyDepth; |
| 487 |
dbgprintf("SetTlsVerifyDepth: %d\n", pThis->DrvrVerifyDepth); |
| 488 |
|
| 489 |
RETiRet; |
| 490 |
} |
| 491 |
|
| 492 |
static rsRetVal SetTlsCAFile(nsd_t *pNsd, const uchar *const caFile) { |
| 493 |
DEFiRet; |
| 494 |
nsd_mbedtls_t *pThis = (nsd_mbedtls_t *)pNsd; |
| 495 |
|
| 496 |
ISOBJ_TYPE_assert((pThis), nsd_mbedtls); |
| 497 |
|
| 498 |
free((void *)pThis->pszCAFile); |
| 499 |
|
| 500 |
if (caFile == NULL) { |
| 501 |
pThis->pszCAFile = NULL; |
| 502 |
} else { |
| 503 |
CHKmalloc(pThis->pszCAFile = (const uchar *)strdup((const char *)caFile)); |
| 504 |
} |
| 505 |
|
| 506 |
finalize_it: |
| 507 |
RETiRet; |
| 508 |
} |
| 509 |
|
| 510 |
static rsRetVal SetTlsCRLFile(nsd_t *pNsd, const uchar *const crlFile) { |
| 511 |
DEFiRet; |
| 512 |
nsd_mbedtls_t *pThis = (nsd_mbedtls_t *)pNsd; |
| 513 |
|
| 514 |
ISOBJ_TYPE_assert((pThis), nsd_mbedtls); |
| 515 |
|
| 516 |
free((void *)pThis->pszCRLFile); |
| 517 |
|
| 518 |
if (crlFile == NULL) { |
| 519 |
pThis->pszCRLFile = NULL; |
| 520 |
} else { |
| 521 |
CHKmalloc(pThis->pszCRLFile = (const uchar *)strdup((const char *)crlFile)); |
| 522 |
} |
| 523 |
|
| 524 |
finalize_it: |
| 525 |
RETiRet; |
| 526 |
} |
| 527 |
|
| 528 |
static rsRetVal SetTlsKeyFile(nsd_t *pNsd, const uchar *const pszFile) { |
| 529 |
DEFiRet; |
| 530 |
nsd_mbedtls_t *pThis = (nsd_mbedtls_t *)pNsd; |
| 531 |
|
| 532 |
ISOBJ_TYPE_assert((pThis), nsd_mbedtls); |
| 533 |
|
| 534 |
free((void *)pThis->pszKeyFile); |
| 535 |
|
| 536 |
if (pszFile == NULL) { |
| 537 |
pThis->pszKeyFile = NULL; |
| 538 |
} else { |
| 539 |
CHKmalloc(pThis->pszKeyFile = (const uchar *)strdup((const char *)pszFile)); |
| 540 |
} |
| 541 |
|
| 542 |
finalize_it: |
| 543 |
RETiRet; |
| 544 |
} |
| 545 |
|
| 546 |
static rsRetVal SetTlsCertFile(nsd_t *pNsd, const uchar *const pszFile) { |
| 547 |
DEFiRet; |
| 548 |
nsd_mbedtls_t *pThis = (nsd_mbedtls_t *)pNsd; |
| 549 |
|
| 550 |
ISOBJ_TYPE_assert((pThis), nsd_mbedtls); |
| 551 |
|
| 552 |
free((void *)pThis->pszCertFile); |
| 553 |
|
| 554 |
if (pszFile == NULL) { |
| 555 |
pThis->pszCertFile = NULL; |
| 556 |
} else { |
| 557 |
CHKmalloc(pThis->pszCertFile = (const uchar *)strdup((const char *)pszFile)); |
| 558 |
} |
| 559 |
|
| 560 |
finalize_it: |
| 561 |
RETiRet; |
| 562 |
} |
| 563 |
|
| 564 |
static rsRetVal SetTlsRevocationCheck(nsd_t *pNsd, int enabled) { |
| 565 |
DEFiRet; |
| 566 |
nsd_mbedtls_t *pThis = (nsd_mbedtls_t *)pNsd; |
| 567 |
|
| 568 |
ISOBJ_TYPE_assert(pThis, nsd_mbedtls); |
| 569 |
pThis->DrvrTlsRevocationCheck = (enabled != 0) ? 1 : 0; |
| 570 |
|
| 571 |
if (pThis->DrvrTlsRevocationCheck) { |
| 572 |
LogError(0, RS_RET_VALUE_NOT_SUPPORTED, |
| 573 |
"error: TLS revocation checking not supported by " |
| 574 |
"mbedtls netstream driver (mbedTLS does not implement OCSP checking)"); |
| 575 |
ABORT_FINALIZE(RS_RET_VALUE_NOT_SUPPORTED); |
| 576 |
} |
| 577 |
|
| 578 |
finalize_it: |
| 579 |
RETiRet; |
| 580 |
} |
| 581 |
|
| 582 |
/* Provide access to the underlying OS socket. This is primarily |
| 583 |
* useful for other drivers (like nsd_mbedtls) who utilize ourselfs |
| 584 |
* for some of their functionality. -- rgerhards, 2008-04-18 |
| 585 |
*/ |
| 586 |
static rsRetVal SetSock(nsd_t *pNsd, int sock) { |
| 587 |
DEFiRet; |
| 588 |
nsd_mbedtls_t *pThis = (nsd_mbedtls_t *)pNsd; |
| 589 |
|
| 590 |
ISOBJ_TYPE_assert((pThis), nsd_mbedtls); |
| 591 |
|
| 592 |
DBGPRINTF("SetSock for [%p]: Setting sock %d\n", pNsd, sock); |
| 593 |
nsd_ptcp.SetSock(pThis->pTcp, sock); |
| 594 |
|
| 595 |
RETiRet; |
| 596 |
} |
| 597 |
|
| 598 |
/* Keep Alive Options |
| 599 |
*/ |
| 600 |
static rsRetVal SetKeepAliveIntvl(nsd_t *pNsd, int keepAliveIntvl) { |
| 601 |
DEFiRet; |
| 602 |
nsd_mbedtls_t *pThis = (nsd_mbedtls_t *)pNsd; |
| 603 |
|
| 604 |
ISOBJ_TYPE_assert((pThis), nsd_mbedtls); |
| 605 |
assert(keepAliveIntvl >= 0); |
| 606 |
|
| 607 |
nsd_ptcp.SetKeepAliveIntvl(pThis->pTcp, keepAliveIntvl); |
| 608 |
|
| 609 |
RETiRet; |
| 610 |
} |
| 611 |
|
| 612 |
/* Keep Alive Options |
| 613 |
*/ |
| 614 |
static rsRetVal SetKeepAliveProbes(nsd_t *pNsd, int keepAliveProbes) { |
| 615 |
DEFiRet; |
| 616 |
nsd_mbedtls_t *pThis = (nsd_mbedtls_t *)pNsd; |
| 617 |
|
| 618 |
ISOBJ_TYPE_assert((pThis), nsd_mbedtls); |
| 619 |
assert(keepAliveProbes >= 0); |
| 620 |
|
| 621 |
nsd_ptcp.SetKeepAliveProbes(pThis->pTcp, keepAliveProbes); |
| 622 |
|
| 623 |
RETiRet; |
| 624 |
} |
| 625 |
|
| 626 |
/* Keep Alive Options |
| 627 |
*/ |
| 628 |
static rsRetVal SetKeepAliveTime(nsd_t *pNsd, int keepAliveTime) { |
| 629 |
DEFiRet; |
| 630 |
nsd_mbedtls_t *pThis = (nsd_mbedtls_t *)pNsd; |
| 631 |
|
| 632 |
ISOBJ_TYPE_assert((pThis), nsd_mbedtls); |
| 633 |
assert(keepAliveTime >= 0); |
| 634 |
|
| 635 |
nsd_ptcp.SetKeepAliveTime(pThis->pTcp, keepAliveTime); |
| 636 |
|
| 637 |
RETiRet; |
| 638 |
} |
| 639 |
|
| 640 |
/* abort a connection. This is meant to be called immediately |
| 641 |
* before the Destruct call. -- rgerhards, 2008-03-24 |
| 642 |
*/ |
| 643 |
static rsRetVal Abort(nsd_t *pNsd) { |
| 644 |
nsd_mbedtls_t *pThis = (nsd_mbedtls_t *)pNsd; |
| 645 |
DEFiRet; |
| 646 |
|
| 647 |
ISOBJ_TYPE_assert((pThis), nsd_mbedtls); |
| 648 |
|
| 649 |
if (pThis->iMode == 0) { |
| 650 |
nsd_ptcp.Abort(pThis->pTcp); |
| 651 |
} |
| 652 |
|
| 653 |
RETiRet; |
| 654 |
} |
| 655 |
|
| 656 |
/* Allow Mbed TLS to read data from the network. |
| 657 |
*/ |
| 658 |
static int mbedtlsNetRecv(void *ctx, unsigned char *buf, size_t len) { |
| 659 |
nsd_mbedtls_t *pThis = (nsd_mbedtls_t *)ctx; |
| 660 |
ssize_t slen = len; |
| 661 |
int oserr; |
| 662 |
DEFiRet; |
| 663 |
|
| 664 |
slen = recv(pThis->sock, buf, len, 0); |
| 665 |
oserr = errno; |
| 666 |
|
| 667 |
if (slen < 0) { |
| 668 |
switch (oserr) { |
| 669 |
case EAGAIN: |
| 670 |
#if EAGAIN != EWOULDBLOCK |
| 671 |
case EWOULDBLOCK: |
| 672 |
#endif |
| 673 |
case EINTR: |
| 674 |
ABORT_FINALIZE(MBEDTLS_ERR_SSL_WANT_READ); |
| 675 |
case EPIPE: |
| 676 |
case ECONNRESET: |
| 677 |
ABORT_FINALIZE(MBEDTLS_ERR_NET_CONN_RESET); |
| 678 |
default: |
| 679 |
ABORT_FINALIZE(MBEDTLS_ERR_NET_RECV_FAILED); |
| 680 |
} |
| 681 |
} |
| 682 |
iRet = slen; |
| 683 |
|
| 684 |
finalize_it: |
| 685 |
RETiRet; |
| 686 |
} |
| 687 |
|
| 688 |
/* Allow Mbed TLS to write data to the network. |
| 689 |
*/ |
| 690 |
static int mbedtlsNetSend(void *ctx, const unsigned char *buf, size_t len) { |
| 691 |
nsd_mbedtls_t *pThis = (nsd_mbedtls_t *)ctx; |
| 692 |
ssize_t slen = len; |
| 693 |
DEFiRet; |
| 694 |
|
| 695 |
iRet = nsd_ptcp.Send(pThis->pTcp, (unsigned char *)buf, &slen); |
| 696 |
|
| 697 |
if (iRet < RS_RET_OK) { |
| 698 |
ABORT_FINALIZE(MBEDTLS_ERR_NET_SEND_FAILED); |
| 699 |
} |
| 700 |
|
| 701 |
iRet = slen; |
| 702 |
|
| 703 |
finalize_it: |
| 704 |
RETiRet; |
| 705 |
} |
| 706 |
|
| 707 |
static int mbedtlsAuthMode(nsd_mbedtls_t *const pThis) { |
| 708 |
int mode; |
| 709 |
|
| 710 |
switch (pThis->authMode) { |
| 711 |
case MBEDTLS_AUTH_CERTNAME: |
| 712 |
case MBEDTLS_AUTH_CERTVALID: |
| 713 |
case MBEDTLS_AUTH_CERTFINGERPRINT: |
| 714 |
if (pThis->dataTypeCheck == MBEDTLS_NONE) |
| 715 |
mode = MBEDTLS_SSL_VERIFY_OPTIONAL; |
| 716 |
else |
| 717 |
mode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 718 |
break; |
| 719 |
case MBEDTLS_AUTH_CERTANON: |
| 720 |
mode = MBEDTLS_SSL_VERIFY_NONE; |
| 721 |
break; |
| 722 |
default: |
| 723 |
assert(0); /* this shall not happen! */ |
| 724 |
dbgprintf("ERROR: pThis->authMode %d invalid in nsd_mbedtls.c:%d\n", pThis->authMode, __LINE__); |
| 725 |
mode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 726 |
break; |
| 727 |
} |
| 728 |
return mode; |
| 729 |
} |
| 730 |
|
| 731 |
/* Obtain fingerprint of crt in buf of size *len. |
| 732 |
* *len will be filled with actual fingerprint length on output. |
| 733 |
*/ |
| 734 |
static rsRetVal mbedtlsCertFingerprint(const mbedtls_x509_crt *crt, mbedtls_md_type_t mdType, uchar *buf, size_t *len) { |
| 735 |
const mbedtls_md_info_t *mdInfo; |
| 736 |
size_t mdLen = 0; |
| 737 |
DEFiRet; |
| 738 |
|
| 739 |
mdInfo = mbedtls_md_info_from_type(mdType); |
| 740 |
if (mdInfo == NULL) ABORT_FINALIZE(RS_RET_CODE_ERR); |
| 741 |
|
| 742 |
mdLen = mbedtls_md_get_size(mdInfo); |
| 743 |
if (*len < mdLen) ABORT_FINALIZE(RS_RET_PROVIDED_BUFFER_TOO_SMALL); |
| 744 |
|
| 745 |
if (crt == NULL || buf == NULL) ABORT_FINALIZE(RS_RET_CODE_ERR); |
| 746 |
|
| 747 |
if (mbedtls_md(mdInfo, crt->raw.p, crt->raw.len, buf) != 0) ABORT_FINALIZE(RS_RET_CODE_ERR); |
| 748 |
|
| 749 |
finalize_it: |
| 750 |
*len = mdLen; |
| 751 |
|
| 752 |
RETiRet; |
| 753 |
} |
| 754 |
|
| 755 |
/* Convert a fingerprint to printable data. The conversion is carried out |
| 756 |
* according IETF I-D syslog-transport-tls-12. The fingerprint string is |
| 757 |
* returned in a new cstr object. It is the caller's responsibility to |
| 758 |
* destruct that object. |
| 759 |
* rgerhards, 2008-05-08 |
| 760 |
*/ |
| 761 |
static rsRetVal GenFingerprintStr(uchar *pFingerprint, size_t sizeFingerprint, cstr_t **ppStr, const char *prefix) { |
| 762 |
cstr_t *pStr = NULL; |
| 763 |
uchar buf[4]; |
| 764 |
size_t i; |
| 765 |
DEFiRet; |
| 766 |
|
| 767 |
CHKiRet(rsCStrConstruct(&pStr)); |
| 768 |
CHKiRet(rsCStrAppendStrWithLen(pStr, (uchar *)prefix, strlen(prefix))); |
| 769 |
for (i = 0; i < sizeFingerprint; ++i) { |
| 770 |
snprintf((char *)buf, sizeof(buf), ":%2.2X", pFingerprint[i]); |
| 771 |
CHKiRet(rsCStrAppendStrWithLen(pStr, buf, 3)); |
| 772 |
} |
| 773 |
cstrFinalize(pStr); |
| 774 |
|
| 775 |
*ppStr = pStr; |
| 776 |
|
| 777 |
finalize_it: |
| 778 |
if (iRet != RS_RET_OK) { |
| 779 |
if (pStr != NULL) rsCStrDestruct(&pStr); |
| 780 |
} |
| 781 |
RETiRet; |
| 782 |
} |
| 783 |
|
| 784 |
static rsRetVal mbedtlsChkPeerFingerprint(nsd_mbedtls_t *pThis, mbedtls_x509_crt *crt) { |
| 785 |
uchar fingerprint[20]; |
| 786 |
uchar fingerprintSha256[32]; |
| 787 |
size_t size; |
| 788 |
size_t sizeSha256; |
| 789 |
cstr_t *pstrFingerprint = NULL; |
| 790 |
cstr_t *pstrFingerprintSha256 = NULL; |
| 791 |
int bFoundPositiveMatch; |
| 792 |
permittedPeers_t *pPeer; |
| 793 |
DEFiRet; |
| 794 |
|
| 795 |
ISOBJ_TYPE_assert(pThis, nsd_mbedtls); |
| 796 |
|
| 797 |
/* obtain the SHA1 fingerprint */ |
| 798 |
size = sizeof(fingerprint); |
| 799 |
sizeSha256 = sizeof(fingerprintSha256); |
| 800 |
CHKiRet(mbedtlsCertFingerprint(crt, MBEDTLS_MD_SHA1, fingerprint, &size)); |
| 801 |
CHKiRet(mbedtlsCertFingerprint(crt, MBEDTLS_MD_SHA256, fingerprintSha256, &sizeSha256)); |
| 802 |
CHKiRet(GenFingerprintStr(fingerprint, size, &pstrFingerprint, "SHA1")); |
| 803 |
CHKiRet(GenFingerprintStr(fingerprintSha256, sizeSha256, &pstrFingerprintSha256, "SHA256")); |
| 804 |
dbgprintf("peer's certificate SHA1 fingerprint: %s\n", cstrGetSzStrNoNULL(pstrFingerprint)); |
| 805 |
dbgprintf("peer's certificate SHA256 fingerprint: %s\n", cstrGetSzStrNoNULL(pstrFingerprintSha256)); |
| 806 |
|
| 807 |
|
| 808 |
/* now search through the permitted peers to see if we can find a permitted one */ |
| 809 |
bFoundPositiveMatch = 0; |
| 810 |
pPeer = pThis->pPermPeers; |
| 811 |
while (pPeer != NULL && !bFoundPositiveMatch) { |
| 812 |
if (!rsCStrSzStrCmp(pstrFingerprint, pPeer->pszID, strlen((char *)pPeer->pszID))) { |
| 813 |
dbgprintf("mbedtlsChkPeerFingerprint: peer's certificate SHA1 MATCH found: %s\n", pPeer->pszID); |
| 814 |
bFoundPositiveMatch = 1; |
| 815 |
} else if (!rsCStrSzStrCmp(pstrFingerprintSha256, pPeer->pszID, strlen((char *)pPeer->pszID))) { |
| 816 |
dbgprintf("mbedtlsChkPeerFingerprint: peer's certificate SHA256 MATCH found: %s\n", pPeer->pszID); |
| 817 |
bFoundPositiveMatch = 1; |
| 818 |
} else { |
| 819 |
pPeer = pPeer->pNext; |
| 820 |
} |
| 821 |
} |
| 822 |
|
| 823 |
if (!bFoundPositiveMatch) { |
| 824 |
dbgprintf("invalid peer fingerprint, not permitted to talk to it\n"); |
| 825 |
if (pThis->bReportAuthErr == 1) { |
| 826 |
errno = 0; |
| 827 |
LogError(0, RS_RET_INVALID_FINGERPRINT, |
| 828 |
"error: peer fingerprint '%s' unknown - we are " |
| 829 |
"not permitted to talk to it", |
| 830 |
cstrGetSzStrNoNULL(pstrFingerprint)); |
| 831 |
pThis->bReportAuthErr = 0; |
| 832 |
} |
| 833 |
ABORT_FINALIZE(RS_RET_INVALID_FINGERPRINT); |
| 834 |
} |
| 835 |
|
| 836 |
finalize_it: |
| 837 |
if (pstrFingerprint != NULL) cstrDestruct(&pstrFingerprint); |
| 838 |
if (pstrFingerprintSha256 != NULL) cstrDestruct(&pstrFingerprintSha256); |
| 839 |
RETiRet; |
| 840 |
} |
| 841 |
|
| 842 |
/* Perform a match on ONE peer name obtained from the certificate. This name |
| 843 |
* is checked against the set of configured credentials. *pbFoundPositiveMatch is |
| 844 |
* set to 1 if the ID matches. *pbFoundPositiveMatch must have been initialized |
| 845 |
* to 0 by the caller (this is a performance enhancement as we expect to be |
| 846 |
* called multiple times). |
| 847 |
* TODO: implemet wildcards? |
| 848 |
* rgerhards, 2008-05-26 |
| 849 |
*/ |
| 850 |
static rsRetVal mbedtlsChkOnePeerName(nsd_mbedtls_t *pThis, uchar *pszPeerID, int *pbFoundPositiveMatch) { |
| 851 |
permittedPeers_t *pPeer; |
| 852 |
DEFiRet; |
| 853 |
|
| 854 |
ISOBJ_TYPE_assert(pThis, nsd_mbedtls); |
| 855 |
assert(pszPeerID != NULL); |
| 856 |
assert(pbFoundPositiveMatch != NULL); |
| 857 |
|
| 858 |
if (pThis->pPermPeers) { /* do we have configured peer IDs? */ |
| 859 |
pPeer = pThis->pPermPeers; |
| 860 |
while (pPeer != NULL) { |
| 861 |
CHKiRet(net.PermittedPeerWildcardMatch(pPeer, pszPeerID, pbFoundPositiveMatch)); |
| 862 |
if (*pbFoundPositiveMatch) break; |
| 863 |
pPeer = pPeer->pNext; |
| 864 |
} |
| 865 |
} else { |
| 866 |
/* we do not have configured peer IDs, so we use defaults */ |
| 867 |
if (pThis->pszConnectHost && !strcmp((char *)pszPeerID, (char *)pThis->pszConnectHost)) { |
| 868 |
*pbFoundPositiveMatch = 1; |
| 869 |
} |
| 870 |
} |
| 871 |
|
| 872 |
finalize_it: |
| 873 |
RETiRet; |
| 874 |
} |
| 875 |
|
| 876 |
/* Get the common name (CN) from a certificate |
| 877 |
* Caller must free() the returned string |
| 878 |
*/ |
| 879 |
static char *mbedtlsCnFromCrt(const mbedtls_x509_crt *crt) { |
| 880 |
const mbedtls_x509_name *name; |
| 881 |
|
| 882 |
for (name = &crt->subject; name != NULL; name = name->next) { |
| 883 |
if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0) { |
| 884 |
return strndup((const char *)(name->val.p), name->val.len); |
| 885 |
} |
| 886 |
} |
| 887 |
return NULL; |
| 888 |
} |
| 889 |
|
| 890 |
static rsRetVal mbedtlsChkPeerName(nsd_mbedtls_t *pThis, mbedtls_x509_crt *crt) { |
| 891 |
uchar lnBuf[256]; |
| 892 |
const mbedtls_x509_sequence *san; |
| 893 |
int bFoundPositiveMatch; |
| 894 |
char *str = NULL; |
| 895 |
int bHaveSAN = 0; |
| 896 |
cstr_t *pStr = NULL; |
| 897 |
DEFiRet; |
| 898 |
|
| 899 |
bFoundPositiveMatch = 0; |
| 900 |
CHKiRet(rsCStrConstruct(&pStr)); |
| 901 |
|
| 902 |
/* Check SANs */ |
| 903 |
for (san = &crt->subject_alt_names; !bFoundPositiveMatch && san != NULL && san->buf.p != NULL; san = san->next) { |
| 904 |
if (san->buf.tag == (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME)) { |
| 905 |
CHKmalloc(str = strndup((const char *)(san->buf.p), san->buf.len)); |
| 906 |
bHaveSAN = 1; |
| 907 |
dbgprintf("subject alt dnsName: '%s'\n", str); |
| 908 |
snprintf((char *)lnBuf, sizeof(lnBuf), "DNSname: %s; ", str); |
| 909 |
CHKiRet(rsCStrAppendStr(pStr, lnBuf)); |
| 910 |
CHKiRet(mbedtlsChkOnePeerName(pThis, (uchar *)str, &bFoundPositiveMatch)); |
| 911 |
free(str); |
| 912 |
str = NULL; |
| 913 |
} |
| 914 |
} |
| 915 |
|
| 916 |
/* Check also CN only if not configured per stricter RFC 6125 or no SAN present*/ |
| 917 |
if (!bFoundPositiveMatch && (!pThis->bSANpriority || !bHaveSAN)) { |
| 918 |
str = mbedtlsCnFromCrt(crt); |
| 919 |
if (str != NULL) { /* NULL if there was no CN present */ |
| 920 |
dbgprintf("mbedtls now checking auth for CN '%s'\n", str); |
| 921 |
snprintf((char *)lnBuf, sizeof(lnBuf), "CN: %s; ", str); |
| 922 |
CHKiRet(rsCStrAppendStr(pStr, lnBuf)); |
| 923 |
CHKiRet(mbedtlsChkOnePeerName(pThis, (uchar *)str, &bFoundPositiveMatch)); |
| 924 |
} |
| 925 |
} |
| 926 |
|
| 927 |
if (!bFoundPositiveMatch) { |
| 928 |
dbgprintf("invalid peer name, not permitted to talk to it\n"); |
| 929 |
if (pThis->bReportAuthErr == 1) { |
| 930 |
cstrFinalize(pStr); |
| 931 |
errno = 0; |
| 932 |
LogError(0, RS_RET_INVALID_FINGERPRINT, |
| 933 |
"error: peer name not authorized - " |
| 934 |
"not permitted to talk to it. Names: %s", |
| 935 |
cstrGetSzStrNoNULL(pStr)); |
| 936 |
pThis->bReportAuthErr = 0; |
| 937 |
} |
| 938 |
ABORT_FINALIZE(RS_RET_INVALID_FINGERPRINT); |
| 939 |
} |
| 940 |
|
| 941 |
finalize_it: |
| 942 |
if (pStr != NULL) rsCStrDestruct(&pStr); |
| 943 |
free(str); |
| 944 |
RETiRet; |
| 945 |
} |
| 946 |
|
| 947 |
/* This is a callback fonction to allow extra checks |
| 948 |
* in addition to default verifications already done by Mbed TLS's |
| 949 |
* default processing. |
| 950 |
*/ |
| 951 |
static int verify(void *ctx, mbedtls_x509_crt *crt, int depth, uint32_t *flags) { |
| 952 |
nsd_mbedtls_t *pThis = (nsd_mbedtls_t *)ctx; |
| 953 |
DEFiRet; |
| 954 |
|
| 955 |
if (depth > (pThis->DrvrVerifyDepth != 0 ? pThis->DrvrVerifyDepth : DEFAULT_MAX_DEPTH)) |
| 956 |
ABORT_FINALIZE(MBEDTLS_ERR_X509_FATAL_ERROR); |
| 957 |
|
| 958 |
if (depth > 0) FINALIZE; /* Nothing more to be done with issuer's certificates */ |
| 959 |
|
| 960 |
/* Mbed TLS API states that if any of the following checks fails, |
| 961 |
* the verify callback should set the corresponding flag (or |
| 962 |
* clear it if the check is not cared about) and return 0. |
| 963 |
* (see mbedtls_x509_crt_verify() documentation) |
| 964 |
*/ |
| 965 |
if ((*flags) & MBEDTLS_X509_BADCERT_EXPIRED) { |
| 966 |
if (pThis->permitExpiredCerts == MBEDTLS_EXPIRED_DENY) { |
| 967 |
FINALIZE; |
| 968 |
} |
| 969 |
if (pThis->permitExpiredCerts == MBEDTLS_EXPIRED_WARN) { |
| 970 |
LogMsg(0, RS_RET_NO_ERRCODE, LOG_WARNING, "Warning, certificate expired but expired certs are permitted"); |
| 971 |
} else { |
| 972 |
dbgprintf( |
| 973 |
"Mbed TLS gives MBEDTLS_X509_BADCERT_EXPIRED" |
| 974 |
", but expired certs are permitted.\n"); |
| 975 |
} |
| 976 |
*flags &= ~MBEDTLS_X509_BADCERT_EXPIRED; |
| 977 |
} |
| 978 |
|
| 979 |
if (pThis->authMode == MBEDTLS_AUTH_CERTFINGERPRINT) { |
| 980 |
if (mbedtlsChkPeerFingerprint(pThis, crt) != RS_RET_OK) { |
| 981 |
*flags |= MBEDTLS_X509_BADCERT_OTHER; |
| 982 |
} |
| 983 |
} else if (pThis->authMode == MBEDTLS_AUTH_CERTNAME) { |
| 984 |
if (mbedtlsChkPeerName(pThis, crt) != RS_RET_OK) { |
| 985 |
*flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH; |
| 986 |
} |
| 987 |
} /* Other auth modes have already been handled by default processing */ |
| 988 |
|
| 989 |
finalize_it: |
| 990 |
RETiRet; |
| 991 |
} |
| 992 |
|
| 993 |
static rsRetVal CheckVerifyResult(nsd_mbedtls_t *pThis) { |
| 994 |
DEFiRet; |
| 995 |
uint32_t flags; |
| 996 |
|
| 997 |
flags = mbedtls_ssl_get_verify_result(&(pThis->ssl)); |
| 998 |
|
| 999 |
if (mbedtlsAuthMode(pThis) != MBEDTLS_SSL_VERIFY_OPTIONAL) FINALIZE; |
| 1000 |
|
| 1001 |
if (pThis->dataTypeCheck == MBEDTLS_NONE && (flags & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE)) { |
| 1002 |
dbgprintf("Mbed TLS gives MBEDTLS_X509_BADCERT_EXT_KEY_USAGE but check disabled\n"); |
| 1003 |
flags &= ~MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
| 1004 |
} |
| 1005 |
if (flags != 0) { |
| 1006 |
logMbedtlsError(RS_RET_CERT_INVALID, MBEDTLS_ERR_X509_CERT_VERIFY_FAILED); |
| 1007 |
ABORT_FINALIZE(RS_RET_CERT_INVALID); |
| 1008 |
} |
| 1009 |
|
| 1010 |
finalize_it: |
| 1011 |
if (flags != 0) { |
| 1012 |
char vrfy_buf[512]; |
| 1013 |
if (mbedtls_x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), "Mbed TLS verify info : ", flags) < 0) |
| 1014 |
LogError(0, RS_RET_PROVIDED_BUFFER_TOO_SMALL, "Can't get Mbed TLS verify info"); |
| 1015 |
else |
| 1016 |
LogError(0, RS_RET_CERT_INVALID, "%s", vrfy_buf); |
| 1017 |
} |
| 1018 |
RETiRet; |
| 1019 |
} |
| 1020 |
|
| 1021 |
static rsRetVal zeroTermIntArrayDup(int *array, int **copy) { |
| 1022 |
int c, i; |
| 1023 |
int *p = NULL; |
| 1024 |
DEFiRet; |
| 1025 |
|
| 1026 |
c = 0; |
| 1027 |
while (array[c] != 0) ++c; |
| 1028 |
|
| 1029 |
CHKmalloc(p = malloc((c + 1) * sizeof(int))); |
| 1030 |
|
| 1031 |
for (i = 0; i <= c; ++i) p[i] = array[i]; |
| 1032 |
|
| 1033 |
finalize_it: |
| 1034 |
|
| 1035 |
*copy = p; |
| 1036 |
RETiRet; |
| 1037 |
} |
| 1038 |
|
| 1039 |
/* accept an incoming connection request - here, we do the usual accept |
| 1040 |
* handling. TLS specific handling is done thereafter (and if we run in TLS |
| 1041 |
* mode at this time). |
| 1042 |
* rgerhards, 2008-04-25 |
| 1043 |
*/ |
| 1044 |
static rsRetVal AcceptConnReq(nsd_t *pNsd, nsd_t **ppNew, char *const connInfo) { |
| 1045 |
DEFiRet; |
| 1046 |
nsd_mbedtls_t *pNew = NULL; |
| 1047 |
nsd_mbedtls_t *pThis = (nsd_mbedtls_t *)pNsd; |
| 1048 |
int mbedtlsRet; |
| 1049 |
|
| 1050 |
ISOBJ_TYPE_assert(pThis, nsd_mbedtls); |
| 1051 |
CHKiRet(nsd_mbedtlsConstruct(&pNew)); // TODO: prevent construct/destruct! |
| 1052 |
CHKiRet(nsd_ptcp.Destruct(&pNew->pTcp)); |
| 1053 |
CHKiRet(nsd_ptcp.AcceptConnReq(pThis->pTcp, &pNew->pTcp, connInfo)); |
| 1054 |
|
| 1055 |
if (pThis->iMode == 0) { |
| 1056 |
/* we are in non-TLS mode, so we are done */ |
| 1057 |
*ppNew = (nsd_t *)pNew; |
| 1058 |
FINALIZE; |
| 1059 |
} |
| 1060 |
/* copy Properties to pnew first */ |
| 1061 |
pNew->authMode = pThis->authMode; |
| 1062 |
pNew->bSANpriority = pThis->bSANpriority; |
| 1063 |
pNew->pPermPeers = pThis->pPermPeers; |
| 1064 |
pNew->DrvrVerifyDepth = pThis->DrvrVerifyDepth; |
| 1065 |
pNew->DrvrTlsRevocationCheck = pThis->DrvrTlsRevocationCheck; |
| 1066 |
pNew->dataTypeCheck = pThis->dataTypeCheck; |
| 1067 |
pNew->permitExpiredCerts = pThis->permitExpiredCerts; |
| 1068 |
if (pThis->anzCipherSuites) CHKiRet(zeroTermIntArrayDup(pThis->anzCipherSuites, &(pNew->anzCipherSuites))); |
| 1069 |
if (pThis->pszCertFile) CHKmalloc(pNew->pszCertFile = (const uchar *)strdup((const char *)(pThis->pszCertFile))); |
| 1070 |
if (pThis->pszKeyFile) CHKmalloc(pNew->pszKeyFile = (const uchar *)strdup((const char *)(pThis->pszKeyFile))); |
| 1071 |
if (pThis->pszCAFile) CHKmalloc(pNew->pszCAFile = (const uchar *)strdup((const char *)(pThis->pszCAFile))); |
| 1072 |
if (pThis->pszCRLFile) CHKmalloc(pNew->pszCRLFile = (const uchar *)strdup((const char *)(pThis->pszCRLFile))); |
| 1073 |
pNew->iMode = pThis->iMode; |
| 1074 |
|
| 1075 |
/* if we reach this point, we are in TLS mode */ |
| 1076 |
CHKiRet(mbedtlsInitSession(pNew)); |
| 1077 |
CHKiRet(mbedtlsInitCred(pNew)); |
| 1078 |
CHKiRet(mbedtls_ssl_config_defaults(&(pNew->conf), MBEDTLS_SSL_IS_SERVER, MBEDTLS_SSL_TRANSPORT_STREAM, |
| 1079 |
MBEDTLS_SSL_PRESET_DEFAULT)); |
| 1080 |
|
| 1081 |
mbedtls_ssl_conf_rng(&(pNew->conf), mbedtls_ctr_drbg_random, &(pNew->ctr_drbg)); |
| 1082 |
mbedtls_ssl_conf_authmode(&(pNew->conf), mbedtlsAuthMode(pNew)); |
| 1083 |
mbedtls_ssl_conf_ca_chain(&(pNew->conf), pNew->bHaveCaCert ? &(pNew->cacert) : NULL, |
| 1084 |
pNew->bHaveCrl ? &(pNew->crl) : NULL); |
| 1085 |
|
| 1086 |
mbedtls_ssl_conf_verify(&(pNew->conf), verify, pNew); |
| 1087 |
|
| 1088 |
if (pNew->bHaveKey && pNew->bHaveCert) |
| 1089 |
CHKiRet(mbedtls_ssl_conf_own_cert(&(pNew->conf), &(pNew->srvcert), &(pNew->pkey))); |
| 1090 |
|
| 1091 |
#if MBEDTLS_DEBUG_LEVEL > 0 |
| 1092 |
mbedtls_ssl_conf_dbg(&(pNew->conf), debug, stdout); |
| 1093 |
#endif |
| 1094 |
if (pNew->anzCipherSuites != NULL) mbedtls_ssl_conf_ciphersuites(&(pNew->conf), pNew->anzCipherSuites); |
| 1095 |
|
| 1096 |
CHKiRet(mbedtls_ssl_setup(&(pNew->ssl), &(pNew->conf))); |
| 1097 |
|
| 1098 |
// peer id will be checked in verify() callback |
| 1099 |
CHKiRet(mbedtls_ssl_set_hostname(&(pNew->ssl), NULL)); |
| 1100 |
|
| 1101 |
CHKiRet(nsd_ptcp.GetSock(pNew->pTcp, &(pNew->sock))); |
| 1102 |
mbedtls_ssl_set_bio(&(pNew->ssl), pNew, mbedtlsNetSend, mbedtlsNetRecv, NULL); |
| 1103 |
|
| 1104 |
mbedtlsRet = mbedtls_ssl_handshake(&(pNew->ssl)); |
| 1105 |
if (mbedtlsRet != 0 && mbedtlsRet != MBEDTLS_ERR_SSL_WANT_READ && mbedtlsRet != MBEDTLS_ERR_SSL_WANT_WRITE) { |
| 1106 |
logMbedtlsError(RS_RET_TLS_HANDSHAKE_ERR, mbedtlsRet); |
| 1107 |
ABORT_FINALIZE(RS_RET_TLS_HANDSHAKE_ERR); |
| 1108 |
} |
| 1109 |
CHKiRet(CheckVerifyResult(pNew)); |
| 1110 |
|
| 1111 |
pNew->bHaveSess = 1; |
| 1112 |
*ppNew = (nsd_t *)pNew; |
| 1113 |
|
| 1114 |
finalize_it: |
| 1115 |
if (iRet != RS_RET_OK) { |
| 1116 |
if (pNew != NULL) nsd_mbedtlsDestruct(&pNew); |
| 1117 |
} |
| 1118 |
RETiRet; |
| 1119 |
} |
| 1120 |
|
| 1121 |
/* receive data from a tcp socket |
| 1122 |
* The lenBuf parameter must contain the max buffer size on entry and contains |
| 1123 |
* the number of octets read on exit. This function |
| 1124 |
* never blocks, not even when called on a blocking socket. That is important |
| 1125 |
* for client sockets, which are set to block during send, but should not |
| 1126 |
* block when trying to read data. -- rgerhards, 2008-03-17 |
| 1127 |
*/ |
| 1128 |
static rsRetVal Rcv(nsd_t *pNsd, uchar *pBuf, ssize_t *pLenBuf, int *const oserr, unsigned *const nextIODirection) { |
| 1129 |
DEFiRet; |
| 1130 |
nsd_mbedtls_t *pThis = (nsd_mbedtls_t *)pNsd; |
| 1131 |
ISOBJ_TYPE_assert(pThis, nsd_mbedtls); |
| 1132 |
int n = 0; |
| 1133 |
|
| 1134 |
if (pThis->bAbortConn) ABORT_FINALIZE(RS_RET_CONNECTION_ABORTREQ); |
| 1135 |
|
| 1136 |
if (pThis->iMode == 0) { |
| 1137 |
CHKiRet(nsd_ptcp.Rcv(pThis->pTcp, pBuf, pLenBuf, oserr, nextIODirection)); |
| 1138 |
FINALIZE; |
| 1139 |
} |
| 1140 |
|
| 1141 |
/* --- in TLS mode now --- */ |
| 1142 |
n = mbedtls_ssl_read(&(pThis->ssl), pBuf, *pLenBuf); |
| 1143 |
|
| 1144 |
if (n == MBEDTLS_ERR_SSL_WANT_READ || n == MBEDTLS_ERR_SSL_WANT_WRITE) ABORT_FINALIZE(RS_RET_RETRY); |
| 1145 |
|
| 1146 |
if (n == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) ABORT_FINALIZE(RS_RET_CLOSED); |
| 1147 |
|
| 1148 |
if (n < 0) { |
| 1149 |
logMbedtlsError(RS_RET_RCV_ERR, n); |
| 1150 |
ABORT_FINALIZE(RS_RET_RCV_ERR); |
| 1151 |
} |
| 1152 |
|
| 1153 |
if (n == 0) ABORT_FINALIZE(RS_RET_RETRY); |
| 1154 |
|
| 1155 |
CHKiRet(CheckVerifyResult(pThis)); |
| 1156 |
|
| 1157 |
*pLenBuf = n; |
| 1158 |
|
| 1159 |
finalize_it: |
| 1160 |
dbgprintf("mbedtlsRcv return. nsd %p, iRet %d, lenRcvBuf %ld\n", pThis, iRet, (long)(*pLenBuf)); |
| 1161 |
|
| 1162 |
if (iRet != RS_RET_OK && iRet != RS_RET_RETRY) *pLenBuf = 0; |
| 1163 |
|
| 1164 |
RETiRet; |
| 1165 |
} |
| 1166 |
|
| 1167 |
/* send a buffer. On entry, pLenBuf contains the number of octets to |
| 1168 |
* write. On exit, it contains the number of octets actually written. |
| 1169 |
* If this number is lower than on entry, only a partial buffer has |
| 1170 |
* been written. |
| 1171 |
* rgerhards, 2008-03-19 |
| 1172 |
*/ |
| 1173 |
static rsRetVal Send(nsd_t *pNsd, uchar *pBuf, ssize_t *pLenBuf) { |
| 1174 |
int iSent; |
| 1175 |
nsd_mbedtls_t *pThis = (nsd_mbedtls_t *)pNsd; |
| 1176 |
DEFiRet; |
| 1177 |
ISOBJ_TYPE_assert(pThis, nsd_mbedtls); |
| 1178 |
|
| 1179 |
if (pThis->bAbortConn) ABORT_FINALIZE(RS_RET_CONNECTION_ABORTREQ); |
| 1180 |
|
| 1181 |
if (pThis->iMode == 0) { |
| 1182 |
CHKiRet(nsd_ptcp.Send(pThis->pTcp, pBuf, pLenBuf)); |
| 1183 |
FINALIZE; |
| 1184 |
} |
| 1185 |
|
| 1186 |
/* in TLS mode now */ |
| 1187 |
iSent = mbedtls_ssl_write(&(pThis->ssl), pBuf, *pLenBuf); |
| 1188 |
if (iSent == MBEDTLS_ERR_SSL_WANT_READ || iSent == MBEDTLS_ERR_SSL_WANT_WRITE) { |
| 1189 |
ABORT_FINALIZE(RS_RET_RETRY); |
| 1190 |
} else if (iSent <= 0) { |
| 1191 |
logMbedtlsError(RS_RET_NO_ERRCODE, iSent); |
| 1192 |
ABORT_FINALIZE(RS_RET_IO_ERROR); |
| 1193 |
} |
| 1194 |
CHKiRet(CheckVerifyResult(pThis)); |
| 1195 |
|
| 1196 |
*pLenBuf = iSent; |
| 1197 |
|
| 1198 |
finalize_it: |
| 1199 |
RETiRet; |
| 1200 |
} |
| 1201 |
|
| 1202 |
/* Enable KEEPALIVE handling on the socket. |
| 1203 |
* rgerhards, 2009-06-02 |
| 1204 |
*/ |
| 1205 |
static rsRetVal EnableKeepAlive(nsd_t *pNsd) { |
| 1206 |
nsd_mbedtls_t *pThis = (nsd_mbedtls_t *)pNsd; |
| 1207 |
ISOBJ_TYPE_assert(pThis, nsd_mbedtls); |
| 1208 |
return nsd_ptcp.EnableKeepAlive(pThis->pTcp); |
| 1209 |
} |
| 1210 |
|
| 1211 |
/* open a connection to a remote host (server). |
| 1212 |
*/ |
| 1213 |
static rsRetVal Connect(nsd_t *pNsd, int family, uchar *port, uchar *host, char *device) { |
| 1214 |
DEFiRet; |
| 1215 |
nsd_mbedtls_t *pThis = (nsd_mbedtls_t *)pNsd; |
| 1216 |
int mbedtlsRet; |
| 1217 |
|
| 1218 |
dbgprintf("Connect to %s:%s\n", host, port); |
| 1219 |
|
| 1220 |
ISOBJ_TYPE_assert(pThis, nsd_mbedtls); |
| 1221 |
assert(port != NULL); |
| 1222 |
assert(host != NULL); |
| 1223 |
|
| 1224 |
CHKiRet(mbedtlsInitSession(pThis)); |
| 1225 |
CHKiRet(mbedtlsInitCred(pThis)); |
| 1226 |
CHKiRet(nsd_ptcp.Connect(pThis->pTcp, family, port, host, device)); |
| 1227 |
|
| 1228 |
if (pThis->iMode == 0) FINALIZE; |
| 1229 |
|
| 1230 |
/* we reach this point if in TLS mode */ |
| 1231 |
CHKiRet(mbedtls_ssl_config_defaults(&(pThis->conf), MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_TRANSPORT_STREAM, |
| 1232 |
MBEDTLS_SSL_PRESET_DEFAULT)); |
| 1233 |
|
| 1234 |
mbedtls_ssl_conf_rng(&(pThis->conf), mbedtls_ctr_drbg_random, &(pThis->ctr_drbg)); |
| 1235 |
mbedtls_ssl_conf_authmode(&(pThis->conf), mbedtlsAuthMode(pThis)); |
| 1236 |
mbedtls_ssl_conf_ca_chain(&(pThis->conf), pThis->bHaveCaCert ? &(pThis->cacert) : NULL, |
| 1237 |
pThis->bHaveCrl ? &(pThis->crl) : NULL); |
| 1238 |
|
| 1239 |
mbedtls_ssl_conf_verify(&(pThis->conf), verify, pThis); |
| 1240 |
|
| 1241 |
if (pThis->bHaveKey && pThis->bHaveCert) |
| 1242 |
CHKiRet(mbedtls_ssl_conf_own_cert(&(pThis->conf), &(pThis->srvcert), &(pThis->pkey))); |
| 1243 |
|
| 1244 |
#if MBEDTLS_DEBUG_LEVEL > 0 |
| 1245 |
mbedtls_ssl_conf_dbg(&(pThis->conf), debug, stdout); |
| 1246 |
#endif |
| 1247 |
if (pThis->anzCipherSuites != NULL) mbedtls_ssl_conf_ciphersuites(&(pThis->conf), pThis->anzCipherSuites); |
| 1248 |
|
| 1249 |
CHKiRet(mbedtls_ssl_setup(&(pThis->ssl), &(pThis->conf))); |
| 1250 |
|
| 1251 |
// peer id will be checked in verify() callback |
| 1252 |
CHKiRet(mbedtls_ssl_set_hostname(&(pThis->ssl), NULL)); |
| 1253 |
CHKmalloc(pThis->pszConnectHost = (uchar *)strdup((char *)host)); |
| 1254 |
|
| 1255 |
CHKiRet(nsd_ptcp.GetSock(pThis->pTcp, &(pThis->sock))); |
| 1256 |
mbedtls_ssl_set_bio(&(pThis->ssl), pThis, mbedtlsNetSend, mbedtlsNetRecv, NULL); |
| 1257 |
|
| 1258 |
mbedtlsRet = mbedtls_ssl_handshake(&(pThis->ssl)); |
| 1259 |
if (mbedtlsRet != 0 && mbedtlsRet != MBEDTLS_ERR_SSL_WANT_READ && mbedtlsRet != MBEDTLS_ERR_SSL_WANT_WRITE) { |
| 1260 |
logMbedtlsError(RS_RET_TLS_HANDSHAKE_ERR, mbedtlsRet); |
| 1261 |
ABORT_FINALIZE(RS_RET_TLS_HANDSHAKE_ERR); |
| 1262 |
} |
| 1263 |
CHKiRet(CheckVerifyResult(pThis)); |
| 1264 |
|
| 1265 |
pThis->bHaveSess = 1; |
| 1266 |
|
| 1267 |
finalize_it: |
| 1268 |
if (iRet != RS_RET_OK) { |
| 1269 |
free(pThis->pszConnectHost); |
| 1270 |
pThis->pszConnectHost = NULL; |
| 1271 |
} |
| 1272 |
|
| 1273 |
RETiRet; |
| 1274 |
} |
| 1275 |
|
| 1276 |
static rsRetVal ATTR_NONNULL(1, 3, 5) LstnInit(netstrms_t *pNS, |
| 1277 |
void *pUsr, |
| 1278 |
rsRetVal (*fAddLstn)(void *, netstrm_t *), |
| 1279 |
const int iSessMax, |
| 1280 |
const tcpLstnParams_t *cnf_params) { |
| 1281 |
DEFiRet; |
| 1282 |
|
| 1283 |
iRet = nsd_ptcp.LstnInit(pNS, pUsr, fAddLstn, iSessMax, cnf_params); |
| 1284 |
|
| 1285 |
RETiRet; |
| 1286 |
} |
| 1287 |
|
| 1288 |
/* This function checks if the connection is still alive - well, kind of... |
| 1289 |
* This is a dummy here. For details, check function common in ptcp driver. |
| 1290 |
* rgerhards, 2008-06-09 |
| 1291 |
*/ |
| 1292 |
static rsRetVal CheckConnection(nsd_t *pNsd) { |
| 1293 |
nsd_mbedtls_t *pThis = (nsd_mbedtls_t *)pNsd; |
| 1294 |
ISOBJ_TYPE_assert(pThis, nsd_mbedtls); |
| 1295 |
|
| 1296 |
dbgprintf("CheckConnection for %p\n", pNsd); |
| 1297 |
return nsd_ptcp.CheckConnection(pThis->pTcp); |
| 1298 |
} |
| 1299 |
|
| 1300 |
/* Provide access to the underlying OS socket. |
| 1301 |
*/ |
| 1302 |
static rsRetVal GetSock(nsd_t *pNsd, int *pSock) { |
| 1303 |
nsd_mbedtls_t *pThis = (nsd_mbedtls_t *)pNsd; |
| 1304 |
ISOBJ_TYPE_assert(pThis, nsd_mbedtls); |
| 1305 |
return nsd_ptcp.GetSock(pThis->pTcp, pSock); |
| 1306 |
} |
| 1307 |
|
| 1308 |
/* get the remote hostname. The returned hostname must be freed by the caller. |
| 1309 |
* rgerhards, 2008-04-25 |
| 1310 |
*/ |
| 1311 |
static rsRetVal GetRemoteHName(nsd_t *pNsd, uchar **ppszHName) { |
| 1312 |
DEFiRet; |
| 1313 |
nsd_mbedtls_t *pThis = (nsd_mbedtls_t *)pNsd; |
| 1314 |
ISOBJ_TYPE_assert(pThis, nsd_mbedtls); |
| 1315 |
iRet = nsd_ptcp.GetRemoteHName(pThis->pTcp, ppszHName); |
| 1316 |
RETiRet; |
| 1317 |
} |
| 1318 |
|
| 1319 |
|
| 1320 |
/* Provide access to the sockaddr_storage of the remote peer. This |
| 1321 |
* is needed by the legacy ACL system. --- gerhards, 2008-12-01 |
| 1322 |
*/ |
| 1323 |
static rsRetVal GetRemAddr(nsd_t *pNsd, struct sockaddr_storage **ppAddr) { |
| 1324 |
DEFiRet; |
| 1325 |
nsd_mbedtls_t *pThis = (nsd_mbedtls_t *)pNsd; |
| 1326 |
ISOBJ_TYPE_assert(pThis, nsd_mbedtls); |
| 1327 |
iRet = nsd_ptcp.GetRemAddr(pThis->pTcp, ppAddr); |
| 1328 |
RETiRet; |
| 1329 |
} |
| 1330 |
|
| 1331 |
/* get the remote host's IP address. Caller must Destruct the object. */ |
| 1332 |
static rsRetVal GetRemoteIP(nsd_t *pNsd, prop_t **ip) { |
| 1333 |
DEFiRet; |
| 1334 |
nsd_mbedtls_t *pThis = (nsd_mbedtls_t *)pNsd; |
| 1335 |
ISOBJ_TYPE_assert(pThis, nsd_mbedtls); |
| 1336 |
iRet = nsd_ptcp.GetRemoteIP(pThis->pTcp, ip); |
| 1337 |
RETiRet; |
| 1338 |
} |
| 1339 |
|
| 1340 |
/* queryInterface function */ |
| 1341 |
BEGINobjQueryInterface(nsd_mbedtls) |
| 1342 |
CODESTARTobjQueryInterface( |
| 1343 |
nsd_mbedtls) if (pIf->ifVersion != |
| 1344 |
nsdCURR_IF_VERSION) { /* check for current version, increment on each change */ |
| 1345 |
ABORT_FINALIZE(RS_RET_INTERFACE_NOT_SUPPORTED); |
| 1346 |
} |
| 1347 |
|
| 1348 |
/* ok, we have the right interface, so let's fill it |
| 1349 |
* Please note that we may also do some backwards-compatibility |
| 1350 |
* work here (if we can support an older interface version - that, |
| 1351 |
* of course, also affects the "if" above). |
| 1352 |
*/ |
| 1353 |
pIf->Construct = (rsRetVal(*)(nsd_t **))nsd_mbedtlsConstruct; |
| 1354 |
pIf->Destruct = (rsRetVal(*)(nsd_t **))nsd_mbedtlsDestruct; |
| 1355 |
pIf->Abort = Abort; |
| 1356 |
pIf->LstnInit = LstnInit; |
| 1357 |
pIf->AcceptConnReq = AcceptConnReq; |
| 1358 |
pIf->Rcv = Rcv; |
| 1359 |
pIf->Send = Send; |
| 1360 |
pIf->Connect = Connect; |
| 1361 |
pIf->GetSock = GetSock; |
| 1362 |
pIf->SetSock = SetSock; |
| 1363 |
pIf->SetMode = SetMode; |
| 1364 |
pIf->SetAuthMode = SetAuthMode; |
| 1365 |
pIf->SetPermitExpiredCerts = SetPermitExpiredCerts; |
| 1366 |
pIf->SetPermPeers = SetPermPeers; |
| 1367 |
pIf->CheckConnection = CheckConnection; |
| 1368 |
pIf->GetRemoteHName = GetRemoteHName; |
| 1369 |
pIf->GetRemoteIP = GetRemoteIP; |
| 1370 |
pIf->GetRemAddr = GetRemAddr; |
| 1371 |
pIf->EnableKeepAlive = EnableKeepAlive; |
| 1372 |
pIf->SetKeepAliveIntvl = SetKeepAliveIntvl; |
| 1373 |
pIf->SetKeepAliveProbes = SetKeepAliveProbes; |
| 1374 |
pIf->SetKeepAliveTime = SetKeepAliveTime; |
| 1375 |
pIf->SetGnutlsPriorityString = SetGnutlsPriorityString; |
| 1376 |
pIf->SetCheckExtendedKeyUsage = SetCheckExtendedKeyUsage; |
| 1377 |
pIf->SetPrioritizeSAN = SetPrioritizeSAN; |
| 1378 |
pIf->SetTlsVerifyDepth = SetTlsVerifyDepth; |
| 1379 |
pIf->SetTlsCAFile = SetTlsCAFile; |
| 1380 |
pIf->SetTlsCRLFile = SetTlsCRLFile; |
| 1381 |
pIf->SetTlsKeyFile = SetTlsKeyFile; |
| 1382 |
pIf->SetTlsCertFile = SetTlsCertFile; |
| 1383 |
pIf->SetTlsRevocationCheck = SetTlsRevocationCheck; |
| 1384 |
finalize_it: |
| 1385 |
ENDobjQueryInterface(nsd_mbedtls) |
| 1386 |
|
| 1387 |
/* exit our class |
| 1388 |
*/ |
| 1389 |
BEGINObjClassExit(nsd_mbedtls, OBJ_IS_LOADABLE_MODULE) /* CHANGE class also in END MACRO! */ |
| 1390 |
CODESTARTObjClassExit(nsd_mbedtls) mbedtlsGlblExit(); /* shut down Mbed TLS */ |
| 1391 |
|
| 1392 |
/* release objects we no longer need */ |
| 1393 |
objRelease(nsd_ptcp, LM_NSD_PTCP_FILENAME); |
| 1394 |
objRelease(net, LM_NET_FILENAME); |
| 1395 |
objRelease(glbl, CORE_COMPONENT); |
| 1396 |
ENDObjClassExit(nsd_mbedtls) |
| 1397 |
|
| 1398 |
/* Initialize the nsd_mbedtls class. Must be called as the very first method |
| 1399 |
* before anything else is called inside this class. |
| 1400 |
* rgerhards, 2008-02-19 |
| 1401 |
*/ |
| 1402 |
BEGINObjClassInit(nsd_mbedtls, 1, OBJ_IS_LOADABLE_MODULE) /* class, version */ |
| 1403 |
/* request objects we use */ |
| 1404 |
CHKiRet(objUse(glbl, CORE_COMPONENT)); |
| 1405 |
CHKiRet(objUse(nsd_ptcp, LM_NSD_PTCP_FILENAME)); |
| 1406 |
CHKiRet(objUse(net, LM_NET_FILENAME)); |
| 1407 |
|
| 1408 |
/* now do global TLS init stuff */ |
| 1409 |
CHKiRet(mbedtlsGlblInit()); |
| 1410 |
ENDObjClassInit(nsd_mbedtls) |
| 1411 |
|
| 1412 |
/* --------------- here now comes the plumbing that makes as a library module --------------- */ |
| 1413 |
|
| 1414 |
BEGINmodExit |
| 1415 |
CODESTARTmodExit nsd_mbedtlsClassExit(); |
| 1416 |
ENDmodExit |
| 1417 |
|
| 1418 |
BEGINqueryEtryPt |
| 1419 |
CODESTARTqueryEtryPt CODEqueryEtryPt_STD_LIB_QUERIES |
| 1420 |
ENDqueryEtryPt |
| 1421 |
|
| 1422 |
BEGINmodInit() |
| 1423 |
CODESTARTmodInit; |
| 1424 |
*ipIFVersProvided = CURR_MOD_IF_VERSION; /* we only support the current interface specification */ |
| 1425 |
|
| 1426 |
/* Initialize all classes that are in our module - this includes ourselfs */ |
| 1427 |
CHKiRet(nsd_mbedtlsClassInit(pModInfo)); /* must be done after tcps_sess, as we use it */ |
| 1428 |
ENDmodInit |