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-2010 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>
63 /* NSS 3.12.5 and later have NSS_InitContext */
64 #if NSS_VMAJOR <= 3 && NSS_VMINOR <= 12 && NSS_VPATCH < 5
67 #define HAVE_NSS_INITCONTEXT 1
70 /* InitContext does not currently work in server mode */
71 /* #define INITCONTEXT_HACK 1 */
73 typedef struct tlsm_ctx {
76 PRBool tc_verify_cert;
77 CERTCertDBHandle *tc_certdb;
80 struct ldaptls *tc_config;
83 PRCallOnceType tc_callonce;
85 char *tc_slotname; /* if using pem */
86 #ifdef HAVE_NSS_INITCONTEXT
87 NSSInitContext *tc_initctx; /* the NSS context */
89 PK11GenericObject **tc_pem_objs; /* array of objects to free */
90 int tc_n_pem_objs; /* number of objects */
92 ldap_pvt_thread_mutex_t tc_refmutex;
96 typedef PRFileDesc tlsm_session;
98 static PRDescIdentity tlsm_layer_id;
100 static const PRIOMethods tlsm_PR_methods;
102 #define PEM_LIBRARY "nsspem"
103 #define PEM_MODULE "PEM"
104 /* hash files for use with cacertdir have this file name suffix */
105 #define PEM_CA_HASH_FILE_SUFFIX ".0"
106 #define PEM_CA_HASH_FILE_SUFFIX_LEN 2
108 static SECMODModule *pem_module;
110 #define DEFAULT_TOKEN_NAME "default"
111 /* sprintf format used to create token name */
112 #define TLSM_PEM_TOKEN_FMT "PEM Token #%ld"
114 static int tlsm_slot_count;
116 #define PK11_SETATTRS(x,id,v,l) (x)->type = (id); \
117 (x)->pValue=(v); (x)->ulValueLen = (l);
119 /* forward declaration */
120 static int tlsm_init( void );
122 #ifdef LDAP_R_COMPILE
125 tlsm_thr_init( void )
129 #endif /* LDAP_R_COMPILE */
132 tlsm_dump_cipher_info(PRFileDesc *fd)
136 for (ii = 0; ii < SSL_NumImplementedCiphers; ++ii) {
137 PRInt32 cipher = (PRInt32)SSL_ImplementedCiphers[ii];
138 PRBool enabled = PR_FALSE;
140 SSLCipherSuiteInfo info;
143 SSL_CipherPrefGet(fd, cipher, &enabled);
145 SSL_CipherPrefGetDefault(cipher, &enabled);
147 SSL_CipherPolicyGet(cipher, &policy);
148 SSL_GetCipherSuiteInfo(cipher, &info, (PRUintn)sizeof(info));
149 Debug( LDAP_DEBUG_TRACE,
150 "TLS: cipher: %d - %s, enabled: %d, ",
151 info.cipherSuite, info.cipherSuiteName, enabled );
152 Debug( LDAP_DEBUG_TRACE,
153 "policy: %d\n", policy, 0, 0 );
159 /* Cipher definitions */
161 char *ossl_name; /* The OpenSSL cipher name */
162 int num; /* The cipher id */
163 int attr; /* cipher attributes: algorithms, etc */
164 int version; /* protocol version valid for this cipher */
165 int bits; /* bits of strength */
166 int alg_bits; /* bits of the algorithm */
167 int strength; /* LOW, MEDIUM, HIGH */
168 int enabled; /* Enabled by default? */
171 /* cipher attributes */
172 #define SSL_kRSA 0x00000001L
173 #define SSL_aRSA 0x00000002L
174 #define SSL_aDSS 0x00000004L
175 #define SSL_DSS SSL_aDSS
176 #define SSL_eNULL 0x00000008L
177 #define SSL_DES 0x00000010L
178 #define SSL_3DES 0x00000020L
179 #define SSL_RC4 0x00000040L
180 #define SSL_RC2 0x00000080L
181 #define SSL_AES 0x00000100L
182 #define SSL_MD5 0x00000200L
183 #define SSL_SHA1 0x00000400L
184 #define SSL_SHA SSL_SHA1
185 #define SSL_RSA (SSL_kRSA|SSL_aRSA)
187 /* cipher strength */
188 #define SSL_NULL 0x00000001L
189 #define SSL_EXPORT40 0x00000002L
190 #define SSL_EXPORT56 0x00000004L
191 #define SSL_LOW 0x00000008L
192 #define SSL_MEDIUM 0x00000010L
193 #define SSL_HIGH 0x00000020L
195 #define SSL2 0x00000001L
196 #define SSL3 0x00000002L
197 /* OpenSSL treats SSL3 and TLSv1 the same */
200 /* Cipher translation */
201 static cipher_properties ciphers_def[] = {
203 {"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},
204 {"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},
205 {"RC4-MD5", SSL_EN_RC4_128_WITH_MD5, SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_MD5, SSL2, 128, 128, SSL_MEDIUM, SSL_ALLOWED},
206 {"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},
207 {"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},
208 {"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},
211 {"RC4-MD5", SSL_RSA_WITH_RC4_128_MD5, SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_MD5, SSL3, 128, 128, SSL_MEDIUM, SSL_ALLOWED},
212 {"RC4-SHA", SSL_RSA_WITH_RC4_128_SHA, SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_SHA1, SSL3, 128, 128, SSL_MEDIUM, SSL_NOT_ALLOWED},
213 {"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},
214 {"DES-CBC-SHA", SSL_RSA_WITH_DES_CBC_SHA, SSL_kRSA|SSL_aRSA|SSL_DES|SSL_SHA1, SSL3, 56, 56, SSL_LOW, SSL_ALLOWED},
215 {"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},
216 {"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},
217 {"NULL-MD5", SSL_RSA_WITH_NULL_MD5, SSL_kRSA|SSL_aRSA|SSL_eNULL|SSL_MD5, SSL3, 0, 0, SSL_NULL, SSL_NOT_ALLOWED},
218 {"NULL-SHA", SSL_RSA_WITH_NULL_SHA, SSL_kRSA|SSL_aRSA|SSL_eNULL|SSL_SHA1, SSL3, 0, 0, SSL_NULL, SSL_NOT_ALLOWED},
221 {"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},
222 {"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},
223 {"AES128-SHA", TLS_RSA_WITH_AES_128_CBC_SHA, SSL_kRSA|SSL_aRSA|SSL_AES|SSL_SHA, TLS1, 128, 128, SSL_HIGH, SSL_NOT_ALLOWED},
224 {"AES256-SHA", TLS_RSA_WITH_AES_256_CBC_SHA, SSL_kRSA|SSL_aRSA|SSL_AES|SSL_SHA, TLS1, 256, 256, SSL_HIGH, SSL_NOT_ALLOWED},
227 #define ciphernum (sizeof(ciphers_def)/sizeof(cipher_properties))
229 /* given err which is the current errno, calls PR_SetError with
230 the corresponding NSPR error code */
232 tlsm_map_error(int err)
238 prError = PR_NO_ACCESS_RIGHTS_ERROR;
241 prError = PR_ADDRESS_IN_USE_ERROR;
244 prError = PR_ADDRESS_NOT_AVAILABLE_ERROR;
247 prError = PR_ADDRESS_NOT_SUPPORTED_ERROR;
250 prError = PR_WOULD_BLOCK_ERROR;
253 * On QNX and Neutrino, EALREADY is defined as EBUSY.
255 #if EALREADY != EBUSY
257 prError = PR_ALREADY_INITIATED_ERROR;
261 prError = PR_BAD_DESCRIPTOR_ERROR;
265 prError = PR_IO_ERROR;
269 prError = PR_FILESYSTEM_MOUNTED_ERROR;
272 prError = PR_CONNECT_ABORTED_ERROR;
275 prError = PR_CONNECT_REFUSED_ERROR;
278 prError = PR_CONNECT_RESET_ERROR;
281 prError = PR_DEADLOCK_ERROR;
285 prError = PR_DIRECTORY_CORRUPTED_ERROR;
290 prError = PR_NO_DEVICE_SPACE_ERROR;
294 prError = PR_FILE_EXISTS_ERROR;
297 prError = PR_ACCESS_FAULT_ERROR;
300 prError = PR_FILE_TOO_BIG_ERROR;
303 prError = PR_HOST_UNREACHABLE_ERROR;
306 prError = PR_IN_PROGRESS_ERROR;
309 prError = PR_PENDING_INTERRUPT_ERROR;
312 prError = PR_INVALID_ARGUMENT_ERROR;
315 prError = PR_IO_ERROR;
318 prError = PR_IS_CONNECTED_ERROR;
321 prError = PR_IS_DIRECTORY_ERROR;
324 prError = PR_LOOP_ERROR;
327 prError = PR_PROC_DESC_TABLE_FULL_ERROR;
330 prError = PR_MAX_DIRECTORY_ENTRIES_ERROR;
333 prError = PR_INVALID_ARGUMENT_ERROR;
337 prError = PR_REMOTE_FILE_ERROR;
341 prError = PR_NAME_TOO_LONG_ERROR;
344 prError = PR_NETWORK_UNREACHABLE_ERROR;
347 prError = PR_SYS_DESC_TABLE_FULL_ERROR;
350 * On SCO OpenServer 5, ENOBUFS is defined as ENOSR.
352 #if defined(ENOBUFS) && (ENOBUFS != ENOSR)
354 prError = PR_INSUFFICIENT_RESOURCES_ERROR;
358 prError = PR_FILE_NOT_FOUND_ERROR;
361 prError = PR_FILE_NOT_FOUND_ERROR;
364 prError = PR_FILE_IS_LOCKED_ERROR;
368 prError = PR_REMOTE_FILE_ERROR;
372 prError = PR_OUT_OF_MEMORY_ERROR;
375 prError = PR_INVALID_ARGUMENT_ERROR;
378 prError = PR_NO_DEVICE_SPACE_ERROR;
382 prError = PR_INSUFFICIENT_RESOURCES_ERROR;
386 prError = PR_NOT_CONNECTED_ERROR;
389 prError = PR_NOT_DIRECTORY_ERROR;
392 prError = PR_NOT_SOCKET_ERROR;
395 prError = PR_FILE_NOT_FOUND_ERROR;
398 prError = PR_NOT_TCP_SOCKET_ERROR;
402 prError = PR_BUFFER_OVERFLOW_ERROR;
406 prError = PR_NO_ACCESS_RIGHTS_ERROR;
409 prError = PR_CONNECT_RESET_ERROR;
413 prError = PR_IO_ERROR;
416 case EPROTONOSUPPORT:
417 prError = PR_PROTOCOL_NOT_SUPPORTED_ERROR;
420 prError = PR_ADDRESS_NOT_SUPPORTED_ERROR;
423 prError = PR_INVALID_METHOD_ERROR;
426 prError = PR_READ_ONLY_FILESYSTEM_ERROR;
429 prError = PR_INVALID_METHOD_ERROR;
432 prError = PR_IO_TIMEOUT_ERROR;
434 #if EWOULDBLOCK != EAGAIN
436 prError = PR_WOULD_BLOCK_ERROR;
440 prError = PR_NOT_SAME_DEVICE_ERROR;
443 prError = PR_UNKNOWN_ERROR;
446 PR_SetError( prError, err );
450 * cipher_list is an integer array with the following values:
451 * -1: never enable this cipher
456 nss_parse_ciphers(const char *cipherstr, int cipher_list[ciphernum])
465 /* All disabled to start */
466 for (i=0; i<ciphernum; i++)
469 ciphertip = strdup(cipherstr);
470 cipher = ciphers = ciphertip;
472 while (ciphers && (strlen(ciphers))) {
473 while ((*cipher) && (isspace(*cipher)))
478 case '+': /* Add something */
482 case '-': /* Subtract something */
486 case '!': /* Disable something */
495 if ((ciphers = strchr(cipher, ':'))) {
499 /* Do the easy one first */
500 if (!strcmp(cipher, "ALL")) {
501 for (i=0; i<ciphernum; i++) {
502 if (!(ciphers_def[i].attr & SSL_eNULL))
503 cipher_list[i] = action;
505 } else if (!strcmp(cipher, "COMPLEMENTOFALL")) {
506 for (i=0; i<ciphernum; i++) {
507 if ((ciphers_def[i].attr & SSL_eNULL))
508 cipher_list[i] = action;
510 } else if (!strcmp(cipher, "DEFAULT")) {
511 for (i=0; i<ciphernum; i++) {
512 cipher_list[i] = ciphers_def[i].enabled == SSL_ALLOWED ? 1 : 0;
521 while (c && (strlen(c))) {
523 if ((c = strchr(cipher, '+'))) {
527 if (!strcmp(cipher, "RSA")) {
529 } else if ((!strcmp(cipher, "NULL")) || (!strcmp(cipher, "eNULL"))) {
531 } else if (!strcmp(cipher, "AES")) {
533 } else if (!strcmp(cipher, "3DES")) {
535 } else if (!strcmp(cipher, "DES")) {
537 } else if (!strcmp(cipher, "RC4")) {
539 } else if (!strcmp(cipher, "RC2")) {
541 } else if (!strcmp(cipher, "MD5")) {
543 } else if ((!strcmp(cipher, "SHA")) || (!strcmp(cipher, "SHA1"))) {
545 } else if (!strcmp(cipher, "SSLv2")) {
547 } else if (!strcmp(cipher, "SSLv3")) {
549 } else if (!strcmp(cipher, "TLSv1")) {
551 } else if (!strcmp(cipher, "HIGH")) {
552 strength |= SSL_HIGH;
553 } else if (!strcmp(cipher, "MEDIUM")) {
554 strength |= SSL_MEDIUM;
555 } else if (!strcmp(cipher, "LOW")) {
557 } else if ((!strcmp(cipher, "EXPORT")) || (!strcmp(cipher, "EXP"))) {
558 strength |= SSL_EXPORT40|SSL_EXPORT56;
559 } else if (!strcmp(cipher, "EXPORT40")) {
560 strength |= SSL_EXPORT40;
561 } else if (!strcmp(cipher, "EXPORT56")) {
562 strength |= SSL_EXPORT56;
570 /* If we have a mask, apply it. If not then perhaps they provided
571 * a specific cipher to enable.
573 if (mask || strength || protocol) {
574 for (i=0; i<ciphernum; i++) {
575 if (((ciphers_def[i].attr & mask) ||
576 (ciphers_def[i].strength & strength) ||
577 (ciphers_def[i].version & protocol)) &&
578 (cipher_list[i] != -1)) {
579 /* Enable the NULL ciphers only if explicity
581 if (ciphers_def[i].attr & SSL_eNULL) {
582 if (mask & SSL_eNULL)
583 cipher_list[i] = action;
585 cipher_list[i] = action;
589 for (i=0; i<ciphernum; i++) {
590 if (!strcmp(ciphers_def[i].ossl_name, cipher) &&
591 cipher_list[1] != -1)
592 cipher_list[i] = action;
601 /* See if any ciphers were enabled */
603 for (i=0; i<ciphernum; i++) {
604 if (cipher_list[i] == 1)
614 tlsm_parse_ciphers(tlsm_ctx *ctx, const char *str)
616 int cipher_state[ciphernum];
622 rv = nss_parse_ciphers(str, cipher_state);
625 /* First disable everything */
626 for (i = 0; i < SSL_NumImplementedCiphers; i++)
627 SSL_CipherPrefSet(ctx->tc_model, SSL_ImplementedCiphers[i], SSL_NOT_ALLOWED);
629 /* Now enable what was requested */
630 for (i=0; i<ciphernum; i++) {
631 SSLCipherSuiteInfo suite;
634 if (SSL_GetCipherSuiteInfo(ciphers_def[i].num, &suite, sizeof suite)
636 enabled = cipher_state[i] < 0 ? 0 : cipher_state[i];
637 if (enabled == SSL_ALLOWED) {
638 if (PK11_IsFIPS() && !suite.isFIPS)
639 enabled = SSL_NOT_ALLOWED;
641 SSL_CipherPrefSet(ctx->tc_model, ciphers_def[i].num, enabled);
646 return rv == 1 ? 0 : -1;
650 tlsm_bad_cert_handler(void *arg, PRFileDesc *ssl)
652 SECStatus success = SECSuccess;
654 tlsm_ctx *ctx = (tlsm_ctx *)arg;
660 err = PORT_GetError();
663 case SEC_ERROR_UNTRUSTED_ISSUER:
664 case SEC_ERROR_UNKNOWN_ISSUER:
665 case SEC_ERROR_EXPIRED_CERTIFICATE:
666 if (ctx->tc_verify_cert) {
667 success = SECFailure;
670 /* we bypass NSS's hostname checks and do our own */
671 case SSL_ERROR_BAD_CERT_DOMAIN:
674 success = SECFailure;
682 tlsm_dump_security_status(PRFileDesc *fd)
684 char * cp; /* bulk cipher name */
685 char * ip; /* cert issuer DN */
686 char * sp; /* cert subject DN */
687 int op; /* High, Low, Off */
688 int kp0; /* total key bits */
689 int kp1; /* secret key bits */
690 SSL3Statistics * ssl3stats = SSL_GetStatistics();
692 SSL_SecurityStatus( fd, &op, &cp, &kp0, &kp1, &ip, &sp );
693 Debug( LDAP_DEBUG_TRACE,
694 "TLS certificate verification: subject: %s, issuer: %s, cipher: %s, ",
695 sp ? sp : "-unknown-", ip ? ip : "-unknown-", cp ? cp : "-unknown-" );
699 Debug( LDAP_DEBUG_TRACE,
700 "security level: %s, secret key bits: %d, total key bits: %d, ",
701 ((op == SSL_SECURITY_STATUS_ON_HIGH) ? "high" :
702 ((op == SSL_SECURITY_STATUS_ON_LOW) ? "low" : "off")),
705 Debug( LDAP_DEBUG_TRACE,
706 "cache hits: %ld, cache misses: %ld, cache not reusable: %ld\n",
707 ssl3stats->hch_sid_cache_hits, ssl3stats->hch_sid_cache_misses,
708 ssl3stats->hch_sid_cache_not_ok );
714 tlsm_handshake_complete_cb( PRFileDesc *fd, void *client_data )
716 tlsm_dump_security_status( fd );
719 #ifdef READ_PASSWORD_FROM_FILE
721 tlsm_get_pin_from_file(const char *token_name, tlsm_ctx *ctx)
724 char *contents = NULL;
727 char *candidate = NULL;
728 PRFileInfo file_info;
729 PRFileDesc *pwd_fileptr = PR_Open( ctx->tc_pin_file, PR_RDONLY, 00400 );
731 /* open the password file */
732 if ( !pwd_fileptr ) {
733 PRErrorCode errcode = PR_GetError();
734 Debug( LDAP_DEBUG_ANY,
735 "TLS: could not open security pin file %s - error %d:%s.\n",
736 ctx->tc_pin_file, errcode,
737 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
741 /* get the file size */
742 if ( PR_SUCCESS != PR_GetFileInfo( ctx->tc_pin_file, &file_info ) ) {
743 PRErrorCode errcode = PR_GetError();
744 Debug( LDAP_DEBUG_ANY,
745 "TLS: could not get file info from pin file %s - error %d:%s.\n",
746 ctx->tc_pin_file, errcode,
747 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
751 /* create a buffer to hold the file contents */
752 if ( !( contents = PR_MALLOC( file_info.size + 1 ) ) ) {
753 PRErrorCode errcode = PR_GetError();
754 Debug( LDAP_DEBUG_ANY,
755 "TLS: could not alloc a buffer for contents of pin file %s - error %d:%s.\n",
756 ctx->tc_pin_file, errcode,
757 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
761 /* read file into the buffer */
762 if( PR_Read( pwd_fileptr, contents, file_info.size ) <= 0 ) {
763 PRErrorCode errcode = PR_GetError();
764 Debug( LDAP_DEBUG_ANY,
765 "TLS: could not read the file contents from pin file %s - error %d:%s.\n",
766 ctx->tc_pin_file, errcode,
767 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
771 /* format is [tokenname:]password EOL [tokenname:]password EOL ... */
772 /* if you want to use a password containing a colon character, use
773 the special tokenname "default" */
774 for ( line = PL_strtok_r( contents, "\r\n", &lasts ); line;
775 line = PL_strtok_r( NULL, "\r\n", &lasts ) ) {
779 continue; /* skip blank lines */
781 colon = PL_strchr( line, ':' );
783 if ( *(colon + 1) && token_name &&
784 !PL_strncmp( token_name, line, colon-line ) ) {
785 candidate = colon + 1; /* found a definite match */
787 } else if ( !PL_strncmp( DEFAULT_TOKEN_NAME, line, colon-line ) ) {
788 candidate = colon + 1; /* found possible match */
790 } else { /* no token name */
796 PR_Close( pwd_fileptr );
799 pwdstr = PL_strdup( candidate );
801 PL_strfree( contents );
805 #endif /* READ_PASSWORD_FROM_FILE */
807 #ifdef READ_PASSWORD_FROM_STDIN
809 * Turn the echoing off on a tty.
814 if ( isatty( fd ) ) {
816 tcgetattr( fd, &tio );
817 tio.c_lflag &= ~ECHO;
818 tcsetattr( fd, TCSAFLUSH, &tio );
823 * Turn the echoing on on a tty.
828 if ( isatty( fd ) ) {
830 tcgetattr( fd, &tio );
832 tcsetattr( fd, TCSAFLUSH, &tio );
833 tcsetattr( fd, TCSAFLUSH, &tio );
836 #endif /* READ_PASSWORD_FROM_STDIN */
839 * This does the actual work of reading the pin/password/pass phrase
842 tlsm_get_pin(PK11SlotInfo *slot, PRBool retry, tlsm_ctx *ctx)
844 char *token_name = NULL;
847 token_name = PK11_GetTokenName( slot );
848 #ifdef READ_PASSWORD_FROM_FILE
849 /* Try to get the passwords from the password file if it exists.
850 * THIS IS UNSAFE and is provided for convenience only. Without this
851 * capability the server would have to be started in foreground mode
852 * if using an encrypted key.
854 if ( ctx->tc_pin_file ) {
855 pwdstr = tlsm_get_pin_from_file( token_name, ctx );
857 #endif /* RETRIEVE_PASSWORD_FROM_FILE */
858 #ifdef READ_PASSWORD_FROM_STDIN
860 int infd = PR_FileDesc2NativeHandle( PR_STDIN );
861 int isTTY = isatty( infd );
862 unsigned char phrase[200];
863 /* Prompt for password */
866 "Please enter pin, password, or pass phrase for security token '%s': ",
867 token_name ? token_name : DEFAULT_TOKEN_NAME );
870 fgets( (char*)phrase, sizeof(phrase), stdin );
872 fprintf( stdout, "\n" );
875 /* stomp on newline */
876 phrase[strlen((char*)phrase)-1] = 0;
878 pwdstr = PL_strdup( (char*)phrase );
881 #endif /* READ_PASSWORD_FROM_STDIN */
886 * PKCS11 devices (including the internal softokn cert/key database)
887 * may be protected by a pin or password or even pass phrase
888 * MozNSS needs a way for the user to provide that
891 tlsm_pin_prompt(PK11SlotInfo *slot, PRBool retry, void *arg)
893 tlsm_ctx *ctx = (tlsm_ctx *)arg;
895 return tlsm_get_pin( slot, retry, ctx );
899 tlsm_auth_cert_handler(void *arg, PRFileDesc *fd,
900 PRBool checksig, PRBool isServer)
902 SECStatus ret = SSL_AuthCertificate(arg, fd, checksig, isServer);
904 if ( ret != SECSuccess ) {
905 PRErrorCode errcode = PORT_GetError();
906 /* we bypass NSS's hostname checks and do our own - tlsm_session_chkhost will handle it */
907 if ( errcode == SSL_ERROR_BAD_CERT_DOMAIN ) {
908 Debug( LDAP_DEBUG_TRACE,
909 "TLS certificate verification: defer\n",
912 Debug( LDAP_DEBUG_ANY,
913 "TLS certificate verification: Error, %d: %s\n",
914 errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ), 0 ) ;
917 Debug( LDAP_DEBUG_TRACE,
918 "TLS certificate verification: ok\n",
926 tlsm_authenticate_to_slot( tlsm_ctx *ctx, PK11SlotInfo *slot )
930 if ( SECSuccess != PK11_Authenticate( slot, PR_FALSE, ctx ) ) {
931 char *token_name = PK11_GetTokenName( slot );
932 PRErrorCode errcode = PR_GetError();
933 Debug( LDAP_DEBUG_ANY,
934 "TLS: could not authenticate to the security token %s - error %d:%s.\n",
935 token_name ? token_name : DEFAULT_TOKEN_NAME, errcode,
936 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
938 rc = 0; /* success */
945 tlsm_nss_shutdown_cb( void *appData, void *nssData )
947 SECStatus rc = SECSuccess;
949 SSL_ShutdownServerSessionIDCache();
950 SSL_ClearSessionCache();
953 SECMOD_UnloadUserModule( pem_module );
954 SECMOD_DestroyModule( pem_module );
961 tlsm_init_pem_module( void )
964 char *fullname = NULL;
965 char *configstring = NULL;
971 /* not loaded - load it */
972 /* get the system dependent library name */
973 fullname = PR_GetLibraryName( NULL, PEM_LIBRARY );
974 /* Load our PKCS#11 module */
975 configstring = PR_smprintf( "library=%s name=" PEM_MODULE " parameters=\"\"", fullname );
976 PL_strfree( fullname );
978 pem_module = SECMOD_LoadUserModule( configstring, NULL, PR_FALSE );
979 PR_smprintf_free( configstring );
981 if ( !pem_module || !pem_module->loaded ) {
983 SECMOD_DestroyModule( pem_module );
993 tlsm_add_pem_obj( tlsm_ctx *ctx, PK11GenericObject *obj )
995 int idx = ctx->tc_n_pem_objs;
996 ctx->tc_n_pem_objs++;
997 ctx->tc_pem_objs = (PK11GenericObject **)
998 PORT_Realloc( ctx->tc_pem_objs, ctx->tc_n_pem_objs * sizeof( PK11GenericObject * ) );
999 ctx->tc_pem_objs[idx] = obj;
1003 tlsm_free_pem_objs( tlsm_ctx *ctx )
1005 /* free in reverse order of allocation */
1006 while ( ctx->tc_n_pem_objs-- ) {
1007 PK11_DestroyGenericObject( ctx->tc_pem_objs[ctx->tc_n_pem_objs] );
1008 ctx->tc_pem_objs[ctx->tc_n_pem_objs] = NULL;
1010 PORT_Free(ctx->tc_pem_objs);
1011 ctx->tc_pem_objs = NULL;
1012 ctx->tc_n_pem_objs = 0;
1016 tlsm_add_cert_from_file( tlsm_ctx *ctx, const char *filename, PRBool isca )
1019 PK11SlotInfo *slot = NULL;
1020 PK11GenericObject *rv;
1021 CK_ATTRIBUTE *attrs;
1022 CK_ATTRIBUTE theTemplate[20];
1023 CK_BBOOL cktrue = CK_TRUE;
1024 CK_BBOOL ckfalse = CK_FALSE;
1025 CK_OBJECT_CLASS objClass = CKO_CERTIFICATE;
1026 char tmpslotname[64];
1027 char *slotname = NULL;
1028 const char *ptr = NULL;
1029 char sep = PR_GetDirectorySeparator();
1033 memset( &fi, 0, sizeof(fi) );
1034 status = PR_GetFileInfo( filename, &fi );
1035 if ( PR_SUCCESS != status) {
1036 PRErrorCode errcode = PR_GetError();
1037 Debug( LDAP_DEBUG_ANY,
1038 "TLS: could not read certificate file %s - error %d:%s.\n",
1040 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1044 if ( fi.type != PR_FILE_FILE ) {
1045 PR_SetError(PR_IS_DIRECTORY_ERROR, 0);
1046 Debug( LDAP_DEBUG_ANY,
1047 "TLS: error: the certificate file %s is not a file.\n",
1052 attrs = theTemplate;
1055 slotID = 0; /* CA and trust objects use slot 0 */
1056 PR_snprintf( tmpslotname, sizeof(tmpslotname), TLSM_PEM_TOKEN_FMT, slotID );
1057 slotname = tmpslotname;
1059 if ( ctx->tc_slotname == NULL ) { /* need new slot */
1060 slotID = ++tlsm_slot_count;
1061 ctx->tc_slotname = PR_smprintf( TLSM_PEM_TOKEN_FMT, slotID );
1063 slotname = ctx->tc_slotname;
1065 if ( ( ptr = PL_strrchr( filename, sep ) ) ) {
1066 PL_strfree( ctx->tc_certname );
1068 ctx->tc_certname = PR_smprintf( "%s:%s", slotname, ptr );
1072 slot = PK11_FindSlotByName( slotname );
1075 PRErrorCode errcode = PR_GetError();
1076 Debug( LDAP_DEBUG_ANY,
1077 "TLS: could not find the slot for certificate %s - error %d:%s.\n",
1078 ctx->tc_certname, errcode,
1079 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1083 PK11_SETATTRS( attrs, CKA_CLASS, &objClass, sizeof(objClass) ); attrs++;
1084 PK11_SETATTRS( attrs, CKA_TOKEN, &cktrue, sizeof(CK_BBOOL) ); attrs++;
1085 PK11_SETATTRS( attrs, CKA_LABEL, (unsigned char *)filename, strlen(filename)+1 ); attrs++;
1087 PK11_SETATTRS( attrs, CKA_TRUST, &cktrue, sizeof(CK_BBOOL) ); attrs++;
1089 PK11_SETATTRS( attrs, CKA_TRUST, &ckfalse, sizeof(CK_BBOOL) ); attrs++;
1091 /* This loads the certificate in our PEM module into the appropriate
1094 rv = PK11_CreateGenericObject( slot, theTemplate, 4, PR_FALSE /* isPerm */ );
1096 PK11_FreeSlot( slot );
1099 PRErrorCode errcode = PR_GetError();
1100 Debug( LDAP_DEBUG_ANY,
1101 "TLS: could not add the certificate %s - error %d:%s.\n",
1102 ctx->tc_certname, errcode,
1103 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1107 tlsm_add_pem_obj( ctx, rv );
1113 tlsm_add_key_from_file( tlsm_ctx *ctx, const char *filename )
1116 PK11SlotInfo * slot = NULL;
1117 PK11GenericObject *rv;
1118 CK_ATTRIBUTE *attrs;
1119 CK_ATTRIBUTE theTemplate[20];
1120 CK_BBOOL cktrue = CK_TRUE;
1121 CK_OBJECT_CLASS objClass = CKO_PRIVATE_KEY;
1126 memset( &fi, 0, sizeof(fi) );
1127 status = PR_GetFileInfo( filename, &fi );
1128 if ( PR_SUCCESS != status) {
1129 PRErrorCode errcode = PR_GetError();
1130 Debug( LDAP_DEBUG_ANY,
1131 "TLS: could not read key file %s - error %d:%s.\n",
1133 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1137 if ( fi.type != PR_FILE_FILE ) {
1138 PR_SetError(PR_IS_DIRECTORY_ERROR, 0);
1139 Debug( LDAP_DEBUG_ANY,
1140 "TLS: error: the key file %s is not a file.\n",
1145 attrs = theTemplate;
1147 if ( ctx->tc_slotname == NULL ) { /* need new slot */
1148 slotID = ++tlsm_slot_count;
1149 ctx->tc_slotname = PR_smprintf( TLSM_PEM_TOKEN_FMT, slotID );
1151 slot = PK11_FindSlotByName( ctx->tc_slotname );
1154 PRErrorCode errcode = PR_GetError();
1155 Debug( LDAP_DEBUG_ANY,
1156 "TLS: could not find the slot %s for the private key - error %d:%s.\n",
1157 ctx->tc_slotname, errcode,
1158 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1162 PK11_SETATTRS( attrs, CKA_CLASS, &objClass, sizeof(objClass) ); attrs++;
1163 PK11_SETATTRS( attrs, CKA_TOKEN, &cktrue, sizeof(CK_BBOOL) ); attrs++;
1164 PK11_SETATTRS( attrs, CKA_LABEL, (unsigned char *)filename, strlen(filename)+1 ); attrs++;
1165 rv = PK11_CreateGenericObject( slot, theTemplate, 3, PR_FALSE /* isPerm */ );
1168 PRErrorCode errcode = PR_GetError();
1169 Debug( LDAP_DEBUG_ANY,
1170 "TLS: could not add the certificate %s - error %d:%s.\n",
1171 ctx->tc_certname, errcode,
1172 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1175 /* When adding an encrypted key the PKCS#11 will be set as removed */
1176 /* This will force the token to be seen as re-inserted */
1177 SECMOD_WaitForAnyTokenEvent( pem_module, 0, 0 );
1178 PK11_IsPresent( slot );
1182 PK11_FreeSlot( slot );
1185 tlsm_add_pem_obj( ctx, rv );
1191 tlsm_init_ca_certs( tlsm_ctx *ctx, const char *cacertfile, const char *cacertdir )
1193 PRBool isca = PR_TRUE;
1194 PRStatus status = PR_FAILURE;
1195 PRErrorCode errcode = PR_SUCCESS;
1197 if ( !cacertfile && !cacertdir ) {
1198 /* no checking - not good, but allowed */
1203 int rc = tlsm_add_cert_from_file( ctx, cacertfile, isca );
1205 errcode = PR_GetError();
1206 Debug( LDAP_DEBUG_ANY,
1207 "TLS: %s is not a valid CA certificate file - error %d:%s.\n",
1208 cacertfile, errcode,
1209 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1211 Debug( LDAP_DEBUG_TRACE,
1212 "TLS: loaded CA certificate file %s.\n",
1214 status = PR_SUCCESS; /* have at least one good CA - we can proceed */
1222 PRStatus fistatus = PR_FAILURE;
1224 memset( &fi, 0, sizeof(fi) );
1225 fistatus = PR_GetFileInfo( cacertdir, &fi );
1226 if ( PR_SUCCESS != fistatus) {
1227 errcode = PR_GetError();
1228 Debug( LDAP_DEBUG_ANY,
1229 "TLS: could not get info about the CA certificate directory %s - error %d:%s.\n",
1231 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1235 if ( fi.type != PR_FILE_DIRECTORY ) {
1236 Debug( LDAP_DEBUG_ANY,
1237 "TLS: error: the CA certificate directory %s is not a directory.\n",
1242 dir = PR_OpenDir( cacertdir );
1243 if ( NULL == dir ) {
1244 errcode = PR_GetError();
1245 Debug( LDAP_DEBUG_ANY,
1246 "TLS: could not open the CA certificate directory %s - error %d:%s.\n",
1248 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1253 entry = PR_ReadDir( dir, PR_SKIP_BOTH | PR_SKIP_HIDDEN );
1254 if ( ( NULL != entry ) && ( NULL != entry->name ) ) {
1255 char *fullpath = NULL;
1258 ptr = PL_strrstr( entry->name, PEM_CA_HASH_FILE_SUFFIX );
1259 if ( ( ptr == NULL ) || ( *(ptr + PEM_CA_HASH_FILE_SUFFIX_LEN) != '\0' ) ) {
1260 Debug( LDAP_DEBUG_TRACE,
1261 "TLS: file %s does not end in [%s] - does not appear to be a CA certificate "
1262 "directory file with a properly hashed file name - skipping.\n",
1263 entry->name, PEM_CA_HASH_FILE_SUFFIX, 0 );
1266 fullpath = PR_smprintf( "%s/%s", cacertdir, entry->name );
1267 if ( !tlsm_add_cert_from_file( ctx, fullpath, isca ) ) {
1268 Debug( LDAP_DEBUG_TRACE,
1269 "TLS: loaded CA certificate file %s from CA certificate directory %s.\n",
1270 fullpath, cacertdir, 0 );
1271 status = PR_SUCCESS; /* found at least 1 valid CA file in the dir */
1273 errcode = PR_GetError();
1274 Debug( LDAP_DEBUG_TRACE,
1275 "TLS: %s is not a valid CA certificate file - error %d:%s.\n",
1277 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1279 PR_smprintf_free( fullpath );
1281 } while ( NULL != entry );
1285 if ( status != PR_SUCCESS ) {
1286 const char *fmtstr = NULL;
1287 if ( cacertfile && cacertdir ) {
1288 fmtstr = "TLS: did not find any valid CA certificates in %s or %s\n";
1290 fmtstr = "TLS: did not find any valid CA certificates in %s%s\n";
1292 Debug( LDAP_DEBUG_ANY, fmtstr, cacertdir ? cacertdir : "",
1293 cacertfile ? cacertfile : "", 0 );
1301 * NSS supports having multiple cert/key databases in the same
1302 * directory, each one having a unique string prefix e.g.
1303 * slapd-01-cert8.db - the prefix here is "slapd-01-"
1304 * this function examines the given certdir - if it looks like
1305 * /path/to/directory/prefix it will return the
1306 * /path/to/directory part in realcertdir, and the prefix in prefix
1309 tlsm_get_certdb_prefix( const char *certdir, char **realcertdir, char **prefix )
1311 char sep = PR_GetDirectorySeparator();
1313 struct PRFileInfo prfi;
1316 *realcertdir = (char *)certdir; /* default is the one passed in */
1318 /* if certdir is not given, just return */
1323 prc = PR_GetFileInfo( certdir, &prfi );
1324 /* if certdir exists (file or directory) then it cannot specify a prefix */
1325 if ( prc == PR_SUCCESS ) {
1329 /* if certdir was given, and there is a '/' in certdir, see if there
1330 is anything after the last '/' - if so, assume it is the prefix */
1331 if ( ( ( ptr = strrchr( certdir, sep ) ) ) && *(ptr+1) ) {
1332 *realcertdir = PL_strndup( certdir, ptr-certdir );
1333 *prefix = PL_strdup( ptr+1 );
1340 * This is the part of the init we defer until we get the
1341 * actual security configuration information. This is
1342 * only called once, protected by a PRCallOnce
1343 * NOTE: This must be done before the first call to SSL_ImportFD,
1344 * especially the setting of the policy
1345 * NOTE: This must be called after fork()
1348 tlsm_deferred_init( void *arg )
1350 tlsm_ctx *ctx = (tlsm_ctx *)arg;
1351 struct ldaptls *lt = ctx->tc_config;
1352 const char *securitydirs[3];
1355 PRErrorCode errcode = 1;
1356 #ifdef HAVE_NSS_INITCONTEXT
1357 NSSInitParameters initParams;
1358 NSSInitContext *initctx = NULL;
1363 #ifdef HAVE_NSS_INITCONTEXT
1364 memset( &initParams, 0, sizeof( initParams ) );
1365 initParams.length = sizeof( initParams );
1366 #endif /* HAVE_NSS_INITCONTEXT */
1368 #ifndef HAVE_NSS_INITCONTEXT
1369 if ( !NSS_IsInitialized() ) {
1370 #endif /* HAVE_NSS_INITCONTEXT */
1372 MOZNSS_DIR will override everything else - you can
1373 always set MOZNSS_DIR to force the use of this
1375 If using MOZNSS, specify the location of the moznss db dir
1376 in the cacertdir directive of the OpenLDAP configuration.
1377 DEFAULT_MOZNSS_DIR will only be used if the code cannot
1378 find a security dir to use based on the current
1382 securitydirs[nn++] = PR_GetEnv( "MOZNSS_DIR" );
1383 securitydirs[nn++] = lt->lt_cacertdir;
1384 securitydirs[nn++] = PR_GetEnv( "DEFAULT_MOZNSS_DIR" );
1385 for ( ii = 0; !done && ( ii < nn ); ++ii ) {
1386 char *realcertdir = NULL;
1387 const char *defprefix = "";
1388 char *prefix = (char *)defprefix;
1389 const char *securitydir = securitydirs[ii];
1390 if ( NULL == securitydir ) {
1394 tlsm_get_certdb_prefix( securitydir, &realcertdir, &prefix );
1395 #ifdef HAVE_NSS_INITCONTEXT
1396 #ifdef INITCONTEXT_HACK
1397 if ( !NSS_IsInitialized() && ctx->tc_is_server ) {
1398 rc = NSS_Initialize( realcertdir, prefix, prefix, SECMOD_DB, NSS_INIT_READONLY );
1400 initctx = NSS_InitContext( realcertdir, prefix, prefix, SECMOD_DB,
1401 &initParams, NSS_INIT_READONLY );
1402 rc = (initctx == NULL) ? SECFailure : SECSuccess;
1405 initctx = NSS_InitContext( realcertdir, prefix, prefix, SECMOD_DB,
1406 &initParams, NSS_INIT_READONLY );
1407 rc = (initctx == NULL) ? SECFailure : SECSuccess;
1410 rc = NSS_Initialize( realcertdir, prefix, prefix, SECMOD_DB, NSS_INIT_READONLY );
1413 if ( rc != SECSuccess ) {
1414 errcode = PORT_GetError();
1415 if ( securitydirs[ii] != lt->lt_cacertdir) {
1416 Debug( LDAP_DEBUG_TRACE,
1417 "TLS: could not initialize moznss using security dir %s prefix %s - error %d.\n",
1418 realcertdir, prefix, errcode );
1422 Debug( LDAP_DEBUG_TRACE, "TLS: using moznss security dir %s prefix %s.\n",
1423 realcertdir, prefix, 0 );
1427 if ( realcertdir != securitydir ) {
1428 PL_strfree( realcertdir );
1430 if ( prefix != defprefix ) {
1431 PL_strfree( prefix );
1435 if ( errcode ) { /* no moznss db found, or not using moznss db */
1436 #ifdef HAVE_NSS_INITCONTEXT
1437 int flags = NSS_INIT_READONLY|NSS_INIT_NOCERTDB|NSS_INIT_NOMODDB;
1438 #ifdef INITCONTEXT_HACK
1439 if ( !NSS_IsInitialized() && ctx->tc_is_server ) {
1440 rc = NSS_NoDB_Init( NULL );
1442 initctx = NSS_InitContext( "", "", "", SECMOD_DB,
1443 &initParams, flags );
1444 rc = (initctx == NULL) ? SECFailure : SECSuccess;
1447 initctx = NSS_InitContext( "", "", "", SECMOD_DB,
1448 &initParams, flags );
1449 rc = (initctx == NULL) ? SECFailure : SECSuccess;
1452 rc = NSS_NoDB_Init( NULL );
1454 if ( rc != SECSuccess ) {
1455 errcode = PORT_GetError();
1456 Debug( LDAP_DEBUG_ANY,
1457 "TLS: could not initialize moznss - error %d:%s.\n",
1458 errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ), 0 );
1462 #ifdef HAVE_NSS_INITCONTEXT
1463 ctx->tc_initctx = initctx;
1466 /* initialize the PEM module */
1467 if ( tlsm_init_pem_module() ) {
1468 errcode = PORT_GetError();
1469 Debug( LDAP_DEBUG_ANY,
1470 "TLS: could not initialize moznss PEM module - error %d:%s.\n",
1471 errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ), 0 );
1475 if ( tlsm_init_ca_certs( ctx, lt->lt_cacertfile, lt->lt_cacertdir ) ) {
1476 /* if we tried to use lt->lt_cacertdir as an NSS key/cert db, errcode
1477 will be a value other than 1 - print an error message so that the
1478 user will know that failed too */
1479 if ( ( errcode != 1 ) && ( lt->lt_cacertdir ) ) {
1480 char *realcertdir = NULL;
1481 char *prefix = NULL;
1482 tlsm_get_certdb_prefix( lt->lt_cacertdir, &realcertdir, &prefix );
1483 Debug( LDAP_DEBUG_TRACE,
1484 "TLS: could not initialize moznss using security dir %s prefix %s - error %d.\n",
1485 realcertdir, prefix ? prefix : "", errcode );
1486 if ( realcertdir != lt->lt_cacertdir ) {
1487 PL_strfree( realcertdir );
1489 PL_strfree( prefix );
1494 ctx->tc_using_pem = PR_TRUE;
1497 #ifdef HAVE_NSS_INITCONTEXT
1498 if ( !ctx->tc_initctx ) {
1499 ctx->tc_initctx = initctx;
1503 NSS_SetDomesticPolicy();
1505 PK11_SetPasswordFunc( tlsm_pin_prompt );
1507 /* register cleanup function */
1508 /* delete the old one, if any */
1509 NSS_UnregisterShutdown( tlsm_nss_shutdown_cb, NULL );
1510 NSS_RegisterShutdown( tlsm_nss_shutdown_cb, NULL );
1512 #ifndef HAVE_NSS_INITCONTEXT
1514 #endif /* HAVE_NSS_INITCONTEXT */
1520 tlsm_authenticate( tlsm_ctx *ctx, const char *certname, const char *pininfo )
1522 const char *colon = NULL;
1523 char *token_name = NULL;
1524 PK11SlotInfo *slot = NULL;
1527 if ( !certname || !*certname ) {
1531 if ( ( colon = PL_strchr( certname, ':' ) ) ) {
1532 token_name = PL_strndup( certname, colon-certname );
1536 slot = PK11_FindSlotByName( token_name );
1538 slot = PK11_GetInternalKeySlot();
1542 PRErrorCode errcode = PR_GetError();
1543 Debug( LDAP_DEBUG_ANY,
1544 "TLS: could not find the slot for security token %s - error %d:%s.\n",
1545 token_name ? token_name : DEFAULT_TOKEN_NAME, errcode,
1546 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1550 rc = tlsm_authenticate_to_slot( ctx, slot );
1553 PL_strfree( token_name );
1555 PK11_FreeSlot( slot );
1562 * Find and verify the certificate.
1563 * Either fd is given, in which case the cert will be taken from it via SSL_PeerCertificate
1564 * or certname is given, and it will be searched for by name
1567 tlsm_find_and_verify_cert_key(tlsm_ctx *ctx, PRFileDesc *fd, const char *certname, int isServer, CERTCertificate **pRetCert, SECKEYPrivateKey **pRetKey)
1569 CERTCertificate *cert = NULL;
1571 void *pin_arg = NULL;
1572 SECKEYPrivateKey *key = NULL;
1574 pin_arg = SSL_RevealPinArg( fd );
1576 cert = PK11_FindCertFromNickname( certname, pin_arg );
1578 PRErrorCode errcode = PR_GetError();
1579 Debug( LDAP_DEBUG_ANY,
1580 "TLS: error: the certificate %s could not be found in the database - error %d:%s\n",
1581 certname, errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1585 /* we are verifying the peer cert
1586 we also need to swap the isServer meaning */
1587 cert = SSL_PeerCertificate( fd );
1589 PRErrorCode errcode = PR_GetError();
1590 Debug( LDAP_DEBUG_ANY,
1591 "TLS: error: could not get the certificate from the peer connection - error %d:%s\n",
1592 errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ), NULL );
1595 isServer = !isServer; /* verify the peer's cert instead */
1598 if ( ctx->tc_slotname ) {
1599 PK11SlotInfo *slot = PK11_FindSlotByName( ctx->tc_slotname );
1600 key = PK11_FindPrivateKeyFromCert( slot, cert, NULL );
1601 PK11_FreeSlot( slot );
1603 key = PK11_FindKeyByAnyCert( cert, pin_arg );
1607 SECCertificateUsage certUsage;
1608 PRBool checkSig = PR_TRUE;
1612 *pRetKey = key; /* caller will deal with this */
1614 SECKEY_DestroyPrivateKey( key );
1617 certUsage = certificateUsageSSLServer;
1619 certUsage = certificateUsageSSLClient;
1621 if ( ctx->tc_verify_cert ) {
1624 checkSig = PR_FALSE;
1626 status = CERT_VerifyCertificateNow( ctx->tc_certdb, cert,
1627 checkSig, certUsage,
1629 if ( status != SECSuccess ) {
1630 /* NSS doesn't like self-signed CA certs that are also used for
1631 TLS/SSL server certs (such as generated by openssl req -x509)
1632 CERT_VerifyCertificateNow returns SEC_ERROR_UNTRUSTED_ISSUER in that case
1633 so, see if the cert and issuer are the same cert
1635 PRErrorCode errcode = PR_GetError();
1637 if ( errcode == SEC_ERROR_UNTRUSTED_ISSUER ) {
1638 CERTCertificate *issuer = CERT_FindCertIssuer( cert, PR_Now(), certUsageSSLServer );
1639 if ( NULL == issuer ) {
1640 /* no issuer - fail */
1641 Debug( LDAP_DEBUG_ANY,
1642 "TLS: error: the server certificate %s has no issuer - "
1643 "please check this certificate for validity\n",
1645 } else if ( CERT_CompareCerts( cert, issuer ) ) {
1646 /* self signed - warn and allow */
1647 status = SECSuccess;
1649 Debug( LDAP_DEBUG_ANY,
1650 "TLS: warning: using self-signed server certificate %s\n",
1653 CERT_DestroyCertificate( issuer );
1656 if ( status != SECSuccess ) {
1657 Debug( LDAP_DEBUG_ANY,
1658 "TLS: error: the certificate %s is not valid - error %d:%s\n",
1659 certname, errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1662 rc = 0; /* success */
1665 PRErrorCode errcode = PR_GetError();
1666 Debug( LDAP_DEBUG_ANY,
1667 "TLS: error: could not find the private key for certificate %s - error %d:%s\n",
1668 certname, errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1672 *pRetCert = cert; /* caller will deal with this */
1674 CERT_DestroyCertificate( cert );
1681 tlsm_get_client_auth_data( void *arg, PRFileDesc *fd,
1682 CERTDistNames *caNames, CERTCertificate **pRetCert,
1683 SECKEYPrivateKey **pRetKey )
1685 tlsm_ctx *ctx = (tlsm_ctx *)arg;
1688 /* don't need caNames - this function will call CERT_VerifyCertificateNow
1689 which will verify the cert against the known CAs */
1690 rc = tlsm_find_and_verify_cert_key( ctx, fd, ctx->tc_certname, 0, pRetCert, pRetKey );
1692 Debug( LDAP_DEBUG_ANY,
1693 "TLS: error: unable to perform client certificate authentication for "
1694 "certificate named %s\n", ctx->tc_certname, 0, 0 );
1702 * ctx must have a tc_model that is valid
1703 * certname is in the form [<tokenname>:]<certnickname>
1704 * where <tokenname> is the name of the PKCS11 token
1705 * and <certnickname> is the nickname of the cert/key in
1709 tlsm_clientauth_init( tlsm_ctx *ctx )
1711 SECStatus status = SECFailure;
1714 rc = tlsm_find_and_verify_cert_key( ctx, ctx->tc_model, ctx->tc_certname, 0, NULL, NULL );
1716 Debug( LDAP_DEBUG_ANY,
1717 "TLS: error: unable to set up client certificate authentication for "
1718 "certificate named %s\n", ctx->tc_certname, 0, 0 );
1722 status = SSL_GetClientAuthDataHook( ctx->tc_model,
1723 tlsm_get_client_auth_data,
1726 return ( status == SECSuccess ? 0 : -1 );
1730 * Tear down the TLS subsystem. Should only be called once.
1733 tlsm_destroy( void )
1738 tlsm_ctx_new ( struct ldapoptions *lo )
1742 ctx = LDAP_MALLOC( sizeof (*ctx) );
1745 #ifdef LDAP_R_COMPILE
1746 ldap_pvt_thread_mutex_init( &ctx->tc_refmutex );
1748 ctx->tc_config = &lo->ldo_tls_info; /* pointer into lo structure - must have global scope and must not go away before we can do real init */
1749 ctx->tc_certdb = NULL;
1750 ctx->tc_certname = NULL;
1751 ctx->tc_pin_file = NULL;
1752 ctx->tc_model = NULL;
1753 memset(&ctx->tc_callonce, 0, sizeof(ctx->tc_callonce));
1754 ctx->tc_require_cert = lo->ldo_tls_require_cert;
1755 ctx->tc_verify_cert = PR_FALSE;
1756 ctx->tc_using_pem = PR_FALSE;
1757 ctx->tc_slotname = NULL;
1758 #ifdef HAVE_NSS_INITCONTEXT
1759 ctx->tc_initctx = NULL;
1760 #endif /* HAVE_NSS_INITCONTEXT */
1761 ctx->tc_pem_objs = NULL;
1762 ctx->tc_n_pem_objs = 0;
1764 return (tls_ctx *)ctx;
1768 tlsm_ctx_ref( tls_ctx *ctx )
1770 tlsm_ctx *c = (tlsm_ctx *)ctx;
1771 LDAP_MUTEX_LOCK( &c->tc_refmutex );
1773 LDAP_MUTEX_UNLOCK( &c->tc_refmutex );
1777 tlsm_ctx_free ( tls_ctx *ctx )
1779 tlsm_ctx *c = (tlsm_ctx *)ctx;
1784 LDAP_MUTEX_LOCK( &c->tc_refmutex );
1785 refcount = --c->tc_refcnt;
1786 LDAP_MUTEX_UNLOCK( &c->tc_refmutex );
1790 PR_Close( c->tc_model );
1791 c->tc_certdb = NULL; /* if not the default, may have to clean up */
1792 PL_strfree( c->tc_certname );
1793 c->tc_certname = NULL;
1794 PL_strfree( c->tc_pin_file );
1795 c->tc_pin_file = NULL;
1796 PL_strfree( c->tc_slotname );
1797 tlsm_free_pem_objs( c );
1798 #ifdef HAVE_NSS_INITCONTEXT
1800 NSS_ShutdownContext( c->tc_initctx );
1801 c->tc_initctx = NULL;
1802 #endif /* HAVE_NSS_INITCONTEXT */
1803 #ifdef LDAP_R_COMPILE
1804 ldap_pvt_thread_mutex_destroy( &c->tc_refmutex );
1810 * initialize a new TLS context
1813 tlsm_ctx_init( struct ldapoptions *lo, struct ldaptls *lt, int is_server )
1815 tlsm_ctx *ctx = (tlsm_ctx *)lo->ldo_tls_ctx;
1816 ctx->tc_is_server = is_server;
1822 tlsm_deferred_ctx_init( void *arg )
1824 tlsm_ctx *ctx = (tlsm_ctx *)arg;
1825 PRBool sslv2 = PR_FALSE;
1826 PRBool sslv3 = PR_TRUE;
1827 PRBool tlsv1 = PR_TRUE;
1828 PRBool request_cert = PR_FALSE;
1829 PRInt32 require_cert = PR_FALSE;
1833 if ( tlsm_deferred_init( ctx ) ) {
1834 Debug( LDAP_DEBUG_ANY,
1835 "TLS: could perform TLS system initialization.\n",
1840 ctx->tc_certdb = CERT_GetDefaultCertDB(); /* If there is ever a per-context db, change this */
1842 fd = PR_CreateIOLayerStub( tlsm_layer_id, &tlsm_PR_methods );
1844 ctx->tc_model = SSL_ImportFD( NULL, fd );
1847 if ( !ctx->tc_model ) {
1848 PRErrorCode err = PR_GetError();
1849 Debug( LDAP_DEBUG_ANY,
1850 "TLS: could perform TLS socket I/O layer initialization - error %d:%s.\n",
1851 err, PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ), NULL );
1859 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_SECURITY, PR_TRUE ) ) {
1860 Debug( LDAP_DEBUG_ANY,
1861 "TLS: could not set secure mode on.\n",
1866 lt = ctx->tc_config;
1868 /* default is sslv3 and tlsv1 */
1869 if ( lt->lt_protocol_min ) {
1870 if ( lt->lt_protocol_min > LDAP_OPT_X_TLS_PROTOCOL_SSL3 ) {
1872 } else if ( lt->lt_protocol_min <= LDAP_OPT_X_TLS_PROTOCOL_SSL2 ) {
1874 Debug( LDAP_DEBUG_ANY,
1875 "TLS: warning: minimum TLS protocol level set to "
1876 "include SSLv2 - SSLv2 is insecure - do not use\n", 0, 0, 0 );
1879 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_ENABLE_SSL2, sslv2 ) ) {
1880 Debug( LDAP_DEBUG_ANY,
1881 "TLS: could not set SSLv2 mode on.\n",
1885 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_ENABLE_SSL3, sslv3 ) ) {
1886 Debug( LDAP_DEBUG_ANY,
1887 "TLS: could not set SSLv3 mode on.\n",
1891 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_ENABLE_TLS, tlsv1 ) ) {
1892 Debug( LDAP_DEBUG_ANY,
1893 "TLS: could not set TLSv1 mode on.\n",
1898 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_HANDSHAKE_AS_CLIENT, !ctx->tc_is_server ) ) {
1899 Debug( LDAP_DEBUG_ANY,
1900 "TLS: could not set handshake as client.\n",
1904 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_HANDSHAKE_AS_SERVER, ctx->tc_is_server ) ) {
1905 Debug( LDAP_DEBUG_ANY,
1906 "TLS: could not set handshake as server.\n",
1911 if ( lt->lt_ciphersuite &&
1912 tlsm_parse_ciphers( ctx, lt->lt_ciphersuite )) {
1913 Debug( LDAP_DEBUG_ANY,
1914 "TLS: could not set cipher list %s.\n",
1915 lt->lt_ciphersuite, 0, 0 );
1919 if ( ctx->tc_require_cert ) {
1920 request_cert = PR_TRUE;
1921 require_cert = SSL_REQUIRE_NO_ERROR;
1922 if ( ctx->tc_require_cert == LDAP_OPT_X_TLS_DEMAND ||
1923 ctx->tc_require_cert == LDAP_OPT_X_TLS_HARD ) {
1924 require_cert = SSL_REQUIRE_ALWAYS;
1926 if ( ctx->tc_require_cert != LDAP_OPT_X_TLS_ALLOW )
1927 ctx->tc_verify_cert = PR_TRUE;
1929 ctx->tc_verify_cert = PR_FALSE;
1932 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_REQUEST_CERTIFICATE, request_cert ) ) {
1933 Debug( LDAP_DEBUG_ANY,
1934 "TLS: could not set request certificate mode.\n",
1939 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_REQUIRE_CERTIFICATE, require_cert ) ) {
1940 Debug( LDAP_DEBUG_ANY,
1941 "TLS: could not set require certificate mode.\n",
1946 /* set up our cert and key, if any */
1947 if ( lt->lt_certfile ) {
1948 /* if using the PEM module, load the PEM file specified by lt_certfile */
1949 /* otherwise, assume this is the name of a cert already in the db */
1950 if ( ctx->tc_using_pem ) {
1951 /* this sets ctx->tc_certname to the correct value */
1952 int rc = tlsm_add_cert_from_file( ctx, lt->lt_certfile, PR_FALSE /* not a ca */ );
1957 PL_strfree( ctx->tc_certname );
1958 ctx->tc_certname = PL_strdup( lt->lt_certfile );
1962 if ( lt->lt_keyfile ) {
1963 /* if using the PEM module, load the PEM file specified by lt_keyfile */
1964 /* otherwise, assume this is the pininfo for the key */
1965 if ( ctx->tc_using_pem ) {
1966 /* this sets ctx->tc_certname to the correct value */
1967 int rc = tlsm_add_key_from_file( ctx, lt->lt_keyfile );
1972 PL_strfree( ctx->tc_pin_file );
1973 ctx->tc_pin_file = PL_strdup( lt->lt_keyfile );
1977 /* Set up callbacks for use by clients */
1978 if ( !ctx->tc_is_server ) {
1979 if ( SSL_OptionSet( ctx->tc_model, SSL_NO_CACHE, PR_TRUE ) != SECSuccess ) {
1980 PRErrorCode err = PR_GetError();
1981 Debug( LDAP_DEBUG_ANY,
1982 "TLS: error: could not set nocache option for moznss - error %d:%s\n",
1983 err, PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ), NULL );
1987 if ( SSL_BadCertHook( ctx->tc_model, tlsm_bad_cert_handler, ctx ) != SECSuccess ) {
1988 PRErrorCode err = PR_GetError();
1989 Debug( LDAP_DEBUG_ANY,
1990 "TLS: error: could not set bad cert handler for moznss - error %d:%s\n",
1991 err, PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ), NULL );
1996 since a cert has been specified, assume the client wants to do cert auth
1998 if ( ctx->tc_certname ) {
1999 if ( tlsm_authenticate( ctx, ctx->tc_certname, ctx->tc_pin_file ) ) {
2000 Debug( LDAP_DEBUG_ANY,
2001 "TLS: error: unable to authenticate to the security device for certificate %s\n",
2002 ctx->tc_certname, 0, 0 );
2005 if ( tlsm_clientauth_init( ctx ) ) {
2006 Debug( LDAP_DEBUG_ANY,
2007 "TLS: error: unable to set up client certificate authentication using %s\n",
2008 ctx->tc_certname, 0, 0 );
2012 } else { /* set up secure server */
2014 CERTCertificate *serverCert;
2015 SECKEYPrivateKey *serverKey;
2018 /* must have a certificate for the server to use */
2019 if ( !ctx->tc_certname ) {
2020 Debug( LDAP_DEBUG_ANY,
2021 "TLS: error: no server certificate: must specify a certificate for the server to use\n",
2026 /* authenticate to the server's token - this will do nothing
2027 if the key/cert db is not password protected */
2028 if ( tlsm_authenticate( ctx, ctx->tc_certname, ctx->tc_pin_file ) ) {
2029 Debug( LDAP_DEBUG_ANY,
2030 "TLS: error: unable to authenticate to the security device for certificate %s\n",
2031 ctx->tc_certname, 0, 0 );
2035 /* get the server's key and cert */
2036 if ( tlsm_find_and_verify_cert_key( ctx, ctx->tc_model, ctx->tc_certname, ctx->tc_is_server,
2037 &serverCert, &serverKey ) ) {
2038 Debug( LDAP_DEBUG_ANY,
2039 "TLS: error: unable to find and verify server's cert and key for certificate %s\n",
2040 ctx->tc_certname, 0, 0 );
2044 certKEA = NSS_FindCertKEAType( serverCert );
2045 /* configure the socket to be a secure server socket */
2046 status = SSL_ConfigSecureServer( ctx->tc_model, serverCert, serverKey, certKEA );
2047 /* SSL_ConfigSecureServer copies these */
2048 CERT_DestroyCertificate( serverCert );
2049 SECKEY_DestroyPrivateKey( serverKey );
2051 if ( SECSuccess != status ) {
2052 PRErrorCode err = PR_GetError();
2053 Debug( LDAP_DEBUG_ANY,
2054 "TLS: error: unable to configure secure server using certificate %s - error %d:%s\n",
2055 ctx->tc_certname, err, PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ) );
2060 /* Callback for authenticating certificate */
2061 if ( SSL_AuthCertificateHook( ctx->tc_model, tlsm_auth_cert_handler,
2062 ctx->tc_certdb ) != SECSuccess ) {
2063 PRErrorCode err = PR_GetError();
2064 Debug( LDAP_DEBUG_ANY,
2065 "TLS: error: could not set auth cert handler for moznss - error %d:%s\n",
2066 err, PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ), NULL );
2070 if ( SSL_HandshakeCallback( ctx->tc_model, tlsm_handshake_complete_cb, ctx ) ) {
2071 PRErrorCode err = PR_GetError();
2072 Debug( LDAP_DEBUG_ANY,
2073 "TLS: error: could not set handshake callback for moznss - error %d:%s\n",
2074 err, PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ), NULL );
2082 tlsm_session *session;
2083 Sockbuf_IO_Desc *sbiod;
2084 /* there seems to be no portable way to determine if the
2085 sockbuf sd has been set to nonblocking mode - the
2086 call to ber_pvt_socket_set_nonblock() takes place
2087 before the tls socket is set up, so we cannot
2088 intercept that call either.
2089 On systems where fcntl is available, we can just
2090 F_GETFL and test for O_NONBLOCK. On other systems,
2091 we will just see if the IO op returns EAGAIN or EWOULDBLOCK,
2092 and just set this flag */
2095 * NSS tries hard to be backwards compatible with SSLv2 clients, or
2096 * clients that send an SSLv2 client hello. This message is not
2097 * tagged in any way, so NSS has no way to know if the incoming
2098 * message is a valid SSLv2 client hello or just some bogus data
2099 * (or cleartext LDAP). We store the first byte read from the
2100 * client here. The most common case will be a client sending
2101 * LDAP data instead of SSL encrypted LDAP data. This can happen,
2102 * for example, if using ldapsearch -Z - if the starttls fails,
2103 * the client will fallback to plain cleartext LDAP. So if we
2104 * see that the firstbyte is a valid LDAP tag, we can be
2105 * pretty sure this is happening.
2109 * NSS doesn't return SSL_ERROR_WANT_READ, SSL_ERROR_WANT_WRITE, etc.
2110 * when it is blocked, so we have to set a flag in the wrapped send
2111 * and recv calls that tells us what operation NSS was last blocked
2115 #define TLSM_WRITE 2
2119 static struct tls_data *
2120 tlsm_get_pvt_tls_data( PRFileDesc *fd )
2129 myfd = PR_GetIdentitiesLayer( fd, tlsm_layer_id );
2135 p = (struct tls_data *)myfd->secret;
2141 tlsm_is_non_ssl_message( PRFileDesc *fd, ber_tag_t *thebyte )
2146 *thebyte = LBER_DEFAULT;
2149 p = tlsm_get_pvt_tls_data( fd );
2150 if ( p == NULL || p->sbiod == NULL ) {
2154 if ( p->firsttag == LBER_SEQUENCE ) {
2156 *thebyte = p->firsttag;
2164 static tls_session *
2165 tlsm_session_new ( tls_ctx * ctx, int is_server )
2167 tlsm_ctx *c = (tlsm_ctx *)ctx;
2168 tlsm_session *session;
2173 c->tc_is_server = is_server;
2174 status = PR_CallOnceWithArg( &c->tc_callonce, tlsm_deferred_ctx_init, c );
2175 if ( PR_SUCCESS != status ) {
2176 PRErrorCode err = PR_GetError();
2177 Debug( LDAP_DEBUG_ANY,
2178 "TLS: error: could not initialize moznss security context - error %d:%s\n",
2179 err, PR_ErrorToString(err, PR_LANGUAGE_I_DEFAULT), NULL );
2183 fd = PR_CreateIOLayerStub( tlsm_layer_id, &tlsm_PR_methods );
2188 session = SSL_ImportFD( c->tc_model, fd );
2195 /* 0 means use the defaults here */
2196 SSL_ConfigServerSessionIDCache( 0, 0, 0, NULL );
2199 rc = SSL_ResetHandshake( session, is_server );
2201 PRErrorCode err = PR_GetError();
2202 Debug( LDAP_DEBUG_TRACE,
2203 "TLS: error: new session - reset handshake failure %d - error %d:%s\n",
2205 err ? PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ) : "unknown" );
2207 PR_Close( session );
2211 return (tls_session *)session;
2215 tlsm_session_accept_or_connect( tls_session *session, int is_accept )
2217 tlsm_session *s = (tlsm_session *)session;
2218 int rc = SSL_ForceHandshake( s );
2219 const char *op = is_accept ? "accept" : "connect";
2222 PRErrorCode err = PR_GetError();
2224 if ( err == PR_WOULD_BLOCK_ERROR ) {
2225 ber_tag_t thetag = LBER_DEFAULT;
2226 /* see if we are blocked because of a bogus packet */
2227 if ( tlsm_is_non_ssl_message( s, &thetag ) ) { /* see if we received a non-SSL message */
2228 Debug( LDAP_DEBUG_ANY,
2229 "TLS: error: %s - error - received non-SSL message [0x%x]\n",
2230 op, (unsigned int)thetag, 0 );
2231 /* reset error to something more descriptive */
2232 PR_SetError( SSL_ERROR_RX_MALFORMED_HELLO_REQUEST, EPROTO );
2235 Debug( LDAP_DEBUG_ANY,
2236 "TLS: error: %s - force handshake failure: errno %d - moznss error %d\n",
2244 tlsm_session_accept( tls_session *session )
2246 return tlsm_session_accept_or_connect( session, 1 );
2250 tlsm_session_connect( LDAP *ld, tls_session *session )
2252 return tlsm_session_accept_or_connect( session, 0 );
2256 tlsm_session_upflags( Sockbuf *sb, tls_session *session, int rc )
2258 int prerror = PR_GetError();
2260 if ( ( prerror == PR_PENDING_INTERRUPT_ERROR ) || ( prerror == PR_WOULD_BLOCK_ERROR ) ) {
2261 tlsm_session *s = (tlsm_session *)session;
2262 struct tls_data *p = tlsm_get_pvt_tls_data( s );
2264 if ( p && ( p->io_flag == TLSM_READ ) ) {
2265 sb->sb_trans_needs_read = 1;
2267 } else if ( p && ( p->io_flag == TLSM_WRITE ) ) {
2268 sb->sb_trans_needs_write = 1;
2277 tlsm_session_errmsg( tls_session *sess, int rc, char *buf, size_t len )
2282 i = PR_GetErrorTextLength();
2284 char *msg = LDAP_MALLOC( i+1 );
2285 PR_GetErrorText( msg );
2286 memcpy( buf, msg, len );
2289 PR_GetErrorText( buf );
2292 return i ? buf : NULL;
2296 tlsm_session_my_dn( tls_session *session, struct berval *der_dn )
2298 tlsm_session *s = (tlsm_session *)session;
2299 CERTCertificate *cert;
2301 cert = SSL_LocalCertificate( s );
2302 if (!cert) return LDAP_INVALID_CREDENTIALS;
2304 der_dn->bv_val = (char *)cert->derSubject.data;
2305 der_dn->bv_len = cert->derSubject.len;
2306 CERT_DestroyCertificate( cert );
2311 tlsm_session_peer_dn( tls_session *session, struct berval *der_dn )
2313 tlsm_session *s = (tlsm_session *)session;
2314 CERTCertificate *cert;
2316 cert = SSL_PeerCertificate( s );
2317 if (!cert) return LDAP_INVALID_CREDENTIALS;
2319 der_dn->bv_val = (char *)cert->derSubject.data;
2320 der_dn->bv_len = cert->derSubject.len;
2321 CERT_DestroyCertificate( cert );
2325 /* what kind of hostname were we given? */
2331 tlsm_session_chkhost( LDAP *ld, tls_session *session, const char *name_in )
2333 tlsm_session *s = (tlsm_session *)session;
2334 CERTCertificate *cert;
2335 const char *name, *domain = NULL, *ptr;
2336 int ret, ntype = IS_DNS, nlen, dlen;
2337 #ifdef LDAP_PF_INET6
2338 struct in6_addr addr;
2340 struct in_addr addr;
2345 if( ldap_int_hostname &&
2346 ( !name_in || !strcasecmp( name_in, "localhost" ) ) )
2348 name = ldap_int_hostname;
2352 nlen = strlen( name );
2354 cert = SSL_PeerCertificate( s );
2356 Debug( LDAP_DEBUG_ANY,
2357 "TLS: unable to get peer certificate.\n",
2359 /* if this was a fatal condition, things would have
2360 * aborted long before now.
2362 return LDAP_SUCCESS;
2365 #ifdef LDAP_PF_INET6
2366 if (name[0] == '[' && strchr(name, ']')) {
2367 char *n2 = ldap_strdup(name+1);
2368 *strchr(n2, ']') = 0;
2369 if (inet_pton(AF_INET6, n2, &addr))
2374 if ((ptr = strrchr(name, '.')) && isdigit((unsigned char)ptr[1])) {
2375 if (inet_aton(name, (struct in_addr *)&addr)) ntype = IS_IP4;
2377 if (ntype == IS_DNS ) {
2378 domain = strchr( name, '.' );
2380 dlen = nlen - ( domain - name );
2383 ret = LDAP_LOCAL_ERROR;
2385 rv = CERT_FindCertExtension( cert, SEC_OID_X509_SUBJECT_ALT_NAME,
2387 if ( rv == SECSuccess && altname.data ) {
2389 CERTGeneralName *names, *cur;
2391 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
2393 ret = LDAP_NO_MEMORY;
2397 names = cur = CERT_DecodeAltNameExtension(arena, &altname);
2406 if ( !cur->name.other.len ) continue;
2408 host = (char *)cur->name.other.data;
2409 hlen = cur->name.other.len;
2411 if ( cur->type == certDNSName ) {
2412 if ( ntype != IS_DNS ) continue;
2414 /* is this an exact match? */
2415 if ( nlen == hlen && !strncasecmp( name, host, nlen )) {
2420 /* is this a wildcard match? */
2421 if ( domain && host[0] == '*' && host[1] == '.' &&
2422 dlen == hlen-1 && !strncasecmp( domain, host+1, dlen )) {
2426 } else if ( cur->type == certIPAddress ) {
2427 if ( ntype == IS_DNS ) continue;
2429 #ifdef LDAP_PF_INET6
2430 if (ntype == IS_IP6 && hlen != sizeof(struct in6_addr)) {
2434 if (ntype == IS_IP4 && hlen != sizeof(struct in_addr)) {
2437 if (!memcmp(host, &addr, hlen)) {
2442 } while (( cur = CERT_GetNextGeneralName( cur )) != names );
2444 PORT_FreeArena( arena, PR_FALSE );
2445 SECITEM_FreeItem( &altname, PR_FALSE );
2447 /* no altnames matched, try the CN */
2448 if ( ret != LDAP_SUCCESS ) {
2449 /* find the last CN */
2450 CERTRDN *rdn, **rdns;
2451 CERTAVA *lastava = NULL;
2455 rdns = cert->subject.rdns;
2456 while ( rdns && ( rdn = *rdns++ )) {
2457 CERTAVA *ava, **avas = rdn->avas;
2458 while ( avas && ( ava = *avas++ )) {
2459 if ( CERT_GetAVATag( ava ) == SEC_OID_AVA_COMMON_NAME )
2464 SECItem *av = CERT_DecodeAVAValue( &lastava->value );
2466 if ( av->len == nlen && !strncasecmp( name, (char *)av->data, nlen )) {
2468 } else if ( av->data[0] == '*' && av->data[1] == '.' &&
2469 domain && dlen == av->len - 1 && !strncasecmp( name,
2470 (char *)(av->data+1), dlen )) {
2474 if ( len >= sizeof(buf) )
2475 len = sizeof(buf)-1;
2476 memcpy( buf, av->data, len );
2479 SECITEM_FreeItem( av, PR_TRUE );
2482 if ( ret != LDAP_SUCCESS ) {
2483 Debug( LDAP_DEBUG_ANY, "TLS: hostname (%s) does not match "
2484 "common name in certificate (%s).\n",
2486 ret = LDAP_CONNECT_ERROR;
2487 if ( ld->ld_error ) {
2488 LDAP_FREE( ld->ld_error );
2490 ld->ld_error = LDAP_STRDUP(
2491 _("TLS: hostname does not match CN in peer certificate"));
2496 CERT_DestroyCertificate( cert );
2501 tlsm_session_strength( tls_session *session )
2503 tlsm_session *s = (tlsm_session *)session;
2506 rc = SSL_SecurityStatus( s, NULL, NULL, NULL, &keySize,
2508 return rc ? 0 : keySize;
2512 * TLS support for LBER Sockbufs
2515 static PRStatus PR_CALLBACK
2516 tlsm_PR_Close(PRFileDesc *fd)
2518 int rc = PR_SUCCESS;
2520 /* we don't need to actually close anything here, just
2521 pop our io layer off the stack */
2522 fd->secret = NULL; /* must have been freed before calling PR_Close */
2524 fd = PR_PopIOLayer( fd, tlsm_layer_id );
2525 /* if we are not the last layer, pass the close along */
2530 rc = fd->methods->close( fd );
2533 /* we are the last layer - just call our dtor */
2540 static PRStatus PR_CALLBACK
2541 tlsm_PR_Shutdown(PRFileDesc *fd, PRShutdownHow how)
2543 int rc = PR_SUCCESS;
2546 rc = PR_Shutdown( fd->lower, how );
2552 static int PR_CALLBACK
2553 tlsm_PR_Recv(PRFileDesc *fd, void *buf, PRInt32 len, PRIntn flags,
2554 PRIntervalTime timeout)
2559 if ( buf == NULL || len <= 0 ) return 0;
2561 p = tlsm_get_pvt_tls_data( fd );
2563 if ( p == NULL || p->sbiod == NULL ) {
2567 rc = LBER_SBIOD_READ_NEXT( p->sbiod, buf, len );
2569 tlsm_map_error( errno );
2570 if ( errno == EAGAIN || errno == EWOULDBLOCK ) {
2571 p->nonblock = PR_TRUE; /* fd is using non-blocking io */
2572 } else if ( errno ) { /* real error */
2573 Debug( LDAP_DEBUG_TRACE,
2574 "TLS: error: tlsm_PR_Recv returned %d - error %d:%s\n",
2575 rc, errno, STRERROR(errno) );
2577 } else if ( ( rc > 0 ) && ( len > 0 ) && ( p->firsttag == LBER_DEFAULT ) ) {
2578 p->firsttag = (ber_tag_t)*((char *)buf);
2580 p->io_flag = TLSM_READ;
2585 static int PR_CALLBACK
2586 tlsm_PR_Send(PRFileDesc *fd, const void *buf, PRInt32 len, PRIntn flags,
2587 PRIntervalTime timeout)
2592 if ( buf == NULL || len <= 0 ) return 0;
2594 p = tlsm_get_pvt_tls_data( fd );
2596 if ( p == NULL || p->sbiod == NULL ) {
2600 rc = LBER_SBIOD_WRITE_NEXT( p->sbiod, (char *)buf, len );
2602 tlsm_map_error( errno );
2603 if ( errno == EAGAIN || errno == EWOULDBLOCK ) {
2604 p->nonblock = PR_TRUE;
2605 } else if ( errno ) { /* real error */
2606 Debug( LDAP_DEBUG_TRACE,
2607 "TLS: error: tlsm_PR_Send returned %d - error %d:%s\n",
2608 rc, errno, STRERROR(errno) );
2611 p->io_flag = TLSM_WRITE;
2616 static int PR_CALLBACK
2617 tlsm_PR_Read(PRFileDesc *fd, void *buf, PRInt32 len)
2619 return tlsm_PR_Recv( fd, buf, len, 0, PR_INTERVAL_NO_TIMEOUT );
2622 static int PR_CALLBACK
2623 tlsm_PR_Write(PRFileDesc *fd, const void *buf, PRInt32 len)
2625 return tlsm_PR_Send( fd, buf, len, 0, PR_INTERVAL_NO_TIMEOUT );
2628 static PRStatus PR_CALLBACK
2629 tlsm_PR_GetPeerName(PRFileDesc *fd, PRNetAddr *addr)
2634 p = tlsm_get_pvt_tls_data( fd );
2636 if ( p == NULL || p->sbiod == NULL ) {
2639 len = sizeof(PRNetAddr);
2640 return getpeername( p->sbiod->sbiod_sb->sb_fd, (struct sockaddr *)addr, &len );
2643 static PRStatus PR_CALLBACK
2644 tlsm_PR_GetSocketOption(PRFileDesc *fd, PRSocketOptionData *data)
2647 p = tlsm_get_pvt_tls_data( fd );
2653 /* only the nonblocking option is supported at this time
2654 MozNSS SSL code needs it */
2655 if ( data->option != PR_SockOpt_Nonblocking ) {
2656 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
2660 int flags = fcntl( p->sbiod->sbiod_sb->sb_fd, F_GETFL );
2661 data->value.non_blocking = (flags & O_NONBLOCK) ? PR_TRUE : PR_FALSE;
2663 data->value.non_blocking = p->nonblock;
2668 static PRStatus PR_CALLBACK
2671 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
2675 static PRFileDesc * PR_CALLBACK
2678 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
2682 static PRInt16 PR_CALLBACK
2685 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
2689 static PRInt32 PR_CALLBACK
2692 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
2696 static PRInt64 PR_CALLBACK
2701 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
2706 static const PRIOMethods tlsm_PR_methods = {
2708 tlsm_PR_Close, /* close */
2709 tlsm_PR_Read, /* read */
2710 tlsm_PR_Write, /* write */
2711 tlsm_PR_i32_unimp, /* available */
2712 tlsm_PR_i64_unimp, /* available64 */
2713 tlsm_PR_prs_unimp, /* fsync */
2714 tlsm_PR_i32_unimp, /* seek */
2715 tlsm_PR_i64_unimp, /* seek64 */
2716 tlsm_PR_prs_unimp, /* fileInfo */
2717 tlsm_PR_prs_unimp, /* fileInfo64 */
2718 tlsm_PR_i32_unimp, /* writev */
2719 tlsm_PR_prs_unimp, /* connect */
2720 tlsm_PR_pfd_unimp, /* accept */
2721 tlsm_PR_prs_unimp, /* bind */
2722 tlsm_PR_prs_unimp, /* listen */
2723 (PRShutdownFN)tlsm_PR_Shutdown, /* shutdown */
2724 tlsm_PR_Recv, /* recv */
2725 tlsm_PR_Send, /* send */
2726 tlsm_PR_i32_unimp, /* recvfrom */
2727 tlsm_PR_i32_unimp, /* sendto */
2728 (PRPollFN)tlsm_PR_i16_unimp, /* poll */
2729 tlsm_PR_i32_unimp, /* acceptread */
2730 tlsm_PR_i32_unimp, /* transmitfile */
2731 tlsm_PR_prs_unimp, /* getsockname */
2732 tlsm_PR_GetPeerName, /* getpeername */
2733 tlsm_PR_i32_unimp, /* getsockopt OBSOLETE */
2734 tlsm_PR_i32_unimp, /* setsockopt OBSOLETE */
2735 tlsm_PR_GetSocketOption, /* getsocketoption */
2736 tlsm_PR_i32_unimp, /* setsocketoption */
2737 tlsm_PR_i32_unimp, /* Send a (partial) file with header/trailer*/
2738 (PRConnectcontinueFN)tlsm_PR_prs_unimp, /* connectcontinue */
2739 tlsm_PR_i32_unimp, /* reserved for future use */
2740 tlsm_PR_i32_unimp, /* reserved for future use */
2741 tlsm_PR_i32_unimp, /* reserved for future use */
2742 tlsm_PR_i32_unimp /* reserved for future use */
2746 * Initialize TLS subsystem. Should be called only once.
2747 * See tlsm_deferred_init for the bulk of the init process
2754 tlsm_layer_id = PR_GetUniqueIdentity( "OpenLDAP" );
2760 tlsm_sb_setup( Sockbuf_IO_Desc *sbiod, void *arg )
2763 tlsm_session *session = arg;
2766 assert( sbiod != NULL );
2768 p = LBER_MALLOC( sizeof( *p ) );
2773 fd = PR_GetIdentitiesLayer( session, tlsm_layer_id );
2779 fd->secret = (PRFilePrivate *)p;
2780 p->session = session;
2782 p->firsttag = LBER_DEFAULT;
2783 sbiod->sbiod_pvt = p;
2788 tlsm_sb_remove( Sockbuf_IO_Desc *sbiod )
2792 assert( sbiod != NULL );
2793 assert( sbiod->sbiod_pvt != NULL );
2795 p = (struct tls_data *)sbiod->sbiod_pvt;
2796 PR_Close( p->session );
2797 LBER_FREE( sbiod->sbiod_pvt );
2798 sbiod->sbiod_pvt = NULL;
2803 tlsm_sb_close( Sockbuf_IO_Desc *sbiod )
2807 assert( sbiod != NULL );
2808 assert( sbiod->sbiod_pvt != NULL );
2810 p = (struct tls_data *)sbiod->sbiod_pvt;
2811 PR_Shutdown( p->session, PR_SHUTDOWN_BOTH );
2816 tlsm_sb_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
2820 assert( sbiod != NULL );
2821 assert( sbiod->sbiod_pvt != NULL );
2823 p = (struct tls_data *)sbiod->sbiod_pvt;
2825 if ( opt == LBER_SB_OPT_GET_SSL ) {
2826 *((tlsm_session **)arg) = p->session;
2829 } else if ( opt == LBER_SB_OPT_DATA_READY ) {
2830 if ( p && ( SSL_DataPending( p->session ) > 0 ) ) {
2836 return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg );
2840 tlsm_sb_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
2846 assert( sbiod != NULL );
2847 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
2849 p = (struct tls_data *)sbiod->sbiod_pvt;
2851 ret = PR_Recv( p->session, buf, len, 0, PR_INTERVAL_NO_TIMEOUT );
2853 err = PR_GetError();
2854 if ( err == PR_PENDING_INTERRUPT_ERROR || err == PR_WOULD_BLOCK_ERROR ) {
2855 sbiod->sbiod_sb->sb_trans_needs_read = 1;
2856 sock_errset(EWOULDBLOCK);
2859 sbiod->sbiod_sb->sb_trans_needs_read = 0;
2865 tlsm_sb_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
2871 assert( sbiod != NULL );
2872 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
2874 p = (struct tls_data *)sbiod->sbiod_pvt;
2876 ret = PR_Send( p->session, (char *)buf, len, 0, PR_INTERVAL_NO_TIMEOUT );
2878 err = PR_GetError();
2879 if ( err == PR_PENDING_INTERRUPT_ERROR || err == PR_WOULD_BLOCK_ERROR ) {
2880 sbiod->sbiod_sb->sb_trans_needs_write = 1;
2881 sock_errset(EWOULDBLOCK);
2885 sbiod->sbiod_sb->sb_trans_needs_write = 0;
2890 static Sockbuf_IO tlsm_sbio =
2892 tlsm_sb_setup, /* sbi_setup */
2893 tlsm_sb_remove, /* sbi_remove */
2894 tlsm_sb_ctrl, /* sbi_ctrl */
2895 tlsm_sb_read, /* sbi_read */
2896 tlsm_sb_write, /* sbi_write */
2897 tlsm_sb_close /* sbi_close */
2900 tls_impl ldap_int_tls_impl = {
2912 tlsm_session_connect,
2913 tlsm_session_accept,
2914 tlsm_session_upflags,
2915 tlsm_session_errmsg,
2917 tlsm_session_peer_dn,
2918 tlsm_session_chkhost,
2919 tlsm_session_strength,
2923 #ifdef LDAP_R_COMPILE
2932 #endif /* HAVE_MOZNSS */