1 /* tls_m.c - Handle tls/ssl using Mozilla NSS. */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2008-2012 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* ACKNOWLEDGEMENTS: Initial version written by Howard Chu.
17 * Additional support by Rich Megginson.
24 #include "ldap_config.h"
28 #if defined( HAVE_FCNTL_H )
32 #include <ac/stdlib.h>
34 #include <ac/socket.h>
35 #include <ac/string.h>
38 #include <ac/unistd.h>
40 #include <ac/dirent.h>
45 #define READ_PASSWORD_FROM_STDIN
46 #define READ_PASSWORD_FROM_FILE
48 #ifdef READ_PASSWORD_FROM_STDIN
49 #include <termios.h> /* for echo on/off */
52 #include <nspr/nspr.h>
53 #include <nspr/private/pprio.h>
56 #include <nss/sslerr.h>
57 #include <nss/sslproto.h>
58 #include <nss/pk11pub.h>
59 #include <nss/secerr.h>
60 #include <nss/keyhi.h>
61 #include <nss/secmod.h>
64 #undef NSS_VERSION_INT
65 #define NSS_VERSION_INT ((NSS_VMAJOR << 24) | (NSS_VMINOR << 16) | \
66 (NSS_VPATCH << 8) | NSS_VBUILD)
68 /* NSS 3.12.5 and later have NSS_InitContext */
69 #if NSS_VERSION_INT >= 0x030c0500
70 #define HAVE_NSS_INITCONTEXT 1
73 /* NSS 3.12.9 and later have SECMOD_RestartModules */
74 #if NSS_VERSION_INT >= 0x030c0900
75 #define HAVE_SECMOD_RESTARTMODULES 1
78 /* InitContext does not currently work in server mode */
79 /* #define INITCONTEXT_HACK 1 */
81 typedef struct tlsm_ctx {
84 int tc_unique; /* unique number associated with this ctx */
85 PRBool tc_verify_cert;
86 CERTCertDBHandle *tc_certdb;
87 PK11SlotInfo *tc_certdb_slot;
90 struct ldaptls *tc_config;
93 PRCallOnceType tc_callonce;
95 char *tc_slotname; /* if using pem */
96 #ifdef HAVE_NSS_INITCONTEXT
97 NSSInitContext *tc_initctx; /* the NSS context */
99 PK11GenericObject **tc_pem_objs; /* array of objects to free */
100 int tc_n_pem_objs; /* number of objects */
101 PRBool tc_warn_only; /* only warn of errors in validation */
102 #ifdef LDAP_R_COMPILE
103 ldap_pvt_thread_mutex_t tc_refmutex;
107 typedef PRFileDesc tlsm_session;
109 static int tlsm_ctx_count;
110 #define TLSM_CERTDB_DESC_FMT "ldap(%d)"
112 static PRDescIdentity tlsm_layer_id;
114 static const PRIOMethods tlsm_PR_methods;
116 #define CERTDB_NONE NULL
117 #define PREFIX_NONE NULL
119 #define PEM_LIBRARY "nsspem"
120 #define PEM_MODULE "PEM"
121 /* hash files for use with cacertdir have this file name suffix */
122 #define PEM_CA_HASH_FILE_SUFFIX ".0"
123 #define PEM_CA_HASH_FILE_SUFFIX_LEN 2
125 static SECMODModule *pem_module;
127 #define DEFAULT_TOKEN_NAME "default"
128 /* sprintf format used to create token name */
129 #define TLSM_PEM_TOKEN_FMT "PEM Token #%ld"
131 static int tlsm_slot_count;
133 #define PK11_SETATTRS(x,id,v,l) (x)->type = (id); \
134 (x)->pValue=(v); (x)->ulValueLen = (l);
136 /* forward declaration */
137 static int tlsm_init( void );
139 #ifdef LDAP_R_COMPILE
141 /* it doesn't seem guaranteed that a client will call
142 tlsm_thr_init in a non-threaded context - so we have
143 to wrap the mutex creation in a prcallonce
145 static ldap_pvt_thread_mutex_t tlsm_ctx_count_mutex;
146 static ldap_pvt_thread_mutex_t tlsm_init_mutex;
147 static ldap_pvt_thread_mutex_t tlsm_pem_mutex;
148 static PRCallOnceType tlsm_init_mutex_callonce = {0,0};
150 static PRStatus PR_CALLBACK
151 tlsm_thr_init_callonce( void )
153 if ( ldap_pvt_thread_mutex_init( &tlsm_ctx_count_mutex ) ) {
154 Debug( LDAP_DEBUG_ANY,
155 "TLS: could not create mutex for context counter: %d\n", errno, 0, 0 );
159 if ( ldap_pvt_thread_mutex_init( &tlsm_init_mutex ) ) {
160 Debug( LDAP_DEBUG_ANY,
161 "TLS: could not create mutex for moznss initialization: %d\n", errno, 0, 0 );
165 if ( ldap_pvt_thread_mutex_init( &tlsm_pem_mutex ) ) {
166 Debug( LDAP_DEBUG_ANY,
167 "TLS: could not create mutex for PEM module: %d\n", errno, 0, 0 );
175 tlsm_thr_init( void )
177 ( void )PR_CallOnce( &tlsm_init_mutex_callonce, tlsm_thr_init_callonce );
180 #endif /* LDAP_R_COMPILE */
183 tlsm_dump_cipher_info(PRFileDesc *fd)
187 for (ii = 0; ii < SSL_NumImplementedCiphers; ++ii) {
188 PRInt32 cipher = (PRInt32)SSL_ImplementedCiphers[ii];
189 PRBool enabled = PR_FALSE;
191 SSLCipherSuiteInfo info;
194 SSL_CipherPrefGet(fd, cipher, &enabled);
196 SSL_CipherPrefGetDefault(cipher, &enabled);
198 SSL_CipherPolicyGet(cipher, &policy);
199 SSL_GetCipherSuiteInfo(cipher, &info, (PRUintn)sizeof(info));
200 Debug( LDAP_DEBUG_TRACE,
201 "TLS: cipher: %d - %s, enabled: %d, ",
202 info.cipherSuite, info.cipherSuiteName, enabled );
203 Debug( LDAP_DEBUG_TRACE,
204 "policy: %d\n", policy, 0, 0 );
210 /* Cipher definitions */
212 char *ossl_name; /* The OpenSSL cipher name */
213 int num; /* The cipher id */
214 int attr; /* cipher attributes: algorithms, etc */
215 int version; /* protocol version valid for this cipher */
216 int bits; /* bits of strength */
217 int alg_bits; /* bits of the algorithm */
218 int strength; /* LOW, MEDIUM, HIGH */
219 int enabled; /* Enabled by default? */
222 /* cipher attributes */
223 #define SSL_kRSA 0x00000001L
224 #define SSL_aRSA 0x00000002L
225 #define SSL_aDSS 0x00000004L
226 #define SSL_DSS SSL_aDSS
227 #define SSL_eNULL 0x00000008L
228 #define SSL_DES 0x00000010L
229 #define SSL_3DES 0x00000020L
230 #define SSL_RC4 0x00000040L
231 #define SSL_RC2 0x00000080L
232 #define SSL_AES 0x00000100L
233 #define SSL_MD5 0x00000200L
234 #define SSL_SHA1 0x00000400L
235 #define SSL_SHA SSL_SHA1
236 #define SSL_RSA (SSL_kRSA|SSL_aRSA)
238 /* cipher strength */
239 #define SSL_NULL 0x00000001L
240 #define SSL_EXPORT40 0x00000002L
241 #define SSL_EXPORT56 0x00000004L
242 #define SSL_LOW 0x00000008L
243 #define SSL_MEDIUM 0x00000010L
244 #define SSL_HIGH 0x00000020L
246 #define SSL2 0x00000001L
247 #define SSL3 0x00000002L
248 /* OpenSSL treats SSL3 and TLSv1 the same */
251 /* Cipher translation */
252 static cipher_properties ciphers_def[] = {
254 {"DES-CBC3-MD5", SSL_EN_DES_192_EDE3_CBC_WITH_MD5, SSL_kRSA|SSL_aRSA|SSL_3DES|SSL_MD5, SSL2, 168, 168, SSL_HIGH, SSL_ALLOWED},
255 {"RC2-CBC-MD5", SSL_EN_RC2_128_CBC_WITH_MD5, SSL_kRSA|SSL_aRSA|SSL_RC2|SSL_MD5, SSL2, 128, 128, SSL_MEDIUM, SSL_ALLOWED},
256 {"RC4-MD5", SSL_EN_RC4_128_WITH_MD5, SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_MD5, SSL2, 128, 128, SSL_MEDIUM, SSL_ALLOWED},
257 {"DES-CBC-MD5", SSL_EN_DES_64_CBC_WITH_MD5, SSL_kRSA|SSL_aRSA|SSL_DES|SSL_MD5, SSL2, 56, 56, SSL_LOW, SSL_ALLOWED},
258 {"EXP-RC2-CBC-MD5", SSL_EN_RC2_128_CBC_EXPORT40_WITH_MD5, SSL_kRSA|SSL_aRSA|SSL_RC2|SSL_MD5, SSL2, 40, 128, SSL_EXPORT40, SSL_ALLOWED},
259 {"EXP-RC4-MD5", SSL_EN_RC4_128_EXPORT40_WITH_MD5, SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_MD5, SSL2, 40, 128, SSL_EXPORT40, SSL_ALLOWED},
262 {"RC4-MD5", SSL_RSA_WITH_RC4_128_MD5, SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_MD5, SSL3, 128, 128, SSL_MEDIUM, SSL_ALLOWED},
263 {"RC4-SHA", SSL_RSA_WITH_RC4_128_SHA, SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_SHA1, SSL3, 128, 128, SSL_MEDIUM, SSL_ALLOWED},
264 {"DES-CBC3-SHA", SSL_RSA_WITH_3DES_EDE_CBC_SHA, SSL_kRSA|SSL_aRSA|SSL_3DES|SSL_SHA1, SSL3, 168, 168, SSL_HIGH, SSL_ALLOWED},
265 {"DES-CBC-SHA", SSL_RSA_WITH_DES_CBC_SHA, SSL_kRSA|SSL_aRSA|SSL_DES|SSL_SHA1, SSL3, 56, 56, SSL_LOW, SSL_ALLOWED},
266 {"EXP-RC4-MD5", SSL_RSA_EXPORT_WITH_RC4_40_MD5, SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_MD5, SSL3, 40, 128, SSL_EXPORT40, SSL_ALLOWED},
267 {"EXP-RC2-CBC-MD5", SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5, SSL_kRSA|SSL_aRSA|SSL_RC2|SSL_MD5, SSL3, 0, 0, SSL_EXPORT40, SSL_ALLOWED},
268 {"NULL-MD5", SSL_RSA_WITH_NULL_MD5, SSL_kRSA|SSL_aRSA|SSL_eNULL|SSL_MD5, SSL3, 0, 0, SSL_NULL, SSL_NOT_ALLOWED},
269 {"NULL-SHA", SSL_RSA_WITH_NULL_SHA, SSL_kRSA|SSL_aRSA|SSL_eNULL|SSL_SHA1, SSL3, 0, 0, SSL_NULL, SSL_NOT_ALLOWED},
272 {"EXP1024-DES-CBC-SHA", TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA, SSL_kRSA|SSL_aRSA|SSL_DES|SSL_SHA, TLS1, 56, 56, SSL_EXPORT56, SSL_ALLOWED},
273 {"EXP1024-RC4-SHA", TLS_RSA_EXPORT1024_WITH_RC4_56_SHA, SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_SHA, TLS1, 56, 56, SSL_EXPORT56, SSL_ALLOWED},
274 {"AES128-SHA", TLS_RSA_WITH_AES_128_CBC_SHA, SSL_kRSA|SSL_aRSA|SSL_AES|SSL_SHA, TLS1, 128, 128, SSL_HIGH, SSL_ALLOWED},
275 {"AES256-SHA", TLS_RSA_WITH_AES_256_CBC_SHA, SSL_kRSA|SSL_aRSA|SSL_AES|SSL_SHA, TLS1, 256, 256, SSL_HIGH, SSL_ALLOWED},
278 #define ciphernum (sizeof(ciphers_def)/sizeof(cipher_properties))
280 /* given err which is the current errno, calls PR_SetError with
281 the corresponding NSPR error code */
283 tlsm_map_error(int err)
289 prError = PR_NO_ACCESS_RIGHTS_ERROR;
292 prError = PR_ADDRESS_IN_USE_ERROR;
295 prError = PR_ADDRESS_NOT_AVAILABLE_ERROR;
298 prError = PR_ADDRESS_NOT_SUPPORTED_ERROR;
301 prError = PR_WOULD_BLOCK_ERROR;
304 * On QNX and Neutrino, EALREADY is defined as EBUSY.
306 #if EALREADY != EBUSY
308 prError = PR_ALREADY_INITIATED_ERROR;
312 prError = PR_BAD_DESCRIPTOR_ERROR;
316 prError = PR_IO_ERROR;
320 prError = PR_FILESYSTEM_MOUNTED_ERROR;
323 prError = PR_CONNECT_ABORTED_ERROR;
326 prError = PR_CONNECT_REFUSED_ERROR;
329 prError = PR_CONNECT_RESET_ERROR;
332 prError = PR_DEADLOCK_ERROR;
336 prError = PR_DIRECTORY_CORRUPTED_ERROR;
341 prError = PR_NO_DEVICE_SPACE_ERROR;
345 prError = PR_FILE_EXISTS_ERROR;
348 prError = PR_ACCESS_FAULT_ERROR;
351 prError = PR_FILE_TOO_BIG_ERROR;
354 prError = PR_HOST_UNREACHABLE_ERROR;
357 prError = PR_IN_PROGRESS_ERROR;
360 prError = PR_PENDING_INTERRUPT_ERROR;
363 prError = PR_INVALID_ARGUMENT_ERROR;
366 prError = PR_IO_ERROR;
369 prError = PR_IS_CONNECTED_ERROR;
372 prError = PR_IS_DIRECTORY_ERROR;
375 prError = PR_LOOP_ERROR;
378 prError = PR_PROC_DESC_TABLE_FULL_ERROR;
381 prError = PR_MAX_DIRECTORY_ENTRIES_ERROR;
384 prError = PR_INVALID_ARGUMENT_ERROR;
388 prError = PR_REMOTE_FILE_ERROR;
392 prError = PR_NAME_TOO_LONG_ERROR;
395 prError = PR_NETWORK_UNREACHABLE_ERROR;
398 prError = PR_SYS_DESC_TABLE_FULL_ERROR;
401 * On SCO OpenServer 5, ENOBUFS is defined as ENOSR.
403 #if defined(ENOBUFS) && (ENOBUFS != ENOSR)
405 prError = PR_INSUFFICIENT_RESOURCES_ERROR;
409 prError = PR_FILE_NOT_FOUND_ERROR;
412 prError = PR_FILE_NOT_FOUND_ERROR;
415 prError = PR_FILE_IS_LOCKED_ERROR;
419 prError = PR_REMOTE_FILE_ERROR;
423 prError = PR_OUT_OF_MEMORY_ERROR;
426 prError = PR_INVALID_ARGUMENT_ERROR;
429 prError = PR_NO_DEVICE_SPACE_ERROR;
433 prError = PR_INSUFFICIENT_RESOURCES_ERROR;
437 prError = PR_NOT_CONNECTED_ERROR;
440 prError = PR_NOT_DIRECTORY_ERROR;
443 prError = PR_NOT_SOCKET_ERROR;
446 prError = PR_FILE_NOT_FOUND_ERROR;
449 prError = PR_NOT_TCP_SOCKET_ERROR;
453 prError = PR_BUFFER_OVERFLOW_ERROR;
457 prError = PR_NO_ACCESS_RIGHTS_ERROR;
460 prError = PR_CONNECT_RESET_ERROR;
464 prError = PR_IO_ERROR;
467 case EPROTONOSUPPORT:
468 prError = PR_PROTOCOL_NOT_SUPPORTED_ERROR;
471 prError = PR_ADDRESS_NOT_SUPPORTED_ERROR;
474 prError = PR_INVALID_METHOD_ERROR;
477 prError = PR_READ_ONLY_FILESYSTEM_ERROR;
480 prError = PR_INVALID_METHOD_ERROR;
483 prError = PR_IO_TIMEOUT_ERROR;
485 #if EWOULDBLOCK != EAGAIN
487 prError = PR_WOULD_BLOCK_ERROR;
491 prError = PR_NOT_SAME_DEVICE_ERROR;
494 prError = PR_UNKNOWN_ERROR;
497 PR_SetError( prError, err );
501 * cipher_list is an integer array with the following values:
502 * -1: never enable this cipher
507 nss_parse_ciphers(const char *cipherstr, int cipher_list[ciphernum])
516 /* All disabled to start */
517 for (i=0; i<ciphernum; i++)
520 ciphertip = strdup(cipherstr);
521 cipher = ciphers = ciphertip;
523 while (ciphers && (strlen(ciphers))) {
524 while ((*cipher) && (isspace(*cipher)))
529 case '+': /* Add something */
533 case '-': /* Subtract something */
537 case '!': /* Disable something */
546 if ((ciphers = strchr(cipher, ':'))) {
550 /* Do the easy one first */
551 if (!strcmp(cipher, "ALL")) {
552 for (i=0; i<ciphernum; i++) {
553 if (!(ciphers_def[i].attr & SSL_eNULL))
554 cipher_list[i] = action;
556 } else if (!strcmp(cipher, "COMPLEMENTOFALL")) {
557 for (i=0; i<ciphernum; i++) {
558 if ((ciphers_def[i].attr & SSL_eNULL))
559 cipher_list[i] = action;
561 } else if (!strcmp(cipher, "DEFAULT")) {
562 for (i=0; i<ciphernum; i++) {
563 cipher_list[i] = ciphers_def[i].enabled == SSL_ALLOWED ? 1 : 0;
572 while (c && (strlen(c))) {
574 if ((c = strchr(cipher, '+'))) {
578 if (!strcmp(cipher, "RSA")) {
580 } else if ((!strcmp(cipher, "NULL")) || (!strcmp(cipher, "eNULL"))) {
582 } else if (!strcmp(cipher, "AES")) {
584 } else if (!strcmp(cipher, "3DES")) {
586 } else if (!strcmp(cipher, "DES")) {
588 } else if (!strcmp(cipher, "RC4")) {
590 } else if (!strcmp(cipher, "RC2")) {
592 } else if (!strcmp(cipher, "MD5")) {
594 } else if ((!strcmp(cipher, "SHA")) || (!strcmp(cipher, "SHA1"))) {
596 } else if (!strcmp(cipher, "SSLv2")) {
598 } else if (!strcmp(cipher, "SSLv3")) {
600 } else if (!strcmp(cipher, "TLSv1")) {
602 } else if (!strcmp(cipher, "HIGH")) {
603 strength |= SSL_HIGH;
604 } else if (!strcmp(cipher, "MEDIUM")) {
605 strength |= SSL_MEDIUM;
606 } else if (!strcmp(cipher, "LOW")) {
608 } else if ((!strcmp(cipher, "EXPORT")) || (!strcmp(cipher, "EXP"))) {
609 strength |= SSL_EXPORT40|SSL_EXPORT56;
610 } else if (!strcmp(cipher, "EXPORT40")) {
611 strength |= SSL_EXPORT40;
612 } else if (!strcmp(cipher, "EXPORT56")) {
613 strength |= SSL_EXPORT56;
621 /* If we have a mask, apply it. If not then perhaps they provided
622 * a specific cipher to enable.
624 if (mask || strength || protocol) {
625 for (i=0; i<ciphernum; i++) {
626 if (((ciphers_def[i].attr & mask) ||
627 (ciphers_def[i].strength & strength) ||
628 (ciphers_def[i].version & protocol)) &&
629 (cipher_list[i] != -1)) {
630 /* Enable the NULL ciphers only if explicity
632 if (ciphers_def[i].attr & SSL_eNULL) {
633 if (mask & SSL_eNULL)
634 cipher_list[i] = action;
636 cipher_list[i] = action;
640 for (i=0; i<ciphernum; i++) {
641 if (!strcmp(ciphers_def[i].ossl_name, cipher) &&
642 cipher_list[i] != -1)
643 cipher_list[i] = action;
652 /* See if any ciphers were enabled */
654 for (i=0; i<ciphernum; i++) {
655 if (cipher_list[i] == 1)
665 tlsm_parse_ciphers(tlsm_ctx *ctx, const char *str)
667 int cipher_state[ciphernum];
673 rv = nss_parse_ciphers(str, cipher_state);
676 /* First disable everything */
677 for (i = 0; i < SSL_NumImplementedCiphers; i++)
678 SSL_CipherPrefSet(ctx->tc_model, SSL_ImplementedCiphers[i], SSL_NOT_ALLOWED);
680 /* Now enable what was requested */
681 for (i=0; i<ciphernum; i++) {
682 SSLCipherSuiteInfo suite;
685 if (SSL_GetCipherSuiteInfo(ciphers_def[i].num, &suite, sizeof suite)
687 enabled = cipher_state[i] < 0 ? 0 : cipher_state[i];
688 if (enabled == SSL_ALLOWED) {
689 if (PK11_IsFIPS() && !suite.isFIPS)
690 enabled = SSL_NOT_ALLOWED;
692 SSL_CipherPrefSet(ctx->tc_model, ciphers_def[i].num, enabled);
697 return rv == 1 ? 0 : -1;
701 tlsm_bad_cert_handler(void *arg, PRFileDesc *ssl)
703 SECStatus success = SECSuccess;
705 tlsm_ctx *ctx = (tlsm_ctx *)arg;
711 err = PORT_GetError();
714 case SEC_ERROR_UNTRUSTED_ISSUER:
715 case SEC_ERROR_UNKNOWN_ISSUER:
716 case SEC_ERROR_EXPIRED_CERTIFICATE:
717 case SEC_ERROR_EXPIRED_ISSUER_CERTIFICATE:
718 if (ctx->tc_verify_cert) {
719 success = SECFailure;
722 /* we bypass NSS's hostname checks and do our own */
723 case SSL_ERROR_BAD_CERT_DOMAIN:
726 success = SECFailure;
734 tlsm_dump_security_status(PRFileDesc *fd)
736 char * cp; /* bulk cipher name */
737 char * ip; /* cert issuer DN */
738 char * sp; /* cert subject DN */
739 int op; /* High, Low, Off */
740 int kp0; /* total key bits */
741 int kp1; /* secret key bits */
742 SSL3Statistics * ssl3stats = SSL_GetStatistics();
744 SSL_SecurityStatus( fd, &op, &cp, &kp0, &kp1, &ip, &sp );
745 Debug( LDAP_DEBUG_TRACE,
746 "TLS certificate verification: subject: %s, issuer: %s, cipher: %s, ",
747 sp ? sp : "-unknown-", ip ? ip : "-unknown-", cp ? cp : "-unknown-" );
751 Debug( LDAP_DEBUG_TRACE,
752 "security level: %s, secret key bits: %d, total key bits: %d, ",
753 ((op == SSL_SECURITY_STATUS_ON_HIGH) ? "high" :
754 ((op == SSL_SECURITY_STATUS_ON_LOW) ? "low" : "off")),
757 Debug( LDAP_DEBUG_TRACE,
758 "cache hits: %ld, cache misses: %ld, cache not reusable: %ld\n",
759 ssl3stats->hch_sid_cache_hits, ssl3stats->hch_sid_cache_misses,
760 ssl3stats->hch_sid_cache_not_ok );
766 tlsm_handshake_complete_cb( PRFileDesc *fd, void *client_data )
768 tlsm_dump_security_status( fd );
771 #ifdef READ_PASSWORD_FROM_FILE
773 tlsm_get_pin_from_file(const char *token_name, tlsm_ctx *ctx)
776 char *contents = NULL;
779 char *candidate = NULL;
780 PRFileInfo file_info;
781 PRFileDesc *pwd_fileptr = PR_Open( ctx->tc_pin_file, PR_RDONLY, 00400 );
783 /* open the password file */
784 if ( !pwd_fileptr ) {
785 PRErrorCode errcode = PR_GetError();
786 Debug( LDAP_DEBUG_ANY,
787 "TLS: could not open security pin file %s - error %d:%s.\n",
788 ctx->tc_pin_file, errcode,
789 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
793 /* get the file size */
794 if ( PR_SUCCESS != PR_GetFileInfo( ctx->tc_pin_file, &file_info ) ) {
795 PRErrorCode errcode = PR_GetError();
796 Debug( LDAP_DEBUG_ANY,
797 "TLS: could not get file info from pin file %s - error %d:%s.\n",
798 ctx->tc_pin_file, errcode,
799 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
803 /* create a buffer to hold the file contents */
804 if ( !( contents = PR_CALLOC( file_info.size + 1 ) ) ) {
805 PRErrorCode errcode = PR_GetError();
806 Debug( LDAP_DEBUG_ANY,
807 "TLS: could not alloc a buffer for contents of pin file %s - error %d:%s.\n",
808 ctx->tc_pin_file, errcode,
809 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
813 /* read file into the buffer */
814 if( PR_Read( pwd_fileptr, contents, file_info.size ) <= 0 ) {
815 PRErrorCode errcode = PR_GetError();
816 Debug( LDAP_DEBUG_ANY,
817 "TLS: could not read the file contents from pin file %s - error %d:%s.\n",
818 ctx->tc_pin_file, errcode,
819 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
823 /* format is [tokenname:]password EOL [tokenname:]password EOL ... */
824 /* if you want to use a password containing a colon character, use
825 the special tokenname "default" */
826 for ( line = PL_strtok_r( contents, "\r\n", &lasts ); line;
827 line = PL_strtok_r( NULL, "\r\n", &lasts ) ) {
831 continue; /* skip blank lines */
833 colon = PL_strchr( line, ':' );
835 if ( *(colon + 1) && token_name &&
836 !PL_strncmp( token_name, line, colon-line ) ) {
837 candidate = colon + 1; /* found a definite match */
839 } else if ( !PL_strncmp( DEFAULT_TOKEN_NAME, line, colon-line ) ) {
840 candidate = colon + 1; /* found possible match */
842 } else { /* no token name */
848 PR_Close( pwd_fileptr );
851 pwdstr = PL_strdup( candidate );
853 PL_strfree( contents );
857 #endif /* READ_PASSWORD_FROM_FILE */
859 #ifdef READ_PASSWORD_FROM_STDIN
861 * Turn the echoing off on a tty.
866 if ( isatty( fd ) ) {
868 tcgetattr( fd, &tio );
869 tio.c_lflag &= ~ECHO;
870 tcsetattr( fd, TCSAFLUSH, &tio );
875 * Turn the echoing on on a tty.
880 if ( isatty( fd ) ) {
882 tcgetattr( fd, &tio );
884 tcsetattr( fd, TCSAFLUSH, &tio );
885 tcsetattr( fd, TCSAFLUSH, &tio );
888 #endif /* READ_PASSWORD_FROM_STDIN */
891 * This does the actual work of reading the pin/password/pass phrase
894 tlsm_get_pin(PK11SlotInfo *slot, PRBool retry, tlsm_ctx *ctx)
896 char *token_name = NULL;
899 token_name = PK11_GetTokenName( slot );
900 #ifdef READ_PASSWORD_FROM_FILE
901 /* Try to get the passwords from the password file if it exists.
902 * THIS IS UNSAFE and is provided for convenience only. Without this
903 * capability the server would have to be started in foreground mode
904 * if using an encrypted key.
906 if ( ctx->tc_pin_file ) {
907 pwdstr = tlsm_get_pin_from_file( token_name, ctx );
909 #endif /* RETRIEVE_PASSWORD_FROM_FILE */
910 #ifdef READ_PASSWORD_FROM_STDIN
912 int infd = PR_FileDesc2NativeHandle( PR_STDIN );
913 int isTTY = isatty( infd );
914 unsigned char phrase[200];
915 /* Prompt for password */
918 "Please enter pin, password, or pass phrase for security token '%s': ",
919 token_name ? token_name : DEFAULT_TOKEN_NAME );
922 fgets( (char*)phrase, sizeof(phrase), stdin );
924 fprintf( stdout, "\n" );
927 /* stomp on newline */
928 phrase[strlen((char*)phrase)-1] = 0;
930 pwdstr = PL_strdup( (char*)phrase );
933 #endif /* READ_PASSWORD_FROM_STDIN */
938 * PKCS11 devices (including the internal softokn cert/key database)
939 * may be protected by a pin or password or even pass phrase
940 * MozNSS needs a way for the user to provide that
943 tlsm_pin_prompt(PK11SlotInfo *slot, PRBool retry, void *arg)
945 tlsm_ctx *ctx = (tlsm_ctx *)arg;
947 return tlsm_get_pin( slot, retry, ctx );
951 tlsm_get_basic_constraint_extension( CERTCertificate *cert,
952 CERTBasicConstraints *cbcval )
954 SECItem encodedVal = { 0, NULL };
957 rc = CERT_FindCertExtension( cert, SEC_OID_X509_BASIC_CONSTRAINTS,
959 if ( rc != SECSuccess ) {
963 rc = CERT_DecodeBasicConstraintValue( cbcval, &encodedVal );
965 /* free the raw extension data */
966 PORT_Free( encodedVal.data );
972 tlsm_cert_is_self_issued( CERTCertificate *cert )
974 /* A cert is self-issued if its subject and issuer are equal and
975 * both are of non-zero length.
977 PRBool is_self_issued = cert &&
978 (PRBool)SECITEM_ItemsAreEqual( &cert->derIssuer,
979 &cert->derSubject ) &&
980 cert->derSubject.len > 0;
981 return is_self_issued;
985 tlsm_verify_cert(CERTCertDBHandle *handle, CERTCertificate *cert, void *pinarg,
986 PRBool checksig, SECCertificateUsage certUsage, int errorToIgnore )
988 CERTVerifyLog verifylog;
989 SECStatus ret = SECSuccess;
991 int debug_level = LDAP_DEBUG_ANY;
993 if ( errorToIgnore == -1 ) {
994 debug_level = LDAP_DEBUG_TRACE;
997 /* the log captures information about every cert in the chain, so we can tell
998 which cert caused the problem and what the problem was */
999 memset( &verifylog, 0, sizeof( verifylog ) );
1000 verifylog.arena = PORT_NewArena( DER_DEFAULT_CHUNKSIZE );
1001 if ( verifylog.arena == NULL ) {
1002 Debug( LDAP_DEBUG_ANY,
1003 "TLS certificate verification: Out of memory for certificate verification logger\n",
1007 ret = CERT_VerifyCertificate( handle, cert, checksig, certUsage, PR_Now(), pinarg, &verifylog,
1009 if ( ( name = cert->subjectName ) == NULL ) {
1010 name = cert->nickname;
1012 if ( verifylog.head == NULL ) {
1013 /* it is possible for CERT_VerifyCertificate return with an error with no logging */
1014 if ( ret != SECSuccess ) {
1015 PRErrorCode errcode = PR_GetError();
1017 "TLS: certificate [%s] is not valid - error %d:%s.\n",
1018 name ? name : "(unknown)",
1019 errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1023 CERTVerifyLogNode *node;
1025 ret = SECSuccess; /* reset */
1026 node = verifylog.head;
1028 if ( ( name = node->cert->subjectName ) == NULL ) {
1029 name = node->cert->nickname;
1031 if ( node->error ) {
1032 /* NSS does not like CA certs that have the basic constraints extension
1033 with the CA flag set to FALSE - openssl doesn't check if the cert
1035 if ( ( node->error == SEC_ERROR_CA_CERT_INVALID ) &&
1036 tlsm_cert_is_self_issued( node->cert ) ) {
1038 PRErrorCode orig_error = PR_GetError();
1039 PRInt32 orig_oserror = PR_GetOSError();
1041 CERTBasicConstraints basicConstraint;
1042 SECStatus rv = tlsm_get_basic_constraint_extension( node->cert, &basicConstraint );
1043 if ( ( rv == SECSuccess ) && ( basicConstraint.isCA == PR_FALSE ) ) {
1044 Debug( LDAP_DEBUG_TRACE,
1045 "TLS: certificate [%s] is not correct because it is a CA cert and the "
1046 "BasicConstraint CA flag is set to FALSE - allowing for now, but "
1047 "please fix your certs if possible\n", name, 0, 0 );
1048 } else { /* does not have basicconstraint, or some other error */
1051 "TLS: certificate [%s] is not valid - CA cert is not valid\n",
1055 PR_SetError(orig_error, orig_oserror);
1057 } else if ( errorToIgnore && ( node->error == errorToIgnore ) ) {
1059 "TLS: Warning: ignoring error for certificate [%s] - error %ld:%s.\n",
1060 name, node->error, PR_ErrorToString( node->error, PR_LANGUAGE_I_DEFAULT ) );
1064 "TLS: certificate [%s] is not valid - error %ld:%s.\n",
1065 name, node->error, PR_ErrorToString( node->error, PR_LANGUAGE_I_DEFAULT ) );
1068 CERT_DestroyCertificate( node->cert );
1073 PORT_FreeArena( verifylog.arena, PR_FALSE );
1075 if ( ret == SECSuccess ) {
1076 Debug( LDAP_DEBUG_TRACE,
1077 "TLS: certificate [%s] is valid\n", name, 0, 0 );
1078 } else if ( errorToIgnore == -1 ) {
1086 tlsm_auth_cert_handler(void *arg, PRFileDesc *fd,
1087 PRBool checksig, PRBool isServer)
1089 SECCertificateUsage certUsage = isServer ? certificateUsageSSLClient : certificateUsageSSLServer;
1090 SECStatus ret = SECSuccess;
1091 CERTCertificate *peercert = SSL_PeerCertificate( fd );
1092 int errorToIgnore = 0;
1093 tlsm_ctx *ctx = (tlsm_ctx *)arg;
1095 if (ctx && ctx->tc_warn_only )
1098 ret = tlsm_verify_cert( ctx->tc_certdb, peercert,
1099 SSL_RevealPinArg( fd ),
1100 checksig, certUsage, errorToIgnore );
1101 CERT_DestroyCertificate( peercert );
1107 tlsm_authenticate_to_slot( tlsm_ctx *ctx, PK11SlotInfo *slot )
1111 if ( SECSuccess != PK11_Authenticate( slot, PR_FALSE, ctx ) ) {
1112 char *token_name = PK11_GetTokenName( slot );
1113 PRErrorCode errcode = PR_GetError();
1114 Debug( LDAP_DEBUG_ANY,
1115 "TLS: could not authenticate to the security token %s - error %d:%s.\n",
1116 token_name ? token_name : DEFAULT_TOKEN_NAME, errcode,
1117 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1119 rc = 0; /* success */
1126 tlsm_nss_shutdown_cb( void *appData, void *nssData )
1128 SECStatus rc = SECSuccess;
1130 SSL_ShutdownServerSessionIDCache();
1133 SECMOD_UnloadUserModule( pem_module );
1134 SECMOD_DestroyModule( pem_module );
1140 static PRCallOnceType tlsm_register_shutdown_callonce = {0,0};
1141 static PRStatus PR_CALLBACK
1142 tlsm_register_nss_shutdown_cb( void )
1144 if ( SECSuccess == NSS_RegisterShutdown( tlsm_nss_shutdown_cb,
1152 tlsm_register_nss_shutdown( void )
1154 return PR_CallOnce( &tlsm_register_shutdown_callonce,
1155 tlsm_register_nss_shutdown_cb );
1159 tlsm_init_pem_module( void )
1162 char *fullname = NULL;
1163 char *configstring = NULL;
1169 /* not loaded - load it */
1170 /* get the system dependent library name */
1171 fullname = PR_GetLibraryName( NULL, PEM_LIBRARY );
1172 /* Load our PKCS#11 module */
1173 configstring = PR_smprintf( "library=%s name=" PEM_MODULE " parameters=\"\"", fullname );
1174 PL_strfree( fullname );
1176 pem_module = SECMOD_LoadUserModule( configstring, NULL, PR_FALSE );
1177 PR_smprintf_free( configstring );
1179 if ( !pem_module || !pem_module->loaded ) {
1181 SECMOD_DestroyModule( pem_module );
1191 tlsm_add_pem_obj( tlsm_ctx *ctx, PK11GenericObject *obj )
1193 int idx = ctx->tc_n_pem_objs;
1194 ctx->tc_n_pem_objs++;
1195 ctx->tc_pem_objs = (PK11GenericObject **)
1196 PORT_Realloc( ctx->tc_pem_objs, ctx->tc_n_pem_objs * sizeof( PK11GenericObject * ) );
1197 ctx->tc_pem_objs[idx] = obj;
1201 tlsm_free_pem_objs( tlsm_ctx *ctx )
1203 /* free in reverse order of allocation */
1204 while ( ctx->tc_n_pem_objs-- ) {
1205 PK11_DestroyGenericObject( ctx->tc_pem_objs[ctx->tc_n_pem_objs] );
1206 ctx->tc_pem_objs[ctx->tc_n_pem_objs] = NULL;
1208 PORT_Free(ctx->tc_pem_objs);
1209 ctx->tc_pem_objs = NULL;
1210 ctx->tc_n_pem_objs = 0;
1214 tlsm_add_cert_from_file( tlsm_ctx *ctx, const char *filename, PRBool isca, PRBool istrusted )
1217 PK11SlotInfo *slot = NULL;
1218 PK11GenericObject *rv;
1219 CK_ATTRIBUTE *attrs;
1220 CK_ATTRIBUTE theTemplate[20];
1221 CK_BBOOL cktrue = CK_TRUE;
1222 CK_BBOOL ckfalse = CK_FALSE;
1223 CK_OBJECT_CLASS objClass = CKO_CERTIFICATE;
1224 char tmpslotname[64];
1225 char *slotname = NULL;
1226 const char *ptr = NULL;
1227 char sep = PR_GetDirectorySeparator();
1231 memset( &fi, 0, sizeof(fi) );
1232 status = PR_GetFileInfo( filename, &fi );
1233 if ( PR_SUCCESS != status) {
1234 PRErrorCode errcode = PR_GetError();
1235 Debug( LDAP_DEBUG_ANY,
1236 "TLS: could not read certificate file %s - error %d:%s.\n",
1238 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1242 if ( fi.type != PR_FILE_FILE ) {
1243 PR_SetError(PR_IS_DIRECTORY_ERROR, 0);
1244 Debug( LDAP_DEBUG_ANY,
1245 "TLS: error: the certificate file %s is not a file.\n",
1250 attrs = theTemplate;
1253 slotID = 0; /* CA and trust objects use slot 0 */
1254 PR_snprintf( tmpslotname, sizeof(tmpslotname), TLSM_PEM_TOKEN_FMT, slotID );
1255 slotname = tmpslotname;
1256 istrusted = PR_TRUE;
1258 if ( ctx->tc_slotname == NULL ) { /* need new slot */
1262 slotID = ++tlsm_slot_count;
1264 ctx->tc_slotname = PR_smprintf( TLSM_PEM_TOKEN_FMT, slotID );
1266 slotname = ctx->tc_slotname;
1268 if ( ( ptr = PL_strrchr( filename, sep ) ) ) {
1269 PL_strfree( ctx->tc_certname );
1272 /* pemnss conflates trusted certs with CA certs - since there can
1273 be more than one CA cert in a file (e.g. ca-bundle.crt) pemnss
1274 numbers each trusted cert - in the case of a server cert, there will be
1275 only one, so it will be number 0 */
1276 ctx->tc_certname = PR_smprintf( "%s:%s - 0", slotname, ptr );
1278 ctx->tc_certname = PR_smprintf( "%s:%s", slotname, ptr );
1283 slot = PK11_FindSlotByName( slotname );
1286 PRErrorCode errcode = PR_GetError();
1287 Debug( LDAP_DEBUG_ANY,
1288 "TLS: could not find the slot for certificate %s - error %d:%s.\n",
1289 ctx->tc_certname, errcode,
1290 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1294 PK11_SETATTRS( attrs, CKA_CLASS, &objClass, sizeof(objClass) ); attrs++;
1295 PK11_SETATTRS( attrs, CKA_TOKEN, &cktrue, sizeof(CK_BBOOL) ); attrs++;
1296 PK11_SETATTRS( attrs, CKA_LABEL, (unsigned char *)filename, strlen(filename)+1 ); attrs++;
1298 PK11_SETATTRS( attrs, CKA_TRUST, &cktrue, sizeof(CK_BBOOL) ); attrs++;
1300 PK11_SETATTRS( attrs, CKA_TRUST, &ckfalse, sizeof(CK_BBOOL) ); attrs++;
1302 /* This loads the certificate in our PEM module into the appropriate
1305 rv = PK11_CreateGenericObject( slot, theTemplate, 4, PR_FALSE /* isPerm */ );
1307 PK11_FreeSlot( slot );
1310 PRErrorCode errcode = PR_GetError();
1311 Debug( LDAP_DEBUG_ANY,
1312 "TLS: could not add the certificate %s - error %d:%s.\n",
1313 ctx->tc_certname, errcode,
1314 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1318 tlsm_add_pem_obj( ctx, rv );
1324 tlsm_add_key_from_file( tlsm_ctx *ctx, const char *filename )
1327 PK11SlotInfo * slot = NULL;
1328 PK11GenericObject *rv;
1329 CK_ATTRIBUTE *attrs;
1330 CK_ATTRIBUTE theTemplate[20];
1331 CK_BBOOL cktrue = CK_TRUE;
1332 CK_OBJECT_CLASS objClass = CKO_PRIVATE_KEY;
1337 memset( &fi, 0, sizeof(fi) );
1338 status = PR_GetFileInfo( filename, &fi );
1339 if ( PR_SUCCESS != status) {
1340 PRErrorCode errcode = PR_GetError();
1341 Debug( LDAP_DEBUG_ANY,
1342 "TLS: could not read key file %s - error %d:%s.\n",
1344 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1348 if ( fi.type != PR_FILE_FILE ) {
1349 PR_SetError(PR_IS_DIRECTORY_ERROR, 0);
1350 Debug( LDAP_DEBUG_ANY,
1351 "TLS: error: the key file %s is not a file.\n",
1356 attrs = theTemplate;
1358 if ( ctx->tc_slotname == NULL ) { /* need new slot */
1359 slotID = ++tlsm_slot_count;
1360 ctx->tc_slotname = PR_smprintf( TLSM_PEM_TOKEN_FMT, slotID );
1362 slot = PK11_FindSlotByName( ctx->tc_slotname );
1365 PRErrorCode errcode = PR_GetError();
1366 Debug( LDAP_DEBUG_ANY,
1367 "TLS: could not find the slot %s for the private key - error %d:%s.\n",
1368 ctx->tc_slotname, errcode,
1369 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1373 PK11_SETATTRS( attrs, CKA_CLASS, &objClass, sizeof(objClass) ); attrs++;
1374 PK11_SETATTRS( attrs, CKA_TOKEN, &cktrue, sizeof(CK_BBOOL) ); attrs++;
1375 PK11_SETATTRS( attrs, CKA_LABEL, (unsigned char *)filename, strlen(filename)+1 ); attrs++;
1376 rv = PK11_CreateGenericObject( slot, theTemplate, 3, PR_FALSE /* isPerm */ );
1379 PRErrorCode errcode = PR_GetError();
1380 Debug( LDAP_DEBUG_ANY,
1381 "TLS: could not add the certificate %s - error %d:%s.\n",
1382 ctx->tc_certname, errcode,
1383 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1386 /* When adding an encrypted key the PKCS#11 will be set as removed */
1387 /* This will force the token to be seen as re-inserted */
1388 SECMOD_WaitForAnyTokenEvent( pem_module, 0, 0 );
1389 PK11_IsPresent( slot );
1393 PK11_FreeSlot( slot );
1396 tlsm_add_pem_obj( ctx, rv );
1402 tlsm_init_ca_certs( tlsm_ctx *ctx, const char *cacertfile, const char *cacertdir )
1404 PRBool isca = PR_TRUE;
1405 PRStatus status = PR_SUCCESS;
1406 PRErrorCode errcode = PR_SUCCESS;
1408 if ( !cacertfile && !cacertdir ) {
1409 /* no checking - not good, but allowed */
1414 int rc = tlsm_add_cert_from_file( ctx, cacertfile, isca, PR_TRUE );
1416 errcode = PR_GetError();
1417 Debug( LDAP_DEBUG_ANY,
1418 "TLS: %s is not a valid CA certificate file - error %d:%s.\n",
1419 cacertfile, errcode,
1420 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1421 /* failure with cacertfile is a hard failure even if cacertdir is
1422 also specified and contains valid CA cert files */
1423 status = PR_FAILURE;
1425 Debug( LDAP_DEBUG_TRACE,
1426 "TLS: loaded CA certificate file %s.\n",
1431 /* if cacertfile above failed, we will return failure, even
1432 if there is a valid CA cert in cacertdir - but we still
1433 process cacertdir in case the user has enabled trace level
1434 debugging so they can see the processing for cacertdir too */
1435 /* any cacertdir failures are "soft" failures - if the user specifies
1436 no cert checking, then we allow the tls/ssl to continue, no matter
1437 what was specified for cacertdir, or the contents of the directory
1438 - this is different behavior than that of cacertfile */
1443 PRStatus fistatus = PR_FAILURE;
1445 memset( &fi, 0, sizeof(fi) );
1446 fistatus = PR_GetFileInfo( cacertdir, &fi );
1447 if ( PR_SUCCESS != fistatus) {
1448 errcode = PR_GetError();
1449 Debug( LDAP_DEBUG_ANY,
1450 "TLS: could not get info about the CA certificate directory %s - error %d:%s.\n",
1452 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1456 if ( fi.type != PR_FILE_DIRECTORY ) {
1457 Debug( LDAP_DEBUG_ANY,
1458 "TLS: error: the CA certificate directory %s is not a directory.\n",
1463 dir = PR_OpenDir( cacertdir );
1464 if ( NULL == dir ) {
1465 errcode = PR_GetError();
1466 Debug( LDAP_DEBUG_ANY,
1467 "TLS: could not open the CA certificate directory %s - error %d:%s.\n",
1469 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1474 entry = PR_ReadDir( dir, PR_SKIP_BOTH | PR_SKIP_HIDDEN );
1475 if ( ( NULL != entry ) && ( NULL != entry->name ) ) {
1476 char *fullpath = NULL;
1479 ptr = PL_strrstr( entry->name, PEM_CA_HASH_FILE_SUFFIX );
1480 if ( ( ptr == NULL ) || ( *(ptr + PEM_CA_HASH_FILE_SUFFIX_LEN) != '\0' ) ) {
1481 Debug( LDAP_DEBUG_TRACE,
1482 "TLS: file %s does not end in [%s] - does not appear to be a CA certificate "
1483 "directory file with a properly hashed file name - skipping.\n",
1484 entry->name, PEM_CA_HASH_FILE_SUFFIX, 0 );
1487 fullpath = PR_smprintf( "%s/%s", cacertdir, entry->name );
1488 if ( !tlsm_add_cert_from_file( ctx, fullpath, isca, PR_TRUE ) ) {
1489 Debug( LDAP_DEBUG_TRACE,
1490 "TLS: loaded CA certificate file %s from CA certificate directory %s.\n",
1491 fullpath, cacertdir, 0 );
1493 errcode = PR_GetError();
1494 Debug( LDAP_DEBUG_TRACE,
1495 "TLS: %s is not a valid CA certificate file - error %d:%s.\n",
1497 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1499 PR_smprintf_free( fullpath );
1501 } while ( NULL != entry );
1505 if ( status != PR_SUCCESS ) {
1513 * NSS supports having multiple cert/key databases in the same
1514 * directory, each one having a unique string prefix e.g.
1515 * slapd-01-cert8.db - the prefix here is "slapd-01-"
1516 * this function examines the given certdir - if it looks like
1517 * /path/to/directory/prefix it will return the
1518 * /path/to/directory part in realcertdir, and the prefix in prefix
1521 tlsm_get_certdb_prefix( const char *certdir, char **realcertdir, char **prefix )
1523 char sep = PR_GetDirectorySeparator();
1525 struct PRFileInfo prfi;
1528 *realcertdir = (char *)certdir; /* default is the one passed in */
1530 /* if certdir is not given, just return */
1535 prc = PR_GetFileInfo( certdir, &prfi );
1536 /* if certdir exists (file or directory) then it cannot specify a prefix */
1537 if ( prc == PR_SUCCESS ) {
1541 /* if certdir was given, and there is a '/' in certdir, see if there
1542 is anything after the last '/' - if so, assume it is the prefix */
1543 if ( ( ( ptr = strrchr( certdir, sep ) ) ) && *(ptr+1) ) {
1544 *realcertdir = PL_strndup( certdir, ptr-certdir );
1545 *prefix = PL_strdup( ptr+1 );
1552 * Currently mutiple MozNSS contexts share one certificate storage. When the
1553 * certdb is being opened, only new certificates are added to the storage.
1554 * When different databases are used, conflicting nicknames make the
1555 * certificate lookup by the nickname impossible. In addition a token
1556 * description might be prepended in certain conditions.
1558 * In order to make the certificate lookup by nickname possible, we explicitly
1559 * open each database using SECMOD_OpenUserDB and assign it the token
1560 * description. The token description is generated using ctx->tc_unique value,
1561 * which is unique for each context.
1563 static PK11SlotInfo *
1564 tlsm_init_open_certdb(tlsm_ctx *ctx, const char *dbdir, const char *prefix)
1566 PK11SlotInfo *slot = NULL;
1567 char *token_desc = NULL;
1568 char *config = NULL;
1570 token_desc = PR_smprintf(TLSM_CERTDB_DESC_FMT, ctx->tc_unique);
1571 config = PR_smprintf("configDir='%s' tokenDescription='%s' certPrefix='%s' keyPrefix='%s' flags=readOnly",
1572 dbdir, token_desc, prefix, prefix);
1573 Debug(LDAP_DEBUG_TRACE, "TLS: certdb config: %s\n", config, 0, 0);
1575 slot = SECMOD_OpenUserDB(config);
1577 PRErrorCode errcode = PR_GetError();
1578 Debug(LDAP_DEBUG_TRACE, "TLS: cannot open certdb '%s', error %d:%s\n", dbdir, errcode,
1579 PR_ErrorToString(errcode, PR_LANGUAGE_I_DEFAULT));
1583 PR_smprintf_free(token_desc);
1585 PR_smprintf_free(config);
1591 * This is the part of the init we defer until we get the
1592 * actual security configuration information. This is
1593 * only called once, protected by a PRCallOnce
1594 * NOTE: This must be done before the first call to SSL_ImportFD,
1595 * especially the setting of the policy
1596 * NOTE: This must be called after fork()
1599 tlsm_deferred_init( void *arg )
1601 tlsm_ctx *ctx = (tlsm_ctx *)arg;
1602 struct ldaptls *lt = ctx->tc_config;
1603 const char *securitydirs[3];
1606 PRErrorCode errcode = 1;
1607 #ifdef HAVE_NSS_INITCONTEXT
1608 NSSInitParameters initParams;
1609 NSSInitContext *initctx = NULL;
1610 PK11SlotInfo *certdb_slot = NULL;
1615 #ifdef HAVE_SECMOD_RESTARTMODULES
1616 /* NSS enforces the pkcs11 requirement that modules should be unloaded after
1617 a fork() - since there is no portable way to determine if NSS has been
1618 already initialized in a parent process, we just call SECMOD_RestartModules
1619 with force == FALSE - if the module has been unloaded due to a fork, it will
1620 be reloaded, otherwise, it is a no-op */
1621 if ( SECFailure == ( rc = SECMOD_RestartModules(PR_FALSE /* do not force */) ) ) {
1622 errcode = PORT_GetError();
1623 if ( errcode != SEC_ERROR_NOT_INITIALIZED ) {
1624 Debug( LDAP_DEBUG_TRACE,
1625 "TLS: could not restart the security modules: %d:%s\n",
1626 errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ), 0 );
1633 #ifdef HAVE_NSS_INITCONTEXT
1634 memset( &initParams, 0, sizeof( initParams ) );
1635 initParams.length = sizeof( initParams );
1636 #endif /* HAVE_NSS_INITCONTEXT */
1638 #ifdef LDAP_R_COMPILE
1639 if ( PR_CallOnce( &tlsm_init_mutex_callonce, tlsm_thr_init_callonce ) ) {
1642 #endif /* LDAP_R_COMPILE */
1644 #ifndef HAVE_NSS_INITCONTEXT
1645 if ( !NSS_IsInitialized() ) {
1646 #endif /* HAVE_NSS_INITCONTEXT */
1648 MOZNSS_DIR will override everything else - you can
1649 always set MOZNSS_DIR to force the use of this
1651 If using MOZNSS, specify the location of the moznss db dir
1652 in the cacertdir directive of the OpenLDAP configuration.
1653 DEFAULT_MOZNSS_DIR will only be used if the code cannot
1654 find a security dir to use based on the current
1658 securitydirs[nn++] = PR_GetEnv( "MOZNSS_DIR" );
1659 securitydirs[nn++] = lt->lt_cacertdir;
1660 securitydirs[nn++] = PR_GetEnv( "DEFAULT_MOZNSS_DIR" );
1661 for ( ii = 0; !done && ( ii < nn ); ++ii ) {
1662 char *realcertdir = NULL;
1663 const char *defprefix = "";
1664 char *prefix = (char *)defprefix;
1665 const char *securitydir = securitydirs[ii];
1666 if ( NULL == securitydir ) {
1670 tlsm_get_certdb_prefix( securitydir, &realcertdir, &prefix );
1671 LDAP_MUTEX_LOCK( &tlsm_init_mutex );
1673 /* initialize only moddb; certdb will be initialized explicitly */
1674 #ifdef HAVE_NSS_INITCONTEXT
1675 #ifdef INITCONTEXT_HACK
1676 if ( !NSS_IsInitialized() && ctx->tc_is_server ) {
1677 rc = NSS_Initialize( realcertdir, prefix, prefix, SECMOD_DB, NSS_INIT_READONLY );
1679 initctx = NSS_InitContext( realcertdir, prefix, prefix, SECMOD_DB,
1680 &initParams, NSS_INIT_READONLY|NSS_INIT_NOCERTDB );
1683 initctx = NSS_InitContext( realcertdir, prefix, prefix, SECMOD_DB,
1684 &initParams, NSS_INIT_READONLY|NSS_INIT_NOCERTDB );
1688 if (initctx != NULL) {
1689 certdb_slot = tlsm_init_open_certdb(ctx, realcertdir, prefix);
1692 ctx->tc_initctx = initctx;
1693 ctx->tc_certdb_slot = certdb_slot;
1695 NSS_ShutdownContext(initctx);
1700 rc = NSS_Initialize( realcertdir, prefix, prefix, SECMOD_DB, NSS_INIT_READONLY );
1703 LDAP_MUTEX_UNLOCK( &tlsm_init_mutex );
1705 if ( rc != SECSuccess ) {
1706 errcode = PORT_GetError();
1707 if ( securitydirs[ii] != lt->lt_cacertdir) {
1708 Debug( LDAP_DEBUG_TRACE,
1709 "TLS: could not initialize moznss using security dir %s prefix %s - error %d.\n",
1710 realcertdir, prefix, errcode );
1714 Debug( LDAP_DEBUG_TRACE, "TLS: using moznss security dir %s prefix %s.\n",
1715 realcertdir, prefix, 0 );
1719 if ( realcertdir != securitydir ) {
1720 PL_strfree( realcertdir );
1722 if ( prefix != defprefix ) {
1723 PL_strfree( prefix );
1727 if ( errcode ) { /* no moznss db found, or not using moznss db */
1728 LDAP_MUTEX_LOCK( &tlsm_init_mutex );
1729 #ifdef HAVE_NSS_INITCONTEXT
1730 int flags = NSS_INIT_READONLY|NSS_INIT_NOCERTDB|NSS_INIT_NOMODDB;
1731 #ifdef INITCONTEXT_HACK
1732 if ( !NSS_IsInitialized() && ctx->tc_is_server ) {
1733 rc = NSS_NoDB_Init( NULL );
1735 initctx = NSS_InitContext( CERTDB_NONE, PREFIX_NONE, PREFIX_NONE, SECMOD_DB,
1736 &initParams, flags );
1737 rc = (initctx == NULL) ? SECFailure : SECSuccess;
1740 initctx = NSS_InitContext( CERTDB_NONE, PREFIX_NONE, PREFIX_NONE, SECMOD_DB,
1741 &initParams, flags );
1743 ctx->tc_initctx = initctx;
1750 rc = NSS_NoDB_Init( NULL );
1752 LDAP_MUTEX_UNLOCK( &tlsm_init_mutex );
1753 if ( rc != SECSuccess ) {
1754 errcode = PORT_GetError();
1755 Debug( LDAP_DEBUG_ANY,
1756 "TLS: could not initialize moznss - error %d:%s.\n",
1757 errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ), 0 );
1762 if ( errcode || lt->lt_cacertfile ) {
1763 /* initialize the PEM module */
1764 LDAP_MUTEX_LOCK( &tlsm_init_mutex );
1765 if ( tlsm_init_pem_module() ) {
1766 LDAP_MUTEX_UNLOCK( &tlsm_init_mutex );
1767 int pem_errcode = PORT_GetError();
1768 Debug( LDAP_DEBUG_ANY,
1769 "TLS: could not initialize moznss PEM module - error %d:%s.\n",
1770 pem_errcode, PR_ErrorToString( pem_errcode, PR_LANGUAGE_I_DEFAULT ), 0 );
1772 if ( errcode ) /* PEM is required */
1775 } else if ( !errcode ) {
1776 tlsm_init_ca_certs( ctx, lt->lt_cacertfile, NULL );
1778 LDAP_MUTEX_UNLOCK( &tlsm_init_mutex );
1782 if ( tlsm_init_ca_certs( ctx, lt->lt_cacertfile, lt->lt_cacertdir ) ) {
1783 /* if we tried to use lt->lt_cacertdir as an NSS key/cert db, errcode
1784 will be a value other than 1 - print an error message so that the
1785 user will know that failed too */
1786 if ( ( errcode != 1 ) && ( lt->lt_cacertdir ) ) {
1787 char *realcertdir = NULL;
1788 char *prefix = NULL;
1789 tlsm_get_certdb_prefix( lt->lt_cacertdir, &realcertdir, &prefix );
1790 Debug( LDAP_DEBUG_TRACE,
1791 "TLS: could not initialize moznss using security dir %s prefix %s - error %d.\n",
1792 realcertdir, prefix ? prefix : "", errcode );
1793 if ( realcertdir != lt->lt_cacertdir ) {
1794 PL_strfree( realcertdir );
1796 PL_strfree( prefix );
1801 ctx->tc_using_pem = PR_TRUE;
1804 NSS_SetDomesticPolicy();
1806 PK11_SetPasswordFunc( tlsm_pin_prompt );
1808 /* register cleanup function */
1809 if ( tlsm_register_nss_shutdown() ) {
1810 errcode = PORT_GetError();
1811 Debug( LDAP_DEBUG_ANY,
1812 "TLS: could not register NSS shutdown function: %d:%s\n",
1813 errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ), 0 );
1817 if ( ctx->tc_is_server ) {
1818 LDAP_MUTEX_LOCK( &tlsm_init_mutex );
1819 /* 0 means use the defaults here */
1820 SSL_ConfigServerSessionIDCache( 0, 0, 0, NULL );
1821 LDAP_MUTEX_UNLOCK( &tlsm_init_mutex );
1824 #ifndef HAVE_NSS_INITCONTEXT
1826 #endif /* HAVE_NSS_INITCONTEXT */
1832 tlsm_authenticate( tlsm_ctx *ctx, const char *certname, const char *pininfo )
1834 const char *colon = NULL;
1835 char *token_name = NULL;
1836 PK11SlotInfo *slot = NULL;
1839 if ( !certname || !*certname ) {
1843 if ( ( colon = PL_strchr( certname, ':' ) ) ) {
1844 token_name = PL_strndup( certname, colon-certname );
1848 slot = PK11_FindSlotByName( token_name );
1850 slot = PK11_GetInternalKeySlot();
1854 PRErrorCode errcode = PR_GetError();
1855 Debug( LDAP_DEBUG_ANY,
1856 "TLS: could not find the slot for security token %s - error %d:%s.\n",
1857 token_name ? token_name : DEFAULT_TOKEN_NAME, errcode,
1858 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1862 rc = tlsm_authenticate_to_slot( ctx, slot );
1865 PL_strfree( token_name );
1867 PK11_FreeSlot( slot );
1874 * Find and verify the certificate.
1875 * Either fd is given, in which case the cert will be taken from it via SSL_PeerCertificate
1876 * or certname is given, and it will be searched for by name
1879 tlsm_find_and_verify_cert_key(tlsm_ctx *ctx, PRFileDesc *fd, const char *certname, int isServer, CERTCertificate **pRetCert, SECKEYPrivateKey **pRetKey)
1881 CERTCertificate *cert = NULL;
1883 void *pin_arg = NULL;
1884 SECKEYPrivateKey *key = NULL;
1886 pin_arg = SSL_RevealPinArg( fd );
1888 cert = PK11_FindCertFromNickname( certname, pin_arg );
1890 PRErrorCode errcode = PR_GetError();
1891 Debug( LDAP_DEBUG_ANY,
1892 "TLS: error: the certificate %s could not be found in the database - error %d:%s\n",
1893 certname, errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1897 /* we are verifying the peer cert
1898 we also need to swap the isServer meaning */
1899 cert = SSL_PeerCertificate( fd );
1901 PRErrorCode errcode = PR_GetError();
1902 Debug( LDAP_DEBUG_ANY,
1903 "TLS: error: could not get the certificate from the peer connection - error %d:%s\n",
1904 errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ), NULL );
1907 isServer = !isServer; /* verify the peer's cert instead */
1910 if ( ctx->tc_slotname ) {
1911 PK11SlotInfo *slot = PK11_FindSlotByName( ctx->tc_slotname );
1912 key = PK11_FindPrivateKeyFromCert( slot, cert, NULL );
1913 PK11_FreeSlot( slot );
1915 key = PK11_FindKeyByAnyCert( cert, pin_arg );
1919 SECCertificateUsage certUsage;
1920 PRBool checkSig = PR_TRUE;
1922 /* may not have a CA cert - ok - ignore SEC_ERROR_UNKNOWN_ISSUER */
1923 int errorToIgnore = SEC_ERROR_UNKNOWN_ISSUER;
1926 *pRetKey = key; /* caller will deal with this */
1928 SECKEY_DestroyPrivateKey( key );
1931 certUsage = certificateUsageSSLServer;
1933 certUsage = certificateUsageSSLClient;
1935 if ( ctx->tc_verify_cert ) {
1938 checkSig = PR_FALSE;
1940 if ( ctx->tc_warn_only ) {
1943 status = tlsm_verify_cert( ctx->tc_certdb, cert, pin_arg,
1944 checkSig, certUsage, errorToIgnore );
1945 if ( status == SECSuccess ) {
1949 PRErrorCode errcode = PR_GetError();
1950 Debug( LDAP_DEBUG_ANY,
1951 "TLS: error: could not find the private key for certificate %s - error %d:%s\n",
1952 certname, errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1956 *pRetCert = cert; /* caller will deal with this */
1958 CERT_DestroyCertificate( cert );
1965 tlsm_get_client_auth_data( void *arg, PRFileDesc *fd,
1966 CERTDistNames *caNames, CERTCertificate **pRetCert,
1967 SECKEYPrivateKey **pRetKey )
1969 tlsm_ctx *ctx = (tlsm_ctx *)arg;
1973 /* don't need caNames - this function will call CERT_VerifyCertificateNow
1974 which will verify the cert against the known CAs */
1975 saveval = ctx->tc_warn_only;
1976 ctx->tc_warn_only = PR_TRUE;
1977 rc = tlsm_find_and_verify_cert_key( ctx, fd, ctx->tc_certname, 0, pRetCert, pRetKey );
1978 ctx->tc_warn_only = saveval;
1980 Debug( LDAP_DEBUG_ANY,
1981 "TLS: error: unable to perform client certificate authentication for "
1982 "certificate named %s\n", ctx->tc_certname, 0, 0 );
1983 if ( pRetKey && *pRetKey ) {
1984 SECKEY_DestroyPrivateKey( *pRetKey );
1987 if ( pRetCert && *pRetCert ) {
1988 CERT_DestroyCertificate( *pRetCert );
1998 * ctx must have a tc_model that is valid
1999 * certname is in the form [<tokenname>:]<certnickname>
2000 * where <tokenname> is the name of the PKCS11 token
2001 * and <certnickname> is the nickname of the cert/key in
2005 tlsm_clientauth_init( tlsm_ctx *ctx )
2007 SECStatus status = SECFailure;
2011 saveval = ctx->tc_warn_only;
2012 ctx->tc_warn_only = PR_TRUE;
2013 rc = tlsm_find_and_verify_cert_key( ctx, ctx->tc_model, ctx->tc_certname, 0, NULL, NULL );
2014 ctx->tc_warn_only = saveval;
2016 Debug( LDAP_DEBUG_ANY,
2017 "TLS: error: unable to set up client certificate authentication for "
2018 "certificate named %s\n", ctx->tc_certname, 0, 0 );
2022 status = SSL_GetClientAuthDataHook( ctx->tc_model,
2023 tlsm_get_client_auth_data,
2026 return ( status == SECSuccess ? 0 : -1 );
2030 * Tear down the TLS subsystem. Should only be called once.
2033 tlsm_destroy( void )
2035 #ifdef LDAP_R_COMPILE
2036 ldap_pvt_thread_mutex_destroy( &tlsm_ctx_count_mutex );
2037 ldap_pvt_thread_mutex_destroy( &tlsm_init_mutex );
2038 ldap_pvt_thread_mutex_destroy( &tlsm_pem_mutex );
2042 static struct ldaptls *
2043 tlsm_copy_config ( const struct ldaptls *config )
2045 struct ldaptls *copy;
2049 copy = LDAP_MALLOC(sizeof(*copy));
2053 memset(copy, 0, sizeof(*copy));
2055 if (config->lt_certfile)
2056 copy->lt_certfile = LDAP_STRDUP(config->lt_certfile);
2057 if (config->lt_keyfile)
2058 copy->lt_keyfile = LDAP_STRDUP(config->lt_keyfile);
2059 if (config->lt_dhfile)
2060 copy->lt_dhfile = LDAP_STRDUP(config->lt_dhfile);
2061 if (config->lt_cacertfile)
2062 copy->lt_cacertfile = LDAP_STRDUP(config->lt_cacertfile);
2063 if (config->lt_cacertdir)
2064 copy->lt_cacertdir = LDAP_STRDUP(config->lt_cacertdir);
2065 if (config->lt_ciphersuite)
2066 copy->lt_ciphersuite = LDAP_STRDUP(config->lt_ciphersuite);
2067 if (config->lt_crlfile)
2068 copy->lt_crlfile = LDAP_STRDUP(config->lt_crlfile);
2069 if (config->lt_randfile)
2070 copy->lt_randfile = LDAP_STRDUP(config->lt_randfile);
2072 copy->lt_protocol_min = config->lt_protocol_min;
2078 tlsm_free_config ( struct ldaptls *config )
2082 if (config->lt_certfile)
2083 LDAP_FREE(config->lt_certfile);
2084 if (config->lt_keyfile)
2085 LDAP_FREE(config->lt_keyfile);
2086 if (config->lt_dhfile)
2087 LDAP_FREE(config->lt_dhfile);
2088 if (config->lt_cacertfile)
2089 LDAP_FREE(config->lt_cacertfile);
2090 if (config->lt_cacertdir)
2091 LDAP_FREE(config->lt_cacertdir);
2092 if (config->lt_ciphersuite)
2093 LDAP_FREE(config->lt_ciphersuite);
2094 if (config->lt_crlfile)
2095 LDAP_FREE(config->lt_crlfile);
2096 if (config->lt_randfile)
2097 LDAP_FREE(config->lt_randfile);
2103 tlsm_ctx_new ( struct ldapoptions *lo )
2107 ctx = LDAP_MALLOC( sizeof (*ctx) );
2110 #ifdef LDAP_R_COMPILE
2111 ldap_pvt_thread_mutex_init( &ctx->tc_refmutex );
2113 LDAP_MUTEX_LOCK( &tlsm_ctx_count_mutex );
2114 ctx->tc_unique = tlsm_ctx_count++;
2115 LDAP_MUTEX_UNLOCK( &tlsm_ctx_count_mutex );
2116 ctx->tc_config = NULL; /* populated later by tlsm_ctx_init */
2117 ctx->tc_certdb = NULL;
2118 ctx->tc_certdb_slot = NULL;
2119 ctx->tc_certname = NULL;
2120 ctx->tc_pin_file = NULL;
2121 ctx->tc_model = NULL;
2122 memset(&ctx->tc_callonce, 0, sizeof(ctx->tc_callonce));
2123 ctx->tc_require_cert = lo->ldo_tls_require_cert;
2124 ctx->tc_verify_cert = PR_FALSE;
2125 ctx->tc_using_pem = PR_FALSE;
2126 ctx->tc_slotname = NULL;
2127 #ifdef HAVE_NSS_INITCONTEXT
2128 ctx->tc_initctx = NULL;
2129 #endif /* HAVE_NSS_INITCONTEXT */
2130 ctx->tc_pem_objs = NULL;
2131 ctx->tc_n_pem_objs = 0;
2132 ctx->tc_warn_only = PR_FALSE;
2134 return (tls_ctx *)ctx;
2138 tlsm_ctx_ref( tls_ctx *ctx )
2140 tlsm_ctx *c = (tlsm_ctx *)ctx;
2141 LDAP_MUTEX_LOCK( &c->tc_refmutex );
2143 LDAP_MUTEX_UNLOCK( &c->tc_refmutex );
2147 tlsm_ctx_free ( tls_ctx *ctx )
2149 tlsm_ctx *c = (tlsm_ctx *)ctx;
2154 LDAP_MUTEX_LOCK( &c->tc_refmutex );
2155 refcount = --c->tc_refcnt;
2156 LDAP_MUTEX_UNLOCK( &c->tc_refmutex );
2160 PR_Close( c->tc_model );
2161 c->tc_certdb = NULL; /* if not the default, may have to clean up */
2162 if ( c->tc_certdb_slot ) {
2163 if ( SECMOD_CloseUserDB( c->tc_certdb_slot ) ) {
2164 PRErrorCode errcode = PR_GetError();
2165 Debug( LDAP_DEBUG_ANY,
2166 "TLS: could not close certdb slot - error %d:%s.\n",
2167 errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ), 0 );
2170 PL_strfree( c->tc_certname );
2171 c->tc_certname = NULL;
2172 PL_strfree( c->tc_pin_file );
2173 c->tc_pin_file = NULL;
2174 PL_strfree( c->tc_slotname );
2175 tlsm_free_pem_objs( c );
2176 #ifdef HAVE_NSS_INITCONTEXT
2177 if ( c->tc_initctx ) {
2178 LDAP_MUTEX_LOCK( &tlsm_init_mutex );
2179 if ( NSS_ShutdownContext( c->tc_initctx ) ) {
2180 PRErrorCode errcode = PR_GetError();
2181 Debug( LDAP_DEBUG_ANY,
2182 "TLS: could not shutdown NSS - error %d:%s.\n",
2183 errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ), 0 );
2185 LDAP_MUTEX_UNLOCK( &tlsm_init_mutex );
2187 c->tc_initctx = NULL;
2188 #endif /* HAVE_NSS_INITCONTEXT */
2189 #ifdef LDAP_R_COMPILE
2190 ldap_pvt_thread_mutex_destroy( &c->tc_refmutex );
2194 tlsm_free_config( c->tc_config );
2200 * initialize a new TLS context
2203 tlsm_ctx_init( struct ldapoptions *lo, struct ldaptls *lt, int is_server )
2205 tlsm_ctx *ctx = (tlsm_ctx *)lo->ldo_tls_ctx;
2206 ctx->tc_config = tlsm_copy_config(lt);
2207 ctx->tc_is_server = is_server;
2213 tlsm_deferred_ctx_init( void *arg )
2215 tlsm_ctx *ctx = (tlsm_ctx *)arg;
2216 PRBool sslv2 = PR_FALSE;
2217 PRBool sslv3 = PR_TRUE;
2218 PRBool tlsv1 = PR_TRUE;
2219 PRBool request_cert = PR_FALSE;
2220 PRInt32 require_cert = PR_FALSE;
2224 if ( tlsm_deferred_init( ctx ) ) {
2225 Debug( LDAP_DEBUG_ANY,
2226 "TLS: could not perform TLS system initialization.\n",
2231 ctx->tc_certdb = CERT_GetDefaultCertDB(); /* If there is ever a per-context db, change this */
2233 fd = PR_CreateIOLayerStub( tlsm_layer_id, &tlsm_PR_methods );
2235 ctx->tc_model = SSL_ImportFD( NULL, fd );
2238 if ( !ctx->tc_model ) {
2239 PRErrorCode err = PR_GetError();
2240 Debug( LDAP_DEBUG_ANY,
2241 "TLS: could perform TLS socket I/O layer initialization - error %d:%s.\n",
2242 err, PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ), NULL );
2250 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_SECURITY, PR_TRUE ) ) {
2251 Debug( LDAP_DEBUG_ANY,
2252 "TLS: could not set secure mode on.\n",
2257 lt = ctx->tc_config;
2259 /* default is sslv3 and tlsv1 */
2260 if ( lt->lt_protocol_min ) {
2261 if ( lt->lt_protocol_min > LDAP_OPT_X_TLS_PROTOCOL_SSL3 ) {
2263 } else if ( lt->lt_protocol_min <= LDAP_OPT_X_TLS_PROTOCOL_SSL2 ) {
2265 Debug( LDAP_DEBUG_ANY,
2266 "TLS: warning: minimum TLS protocol level set to "
2267 "include SSLv2 - SSLv2 is insecure - do not use\n", 0, 0, 0 );
2270 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_ENABLE_SSL2, sslv2 ) ) {
2271 Debug( LDAP_DEBUG_ANY,
2272 "TLS: could not set SSLv2 mode on.\n",
2276 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_ENABLE_SSL3, sslv3 ) ) {
2277 Debug( LDAP_DEBUG_ANY,
2278 "TLS: could not set SSLv3 mode on.\n",
2282 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_ENABLE_TLS, tlsv1 ) ) {
2283 Debug( LDAP_DEBUG_ANY,
2284 "TLS: could not set TLSv1 mode on.\n",
2289 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_HANDSHAKE_AS_CLIENT, !ctx->tc_is_server ) ) {
2290 Debug( LDAP_DEBUG_ANY,
2291 "TLS: could not set handshake as client.\n",
2295 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_HANDSHAKE_AS_SERVER, ctx->tc_is_server ) ) {
2296 Debug( LDAP_DEBUG_ANY,
2297 "TLS: could not set handshake as server.\n",
2302 if ( lt->lt_ciphersuite ) {
2303 if ( tlsm_parse_ciphers( ctx, lt->lt_ciphersuite ) ) {
2304 Debug( LDAP_DEBUG_ANY,
2305 "TLS: could not set cipher list %s.\n",
2306 lt->lt_ciphersuite, 0, 0 );
2309 } else if ( tlsm_parse_ciphers( ctx, "DEFAULT" ) ) {
2310 Debug( LDAP_DEBUG_ANY,
2311 "TLS: could not set cipher list DEFAULT.\n",
2316 if ( !ctx->tc_require_cert ) {
2317 ctx->tc_verify_cert = PR_FALSE;
2318 } else if ( !ctx->tc_is_server ) {
2319 request_cert = PR_TRUE;
2320 require_cert = SSL_REQUIRE_NO_ERROR;
2321 if ( ctx->tc_require_cert == LDAP_OPT_X_TLS_DEMAND ||
2322 ctx->tc_require_cert == LDAP_OPT_X_TLS_HARD ) {
2323 require_cert = SSL_REQUIRE_ALWAYS;
2325 if ( ctx->tc_require_cert != LDAP_OPT_X_TLS_ALLOW )
2326 ctx->tc_verify_cert = PR_TRUE;
2327 } else { /* server */
2328 /* server does not request certs by default */
2329 /* if allow - client may send cert, server will ignore if errors */
2330 /* if try - client may send cert, server will error if bad cert */
2331 /* if hard or demand - client must send cert, server will error if bad cert */
2332 request_cert = PR_TRUE;
2333 require_cert = SSL_REQUIRE_NO_ERROR;
2334 if ( ctx->tc_require_cert == LDAP_OPT_X_TLS_DEMAND ||
2335 ctx->tc_require_cert == LDAP_OPT_X_TLS_HARD ) {
2336 require_cert = SSL_REQUIRE_ALWAYS;
2338 if ( ctx->tc_require_cert != LDAP_OPT_X_TLS_ALLOW ) {
2339 ctx->tc_verify_cert = PR_TRUE;
2341 ctx->tc_warn_only = PR_TRUE;
2345 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_REQUEST_CERTIFICATE, request_cert ) ) {
2346 Debug( LDAP_DEBUG_ANY,
2347 "TLS: could not set request certificate mode.\n",
2352 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_REQUIRE_CERTIFICATE, require_cert ) ) {
2353 Debug( LDAP_DEBUG_ANY,
2354 "TLS: could not set require certificate mode.\n",
2359 /* set up our cert and key, if any */
2360 if ( lt->lt_certfile ) {
2361 /* if using the PEM module, load the PEM file specified by lt_certfile */
2362 /* otherwise, assume this is the name of a cert already in the db */
2363 if ( ctx->tc_using_pem ) {
2364 /* this sets ctx->tc_certname to the correct value */
2365 int rc = tlsm_add_cert_from_file( ctx, lt->lt_certfile, PR_FALSE, PR_TRUE );
2372 if (ctx->tc_certdb_slot) {
2373 tmp_certname = PR_smprintf(TLSM_CERTDB_DESC_FMT ":%s", ctx->tc_unique, lt->lt_certfile);
2374 ctx->tc_certname = PL_strdup( tmp_certname );
2375 PR_smprintf_free(tmp_certname);
2377 ctx->tc_certname = PL_strdup( lt->lt_certfile );
2382 if ( lt->lt_keyfile ) {
2383 /* if using the PEM module, load the PEM file specified by lt_keyfile */
2384 /* otherwise, assume this is the pininfo for the key */
2385 if ( ctx->tc_using_pem ) {
2386 /* this sets ctx->tc_certname to the correct value */
2387 int rc = tlsm_add_key_from_file( ctx, lt->lt_keyfile );
2392 PL_strfree( ctx->tc_pin_file );
2393 ctx->tc_pin_file = PL_strdup( lt->lt_keyfile );
2397 /* Set up callbacks for use by clients */
2398 if ( !ctx->tc_is_server ) {
2399 if ( SSL_OptionSet( ctx->tc_model, SSL_NO_CACHE, PR_TRUE ) != SECSuccess ) {
2400 PRErrorCode err = PR_GetError();
2401 Debug( LDAP_DEBUG_ANY,
2402 "TLS: error: could not set nocache option for moznss - error %d:%s\n",
2403 err, PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ), NULL );
2407 if ( SSL_BadCertHook( ctx->tc_model, tlsm_bad_cert_handler, ctx ) != SECSuccess ) {
2408 PRErrorCode err = PR_GetError();
2409 Debug( LDAP_DEBUG_ANY,
2410 "TLS: error: could not set bad cert handler for moznss - error %d:%s\n",
2411 err, PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ), NULL );
2416 since a cert has been specified, assume the client wants to do cert auth
2418 if ( ctx->tc_certname ) {
2419 if ( tlsm_authenticate( ctx, ctx->tc_certname, ctx->tc_pin_file ) ) {
2420 Debug( LDAP_DEBUG_ANY,
2421 "TLS: error: unable to authenticate to the security device for certificate %s\n",
2422 ctx->tc_certname, 0, 0 );
2425 if ( tlsm_clientauth_init( ctx ) ) {
2426 Debug( LDAP_DEBUG_ANY,
2427 "TLS: error: unable to set up client certificate authentication using %s\n",
2428 ctx->tc_certname, 0, 0 );
2432 } else { /* set up secure server */
2434 CERTCertificate *serverCert = NULL;
2435 SECKEYPrivateKey *serverKey = NULL;
2438 /* must have a certificate for the server to use */
2439 if ( !ctx->tc_certname ) {
2440 Debug( LDAP_DEBUG_ANY,
2441 "TLS: error: no server certificate: must specify a certificate for the server to use\n",
2446 /* authenticate to the server's token - this will do nothing
2447 if the key/cert db is not password protected */
2448 if ( tlsm_authenticate( ctx, ctx->tc_certname, ctx->tc_pin_file ) ) {
2449 Debug( LDAP_DEBUG_ANY,
2450 "TLS: error: unable to authenticate to the security device for certificate %s\n",
2451 ctx->tc_certname, 0, 0 );
2455 /* get the server's key and cert */
2456 if ( tlsm_find_and_verify_cert_key( ctx, ctx->tc_model, ctx->tc_certname, ctx->tc_is_server,
2457 &serverCert, &serverKey ) ) {
2458 Debug( LDAP_DEBUG_ANY,
2459 "TLS: error: unable to find and verify server's cert and key for certificate %s\n",
2460 ctx->tc_certname, 0, 0 );
2461 CERT_DestroyCertificate( serverCert );
2462 SECKEY_DestroyPrivateKey( serverKey );
2466 certKEA = NSS_FindCertKEAType( serverCert );
2467 /* configure the socket to be a secure server socket */
2468 status = SSL_ConfigSecureServer( ctx->tc_model, serverCert, serverKey, certKEA );
2469 /* SSL_ConfigSecureServer copies these */
2470 CERT_DestroyCertificate( serverCert );
2471 SECKEY_DestroyPrivateKey( serverKey );
2473 if ( SECSuccess != status ) {
2474 PRErrorCode err = PR_GetError();
2475 Debug( LDAP_DEBUG_ANY,
2476 "TLS: error: unable to configure secure server using certificate %s - error %d:%s\n",
2477 ctx->tc_certname, err, PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ) );
2482 /* Callback for authenticating certificate */
2483 if ( SSL_AuthCertificateHook( ctx->tc_model, tlsm_auth_cert_handler,
2484 ctx ) != SECSuccess ) {
2485 PRErrorCode err = PR_GetError();
2486 Debug( LDAP_DEBUG_ANY,
2487 "TLS: error: could not set auth cert handler for moznss - error %d:%s\n",
2488 err, PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ), NULL );
2492 if ( SSL_HandshakeCallback( ctx->tc_model, tlsm_handshake_complete_cb, ctx ) ) {
2493 PRErrorCode err = PR_GetError();
2494 Debug( LDAP_DEBUG_ANY,
2495 "TLS: error: could not set handshake callback for moznss - error %d:%s\n",
2496 err, PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ), NULL );
2500 tlsm_free_config( ctx->tc_config );
2501 ctx->tc_config = NULL;
2507 tlsm_session *session;
2508 Sockbuf_IO_Desc *sbiod;
2509 /* there seems to be no portable way to determine if the
2510 sockbuf sd has been set to nonblocking mode - the
2511 call to ber_pvt_socket_set_nonblock() takes place
2512 before the tls socket is set up, so we cannot
2513 intercept that call either.
2514 On systems where fcntl is available, we can just
2515 F_GETFL and test for O_NONBLOCK. On other systems,
2516 we will just see if the IO op returns EAGAIN or EWOULDBLOCK,
2517 and just set this flag */
2520 * NSS tries hard to be backwards compatible with SSLv2 clients, or
2521 * clients that send an SSLv2 client hello. This message is not
2522 * tagged in any way, so NSS has no way to know if the incoming
2523 * message is a valid SSLv2 client hello or just some bogus data
2524 * (or cleartext LDAP). We store the first byte read from the
2525 * client here. The most common case will be a client sending
2526 * LDAP data instead of SSL encrypted LDAP data. This can happen,
2527 * for example, if using ldapsearch -Z - if the starttls fails,
2528 * the client will fallback to plain cleartext LDAP. So if we
2529 * see that the firstbyte is a valid LDAP tag, we can be
2530 * pretty sure this is happening.
2534 * NSS doesn't return SSL_ERROR_WANT_READ, SSL_ERROR_WANT_WRITE, etc.
2535 * when it is blocked, so we have to set a flag in the wrapped send
2536 * and recv calls that tells us what operation NSS was last blocked
2540 #define TLSM_WRITE 2
2544 static struct tls_data *
2545 tlsm_get_pvt_tls_data( PRFileDesc *fd )
2554 myfd = PR_GetIdentitiesLayer( fd, tlsm_layer_id );
2560 p = (struct tls_data *)myfd->secret;
2566 tlsm_is_non_ssl_message( PRFileDesc *fd, ber_tag_t *thebyte )
2571 *thebyte = LBER_DEFAULT;
2574 p = tlsm_get_pvt_tls_data( fd );
2575 if ( p == NULL || p->sbiod == NULL ) {
2579 if ( p->firsttag == LBER_SEQUENCE ) {
2581 *thebyte = p->firsttag;
2589 static tls_session *
2590 tlsm_session_new ( tls_ctx * ctx, int is_server )
2592 tlsm_ctx *c = (tlsm_ctx *)ctx;
2593 tlsm_session *session;
2598 c->tc_is_server = is_server;
2599 status = PR_CallOnceWithArg( &c->tc_callonce, tlsm_deferred_ctx_init, c );
2600 if ( PR_SUCCESS != status ) {
2601 PRErrorCode err = PR_GetError();
2602 Debug( LDAP_DEBUG_ANY,
2603 "TLS: error: could not initialize moznss security context - error %d:%s\n",
2604 err, PR_ErrorToString(err, PR_LANGUAGE_I_DEFAULT), NULL );
2608 fd = PR_CreateIOLayerStub( tlsm_layer_id, &tlsm_PR_methods );
2613 session = SSL_ImportFD( c->tc_model, fd );
2619 rc = SSL_ResetHandshake( session, is_server );
2621 PRErrorCode err = PR_GetError();
2622 Debug( LDAP_DEBUG_TRACE,
2623 "TLS: error: new session - reset handshake failure %d - error %d:%s\n",
2625 err ? PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ) : "unknown" );
2627 PR_Close( session );
2631 return (tls_session *)session;
2635 tlsm_session_accept_or_connect( tls_session *session, int is_accept )
2637 tlsm_session *s = (tlsm_session *)session;
2639 const char *op = is_accept ? "accept" : "connect";
2642 LDAP_MUTEX_LOCK( &tlsm_pem_mutex );
2644 rc = SSL_ForceHandshake( s );
2646 LDAP_MUTEX_UNLOCK( &tlsm_pem_mutex );
2649 PRErrorCode err = PR_GetError();
2651 if ( err == PR_WOULD_BLOCK_ERROR ) {
2652 ber_tag_t thetag = LBER_DEFAULT;
2653 /* see if we are blocked because of a bogus packet */
2654 if ( tlsm_is_non_ssl_message( s, &thetag ) ) { /* see if we received a non-SSL message */
2655 Debug( LDAP_DEBUG_ANY,
2656 "TLS: error: %s - error - received non-SSL message [0x%x]\n",
2657 op, (unsigned int)thetag, 0 );
2658 /* reset error to something more descriptive */
2659 PR_SetError( SSL_ERROR_RX_MALFORMED_HELLO_REQUEST, EPROTO );
2662 Debug( LDAP_DEBUG_ANY,
2663 "TLS: error: %s - force handshake failure: errno %d - moznss error %d\n",
2671 tlsm_session_accept( tls_session *session )
2673 return tlsm_session_accept_or_connect( session, 1 );
2677 tlsm_session_connect( LDAP *ld, tls_session *session )
2679 return tlsm_session_accept_or_connect( session, 0 );
2683 tlsm_session_upflags( Sockbuf *sb, tls_session *session, int rc )
2685 int prerror = PR_GetError();
2687 if ( ( prerror == PR_PENDING_INTERRUPT_ERROR ) || ( prerror == PR_WOULD_BLOCK_ERROR ) ) {
2688 tlsm_session *s = (tlsm_session *)session;
2689 struct tls_data *p = tlsm_get_pvt_tls_data( s );
2691 if ( p && ( p->io_flag == TLSM_READ ) ) {
2692 sb->sb_trans_needs_read = 1;
2694 } else if ( p && ( p->io_flag == TLSM_WRITE ) ) {
2695 sb->sb_trans_needs_write = 1;
2704 tlsm_session_errmsg( tls_session *sess, int rc, char *buf, size_t len )
2707 int prerror = PR_GetError();
2709 i = PR_GetErrorTextLength();
2711 char *msg = LDAP_MALLOC( i+1 );
2712 PR_GetErrorText( msg );
2713 memcpy( buf, msg, len );
2716 PR_GetErrorText( buf );
2717 } else if ( prerror ) {
2718 i = PR_snprintf( buf, len, "TLS error %d:%s",
2719 prerror, PR_ErrorToString( prerror, PR_LANGUAGE_I_DEFAULT ) );
2722 return ( i > 0 ) ? buf : NULL;
2726 tlsm_session_my_dn( tls_session *session, struct berval *der_dn )
2728 tlsm_session *s = (tlsm_session *)session;
2729 CERTCertificate *cert;
2731 cert = SSL_LocalCertificate( s );
2732 if (!cert) return LDAP_INVALID_CREDENTIALS;
2734 der_dn->bv_val = (char *)cert->derSubject.data;
2735 der_dn->bv_len = cert->derSubject.len;
2736 CERT_DestroyCertificate( cert );
2741 tlsm_session_peer_dn( tls_session *session, struct berval *der_dn )
2743 tlsm_session *s = (tlsm_session *)session;
2744 CERTCertificate *cert;
2746 cert = SSL_PeerCertificate( s );
2747 if (!cert) return LDAP_INVALID_CREDENTIALS;
2749 der_dn->bv_val = (char *)cert->derSubject.data;
2750 der_dn->bv_len = cert->derSubject.len;
2751 CERT_DestroyCertificate( cert );
2755 /* what kind of hostname were we given? */
2761 tlsm_session_chkhost( LDAP *ld, tls_session *session, const char *name_in )
2763 tlsm_session *s = (tlsm_session *)session;
2764 CERTCertificate *cert;
2765 const char *name, *domain = NULL, *ptr;
2766 int ret, ntype = IS_DNS, nlen, dlen;
2767 #ifdef LDAP_PF_INET6
2768 struct in6_addr addr;
2770 struct in_addr addr;
2775 if( ldap_int_hostname &&
2776 ( !name_in || !strcasecmp( name_in, "localhost" ) ) )
2778 name = ldap_int_hostname;
2782 nlen = strlen( name );
2784 cert = SSL_PeerCertificate( s );
2786 Debug( LDAP_DEBUG_ANY,
2787 "TLS: unable to get peer certificate.\n",
2789 /* if this was a fatal condition, things would have
2790 * aborted long before now.
2792 return LDAP_SUCCESS;
2795 #ifdef LDAP_PF_INET6
2796 if (inet_pton(AF_INET6, name, &addr)) {
2800 if ((ptr = strrchr(name, '.')) && isdigit((unsigned char)ptr[1])) {
2801 if (inet_aton(name, (struct in_addr *)&addr)) ntype = IS_IP4;
2803 if (ntype == IS_DNS ) {
2804 domain = strchr( name, '.' );
2806 dlen = nlen - ( domain - name );
2809 ret = LDAP_LOCAL_ERROR;
2811 rv = CERT_FindCertExtension( cert, SEC_OID_X509_SUBJECT_ALT_NAME,
2813 if ( rv == SECSuccess && altname.data ) {
2815 CERTGeneralName *names, *cur;
2817 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
2819 ret = LDAP_NO_MEMORY;
2823 names = cur = CERT_DecodeAltNameExtension(arena, &altname);
2832 if ( !cur->name.other.len ) continue;
2834 host = (char *)cur->name.other.data;
2835 hlen = cur->name.other.len;
2837 if ( cur->type == certDNSName ) {
2838 if ( ntype != IS_DNS ) continue;
2840 /* is this an exact match? */
2841 if ( nlen == hlen && !strncasecmp( name, host, nlen )) {
2846 /* is this a wildcard match? */
2847 if ( domain && host[0] == '*' && host[1] == '.' &&
2848 dlen == hlen-1 && !strncasecmp( domain, host+1, dlen )) {
2852 } else if ( cur->type == certIPAddress ) {
2853 if ( ntype == IS_DNS ) continue;
2855 #ifdef LDAP_PF_INET6
2856 if (ntype == IS_IP6 && hlen != sizeof(struct in6_addr)) {
2860 if (ntype == IS_IP4 && hlen != sizeof(struct in_addr)) {
2863 if (!memcmp(host, &addr, hlen)) {
2868 } while (( cur = CERT_GetNextGeneralName( cur )) != names );
2870 PORT_FreeArena( arena, PR_FALSE );
2871 SECITEM_FreeItem( &altname, PR_FALSE );
2873 /* no altnames matched, try the CN */
2874 if ( ret != LDAP_SUCCESS ) {
2875 /* find the last CN */
2876 CERTRDN *rdn, **rdns;
2877 CERTAVA *lastava = NULL;
2881 rdns = cert->subject.rdns;
2882 while ( rdns && ( rdn = *rdns++ )) {
2883 CERTAVA *ava, **avas = rdn->avas;
2884 while ( avas && ( ava = *avas++ )) {
2885 if ( CERT_GetAVATag( ava ) == SEC_OID_AVA_COMMON_NAME )
2890 SECItem *av = CERT_DecodeAVAValue( &lastava->value );
2892 if ( av->len == nlen && !strncasecmp( name, (char *)av->data, nlen )) {
2894 } else if ( av->data[0] == '*' && av->data[1] == '.' &&
2895 domain && dlen == av->len - 1 && !strncasecmp( domain,
2896 (char *)(av->data+1), dlen )) {
2900 if ( len >= sizeof(buf) )
2901 len = sizeof(buf)-1;
2902 memcpy( buf, av->data, len );
2905 SECITEM_FreeItem( av, PR_TRUE );
2908 if ( ret != LDAP_SUCCESS ) {
2909 Debug( LDAP_DEBUG_ANY, "TLS: hostname (%s) does not match "
2910 "common name in certificate (%s).\n",
2912 ret = LDAP_CONNECT_ERROR;
2913 if ( ld->ld_error ) {
2914 LDAP_FREE( ld->ld_error );
2916 ld->ld_error = LDAP_STRDUP(
2917 _("TLS: hostname does not match CN in peer certificate"));
2922 CERT_DestroyCertificate( cert );
2927 tlsm_session_strength( tls_session *session )
2929 tlsm_session *s = (tlsm_session *)session;
2932 rc = SSL_SecurityStatus( s, NULL, NULL, NULL, &keySize,
2934 return rc ? 0 : keySize;
2938 * TLS support for LBER Sockbufs
2941 static PRStatus PR_CALLBACK
2942 tlsm_PR_Close(PRFileDesc *fd)
2944 int rc = PR_SUCCESS;
2946 /* we don't need to actually close anything here, just
2947 pop our io layer off the stack */
2948 fd->secret = NULL; /* must have been freed before calling PR_Close */
2950 fd = PR_PopIOLayer( fd, tlsm_layer_id );
2951 /* if we are not the last layer, pass the close along */
2956 rc = fd->methods->close( fd );
2959 /* we are the last layer - just call our dtor */
2966 static PRStatus PR_CALLBACK
2967 tlsm_PR_Shutdown(PRFileDesc *fd, PRShutdownHow how)
2969 int rc = PR_SUCCESS;
2972 rc = PR_Shutdown( fd->lower, how );
2978 static int PR_CALLBACK
2979 tlsm_PR_Recv(PRFileDesc *fd, void *buf, PRInt32 len, PRIntn flags,
2980 PRIntervalTime timeout)
2985 if ( buf == NULL || len <= 0 ) return 0;
2987 p = tlsm_get_pvt_tls_data( fd );
2989 if ( p == NULL || p->sbiod == NULL ) {
2993 rc = LBER_SBIOD_READ_NEXT( p->sbiod, buf, len );
2995 tlsm_map_error( errno );
2996 if ( errno == EAGAIN || errno == EWOULDBLOCK ) {
2997 p->nonblock = PR_TRUE; /* fd is using non-blocking io */
2998 } else if ( errno ) { /* real error */
2999 Debug( LDAP_DEBUG_TRACE,
3000 "TLS: error: tlsm_PR_Recv returned %d - error %d:%s\n",
3001 rc, errno, STRERROR(errno) );
3003 } else if ( ( rc > 0 ) && ( len > 0 ) && ( p->firsttag == LBER_DEFAULT ) ) {
3004 p->firsttag = (ber_tag_t)*((char *)buf);
3006 p->io_flag = TLSM_READ;
3011 static int PR_CALLBACK
3012 tlsm_PR_Send(PRFileDesc *fd, const void *buf, PRInt32 len, PRIntn flags,
3013 PRIntervalTime timeout)
3018 if ( buf == NULL || len <= 0 ) return 0;
3020 p = tlsm_get_pvt_tls_data( fd );
3022 if ( p == NULL || p->sbiod == NULL ) {
3026 rc = LBER_SBIOD_WRITE_NEXT( p->sbiod, (char *)buf, len );
3028 tlsm_map_error( errno );
3029 if ( errno == EAGAIN || errno == EWOULDBLOCK ) {
3030 p->nonblock = PR_TRUE;
3031 } else if ( errno ) { /* real error */
3032 Debug( LDAP_DEBUG_TRACE,
3033 "TLS: error: tlsm_PR_Send returned %d - error %d:%s\n",
3034 rc, errno, STRERROR(errno) );
3037 p->io_flag = TLSM_WRITE;
3042 static int PR_CALLBACK
3043 tlsm_PR_Read(PRFileDesc *fd, void *buf, PRInt32 len)
3045 return tlsm_PR_Recv( fd, buf, len, 0, PR_INTERVAL_NO_TIMEOUT );
3048 static int PR_CALLBACK
3049 tlsm_PR_Write(PRFileDesc *fd, const void *buf, PRInt32 len)
3051 return tlsm_PR_Send( fd, buf, len, 0, PR_INTERVAL_NO_TIMEOUT );
3054 static PRStatus PR_CALLBACK
3055 tlsm_PR_GetPeerName(PRFileDesc *fd, PRNetAddr *addr)
3060 p = tlsm_get_pvt_tls_data( fd );
3062 if ( p == NULL || p->sbiod == NULL ) {
3065 len = sizeof(PRNetAddr);
3066 return getpeername( p->sbiod->sbiod_sb->sb_fd, (struct sockaddr *)addr, &len );
3069 static PRStatus PR_CALLBACK
3070 tlsm_PR_GetSocketOption(PRFileDesc *fd, PRSocketOptionData *data)
3073 p = tlsm_get_pvt_tls_data( fd );
3075 if ( p == NULL || data == NULL ) {
3079 /* only the nonblocking option is supported at this time
3080 MozNSS SSL code needs it */
3081 if ( data->option != PR_SockOpt_Nonblocking ) {
3082 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
3086 int flags = fcntl( p->sbiod->sbiod_sb->sb_fd, F_GETFL );
3087 data->value.non_blocking = (flags & O_NONBLOCK) ? PR_TRUE : PR_FALSE;
3089 data->value.non_blocking = p->nonblock;
3094 static PRStatus PR_CALLBACK
3097 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
3101 static PRFileDesc * PR_CALLBACK
3104 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
3108 static PRInt16 PR_CALLBACK
3111 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
3115 static PRInt32 PR_CALLBACK
3118 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
3122 static PRInt64 PR_CALLBACK
3127 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
3132 static const PRIOMethods tlsm_PR_methods = {
3134 tlsm_PR_Close, /* close */
3135 tlsm_PR_Read, /* read */
3136 tlsm_PR_Write, /* write */
3137 tlsm_PR_i32_unimp, /* available */
3138 tlsm_PR_i64_unimp, /* available64 */
3139 tlsm_PR_prs_unimp, /* fsync */
3140 tlsm_PR_i32_unimp, /* seek */
3141 tlsm_PR_i64_unimp, /* seek64 */
3142 tlsm_PR_prs_unimp, /* fileInfo */
3143 tlsm_PR_prs_unimp, /* fileInfo64 */
3144 tlsm_PR_i32_unimp, /* writev */
3145 tlsm_PR_prs_unimp, /* connect */
3146 tlsm_PR_pfd_unimp, /* accept */
3147 tlsm_PR_prs_unimp, /* bind */
3148 tlsm_PR_prs_unimp, /* listen */
3149 (PRShutdownFN)tlsm_PR_Shutdown, /* shutdown */
3150 tlsm_PR_Recv, /* recv */
3151 tlsm_PR_Send, /* send */
3152 tlsm_PR_i32_unimp, /* recvfrom */
3153 tlsm_PR_i32_unimp, /* sendto */
3154 (PRPollFN)tlsm_PR_i16_unimp, /* poll */
3155 tlsm_PR_i32_unimp, /* acceptread */
3156 tlsm_PR_i32_unimp, /* transmitfile */
3157 tlsm_PR_prs_unimp, /* getsockname */
3158 tlsm_PR_GetPeerName, /* getpeername */
3159 tlsm_PR_i32_unimp, /* getsockopt OBSOLETE */
3160 tlsm_PR_i32_unimp, /* setsockopt OBSOLETE */
3161 tlsm_PR_GetSocketOption, /* getsocketoption */
3162 tlsm_PR_i32_unimp, /* setsocketoption */
3163 tlsm_PR_i32_unimp, /* Send a (partial) file with header/trailer*/
3164 (PRConnectcontinueFN)tlsm_PR_prs_unimp, /* connectcontinue */
3165 tlsm_PR_i32_unimp, /* reserved for future use */
3166 tlsm_PR_i32_unimp, /* reserved for future use */
3167 tlsm_PR_i32_unimp, /* reserved for future use */
3168 tlsm_PR_i32_unimp /* reserved for future use */
3172 * Initialize TLS subsystem. Should be called only once.
3173 * See tlsm_deferred_init for the bulk of the init process
3178 char *nofork = PR_GetEnv( "NSS_STRICT_NOFORK" );
3182 tlsm_layer_id = PR_GetUniqueIdentity( "OpenLDAP" );
3185 * There are some applications that acquire a crypto context in the parent process
3186 * and expect that crypto context to work after a fork(). This does not work
3187 * with NSS using strict PKCS11 compliance mode. We set this environment
3188 * variable here to tell the software encryption module/token to allow crypto
3189 * contexts to persist across a fork(). However, if you are using some other
3190 * module or encryption device that supports and expects full PKCS11 semantics,
3191 * the only recourse is to rewrite the application with atfork() handlers to save
3192 * the crypto context in the parent and restore (and SECMOD_RestartModules) the
3193 * context in the child.
3196 /* will leak one time */
3197 char *noforkenvvar = PL_strdup( "NSS_STRICT_NOFORK=DISABLED" );
3198 PR_SetEnv( noforkenvvar );
3205 tlsm_sb_setup( Sockbuf_IO_Desc *sbiod, void *arg )
3208 tlsm_session *session = arg;
3211 assert( sbiod != NULL );
3213 p = LBER_MALLOC( sizeof( *p ) );
3218 fd = PR_GetIdentitiesLayer( session, tlsm_layer_id );
3224 fd->secret = (PRFilePrivate *)p;
3225 p->session = session;
3227 p->firsttag = LBER_DEFAULT;
3228 sbiod->sbiod_pvt = p;
3233 tlsm_sb_remove( Sockbuf_IO_Desc *sbiod )
3237 assert( sbiod != NULL );
3238 assert( sbiod->sbiod_pvt != NULL );
3240 p = (struct tls_data *)sbiod->sbiod_pvt;
3241 PR_Close( p->session );
3242 LBER_FREE( sbiod->sbiod_pvt );
3243 sbiod->sbiod_pvt = NULL;
3248 tlsm_sb_close( Sockbuf_IO_Desc *sbiod )
3252 assert( sbiod != NULL );
3253 assert( sbiod->sbiod_pvt != NULL );
3255 p = (struct tls_data *)sbiod->sbiod_pvt;
3256 PR_Shutdown( p->session, PR_SHUTDOWN_BOTH );
3261 tlsm_sb_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
3265 assert( sbiod != NULL );
3266 assert( sbiod->sbiod_pvt != NULL );
3268 p = (struct tls_data *)sbiod->sbiod_pvt;
3270 if ( opt == LBER_SB_OPT_GET_SSL ) {
3271 *((tlsm_session **)arg) = p->session;
3274 } else if ( opt == LBER_SB_OPT_DATA_READY ) {
3275 if ( p && ( SSL_DataPending( p->session ) > 0 ) ) {
3281 return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg );
3285 tlsm_sb_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
3291 assert( sbiod != NULL );
3292 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
3294 p = (struct tls_data *)sbiod->sbiod_pvt;
3296 ret = PR_Recv( p->session, buf, len, 0, PR_INTERVAL_NO_TIMEOUT );
3298 err = PR_GetError();
3299 if ( err == PR_PENDING_INTERRUPT_ERROR || err == PR_WOULD_BLOCK_ERROR ) {
3300 sbiod->sbiod_sb->sb_trans_needs_read = 1;
3301 sock_errset(EWOULDBLOCK);
3304 sbiod->sbiod_sb->sb_trans_needs_read = 0;
3310 tlsm_sb_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
3316 assert( sbiod != NULL );
3317 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
3319 p = (struct tls_data *)sbiod->sbiod_pvt;
3321 ret = PR_Send( p->session, (char *)buf, len, 0, PR_INTERVAL_NO_TIMEOUT );
3323 err = PR_GetError();
3324 if ( err == PR_PENDING_INTERRUPT_ERROR || err == PR_WOULD_BLOCK_ERROR ) {
3325 sbiod->sbiod_sb->sb_trans_needs_write = 1;
3326 sock_errset(EWOULDBLOCK);
3330 sbiod->sbiod_sb->sb_trans_needs_write = 0;
3335 static Sockbuf_IO tlsm_sbio =
3337 tlsm_sb_setup, /* sbi_setup */
3338 tlsm_sb_remove, /* sbi_remove */
3339 tlsm_sb_ctrl, /* sbi_ctrl */
3340 tlsm_sb_read, /* sbi_read */
3341 tlsm_sb_write, /* sbi_write */
3342 tlsm_sb_close /* sbi_close */
3345 tls_impl ldap_int_tls_impl = {
3357 tlsm_session_connect,
3358 tlsm_session_accept,
3359 tlsm_session_upflags,
3360 tlsm_session_errmsg,
3362 tlsm_session_peer_dn,
3363 tlsm_session_chkhost,
3364 tlsm_session_strength,
3368 #ifdef LDAP_R_COMPILE
3377 #endif /* HAVE_MOZNSS */