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 PRBool tc_verify_cert;
85 CERTCertDBHandle *tc_certdb;
88 struct ldaptls *tc_config;
91 PRCallOnceType tc_callonce;
93 char *tc_slotname; /* if using pem */
94 #ifdef HAVE_NSS_INITCONTEXT
95 NSSInitContext *tc_initctx; /* the NSS context */
97 PK11GenericObject **tc_pem_objs; /* array of objects to free */
98 int tc_n_pem_objs; /* number of objects */
99 PRBool tc_warn_only; /* only warn of errors in validation */
100 #ifdef LDAP_R_COMPILE
101 ldap_pvt_thread_mutex_t tc_refmutex;
105 typedef PRFileDesc tlsm_session;
107 static PRDescIdentity tlsm_layer_id;
109 static const PRIOMethods tlsm_PR_methods;
111 #define PEM_LIBRARY "nsspem"
112 #define PEM_MODULE "PEM"
113 /* hash files for use with cacertdir have this file name suffix */
114 #define PEM_CA_HASH_FILE_SUFFIX ".0"
115 #define PEM_CA_HASH_FILE_SUFFIX_LEN 2
117 static SECMODModule *pem_module;
119 #define DEFAULT_TOKEN_NAME "default"
120 /* sprintf format used to create token name */
121 #define TLSM_PEM_TOKEN_FMT "PEM Token #%ld"
123 static int tlsm_slot_count;
125 #define PK11_SETATTRS(x,id,v,l) (x)->type = (id); \
126 (x)->pValue=(v); (x)->ulValueLen = (l);
128 /* forward declaration */
129 static int tlsm_init( void );
131 #ifdef LDAP_R_COMPILE
133 /* it doesn't seem guaranteed that a client will call
134 tlsm_thr_init in a non-threaded context - so we have
135 to wrap the mutex creation in a prcallonce
137 static ldap_pvt_thread_mutex_t tlsm_init_mutex;
138 static ldap_pvt_thread_mutex_t tlsm_pem_mutex;
139 static PRCallOnceType tlsm_init_mutex_callonce = {0,0};
141 static PRStatus PR_CALLBACK
142 tlsm_thr_init_callonce( void )
144 if ( ldap_pvt_thread_mutex_init( &tlsm_init_mutex ) ) {
145 Debug( LDAP_DEBUG_ANY,
146 "TLS: could not create mutex for moznss initialization: %d\n", errno, 0, 0 );
150 if ( ldap_pvt_thread_mutex_init( &tlsm_pem_mutex ) ) {
151 Debug( LDAP_DEBUG_ANY,
152 "TLS: could not create mutex for PEM module: %d\n", errno, 0, 0 );
160 tlsm_thr_init( void )
162 ( void )PR_CallOnce( &tlsm_init_mutex_callonce, tlsm_thr_init_callonce );
165 #endif /* LDAP_R_COMPILE */
168 tlsm_dump_cipher_info(PRFileDesc *fd)
172 for (ii = 0; ii < SSL_NumImplementedCiphers; ++ii) {
173 PRInt32 cipher = (PRInt32)SSL_ImplementedCiphers[ii];
174 PRBool enabled = PR_FALSE;
176 SSLCipherSuiteInfo info;
179 SSL_CipherPrefGet(fd, cipher, &enabled);
181 SSL_CipherPrefGetDefault(cipher, &enabled);
183 SSL_CipherPolicyGet(cipher, &policy);
184 SSL_GetCipherSuiteInfo(cipher, &info, (PRUintn)sizeof(info));
185 Debug( LDAP_DEBUG_TRACE,
186 "TLS: cipher: %d - %s, enabled: %d, ",
187 info.cipherSuite, info.cipherSuiteName, enabled );
188 Debug( LDAP_DEBUG_TRACE,
189 "policy: %d\n", policy, 0, 0 );
195 /* Cipher definitions */
197 char *ossl_name; /* The OpenSSL cipher name */
198 int num; /* The cipher id */
199 int attr; /* cipher attributes: algorithms, etc */
200 int version; /* protocol version valid for this cipher */
201 int bits; /* bits of strength */
202 int alg_bits; /* bits of the algorithm */
203 int strength; /* LOW, MEDIUM, HIGH */
204 int enabled; /* Enabled by default? */
207 /* cipher attributes */
208 #define SSL_kRSA 0x00000001L
209 #define SSL_aRSA 0x00000002L
210 #define SSL_aDSS 0x00000004L
211 #define SSL_DSS SSL_aDSS
212 #define SSL_eNULL 0x00000008L
213 #define SSL_DES 0x00000010L
214 #define SSL_3DES 0x00000020L
215 #define SSL_RC4 0x00000040L
216 #define SSL_RC2 0x00000080L
217 #define SSL_AES 0x00000100L
218 #define SSL_MD5 0x00000200L
219 #define SSL_SHA1 0x00000400L
220 #define SSL_SHA SSL_SHA1
221 #define SSL_RSA (SSL_kRSA|SSL_aRSA)
223 /* cipher strength */
224 #define SSL_NULL 0x00000001L
225 #define SSL_EXPORT40 0x00000002L
226 #define SSL_EXPORT56 0x00000004L
227 #define SSL_LOW 0x00000008L
228 #define SSL_MEDIUM 0x00000010L
229 #define SSL_HIGH 0x00000020L
231 #define SSL2 0x00000001L
232 #define SSL3 0x00000002L
233 /* OpenSSL treats SSL3 and TLSv1 the same */
236 /* Cipher translation */
237 static cipher_properties ciphers_def[] = {
239 {"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},
240 {"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},
241 {"RC4-MD5", SSL_EN_RC4_128_WITH_MD5, SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_MD5, SSL2, 128, 128, SSL_MEDIUM, SSL_ALLOWED},
242 {"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},
243 {"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},
244 {"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},
247 {"RC4-MD5", SSL_RSA_WITH_RC4_128_MD5, SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_MD5, SSL3, 128, 128, SSL_MEDIUM, SSL_ALLOWED},
248 {"RC4-SHA", SSL_RSA_WITH_RC4_128_SHA, SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_SHA1, SSL3, 128, 128, SSL_MEDIUM, SSL_ALLOWED},
249 {"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},
250 {"DES-CBC-SHA", SSL_RSA_WITH_DES_CBC_SHA, SSL_kRSA|SSL_aRSA|SSL_DES|SSL_SHA1, SSL3, 56, 56, SSL_LOW, SSL_ALLOWED},
251 {"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},
252 {"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},
253 {"NULL-MD5", SSL_RSA_WITH_NULL_MD5, SSL_kRSA|SSL_aRSA|SSL_eNULL|SSL_MD5, SSL3, 0, 0, SSL_NULL, SSL_NOT_ALLOWED},
254 {"NULL-SHA", SSL_RSA_WITH_NULL_SHA, SSL_kRSA|SSL_aRSA|SSL_eNULL|SSL_SHA1, SSL3, 0, 0, SSL_NULL, SSL_NOT_ALLOWED},
257 {"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},
258 {"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},
259 {"AES128-SHA", TLS_RSA_WITH_AES_128_CBC_SHA, SSL_kRSA|SSL_aRSA|SSL_AES|SSL_SHA, TLS1, 128, 128, SSL_HIGH, SSL_ALLOWED},
260 {"AES256-SHA", TLS_RSA_WITH_AES_256_CBC_SHA, SSL_kRSA|SSL_aRSA|SSL_AES|SSL_SHA, TLS1, 256, 256, SSL_HIGH, SSL_ALLOWED},
263 #define ciphernum (sizeof(ciphers_def)/sizeof(cipher_properties))
265 /* given err which is the current errno, calls PR_SetError with
266 the corresponding NSPR error code */
268 tlsm_map_error(int err)
274 prError = PR_NO_ACCESS_RIGHTS_ERROR;
277 prError = PR_ADDRESS_IN_USE_ERROR;
280 prError = PR_ADDRESS_NOT_AVAILABLE_ERROR;
283 prError = PR_ADDRESS_NOT_SUPPORTED_ERROR;
286 prError = PR_WOULD_BLOCK_ERROR;
289 * On QNX and Neutrino, EALREADY is defined as EBUSY.
291 #if EALREADY != EBUSY
293 prError = PR_ALREADY_INITIATED_ERROR;
297 prError = PR_BAD_DESCRIPTOR_ERROR;
301 prError = PR_IO_ERROR;
305 prError = PR_FILESYSTEM_MOUNTED_ERROR;
308 prError = PR_CONNECT_ABORTED_ERROR;
311 prError = PR_CONNECT_REFUSED_ERROR;
314 prError = PR_CONNECT_RESET_ERROR;
317 prError = PR_DEADLOCK_ERROR;
321 prError = PR_DIRECTORY_CORRUPTED_ERROR;
326 prError = PR_NO_DEVICE_SPACE_ERROR;
330 prError = PR_FILE_EXISTS_ERROR;
333 prError = PR_ACCESS_FAULT_ERROR;
336 prError = PR_FILE_TOO_BIG_ERROR;
339 prError = PR_HOST_UNREACHABLE_ERROR;
342 prError = PR_IN_PROGRESS_ERROR;
345 prError = PR_PENDING_INTERRUPT_ERROR;
348 prError = PR_INVALID_ARGUMENT_ERROR;
351 prError = PR_IO_ERROR;
354 prError = PR_IS_CONNECTED_ERROR;
357 prError = PR_IS_DIRECTORY_ERROR;
360 prError = PR_LOOP_ERROR;
363 prError = PR_PROC_DESC_TABLE_FULL_ERROR;
366 prError = PR_MAX_DIRECTORY_ENTRIES_ERROR;
369 prError = PR_INVALID_ARGUMENT_ERROR;
373 prError = PR_REMOTE_FILE_ERROR;
377 prError = PR_NAME_TOO_LONG_ERROR;
380 prError = PR_NETWORK_UNREACHABLE_ERROR;
383 prError = PR_SYS_DESC_TABLE_FULL_ERROR;
386 * On SCO OpenServer 5, ENOBUFS is defined as ENOSR.
388 #if defined(ENOBUFS) && (ENOBUFS != ENOSR)
390 prError = PR_INSUFFICIENT_RESOURCES_ERROR;
394 prError = PR_FILE_NOT_FOUND_ERROR;
397 prError = PR_FILE_NOT_FOUND_ERROR;
400 prError = PR_FILE_IS_LOCKED_ERROR;
404 prError = PR_REMOTE_FILE_ERROR;
408 prError = PR_OUT_OF_MEMORY_ERROR;
411 prError = PR_INVALID_ARGUMENT_ERROR;
414 prError = PR_NO_DEVICE_SPACE_ERROR;
418 prError = PR_INSUFFICIENT_RESOURCES_ERROR;
422 prError = PR_NOT_CONNECTED_ERROR;
425 prError = PR_NOT_DIRECTORY_ERROR;
428 prError = PR_NOT_SOCKET_ERROR;
431 prError = PR_FILE_NOT_FOUND_ERROR;
434 prError = PR_NOT_TCP_SOCKET_ERROR;
438 prError = PR_BUFFER_OVERFLOW_ERROR;
442 prError = PR_NO_ACCESS_RIGHTS_ERROR;
445 prError = PR_CONNECT_RESET_ERROR;
449 prError = PR_IO_ERROR;
452 case EPROTONOSUPPORT:
453 prError = PR_PROTOCOL_NOT_SUPPORTED_ERROR;
456 prError = PR_ADDRESS_NOT_SUPPORTED_ERROR;
459 prError = PR_INVALID_METHOD_ERROR;
462 prError = PR_READ_ONLY_FILESYSTEM_ERROR;
465 prError = PR_INVALID_METHOD_ERROR;
468 prError = PR_IO_TIMEOUT_ERROR;
470 #if EWOULDBLOCK != EAGAIN
472 prError = PR_WOULD_BLOCK_ERROR;
476 prError = PR_NOT_SAME_DEVICE_ERROR;
479 prError = PR_UNKNOWN_ERROR;
482 PR_SetError( prError, err );
486 * cipher_list is an integer array with the following values:
487 * -1: never enable this cipher
492 nss_parse_ciphers(const char *cipherstr, int cipher_list[ciphernum])
501 /* All disabled to start */
502 for (i=0; i<ciphernum; i++)
505 ciphertip = strdup(cipherstr);
506 cipher = ciphers = ciphertip;
508 while (ciphers && (strlen(ciphers))) {
509 while ((*cipher) && (isspace(*cipher)))
514 case '+': /* Add something */
518 case '-': /* Subtract something */
522 case '!': /* Disable something */
531 if ((ciphers = strchr(cipher, ':'))) {
535 /* Do the easy one first */
536 if (!strcmp(cipher, "ALL")) {
537 for (i=0; i<ciphernum; i++) {
538 if (!(ciphers_def[i].attr & SSL_eNULL))
539 cipher_list[i] = action;
541 } else if (!strcmp(cipher, "COMPLEMENTOFALL")) {
542 for (i=0; i<ciphernum; i++) {
543 if ((ciphers_def[i].attr & SSL_eNULL))
544 cipher_list[i] = action;
546 } else if (!strcmp(cipher, "DEFAULT")) {
547 for (i=0; i<ciphernum; i++) {
548 cipher_list[i] = ciphers_def[i].enabled == SSL_ALLOWED ? 1 : 0;
557 while (c && (strlen(c))) {
559 if ((c = strchr(cipher, '+'))) {
563 if (!strcmp(cipher, "RSA")) {
565 } else if ((!strcmp(cipher, "NULL")) || (!strcmp(cipher, "eNULL"))) {
567 } else if (!strcmp(cipher, "AES")) {
569 } else if (!strcmp(cipher, "3DES")) {
571 } else if (!strcmp(cipher, "DES")) {
573 } else if (!strcmp(cipher, "RC4")) {
575 } else if (!strcmp(cipher, "RC2")) {
577 } else if (!strcmp(cipher, "MD5")) {
579 } else if ((!strcmp(cipher, "SHA")) || (!strcmp(cipher, "SHA1"))) {
581 } else if (!strcmp(cipher, "SSLv2")) {
583 } else if (!strcmp(cipher, "SSLv3")) {
585 } else if (!strcmp(cipher, "TLSv1")) {
587 } else if (!strcmp(cipher, "HIGH")) {
588 strength |= SSL_HIGH;
589 } else if (!strcmp(cipher, "MEDIUM")) {
590 strength |= SSL_MEDIUM;
591 } else if (!strcmp(cipher, "LOW")) {
593 } else if ((!strcmp(cipher, "EXPORT")) || (!strcmp(cipher, "EXP"))) {
594 strength |= SSL_EXPORT40|SSL_EXPORT56;
595 } else if (!strcmp(cipher, "EXPORT40")) {
596 strength |= SSL_EXPORT40;
597 } else if (!strcmp(cipher, "EXPORT56")) {
598 strength |= SSL_EXPORT56;
606 /* If we have a mask, apply it. If not then perhaps they provided
607 * a specific cipher to enable.
609 if (mask || strength || protocol) {
610 for (i=0; i<ciphernum; i++) {
611 if (((ciphers_def[i].attr & mask) ||
612 (ciphers_def[i].strength & strength) ||
613 (ciphers_def[i].version & protocol)) &&
614 (cipher_list[i] != -1)) {
615 /* Enable the NULL ciphers only if explicity
617 if (ciphers_def[i].attr & SSL_eNULL) {
618 if (mask & SSL_eNULL)
619 cipher_list[i] = action;
621 cipher_list[i] = action;
625 for (i=0; i<ciphernum; i++) {
626 if (!strcmp(ciphers_def[i].ossl_name, cipher) &&
627 cipher_list[1] != -1)
628 cipher_list[i] = action;
637 /* See if any ciphers were enabled */
639 for (i=0; i<ciphernum; i++) {
640 if (cipher_list[i] == 1)
650 tlsm_parse_ciphers(tlsm_ctx *ctx, const char *str)
652 int cipher_state[ciphernum];
658 rv = nss_parse_ciphers(str, cipher_state);
661 /* First disable everything */
662 for (i = 0; i < SSL_NumImplementedCiphers; i++)
663 SSL_CipherPrefSet(ctx->tc_model, SSL_ImplementedCiphers[i], SSL_NOT_ALLOWED);
665 /* Now enable what was requested */
666 for (i=0; i<ciphernum; i++) {
667 SSLCipherSuiteInfo suite;
670 if (SSL_GetCipherSuiteInfo(ciphers_def[i].num, &suite, sizeof suite)
672 enabled = cipher_state[i] < 0 ? 0 : cipher_state[i];
673 if (enabled == SSL_ALLOWED) {
674 if (PK11_IsFIPS() && !suite.isFIPS)
675 enabled = SSL_NOT_ALLOWED;
677 SSL_CipherPrefSet(ctx->tc_model, ciphers_def[i].num, enabled);
682 return rv == 1 ? 0 : -1;
686 tlsm_bad_cert_handler(void *arg, PRFileDesc *ssl)
688 SECStatus success = SECSuccess;
690 tlsm_ctx *ctx = (tlsm_ctx *)arg;
696 err = PORT_GetError();
699 case SEC_ERROR_UNTRUSTED_ISSUER:
700 case SEC_ERROR_UNKNOWN_ISSUER:
701 case SEC_ERROR_EXPIRED_CERTIFICATE:
702 case SEC_ERROR_EXPIRED_ISSUER_CERTIFICATE:
703 if (ctx->tc_verify_cert) {
704 success = SECFailure;
707 /* we bypass NSS's hostname checks and do our own */
708 case SSL_ERROR_BAD_CERT_DOMAIN:
711 success = SECFailure;
719 tlsm_dump_security_status(PRFileDesc *fd)
721 char * cp; /* bulk cipher name */
722 char * ip; /* cert issuer DN */
723 char * sp; /* cert subject DN */
724 int op; /* High, Low, Off */
725 int kp0; /* total key bits */
726 int kp1; /* secret key bits */
727 SSL3Statistics * ssl3stats = SSL_GetStatistics();
729 SSL_SecurityStatus( fd, &op, &cp, &kp0, &kp1, &ip, &sp );
730 Debug( LDAP_DEBUG_TRACE,
731 "TLS certificate verification: subject: %s, issuer: %s, cipher: %s, ",
732 sp ? sp : "-unknown-", ip ? ip : "-unknown-", cp ? cp : "-unknown-" );
736 Debug( LDAP_DEBUG_TRACE,
737 "security level: %s, secret key bits: %d, total key bits: %d, ",
738 ((op == SSL_SECURITY_STATUS_ON_HIGH) ? "high" :
739 ((op == SSL_SECURITY_STATUS_ON_LOW) ? "low" : "off")),
742 Debug( LDAP_DEBUG_TRACE,
743 "cache hits: %ld, cache misses: %ld, cache not reusable: %ld\n",
744 ssl3stats->hch_sid_cache_hits, ssl3stats->hch_sid_cache_misses,
745 ssl3stats->hch_sid_cache_not_ok );
751 tlsm_handshake_complete_cb( PRFileDesc *fd, void *client_data )
753 tlsm_dump_security_status( fd );
756 #ifdef READ_PASSWORD_FROM_FILE
758 tlsm_get_pin_from_file(const char *token_name, tlsm_ctx *ctx)
761 char *contents = NULL;
764 char *candidate = NULL;
765 PRFileInfo file_info;
766 PRFileDesc *pwd_fileptr = PR_Open( ctx->tc_pin_file, PR_RDONLY, 00400 );
768 /* open the password file */
769 if ( !pwd_fileptr ) {
770 PRErrorCode errcode = PR_GetError();
771 Debug( LDAP_DEBUG_ANY,
772 "TLS: could not open security pin file %s - error %d:%s.\n",
773 ctx->tc_pin_file, errcode,
774 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
778 /* get the file size */
779 if ( PR_SUCCESS != PR_GetFileInfo( ctx->tc_pin_file, &file_info ) ) {
780 PRErrorCode errcode = PR_GetError();
781 Debug( LDAP_DEBUG_ANY,
782 "TLS: could not get file info from pin file %s - error %d:%s.\n",
783 ctx->tc_pin_file, errcode,
784 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
788 /* create a buffer to hold the file contents */
789 if ( !( contents = PR_MALLOC( file_info.size + 1 ) ) ) {
790 PRErrorCode errcode = PR_GetError();
791 Debug( LDAP_DEBUG_ANY,
792 "TLS: could not alloc a buffer for contents of pin file %s - error %d:%s.\n",
793 ctx->tc_pin_file, errcode,
794 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
798 /* read file into the buffer */
799 if( PR_Read( pwd_fileptr, contents, file_info.size ) <= 0 ) {
800 PRErrorCode errcode = PR_GetError();
801 Debug( LDAP_DEBUG_ANY,
802 "TLS: could not read the file contents from pin file %s - error %d:%s.\n",
803 ctx->tc_pin_file, errcode,
804 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
808 /* format is [tokenname:]password EOL [tokenname:]password EOL ... */
809 /* if you want to use a password containing a colon character, use
810 the special tokenname "default" */
811 for ( line = PL_strtok_r( contents, "\r\n", &lasts ); line;
812 line = PL_strtok_r( NULL, "\r\n", &lasts ) ) {
816 continue; /* skip blank lines */
818 colon = PL_strchr( line, ':' );
820 if ( *(colon + 1) && token_name &&
821 !PL_strncmp( token_name, line, colon-line ) ) {
822 candidate = colon + 1; /* found a definite match */
824 } else if ( !PL_strncmp( DEFAULT_TOKEN_NAME, line, colon-line ) ) {
825 candidate = colon + 1; /* found possible match */
827 } else { /* no token name */
833 PR_Close( pwd_fileptr );
836 pwdstr = PL_strdup( candidate );
838 PL_strfree( contents );
842 #endif /* READ_PASSWORD_FROM_FILE */
844 #ifdef READ_PASSWORD_FROM_STDIN
846 * Turn the echoing off on a tty.
851 if ( isatty( fd ) ) {
853 tcgetattr( fd, &tio );
854 tio.c_lflag &= ~ECHO;
855 tcsetattr( fd, TCSAFLUSH, &tio );
860 * Turn the echoing on on a tty.
865 if ( isatty( fd ) ) {
867 tcgetattr( fd, &tio );
869 tcsetattr( fd, TCSAFLUSH, &tio );
870 tcsetattr( fd, TCSAFLUSH, &tio );
873 #endif /* READ_PASSWORD_FROM_STDIN */
876 * This does the actual work of reading the pin/password/pass phrase
879 tlsm_get_pin(PK11SlotInfo *slot, PRBool retry, tlsm_ctx *ctx)
881 char *token_name = NULL;
884 token_name = PK11_GetTokenName( slot );
885 #ifdef READ_PASSWORD_FROM_FILE
886 /* Try to get the passwords from the password file if it exists.
887 * THIS IS UNSAFE and is provided for convenience only. Without this
888 * capability the server would have to be started in foreground mode
889 * if using an encrypted key.
891 if ( ctx->tc_pin_file ) {
892 pwdstr = tlsm_get_pin_from_file( token_name, ctx );
894 #endif /* RETRIEVE_PASSWORD_FROM_FILE */
895 #ifdef READ_PASSWORD_FROM_STDIN
897 int infd = PR_FileDesc2NativeHandle( PR_STDIN );
898 int isTTY = isatty( infd );
899 unsigned char phrase[200];
900 /* Prompt for password */
903 "Please enter pin, password, or pass phrase for security token '%s': ",
904 token_name ? token_name : DEFAULT_TOKEN_NAME );
907 fgets( (char*)phrase, sizeof(phrase), stdin );
909 fprintf( stdout, "\n" );
912 /* stomp on newline */
913 phrase[strlen((char*)phrase)-1] = 0;
915 pwdstr = PL_strdup( (char*)phrase );
918 #endif /* READ_PASSWORD_FROM_STDIN */
923 * PKCS11 devices (including the internal softokn cert/key database)
924 * may be protected by a pin or password or even pass phrase
925 * MozNSS needs a way for the user to provide that
928 tlsm_pin_prompt(PK11SlotInfo *slot, PRBool retry, void *arg)
930 tlsm_ctx *ctx = (tlsm_ctx *)arg;
932 return tlsm_get_pin( slot, retry, ctx );
936 tlsm_get_basic_constraint_extension( CERTCertificate *cert,
937 CERTBasicConstraints *cbcval )
939 SECItem encodedVal = { 0, NULL };
942 rc = CERT_FindCertExtension( cert, SEC_OID_X509_BASIC_CONSTRAINTS,
944 if ( rc != SECSuccess ) {
948 rc = CERT_DecodeBasicConstraintValue( cbcval, &encodedVal );
950 /* free the raw extension data */
951 PORT_Free( encodedVal.data );
957 tlsm_cert_is_self_issued( CERTCertificate *cert )
959 /* A cert is self-issued if its subject and issuer are equal and
960 * both are of non-zero length.
962 PRBool is_self_issued = cert &&
963 (PRBool)SECITEM_ItemsAreEqual( &cert->derIssuer,
964 &cert->derSubject ) &&
965 cert->derSubject.len > 0;
966 return is_self_issued;
970 tlsm_verify_cert(CERTCertDBHandle *handle, CERTCertificate *cert, void *pinarg,
971 PRBool checksig, SECCertificateUsage certUsage, int errorToIgnore )
973 CERTVerifyLog verifylog;
974 SECStatus ret = SECSuccess;
976 int debug_level = LDAP_DEBUG_ANY;
978 if ( errorToIgnore == -1 ) {
979 debug_level = LDAP_DEBUG_TRACE;
982 /* the log captures information about every cert in the chain, so we can tell
983 which cert caused the problem and what the problem was */
984 memset( &verifylog, 0, sizeof( verifylog ) );
985 verifylog.arena = PORT_NewArena( DER_DEFAULT_CHUNKSIZE );
986 if ( verifylog.arena == NULL ) {
987 Debug( LDAP_DEBUG_ANY,
988 "TLS certificate verification: Out of memory for certificate verification logger\n",
992 ret = CERT_VerifyCertificate( handle, cert, checksig, certUsage, PR_Now(), pinarg, &verifylog,
994 if ( ( name = cert->subjectName ) == NULL ) {
995 name = cert->nickname;
997 if ( verifylog.head == NULL ) {
998 /* it is possible for CERT_VerifyCertificate return with an error with no logging */
999 if ( ret != SECSuccess ) {
1000 PRErrorCode errcode = PR_GetError();
1002 "TLS: certificate [%s] is not valid - error %d:%s.\n",
1003 name ? name : "(unknown)",
1004 errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1008 CERTVerifyLogNode *node;
1010 ret = SECSuccess; /* reset */
1011 node = verifylog.head;
1013 if ( ( name = node->cert->subjectName ) == NULL ) {
1014 name = node->cert->nickname;
1016 if ( node->error ) {
1017 /* NSS does not like CA certs that have the basic constraints extension
1018 with the CA flag set to FALSE - openssl doesn't check if the cert
1020 if ( ( node->error == SEC_ERROR_CA_CERT_INVALID ) &&
1021 tlsm_cert_is_self_issued( node->cert ) ) {
1022 CERTBasicConstraints basicConstraint;
1023 SECStatus rv = tlsm_get_basic_constraint_extension( node->cert, &basicConstraint );
1024 if ( ( rv == SECSuccess ) && ( basicConstraint.isCA == PR_FALSE ) ) {
1025 Debug( LDAP_DEBUG_TRACE,
1026 "TLS: certificate [%s] is not correct because it is a CA cert and the "
1027 "BasicConstraint CA flag is set to FALSE - allowing for now, but "
1028 "please fix your certs if possible\n", name, 0, 0 );
1029 } else { /* does not have basicconstraint, or some other error */
1032 "TLS: certificate [%s] is not valid - CA cert is not valid\n",
1035 } else if ( errorToIgnore && ( node->error == errorToIgnore ) ) {
1037 "TLS: Warning: ignoring error for certificate [%s] - error %ld:%s.\n",
1038 name, node->error, PR_ErrorToString( node->error, PR_LANGUAGE_I_DEFAULT ) );
1042 "TLS: certificate [%s] is not valid - error %ld:%s.\n",
1043 name, node->error, PR_ErrorToString( node->error, PR_LANGUAGE_I_DEFAULT ) );
1046 CERT_DestroyCertificate( node->cert );
1051 PORT_FreeArena( verifylog.arena, PR_FALSE );
1053 if ( ret == SECSuccess ) {
1054 Debug( LDAP_DEBUG_TRACE,
1055 "TLS: certificate [%s] is valid\n", name, 0, 0 );
1056 } else if ( errorToIgnore == -1 ) {
1064 tlsm_auth_cert_handler(void *arg, PRFileDesc *fd,
1065 PRBool checksig, PRBool isServer)
1067 SECCertificateUsage certUsage = isServer ? certificateUsageSSLClient : certificateUsageSSLServer;
1068 SECStatus ret = SECSuccess;
1069 CERTCertificate *peercert = SSL_PeerCertificate( fd );
1070 int errorToIgnore = 0;
1071 tlsm_ctx *ctx = (tlsm_ctx *)arg;
1073 if (ctx && ctx->tc_warn_only )
1076 ret = tlsm_verify_cert( ctx->tc_certdb, peercert,
1077 SSL_RevealPinArg( fd ),
1078 checksig, certUsage, errorToIgnore );
1079 CERT_DestroyCertificate( peercert );
1085 tlsm_authenticate_to_slot( tlsm_ctx *ctx, PK11SlotInfo *slot )
1089 if ( SECSuccess != PK11_Authenticate( slot, PR_FALSE, ctx ) ) {
1090 char *token_name = PK11_GetTokenName( slot );
1091 PRErrorCode errcode = PR_GetError();
1092 Debug( LDAP_DEBUG_ANY,
1093 "TLS: could not authenticate to the security token %s - error %d:%s.\n",
1094 token_name ? token_name : DEFAULT_TOKEN_NAME, errcode,
1095 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1097 rc = 0; /* success */
1104 tlsm_nss_shutdown_cb( void *appData, void *nssData )
1106 SECStatus rc = SECSuccess;
1108 SSL_ShutdownServerSessionIDCache();
1111 SECMOD_UnloadUserModule( pem_module );
1112 SECMOD_DestroyModule( pem_module );
1118 static PRCallOnceType tlsm_register_shutdown_callonce = {0,0};
1119 static PRStatus PR_CALLBACK
1120 tlsm_register_nss_shutdown_cb( void )
1122 if ( SECSuccess == NSS_RegisterShutdown( tlsm_nss_shutdown_cb,
1130 tlsm_register_nss_shutdown( void )
1132 return PR_CallOnce( &tlsm_register_shutdown_callonce,
1133 tlsm_register_nss_shutdown_cb );
1137 tlsm_init_pem_module( void )
1140 char *fullname = NULL;
1141 char *configstring = NULL;
1147 /* not loaded - load it */
1148 /* get the system dependent library name */
1149 fullname = PR_GetLibraryName( NULL, PEM_LIBRARY );
1150 /* Load our PKCS#11 module */
1151 configstring = PR_smprintf( "library=%s name=" PEM_MODULE " parameters=\"\"", fullname );
1152 PL_strfree( fullname );
1154 pem_module = SECMOD_LoadUserModule( configstring, NULL, PR_FALSE );
1155 PR_smprintf_free( configstring );
1157 if ( !pem_module || !pem_module->loaded ) {
1159 SECMOD_DestroyModule( pem_module );
1169 tlsm_add_pem_obj( tlsm_ctx *ctx, PK11GenericObject *obj )
1171 int idx = ctx->tc_n_pem_objs;
1172 ctx->tc_n_pem_objs++;
1173 ctx->tc_pem_objs = (PK11GenericObject **)
1174 PORT_Realloc( ctx->tc_pem_objs, ctx->tc_n_pem_objs * sizeof( PK11GenericObject * ) );
1175 ctx->tc_pem_objs[idx] = obj;
1179 tlsm_free_pem_objs( tlsm_ctx *ctx )
1181 /* free in reverse order of allocation */
1182 while ( ctx->tc_n_pem_objs-- ) {
1183 PK11_DestroyGenericObject( ctx->tc_pem_objs[ctx->tc_n_pem_objs] );
1184 ctx->tc_pem_objs[ctx->tc_n_pem_objs] = NULL;
1186 PORT_Free(ctx->tc_pem_objs);
1187 ctx->tc_pem_objs = NULL;
1188 ctx->tc_n_pem_objs = 0;
1192 tlsm_add_cert_from_file( tlsm_ctx *ctx, const char *filename, PRBool isca, PRBool istrusted )
1195 PK11SlotInfo *slot = NULL;
1196 PK11GenericObject *rv;
1197 CK_ATTRIBUTE *attrs;
1198 CK_ATTRIBUTE theTemplate[20];
1199 CK_BBOOL cktrue = CK_TRUE;
1200 CK_BBOOL ckfalse = CK_FALSE;
1201 CK_OBJECT_CLASS objClass = CKO_CERTIFICATE;
1202 char tmpslotname[64];
1203 char *slotname = NULL;
1204 const char *ptr = NULL;
1205 char sep = PR_GetDirectorySeparator();
1209 memset( &fi, 0, sizeof(fi) );
1210 status = PR_GetFileInfo( filename, &fi );
1211 if ( PR_SUCCESS != status) {
1212 PRErrorCode errcode = PR_GetError();
1213 Debug( LDAP_DEBUG_ANY,
1214 "TLS: could not read certificate file %s - error %d:%s.\n",
1216 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1220 if ( fi.type != PR_FILE_FILE ) {
1221 PR_SetError(PR_IS_DIRECTORY_ERROR, 0);
1222 Debug( LDAP_DEBUG_ANY,
1223 "TLS: error: the certificate file %s is not a file.\n",
1228 attrs = theTemplate;
1231 slotID = 0; /* CA and trust objects use slot 0 */
1232 PR_snprintf( tmpslotname, sizeof(tmpslotname), TLSM_PEM_TOKEN_FMT, slotID );
1233 slotname = tmpslotname;
1234 istrusted = PR_TRUE;
1236 if ( ctx->tc_slotname == NULL ) { /* need new slot */
1240 slotID = ++tlsm_slot_count;
1242 ctx->tc_slotname = PR_smprintf( TLSM_PEM_TOKEN_FMT, slotID );
1244 slotname = ctx->tc_slotname;
1246 if ( ( ptr = PL_strrchr( filename, sep ) ) ) {
1247 PL_strfree( ctx->tc_certname );
1250 /* pemnss conflates trusted certs with CA certs - since there can
1251 be more than one CA cert in a file (e.g. ca-bundle.crt) pemnss
1252 numbers each trusted cert - in the case of a server cert, there will be
1253 only one, so it will be number 0 */
1254 ctx->tc_certname = PR_smprintf( "%s:%s - 0", slotname, ptr );
1256 ctx->tc_certname = PR_smprintf( "%s:%s", slotname, ptr );
1261 slot = PK11_FindSlotByName( slotname );
1264 PRErrorCode errcode = PR_GetError();
1265 Debug( LDAP_DEBUG_ANY,
1266 "TLS: could not find the slot for certificate %s - error %d:%s.\n",
1267 ctx->tc_certname, errcode,
1268 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1272 PK11_SETATTRS( attrs, CKA_CLASS, &objClass, sizeof(objClass) ); attrs++;
1273 PK11_SETATTRS( attrs, CKA_TOKEN, &cktrue, sizeof(CK_BBOOL) ); attrs++;
1274 PK11_SETATTRS( attrs, CKA_LABEL, (unsigned char *)filename, strlen(filename)+1 ); attrs++;
1276 PK11_SETATTRS( attrs, CKA_TRUST, &cktrue, sizeof(CK_BBOOL) ); attrs++;
1278 PK11_SETATTRS( attrs, CKA_TRUST, &ckfalse, sizeof(CK_BBOOL) ); attrs++;
1280 /* This loads the certificate in our PEM module into the appropriate
1283 rv = PK11_CreateGenericObject( slot, theTemplate, 4, PR_FALSE /* isPerm */ );
1285 PK11_FreeSlot( slot );
1288 PRErrorCode errcode = PR_GetError();
1289 Debug( LDAP_DEBUG_ANY,
1290 "TLS: could not add the certificate %s - error %d:%s.\n",
1291 ctx->tc_certname, errcode,
1292 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1296 tlsm_add_pem_obj( ctx, rv );
1302 tlsm_add_key_from_file( tlsm_ctx *ctx, const char *filename )
1305 PK11SlotInfo * slot = NULL;
1306 PK11GenericObject *rv;
1307 CK_ATTRIBUTE *attrs;
1308 CK_ATTRIBUTE theTemplate[20];
1309 CK_BBOOL cktrue = CK_TRUE;
1310 CK_OBJECT_CLASS objClass = CKO_PRIVATE_KEY;
1315 memset( &fi, 0, sizeof(fi) );
1316 status = PR_GetFileInfo( filename, &fi );
1317 if ( PR_SUCCESS != status) {
1318 PRErrorCode errcode = PR_GetError();
1319 Debug( LDAP_DEBUG_ANY,
1320 "TLS: could not read key file %s - error %d:%s.\n",
1322 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1326 if ( fi.type != PR_FILE_FILE ) {
1327 PR_SetError(PR_IS_DIRECTORY_ERROR, 0);
1328 Debug( LDAP_DEBUG_ANY,
1329 "TLS: error: the key file %s is not a file.\n",
1334 attrs = theTemplate;
1336 if ( ctx->tc_slotname == NULL ) { /* need new slot */
1337 slotID = ++tlsm_slot_count;
1338 ctx->tc_slotname = PR_smprintf( TLSM_PEM_TOKEN_FMT, slotID );
1340 slot = PK11_FindSlotByName( ctx->tc_slotname );
1343 PRErrorCode errcode = PR_GetError();
1344 Debug( LDAP_DEBUG_ANY,
1345 "TLS: could not find the slot %s for the private key - error %d:%s.\n",
1346 ctx->tc_slotname, errcode,
1347 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1351 PK11_SETATTRS( attrs, CKA_CLASS, &objClass, sizeof(objClass) ); attrs++;
1352 PK11_SETATTRS( attrs, CKA_TOKEN, &cktrue, sizeof(CK_BBOOL) ); attrs++;
1353 PK11_SETATTRS( attrs, CKA_LABEL, (unsigned char *)filename, strlen(filename)+1 ); attrs++;
1354 rv = PK11_CreateGenericObject( slot, theTemplate, 3, PR_FALSE /* isPerm */ );
1357 PRErrorCode errcode = PR_GetError();
1358 Debug( LDAP_DEBUG_ANY,
1359 "TLS: could not add the certificate %s - error %d:%s.\n",
1360 ctx->tc_certname, errcode,
1361 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1364 /* When adding an encrypted key the PKCS#11 will be set as removed */
1365 /* This will force the token to be seen as re-inserted */
1366 SECMOD_WaitForAnyTokenEvent( pem_module, 0, 0 );
1367 PK11_IsPresent( slot );
1371 PK11_FreeSlot( slot );
1374 tlsm_add_pem_obj( ctx, rv );
1380 tlsm_init_ca_certs( tlsm_ctx *ctx, const char *cacertfile, const char *cacertdir )
1382 PRBool isca = PR_TRUE;
1383 PRStatus status = PR_SUCCESS;
1384 PRErrorCode errcode = PR_SUCCESS;
1386 if ( !cacertfile && !cacertdir ) {
1387 /* no checking - not good, but allowed */
1392 int rc = tlsm_add_cert_from_file( ctx, cacertfile, isca, PR_TRUE );
1394 errcode = PR_GetError();
1395 Debug( LDAP_DEBUG_ANY,
1396 "TLS: %s is not a valid CA certificate file - error %d:%s.\n",
1397 cacertfile, errcode,
1398 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1399 /* failure with cacertfile is a hard failure even if cacertdir is
1400 also specified and contains valid CA cert files */
1401 status = PR_FAILURE;
1403 Debug( LDAP_DEBUG_TRACE,
1404 "TLS: loaded CA certificate file %s.\n",
1409 /* if cacertfile above failed, we will return failure, even
1410 if there is a valid CA cert in cacertdir - but we still
1411 process cacertdir in case the user has enabled trace level
1412 debugging so they can see the processing for cacertdir too */
1413 /* any cacertdir failures are "soft" failures - if the user specifies
1414 no cert checking, then we allow the tls/ssl to continue, no matter
1415 what was specified for cacertdir, or the contents of the directory
1416 - this is different behavior than that of cacertfile */
1421 PRStatus fistatus = PR_FAILURE;
1423 memset( &fi, 0, sizeof(fi) );
1424 fistatus = PR_GetFileInfo( cacertdir, &fi );
1425 if ( PR_SUCCESS != fistatus) {
1426 errcode = PR_GetError();
1427 Debug( LDAP_DEBUG_ANY,
1428 "TLS: could not get info about the CA certificate directory %s - error %d:%s.\n",
1430 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1434 if ( fi.type != PR_FILE_DIRECTORY ) {
1435 Debug( LDAP_DEBUG_ANY,
1436 "TLS: error: the CA certificate directory %s is not a directory.\n",
1441 dir = PR_OpenDir( cacertdir );
1442 if ( NULL == dir ) {
1443 errcode = PR_GetError();
1444 Debug( LDAP_DEBUG_ANY,
1445 "TLS: could not open the CA certificate directory %s - error %d:%s.\n",
1447 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1452 entry = PR_ReadDir( dir, PR_SKIP_BOTH | PR_SKIP_HIDDEN );
1453 if ( ( NULL != entry ) && ( NULL != entry->name ) ) {
1454 char *fullpath = NULL;
1457 ptr = PL_strrstr( entry->name, PEM_CA_HASH_FILE_SUFFIX );
1458 if ( ( ptr == NULL ) || ( *(ptr + PEM_CA_HASH_FILE_SUFFIX_LEN) != '\0' ) ) {
1459 Debug( LDAP_DEBUG_TRACE,
1460 "TLS: file %s does not end in [%s] - does not appear to be a CA certificate "
1461 "directory file with a properly hashed file name - skipping.\n",
1462 entry->name, PEM_CA_HASH_FILE_SUFFIX, 0 );
1465 fullpath = PR_smprintf( "%s/%s", cacertdir, entry->name );
1466 if ( !tlsm_add_cert_from_file( ctx, fullpath, isca, PR_TRUE ) ) {
1467 Debug( LDAP_DEBUG_TRACE,
1468 "TLS: loaded CA certificate file %s from CA certificate directory %s.\n",
1469 fullpath, cacertdir, 0 );
1471 errcode = PR_GetError();
1472 Debug( LDAP_DEBUG_TRACE,
1473 "TLS: %s is not a valid CA certificate file - error %d:%s.\n",
1475 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1477 PR_smprintf_free( fullpath );
1479 } while ( NULL != entry );
1483 if ( status != PR_SUCCESS ) {
1491 * NSS supports having multiple cert/key databases in the same
1492 * directory, each one having a unique string prefix e.g.
1493 * slapd-01-cert8.db - the prefix here is "slapd-01-"
1494 * this function examines the given certdir - if it looks like
1495 * /path/to/directory/prefix it will return the
1496 * /path/to/directory part in realcertdir, and the prefix in prefix
1499 tlsm_get_certdb_prefix( const char *certdir, char **realcertdir, char **prefix )
1501 char sep = PR_GetDirectorySeparator();
1503 struct PRFileInfo prfi;
1506 *realcertdir = (char *)certdir; /* default is the one passed in */
1508 /* if certdir is not given, just return */
1513 prc = PR_GetFileInfo( certdir, &prfi );
1514 /* if certdir exists (file or directory) then it cannot specify a prefix */
1515 if ( prc == PR_SUCCESS ) {
1519 /* if certdir was given, and there is a '/' in certdir, see if there
1520 is anything after the last '/' - if so, assume it is the prefix */
1521 if ( ( ( ptr = strrchr( certdir, sep ) ) ) && *(ptr+1) ) {
1522 *realcertdir = PL_strndup( certdir, ptr-certdir );
1523 *prefix = PL_strdup( ptr+1 );
1530 * This is the part of the init we defer until we get the
1531 * actual security configuration information. This is
1532 * only called once, protected by a PRCallOnce
1533 * NOTE: This must be done before the first call to SSL_ImportFD,
1534 * especially the setting of the policy
1535 * NOTE: This must be called after fork()
1538 tlsm_deferred_init( void *arg )
1540 tlsm_ctx *ctx = (tlsm_ctx *)arg;
1541 struct ldaptls *lt = ctx->tc_config;
1542 const char *securitydirs[3];
1545 PRErrorCode errcode = 1;
1546 #ifdef HAVE_NSS_INITCONTEXT
1547 NSSInitParameters initParams;
1548 NSSInitContext *initctx = NULL;
1553 #ifdef HAVE_SECMOD_RESTARTMODULES
1554 /* NSS enforces the pkcs11 requirement that modules should be unloaded after
1555 a fork() - since there is no portable way to determine if NSS has been
1556 already initialized in a parent process, we just call SECMOD_RestartModules
1557 with force == FALSE - if the module has been unloaded due to a fork, it will
1558 be reloaded, otherwise, it is a no-op */
1559 if ( SECFailure == ( rc = SECMOD_RestartModules(PR_FALSE /* do not force */) ) ) {
1560 errcode = PORT_GetError();
1561 if ( errcode != SEC_ERROR_NOT_INITIALIZED ) {
1562 Debug( LDAP_DEBUG_TRACE,
1563 "TLS: could not restart the security modules: %d:%s\n",
1564 errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ), 0 );
1571 #ifdef HAVE_NSS_INITCONTEXT
1572 memset( &initParams, 0, sizeof( initParams ) );
1573 initParams.length = sizeof( initParams );
1574 #endif /* HAVE_NSS_INITCONTEXT */
1576 #ifdef LDAP_R_COMPILE
1577 if ( PR_CallOnce( &tlsm_init_mutex_callonce, tlsm_thr_init_callonce ) ) {
1580 #endif /* LDAP_R_COMPILE */
1582 #ifndef HAVE_NSS_INITCONTEXT
1583 if ( !NSS_IsInitialized() ) {
1584 #endif /* HAVE_NSS_INITCONTEXT */
1586 MOZNSS_DIR will override everything else - you can
1587 always set MOZNSS_DIR to force the use of this
1589 If using MOZNSS, specify the location of the moznss db dir
1590 in the cacertdir directive of the OpenLDAP configuration.
1591 DEFAULT_MOZNSS_DIR will only be used if the code cannot
1592 find a security dir to use based on the current
1596 securitydirs[nn++] = PR_GetEnv( "MOZNSS_DIR" );
1597 securitydirs[nn++] = lt->lt_cacertdir;
1598 securitydirs[nn++] = PR_GetEnv( "DEFAULT_MOZNSS_DIR" );
1599 for ( ii = 0; !done && ( ii < nn ); ++ii ) {
1600 char *realcertdir = NULL;
1601 const char *defprefix = "";
1602 char *prefix = (char *)defprefix;
1603 const char *securitydir = securitydirs[ii];
1604 if ( NULL == securitydir ) {
1608 tlsm_get_certdb_prefix( securitydir, &realcertdir, &prefix );
1609 LDAP_MUTEX_LOCK( &tlsm_init_mutex );
1611 #ifdef HAVE_NSS_INITCONTEXT
1612 #ifdef INITCONTEXT_HACK
1613 if ( !NSS_IsInitialized() && ctx->tc_is_server ) {
1614 rc = NSS_Initialize( realcertdir, prefix, prefix, SECMOD_DB, NSS_INIT_READONLY );
1616 initctx = NSS_InitContext( realcertdir, prefix, prefix, SECMOD_DB,
1617 &initParams, NSS_INIT_READONLY );
1618 rc = (initctx == NULL) ? SECFailure : SECSuccess;
1621 initctx = NSS_InitContext( realcertdir, prefix, prefix, SECMOD_DB,
1622 &initParams, NSS_INIT_READONLY );
1623 rc = (initctx == NULL) ? SECFailure : SECSuccess;
1626 rc = NSS_Initialize( realcertdir, prefix, prefix, SECMOD_DB, NSS_INIT_READONLY );
1629 LDAP_MUTEX_UNLOCK( &tlsm_init_mutex );
1631 if ( rc != SECSuccess ) {
1632 errcode = PORT_GetError();
1633 if ( securitydirs[ii] != lt->lt_cacertdir) {
1634 Debug( LDAP_DEBUG_TRACE,
1635 "TLS: could not initialize moznss using security dir %s prefix %s - error %d.\n",
1636 realcertdir, prefix, errcode );
1640 Debug( LDAP_DEBUG_TRACE, "TLS: using moznss security dir %s prefix %s.\n",
1641 realcertdir, prefix, 0 );
1645 if ( realcertdir != securitydir ) {
1646 PL_strfree( realcertdir );
1648 if ( prefix != defprefix ) {
1649 PL_strfree( prefix );
1653 if ( errcode ) { /* no moznss db found, or not using moznss db */
1654 LDAP_MUTEX_LOCK( &tlsm_init_mutex );
1655 #ifdef HAVE_NSS_INITCONTEXT
1656 int flags = NSS_INIT_READONLY|NSS_INIT_NOCERTDB|NSS_INIT_NOMODDB;
1657 #ifdef INITCONTEXT_HACK
1658 if ( !NSS_IsInitialized() && ctx->tc_is_server ) {
1659 rc = NSS_NoDB_Init( NULL );
1661 initctx = NSS_InitContext( "", "", "", SECMOD_DB,
1662 &initParams, flags );
1663 rc = (initctx == NULL) ? SECFailure : SECSuccess;
1666 initctx = NSS_InitContext( "", "", "", SECMOD_DB,
1667 &initParams, flags );
1668 rc = (initctx == NULL) ? SECFailure : SECSuccess;
1671 rc = NSS_NoDB_Init( NULL );
1673 LDAP_MUTEX_UNLOCK( &tlsm_init_mutex );
1674 if ( rc != SECSuccess ) {
1675 errcode = PORT_GetError();
1676 Debug( LDAP_DEBUG_ANY,
1677 "TLS: could not initialize moznss - error %d:%s.\n",
1678 errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ), 0 );
1682 #ifdef HAVE_NSS_INITCONTEXT
1683 ctx->tc_initctx = initctx;
1686 /* initialize the PEM module */
1687 LDAP_MUTEX_LOCK( &tlsm_init_mutex );
1688 if ( tlsm_init_pem_module() ) {
1689 LDAP_MUTEX_UNLOCK( &tlsm_init_mutex );
1690 errcode = PORT_GetError();
1691 Debug( LDAP_DEBUG_ANY,
1692 "TLS: could not initialize moznss PEM module - error %d:%s.\n",
1693 errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ), 0 );
1696 LDAP_MUTEX_UNLOCK( &tlsm_init_mutex );
1698 if ( tlsm_init_ca_certs( ctx, lt->lt_cacertfile, lt->lt_cacertdir ) ) {
1699 /* if we tried to use lt->lt_cacertdir as an NSS key/cert db, errcode
1700 will be a value other than 1 - print an error message so that the
1701 user will know that failed too */
1702 if ( ( errcode != 1 ) && ( lt->lt_cacertdir ) ) {
1703 char *realcertdir = NULL;
1704 char *prefix = NULL;
1705 tlsm_get_certdb_prefix( lt->lt_cacertdir, &realcertdir, &prefix );
1706 Debug( LDAP_DEBUG_TRACE,
1707 "TLS: could not initialize moznss using security dir %s prefix %s - error %d.\n",
1708 realcertdir, prefix ? prefix : "", errcode );
1709 if ( realcertdir != lt->lt_cacertdir ) {
1710 PL_strfree( realcertdir );
1712 PL_strfree( prefix );
1717 ctx->tc_using_pem = PR_TRUE;
1720 #ifdef HAVE_NSS_INITCONTEXT
1721 if ( !ctx->tc_initctx ) {
1722 ctx->tc_initctx = initctx;
1726 NSS_SetDomesticPolicy();
1728 PK11_SetPasswordFunc( tlsm_pin_prompt );
1730 /* register cleanup function */
1731 if ( tlsm_register_nss_shutdown() ) {
1732 errcode = PORT_GetError();
1733 Debug( LDAP_DEBUG_ANY,
1734 "TLS: could not register NSS shutdown function: %d:%s\n",
1735 errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ), 0 );
1739 if ( ctx->tc_is_server ) {
1740 LDAP_MUTEX_LOCK( &tlsm_init_mutex );
1741 /* 0 means use the defaults here */
1742 SSL_ConfigServerSessionIDCache( 0, 0, 0, NULL );
1743 LDAP_MUTEX_UNLOCK( &tlsm_init_mutex );
1746 #ifndef HAVE_NSS_INITCONTEXT
1748 #endif /* HAVE_NSS_INITCONTEXT */
1754 tlsm_authenticate( tlsm_ctx *ctx, const char *certname, const char *pininfo )
1756 const char *colon = NULL;
1757 char *token_name = NULL;
1758 PK11SlotInfo *slot = NULL;
1761 if ( !certname || !*certname ) {
1765 if ( ( colon = PL_strchr( certname, ':' ) ) ) {
1766 token_name = PL_strndup( certname, colon-certname );
1770 slot = PK11_FindSlotByName( token_name );
1772 slot = PK11_GetInternalKeySlot();
1776 PRErrorCode errcode = PR_GetError();
1777 Debug( LDAP_DEBUG_ANY,
1778 "TLS: could not find the slot for security token %s - error %d:%s.\n",
1779 token_name ? token_name : DEFAULT_TOKEN_NAME, errcode,
1780 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1784 rc = tlsm_authenticate_to_slot( ctx, slot );
1787 PL_strfree( token_name );
1789 PK11_FreeSlot( slot );
1796 * Find and verify the certificate.
1797 * Either fd is given, in which case the cert will be taken from it via SSL_PeerCertificate
1798 * or certname is given, and it will be searched for by name
1801 tlsm_find_and_verify_cert_key(tlsm_ctx *ctx, PRFileDesc *fd, const char *certname, int isServer, CERTCertificate **pRetCert, SECKEYPrivateKey **pRetKey)
1803 CERTCertificate *cert = NULL;
1805 void *pin_arg = NULL;
1806 SECKEYPrivateKey *key = NULL;
1808 pin_arg = SSL_RevealPinArg( fd );
1810 cert = PK11_FindCertFromNickname( certname, pin_arg );
1812 PRErrorCode errcode = PR_GetError();
1813 Debug( LDAP_DEBUG_ANY,
1814 "TLS: error: the certificate %s could not be found in the database - error %d:%s\n",
1815 certname, errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1819 /* we are verifying the peer cert
1820 we also need to swap the isServer meaning */
1821 cert = SSL_PeerCertificate( fd );
1823 PRErrorCode errcode = PR_GetError();
1824 Debug( LDAP_DEBUG_ANY,
1825 "TLS: error: could not get the certificate from the peer connection - error %d:%s\n",
1826 errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ), NULL );
1829 isServer = !isServer; /* verify the peer's cert instead */
1832 if ( ctx->tc_slotname ) {
1833 PK11SlotInfo *slot = PK11_FindSlotByName( ctx->tc_slotname );
1834 key = PK11_FindPrivateKeyFromCert( slot, cert, NULL );
1835 PK11_FreeSlot( slot );
1837 key = PK11_FindKeyByAnyCert( cert, pin_arg );
1841 SECCertificateUsage certUsage;
1842 PRBool checkSig = PR_TRUE;
1844 /* may not have a CA cert - ok - ignore SEC_ERROR_UNKNOWN_ISSUER */
1845 int errorToIgnore = SEC_ERROR_UNKNOWN_ISSUER;
1848 *pRetKey = key; /* caller will deal with this */
1850 SECKEY_DestroyPrivateKey( key );
1853 certUsage = certificateUsageSSLServer;
1855 certUsage = certificateUsageSSLClient;
1857 if ( ctx->tc_verify_cert ) {
1860 checkSig = PR_FALSE;
1862 if ( ctx->tc_warn_only ) {
1865 status = tlsm_verify_cert( ctx->tc_certdb, cert, pin_arg,
1866 checkSig, certUsage, errorToIgnore );
1867 if ( status == SECSuccess ) {
1871 PRErrorCode errcode = PR_GetError();
1872 Debug( LDAP_DEBUG_ANY,
1873 "TLS: error: could not find the private key for certificate %s - error %d:%s\n",
1874 certname, errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1878 *pRetCert = cert; /* caller will deal with this */
1880 CERT_DestroyCertificate( cert );
1887 tlsm_get_client_auth_data( void *arg, PRFileDesc *fd,
1888 CERTDistNames *caNames, CERTCertificate **pRetCert,
1889 SECKEYPrivateKey **pRetKey )
1891 tlsm_ctx *ctx = (tlsm_ctx *)arg;
1895 /* don't need caNames - this function will call CERT_VerifyCertificateNow
1896 which will verify the cert against the known CAs */
1897 saveval = ctx->tc_warn_only;
1898 ctx->tc_warn_only = PR_TRUE;
1899 rc = tlsm_find_and_verify_cert_key( ctx, fd, ctx->tc_certname, 0, pRetCert, pRetKey );
1900 ctx->tc_warn_only = saveval;
1902 Debug( LDAP_DEBUG_ANY,
1903 "TLS: error: unable to perform client certificate authentication for "
1904 "certificate named %s\n", ctx->tc_certname, 0, 0 );
1905 if ( pRetKey && *pRetKey ) {
1906 SECKEY_DestroyPrivateKey( *pRetKey );
1909 if ( pRetCert && *pRetCert ) {
1910 CERT_DestroyCertificate( *pRetCert );
1920 * ctx must have a tc_model that is valid
1921 * certname is in the form [<tokenname>:]<certnickname>
1922 * where <tokenname> is the name of the PKCS11 token
1923 * and <certnickname> is the nickname of the cert/key in
1927 tlsm_clientauth_init( tlsm_ctx *ctx )
1929 SECStatus status = SECFailure;
1933 saveval = ctx->tc_warn_only;
1934 ctx->tc_warn_only = PR_TRUE;
1935 rc = tlsm_find_and_verify_cert_key( ctx, ctx->tc_model, ctx->tc_certname, 0, NULL, NULL );
1936 ctx->tc_warn_only = saveval;
1938 Debug( LDAP_DEBUG_ANY,
1939 "TLS: error: unable to set up client certificate authentication for "
1940 "certificate named %s\n", ctx->tc_certname, 0, 0 );
1944 status = SSL_GetClientAuthDataHook( ctx->tc_model,
1945 tlsm_get_client_auth_data,
1948 return ( status == SECSuccess ? 0 : -1 );
1952 * Tear down the TLS subsystem. Should only be called once.
1955 tlsm_destroy( void )
1957 #ifdef LDAP_R_COMPILE
1958 ldap_pvt_thread_mutex_destroy( &tlsm_init_mutex );
1959 ldap_pvt_thread_mutex_destroy( &tlsm_pem_mutex );
1963 static struct ldaptls *
1964 tlsm_copy_config ( const struct ldaptls *config )
1966 struct ldaptls *copy;
1970 copy = LDAP_MALLOC(sizeof(*copy));
1974 memset(copy, 0, sizeof(*copy));
1976 if (config->lt_certfile)
1977 copy->lt_certfile = LDAP_STRDUP(config->lt_certfile);
1978 if (config->lt_keyfile)
1979 copy->lt_keyfile = LDAP_STRDUP(config->lt_keyfile);
1980 if (config->lt_dhfile)
1981 copy->lt_dhfile = LDAP_STRDUP(config->lt_dhfile);
1982 if (config->lt_cacertfile)
1983 copy->lt_cacertfile = LDAP_STRDUP(config->lt_cacertfile);
1984 if (config->lt_cacertdir)
1985 copy->lt_cacertdir = LDAP_STRDUP(config->lt_cacertdir);
1986 if (config->lt_ciphersuite)
1987 copy->lt_ciphersuite = LDAP_STRDUP(config->lt_ciphersuite);
1988 if (config->lt_crlfile)
1989 copy->lt_crlfile = LDAP_STRDUP(config->lt_crlfile);
1990 if (config->lt_randfile)
1991 copy->lt_randfile = LDAP_STRDUP(config->lt_randfile);
1993 copy->lt_protocol_min = config->lt_protocol_min;
1999 tlsm_free_config ( struct ldaptls *config )
2003 if (config->lt_certfile)
2004 LDAP_FREE(config->lt_certfile);
2005 if (config->lt_keyfile)
2006 LDAP_FREE(config->lt_keyfile);
2007 if (config->lt_dhfile)
2008 LDAP_FREE(config->lt_dhfile);
2009 if (config->lt_cacertfile)
2010 LDAP_FREE(config->lt_cacertfile);
2011 if (config->lt_cacertdir)
2012 LDAP_FREE(config->lt_cacertdir);
2013 if (config->lt_ciphersuite)
2014 LDAP_FREE(config->lt_ciphersuite);
2015 if (config->lt_crlfile)
2016 LDAP_FREE(config->lt_crlfile);
2017 if (config->lt_randfile)
2018 LDAP_FREE(config->lt_randfile);
2024 tlsm_ctx_new ( struct ldapoptions *lo )
2028 ctx = LDAP_MALLOC( sizeof (*ctx) );
2031 #ifdef LDAP_R_COMPILE
2032 ldap_pvt_thread_mutex_init( &ctx->tc_refmutex );
2034 ctx->tc_config = NULL; /* populated later by tlsm_ctx_init */
2035 ctx->tc_certdb = NULL;
2036 ctx->tc_certname = NULL;
2037 ctx->tc_pin_file = NULL;
2038 ctx->tc_model = NULL;
2039 memset(&ctx->tc_callonce, 0, sizeof(ctx->tc_callonce));
2040 ctx->tc_require_cert = lo->ldo_tls_require_cert;
2041 ctx->tc_verify_cert = PR_FALSE;
2042 ctx->tc_using_pem = PR_FALSE;
2043 ctx->tc_slotname = NULL;
2044 #ifdef HAVE_NSS_INITCONTEXT
2045 ctx->tc_initctx = NULL;
2046 #endif /* HAVE_NSS_INITCONTEXT */
2047 ctx->tc_pem_objs = NULL;
2048 ctx->tc_n_pem_objs = 0;
2049 ctx->tc_warn_only = PR_FALSE;
2051 return (tls_ctx *)ctx;
2055 tlsm_ctx_ref( tls_ctx *ctx )
2057 tlsm_ctx *c = (tlsm_ctx *)ctx;
2058 LDAP_MUTEX_LOCK( &c->tc_refmutex );
2060 LDAP_MUTEX_UNLOCK( &c->tc_refmutex );
2064 tlsm_ctx_free ( tls_ctx *ctx )
2066 tlsm_ctx *c = (tlsm_ctx *)ctx;
2071 LDAP_MUTEX_LOCK( &c->tc_refmutex );
2072 refcount = --c->tc_refcnt;
2073 LDAP_MUTEX_UNLOCK( &c->tc_refmutex );
2077 PR_Close( c->tc_model );
2078 c->tc_certdb = NULL; /* if not the default, may have to clean up */
2079 PL_strfree( c->tc_certname );
2080 c->tc_certname = NULL;
2081 PL_strfree( c->tc_pin_file );
2082 c->tc_pin_file = NULL;
2083 PL_strfree( c->tc_slotname );
2084 tlsm_free_pem_objs( c );
2085 #ifdef HAVE_NSS_INITCONTEXT
2086 if ( c->tc_initctx ) {
2087 LDAP_MUTEX_LOCK( &tlsm_init_mutex );
2088 if ( NSS_ShutdownContext( c->tc_initctx ) ) {
2089 PRErrorCode errcode = PR_GetError();
2090 Debug( LDAP_DEBUG_ANY,
2091 "TLS: could not shutdown NSS - error %d:%s.\n",
2092 errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ), 0 );
2094 LDAP_MUTEX_UNLOCK( &tlsm_init_mutex );
2096 c->tc_initctx = NULL;
2097 #endif /* HAVE_NSS_INITCONTEXT */
2098 #ifdef LDAP_R_COMPILE
2099 ldap_pvt_thread_mutex_destroy( &c->tc_refmutex );
2103 tlsm_free_config( c->tc_config );
2109 * initialize a new TLS context
2112 tlsm_ctx_init( struct ldapoptions *lo, struct ldaptls *lt, int is_server )
2114 tlsm_ctx *ctx = (tlsm_ctx *)lo->ldo_tls_ctx;
2115 ctx->tc_config = tlsm_copy_config(lt);
2116 ctx->tc_is_server = is_server;
2122 tlsm_deferred_ctx_init( void *arg )
2124 tlsm_ctx *ctx = (tlsm_ctx *)arg;
2125 PRBool sslv2 = PR_FALSE;
2126 PRBool sslv3 = PR_TRUE;
2127 PRBool tlsv1 = PR_TRUE;
2128 PRBool request_cert = PR_FALSE;
2129 PRInt32 require_cert = PR_FALSE;
2133 if ( tlsm_deferred_init( ctx ) ) {
2134 Debug( LDAP_DEBUG_ANY,
2135 "TLS: could not perform TLS system initialization.\n",
2140 ctx->tc_certdb = CERT_GetDefaultCertDB(); /* If there is ever a per-context db, change this */
2142 fd = PR_CreateIOLayerStub( tlsm_layer_id, &tlsm_PR_methods );
2144 ctx->tc_model = SSL_ImportFD( NULL, fd );
2147 if ( !ctx->tc_model ) {
2148 PRErrorCode err = PR_GetError();
2149 Debug( LDAP_DEBUG_ANY,
2150 "TLS: could perform TLS socket I/O layer initialization - error %d:%s.\n",
2151 err, PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ), NULL );
2159 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_SECURITY, PR_TRUE ) ) {
2160 Debug( LDAP_DEBUG_ANY,
2161 "TLS: could not set secure mode on.\n",
2166 lt = ctx->tc_config;
2168 /* default is sslv3 and tlsv1 */
2169 if ( lt->lt_protocol_min ) {
2170 if ( lt->lt_protocol_min > LDAP_OPT_X_TLS_PROTOCOL_SSL3 ) {
2172 } else if ( lt->lt_protocol_min <= LDAP_OPT_X_TLS_PROTOCOL_SSL2 ) {
2174 Debug( LDAP_DEBUG_ANY,
2175 "TLS: warning: minimum TLS protocol level set to "
2176 "include SSLv2 - SSLv2 is insecure - do not use\n", 0, 0, 0 );
2179 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_ENABLE_SSL2, sslv2 ) ) {
2180 Debug( LDAP_DEBUG_ANY,
2181 "TLS: could not set SSLv2 mode on.\n",
2185 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_ENABLE_SSL3, sslv3 ) ) {
2186 Debug( LDAP_DEBUG_ANY,
2187 "TLS: could not set SSLv3 mode on.\n",
2191 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_ENABLE_TLS, tlsv1 ) ) {
2192 Debug( LDAP_DEBUG_ANY,
2193 "TLS: could not set TLSv1 mode on.\n",
2198 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_HANDSHAKE_AS_CLIENT, !ctx->tc_is_server ) ) {
2199 Debug( LDAP_DEBUG_ANY,
2200 "TLS: could not set handshake as client.\n",
2204 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_HANDSHAKE_AS_SERVER, ctx->tc_is_server ) ) {
2205 Debug( LDAP_DEBUG_ANY,
2206 "TLS: could not set handshake as server.\n",
2211 if ( lt->lt_ciphersuite &&
2212 tlsm_parse_ciphers( ctx, lt->lt_ciphersuite )) {
2213 Debug( LDAP_DEBUG_ANY,
2214 "TLS: could not set cipher list %s.\n",
2215 lt->lt_ciphersuite, 0, 0 );
2217 } else if ( tlsm_parse_ciphers( ctx, "DEFAULT" ) ) {
2218 Debug( LDAP_DEBUG_ANY,
2219 "TLS: could not set cipher list DEFAULT.\n",
2224 if ( !ctx->tc_require_cert ) {
2225 ctx->tc_verify_cert = PR_FALSE;
2226 } else if ( !ctx->tc_is_server ) {
2227 request_cert = PR_TRUE;
2228 require_cert = SSL_REQUIRE_NO_ERROR;
2229 if ( ctx->tc_require_cert == LDAP_OPT_X_TLS_DEMAND ||
2230 ctx->tc_require_cert == LDAP_OPT_X_TLS_HARD ) {
2231 require_cert = SSL_REQUIRE_ALWAYS;
2233 if ( ctx->tc_require_cert != LDAP_OPT_X_TLS_ALLOW )
2234 ctx->tc_verify_cert = PR_TRUE;
2235 } else { /* server */
2236 /* server does not request certs by default */
2237 /* if allow - client may send cert, server will ignore if errors */
2238 /* if try - client may send cert, server will error if bad cert */
2239 /* if hard or demand - client must send cert, server will error if bad cert */
2240 request_cert = PR_TRUE;
2241 require_cert = SSL_REQUIRE_NO_ERROR;
2242 if ( ctx->tc_require_cert == LDAP_OPT_X_TLS_DEMAND ||
2243 ctx->tc_require_cert == LDAP_OPT_X_TLS_HARD ) {
2244 require_cert = SSL_REQUIRE_ALWAYS;
2246 if ( ctx->tc_require_cert != LDAP_OPT_X_TLS_ALLOW ) {
2247 ctx->tc_verify_cert = PR_TRUE;
2249 ctx->tc_warn_only = PR_TRUE;
2253 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_REQUEST_CERTIFICATE, request_cert ) ) {
2254 Debug( LDAP_DEBUG_ANY,
2255 "TLS: could not set request certificate mode.\n",
2260 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_REQUIRE_CERTIFICATE, require_cert ) ) {
2261 Debug( LDAP_DEBUG_ANY,
2262 "TLS: could not set require certificate mode.\n",
2267 /* set up our cert and key, if any */
2268 if ( lt->lt_certfile ) {
2269 /* if using the PEM module, load the PEM file specified by lt_certfile */
2270 /* otherwise, assume this is the name of a cert already in the db */
2271 if ( ctx->tc_using_pem ) {
2272 /* this sets ctx->tc_certname to the correct value */
2273 int rc = tlsm_add_cert_from_file( ctx, lt->lt_certfile, PR_FALSE, PR_TRUE );
2278 PL_strfree( ctx->tc_certname );
2279 ctx->tc_certname = PL_strdup( lt->lt_certfile );
2283 if ( lt->lt_keyfile ) {
2284 /* if using the PEM module, load the PEM file specified by lt_keyfile */
2285 /* otherwise, assume this is the pininfo for the key */
2286 if ( ctx->tc_using_pem ) {
2287 /* this sets ctx->tc_certname to the correct value */
2288 int rc = tlsm_add_key_from_file( ctx, lt->lt_keyfile );
2293 PL_strfree( ctx->tc_pin_file );
2294 ctx->tc_pin_file = PL_strdup( lt->lt_keyfile );
2298 /* Set up callbacks for use by clients */
2299 if ( !ctx->tc_is_server ) {
2300 if ( SSL_OptionSet( ctx->tc_model, SSL_NO_CACHE, PR_TRUE ) != SECSuccess ) {
2301 PRErrorCode err = PR_GetError();
2302 Debug( LDAP_DEBUG_ANY,
2303 "TLS: error: could not set nocache option for moznss - error %d:%s\n",
2304 err, PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ), NULL );
2308 if ( SSL_BadCertHook( ctx->tc_model, tlsm_bad_cert_handler, ctx ) != SECSuccess ) {
2309 PRErrorCode err = PR_GetError();
2310 Debug( LDAP_DEBUG_ANY,
2311 "TLS: error: could not set bad cert handler for moznss - error %d:%s\n",
2312 err, PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ), NULL );
2317 since a cert has been specified, assume the client wants to do cert auth
2319 if ( ctx->tc_certname ) {
2320 if ( tlsm_authenticate( ctx, ctx->tc_certname, ctx->tc_pin_file ) ) {
2321 Debug( LDAP_DEBUG_ANY,
2322 "TLS: error: unable to authenticate to the security device for certificate %s\n",
2323 ctx->tc_certname, 0, 0 );
2326 if ( tlsm_clientauth_init( ctx ) ) {
2327 Debug( LDAP_DEBUG_ANY,
2328 "TLS: error: unable to set up client certificate authentication using %s\n",
2329 ctx->tc_certname, 0, 0 );
2333 } else { /* set up secure server */
2335 CERTCertificate *serverCert = NULL;
2336 SECKEYPrivateKey *serverKey = NULL;
2339 /* must have a certificate for the server to use */
2340 if ( !ctx->tc_certname ) {
2341 Debug( LDAP_DEBUG_ANY,
2342 "TLS: error: no server certificate: must specify a certificate for the server to use\n",
2347 /* authenticate to the server's token - this will do nothing
2348 if the key/cert db is not password protected */
2349 if ( tlsm_authenticate( ctx, ctx->tc_certname, ctx->tc_pin_file ) ) {
2350 Debug( LDAP_DEBUG_ANY,
2351 "TLS: error: unable to authenticate to the security device for certificate %s\n",
2352 ctx->tc_certname, 0, 0 );
2356 /* get the server's key and cert */
2357 if ( tlsm_find_and_verify_cert_key( ctx, ctx->tc_model, ctx->tc_certname, ctx->tc_is_server,
2358 &serverCert, &serverKey ) ) {
2359 Debug( LDAP_DEBUG_ANY,
2360 "TLS: error: unable to find and verify server's cert and key for certificate %s\n",
2361 ctx->tc_certname, 0, 0 );
2362 CERT_DestroyCertificate( serverCert );
2363 SECKEY_DestroyPrivateKey( serverKey );
2367 certKEA = NSS_FindCertKEAType( serverCert );
2368 /* configure the socket to be a secure server socket */
2369 status = SSL_ConfigSecureServer( ctx->tc_model, serverCert, serverKey, certKEA );
2370 /* SSL_ConfigSecureServer copies these */
2371 CERT_DestroyCertificate( serverCert );
2372 SECKEY_DestroyPrivateKey( serverKey );
2374 if ( SECSuccess != status ) {
2375 PRErrorCode err = PR_GetError();
2376 Debug( LDAP_DEBUG_ANY,
2377 "TLS: error: unable to configure secure server using certificate %s - error %d:%s\n",
2378 ctx->tc_certname, err, PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ) );
2383 /* Callback for authenticating certificate */
2384 if ( SSL_AuthCertificateHook( ctx->tc_model, tlsm_auth_cert_handler,
2385 ctx ) != SECSuccess ) {
2386 PRErrorCode err = PR_GetError();
2387 Debug( LDAP_DEBUG_ANY,
2388 "TLS: error: could not set auth cert handler for moznss - error %d:%s\n",
2389 err, PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ), NULL );
2393 if ( SSL_HandshakeCallback( ctx->tc_model, tlsm_handshake_complete_cb, ctx ) ) {
2394 PRErrorCode err = PR_GetError();
2395 Debug( LDAP_DEBUG_ANY,
2396 "TLS: error: could not set handshake callback for moznss - error %d:%s\n",
2397 err, PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ), NULL );
2401 tlsm_free_config( ctx->tc_config );
2402 ctx->tc_config = NULL;
2408 tlsm_session *session;
2409 Sockbuf_IO_Desc *sbiod;
2410 /* there seems to be no portable way to determine if the
2411 sockbuf sd has been set to nonblocking mode - the
2412 call to ber_pvt_socket_set_nonblock() takes place
2413 before the tls socket is set up, so we cannot
2414 intercept that call either.
2415 On systems where fcntl is available, we can just
2416 F_GETFL and test for O_NONBLOCK. On other systems,
2417 we will just see if the IO op returns EAGAIN or EWOULDBLOCK,
2418 and just set this flag */
2421 * NSS tries hard to be backwards compatible with SSLv2 clients, or
2422 * clients that send an SSLv2 client hello. This message is not
2423 * tagged in any way, so NSS has no way to know if the incoming
2424 * message is a valid SSLv2 client hello or just some bogus data
2425 * (or cleartext LDAP). We store the first byte read from the
2426 * client here. The most common case will be a client sending
2427 * LDAP data instead of SSL encrypted LDAP data. This can happen,
2428 * for example, if using ldapsearch -Z - if the starttls fails,
2429 * the client will fallback to plain cleartext LDAP. So if we
2430 * see that the firstbyte is a valid LDAP tag, we can be
2431 * pretty sure this is happening.
2435 * NSS doesn't return SSL_ERROR_WANT_READ, SSL_ERROR_WANT_WRITE, etc.
2436 * when it is blocked, so we have to set a flag in the wrapped send
2437 * and recv calls that tells us what operation NSS was last blocked
2441 #define TLSM_WRITE 2
2445 static struct tls_data *
2446 tlsm_get_pvt_tls_data( PRFileDesc *fd )
2455 myfd = PR_GetIdentitiesLayer( fd, tlsm_layer_id );
2461 p = (struct tls_data *)myfd->secret;
2467 tlsm_is_non_ssl_message( PRFileDesc *fd, ber_tag_t *thebyte )
2472 *thebyte = LBER_DEFAULT;
2475 p = tlsm_get_pvt_tls_data( fd );
2476 if ( p == NULL || p->sbiod == NULL ) {
2480 if ( p->firsttag == LBER_SEQUENCE ) {
2482 *thebyte = p->firsttag;
2490 static tls_session *
2491 tlsm_session_new ( tls_ctx * ctx, int is_server )
2493 tlsm_ctx *c = (tlsm_ctx *)ctx;
2494 tlsm_session *session;
2499 c->tc_is_server = is_server;
2500 status = PR_CallOnceWithArg( &c->tc_callonce, tlsm_deferred_ctx_init, c );
2501 if ( PR_SUCCESS != status ) {
2502 PRErrorCode err = PR_GetError();
2503 Debug( LDAP_DEBUG_ANY,
2504 "TLS: error: could not initialize moznss security context - error %d:%s\n",
2505 err, PR_ErrorToString(err, PR_LANGUAGE_I_DEFAULT), NULL );
2509 fd = PR_CreateIOLayerStub( tlsm_layer_id, &tlsm_PR_methods );
2514 session = SSL_ImportFD( c->tc_model, fd );
2520 rc = SSL_ResetHandshake( session, is_server );
2522 PRErrorCode err = PR_GetError();
2523 Debug( LDAP_DEBUG_TRACE,
2524 "TLS: error: new session - reset handshake failure %d - error %d:%s\n",
2526 err ? PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ) : "unknown" );
2528 PR_Close( session );
2532 return (tls_session *)session;
2536 tlsm_session_accept_or_connect( tls_session *session, int is_accept )
2538 tlsm_session *s = (tlsm_session *)session;
2540 const char *op = is_accept ? "accept" : "connect";
2543 LDAP_MUTEX_LOCK( &tlsm_pem_mutex );
2545 rc = SSL_ForceHandshake( s );
2547 LDAP_MUTEX_UNLOCK( &tlsm_pem_mutex );
2550 PRErrorCode err = PR_GetError();
2552 if ( err == PR_WOULD_BLOCK_ERROR ) {
2553 ber_tag_t thetag = LBER_DEFAULT;
2554 /* see if we are blocked because of a bogus packet */
2555 if ( tlsm_is_non_ssl_message( s, &thetag ) ) { /* see if we received a non-SSL message */
2556 Debug( LDAP_DEBUG_ANY,
2557 "TLS: error: %s - error - received non-SSL message [0x%x]\n",
2558 op, (unsigned int)thetag, 0 );
2559 /* reset error to something more descriptive */
2560 PR_SetError( SSL_ERROR_RX_MALFORMED_HELLO_REQUEST, EPROTO );
2563 Debug( LDAP_DEBUG_ANY,
2564 "TLS: error: %s - force handshake failure: errno %d - moznss error %d\n",
2572 tlsm_session_accept( tls_session *session )
2574 return tlsm_session_accept_or_connect( session, 1 );
2578 tlsm_session_connect( LDAP *ld, tls_session *session )
2580 return tlsm_session_accept_or_connect( session, 0 );
2584 tlsm_session_upflags( Sockbuf *sb, tls_session *session, int rc )
2586 int prerror = PR_GetError();
2588 if ( ( prerror == PR_PENDING_INTERRUPT_ERROR ) || ( prerror == PR_WOULD_BLOCK_ERROR ) ) {
2589 tlsm_session *s = (tlsm_session *)session;
2590 struct tls_data *p = tlsm_get_pvt_tls_data( s );
2592 if ( p && ( p->io_flag == TLSM_READ ) ) {
2593 sb->sb_trans_needs_read = 1;
2595 } else if ( p && ( p->io_flag == TLSM_WRITE ) ) {
2596 sb->sb_trans_needs_write = 1;
2605 tlsm_session_errmsg( tls_session *sess, int rc, char *buf, size_t len )
2608 int prerror = PR_GetError();
2610 i = PR_GetErrorTextLength();
2612 char *msg = LDAP_MALLOC( i+1 );
2613 PR_GetErrorText( msg );
2614 memcpy( buf, msg, len );
2617 PR_GetErrorText( buf );
2618 } else if ( prerror ) {
2619 i = PR_snprintf( buf, len, "TLS error %d:%s",
2620 prerror, PR_ErrorToString( prerror, PR_LANGUAGE_I_DEFAULT ) );
2623 return ( i > 0 ) ? buf : NULL;
2627 tlsm_session_my_dn( tls_session *session, struct berval *der_dn )
2629 tlsm_session *s = (tlsm_session *)session;
2630 CERTCertificate *cert;
2632 cert = SSL_LocalCertificate( s );
2633 if (!cert) return LDAP_INVALID_CREDENTIALS;
2635 der_dn->bv_val = (char *)cert->derSubject.data;
2636 der_dn->bv_len = cert->derSubject.len;
2637 CERT_DestroyCertificate( cert );
2642 tlsm_session_peer_dn( tls_session *session, struct berval *der_dn )
2644 tlsm_session *s = (tlsm_session *)session;
2645 CERTCertificate *cert;
2647 cert = SSL_PeerCertificate( s );
2648 if (!cert) return LDAP_INVALID_CREDENTIALS;
2650 der_dn->bv_val = (char *)cert->derSubject.data;
2651 der_dn->bv_len = cert->derSubject.len;
2652 CERT_DestroyCertificate( cert );
2656 /* what kind of hostname were we given? */
2662 tlsm_session_chkhost( LDAP *ld, tls_session *session, const char *name_in )
2664 tlsm_session *s = (tlsm_session *)session;
2665 CERTCertificate *cert;
2666 const char *name, *domain = NULL, *ptr;
2667 int ret, ntype = IS_DNS, nlen, dlen;
2668 #ifdef LDAP_PF_INET6
2669 struct in6_addr addr;
2671 struct in_addr addr;
2676 if( ldap_int_hostname &&
2677 ( !name_in || !strcasecmp( name_in, "localhost" ) ) )
2679 name = ldap_int_hostname;
2683 nlen = strlen( name );
2685 cert = SSL_PeerCertificate( s );
2687 Debug( LDAP_DEBUG_ANY,
2688 "TLS: unable to get peer certificate.\n",
2690 /* if this was a fatal condition, things would have
2691 * aborted long before now.
2693 return LDAP_SUCCESS;
2696 #ifdef LDAP_PF_INET6
2697 if (name[0] == '[' && strchr(name, ']')) {
2698 char *n2 = ldap_strdup(name+1);
2699 *strchr(n2, ']') = 0;
2700 if (inet_pton(AF_INET6, n2, &addr))
2705 if ((ptr = strrchr(name, '.')) && isdigit((unsigned char)ptr[1])) {
2706 if (inet_aton(name, (struct in_addr *)&addr)) ntype = IS_IP4;
2708 if (ntype == IS_DNS ) {
2709 domain = strchr( name, '.' );
2711 dlen = nlen - ( domain - name );
2714 ret = LDAP_LOCAL_ERROR;
2716 rv = CERT_FindCertExtension( cert, SEC_OID_X509_SUBJECT_ALT_NAME,
2718 if ( rv == SECSuccess && altname.data ) {
2720 CERTGeneralName *names, *cur;
2722 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
2724 ret = LDAP_NO_MEMORY;
2728 names = cur = CERT_DecodeAltNameExtension(arena, &altname);
2737 if ( !cur->name.other.len ) continue;
2739 host = (char *)cur->name.other.data;
2740 hlen = cur->name.other.len;
2742 if ( cur->type == certDNSName ) {
2743 if ( ntype != IS_DNS ) continue;
2745 /* is this an exact match? */
2746 if ( nlen == hlen && !strncasecmp( name, host, nlen )) {
2751 /* is this a wildcard match? */
2752 if ( domain && host[0] == '*' && host[1] == '.' &&
2753 dlen == hlen-1 && !strncasecmp( domain, host+1, dlen )) {
2757 } else if ( cur->type == certIPAddress ) {
2758 if ( ntype == IS_DNS ) continue;
2760 #ifdef LDAP_PF_INET6
2761 if (ntype == IS_IP6 && hlen != sizeof(struct in6_addr)) {
2765 if (ntype == IS_IP4 && hlen != sizeof(struct in_addr)) {
2768 if (!memcmp(host, &addr, hlen)) {
2773 } while (( cur = CERT_GetNextGeneralName( cur )) != names );
2775 PORT_FreeArena( arena, PR_FALSE );
2776 SECITEM_FreeItem( &altname, PR_FALSE );
2778 /* no altnames matched, try the CN */
2779 if ( ret != LDAP_SUCCESS ) {
2780 /* find the last CN */
2781 CERTRDN *rdn, **rdns;
2782 CERTAVA *lastava = NULL;
2786 rdns = cert->subject.rdns;
2787 while ( rdns && ( rdn = *rdns++ )) {
2788 CERTAVA *ava, **avas = rdn->avas;
2789 while ( avas && ( ava = *avas++ )) {
2790 if ( CERT_GetAVATag( ava ) == SEC_OID_AVA_COMMON_NAME )
2795 SECItem *av = CERT_DecodeAVAValue( &lastava->value );
2797 if ( av->len == nlen && !strncasecmp( name, (char *)av->data, nlen )) {
2799 } else if ( av->data[0] == '*' && av->data[1] == '.' &&
2800 domain && dlen == av->len - 1 && !strncasecmp( domain,
2801 (char *)(av->data+1), dlen )) {
2805 if ( len >= sizeof(buf) )
2806 len = sizeof(buf)-1;
2807 memcpy( buf, av->data, len );
2810 SECITEM_FreeItem( av, PR_TRUE );
2813 if ( ret != LDAP_SUCCESS ) {
2814 Debug( LDAP_DEBUG_ANY, "TLS: hostname (%s) does not match "
2815 "common name in certificate (%s).\n",
2817 ret = LDAP_CONNECT_ERROR;
2818 if ( ld->ld_error ) {
2819 LDAP_FREE( ld->ld_error );
2821 ld->ld_error = LDAP_STRDUP(
2822 _("TLS: hostname does not match CN in peer certificate"));
2827 CERT_DestroyCertificate( cert );
2832 tlsm_session_strength( tls_session *session )
2834 tlsm_session *s = (tlsm_session *)session;
2837 rc = SSL_SecurityStatus( s, NULL, NULL, NULL, &keySize,
2839 return rc ? 0 : keySize;
2843 * TLS support for LBER Sockbufs
2846 static PRStatus PR_CALLBACK
2847 tlsm_PR_Close(PRFileDesc *fd)
2849 int rc = PR_SUCCESS;
2851 /* we don't need to actually close anything here, just
2852 pop our io layer off the stack */
2853 fd->secret = NULL; /* must have been freed before calling PR_Close */
2855 fd = PR_PopIOLayer( fd, tlsm_layer_id );
2856 /* if we are not the last layer, pass the close along */
2861 rc = fd->methods->close( fd );
2864 /* we are the last layer - just call our dtor */
2871 static PRStatus PR_CALLBACK
2872 tlsm_PR_Shutdown(PRFileDesc *fd, PRShutdownHow how)
2874 int rc = PR_SUCCESS;
2877 rc = PR_Shutdown( fd->lower, how );
2883 static int PR_CALLBACK
2884 tlsm_PR_Recv(PRFileDesc *fd, void *buf, PRInt32 len, PRIntn flags,
2885 PRIntervalTime timeout)
2890 if ( buf == NULL || len <= 0 ) return 0;
2892 p = tlsm_get_pvt_tls_data( fd );
2894 if ( p == NULL || p->sbiod == NULL ) {
2898 rc = LBER_SBIOD_READ_NEXT( p->sbiod, buf, len );
2900 tlsm_map_error( errno );
2901 if ( errno == EAGAIN || errno == EWOULDBLOCK ) {
2902 p->nonblock = PR_TRUE; /* fd is using non-blocking io */
2903 } else if ( errno ) { /* real error */
2904 Debug( LDAP_DEBUG_TRACE,
2905 "TLS: error: tlsm_PR_Recv returned %d - error %d:%s\n",
2906 rc, errno, STRERROR(errno) );
2908 } else if ( ( rc > 0 ) && ( len > 0 ) && ( p->firsttag == LBER_DEFAULT ) ) {
2909 p->firsttag = (ber_tag_t)*((char *)buf);
2911 p->io_flag = TLSM_READ;
2916 static int PR_CALLBACK
2917 tlsm_PR_Send(PRFileDesc *fd, const void *buf, PRInt32 len, PRIntn flags,
2918 PRIntervalTime timeout)
2923 if ( buf == NULL || len <= 0 ) return 0;
2925 p = tlsm_get_pvt_tls_data( fd );
2927 if ( p == NULL || p->sbiod == NULL ) {
2931 rc = LBER_SBIOD_WRITE_NEXT( p->sbiod, (char *)buf, len );
2933 tlsm_map_error( errno );
2934 if ( errno == EAGAIN || errno == EWOULDBLOCK ) {
2935 p->nonblock = PR_TRUE;
2936 } else if ( errno ) { /* real error */
2937 Debug( LDAP_DEBUG_TRACE,
2938 "TLS: error: tlsm_PR_Send returned %d - error %d:%s\n",
2939 rc, errno, STRERROR(errno) );
2942 p->io_flag = TLSM_WRITE;
2947 static int PR_CALLBACK
2948 tlsm_PR_Read(PRFileDesc *fd, void *buf, PRInt32 len)
2950 return tlsm_PR_Recv( fd, buf, len, 0, PR_INTERVAL_NO_TIMEOUT );
2953 static int PR_CALLBACK
2954 tlsm_PR_Write(PRFileDesc *fd, const void *buf, PRInt32 len)
2956 return tlsm_PR_Send( fd, buf, len, 0, PR_INTERVAL_NO_TIMEOUT );
2959 static PRStatus PR_CALLBACK
2960 tlsm_PR_GetPeerName(PRFileDesc *fd, PRNetAddr *addr)
2965 p = tlsm_get_pvt_tls_data( fd );
2967 if ( p == NULL || p->sbiod == NULL ) {
2970 len = sizeof(PRNetAddr);
2971 return getpeername( p->sbiod->sbiod_sb->sb_fd, (struct sockaddr *)addr, &len );
2974 static PRStatus PR_CALLBACK
2975 tlsm_PR_GetSocketOption(PRFileDesc *fd, PRSocketOptionData *data)
2978 p = tlsm_get_pvt_tls_data( fd );
2980 if ( p == NULL || data == NULL ) {
2984 /* only the nonblocking option is supported at this time
2985 MozNSS SSL code needs it */
2986 if ( data->option != PR_SockOpt_Nonblocking ) {
2987 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
2991 int flags = fcntl( p->sbiod->sbiod_sb->sb_fd, F_GETFL );
2992 data->value.non_blocking = (flags & O_NONBLOCK) ? PR_TRUE : PR_FALSE;
2994 data->value.non_blocking = p->nonblock;
2999 static PRStatus PR_CALLBACK
3002 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
3006 static PRFileDesc * PR_CALLBACK
3009 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
3013 static PRInt16 PR_CALLBACK
3016 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
3020 static PRInt32 PR_CALLBACK
3023 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
3027 static PRInt64 PR_CALLBACK
3032 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
3037 static const PRIOMethods tlsm_PR_methods = {
3039 tlsm_PR_Close, /* close */
3040 tlsm_PR_Read, /* read */
3041 tlsm_PR_Write, /* write */
3042 tlsm_PR_i32_unimp, /* available */
3043 tlsm_PR_i64_unimp, /* available64 */
3044 tlsm_PR_prs_unimp, /* fsync */
3045 tlsm_PR_i32_unimp, /* seek */
3046 tlsm_PR_i64_unimp, /* seek64 */
3047 tlsm_PR_prs_unimp, /* fileInfo */
3048 tlsm_PR_prs_unimp, /* fileInfo64 */
3049 tlsm_PR_i32_unimp, /* writev */
3050 tlsm_PR_prs_unimp, /* connect */
3051 tlsm_PR_pfd_unimp, /* accept */
3052 tlsm_PR_prs_unimp, /* bind */
3053 tlsm_PR_prs_unimp, /* listen */
3054 (PRShutdownFN)tlsm_PR_Shutdown, /* shutdown */
3055 tlsm_PR_Recv, /* recv */
3056 tlsm_PR_Send, /* send */
3057 tlsm_PR_i32_unimp, /* recvfrom */
3058 tlsm_PR_i32_unimp, /* sendto */
3059 (PRPollFN)tlsm_PR_i16_unimp, /* poll */
3060 tlsm_PR_i32_unimp, /* acceptread */
3061 tlsm_PR_i32_unimp, /* transmitfile */
3062 tlsm_PR_prs_unimp, /* getsockname */
3063 tlsm_PR_GetPeerName, /* getpeername */
3064 tlsm_PR_i32_unimp, /* getsockopt OBSOLETE */
3065 tlsm_PR_i32_unimp, /* setsockopt OBSOLETE */
3066 tlsm_PR_GetSocketOption, /* getsocketoption */
3067 tlsm_PR_i32_unimp, /* setsocketoption */
3068 tlsm_PR_i32_unimp, /* Send a (partial) file with header/trailer*/
3069 (PRConnectcontinueFN)tlsm_PR_prs_unimp, /* connectcontinue */
3070 tlsm_PR_i32_unimp, /* reserved for future use */
3071 tlsm_PR_i32_unimp, /* reserved for future use */
3072 tlsm_PR_i32_unimp, /* reserved for future use */
3073 tlsm_PR_i32_unimp /* reserved for future use */
3077 * Initialize TLS subsystem. Should be called only once.
3078 * See tlsm_deferred_init for the bulk of the init process
3083 char *nofork = PR_GetEnv( "NSS_STRICT_NOFORK" );
3087 tlsm_layer_id = PR_GetUniqueIdentity( "OpenLDAP" );
3090 * There are some applications that acquire a crypto context in the parent process
3091 * and expect that crypto context to work after a fork(). This does not work
3092 * with NSS using strict PKCS11 compliance mode. We set this environment
3093 * variable here to tell the software encryption module/token to allow crypto
3094 * contexts to persist across a fork(). However, if you are using some other
3095 * module or encryption device that supports and expects full PKCS11 semantics,
3096 * the only recourse is to rewrite the application with atfork() handlers to save
3097 * the crypto context in the parent and restore (and SECMOD_RestartModules) the
3098 * context in the child.
3101 /* will leak one time */
3102 char *noforkenvvar = PL_strdup( "NSS_STRICT_NOFORK=DISABLED" );
3103 PR_SetEnv( noforkenvvar );
3110 tlsm_sb_setup( Sockbuf_IO_Desc *sbiod, void *arg )
3113 tlsm_session *session = arg;
3116 assert( sbiod != NULL );
3118 p = LBER_MALLOC( sizeof( *p ) );
3123 fd = PR_GetIdentitiesLayer( session, tlsm_layer_id );
3129 fd->secret = (PRFilePrivate *)p;
3130 p->session = session;
3132 p->firsttag = LBER_DEFAULT;
3133 sbiod->sbiod_pvt = p;
3138 tlsm_sb_remove( Sockbuf_IO_Desc *sbiod )
3142 assert( sbiod != NULL );
3143 assert( sbiod->sbiod_pvt != NULL );
3145 p = (struct tls_data *)sbiod->sbiod_pvt;
3146 PR_Close( p->session );
3147 LBER_FREE( sbiod->sbiod_pvt );
3148 sbiod->sbiod_pvt = NULL;
3153 tlsm_sb_close( Sockbuf_IO_Desc *sbiod )
3157 assert( sbiod != NULL );
3158 assert( sbiod->sbiod_pvt != NULL );
3160 p = (struct tls_data *)sbiod->sbiod_pvt;
3161 PR_Shutdown( p->session, PR_SHUTDOWN_BOTH );
3166 tlsm_sb_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
3170 assert( sbiod != NULL );
3171 assert( sbiod->sbiod_pvt != NULL );
3173 p = (struct tls_data *)sbiod->sbiod_pvt;
3175 if ( opt == LBER_SB_OPT_GET_SSL ) {
3176 *((tlsm_session **)arg) = p->session;
3179 } else if ( opt == LBER_SB_OPT_DATA_READY ) {
3180 if ( p && ( SSL_DataPending( p->session ) > 0 ) ) {
3186 return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg );
3190 tlsm_sb_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
3196 assert( sbiod != NULL );
3197 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
3199 p = (struct tls_data *)sbiod->sbiod_pvt;
3201 ret = PR_Recv( p->session, buf, len, 0, PR_INTERVAL_NO_TIMEOUT );
3203 err = PR_GetError();
3204 if ( err == PR_PENDING_INTERRUPT_ERROR || err == PR_WOULD_BLOCK_ERROR ) {
3205 sbiod->sbiod_sb->sb_trans_needs_read = 1;
3206 sock_errset(EWOULDBLOCK);
3209 sbiod->sbiod_sb->sb_trans_needs_read = 0;
3215 tlsm_sb_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
3221 assert( sbiod != NULL );
3222 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
3224 p = (struct tls_data *)sbiod->sbiod_pvt;
3226 ret = PR_Send( p->session, (char *)buf, len, 0, PR_INTERVAL_NO_TIMEOUT );
3228 err = PR_GetError();
3229 if ( err == PR_PENDING_INTERRUPT_ERROR || err == PR_WOULD_BLOCK_ERROR ) {
3230 sbiod->sbiod_sb->sb_trans_needs_write = 1;
3231 sock_errset(EWOULDBLOCK);
3235 sbiod->sbiod_sb->sb_trans_needs_write = 0;
3240 static Sockbuf_IO tlsm_sbio =
3242 tlsm_sb_setup, /* sbi_setup */
3243 tlsm_sb_remove, /* sbi_remove */
3244 tlsm_sb_ctrl, /* sbi_ctrl */
3245 tlsm_sb_read, /* sbi_read */
3246 tlsm_sb_write, /* sbi_write */
3247 tlsm_sb_close /* sbi_close */
3250 tls_impl ldap_int_tls_impl = {
3262 tlsm_session_connect,
3263 tlsm_session_accept,
3264 tlsm_session_upflags,
3265 tlsm_session_errmsg,
3267 tlsm_session_peer_dn,
3268 tlsm_session_chkhost,
3269 tlsm_session_strength,
3273 #ifdef LDAP_R_COMPILE
3282 #endif /* HAVE_MOZNSS */