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;
88 CERTCertificate *tc_certificate;
89 SECKEYPrivateKey *tc_private_key;
91 struct ldaptls *tc_config;
94 PRCallOnceType tc_callonce;
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 #define TLSM_PEM_SLOT_CACERTS "PEM Token #0"
129 #define TLSM_PEM_SLOT_CERTS "PEM Token #1"
131 #define PK11_SETATTRS(x,id,v,l) (x).type = (id); \
132 (x).pValue=(v); (x).ulValueLen = (l);
134 /* forward declaration */
135 static int tlsm_init( void );
137 #ifdef LDAP_R_COMPILE
139 /* it doesn't seem guaranteed that a client will call
140 tlsm_thr_init in a non-threaded context - so we have
141 to wrap the mutex creation in a prcallonce
143 static ldap_pvt_thread_mutex_t tlsm_ctx_count_mutex;
144 static ldap_pvt_thread_mutex_t tlsm_init_mutex;
145 static ldap_pvt_thread_mutex_t tlsm_pem_mutex;
146 static PRCallOnceType tlsm_init_mutex_callonce = {0,0};
148 static PRStatus PR_CALLBACK
149 tlsm_thr_init_callonce( void )
151 if ( ldap_pvt_thread_mutex_init( &tlsm_ctx_count_mutex ) ) {
152 Debug( LDAP_DEBUG_ANY,
153 "TLS: could not create mutex for context counter: %d\n", errno, 0, 0 );
157 if ( ldap_pvt_thread_mutex_init( &tlsm_init_mutex ) ) {
158 Debug( LDAP_DEBUG_ANY,
159 "TLS: could not create mutex for moznss initialization: %d\n", errno, 0, 0 );
163 if ( ldap_pvt_thread_mutex_init( &tlsm_pem_mutex ) ) {
164 Debug( LDAP_DEBUG_ANY,
165 "TLS: could not create mutex for PEM module: %d\n", errno, 0, 0 );
173 tlsm_thr_init( void )
175 ( void )PR_CallOnce( &tlsm_init_mutex_callonce, tlsm_thr_init_callonce );
178 #endif /* LDAP_R_COMPILE */
181 tlsm_dump_cipher_info(PRFileDesc *fd)
185 for (ii = 0; ii < SSL_NumImplementedCiphers; ++ii) {
186 PRInt32 cipher = (PRInt32)SSL_ImplementedCiphers[ii];
187 PRBool enabled = PR_FALSE;
189 SSLCipherSuiteInfo info;
192 SSL_CipherPrefGet(fd, cipher, &enabled);
194 SSL_CipherPrefGetDefault(cipher, &enabled);
196 SSL_CipherPolicyGet(cipher, &policy);
197 SSL_GetCipherSuiteInfo(cipher, &info, (PRUintn)sizeof(info));
198 Debug( LDAP_DEBUG_TRACE,
199 "TLS: cipher: %d - %s, enabled: %d, ",
200 info.cipherSuite, info.cipherSuiteName, enabled );
201 Debug( LDAP_DEBUG_TRACE,
202 "policy: %d\n", policy, 0, 0 );
208 /* Cipher definitions */
210 char *ossl_name; /* The OpenSSL cipher name */
211 int num; /* The cipher id */
212 int attr; /* cipher attributes: algorithms, etc */
213 int version; /* protocol version valid for this cipher */
214 int bits; /* bits of strength */
215 int alg_bits; /* bits of the algorithm */
216 int strength; /* LOW, MEDIUM, HIGH */
217 int enabled; /* Enabled by default? */
220 /* cipher attributes */
221 #define SSL_kRSA 0x00000001L
222 #define SSL_aRSA 0x00000002L
223 #define SSL_aDSS 0x00000004L
224 #define SSL_DSS SSL_aDSS
225 #define SSL_eNULL 0x00000008L
226 #define SSL_DES 0x00000010L
227 #define SSL_3DES 0x00000020L
228 #define SSL_RC4 0x00000040L
229 #define SSL_RC2 0x00000080L
230 #define SSL_AES 0x00000100L
231 #define SSL_MD5 0x00000200L
232 #define SSL_SHA1 0x00000400L
233 #define SSL_SHA SSL_SHA1
234 #define SSL_RSA (SSL_kRSA|SSL_aRSA)
236 /* cipher strength */
237 #define SSL_NULL 0x00000001L
238 #define SSL_EXPORT40 0x00000002L
239 #define SSL_EXPORT56 0x00000004L
240 #define SSL_LOW 0x00000008L
241 #define SSL_MEDIUM 0x00000010L
242 #define SSL_HIGH 0x00000020L
244 #define SSL2 0x00000001L
245 #define SSL3 0x00000002L
246 /* OpenSSL treats SSL3 and TLSv1 the same */
249 /* Cipher translation */
250 static cipher_properties ciphers_def[] = {
252 {"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},
253 {"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},
254 {"RC4-MD5", SSL_EN_RC4_128_WITH_MD5, SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_MD5, SSL2, 128, 128, SSL_MEDIUM, SSL_ALLOWED},
255 {"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},
256 {"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},
257 {"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},
260 {"RC4-MD5", SSL_RSA_WITH_RC4_128_MD5, SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_MD5, SSL3, 128, 128, SSL_MEDIUM, SSL_ALLOWED},
261 {"RC4-SHA", SSL_RSA_WITH_RC4_128_SHA, SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_SHA1, SSL3, 128, 128, SSL_MEDIUM, SSL_ALLOWED},
262 {"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},
263 {"DES-CBC-SHA", SSL_RSA_WITH_DES_CBC_SHA, SSL_kRSA|SSL_aRSA|SSL_DES|SSL_SHA1, SSL3, 56, 56, SSL_LOW, SSL_ALLOWED},
264 {"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},
265 {"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},
266 {"NULL-MD5", SSL_RSA_WITH_NULL_MD5, SSL_kRSA|SSL_aRSA|SSL_eNULL|SSL_MD5, SSL3, 0, 0, SSL_NULL, SSL_NOT_ALLOWED},
267 {"NULL-SHA", SSL_RSA_WITH_NULL_SHA, SSL_kRSA|SSL_aRSA|SSL_eNULL|SSL_SHA1, SSL3, 0, 0, SSL_NULL, SSL_NOT_ALLOWED},
270 {"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},
271 {"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},
272 {"AES128-SHA", TLS_RSA_WITH_AES_128_CBC_SHA, SSL_kRSA|SSL_aRSA|SSL_AES|SSL_SHA, TLS1, 128, 128, SSL_HIGH, SSL_ALLOWED},
273 {"AES256-SHA", TLS_RSA_WITH_AES_256_CBC_SHA, SSL_kRSA|SSL_aRSA|SSL_AES|SSL_SHA, TLS1, 256, 256, SSL_HIGH, SSL_ALLOWED},
276 #define ciphernum (sizeof(ciphers_def)/sizeof(cipher_properties))
278 /* given err which is the current errno, calls PR_SetError with
279 the corresponding NSPR error code */
281 tlsm_map_error(int err)
287 prError = PR_NO_ACCESS_RIGHTS_ERROR;
290 prError = PR_ADDRESS_IN_USE_ERROR;
293 prError = PR_ADDRESS_NOT_AVAILABLE_ERROR;
296 prError = PR_ADDRESS_NOT_SUPPORTED_ERROR;
299 prError = PR_WOULD_BLOCK_ERROR;
302 * On QNX and Neutrino, EALREADY is defined as EBUSY.
304 #if EALREADY != EBUSY
306 prError = PR_ALREADY_INITIATED_ERROR;
310 prError = PR_BAD_DESCRIPTOR_ERROR;
314 prError = PR_IO_ERROR;
318 prError = PR_FILESYSTEM_MOUNTED_ERROR;
321 prError = PR_CONNECT_ABORTED_ERROR;
324 prError = PR_CONNECT_REFUSED_ERROR;
327 prError = PR_CONNECT_RESET_ERROR;
330 prError = PR_DEADLOCK_ERROR;
334 prError = PR_DIRECTORY_CORRUPTED_ERROR;
339 prError = PR_NO_DEVICE_SPACE_ERROR;
343 prError = PR_FILE_EXISTS_ERROR;
346 prError = PR_ACCESS_FAULT_ERROR;
349 prError = PR_FILE_TOO_BIG_ERROR;
352 prError = PR_HOST_UNREACHABLE_ERROR;
355 prError = PR_IN_PROGRESS_ERROR;
358 prError = PR_PENDING_INTERRUPT_ERROR;
361 prError = PR_INVALID_ARGUMENT_ERROR;
364 prError = PR_IO_ERROR;
367 prError = PR_IS_CONNECTED_ERROR;
370 prError = PR_IS_DIRECTORY_ERROR;
373 prError = PR_LOOP_ERROR;
376 prError = PR_PROC_DESC_TABLE_FULL_ERROR;
379 prError = PR_MAX_DIRECTORY_ENTRIES_ERROR;
382 prError = PR_INVALID_ARGUMENT_ERROR;
386 prError = PR_REMOTE_FILE_ERROR;
390 prError = PR_NAME_TOO_LONG_ERROR;
393 prError = PR_NETWORK_UNREACHABLE_ERROR;
396 prError = PR_SYS_DESC_TABLE_FULL_ERROR;
399 * On SCO OpenServer 5, ENOBUFS is defined as ENOSR.
401 #if defined(ENOBUFS) && (ENOBUFS != ENOSR)
403 prError = PR_INSUFFICIENT_RESOURCES_ERROR;
407 prError = PR_FILE_NOT_FOUND_ERROR;
410 prError = PR_FILE_NOT_FOUND_ERROR;
413 prError = PR_FILE_IS_LOCKED_ERROR;
417 prError = PR_REMOTE_FILE_ERROR;
421 prError = PR_OUT_OF_MEMORY_ERROR;
424 prError = PR_INVALID_ARGUMENT_ERROR;
427 prError = PR_NO_DEVICE_SPACE_ERROR;
431 prError = PR_INSUFFICIENT_RESOURCES_ERROR;
435 prError = PR_NOT_CONNECTED_ERROR;
438 prError = PR_NOT_DIRECTORY_ERROR;
441 prError = PR_NOT_SOCKET_ERROR;
444 prError = PR_FILE_NOT_FOUND_ERROR;
447 prError = PR_NOT_TCP_SOCKET_ERROR;
451 prError = PR_BUFFER_OVERFLOW_ERROR;
455 prError = PR_NO_ACCESS_RIGHTS_ERROR;
458 prError = PR_CONNECT_RESET_ERROR;
462 prError = PR_IO_ERROR;
465 case EPROTONOSUPPORT:
466 prError = PR_PROTOCOL_NOT_SUPPORTED_ERROR;
469 prError = PR_ADDRESS_NOT_SUPPORTED_ERROR;
472 prError = PR_INVALID_METHOD_ERROR;
475 prError = PR_READ_ONLY_FILESYSTEM_ERROR;
478 prError = PR_INVALID_METHOD_ERROR;
481 prError = PR_IO_TIMEOUT_ERROR;
483 #if EWOULDBLOCK != EAGAIN
485 prError = PR_WOULD_BLOCK_ERROR;
489 prError = PR_NOT_SAME_DEVICE_ERROR;
492 prError = PR_UNKNOWN_ERROR;
495 PR_SetError( prError, err );
499 * cipher_list is an integer array with the following values:
500 * -1: never enable this cipher
505 nss_parse_ciphers(const char *cipherstr, int cipher_list[ciphernum])
514 /* All disabled to start */
515 for (i=0; i<ciphernum; i++)
518 ciphertip = strdup(cipherstr);
519 cipher = ciphers = ciphertip;
521 while (ciphers && (strlen(ciphers))) {
522 while ((*cipher) && (isspace(*cipher)))
527 case '+': /* Add something */
531 case '-': /* Subtract something */
535 case '!': /* Disable something */
544 if ((ciphers = strchr(cipher, ':'))) {
548 /* Do the easy one first */
549 if (!strcmp(cipher, "ALL")) {
550 for (i=0; i<ciphernum; i++) {
551 if (!(ciphers_def[i].attr & SSL_eNULL))
552 cipher_list[i] = action;
554 } else if (!strcmp(cipher, "COMPLEMENTOFALL")) {
555 for (i=0; i<ciphernum; i++) {
556 if ((ciphers_def[i].attr & SSL_eNULL))
557 cipher_list[i] = action;
559 } else if (!strcmp(cipher, "DEFAULT")) {
560 for (i=0; i<ciphernum; i++) {
561 cipher_list[i] = ciphers_def[i].enabled == SSL_ALLOWED ? 1 : 0;
570 while (c && (strlen(c))) {
572 if ((c = strchr(cipher, '+'))) {
576 if (!strcmp(cipher, "RSA")) {
578 } else if ((!strcmp(cipher, "NULL")) || (!strcmp(cipher, "eNULL"))) {
580 } else if (!strcmp(cipher, "AES")) {
582 } else if (!strcmp(cipher, "3DES")) {
584 } else if (!strcmp(cipher, "DES")) {
586 } else if (!strcmp(cipher, "RC4")) {
588 } else if (!strcmp(cipher, "RC2")) {
590 } else if (!strcmp(cipher, "MD5")) {
592 } else if ((!strcmp(cipher, "SHA")) || (!strcmp(cipher, "SHA1"))) {
594 } else if (!strcmp(cipher, "SSLv2")) {
596 } else if (!strcmp(cipher, "SSLv3")) {
598 } else if (!strcmp(cipher, "TLSv1")) {
600 } else if (!strcmp(cipher, "HIGH")) {
601 strength |= SSL_HIGH;
602 } else if (!strcmp(cipher, "MEDIUM")) {
603 strength |= SSL_MEDIUM;
604 } else if (!strcmp(cipher, "LOW")) {
606 } else if ((!strcmp(cipher, "EXPORT")) || (!strcmp(cipher, "EXP"))) {
607 strength |= SSL_EXPORT40|SSL_EXPORT56;
608 } else if (!strcmp(cipher, "EXPORT40")) {
609 strength |= SSL_EXPORT40;
610 } else if (!strcmp(cipher, "EXPORT56")) {
611 strength |= SSL_EXPORT56;
619 /* If we have a mask, apply it. If not then perhaps they provided
620 * a specific cipher to enable.
622 if (mask || strength || protocol) {
623 for (i=0; i<ciphernum; i++) {
624 if (((ciphers_def[i].attr & mask) ||
625 (ciphers_def[i].strength & strength) ||
626 (ciphers_def[i].version & protocol)) &&
627 (cipher_list[i] != -1)) {
628 /* Enable the NULL ciphers only if explicity
630 if (ciphers_def[i].attr & SSL_eNULL) {
631 if (mask & SSL_eNULL)
632 cipher_list[i] = action;
634 cipher_list[i] = action;
638 for (i=0; i<ciphernum; i++) {
639 if (!strcmp(ciphers_def[i].ossl_name, cipher) &&
640 cipher_list[i] != -1)
641 cipher_list[i] = action;
650 /* See if any ciphers were enabled */
652 for (i=0; i<ciphernum; i++) {
653 if (cipher_list[i] == 1)
663 tlsm_parse_ciphers(tlsm_ctx *ctx, const char *str)
665 int cipher_state[ciphernum];
671 rv = nss_parse_ciphers(str, cipher_state);
674 /* First disable everything */
675 for (i = 0; i < SSL_NumImplementedCiphers; i++)
676 SSL_CipherPrefSet(ctx->tc_model, SSL_ImplementedCiphers[i], SSL_NOT_ALLOWED);
678 /* Now enable what was requested */
679 for (i=0; i<ciphernum; i++) {
680 SSLCipherSuiteInfo suite;
683 if (SSL_GetCipherSuiteInfo(ciphers_def[i].num, &suite, sizeof suite)
685 enabled = cipher_state[i] < 0 ? 0 : cipher_state[i];
686 if (enabled == SSL_ALLOWED) {
687 if (PK11_IsFIPS() && !suite.isFIPS)
688 enabled = SSL_NOT_ALLOWED;
690 SSL_CipherPrefSet(ctx->tc_model, ciphers_def[i].num, enabled);
695 return rv == 1 ? 0 : -1;
699 tlsm_bad_cert_handler(void *arg, PRFileDesc *ssl)
701 SECStatus success = SECSuccess;
703 tlsm_ctx *ctx = (tlsm_ctx *)arg;
709 err = PORT_GetError();
712 case SEC_ERROR_UNTRUSTED_ISSUER:
713 case SEC_ERROR_UNKNOWN_ISSUER:
714 case SEC_ERROR_EXPIRED_CERTIFICATE:
715 case SEC_ERROR_EXPIRED_ISSUER_CERTIFICATE:
716 if (ctx->tc_verify_cert) {
717 success = SECFailure;
720 /* we bypass NSS's hostname checks and do our own */
721 case SSL_ERROR_BAD_CERT_DOMAIN:
724 success = SECFailure;
732 tlsm_dump_security_status(PRFileDesc *fd)
734 char * cp; /* bulk cipher name */
735 char * ip; /* cert issuer DN */
736 char * sp; /* cert subject DN */
737 int op; /* High, Low, Off */
738 int kp0; /* total key bits */
739 int kp1; /* secret key bits */
740 SSL3Statistics * ssl3stats = SSL_GetStatistics();
742 SSL_SecurityStatus( fd, &op, &cp, &kp0, &kp1, &ip, &sp );
743 Debug( LDAP_DEBUG_TRACE,
744 "TLS certificate verification: subject: %s, issuer: %s, cipher: %s, ",
745 sp ? sp : "-unknown-", ip ? ip : "-unknown-", cp ? cp : "-unknown-" );
749 Debug( LDAP_DEBUG_TRACE,
750 "security level: %s, secret key bits: %d, total key bits: %d, ",
751 ((op == SSL_SECURITY_STATUS_ON_HIGH) ? "high" :
752 ((op == SSL_SECURITY_STATUS_ON_LOW) ? "low" : "off")),
755 Debug( LDAP_DEBUG_TRACE,
756 "cache hits: %ld, cache misses: %ld, cache not reusable: %ld\n",
757 ssl3stats->hch_sid_cache_hits, ssl3stats->hch_sid_cache_misses,
758 ssl3stats->hch_sid_cache_not_ok );
764 tlsm_handshake_complete_cb( PRFileDesc *fd, void *client_data )
766 tlsm_dump_security_status( fd );
769 #ifdef READ_PASSWORD_FROM_FILE
771 tlsm_get_pin_from_file(const char *token_name, tlsm_ctx *ctx)
774 char *contents = NULL;
777 char *candidate = NULL;
778 PRFileInfo file_info;
779 PRFileDesc *pwd_fileptr = PR_Open( ctx->tc_pin_file, PR_RDONLY, 00400 );
781 /* open the password file */
782 if ( !pwd_fileptr ) {
783 PRErrorCode errcode = PR_GetError();
784 Debug( LDAP_DEBUG_ANY,
785 "TLS: could not open security pin file %s - error %d:%s.\n",
786 ctx->tc_pin_file, errcode,
787 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
791 /* get the file size */
792 if ( PR_SUCCESS != PR_GetFileInfo( ctx->tc_pin_file, &file_info ) ) {
793 PRErrorCode errcode = PR_GetError();
794 Debug( LDAP_DEBUG_ANY,
795 "TLS: could not get file info from pin file %s - error %d:%s.\n",
796 ctx->tc_pin_file, errcode,
797 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
801 /* create a buffer to hold the file contents */
802 if ( !( contents = PR_CALLOC( file_info.size + 1 ) ) ) {
803 PRErrorCode errcode = PR_GetError();
804 Debug( LDAP_DEBUG_ANY,
805 "TLS: could not alloc a buffer for contents of pin file %s - error %d:%s.\n",
806 ctx->tc_pin_file, errcode,
807 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
811 /* read file into the buffer */
812 if( PR_Read( pwd_fileptr, contents, file_info.size ) <= 0 ) {
813 PRErrorCode errcode = PR_GetError();
814 Debug( LDAP_DEBUG_ANY,
815 "TLS: could not read the file contents from pin file %s - error %d:%s.\n",
816 ctx->tc_pin_file, errcode,
817 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
821 /* format is [tokenname:]password EOL [tokenname:]password EOL ... */
822 /* if you want to use a password containing a colon character, use
823 the special tokenname "default" */
824 for ( line = PL_strtok_r( contents, "\r\n", &lasts ); line;
825 line = PL_strtok_r( NULL, "\r\n", &lasts ) ) {
829 continue; /* skip blank lines */
831 colon = PL_strchr( line, ':' );
833 if ( *(colon + 1) && token_name &&
834 !PL_strncmp( token_name, line, colon-line ) ) {
835 candidate = colon + 1; /* found a definite match */
837 } else if ( !PL_strncmp( DEFAULT_TOKEN_NAME, line, colon-line ) ) {
838 candidate = colon + 1; /* found possible match */
840 } else { /* no token name */
846 PR_Close( pwd_fileptr );
849 pwdstr = PL_strdup( candidate );
851 PL_strfree( contents );
855 #endif /* READ_PASSWORD_FROM_FILE */
857 #ifdef READ_PASSWORD_FROM_STDIN
859 * Turn the echoing off on a tty.
864 if ( isatty( fd ) ) {
866 tcgetattr( fd, &tio );
867 tio.c_lflag &= ~ECHO;
868 tcsetattr( fd, TCSAFLUSH, &tio );
873 * Turn the echoing on on a tty.
878 if ( isatty( fd ) ) {
880 tcgetattr( fd, &tio );
882 tcsetattr( fd, TCSAFLUSH, &tio );
883 tcsetattr( fd, TCSAFLUSH, &tio );
886 #endif /* READ_PASSWORD_FROM_STDIN */
889 * This does the actual work of reading the pin/password/pass phrase
892 tlsm_get_pin(PK11SlotInfo *slot, PRBool retry, tlsm_ctx *ctx)
894 char *token_name = NULL;
897 token_name = PK11_GetTokenName( slot );
898 #ifdef READ_PASSWORD_FROM_FILE
899 /* Try to get the passwords from the password file if it exists.
900 * THIS IS UNSAFE and is provided for convenience only. Without this
901 * capability the server would have to be started in foreground mode
902 * if using an encrypted key.
904 if ( ctx->tc_pin_file ) {
905 pwdstr = tlsm_get_pin_from_file( token_name, ctx );
906 if (retry && pwdstr != NULL)
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_ctx_subject_name(tlsm_ctx *ctx)
953 if (!ctx || !ctx->tc_certificate)
956 return ctx->tc_certificate->subjectName;
960 tlsm_get_basic_constraint_extension( CERTCertificate *cert,
961 CERTBasicConstraints *cbcval )
963 SECItem encodedVal = { 0, NULL };
966 rc = CERT_FindCertExtension( cert, SEC_OID_X509_BASIC_CONSTRAINTS,
968 if ( rc != SECSuccess ) {
972 rc = CERT_DecodeBasicConstraintValue( cbcval, &encodedVal );
974 /* free the raw extension data */
975 PORT_Free( encodedVal.data );
981 tlsm_cert_is_self_issued( CERTCertificate *cert )
983 /* A cert is self-issued if its subject and issuer are equal and
984 * both are of non-zero length.
986 PRBool is_self_issued = cert &&
987 (PRBool)SECITEM_ItemsAreEqual( &cert->derIssuer,
988 &cert->derSubject ) &&
989 cert->derSubject.len > 0;
990 return is_self_issued;
994 tlsm_verify_cert(CERTCertDBHandle *handle, CERTCertificate *cert, void *pinarg,
995 PRBool checksig, SECCertificateUsage certUsage, int errorToIgnore )
997 CERTVerifyLog verifylog;
998 SECStatus ret = SECSuccess;
1000 int debug_level = LDAP_DEBUG_ANY;
1002 if ( errorToIgnore == -1 ) {
1003 debug_level = LDAP_DEBUG_TRACE;
1006 /* the log captures information about every cert in the chain, so we can tell
1007 which cert caused the problem and what the problem was */
1008 memset( &verifylog, 0, sizeof( verifylog ) );
1009 verifylog.arena = PORT_NewArena( DER_DEFAULT_CHUNKSIZE );
1010 if ( verifylog.arena == NULL ) {
1011 Debug( LDAP_DEBUG_ANY,
1012 "TLS certificate verification: Out of memory for certificate verification logger\n",
1016 ret = CERT_VerifyCertificate( handle, cert, checksig, certUsage, PR_Now(), pinarg, &verifylog,
1018 if ( ( name = cert->subjectName ) == NULL ) {
1019 name = cert->nickname;
1021 if ( verifylog.head == NULL ) {
1022 /* it is possible for CERT_VerifyCertificate return with an error with no logging */
1023 if ( ret != SECSuccess ) {
1024 PRErrorCode errcode = PR_GetError();
1026 "TLS: certificate [%s] is not valid - error %d:%s.\n",
1027 name ? name : "(unknown)",
1028 errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1032 CERTVerifyLogNode *node;
1034 ret = SECSuccess; /* reset */
1035 node = verifylog.head;
1037 if ( ( name = node->cert->subjectName ) == NULL ) {
1038 name = node->cert->nickname;
1040 if ( node->error ) {
1041 /* NSS does not like CA certs that have the basic constraints extension
1042 with the CA flag set to FALSE - openssl doesn't check if the cert
1044 if ( ( node->error == SEC_ERROR_CA_CERT_INVALID ) &&
1045 tlsm_cert_is_self_issued( node->cert ) ) {
1047 PRErrorCode orig_error = PR_GetError();
1048 PRInt32 orig_oserror = PR_GetOSError();
1050 CERTBasicConstraints basicConstraint;
1051 SECStatus rv = tlsm_get_basic_constraint_extension( node->cert, &basicConstraint );
1052 if ( ( rv == SECSuccess ) && ( basicConstraint.isCA == PR_FALSE ) ) {
1053 Debug( LDAP_DEBUG_TRACE,
1054 "TLS: certificate [%s] is not correct because it is a CA cert and the "
1055 "BasicConstraint CA flag is set to FALSE - allowing for now, but "
1056 "please fix your certs if possible\n", name, 0, 0 );
1057 } else { /* does not have basicconstraint, or some other error */
1060 "TLS: certificate [%s] is not valid - CA cert is not valid\n",
1064 PR_SetError(orig_error, orig_oserror);
1066 } else if ( errorToIgnore && ( node->error == errorToIgnore ) ) {
1068 "TLS: Warning: ignoring error for certificate [%s] - error %ld:%s.\n",
1069 name, node->error, PR_ErrorToString( node->error, PR_LANGUAGE_I_DEFAULT ) );
1073 "TLS: certificate [%s] is not valid - error %ld:%s.\n",
1074 name, node->error, PR_ErrorToString( node->error, PR_LANGUAGE_I_DEFAULT ) );
1077 CERT_DestroyCertificate( node->cert );
1082 PORT_FreeArena( verifylog.arena, PR_FALSE );
1084 if ( ret == SECSuccess ) {
1085 Debug( LDAP_DEBUG_TRACE,
1086 "TLS: certificate [%s] is valid\n", name, 0, 0 );
1087 } else if ( errorToIgnore == -1 ) {
1095 tlsm_auth_cert_handler(void *arg, PRFileDesc *fd,
1096 PRBool checksig, PRBool isServer)
1098 SECCertificateUsage certUsage = isServer ? certificateUsageSSLClient : certificateUsageSSLServer;
1099 SECStatus ret = SECSuccess;
1100 CERTCertificate *peercert = SSL_PeerCertificate( fd );
1101 int errorToIgnore = 0;
1102 tlsm_ctx *ctx = (tlsm_ctx *)arg;
1104 if (ctx && ctx->tc_warn_only )
1107 ret = tlsm_verify_cert( ctx->tc_certdb, peercert,
1108 SSL_RevealPinArg( fd ),
1109 checksig, certUsage, errorToIgnore );
1110 CERT_DestroyCertificate( peercert );
1116 tlsm_nss_shutdown_cb( void *appData, void *nssData )
1118 SECStatus rc = SECSuccess;
1120 SSL_ShutdownServerSessionIDCache();
1123 SECMOD_UnloadUserModule( pem_module );
1124 SECMOD_DestroyModule( pem_module );
1130 static PRCallOnceType tlsm_register_shutdown_callonce = {0,0};
1131 static PRStatus PR_CALLBACK
1132 tlsm_register_nss_shutdown_cb( void )
1134 if ( SECSuccess == NSS_RegisterShutdown( tlsm_nss_shutdown_cb,
1142 tlsm_register_nss_shutdown( void )
1144 return PR_CallOnce( &tlsm_register_shutdown_callonce,
1145 tlsm_register_nss_shutdown_cb );
1149 tlsm_init_pem_module( void )
1152 char *fullname = NULL;
1153 char *configstring = NULL;
1159 /* not loaded - load it */
1160 /* get the system dependent library name */
1161 fullname = PR_GetLibraryName( NULL, PEM_LIBRARY );
1162 /* Load our PKCS#11 module */
1163 configstring = PR_smprintf( "library=%s name=" PEM_MODULE " parameters=\"\"", fullname );
1164 PL_strfree( fullname );
1166 pem_module = SECMOD_LoadUserModule( configstring, NULL, PR_FALSE );
1167 PR_smprintf_free( configstring );
1169 if ( !pem_module || !pem_module->loaded ) {
1171 SECMOD_DestroyModule( pem_module );
1181 tlsm_add_pem_obj( tlsm_ctx *ctx, PK11GenericObject *obj )
1183 int idx = ctx->tc_n_pem_objs;
1184 ctx->tc_n_pem_objs++;
1185 ctx->tc_pem_objs = (PK11GenericObject **)
1186 PORT_Realloc( ctx->tc_pem_objs, ctx->tc_n_pem_objs * sizeof( PK11GenericObject * ) );
1187 ctx->tc_pem_objs[idx] = obj;
1191 tlsm_free_pem_objs( tlsm_ctx *ctx )
1193 /* free in reverse order of allocation */
1194 while ( ctx->tc_n_pem_objs-- ) {
1195 PK11_DestroyGenericObject( ctx->tc_pem_objs[ctx->tc_n_pem_objs] );
1196 ctx->tc_pem_objs[ctx->tc_n_pem_objs] = NULL;
1198 PORT_Free(ctx->tc_pem_objs);
1199 ctx->tc_pem_objs = NULL;
1200 ctx->tc_n_pem_objs = 0;
1204 tlsm_add_cert_from_file( tlsm_ctx *ctx, const char *filename, PRBool isca )
1207 PK11GenericObject *cert;
1208 CK_ATTRIBUTE attrs[4];
1209 CK_BBOOL cktrue = CK_TRUE;
1210 CK_BBOOL ckfalse = CK_FALSE;
1211 CK_OBJECT_CLASS objClass = CKO_CERTIFICATE;
1215 SECItem certDER = { 0, NULL, 0 };
1217 memset( &fi, 0, sizeof(fi) );
1218 status = PR_GetFileInfo( filename, &fi );
1219 if ( PR_SUCCESS != status) {
1220 PRErrorCode errcode = PR_GetError();
1221 Debug( LDAP_DEBUG_ANY,
1222 "TLS: could not read certificate file %s - error %d:%s.\n",
1224 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1228 if ( fi.type != PR_FILE_FILE ) {
1229 PR_SetError(PR_IS_DIRECTORY_ERROR, 0);
1230 Debug( LDAP_DEBUG_ANY,
1231 "TLS: error: the certificate file %s is not a file.\n",
1236 slotname = isca ? TLSM_PEM_SLOT_CACERTS : TLSM_PEM_SLOT_CERTS;
1237 slot = PK11_FindSlotByName( slotname );
1240 PRErrorCode errcode = PR_GetError();
1241 Debug( LDAP_DEBUG_ANY,
1242 "TLS: could not find the slot for the certificate '%s' - error %d:%s.\n",
1243 filename, errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1247 PK11_SETATTRS( attrs[0], CKA_CLASS, &objClass, sizeof(objClass) );
1248 PK11_SETATTRS( attrs[1], CKA_TOKEN, &cktrue, sizeof(CK_BBOOL) );
1249 PK11_SETATTRS( attrs[2], CKA_LABEL, (unsigned char *)filename, strlen(filename)+1 );
1250 PK11_SETATTRS( attrs[3], CKA_TRUST, isca ? &cktrue : &ckfalse, sizeof(CK_BBOOL) );
1252 cert = PK11_CreateGenericObject( slot, attrs, 4, PR_FALSE /* isPerm */ );
1255 PRErrorCode errcode = PR_GetError();
1256 Debug( LDAP_DEBUG_ANY,
1257 "TLS: could not add the certificate '%s' - error %d:%s.\n",
1258 filename, errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1259 PK11_FreeSlot( slot );
1263 /* if not CA, we store the certificate in ctx->tc_certificate */
1265 if ( PK11_ReadRawAttribute( PK11_TypeGeneric, cert, CKA_VALUE, &certDER ) != SECSuccess ) {
1266 PRErrorCode errcode = PR_GetError();
1267 Debug( LDAP_DEBUG_ANY,
1268 "TLS: could not get DER of the '%s' certificate - error %d:%s.\n",
1269 filename, errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1270 PK11_DestroyGenericObject( cert );
1271 PK11_FreeSlot( slot );
1275 ctx->tc_certificate = PK11_FindCertFromDERCertItem( slot, &certDER, NULL );
1276 SECITEM_FreeItem( &certDER, PR_FALSE );
1278 if ( !ctx->tc_certificate ) {
1279 PRErrorCode errcode = PR_GetError();
1280 Debug( LDAP_DEBUG_ANY,
1281 "TLS: could not get certificate '%s' using DER - error %d:%s.\n",
1282 filename, errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1283 PK11_DestroyGenericObject( cert );
1284 PK11_FreeSlot( slot );
1289 tlsm_add_pem_obj( ctx, cert );
1291 PK11_FreeSlot( slot );
1297 tlsm_ctx_load_private_key(tlsm_ctx *ctx)
1299 if (!ctx->tc_certificate)
1302 if (ctx->tc_private_key)
1305 void *pin_arg = SSL_RevealPinArg(ctx->tc_model);
1307 ctx->tc_private_key = PK11_FindKeyByAnyCert(ctx->tc_certificate, pin_arg);
1308 if (!ctx->tc_private_key) {
1309 PRErrorCode errcode = PR_GetError();
1310 Debug(LDAP_DEBUG_ANY,
1311 "TLS: cannot find private key for certificate '%s' (error %d: %s)",
1312 tlsm_ctx_subject_name(ctx), errcode,
1313 PR_ErrorToString(errcode, PR_LANGUAGE_I_DEFAULT));
1321 tlsm_add_key_from_file( tlsm_ctx *ctx, const char *filename )
1323 PK11SlotInfo * slot = NULL;
1324 PK11GenericObject *key;
1325 CK_ATTRIBUTE attrs[3];
1326 CK_BBOOL cktrue = CK_TRUE;
1327 CK_OBJECT_CLASS objClass = CKO_PRIVATE_KEY;
1332 memset( &fi, 0, sizeof(fi) );
1333 status = PR_GetFileInfo( filename, &fi );
1334 if ( PR_SUCCESS != status) {
1335 PRErrorCode errcode = PR_GetError();
1336 Debug( LDAP_DEBUG_ANY,
1337 "TLS: could not read key file %s - error %d:%s.\n",
1339 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1343 if ( fi.type != PR_FILE_FILE ) {
1344 PR_SetError(PR_IS_DIRECTORY_ERROR, 0);
1345 Debug( LDAP_DEBUG_ANY,
1346 "TLS: error: the key file %s is not a file.\n",
1351 slot = PK11_FindSlotByName( TLSM_PEM_SLOT_CERTS );
1354 PRErrorCode errcode = PR_GetError();
1355 Debug( LDAP_DEBUG_ANY,
1356 "TLS: could not find the slot for the private key '%s' - error %d:%s.\n",
1357 filename, errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1361 PK11_SETATTRS( attrs[0], CKA_CLASS, &objClass, sizeof(objClass) );
1362 PK11_SETATTRS( attrs[1], CKA_TOKEN, &cktrue, sizeof(CK_BBOOL) );
1363 PK11_SETATTRS( attrs[2], CKA_LABEL, (unsigned char *)filename, strlen(filename)+1 );
1365 key = PK11_CreateGenericObject( slot, attrs, 3, PR_FALSE /* isPerm */ );
1368 PRErrorCode errcode = PR_GetError();
1369 Debug( LDAP_DEBUG_ANY,
1370 "TLS: could not add the private key '%s' - error %d:%s.\n",
1371 filename, errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1374 tlsm_add_pem_obj( ctx, key );
1377 /* When adding an encrypted key the PKCS#11 will be set as removed */
1378 /* This will force the token to be seen as re-inserted */
1379 SECMOD_WaitForAnyTokenEvent( pem_module, 0, 0 );
1380 PK11_IsPresent( slot );
1383 PK11_FreeSlot( slot );
1389 tlsm_init_ca_certs( tlsm_ctx *ctx, const char *cacertfile, const char *cacertdir )
1391 PRBool isca = PR_TRUE;
1392 PRStatus status = PR_SUCCESS;
1393 PRErrorCode errcode = PR_SUCCESS;
1395 if ( !cacertfile && !cacertdir ) {
1396 /* no checking - not good, but allowed */
1401 int rc = tlsm_add_cert_from_file( ctx, cacertfile, isca );
1403 errcode = PR_GetError();
1404 Debug( LDAP_DEBUG_ANY,
1405 "TLS: %s is not a valid CA certificate file - error %d:%s.\n",
1406 cacertfile, errcode,
1407 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1408 /* failure with cacertfile is a hard failure even if cacertdir is
1409 also specified and contains valid CA cert files */
1410 status = PR_FAILURE;
1412 Debug( LDAP_DEBUG_TRACE,
1413 "TLS: loaded CA certificate file %s.\n",
1418 /* if cacertfile above failed, we will return failure, even
1419 if there is a valid CA cert in cacertdir - but we still
1420 process cacertdir in case the user has enabled trace level
1421 debugging so they can see the processing for cacertdir too */
1422 /* any cacertdir failures are "soft" failures - if the user specifies
1423 no cert checking, then we allow the tls/ssl to continue, no matter
1424 what was specified for cacertdir, or the contents of the directory
1425 - this is different behavior than that of cacertfile */
1430 PRStatus fistatus = PR_FAILURE;
1432 memset( &fi, 0, sizeof(fi) );
1433 fistatus = PR_GetFileInfo( cacertdir, &fi );
1434 if ( PR_SUCCESS != fistatus) {
1435 errcode = PR_GetError();
1436 Debug( LDAP_DEBUG_ANY,
1437 "TLS: could not get info about the CA certificate directory %s - error %d:%s.\n",
1439 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1443 if ( fi.type != PR_FILE_DIRECTORY ) {
1444 Debug( LDAP_DEBUG_ANY,
1445 "TLS: error: the CA certificate directory %s is not a directory.\n",
1450 dir = PR_OpenDir( cacertdir );
1451 if ( NULL == dir ) {
1452 errcode = PR_GetError();
1453 Debug( LDAP_DEBUG_ANY,
1454 "TLS: could not open the CA certificate directory %s - error %d:%s.\n",
1456 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1461 entry = PR_ReadDir( dir, PR_SKIP_BOTH | PR_SKIP_HIDDEN );
1462 if ( ( NULL != entry ) && ( NULL != entry->name ) ) {
1463 char *fullpath = NULL;
1466 ptr = PL_strrstr( entry->name, PEM_CA_HASH_FILE_SUFFIX );
1467 if ( ( ptr == NULL ) || ( *(ptr + PEM_CA_HASH_FILE_SUFFIX_LEN) != '\0' ) ) {
1468 Debug( LDAP_DEBUG_TRACE,
1469 "TLS: file %s does not end in [%s] - does not appear to be a CA certificate "
1470 "directory file with a properly hashed file name - skipping.\n",
1471 entry->name, PEM_CA_HASH_FILE_SUFFIX, 0 );
1474 fullpath = PR_smprintf( "%s/%s", cacertdir, entry->name );
1475 if ( !tlsm_add_cert_from_file( ctx, fullpath, isca ) ) {
1476 Debug( LDAP_DEBUG_TRACE,
1477 "TLS: loaded CA certificate file %s from CA certificate directory %s.\n",
1478 fullpath, cacertdir, 0 );
1480 errcode = PR_GetError();
1481 Debug( LDAP_DEBUG_TRACE,
1482 "TLS: %s is not a valid CA certificate file - error %d:%s.\n",
1484 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1486 PR_smprintf_free( fullpath );
1488 } while ( NULL != entry );
1492 if ( status != PR_SUCCESS ) {
1500 * NSS supports having multiple cert/key databases in the same
1501 * directory, each one having a unique string prefix e.g.
1502 * slapd-01-cert8.db - the prefix here is "slapd-01-"
1503 * this function examines the given certdir - if it looks like
1504 * /path/to/directory/prefix it will return the
1505 * /path/to/directory part in realcertdir, and the prefix in prefix
1508 tlsm_get_certdb_prefix( const char *certdir, char **realcertdir, char **prefix )
1510 char sep = PR_GetDirectorySeparator();
1512 struct PRFileInfo prfi;
1515 *realcertdir = (char *)certdir; /* default is the one passed in */
1517 /* if certdir is not given, just return */
1522 prc = PR_GetFileInfo( certdir, &prfi );
1523 /* if certdir exists (file or directory) then it cannot specify a prefix */
1524 if ( prc == PR_SUCCESS ) {
1528 /* if certdir was given, and there is a '/' in certdir, see if there
1529 is anything after the last '/' - if so, assume it is the prefix */
1530 if ( ( ( ptr = strrchr( certdir, sep ) ) ) && *(ptr+1) ) {
1531 *realcertdir = PL_strndup( certdir, ptr-certdir );
1532 *prefix = PL_strdup( ptr+1 );
1539 * Currently mutiple MozNSS contexts share one certificate storage. When the
1540 * certdb is being opened, only new certificates are added to the storage.
1541 * When different databases are used, conflicting nicknames make the
1542 * certificate lookup by the nickname impossible. In addition a token
1543 * description might be prepended in certain conditions.
1545 * In order to make the certificate lookup by nickname possible, we explicitly
1546 * open each database using SECMOD_OpenUserDB and assign it the token
1547 * description. The token description is generated using ctx->tc_unique value,
1548 * which is unique for each context.
1550 static PK11SlotInfo *
1551 tlsm_init_open_certdb(tlsm_ctx *ctx, const char *dbdir, const char *prefix)
1553 PK11SlotInfo *slot = NULL;
1554 char *token_desc = NULL;
1555 char *config = NULL;
1557 token_desc = PR_smprintf(TLSM_CERTDB_DESC_FMT, ctx->tc_unique);
1558 config = PR_smprintf("configDir='%s' tokenDescription='%s' certPrefix='%s' keyPrefix='%s' flags=readOnly",
1559 dbdir, token_desc, prefix, prefix);
1560 Debug(LDAP_DEBUG_TRACE, "TLS: certdb config: %s\n", config, 0, 0);
1562 slot = SECMOD_OpenUserDB(config);
1564 PRErrorCode errcode = PR_GetError();
1565 Debug(LDAP_DEBUG_TRACE, "TLS: cannot open certdb '%s', error %d:%s\n", dbdir, errcode,
1566 PR_ErrorToString(errcode, PR_LANGUAGE_I_DEFAULT));
1570 PR_smprintf_free(token_desc);
1572 PR_smprintf_free(config);
1578 * This is the part of the init we defer until we get the
1579 * actual security configuration information. This is
1580 * only called once, protected by a PRCallOnce
1581 * NOTE: This must be done before the first call to SSL_ImportFD,
1582 * especially the setting of the policy
1583 * NOTE: This must be called after fork()
1586 tlsm_deferred_init( void *arg )
1588 tlsm_ctx *ctx = (tlsm_ctx *)arg;
1589 struct ldaptls *lt = ctx->tc_config;
1590 const char *securitydirs[3];
1593 PRErrorCode errcode = 1;
1594 #ifdef HAVE_NSS_INITCONTEXT
1595 NSSInitParameters initParams;
1596 NSSInitContext *initctx = NULL;
1597 PK11SlotInfo *certdb_slot = NULL;
1602 #ifdef HAVE_SECMOD_RESTARTMODULES
1603 /* NSS enforces the pkcs11 requirement that modules should be unloaded after
1604 a fork() - since there is no portable way to determine if NSS has been
1605 already initialized in a parent process, we just call SECMOD_RestartModules
1606 with force == FALSE - if the module has been unloaded due to a fork, it will
1607 be reloaded, otherwise, it is a no-op */
1608 if ( SECFailure == ( rc = SECMOD_RestartModules(PR_FALSE /* do not force */) ) ) {
1609 errcode = PORT_GetError();
1610 if ( errcode != SEC_ERROR_NOT_INITIALIZED ) {
1611 Debug( LDAP_DEBUG_TRACE,
1612 "TLS: could not restart the security modules: %d:%s\n",
1613 errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ), 0 );
1620 #ifdef HAVE_NSS_INITCONTEXT
1621 memset( &initParams, 0, sizeof( initParams ) );
1622 initParams.length = sizeof( initParams );
1623 #endif /* HAVE_NSS_INITCONTEXT */
1625 #ifdef LDAP_R_COMPILE
1626 if ( PR_CallOnce( &tlsm_init_mutex_callonce, tlsm_thr_init_callonce ) ) {
1629 #endif /* LDAP_R_COMPILE */
1631 #ifndef HAVE_NSS_INITCONTEXT
1632 if ( !NSS_IsInitialized() ) {
1633 #endif /* HAVE_NSS_INITCONTEXT */
1635 MOZNSS_DIR will override everything else - you can
1636 always set MOZNSS_DIR to force the use of this
1638 If using MOZNSS, specify the location of the moznss db dir
1639 in the cacertdir directive of the OpenLDAP configuration.
1640 DEFAULT_MOZNSS_DIR will only be used if the code cannot
1641 find a security dir to use based on the current
1645 securitydirs[nn++] = PR_GetEnv( "MOZNSS_DIR" );
1646 securitydirs[nn++] = lt->lt_cacertdir;
1647 securitydirs[nn++] = PR_GetEnv( "DEFAULT_MOZNSS_DIR" );
1648 for ( ii = 0; !done && ( ii < nn ); ++ii ) {
1649 char *realcertdir = NULL;
1650 const char *defprefix = "";
1651 char *prefix = (char *)defprefix;
1652 const char *securitydir = securitydirs[ii];
1653 if ( NULL == securitydir ) {
1657 tlsm_get_certdb_prefix( securitydir, &realcertdir, &prefix );
1659 /* initialize only moddb; certdb will be initialized explicitly */
1660 #ifdef HAVE_NSS_INITCONTEXT
1661 #ifdef INITCONTEXT_HACK
1662 if ( !NSS_IsInitialized() && ctx->tc_is_server ) {
1663 rc = NSS_Initialize( realcertdir, prefix, prefix, SECMOD_DB, NSS_INIT_READONLY );
1665 initctx = NSS_InitContext( realcertdir, prefix, prefix, SECMOD_DB,
1666 &initParams, NSS_INIT_READONLY|NSS_INIT_NOCERTDB );
1669 initctx = NSS_InitContext( realcertdir, prefix, prefix, SECMOD_DB,
1670 &initParams, NSS_INIT_READONLY|NSS_INIT_NOCERTDB );
1674 if (initctx != NULL) {
1675 certdb_slot = tlsm_init_open_certdb(ctx, realcertdir, prefix);
1678 ctx->tc_initctx = initctx;
1679 ctx->tc_certdb_slot = certdb_slot;
1681 NSS_ShutdownContext(initctx);
1686 rc = NSS_Initialize( realcertdir, prefix, prefix, SECMOD_DB, NSS_INIT_READONLY );
1689 if ( rc != SECSuccess ) {
1690 errcode = PORT_GetError();
1691 if ( securitydirs[ii] != lt->lt_cacertdir) {
1692 Debug( LDAP_DEBUG_TRACE,
1693 "TLS: could not initialize moznss using security dir %s prefix %s - error %d.\n",
1694 realcertdir, prefix, errcode );
1698 Debug( LDAP_DEBUG_TRACE, "TLS: using moznss security dir %s prefix %s.\n",
1699 realcertdir, prefix, 0 );
1703 if ( realcertdir != securitydir ) {
1704 PL_strfree( realcertdir );
1706 if ( prefix != defprefix ) {
1707 PL_strfree( prefix );
1711 if ( errcode ) { /* no moznss db found, or not using moznss db */
1712 #ifdef HAVE_NSS_INITCONTEXT
1713 int flags = NSS_INIT_READONLY|NSS_INIT_NOCERTDB|NSS_INIT_NOMODDB;
1714 #ifdef INITCONTEXT_HACK
1715 if ( !NSS_IsInitialized() && ctx->tc_is_server ) {
1716 rc = NSS_NoDB_Init( NULL );
1718 initctx = NSS_InitContext( CERTDB_NONE, PREFIX_NONE, PREFIX_NONE, SECMOD_DB,
1719 &initParams, flags );
1720 rc = (initctx == NULL) ? SECFailure : SECSuccess;
1723 initctx = NSS_InitContext( CERTDB_NONE, PREFIX_NONE, PREFIX_NONE, SECMOD_DB,
1724 &initParams, flags );
1726 ctx->tc_initctx = initctx;
1733 rc = NSS_NoDB_Init( NULL );
1735 if ( rc != SECSuccess ) {
1736 errcode = PORT_GetError();
1737 Debug( LDAP_DEBUG_ANY,
1738 "TLS: could not initialize moznss - error %d:%s.\n",
1739 errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ), 0 );
1744 if ( errcode || lt->lt_cacertfile ) {
1745 /* initialize the PEM module */
1746 if ( tlsm_init_pem_module() ) {
1747 int pem_errcode = PORT_GetError();
1748 Debug( LDAP_DEBUG_ANY,
1749 "TLS: could not initialize moznss PEM module - error %d:%s.\n",
1750 pem_errcode, PR_ErrorToString( pem_errcode, PR_LANGUAGE_I_DEFAULT ), 0 );
1752 if ( errcode ) /* PEM is required */
1755 } else if ( !errcode ) {
1756 tlsm_init_ca_certs( ctx, lt->lt_cacertfile, NULL );
1761 if ( tlsm_init_ca_certs( ctx, lt->lt_cacertfile, lt->lt_cacertdir ) ) {
1762 /* if we tried to use lt->lt_cacertdir as an NSS key/cert db, errcode
1763 will be a value other than 1 - print an error message so that the
1764 user will know that failed too */
1765 if ( ( errcode != 1 ) && ( lt->lt_cacertdir ) ) {
1766 char *realcertdir = NULL;
1767 char *prefix = NULL;
1768 tlsm_get_certdb_prefix( lt->lt_cacertdir, &realcertdir, &prefix );
1769 Debug( LDAP_DEBUG_TRACE,
1770 "TLS: could not initialize moznss using security dir %s prefix %s - error %d.\n",
1771 realcertdir, prefix ? prefix : "", errcode );
1772 if ( realcertdir != lt->lt_cacertdir ) {
1773 PL_strfree( realcertdir );
1775 PL_strfree( prefix );
1780 ctx->tc_using_pem = PR_TRUE;
1783 NSS_SetDomesticPolicy();
1785 PK11_SetPasswordFunc( tlsm_pin_prompt );
1787 /* register cleanup function */
1788 if ( tlsm_register_nss_shutdown() ) {
1789 errcode = PORT_GetError();
1790 Debug( LDAP_DEBUG_ANY,
1791 "TLS: could not register NSS shutdown function: %d:%s\n",
1792 errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ), 0 );
1796 if ( ctx->tc_is_server ) {
1797 /* 0 means use the defaults here */
1798 SSL_ConfigServerSessionIDCache( 0, 0, 0, NULL );
1801 #ifndef HAVE_NSS_INITCONTEXT
1803 #endif /* HAVE_NSS_INITCONTEXT */
1809 * Find and verify the certificate.
1810 * The key is loaded and stored in ctx->tc_private_key
1813 tlsm_find_and_verify_cert_key(tlsm_ctx *ctx)
1815 SECCertificateUsage certUsage;
1821 if (tlsm_ctx_load_private_key(ctx))
1824 pin_arg = SSL_RevealPinArg(ctx->tc_model);
1825 certUsage = ctx->tc_is_server ? certificateUsageSSLServer : certificateUsageSSLClient;
1826 checkSig = ctx->tc_verify_cert ? PR_TRUE : PR_FALSE;
1827 if ( ctx->tc_warn_only )
1830 errorToIgnore = SEC_ERROR_UNKNOWN_ISSUER; /* may not have a CA cert */
1832 status = tlsm_verify_cert( ctx->tc_certdb, ctx->tc_certificate, pin_arg,
1833 checkSig, certUsage, errorToIgnore );
1835 return status == SECSuccess ? 0 : -1;
1839 tlsm_get_client_auth_data( void *arg, PRFileDesc *fd,
1840 CERTDistNames *caNames, CERTCertificate **pRetCert,
1841 SECKEYPrivateKey **pRetKey )
1843 tlsm_ctx *ctx = (tlsm_ctx *)arg;
1846 *pRetCert = CERT_DupCertificate(ctx->tc_certificate);
1849 *pRetKey = SECKEY_CopyPrivateKey(ctx->tc_private_key);
1855 * ctx must have a tc_model that is valid
1858 tlsm_clientauth_init( tlsm_ctx *ctx )
1860 SECStatus status = SECFailure;
1864 saveval = ctx->tc_warn_only;
1865 ctx->tc_warn_only = PR_TRUE;
1866 rc = tlsm_find_and_verify_cert_key(ctx);
1867 ctx->tc_warn_only = saveval;
1869 Debug( LDAP_DEBUG_ANY,
1870 "TLS: error: unable to set up client certificate authentication for "
1871 "certificate named %s\n", tlsm_ctx_subject_name(ctx), 0, 0 );
1875 status = SSL_GetClientAuthDataHook( ctx->tc_model,
1876 tlsm_get_client_auth_data,
1879 return ( status == SECSuccess ? 0 : -1 );
1883 * Tear down the TLS subsystem. Should only be called once.
1886 tlsm_destroy( void )
1888 #ifdef LDAP_R_COMPILE
1889 ldap_pvt_thread_mutex_destroy( &tlsm_ctx_count_mutex );
1890 ldap_pvt_thread_mutex_destroy( &tlsm_init_mutex );
1891 ldap_pvt_thread_mutex_destroy( &tlsm_pem_mutex );
1895 static struct ldaptls *
1896 tlsm_copy_config ( const struct ldaptls *config )
1898 struct ldaptls *copy;
1902 copy = LDAP_MALLOC(sizeof(*copy));
1906 memset(copy, 0, sizeof(*copy));
1908 if (config->lt_certfile)
1909 copy->lt_certfile = LDAP_STRDUP(config->lt_certfile);
1910 if (config->lt_keyfile)
1911 copy->lt_keyfile = LDAP_STRDUP(config->lt_keyfile);
1912 if (config->lt_dhfile)
1913 copy->lt_dhfile = LDAP_STRDUP(config->lt_dhfile);
1914 if (config->lt_cacertfile)
1915 copy->lt_cacertfile = LDAP_STRDUP(config->lt_cacertfile);
1916 if (config->lt_cacertdir)
1917 copy->lt_cacertdir = LDAP_STRDUP(config->lt_cacertdir);
1918 if (config->lt_ciphersuite)
1919 copy->lt_ciphersuite = LDAP_STRDUP(config->lt_ciphersuite);
1920 if (config->lt_crlfile)
1921 copy->lt_crlfile = LDAP_STRDUP(config->lt_crlfile);
1922 if (config->lt_randfile)
1923 copy->lt_randfile = LDAP_STRDUP(config->lt_randfile);
1925 copy->lt_protocol_min = config->lt_protocol_min;
1931 tlsm_free_config ( struct ldaptls *config )
1935 if (config->lt_certfile)
1936 LDAP_FREE(config->lt_certfile);
1937 if (config->lt_keyfile)
1938 LDAP_FREE(config->lt_keyfile);
1939 if (config->lt_dhfile)
1940 LDAP_FREE(config->lt_dhfile);
1941 if (config->lt_cacertfile)
1942 LDAP_FREE(config->lt_cacertfile);
1943 if (config->lt_cacertdir)
1944 LDAP_FREE(config->lt_cacertdir);
1945 if (config->lt_ciphersuite)
1946 LDAP_FREE(config->lt_ciphersuite);
1947 if (config->lt_crlfile)
1948 LDAP_FREE(config->lt_crlfile);
1949 if (config->lt_randfile)
1950 LDAP_FREE(config->lt_randfile);
1956 tlsm_ctx_new ( struct ldapoptions *lo )
1960 ctx = LDAP_MALLOC( sizeof (*ctx) );
1963 #ifdef LDAP_R_COMPILE
1964 ldap_pvt_thread_mutex_init( &ctx->tc_refmutex );
1966 LDAP_MUTEX_LOCK( &tlsm_ctx_count_mutex );
1967 ctx->tc_unique = tlsm_ctx_count++;
1968 LDAP_MUTEX_UNLOCK( &tlsm_ctx_count_mutex );
1969 ctx->tc_config = NULL; /* populated later by tlsm_ctx_init */
1970 ctx->tc_certdb = NULL;
1971 ctx->tc_certdb_slot = NULL;
1972 ctx->tc_certificate = NULL;
1973 ctx->tc_private_key = NULL;
1974 ctx->tc_pin_file = NULL;
1975 ctx->tc_model = NULL;
1976 memset(&ctx->tc_callonce, 0, sizeof(ctx->tc_callonce));
1977 ctx->tc_require_cert = lo->ldo_tls_require_cert;
1978 ctx->tc_verify_cert = PR_FALSE;
1979 ctx->tc_using_pem = PR_FALSE;
1980 #ifdef HAVE_NSS_INITCONTEXT
1981 ctx->tc_initctx = NULL;
1982 #endif /* HAVE_NSS_INITCONTEXT */
1983 ctx->tc_pem_objs = NULL;
1984 ctx->tc_n_pem_objs = 0;
1985 ctx->tc_warn_only = PR_FALSE;
1987 return (tls_ctx *)ctx;
1991 tlsm_ctx_ref( tls_ctx *ctx )
1993 tlsm_ctx *c = (tlsm_ctx *)ctx;
1994 LDAP_MUTEX_LOCK( &c->tc_refmutex );
1996 LDAP_MUTEX_UNLOCK( &c->tc_refmutex );
2000 tlsm_ctx_free ( tls_ctx *ctx )
2002 tlsm_ctx *c = (tlsm_ctx *)ctx;
2007 LDAP_MUTEX_LOCK( &c->tc_refmutex );
2008 refcount = --c->tc_refcnt;
2009 LDAP_MUTEX_UNLOCK( &c->tc_refmutex );
2013 LDAP_MUTEX_LOCK( &tlsm_init_mutex );
2015 PR_Close( c->tc_model );
2016 if (c->tc_certificate)
2017 CERT_DestroyCertificate(c->tc_certificate);
2018 if (c->tc_private_key)
2019 SECKEY_DestroyPrivateKey(c->tc_private_key);
2020 c->tc_certdb = NULL; /* if not the default, may have to clean up */
2021 if ( c->tc_certdb_slot ) {
2022 if ( SECMOD_CloseUserDB( c->tc_certdb_slot ) ) {
2023 PRErrorCode errcode = PR_GetError();
2024 Debug( LDAP_DEBUG_ANY,
2025 "TLS: could not close certdb slot - error %d:%s.\n",
2026 errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ), 0 );
2029 PL_strfree( c->tc_pin_file );
2030 c->tc_pin_file = NULL;
2031 tlsm_free_pem_objs( c );
2032 #ifdef HAVE_NSS_INITCONTEXT
2033 if ( c->tc_initctx ) {
2034 if ( NSS_ShutdownContext( c->tc_initctx ) ) {
2035 PRErrorCode errcode = PR_GetError();
2036 Debug( LDAP_DEBUG_ANY,
2037 "TLS: could not shutdown NSS - error %d:%s.\n",
2038 errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ), 0 );
2041 c->tc_initctx = NULL;
2042 #endif /* HAVE_NSS_INITCONTEXT */
2043 LDAP_MUTEX_UNLOCK( &tlsm_init_mutex );
2044 #ifdef LDAP_R_COMPILE
2045 ldap_pvt_thread_mutex_destroy( &c->tc_refmutex );
2049 tlsm_free_config( c->tc_config );
2055 * initialize a new TLS context
2058 tlsm_ctx_init( struct ldapoptions *lo, struct ldaptls *lt, int is_server )
2060 tlsm_ctx *ctx = (tlsm_ctx *)lo->ldo_tls_ctx;
2061 ctx->tc_config = tlsm_copy_config(lt);
2062 ctx->tc_is_server = is_server;
2068 tlsm_deferred_ctx_init( void *arg )
2070 tlsm_ctx *ctx = (tlsm_ctx *)arg;
2071 PRBool sslv2 = PR_FALSE;
2072 PRBool sslv3 = PR_TRUE;
2073 PRBool tlsv1 = PR_TRUE;
2074 PRBool request_cert = PR_FALSE;
2075 PRInt32 require_cert = PR_FALSE;
2079 if ( tlsm_deferred_init( ctx ) ) {
2080 Debug( LDAP_DEBUG_ANY,
2081 "TLS: could not perform TLS system initialization.\n",
2086 ctx->tc_certdb = CERT_GetDefaultCertDB(); /* If there is ever a per-context db, change this */
2088 fd = PR_CreateIOLayerStub( tlsm_layer_id, &tlsm_PR_methods );
2090 ctx->tc_model = SSL_ImportFD( NULL, fd );
2093 if ( !ctx->tc_model ) {
2094 PRErrorCode err = PR_GetError();
2095 Debug( LDAP_DEBUG_ANY,
2096 "TLS: could perform TLS socket I/O layer initialization - error %d:%s.\n",
2097 err, PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ), NULL );
2105 if ( SSL_SetPKCS11PinArg(ctx->tc_model, ctx) ) {
2106 Debug( LDAP_DEBUG_ANY,
2107 "TLS: could not set pin prompt argument\n", 0, 0, 0);
2111 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_SECURITY, PR_TRUE ) ) {
2112 Debug( LDAP_DEBUG_ANY,
2113 "TLS: could not set secure mode on.\n",
2118 lt = ctx->tc_config;
2120 /* default is sslv3 and tlsv1 */
2121 if ( lt->lt_protocol_min ) {
2122 if ( lt->lt_protocol_min > LDAP_OPT_X_TLS_PROTOCOL_SSL3 ) {
2124 } else if ( lt->lt_protocol_min <= LDAP_OPT_X_TLS_PROTOCOL_SSL2 ) {
2126 Debug( LDAP_DEBUG_ANY,
2127 "TLS: warning: minimum TLS protocol level set to "
2128 "include SSLv2 - SSLv2 is insecure - do not use\n", 0, 0, 0 );
2131 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_ENABLE_SSL2, sslv2 ) ) {
2132 Debug( LDAP_DEBUG_ANY,
2133 "TLS: could not set SSLv2 mode on.\n",
2137 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_ENABLE_SSL3, sslv3 ) ) {
2138 Debug( LDAP_DEBUG_ANY,
2139 "TLS: could not set SSLv3 mode on.\n",
2143 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_ENABLE_TLS, tlsv1 ) ) {
2144 Debug( LDAP_DEBUG_ANY,
2145 "TLS: could not set TLSv1 mode on.\n",
2150 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_HANDSHAKE_AS_CLIENT, !ctx->tc_is_server ) ) {
2151 Debug( LDAP_DEBUG_ANY,
2152 "TLS: could not set handshake as client.\n",
2156 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_HANDSHAKE_AS_SERVER, ctx->tc_is_server ) ) {
2157 Debug( LDAP_DEBUG_ANY,
2158 "TLS: could not set handshake as server.\n",
2163 if ( lt->lt_ciphersuite ) {
2164 if ( tlsm_parse_ciphers( ctx, lt->lt_ciphersuite ) ) {
2165 Debug( LDAP_DEBUG_ANY,
2166 "TLS: could not set cipher list %s.\n",
2167 lt->lt_ciphersuite, 0, 0 );
2170 } else if ( tlsm_parse_ciphers( ctx, "DEFAULT" ) ) {
2171 Debug( LDAP_DEBUG_ANY,
2172 "TLS: could not set cipher list DEFAULT.\n",
2177 if ( !ctx->tc_require_cert ) {
2178 ctx->tc_verify_cert = PR_FALSE;
2179 } else if ( !ctx->tc_is_server ) {
2180 request_cert = PR_TRUE;
2181 require_cert = SSL_REQUIRE_NO_ERROR;
2182 if ( ctx->tc_require_cert == LDAP_OPT_X_TLS_DEMAND ||
2183 ctx->tc_require_cert == LDAP_OPT_X_TLS_HARD ) {
2184 require_cert = SSL_REQUIRE_ALWAYS;
2186 if ( ctx->tc_require_cert != LDAP_OPT_X_TLS_ALLOW )
2187 ctx->tc_verify_cert = PR_TRUE;
2188 } else { /* server */
2189 /* server does not request certs by default */
2190 /* if allow - client may send cert, server will ignore if errors */
2191 /* if try - client may send cert, server will error if bad cert */
2192 /* if hard or demand - client must send cert, server will error if bad cert */
2193 request_cert = PR_TRUE;
2194 require_cert = SSL_REQUIRE_NO_ERROR;
2195 if ( ctx->tc_require_cert == LDAP_OPT_X_TLS_DEMAND ||
2196 ctx->tc_require_cert == LDAP_OPT_X_TLS_HARD ) {
2197 require_cert = SSL_REQUIRE_ALWAYS;
2199 if ( ctx->tc_require_cert != LDAP_OPT_X_TLS_ALLOW ) {
2200 ctx->tc_verify_cert = PR_TRUE;
2202 ctx->tc_warn_only = PR_TRUE;
2206 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_REQUEST_CERTIFICATE, request_cert ) ) {
2207 Debug( LDAP_DEBUG_ANY,
2208 "TLS: could not set request certificate mode.\n",
2213 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_REQUIRE_CERTIFICATE, require_cert ) ) {
2214 Debug( LDAP_DEBUG_ANY,
2215 "TLS: could not set require certificate mode.\n",
2220 /* set up our cert and key, if any */
2221 if ( lt->lt_certfile ) {
2222 /* if using the PEM module, load the PEM file specified by lt_certfile */
2223 /* otherwise, assume this is the name of a cert already in the db */
2224 if ( ctx->tc_using_pem ) {
2225 /* this sets ctx->tc_certificate to the correct value */
2226 int rc = tlsm_add_cert_from_file( ctx, lt->lt_certfile, PR_FALSE );
2233 if (ctx->tc_certdb_slot) {
2234 tmp_certname = PR_smprintf(TLSM_CERTDB_DESC_FMT ":%s", ctx->tc_unique, lt->lt_certfile);
2236 tmp_certname = PR_smprintf("%s", lt->lt_certfile);
2239 ctx->tc_certificate = PK11_FindCertFromNickname(tmp_certname, SSL_RevealPinArg(ctx->tc_model));
2240 PR_smprintf_free(tmp_certname);
2242 if (!ctx->tc_certificate) {
2243 PRErrorCode errcode = PR_GetError();
2244 Debug( LDAP_DEBUG_ANY,
2245 "TLS: error: the certificate '%s' could not be found in the database - error %d:%s.\n",
2246 lt->lt_certfile, errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
2252 if ( lt->lt_keyfile ) {
2253 /* if using the PEM module, load the PEM file specified by lt_keyfile */
2254 /* otherwise, assume this is the pininfo for the key */
2255 if ( ctx->tc_using_pem ) {
2256 int rc = tlsm_add_key_from_file( ctx, lt->lt_keyfile );
2261 PL_strfree( ctx->tc_pin_file );
2262 ctx->tc_pin_file = PL_strdup( lt->lt_keyfile );
2266 /* Set up callbacks for use by clients */
2267 if ( !ctx->tc_is_server ) {
2268 if ( SSL_OptionSet( ctx->tc_model, SSL_NO_CACHE, PR_TRUE ) != SECSuccess ) {
2269 PRErrorCode err = PR_GetError();
2270 Debug( LDAP_DEBUG_ANY,
2271 "TLS: error: could not set nocache option for moznss - error %d:%s\n",
2272 err, PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ), NULL );
2276 if ( SSL_BadCertHook( ctx->tc_model, tlsm_bad_cert_handler, ctx ) != SECSuccess ) {
2277 PRErrorCode err = PR_GetError();
2278 Debug( LDAP_DEBUG_ANY,
2279 "TLS: error: could not set bad cert handler for moznss - error %d:%s\n",
2280 err, PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ), NULL );
2285 since a cert has been specified, assume the client wants to do cert auth
2287 if ( ctx->tc_certificate ) {
2288 if ( tlsm_clientauth_init( ctx ) ) {
2289 Debug( LDAP_DEBUG_ANY,
2290 "TLS: error: unable to set up client certificate authentication using '%s'\n",
2291 tlsm_ctx_subject_name(ctx), 0, 0 );
2295 } else { /* set up secure server */
2299 /* must have a certificate for the server to use */
2300 if ( !ctx->tc_certificate ) {
2301 Debug( LDAP_DEBUG_ANY,
2302 "TLS: error: no server certificate: must specify a certificate for the server to use\n",
2307 if (tlsm_find_and_verify_cert_key(ctx)) {
2308 Debug( LDAP_DEBUG_ANY,
2309 "TLS: error: unable to find and verify server's cert and key for certificate %s\n",
2310 tlsm_ctx_subject_name(ctx), 0, 0 );
2314 /* configure the socket to be a secure server socket */
2315 certKEA = NSS_FindCertKEAType( ctx->tc_certificate );
2316 status = SSL_ConfigSecureServer( ctx->tc_model, ctx->tc_certificate, ctx->tc_private_key, certKEA );
2318 if ( SECSuccess != status ) {
2319 PRErrorCode err = PR_GetError();
2320 Debug( LDAP_DEBUG_ANY,
2321 "TLS: error: unable to configure secure server using certificate '%s' - error %d:%s\n",
2322 tlsm_ctx_subject_name(ctx), err, PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ) );
2327 /* Callback for authenticating certificate */
2328 if ( SSL_AuthCertificateHook( ctx->tc_model, tlsm_auth_cert_handler,
2329 ctx ) != SECSuccess ) {
2330 PRErrorCode err = PR_GetError();
2331 Debug( LDAP_DEBUG_ANY,
2332 "TLS: error: could not set auth cert handler for moznss - error %d:%s\n",
2333 err, PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ), NULL );
2337 if ( SSL_HandshakeCallback( ctx->tc_model, tlsm_handshake_complete_cb, ctx ) ) {
2338 PRErrorCode err = PR_GetError();
2339 Debug( LDAP_DEBUG_ANY,
2340 "TLS: error: could not set handshake callback for moznss - error %d:%s\n",
2341 err, PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ), NULL );
2345 tlsm_free_config( ctx->tc_config );
2346 ctx->tc_config = NULL;
2352 tlsm_session *session;
2353 Sockbuf_IO_Desc *sbiod;
2354 /* there seems to be no portable way to determine if the
2355 sockbuf sd has been set to nonblocking mode - the
2356 call to ber_pvt_socket_set_nonblock() takes place
2357 before the tls socket is set up, so we cannot
2358 intercept that call either.
2359 On systems where fcntl is available, we can just
2360 F_GETFL and test for O_NONBLOCK. On other systems,
2361 we will just see if the IO op returns EAGAIN or EWOULDBLOCK,
2362 and just set this flag */
2365 * NSS tries hard to be backwards compatible with SSLv2 clients, or
2366 * clients that send an SSLv2 client hello. This message is not
2367 * tagged in any way, so NSS has no way to know if the incoming
2368 * message is a valid SSLv2 client hello or just some bogus data
2369 * (or cleartext LDAP). We store the first byte read from the
2370 * client here. The most common case will be a client sending
2371 * LDAP data instead of SSL encrypted LDAP data. This can happen,
2372 * for example, if using ldapsearch -Z - if the starttls fails,
2373 * the client will fallback to plain cleartext LDAP. So if we
2374 * see that the firstbyte is a valid LDAP tag, we can be
2375 * pretty sure this is happening.
2379 * NSS doesn't return SSL_ERROR_WANT_READ, SSL_ERROR_WANT_WRITE, etc.
2380 * when it is blocked, so we have to set a flag in the wrapped send
2381 * and recv calls that tells us what operation NSS was last blocked
2385 #define TLSM_WRITE 2
2389 static struct tls_data *
2390 tlsm_get_pvt_tls_data( PRFileDesc *fd )
2399 myfd = PR_GetIdentitiesLayer( fd, tlsm_layer_id );
2405 p = (struct tls_data *)myfd->secret;
2411 tlsm_is_non_ssl_message( PRFileDesc *fd, ber_tag_t *thebyte )
2416 *thebyte = LBER_DEFAULT;
2419 p = tlsm_get_pvt_tls_data( fd );
2420 if ( p == NULL || p->sbiod == NULL ) {
2424 if ( p->firsttag == LBER_SEQUENCE ) {
2426 *thebyte = p->firsttag;
2434 static tls_session *
2435 tlsm_session_new ( tls_ctx * ctx, int is_server )
2437 tlsm_ctx *c = (tlsm_ctx *)ctx;
2438 tlsm_session *session;
2443 c->tc_is_server = is_server;
2444 LDAP_MUTEX_LOCK( &tlsm_init_mutex );
2445 status = PR_CallOnceWithArg( &c->tc_callonce, tlsm_deferred_ctx_init, c );
2446 LDAP_MUTEX_UNLOCK( &tlsm_init_mutex );
2447 if ( PR_SUCCESS != status ) {
2448 PRErrorCode err = PR_GetError();
2449 Debug( LDAP_DEBUG_ANY,
2450 "TLS: error: could not initialize moznss security context - error %d:%s\n",
2451 err, PR_ErrorToString(err, PR_LANGUAGE_I_DEFAULT), NULL );
2455 fd = PR_CreateIOLayerStub( tlsm_layer_id, &tlsm_PR_methods );
2460 session = SSL_ImportFD( c->tc_model, fd );
2466 rc = SSL_ResetHandshake( session, is_server );
2468 PRErrorCode err = PR_GetError();
2469 Debug( LDAP_DEBUG_TRACE,
2470 "TLS: error: new session - reset handshake failure %d - error %d:%s\n",
2472 err ? PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ) : "unknown" );
2474 PR_Close( session );
2478 return (tls_session *)session;
2482 tlsm_session_accept_or_connect( tls_session *session, int is_accept )
2484 tlsm_session *s = (tlsm_session *)session;
2486 const char *op = is_accept ? "accept" : "connect";
2489 LDAP_MUTEX_LOCK( &tlsm_pem_mutex );
2491 rc = SSL_ForceHandshake( s );
2493 LDAP_MUTEX_UNLOCK( &tlsm_pem_mutex );
2496 PRErrorCode err = PR_GetError();
2498 if ( err == PR_WOULD_BLOCK_ERROR ) {
2499 ber_tag_t thetag = LBER_DEFAULT;
2500 /* see if we are blocked because of a bogus packet */
2501 if ( tlsm_is_non_ssl_message( s, &thetag ) ) { /* see if we received a non-SSL message */
2502 Debug( LDAP_DEBUG_ANY,
2503 "TLS: error: %s - error - received non-SSL message [0x%x]\n",
2504 op, (unsigned int)thetag, 0 );
2505 /* reset error to something more descriptive */
2506 PR_SetError( SSL_ERROR_RX_MALFORMED_HELLO_REQUEST, EPROTO );
2509 Debug( LDAP_DEBUG_ANY,
2510 "TLS: error: %s - force handshake failure: errno %d - moznss error %d\n",
2518 tlsm_session_accept( tls_session *session )
2520 return tlsm_session_accept_or_connect( session, 1 );
2524 tlsm_session_connect( LDAP *ld, tls_session *session )
2526 return tlsm_session_accept_or_connect( session, 0 );
2530 tlsm_session_upflags( Sockbuf *sb, tls_session *session, int rc )
2532 int prerror = PR_GetError();
2534 if ( ( prerror == PR_PENDING_INTERRUPT_ERROR ) || ( prerror == PR_WOULD_BLOCK_ERROR ) ) {
2535 tlsm_session *s = (tlsm_session *)session;
2536 struct tls_data *p = tlsm_get_pvt_tls_data( s );
2538 if ( p && ( p->io_flag == TLSM_READ ) ) {
2539 sb->sb_trans_needs_read = 1;
2541 } else if ( p && ( p->io_flag == TLSM_WRITE ) ) {
2542 sb->sb_trans_needs_write = 1;
2551 tlsm_session_errmsg( tls_session *sess, int rc, char *buf, size_t len )
2554 int prerror = PR_GetError();
2556 i = PR_GetErrorTextLength();
2558 char *msg = LDAP_MALLOC( i+1 );
2559 PR_GetErrorText( msg );
2560 memcpy( buf, msg, len );
2563 PR_GetErrorText( buf );
2564 } else if ( prerror ) {
2565 i = PR_snprintf( buf, len, "TLS error %d:%s",
2566 prerror, PR_ErrorToString( prerror, PR_LANGUAGE_I_DEFAULT ) );
2569 return ( i > 0 ) ? buf : NULL;
2573 tlsm_session_my_dn( tls_session *session, struct berval *der_dn )
2575 tlsm_session *s = (tlsm_session *)session;
2576 CERTCertificate *cert;
2578 cert = SSL_LocalCertificate( s );
2579 if (!cert) return LDAP_INVALID_CREDENTIALS;
2581 der_dn->bv_val = (char *)cert->derSubject.data;
2582 der_dn->bv_len = cert->derSubject.len;
2583 CERT_DestroyCertificate( cert );
2588 tlsm_session_peer_dn( tls_session *session, struct berval *der_dn )
2590 tlsm_session *s = (tlsm_session *)session;
2591 CERTCertificate *cert;
2593 cert = SSL_PeerCertificate( s );
2594 if (!cert) return LDAP_INVALID_CREDENTIALS;
2596 der_dn->bv_val = (char *)cert->derSubject.data;
2597 der_dn->bv_len = cert->derSubject.len;
2598 CERT_DestroyCertificate( cert );
2602 /* what kind of hostname were we given? */
2608 tlsm_session_chkhost( LDAP *ld, tls_session *session, const char *name_in )
2610 tlsm_session *s = (tlsm_session *)session;
2611 CERTCertificate *cert;
2612 const char *name, *domain = NULL, *ptr;
2613 int ret, ntype = IS_DNS, nlen, dlen;
2614 #ifdef LDAP_PF_INET6
2615 struct in6_addr addr;
2617 struct in_addr addr;
2622 if( ldap_int_hostname &&
2623 ( !name_in || !strcasecmp( name_in, "localhost" ) ) )
2625 name = ldap_int_hostname;
2629 nlen = strlen( name );
2631 cert = SSL_PeerCertificate( s );
2633 Debug( LDAP_DEBUG_ANY,
2634 "TLS: unable to get peer certificate.\n",
2636 /* if this was a fatal condition, things would have
2637 * aborted long before now.
2639 return LDAP_SUCCESS;
2642 #ifdef LDAP_PF_INET6
2643 if (inet_pton(AF_INET6, name, &addr)) {
2647 if ((ptr = strrchr(name, '.')) && isdigit((unsigned char)ptr[1])) {
2648 if (inet_aton(name, (struct in_addr *)&addr)) ntype = IS_IP4;
2650 if (ntype == IS_DNS ) {
2651 domain = strchr( name, '.' );
2653 dlen = nlen - ( domain - name );
2656 ret = LDAP_LOCAL_ERROR;
2658 rv = CERT_FindCertExtension( cert, SEC_OID_X509_SUBJECT_ALT_NAME,
2660 if ( rv == SECSuccess && altname.data ) {
2662 CERTGeneralName *names, *cur;
2664 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
2666 ret = LDAP_NO_MEMORY;
2670 names = cur = CERT_DecodeAltNameExtension(arena, &altname);
2679 if ( !cur->name.other.len ) continue;
2681 host = (char *)cur->name.other.data;
2682 hlen = cur->name.other.len;
2684 if ( cur->type == certDNSName ) {
2685 if ( ntype != IS_DNS ) continue;
2687 /* is this an exact match? */
2688 if ( nlen == hlen && !strncasecmp( name, host, nlen )) {
2693 /* is this a wildcard match? */
2694 if ( domain && host[0] == '*' && host[1] == '.' &&
2695 dlen == hlen-1 && !strncasecmp( domain, host+1, dlen )) {
2699 } else if ( cur->type == certIPAddress ) {
2700 if ( ntype == IS_DNS ) continue;
2702 #ifdef LDAP_PF_INET6
2703 if (ntype == IS_IP6 && hlen != sizeof(struct in6_addr)) {
2707 if (ntype == IS_IP4 && hlen != sizeof(struct in_addr)) {
2710 if (!memcmp(host, &addr, hlen)) {
2715 } while (( cur = CERT_GetNextGeneralName( cur )) != names );
2717 PORT_FreeArena( arena, PR_FALSE );
2718 SECITEM_FreeItem( &altname, PR_FALSE );
2720 /* no altnames matched, try the CN */
2721 if ( ret != LDAP_SUCCESS ) {
2722 /* find the last CN */
2723 CERTRDN *rdn, **rdns;
2724 CERTAVA *lastava = NULL;
2728 rdns = cert->subject.rdns;
2729 while ( rdns && ( rdn = *rdns++ )) {
2730 CERTAVA *ava, **avas = rdn->avas;
2731 while ( avas && ( ava = *avas++ )) {
2732 if ( CERT_GetAVATag( ava ) == SEC_OID_AVA_COMMON_NAME )
2737 SECItem *av = CERT_DecodeAVAValue( &lastava->value );
2739 if ( av->len == nlen && !strncasecmp( name, (char *)av->data, nlen )) {
2741 } else if ( av->data[0] == '*' && av->data[1] == '.' &&
2742 domain && dlen == av->len - 1 && !strncasecmp( domain,
2743 (char *)(av->data+1), dlen )) {
2747 if ( len >= sizeof(buf) )
2748 len = sizeof(buf)-1;
2749 memcpy( buf, av->data, len );
2752 SECITEM_FreeItem( av, PR_TRUE );
2755 if ( ret != LDAP_SUCCESS ) {
2756 Debug( LDAP_DEBUG_ANY, "TLS: hostname (%s) does not match "
2757 "common name in certificate (%s).\n",
2759 ret = LDAP_CONNECT_ERROR;
2760 if ( ld->ld_error ) {
2761 LDAP_FREE( ld->ld_error );
2763 ld->ld_error = LDAP_STRDUP(
2764 _("TLS: hostname does not match CN in peer certificate"));
2769 CERT_DestroyCertificate( cert );
2774 tlsm_session_strength( tls_session *session )
2776 tlsm_session *s = (tlsm_session *)session;
2779 rc = SSL_SecurityStatus( s, NULL, NULL, NULL, &keySize,
2781 return rc ? 0 : keySize;
2785 * TLS support for LBER Sockbufs
2788 static PRStatus PR_CALLBACK
2789 tlsm_PR_Close(PRFileDesc *fd)
2791 int rc = PR_SUCCESS;
2793 /* we don't need to actually close anything here, just
2794 pop our io layer off the stack */
2795 fd->secret = NULL; /* must have been freed before calling PR_Close */
2797 fd = PR_PopIOLayer( fd, tlsm_layer_id );
2798 /* if we are not the last layer, pass the close along */
2803 rc = fd->methods->close( fd );
2806 /* we are the last layer - just call our dtor */
2813 static PRStatus PR_CALLBACK
2814 tlsm_PR_Shutdown(PRFileDesc *fd, PRShutdownHow how)
2816 int rc = PR_SUCCESS;
2819 rc = PR_Shutdown( fd->lower, how );
2825 static int PR_CALLBACK
2826 tlsm_PR_Recv(PRFileDesc *fd, void *buf, PRInt32 len, PRIntn flags,
2827 PRIntervalTime timeout)
2832 if ( buf == NULL || len <= 0 ) return 0;
2834 p = tlsm_get_pvt_tls_data( fd );
2836 if ( p == NULL || p->sbiod == NULL ) {
2840 rc = LBER_SBIOD_READ_NEXT( p->sbiod, buf, len );
2842 tlsm_map_error( errno );
2843 if ( errno == EAGAIN || errno == EWOULDBLOCK ) {
2844 p->nonblock = PR_TRUE; /* fd is using non-blocking io */
2845 } else if ( errno ) { /* real error */
2846 Debug( LDAP_DEBUG_TRACE,
2847 "TLS: error: tlsm_PR_Recv returned %d - error %d:%s\n",
2848 rc, errno, STRERROR(errno) );
2850 } else if ( ( rc > 0 ) && ( len > 0 ) && ( p->firsttag == LBER_DEFAULT ) ) {
2851 p->firsttag = (ber_tag_t)*((char *)buf);
2853 p->io_flag = TLSM_READ;
2858 static int PR_CALLBACK
2859 tlsm_PR_Send(PRFileDesc *fd, const void *buf, PRInt32 len, PRIntn flags,
2860 PRIntervalTime timeout)
2865 if ( buf == NULL || len <= 0 ) return 0;
2867 p = tlsm_get_pvt_tls_data( fd );
2869 if ( p == NULL || p->sbiod == NULL ) {
2873 rc = LBER_SBIOD_WRITE_NEXT( p->sbiod, (char *)buf, len );
2875 tlsm_map_error( errno );
2876 if ( errno == EAGAIN || errno == EWOULDBLOCK ) {
2877 p->nonblock = PR_TRUE;
2878 } else if ( errno ) { /* real error */
2879 Debug( LDAP_DEBUG_TRACE,
2880 "TLS: error: tlsm_PR_Send returned %d - error %d:%s\n",
2881 rc, errno, STRERROR(errno) );
2884 p->io_flag = TLSM_WRITE;
2889 static int PR_CALLBACK
2890 tlsm_PR_Read(PRFileDesc *fd, void *buf, PRInt32 len)
2892 return tlsm_PR_Recv( fd, buf, len, 0, PR_INTERVAL_NO_TIMEOUT );
2895 static int PR_CALLBACK
2896 tlsm_PR_Write(PRFileDesc *fd, const void *buf, PRInt32 len)
2898 return tlsm_PR_Send( fd, buf, len, 0, PR_INTERVAL_NO_TIMEOUT );
2901 static PRStatus PR_CALLBACK
2902 tlsm_PR_GetPeerName(PRFileDesc *fd, PRNetAddr *addr)
2907 p = tlsm_get_pvt_tls_data( fd );
2909 if ( p == NULL || p->sbiod == NULL ) {
2912 len = sizeof(PRNetAddr);
2913 return getpeername( p->sbiod->sbiod_sb->sb_fd, (struct sockaddr *)addr, &len );
2916 static PRStatus PR_CALLBACK
2917 tlsm_PR_GetSocketOption(PRFileDesc *fd, PRSocketOptionData *data)
2920 p = tlsm_get_pvt_tls_data( fd );
2922 if ( p == NULL || data == NULL ) {
2926 /* only the nonblocking option is supported at this time
2927 MozNSS SSL code needs it */
2928 if ( data->option != PR_SockOpt_Nonblocking ) {
2929 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
2933 int flags = fcntl( p->sbiod->sbiod_sb->sb_fd, F_GETFL );
2934 data->value.non_blocking = (flags & O_NONBLOCK) ? PR_TRUE : PR_FALSE;
2936 data->value.non_blocking = p->nonblock;
2941 static PRStatus PR_CALLBACK
2944 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
2948 static PRFileDesc * PR_CALLBACK
2951 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
2955 static PRInt16 PR_CALLBACK
2958 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
2962 static PRInt32 PR_CALLBACK
2965 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
2969 static PRInt64 PR_CALLBACK
2974 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
2979 static const PRIOMethods tlsm_PR_methods = {
2981 tlsm_PR_Close, /* close */
2982 tlsm_PR_Read, /* read */
2983 tlsm_PR_Write, /* write */
2984 tlsm_PR_i32_unimp, /* available */
2985 tlsm_PR_i64_unimp, /* available64 */
2986 tlsm_PR_prs_unimp, /* fsync */
2987 tlsm_PR_i32_unimp, /* seek */
2988 tlsm_PR_i64_unimp, /* seek64 */
2989 tlsm_PR_prs_unimp, /* fileInfo */
2990 tlsm_PR_prs_unimp, /* fileInfo64 */
2991 tlsm_PR_i32_unimp, /* writev */
2992 tlsm_PR_prs_unimp, /* connect */
2993 tlsm_PR_pfd_unimp, /* accept */
2994 tlsm_PR_prs_unimp, /* bind */
2995 tlsm_PR_prs_unimp, /* listen */
2996 (PRShutdownFN)tlsm_PR_Shutdown, /* shutdown */
2997 tlsm_PR_Recv, /* recv */
2998 tlsm_PR_Send, /* send */
2999 tlsm_PR_i32_unimp, /* recvfrom */
3000 tlsm_PR_i32_unimp, /* sendto */
3001 (PRPollFN)tlsm_PR_i16_unimp, /* poll */
3002 tlsm_PR_i32_unimp, /* acceptread */
3003 tlsm_PR_i32_unimp, /* transmitfile */
3004 tlsm_PR_prs_unimp, /* getsockname */
3005 tlsm_PR_GetPeerName, /* getpeername */
3006 tlsm_PR_i32_unimp, /* getsockopt OBSOLETE */
3007 tlsm_PR_i32_unimp, /* setsockopt OBSOLETE */
3008 tlsm_PR_GetSocketOption, /* getsocketoption */
3009 tlsm_PR_i32_unimp, /* setsocketoption */
3010 tlsm_PR_i32_unimp, /* Send a (partial) file with header/trailer*/
3011 (PRConnectcontinueFN)tlsm_PR_prs_unimp, /* connectcontinue */
3012 tlsm_PR_i32_unimp, /* reserved for future use */
3013 tlsm_PR_i32_unimp, /* reserved for future use */
3014 tlsm_PR_i32_unimp, /* reserved for future use */
3015 tlsm_PR_i32_unimp /* reserved for future use */
3019 * Initialize TLS subsystem. Should be called only once.
3020 * See tlsm_deferred_init for the bulk of the init process
3025 char *nofork = PR_GetEnv( "NSS_STRICT_NOFORK" );
3029 tlsm_layer_id = PR_GetUniqueIdentity( "OpenLDAP" );
3032 * There are some applications that acquire a crypto context in the parent process
3033 * and expect that crypto context to work after a fork(). This does not work
3034 * with NSS using strict PKCS11 compliance mode. We set this environment
3035 * variable here to tell the software encryption module/token to allow crypto
3036 * contexts to persist across a fork(). However, if you are using some other
3037 * module or encryption device that supports and expects full PKCS11 semantics,
3038 * the only recourse is to rewrite the application with atfork() handlers to save
3039 * the crypto context in the parent and restore (and SECMOD_RestartModules) the
3040 * context in the child.
3043 /* will leak one time */
3044 char *noforkenvvar = PL_strdup( "NSS_STRICT_NOFORK=DISABLED" );
3045 PR_SetEnv( noforkenvvar );
3052 tlsm_sb_setup( Sockbuf_IO_Desc *sbiod, void *arg )
3055 tlsm_session *session = arg;
3058 assert( sbiod != NULL );
3060 p = LBER_MALLOC( sizeof( *p ) );
3065 fd = PR_GetIdentitiesLayer( session, tlsm_layer_id );
3071 fd->secret = (PRFilePrivate *)p;
3072 p->session = session;
3074 p->firsttag = LBER_DEFAULT;
3075 sbiod->sbiod_pvt = p;
3080 tlsm_sb_remove( Sockbuf_IO_Desc *sbiod )
3084 assert( sbiod != NULL );
3085 assert( sbiod->sbiod_pvt != NULL );
3087 p = (struct tls_data *)sbiod->sbiod_pvt;
3088 PR_Close( p->session );
3089 LBER_FREE( sbiod->sbiod_pvt );
3090 sbiod->sbiod_pvt = NULL;
3095 tlsm_sb_close( Sockbuf_IO_Desc *sbiod )
3099 assert( sbiod != NULL );
3100 assert( sbiod->sbiod_pvt != NULL );
3102 p = (struct tls_data *)sbiod->sbiod_pvt;
3103 PR_Shutdown( p->session, PR_SHUTDOWN_BOTH );
3108 tlsm_sb_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
3112 assert( sbiod != NULL );
3113 assert( sbiod->sbiod_pvt != NULL );
3115 p = (struct tls_data *)sbiod->sbiod_pvt;
3117 if ( opt == LBER_SB_OPT_GET_SSL ) {
3118 *((tlsm_session **)arg) = p->session;
3121 } else if ( opt == LBER_SB_OPT_DATA_READY ) {
3122 if ( p && ( SSL_DataPending( p->session ) > 0 ) ) {
3128 return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg );
3132 tlsm_sb_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
3138 assert( sbiod != NULL );
3139 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
3141 p = (struct tls_data *)sbiod->sbiod_pvt;
3143 ret = PR_Recv( p->session, buf, len, 0, PR_INTERVAL_NO_TIMEOUT );
3145 err = PR_GetError();
3146 if ( err == PR_PENDING_INTERRUPT_ERROR || err == PR_WOULD_BLOCK_ERROR ) {
3147 sbiod->sbiod_sb->sb_trans_needs_read = 1;
3148 sock_errset(EWOULDBLOCK);
3151 sbiod->sbiod_sb->sb_trans_needs_read = 0;
3157 tlsm_sb_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
3163 assert( sbiod != NULL );
3164 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
3166 p = (struct tls_data *)sbiod->sbiod_pvt;
3168 ret = PR_Send( p->session, (char *)buf, len, 0, PR_INTERVAL_NO_TIMEOUT );
3170 err = PR_GetError();
3171 if ( err == PR_PENDING_INTERRUPT_ERROR || err == PR_WOULD_BLOCK_ERROR ) {
3172 sbiod->sbiod_sb->sb_trans_needs_write = 1;
3173 sock_errset(EWOULDBLOCK);
3177 sbiod->sbiod_sb->sb_trans_needs_write = 0;
3182 static Sockbuf_IO tlsm_sbio =
3184 tlsm_sb_setup, /* sbi_setup */
3185 tlsm_sb_remove, /* sbi_remove */
3186 tlsm_sb_ctrl, /* sbi_ctrl */
3187 tlsm_sb_read, /* sbi_read */
3188 tlsm_sb_write, /* sbi_write */
3189 tlsm_sb_close /* sbi_close */
3192 tls_impl ldap_int_tls_impl = {
3204 tlsm_session_connect,
3205 tlsm_session_accept,
3206 tlsm_session_upflags,
3207 tlsm_session_errmsg,
3209 tlsm_session_peer_dn,
3210 tlsm_session_chkhost,
3211 tlsm_session_strength,
3215 #ifdef LDAP_R_COMPILE
3224 #endif /* HAVE_MOZNSS */