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>
46 #include <ldap_pvt_thread.h>
49 #define READ_PASSWORD_FROM_STDIN
50 #define READ_PASSWORD_FROM_FILE
52 #ifdef READ_PASSWORD_FROM_STDIN
53 #include <termios.h> /* for echo on/off */
56 #include <nspr/nspr.h>
57 #include <nspr/private/pprio.h>
60 #include <nss/sslerr.h>
61 #include <nss/sslproto.h>
62 #include <nss/pk11pub.h>
63 #include <nss/secerr.h>
64 #include <nss/keyhi.h>
65 #include <nss/secmod.h>
67 /* NSS 3.12.5 and later have NSS_InitContext */
68 #if NSS_VMAJOR <= 3 && NSS_VMINOR <= 12 && NSS_VPATCH < 5
71 #define HAVE_NSS_INITCONTEXT 1
74 /* InitContext does not currently work in server mode */
75 /* #define INITCONTEXT_HACK 1 */
77 typedef struct tlsm_ctx {
80 PRBool tc_verify_cert;
81 CERTCertDBHandle *tc_certdb;
84 struct ldaptls *tc_config;
87 PRCallOnceType tc_callonce;
89 char *tc_slotname; /* if using pem */
90 #ifdef HAVE_NSS_INITCONTEXT
91 NSSInitContext *tc_initctx; /* the NSS context */
93 PK11GenericObject **tc_pem_objs; /* array of objects to free */
94 int tc_n_pem_objs; /* number of objects */
96 ldap_pvt_thread_mutex_t tc_refmutex;
100 typedef PRFileDesc tlsm_session;
102 static PRDescIdentity tlsm_layer_id;
104 static const PRIOMethods tlsm_PR_methods;
106 #define PEM_LIBRARY "nsspem"
107 #define PEM_MODULE "PEM"
109 static SECMODModule *pem_module;
111 #define DEFAULT_TOKEN_NAME "default"
112 /* sprintf format used to create token name */
113 #define TLSM_PEM_TOKEN_FMT "PEM Token #%ld"
115 static int tlsm_slot_count;
117 #define PK11_SETATTRS(x,id,v,l) (x)->type = (id); \
118 (x)->pValue=(v); (x)->ulValueLen = (l);
120 /* forward declaration */
121 static int tlsm_init( void );
123 #ifdef LDAP_R_COMPILE
126 tlsm_thr_init( void )
130 #endif /* LDAP_R_COMPILE */
133 tlsm_dump_cipher_info(PRFileDesc *fd)
137 for (ii = 0; ii < SSL_NumImplementedCiphers; ++ii) {
138 PRInt32 cipher = (PRInt32)SSL_ImplementedCiphers[ii];
139 PRBool enabled = PR_FALSE;
141 SSLCipherSuiteInfo info;
144 SSL_CipherPrefGet(fd, cipher, &enabled);
146 SSL_CipherPrefGetDefault(cipher, &enabled);
148 SSL_CipherPolicyGet(cipher, &policy);
149 SSL_GetCipherSuiteInfo(cipher, &info, (PRUintn)sizeof(info));
150 Debug( LDAP_DEBUG_TRACE,
151 "TLS: cipher: %d - %s, enabled: %d, ",
152 info.cipherSuite, info.cipherSuiteName, enabled );
153 Debug( LDAP_DEBUG_TRACE,
154 "policy: %d\n", policy, 0, 0 );
160 /* Cipher definitions */
162 char *ossl_name; /* The OpenSSL cipher name */
163 int num; /* The cipher id */
164 int attr; /* cipher attributes: algorithms, etc */
165 int version; /* protocol version valid for this cipher */
166 int bits; /* bits of strength */
167 int alg_bits; /* bits of the algorithm */
168 int strength; /* LOW, MEDIUM, HIGH */
169 int enabled; /* Enabled by default? */
172 /* cipher attributes */
173 #define SSL_kRSA 0x00000001L
174 #define SSL_aRSA 0x00000002L
175 #define SSL_aDSS 0x00000004L
176 #define SSL_DSS SSL_aDSS
177 #define SSL_eNULL 0x00000008L
178 #define SSL_DES 0x00000010L
179 #define SSL_3DES 0x00000020L
180 #define SSL_RC4 0x00000040L
181 #define SSL_RC2 0x00000080L
182 #define SSL_AES 0x00000100L
183 #define SSL_MD5 0x00000200L
184 #define SSL_SHA1 0x00000400L
185 #define SSL_SHA SSL_SHA1
186 #define SSL_RSA (SSL_kRSA|SSL_aRSA)
188 /* cipher strength */
189 #define SSL_NULL 0x00000001L
190 #define SSL_EXPORT40 0x00000002L
191 #define SSL_EXPORT56 0x00000004L
192 #define SSL_LOW 0x00000008L
193 #define SSL_MEDIUM 0x00000010L
194 #define SSL_HIGH 0x00000020L
196 #define SSL2 0x00000001L
197 #define SSL3 0x00000002L
198 /* OpenSSL treats SSL3 and TLSv1 the same */
201 /* Cipher translation */
202 static cipher_properties ciphers_def[] = {
204 {"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},
205 {"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},
206 {"RC4-MD5", SSL_EN_RC4_128_WITH_MD5, SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_MD5, SSL2, 128, 128, SSL_MEDIUM, SSL_ALLOWED},
207 {"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},
208 {"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},
209 {"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},
212 {"RC4-MD5", SSL_RSA_WITH_RC4_128_MD5, SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_MD5, SSL3, 128, 128, SSL_MEDIUM, SSL_ALLOWED},
213 {"RC4-SHA", SSL_RSA_WITH_RC4_128_SHA, SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_SHA1, SSL3, 128, 128, SSL_MEDIUM, SSL_NOT_ALLOWED},
214 {"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},
215 {"DES-CBC-SHA", SSL_RSA_WITH_DES_CBC_SHA, SSL_kRSA|SSL_aRSA|SSL_DES|SSL_SHA1, SSL3, 56, 56, SSL_LOW, SSL_ALLOWED},
216 {"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},
217 {"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},
218 {"NULL-MD5", SSL_RSA_WITH_NULL_MD5, SSL_kRSA|SSL_aRSA|SSL_eNULL|SSL_MD5, SSL3, 0, 0, SSL_NULL, SSL_NOT_ALLOWED},
219 {"NULL-SHA", SSL_RSA_WITH_NULL_SHA, SSL_kRSA|SSL_aRSA|SSL_eNULL|SSL_SHA1, SSL3, 0, 0, SSL_NULL, SSL_NOT_ALLOWED},
222 {"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},
223 {"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},
224 {"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},
225 {"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},
228 #define ciphernum (sizeof(ciphers_def)/sizeof(cipher_properties))
230 /* given err which is the current errno, calls PR_SetError with
231 the corresponding NSPR error code */
233 tlsm_map_error(int err)
239 prError = PR_NO_ACCESS_RIGHTS_ERROR;
242 prError = PR_ADDRESS_IN_USE_ERROR;
245 prError = PR_ADDRESS_NOT_AVAILABLE_ERROR;
248 prError = PR_ADDRESS_NOT_SUPPORTED_ERROR;
251 prError = PR_WOULD_BLOCK_ERROR;
254 * On QNX and Neutrino, EALREADY is defined as EBUSY.
256 #if EALREADY != EBUSY
258 prError = PR_ALREADY_INITIATED_ERROR;
262 prError = PR_BAD_DESCRIPTOR_ERROR;
266 prError = PR_IO_ERROR;
270 prError = PR_FILESYSTEM_MOUNTED_ERROR;
273 prError = PR_CONNECT_ABORTED_ERROR;
276 prError = PR_CONNECT_REFUSED_ERROR;
279 prError = PR_CONNECT_RESET_ERROR;
282 prError = PR_DEADLOCK_ERROR;
286 prError = PR_DIRECTORY_CORRUPTED_ERROR;
291 prError = PR_NO_DEVICE_SPACE_ERROR;
295 prError = PR_FILE_EXISTS_ERROR;
298 prError = PR_ACCESS_FAULT_ERROR;
301 prError = PR_FILE_TOO_BIG_ERROR;
304 prError = PR_HOST_UNREACHABLE_ERROR;
307 prError = PR_IN_PROGRESS_ERROR;
310 prError = PR_PENDING_INTERRUPT_ERROR;
313 prError = PR_INVALID_ARGUMENT_ERROR;
316 prError = PR_IO_ERROR;
319 prError = PR_IS_CONNECTED_ERROR;
322 prError = PR_IS_DIRECTORY_ERROR;
325 prError = PR_LOOP_ERROR;
328 prError = PR_PROC_DESC_TABLE_FULL_ERROR;
331 prError = PR_MAX_DIRECTORY_ENTRIES_ERROR;
334 prError = PR_INVALID_ARGUMENT_ERROR;
338 prError = PR_REMOTE_FILE_ERROR;
342 prError = PR_NAME_TOO_LONG_ERROR;
345 prError = PR_NETWORK_UNREACHABLE_ERROR;
348 prError = PR_SYS_DESC_TABLE_FULL_ERROR;
351 * On SCO OpenServer 5, ENOBUFS is defined as ENOSR.
353 #if defined(ENOBUFS) && (ENOBUFS != ENOSR)
355 prError = PR_INSUFFICIENT_RESOURCES_ERROR;
359 prError = PR_FILE_NOT_FOUND_ERROR;
362 prError = PR_FILE_NOT_FOUND_ERROR;
365 prError = PR_FILE_IS_LOCKED_ERROR;
369 prError = PR_REMOTE_FILE_ERROR;
373 prError = PR_OUT_OF_MEMORY_ERROR;
376 prError = PR_INVALID_ARGUMENT_ERROR;
379 prError = PR_NO_DEVICE_SPACE_ERROR;
383 prError = PR_INSUFFICIENT_RESOURCES_ERROR;
387 prError = PR_NOT_CONNECTED_ERROR;
390 prError = PR_NOT_DIRECTORY_ERROR;
393 prError = PR_NOT_SOCKET_ERROR;
396 prError = PR_FILE_NOT_FOUND_ERROR;
399 prError = PR_NOT_TCP_SOCKET_ERROR;
403 prError = PR_BUFFER_OVERFLOW_ERROR;
407 prError = PR_NO_ACCESS_RIGHTS_ERROR;
410 prError = PR_CONNECT_RESET_ERROR;
414 prError = PR_IO_ERROR;
417 case EPROTONOSUPPORT:
418 prError = PR_PROTOCOL_NOT_SUPPORTED_ERROR;
421 prError = PR_ADDRESS_NOT_SUPPORTED_ERROR;
424 prError = PR_INVALID_METHOD_ERROR;
427 prError = PR_READ_ONLY_FILESYSTEM_ERROR;
430 prError = PR_INVALID_METHOD_ERROR;
433 prError = PR_IO_TIMEOUT_ERROR;
435 #if EWOULDBLOCK != EAGAIN
437 prError = PR_WOULD_BLOCK_ERROR;
441 prError = PR_NOT_SAME_DEVICE_ERROR;
444 prError = PR_UNKNOWN_ERROR;
447 PR_SetError( prError, err );
451 * cipher_list is an integer array with the following values:
452 * -1: never enable this cipher
457 nss_parse_ciphers(const char *cipherstr, int cipher_list[ciphernum])
466 /* All disabled to start */
467 for (i=0; i<ciphernum; i++)
470 ciphertip = strdup(cipherstr);
471 cipher = ciphers = ciphertip;
473 while (ciphers && (strlen(ciphers))) {
474 while ((*cipher) && (isspace(*cipher)))
479 case '+': /* Add something */
483 case '-': /* Subtract something */
487 case '!': /* Disable something */
496 if ((ciphers = strchr(cipher, ':'))) {
500 /* Do the easy one first */
501 if (!strcmp(cipher, "ALL")) {
502 for (i=0; i<ciphernum; i++) {
503 if (!(ciphers_def[i].attr & SSL_eNULL))
504 cipher_list[i] = action;
506 } else if (!strcmp(cipher, "COMPLEMENTOFALL")) {
507 for (i=0; i<ciphernum; i++) {
508 if ((ciphers_def[i].attr & SSL_eNULL))
509 cipher_list[i] = action;
511 } else if (!strcmp(cipher, "DEFAULT")) {
512 for (i=0; i<ciphernum; i++) {
513 cipher_list[i] = ciphers_def[i].enabled == SSL_ALLOWED ? 1 : 0;
522 while (c && (strlen(c))) {
524 if ((c = strchr(cipher, '+'))) {
528 if (!strcmp(cipher, "RSA")) {
530 } else if ((!strcmp(cipher, "NULL")) || (!strcmp(cipher, "eNULL"))) {
532 } else if (!strcmp(cipher, "AES")) {
534 } else if (!strcmp(cipher, "3DES")) {
536 } else if (!strcmp(cipher, "DES")) {
538 } else if (!strcmp(cipher, "RC4")) {
540 } else if (!strcmp(cipher, "RC2")) {
542 } else if (!strcmp(cipher, "MD5")) {
544 } else if ((!strcmp(cipher, "SHA")) || (!strcmp(cipher, "SHA1"))) {
546 } else if (!strcmp(cipher, "SSLv2")) {
548 } else if (!strcmp(cipher, "SSLv3")) {
550 } else if (!strcmp(cipher, "TLSv1")) {
552 } else if (!strcmp(cipher, "HIGH")) {
553 strength |= SSL_HIGH;
554 } else if (!strcmp(cipher, "MEDIUM")) {
555 strength |= SSL_MEDIUM;
556 } else if (!strcmp(cipher, "LOW")) {
558 } else if ((!strcmp(cipher, "EXPORT")) || (!strcmp(cipher, "EXP"))) {
559 strength |= SSL_EXPORT40|SSL_EXPORT56;
560 } else if (!strcmp(cipher, "EXPORT40")) {
561 strength |= SSL_EXPORT40;
562 } else if (!strcmp(cipher, "EXPORT56")) {
563 strength |= SSL_EXPORT56;
571 /* If we have a mask, apply it. If not then perhaps they provided
572 * a specific cipher to enable.
574 if (mask || strength || protocol) {
575 for (i=0; i<ciphernum; i++) {
576 if (((ciphers_def[i].attr & mask) ||
577 (ciphers_def[i].strength & strength) ||
578 (ciphers_def[i].version & protocol)) &&
579 (cipher_list[i] != -1)) {
580 /* Enable the NULL ciphers only if explicity
582 if (ciphers_def[i].attr & SSL_eNULL) {
583 if (mask & SSL_eNULL)
584 cipher_list[i] = action;
586 cipher_list[i] = action;
590 for (i=0; i<ciphernum; i++) {
591 if (!strcmp(ciphers_def[i].ossl_name, cipher) &&
592 cipher_list[1] != -1)
593 cipher_list[i] = action;
602 /* See if any ciphers were enabled */
604 for (i=0; i<ciphernum; i++) {
605 if (cipher_list[i] == 1)
615 tlsm_parse_ciphers(tlsm_ctx *ctx, const char *str)
617 int cipher_state[ciphernum];
623 rv = nss_parse_ciphers(str, cipher_state);
626 /* First disable everything */
627 for (i = 0; i < SSL_NumImplementedCiphers; i++)
628 SSL_CipherPrefSet(ctx->tc_model, SSL_ImplementedCiphers[i], SSL_NOT_ALLOWED);
630 /* Now enable what was requested */
631 for (i=0; i<ciphernum; i++) {
632 SSLCipherSuiteInfo suite;
635 if (SSL_GetCipherSuiteInfo(ciphers_def[i].num, &suite, sizeof suite)
637 enabled = cipher_state[i] < 0 ? 0 : cipher_state[i];
638 if (enabled == SSL_ALLOWED) {
639 if (PK11_IsFIPS() && !suite.isFIPS)
640 enabled = SSL_NOT_ALLOWED;
642 SSL_CipherPrefSet(ctx->tc_model, ciphers_def[i].num, enabled);
647 return rv == 1 ? 0 : -1;
651 tlsm_bad_cert_handler(void *arg, PRFileDesc *ssl)
653 SECStatus success = SECSuccess;
655 tlsm_ctx *ctx = (tlsm_ctx *)arg;
661 err = PORT_GetError();
664 case SEC_ERROR_UNTRUSTED_ISSUER:
665 case SEC_ERROR_UNKNOWN_ISSUER:
666 case SEC_ERROR_EXPIRED_CERTIFICATE:
667 if (ctx->tc_verify_cert) {
668 success = SECFailure;
671 /* we bypass NSS's hostname checks and do our own */
672 case SSL_ERROR_BAD_CERT_DOMAIN:
675 success = SECFailure;
683 tlsm_dump_security_status(PRFileDesc *fd)
685 char * cp; /* bulk cipher name */
686 char * ip; /* cert issuer DN */
687 char * sp; /* cert subject DN */
688 int op; /* High, Low, Off */
689 int kp0; /* total key bits */
690 int kp1; /* secret key bits */
691 SSL3Statistics * ssl3stats = SSL_GetStatistics();
693 SSL_SecurityStatus( fd, &op, &cp, &kp0, &kp1, &ip, &sp );
694 Debug( LDAP_DEBUG_TRACE,
695 "TLS certificate verification: subject: %s, issuer: %s, cipher: %s, ",
696 sp ? sp : "-unknown-", ip ? ip : "-unknown-", cp ? cp : "-unknown-" );
700 Debug( LDAP_DEBUG_TRACE,
701 "security level: %s, secret key bits: %d, total key bits: %d, ",
702 ((op == SSL_SECURITY_STATUS_ON_HIGH) ? "high" :
703 ((op == SSL_SECURITY_STATUS_ON_LOW) ? "low" : "off")),
706 Debug( LDAP_DEBUG_TRACE,
707 "cache hits: %ld, cache misses: %ld, cache not reusable: %ld\n",
708 ssl3stats->hch_sid_cache_hits, ssl3stats->hch_sid_cache_misses,
709 ssl3stats->hch_sid_cache_not_ok );
714 #ifdef READ_PASSWORD_FROM_FILE
716 tlsm_get_pin_from_file(const char *token_name, tlsm_ctx *ctx)
719 char *contents = NULL;
722 char *candidate = NULL;
723 PRFileInfo file_info;
724 PRFileDesc *pwd_fileptr = PR_Open( ctx->tc_pin_file, PR_RDONLY, 00400 );
726 /* open the password file */
727 if ( !pwd_fileptr ) {
728 PRErrorCode errcode = PR_GetError();
729 Debug( LDAP_DEBUG_ANY,
730 "TLS: could not open security pin file %s - error %d:%s.\n",
731 ctx->tc_pin_file, errcode,
732 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
736 /* get the file size */
737 if ( PR_SUCCESS != PR_GetFileInfo( ctx->tc_pin_file, &file_info ) ) {
738 PRErrorCode errcode = PR_GetError();
739 Debug( LDAP_DEBUG_ANY,
740 "TLS: could not get file info from pin file %s - error %d:%s.\n",
741 ctx->tc_pin_file, errcode,
742 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
746 /* create a buffer to hold the file contents */
747 if ( !( contents = PR_MALLOC( file_info.size + 1 ) ) ) {
748 PRErrorCode errcode = PR_GetError();
749 Debug( LDAP_DEBUG_ANY,
750 "TLS: could not alloc a buffer for contents of pin file %s - error %d:%s.\n",
751 ctx->tc_pin_file, errcode,
752 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
756 /* read file into the buffer */
757 if( PR_Read( pwd_fileptr, contents, file_info.size ) <= 0 ) {
758 PRErrorCode errcode = PR_GetError();
759 Debug( LDAP_DEBUG_ANY,
760 "TLS: could not read the file contents from pin file %s - error %d:%s.\n",
761 ctx->tc_pin_file, errcode,
762 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
766 /* format is [tokenname:]password EOL [tokenname:]password EOL ... */
767 /* if you want to use a password containing a colon character, use
768 the special tokenname "default" */
769 for ( line = PL_strtok_r( contents, "\r\n", &lasts ); line;
770 line = PL_strtok_r( NULL, "\r\n", &lasts ) ) {
774 continue; /* skip blank lines */
776 colon = PL_strchr( line, ':' );
778 if ( *(colon + 1) && token_name &&
779 !PL_strncmp( token_name, line, colon-line ) ) {
780 candidate = colon + 1; /* found a definite match */
782 } else if ( !PL_strncmp( DEFAULT_TOKEN_NAME, line, colon-line ) ) {
783 candidate = colon + 1; /* found possible match */
785 } else { /* no token name */
791 PR_Close( pwd_fileptr );
794 pwdstr = PL_strdup( candidate );
796 PL_strfree( contents );
800 #endif /* READ_PASSWORD_FROM_FILE */
802 #ifdef READ_PASSWORD_FROM_STDIN
804 * Turn the echoing off on a tty.
809 if ( isatty( fd ) ) {
811 tcgetattr( fd, &tio );
812 tio.c_lflag &= ~ECHO;
813 tcsetattr( fd, TCSAFLUSH, &tio );
818 * Turn the echoing on on a tty.
823 if ( isatty( fd ) ) {
825 tcgetattr( fd, &tio );
827 tcsetattr( fd, TCSAFLUSH, &tio );
828 tcsetattr( fd, TCSAFLUSH, &tio );
831 #endif /* READ_PASSWORD_FROM_STDIN */
834 * This does the actual work of reading the pin/password/pass phrase
837 tlsm_get_pin(PK11SlotInfo *slot, PRBool retry, tlsm_ctx *ctx)
839 char *token_name = NULL;
842 token_name = PK11_GetTokenName( slot );
843 #ifdef READ_PASSWORD_FROM_FILE
844 /* Try to get the passwords from the password file if it exists.
845 * THIS IS UNSAFE and is provided for convenience only. Without this
846 * capability the server would have to be started in foreground mode
847 * if using an encrypted key.
849 if ( ctx->tc_pin_file ) {
850 pwdstr = tlsm_get_pin_from_file( token_name, ctx );
852 #endif /* RETRIEVE_PASSWORD_FROM_FILE */
853 #ifdef READ_PASSWORD_FROM_STDIN
855 int infd = PR_FileDesc2NativeHandle( PR_STDIN );
856 int isTTY = isatty( infd );
857 unsigned char phrase[200];
858 /* Prompt for password */
861 "Please enter pin, password, or pass phrase for security token '%s': ",
862 token_name ? token_name : DEFAULT_TOKEN_NAME );
865 fgets( (char*)phrase, sizeof(phrase), stdin );
867 fprintf( stdout, "\n" );
870 /* stomp on newline */
871 phrase[strlen((char*)phrase)-1] = 0;
873 pwdstr = PL_strdup( (char*)phrase );
876 #endif /* READ_PASSWORD_FROM_STDIN */
881 * PKCS11 devices (including the internal softokn cert/key database)
882 * may be protected by a pin or password or even pass phrase
883 * MozNSS needs a way for the user to provide that
886 tlsm_pin_prompt(PK11SlotInfo *slot, PRBool retry, void *arg)
888 tlsm_ctx *ctx = (tlsm_ctx *)arg;
890 return tlsm_get_pin( slot, retry, ctx );
894 tlsm_auth_cert_handler(void *arg, PRFileDesc *fd,
895 PRBool checksig, PRBool isServer)
897 SECStatus ret = SSL_AuthCertificate(arg, fd, checksig, isServer);
899 tlsm_dump_security_status( fd );
900 Debug( LDAP_DEBUG_TRACE,
901 "TLS certificate verification: %s\n",
902 ret == SECSuccess ? "ok" : "bad", 0, 0 );
904 if ( ret != SECSuccess ) {
905 PRErrorCode errcode = PORT_GetError();
906 Debug( LDAP_DEBUG_ANY,
907 "TLS certificate verification: Error, %d: %s\n",
908 errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ), 0 ) ;
915 tlsm_authenticate_to_slot( tlsm_ctx *ctx, PK11SlotInfo *slot )
919 if ( SECSuccess != PK11_Authenticate( slot, PR_FALSE, ctx ) ) {
920 char *token_name = PK11_GetTokenName( slot );
921 PRErrorCode errcode = PR_GetError();
922 Debug( LDAP_DEBUG_ANY,
923 "TLS: could not authenticate to the security token %s - error %d:%s.\n",
924 token_name ? token_name : DEFAULT_TOKEN_NAME, errcode,
925 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
927 rc = 0; /* success */
934 tlsm_nss_shutdown_cb( void *appData, void *nssData )
936 SECStatus rc = SECSuccess;
938 SSL_ShutdownServerSessionIDCache();
939 SSL_ClearSessionCache();
942 SECMOD_UnloadUserModule( pem_module );
943 SECMOD_DestroyModule( pem_module );
950 tlsm_init_pem_module( void )
953 char *fullname = NULL;
954 char *configstring = NULL;
960 /* not loaded - load it */
961 /* get the system dependent library name */
962 fullname = PR_GetLibraryName( NULL, PEM_LIBRARY );
963 /* Load our PKCS#11 module */
964 configstring = PR_smprintf( "library=%s name=" PEM_MODULE " parameters=\"\"", fullname );
965 PL_strfree( fullname );
967 pem_module = SECMOD_LoadUserModule( configstring, NULL, PR_FALSE );
968 PR_smprintf_free( configstring );
970 if ( !pem_module || !pem_module->loaded ) {
972 SECMOD_DestroyModule( pem_module );
982 tlsm_add_pem_obj( tlsm_ctx *ctx, PK11GenericObject *obj )
984 int idx = ctx->tc_n_pem_objs;
985 ctx->tc_n_pem_objs++;
986 ctx->tc_pem_objs = (PK11GenericObject **)
987 PORT_Realloc( ctx->tc_pem_objs, ctx->tc_n_pem_objs * sizeof( PK11GenericObject * ) );
988 ctx->tc_pem_objs[idx] = obj;
992 tlsm_free_pem_objs( tlsm_ctx *ctx )
994 /* free in reverse order of allocation */
995 while ( ctx->tc_n_pem_objs-- ) {
996 PK11_DestroyGenericObject( ctx->tc_pem_objs[ctx->tc_n_pem_objs] );
997 ctx->tc_pem_objs[ctx->tc_n_pem_objs] = NULL;
999 PORT_Free(ctx->tc_pem_objs);
1000 ctx->tc_pem_objs = NULL;
1001 ctx->tc_n_pem_objs = 0;
1005 tlsm_add_cert_from_file( tlsm_ctx *ctx, const char *filename, PRBool isca )
1008 PK11SlotInfo *slot = NULL;
1009 PK11GenericObject *rv;
1010 CK_ATTRIBUTE *attrs;
1011 CK_ATTRIBUTE theTemplate[20];
1012 CK_BBOOL cktrue = CK_TRUE;
1013 CK_BBOOL ckfalse = CK_FALSE;
1014 CK_OBJECT_CLASS objClass = CKO_CERTIFICATE;
1015 char tmpslotname[64];
1016 char *slotname = NULL;
1017 const char *ptr = NULL;
1018 char sep = PR_GetDirectorySeparator();
1020 attrs = theTemplate;
1023 slotID = 0; /* CA and trust objects use slot 0 */
1024 PR_snprintf( tmpslotname, sizeof(tmpslotname), TLSM_PEM_TOKEN_FMT, slotID );
1025 slotname = tmpslotname;
1027 if ( ctx->tc_slotname == NULL ) { /* need new slot */
1028 slotID = ++tlsm_slot_count;
1029 ctx->tc_slotname = PR_smprintf( TLSM_PEM_TOKEN_FMT, slotID );
1031 slotname = ctx->tc_slotname;
1033 if ( ( ptr = PL_strrchr( filename, sep ) ) ) {
1034 PL_strfree( ctx->tc_certname );
1036 ctx->tc_certname = PR_smprintf( "%s:%s", slotname, ptr );
1040 slot = PK11_FindSlotByName( slotname );
1043 PRErrorCode errcode = PR_GetError();
1044 Debug( LDAP_DEBUG_ANY,
1045 "TLS: could not find the slot for certificate %s - error %d:%s.\n",
1046 ctx->tc_certname, errcode,
1047 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1051 PK11_SETATTRS( attrs, CKA_CLASS, &objClass, sizeof(objClass) ); attrs++;
1052 PK11_SETATTRS( attrs, CKA_TOKEN, &cktrue, sizeof(CK_BBOOL) ); attrs++;
1053 PK11_SETATTRS( attrs, CKA_LABEL, (unsigned char *)filename, strlen(filename)+1 ); attrs++;
1055 PK11_SETATTRS( attrs, CKA_TRUST, &cktrue, sizeof(CK_BBOOL) ); attrs++;
1057 PK11_SETATTRS( attrs, CKA_TRUST, &ckfalse, sizeof(CK_BBOOL) ); attrs++;
1059 /* This loads the certificate in our PEM module into the appropriate
1062 rv = PK11_CreateGenericObject( slot, theTemplate, 4, PR_FALSE /* isPerm */ );
1064 PK11_FreeSlot( slot );
1067 PRErrorCode errcode = PR_GetError();
1068 Debug( LDAP_DEBUG_ANY,
1069 "TLS: could not add the certificate %s - error %d:%s.\n",
1070 ctx->tc_certname, errcode,
1071 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1075 tlsm_add_pem_obj( ctx, rv );
1081 tlsm_add_key_from_file( tlsm_ctx *ctx, const char *filename )
1084 PK11SlotInfo * slot = NULL;
1085 PK11GenericObject *rv;
1086 CK_ATTRIBUTE *attrs;
1087 CK_ATTRIBUTE theTemplate[20];
1088 CK_BBOOL cktrue = CK_TRUE;
1089 CK_OBJECT_CLASS objClass = CKO_PRIVATE_KEY;
1092 attrs = theTemplate;
1094 if ( ctx->tc_slotname == NULL ) { /* need new slot */
1095 slotID = ++tlsm_slot_count;
1096 ctx->tc_slotname = PR_smprintf( TLSM_PEM_TOKEN_FMT, slotID );
1098 slot = PK11_FindSlotByName( ctx->tc_slotname );
1101 PRErrorCode errcode = PR_GetError();
1102 Debug( LDAP_DEBUG_ANY,
1103 "TLS: could not find the slot %s for the private key - error %d:%s.\n",
1104 ctx->tc_slotname, errcode,
1105 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1109 PK11_SETATTRS( attrs, CKA_CLASS, &objClass, sizeof(objClass) ); attrs++;
1110 PK11_SETATTRS( attrs, CKA_TOKEN, &cktrue, sizeof(CK_BBOOL) ); attrs++;
1111 PK11_SETATTRS( attrs, CKA_LABEL, (unsigned char *)filename, strlen(filename)+1 ); attrs++;
1112 rv = PK11_CreateGenericObject( slot, theTemplate, 3, PR_FALSE /* isPerm */ );
1115 PRErrorCode errcode = PR_GetError();
1116 Debug( LDAP_DEBUG_ANY,
1117 "TLS: could not add the certificate %s - error %d:%s.\n",
1118 ctx->tc_certname, errcode,
1119 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1122 /* When adding an encrypted key the PKCS#11 will be set as removed */
1123 /* This will force the token to be seen as re-inserted */
1124 SECMOD_WaitForAnyTokenEvent( pem_module, 0, 0 );
1125 PK11_IsPresent( slot );
1129 PK11_FreeSlot( slot );
1132 tlsm_add_pem_obj( ctx, rv );
1138 tlsm_init_ca_certs( tlsm_ctx *ctx, const char *cacertfile, const char *cacertdir )
1140 PRBool isca = PR_TRUE;
1143 int rc = tlsm_add_cert_from_file( ctx, cacertfile, isca );
1155 memset( &fi, 0, sizeof(fi) );
1156 status = PR_GetFileInfo( cacertdir, &fi );
1157 if ( PR_SUCCESS != status) {
1158 PRErrorCode errcode = PR_GetError();
1159 Debug( LDAP_DEBUG_ANY,
1160 "TLS: could not get info about the CA certificate directory %s - error %d:%s.\n",
1162 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1166 if ( fi.type != PR_FILE_DIRECTORY ) {
1167 Debug( LDAP_DEBUG_ANY,
1168 "TLS: error: the CA certificate directory %s is not a directory.\n",
1173 dir = PR_OpenDir( cacertdir );
1174 if ( NULL == dir ) {
1175 PRErrorCode errcode = PR_GetError();
1176 Debug( LDAP_DEBUG_ANY,
1177 "TLS: could not open the CA certificate directory %s - error %d:%s.\n",
1179 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1185 entry = PR_ReadDir( dir, PR_SKIP_BOTH | PR_SKIP_HIDDEN );
1186 if ( NULL != entry ) {
1187 char *fullpath = PR_smprintf( "%s/%s", cacertdir, entry->name );
1188 if ( !tlsm_add_cert_from_file( ctx, fullpath, isca ) ) {
1189 status = 0; /* found at least 1 valid CA file in the dir */
1191 PR_smprintf_free( fullpath );
1193 } while ( NULL != entry );
1197 PRErrorCode errcode = PR_GetError();
1198 Debug( LDAP_DEBUG_ANY,
1199 "TLS: did not find any valid CA certificate files in the CA certificate directory %s - error %d:%s.\n",
1201 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1210 * NSS supports having multiple cert/key databases in the same
1211 * directory, each one having a unique string prefix e.g.
1212 * slapd-01-cert8.db - the prefix here is "slapd-01-"
1213 * this function examines the given certdir - if it looks like
1214 * /path/to/directory/prefix it will return the
1215 * /path/to/directory part in realcertdir, and the prefix in prefix
1218 tlsm_get_certdb_prefix( const char *certdir, char **realcertdir, char **prefix )
1220 char sep = PR_GetDirectorySeparator();
1222 struct PRFileInfo prfi;
1225 *realcertdir = (char *)certdir; /* default is the one passed in */
1227 /* if certdir is not given, just return */
1232 prc = PR_GetFileInfo( certdir, &prfi );
1233 /* if certdir exists (file or directory) then it cannot specify a prefix */
1234 if ( prc == PR_SUCCESS ) {
1238 /* if certdir was given, and there is a '/' in certdir, see if there
1239 is anything after the last '/' - if so, assume it is the prefix */
1240 if ( ( ( ptr = strrchr( certdir, sep ) ) ) && *(ptr+1) ) {
1241 *realcertdir = PL_strndup( certdir, ptr-certdir );
1242 *prefix = PL_strdup( ptr+1 );
1249 * This is the part of the init we defer until we get the
1250 * actual security configuration information. This is
1251 * only called once, protected by a PRCallOnce
1252 * NOTE: This must be done before the first call to SSL_ImportFD,
1253 * especially the setting of the policy
1254 * NOTE: This must be called after fork()
1257 tlsm_deferred_init( void *arg )
1259 tlsm_ctx *ctx = (tlsm_ctx *)arg;
1260 struct ldaptls *lt = ctx->tc_config;
1261 const char *securitydirs[3];
1264 PRErrorCode errcode = 1;
1265 #ifdef HAVE_NSS_INITCONTEXT
1266 NSSInitParameters initParams;
1267 NSSInitContext *initctx = NULL;
1272 #ifdef HAVE_NSS_INITCONTEXT
1273 memset( &initParams, 0, sizeof( initParams ) );
1274 initParams.length = sizeof( initParams );
1275 #endif /* HAVE_NSS_INITCONTEXT */
1277 #ifndef HAVE_NSS_INITCONTEXT
1278 if ( !NSS_IsInitialized() ) {
1279 #endif /* HAVE_NSS_INITCONTEXT */
1281 MOZNSS_DIR will override everything else - you can
1282 always set MOZNSS_DIR to force the use of this
1284 If using MOZNSS, specify the location of the moznss db dir
1285 in the cacertdir directive of the OpenLDAP configuration.
1286 DEFAULT_MOZNSS_DIR will only be used if the code cannot
1287 find a security dir to use based on the current
1291 securitydirs[nn++] = PR_GetEnv( "MOZNSS_DIR" );
1292 securitydirs[nn++] = lt->lt_cacertdir;
1293 securitydirs[nn++] = PR_GetEnv( "DEFAULT_MOZNSS_DIR" );
1294 for ( ii = 0; !done && ( ii < nn ); ++ii ) {
1295 char *realcertdir = NULL;
1296 const char *defprefix = "";
1297 char *prefix = (char *)defprefix;
1298 const char *securitydir = securitydirs[ii];
1299 if ( NULL == securitydir ) {
1303 tlsm_get_certdb_prefix( securitydir, &realcertdir, &prefix );
1304 #ifdef HAVE_NSS_INITCONTEXT
1305 #ifdef INITCONTEXT_HACK
1306 if ( !NSS_IsInitialized() && ctx->tc_is_server ) {
1307 rc = NSS_Initialize( realcertdir, prefix, prefix, SECMOD_DB, NSS_INIT_READONLY );
1309 initctx = NSS_InitContext( realcertdir, prefix, prefix, SECMOD_DB,
1310 &initParams, NSS_INIT_READONLY );
1311 rc = (initctx == NULL) ? SECFailure : SECSuccess;
1314 initctx = NSS_InitContext( realcertdir, prefix, prefix, SECMOD_DB,
1315 &initParams, NSS_INIT_READONLY );
1316 rc = (initctx == NULL) ? SECFailure : SECSuccess;
1319 rc = NSS_Initialize( realcertdir, prefix, prefix, SECMOD_DB, NSS_INIT_READONLY );
1322 if ( rc != SECSuccess ) {
1323 errcode = PORT_GetError();
1324 Debug( LDAP_DEBUG_TRACE,
1325 "TLS: could not initialize moznss using security dir %s prefix %s - error %d.\n",
1326 realcertdir, prefix, errcode );
1329 Debug( LDAP_DEBUG_TRACE, "TLS: using moznss security dir %s prefix %s.\n",
1330 realcertdir, prefix, 0 );
1334 if ( realcertdir != securitydir ) {
1335 PL_strfree( realcertdir );
1337 if ( prefix != defprefix ) {
1338 PL_strfree( prefix );
1342 if ( errcode ) { /* no moznss db found, or not using moznss db */
1343 #ifdef HAVE_NSS_INITCONTEXT
1344 int flags = NSS_INIT_READONLY|NSS_INIT_NOCERTDB|NSS_INIT_NOMODDB;
1345 #ifdef INITCONTEXT_HACK
1346 if ( !NSS_IsInitialized() && ctx->tc_is_server ) {
1347 rc = NSS_NoDB_Init( NULL );
1349 initctx = NSS_InitContext( "", "", "", SECMOD_DB,
1350 &initParams, flags );
1351 rc = (initctx == NULL) ? SECFailure : SECSuccess;
1354 initctx = NSS_InitContext( "", "", "", SECMOD_DB,
1355 &initParams, flags );
1356 rc = (initctx == NULL) ? SECFailure : SECSuccess;
1359 rc = NSS_NoDB_Init( NULL );
1361 if ( rc != SECSuccess ) {
1362 errcode = PORT_GetError();
1363 Debug( LDAP_DEBUG_ANY,
1364 "TLS: could not initialize moznss - error %d:%s.\n",
1365 errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ), 0 );
1369 #ifdef HAVE_NSS_INITCONTEXT
1370 ctx->tc_initctx = initctx;
1373 /* initialize the PEM module */
1374 if ( tlsm_init_pem_module() ) {
1375 errcode = PORT_GetError();
1376 Debug( LDAP_DEBUG_ANY,
1377 "TLS: could not initialize moznss PEM module - error %d:%s.\n",
1378 errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ), 0 );
1382 if ( tlsm_init_ca_certs( ctx, lt->lt_cacertfile, lt->lt_cacertdir ) ) {
1386 ctx->tc_using_pem = PR_TRUE;
1389 #ifdef HAVE_NSS_INITCONTEXT
1390 if ( !ctx->tc_initctx ) {
1391 ctx->tc_initctx = initctx;
1395 NSS_SetDomesticPolicy();
1397 PK11_SetPasswordFunc( tlsm_pin_prompt );
1399 /* register cleanup function */
1400 /* delete the old one, if any */
1401 NSS_UnregisterShutdown( tlsm_nss_shutdown_cb, NULL );
1402 NSS_RegisterShutdown( tlsm_nss_shutdown_cb, NULL );
1404 #ifndef HAVE_NSS_INITCONTEXT
1406 #endif /* HAVE_NSS_INITCONTEXT */
1412 tlsm_authenticate( tlsm_ctx *ctx, const char *certname, const char *pininfo )
1414 const char *colon = NULL;
1415 char *token_name = NULL;
1416 PK11SlotInfo *slot = NULL;
1419 if ( !certname || !*certname ) {
1423 if ( ( colon = PL_strchr( certname, ':' ) ) ) {
1424 token_name = PL_strndup( certname, colon-certname );
1428 slot = PK11_FindSlotByName( token_name );
1430 slot = PK11_GetInternalKeySlot();
1434 PRErrorCode errcode = PR_GetError();
1435 Debug( LDAP_DEBUG_ANY,
1436 "TLS: could not find the slot for security token %s - error %d:%s.\n",
1437 token_name ? token_name : DEFAULT_TOKEN_NAME, errcode,
1438 PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1442 rc = tlsm_authenticate_to_slot( ctx, slot );
1445 PL_strfree( token_name );
1447 PK11_FreeSlot( slot );
1454 * Find and verify the certificate.
1455 * Either fd is given, in which case the cert will be taken from it via SSL_PeerCertificate
1456 * or certname is given, and it will be searched for by name
1459 tlsm_find_and_verify_cert_key(tlsm_ctx *ctx, PRFileDesc *fd, const char *certname, int isServer, CERTCertificate **pRetCert, SECKEYPrivateKey **pRetKey)
1461 CERTCertificate *cert = NULL;
1463 void *pin_arg = NULL;
1464 SECKEYPrivateKey *key = NULL;
1466 pin_arg = SSL_RevealPinArg( fd );
1468 cert = PK11_FindCertFromNickname( certname, pin_arg );
1470 PRErrorCode errcode = PR_GetError();
1471 Debug( LDAP_DEBUG_ANY,
1472 "TLS: error: the certificate %s could not be found in the database - error %d:%s\n",
1473 certname, errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1477 /* we are verifying the peer cert
1478 we also need to swap the isServer meaning */
1479 cert = SSL_PeerCertificate( fd );
1481 PRErrorCode errcode = PR_GetError();
1482 Debug( LDAP_DEBUG_ANY,
1483 "TLS: error: could not get the certificate from the peer connection - error %d:%s\n",
1484 errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ), NULL );
1487 isServer = !isServer; /* verify the peer's cert instead */
1490 if ( ctx->tc_slotname ) {
1491 PK11SlotInfo *slot = PK11_FindSlotByName( ctx->tc_slotname );
1492 key = PK11_FindPrivateKeyFromCert( slot, cert, NULL );
1493 PK11_FreeSlot( slot );
1495 key = PK11_FindKeyByAnyCert( cert, pin_arg );
1499 SECCertificateUsage certUsage;
1500 PRBool checkSig = PR_TRUE;
1504 *pRetKey = key; /* caller will deal with this */
1506 SECKEY_DestroyPrivateKey( key );
1509 certUsage = certificateUsageSSLServer;
1511 certUsage = certificateUsageSSLClient;
1513 if ( ctx->tc_verify_cert ) {
1516 checkSig = PR_FALSE;
1518 status = CERT_VerifyCertificateNow( ctx->tc_certdb, cert,
1519 checkSig, certUsage,
1521 if ( status != SECSuccess ) {
1522 /* NSS doesn't like self-signed CA certs that are also used for
1523 TLS/SSL server certs (such as generated by openssl req -x509)
1524 CERT_VerifyCertificateNow returns SEC_ERROR_UNTRUSTED_ISSUER in that case
1525 so, see if the cert and issuer are the same cert
1527 PRErrorCode errcode = PR_GetError();
1529 if ( errcode == SEC_ERROR_UNTRUSTED_ISSUER ) {
1530 CERTCertificate *issuer = CERT_FindCertIssuer( cert, PR_Now(), certUsageSSLServer );
1531 if ( NULL == issuer ) {
1532 /* no issuer - fail */
1533 Debug( LDAP_DEBUG_ANY,
1534 "TLS: error: the server certificate %s has no issuer - "
1535 "please check this certificate for validity\n",
1537 } else if ( CERT_CompareCerts( cert, issuer ) ) {
1538 /* self signed - warn and allow */
1539 status = SECSuccess;
1541 Debug( LDAP_DEBUG_ANY,
1542 "TLS: warning: using self-signed server certificate %s\n",
1545 CERT_DestroyCertificate( issuer );
1548 if ( status != SECSuccess ) {
1549 Debug( LDAP_DEBUG_ANY,
1550 "TLS: error: the certificate %s is not valid - error %d:%s\n",
1551 certname, errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1554 rc = 0; /* success */
1557 PRErrorCode errcode = PR_GetError();
1558 Debug( LDAP_DEBUG_ANY,
1559 "TLS: error: could not find the private key for certificate %s - error %d:%s\n",
1560 certname, errcode, PR_ErrorToString( errcode, PR_LANGUAGE_I_DEFAULT ) );
1564 *pRetCert = cert; /* caller will deal with this */
1566 CERT_DestroyCertificate( cert );
1573 tlsm_get_client_auth_data( void *arg, PRFileDesc *fd,
1574 CERTDistNames *caNames, CERTCertificate **pRetCert,
1575 SECKEYPrivateKey **pRetKey )
1577 tlsm_ctx *ctx = (tlsm_ctx *)arg;
1580 /* don't need caNames - this function will call CERT_VerifyCertificateNow
1581 which will verify the cert against the known CAs */
1582 rc = tlsm_find_and_verify_cert_key( ctx, fd, ctx->tc_certname, 0, pRetCert, pRetKey );
1584 Debug( LDAP_DEBUG_ANY,
1585 "TLS: error: unable to perform client certificate authentication for "
1586 "certificate named %s\n", ctx->tc_certname, 0, 0 );
1594 * ctx must have a tc_model that is valid
1595 * certname is in the form [<tokenname>:]<certnickname>
1596 * where <tokenname> is the name of the PKCS11 token
1597 * and <certnickname> is the nickname of the cert/key in
1601 tlsm_clientauth_init( tlsm_ctx *ctx )
1603 SECStatus status = SECFailure;
1606 rc = tlsm_find_and_verify_cert_key( ctx, ctx->tc_model, ctx->tc_certname, 0, NULL, NULL );
1608 Debug( LDAP_DEBUG_ANY,
1609 "TLS: error: unable to set up client certificate authentication for "
1610 "certificate named %s\n", ctx->tc_certname, 0, 0 );
1614 status = SSL_GetClientAuthDataHook( ctx->tc_model,
1615 tlsm_get_client_auth_data,
1618 return ( status == SECSuccess ? 0 : -1 );
1622 * Tear down the TLS subsystem. Should only be called once.
1625 tlsm_destroy( void )
1630 tlsm_ctx_new ( struct ldapoptions *lo )
1634 ctx = LDAP_MALLOC( sizeof (*ctx) );
1637 #ifdef LDAP_R_COMPILE
1638 ldap_pvt_thread_mutex_init( &ctx->tc_refmutex );
1640 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 */
1641 ctx->tc_certdb = NULL;
1642 ctx->tc_certname = NULL;
1643 ctx->tc_pin_file = NULL;
1644 ctx->tc_model = NULL;
1645 memset(&ctx->tc_callonce, 0, sizeof(ctx->tc_callonce));
1646 ctx->tc_require_cert = lo->ldo_tls_require_cert;
1647 ctx->tc_verify_cert = PR_FALSE;
1648 ctx->tc_using_pem = PR_FALSE;
1649 ctx->tc_slotname = NULL;
1650 #ifdef HAVE_NSS_INITCONTEXT
1651 ctx->tc_initctx = NULL;
1652 #endif /* HAVE_NSS_INITCONTEXT */
1653 ctx->tc_pem_objs = NULL;
1654 ctx->tc_n_pem_objs = 0;
1656 return (tls_ctx *)ctx;
1660 tlsm_ctx_ref( tls_ctx *ctx )
1662 tlsm_ctx *c = (tlsm_ctx *)ctx;
1663 #ifdef LDAP_R_COMPILE
1664 ldap_pvt_thread_mutex_lock( &c->tc_refmutex );
1667 #ifdef LDAP_R_COMPILE
1668 ldap_pvt_thread_mutex_unlock( &c->tc_refmutex );
1673 tlsm_ctx_free ( tls_ctx *ctx )
1675 tlsm_ctx *c = (tlsm_ctx *)ctx;
1680 #ifdef LDAP_R_COMPILE
1681 ldap_pvt_thread_mutex_lock( &c->tc_refmutex );
1683 refcount = --c->tc_refcnt;
1684 #ifdef LDAP_R_COMPILE
1685 ldap_pvt_thread_mutex_unlock( &c->tc_refmutex );
1690 PR_Close( c->tc_model );
1691 c->tc_certdb = NULL; /* if not the default, may have to clean up */
1692 PL_strfree( c->tc_certname );
1693 c->tc_certname = NULL;
1694 PL_strfree( c->tc_pin_file );
1695 c->tc_pin_file = NULL;
1696 PL_strfree( c->tc_slotname );
1697 tlsm_free_pem_objs( c );
1698 #ifdef HAVE_NSS_INITCONTEXT
1700 NSS_ShutdownContext( c->tc_initctx );
1701 c->tc_initctx = NULL;
1702 #endif /* HAVE_NSS_INITCONTEXT */
1703 #ifdef LDAP_R_COMPILE
1704 ldap_pvt_thread_mutex_destroy( &c->tc_refmutex );
1710 * initialize a new TLS context
1713 tlsm_ctx_init( struct ldapoptions *lo, struct ldaptls *lt, int is_server )
1715 tlsm_ctx *ctx = (tlsm_ctx *)lo->ldo_tls_ctx;
1716 ctx->tc_is_server = is_server;
1722 tlsm_deferred_ctx_init( void *arg )
1724 tlsm_ctx *ctx = (tlsm_ctx *)arg;
1725 PRBool sslv2 = PR_FALSE;
1726 PRBool sslv3 = PR_TRUE;
1727 PRBool tlsv1 = PR_TRUE;
1728 PRBool request_cert = PR_FALSE;
1729 PRInt32 require_cert = PR_FALSE;
1733 if ( tlsm_deferred_init( ctx ) ) {
1734 Debug( LDAP_DEBUG_ANY,
1735 "TLS: could perform TLS system initialization.\n",
1740 ctx->tc_certdb = CERT_GetDefaultCertDB(); /* If there is ever a per-context db, change this */
1742 fd = PR_CreateIOLayerStub( tlsm_layer_id, &tlsm_PR_methods );
1744 ctx->tc_model = SSL_ImportFD( NULL, fd );
1747 if ( !ctx->tc_model ) {
1748 PRErrorCode err = PR_GetError();
1749 Debug( LDAP_DEBUG_ANY,
1750 "TLS: could perform TLS socket I/O layer initialization - error %d:%s.\n",
1751 err, PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ), NULL );
1759 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_SECURITY, PR_TRUE ) ) {
1760 Debug( LDAP_DEBUG_ANY,
1761 "TLS: could not set secure mode on.\n",
1766 lt = ctx->tc_config;
1768 /* default is sslv3 and tlsv1 */
1769 if ( lt->lt_protocol_min ) {
1770 if ( lt->lt_protocol_min > LDAP_OPT_X_TLS_PROTOCOL_SSL3 ) {
1772 } else if ( lt->lt_protocol_min <= LDAP_OPT_X_TLS_PROTOCOL_SSL2 ) {
1774 Debug( LDAP_DEBUG_ANY,
1775 "TLS: warning: minimum TLS protocol level set to "
1776 "include SSLv2 - SSLv2 is insecure - do not use\n", 0, 0, 0 );
1779 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_ENABLE_SSL2, sslv2 ) ) {
1780 Debug( LDAP_DEBUG_ANY,
1781 "TLS: could not set SSLv2 mode on.\n",
1785 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_ENABLE_SSL3, sslv3 ) ) {
1786 Debug( LDAP_DEBUG_ANY,
1787 "TLS: could not set SSLv3 mode on.\n",
1791 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_ENABLE_TLS, tlsv1 ) ) {
1792 Debug( LDAP_DEBUG_ANY,
1793 "TLS: could not set TLSv1 mode on.\n",
1798 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_HANDSHAKE_AS_CLIENT, !ctx->tc_is_server ) ) {
1799 Debug( LDAP_DEBUG_ANY,
1800 "TLS: could not set handshake as client.\n",
1804 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_HANDSHAKE_AS_SERVER, ctx->tc_is_server ) ) {
1805 Debug( LDAP_DEBUG_ANY,
1806 "TLS: could not set handshake as server.\n",
1811 if ( lt->lt_ciphersuite &&
1812 tlsm_parse_ciphers( ctx, lt->lt_ciphersuite )) {
1813 Debug( LDAP_DEBUG_ANY,
1814 "TLS: could not set cipher list %s.\n",
1815 lt->lt_ciphersuite, 0, 0 );
1819 if ( ctx->tc_require_cert ) {
1820 request_cert = PR_TRUE;
1821 require_cert = SSL_REQUIRE_NO_ERROR;
1822 if ( ctx->tc_require_cert == LDAP_OPT_X_TLS_DEMAND ||
1823 ctx->tc_require_cert == LDAP_OPT_X_TLS_HARD ) {
1824 require_cert = SSL_REQUIRE_ALWAYS;
1826 if ( ctx->tc_require_cert != LDAP_OPT_X_TLS_ALLOW )
1827 ctx->tc_verify_cert = PR_TRUE;
1829 ctx->tc_verify_cert = PR_FALSE;
1832 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_REQUEST_CERTIFICATE, request_cert ) ) {
1833 Debug( LDAP_DEBUG_ANY,
1834 "TLS: could not set request certificate mode.\n",
1839 if ( SECSuccess != SSL_OptionSet( ctx->tc_model, SSL_REQUIRE_CERTIFICATE, require_cert ) ) {
1840 Debug( LDAP_DEBUG_ANY,
1841 "TLS: could not set require certificate mode.\n",
1846 /* set up our cert and key, if any */
1847 if ( lt->lt_certfile ) {
1848 /* if using the PEM module, load the PEM file specified by lt_certfile */
1849 /* otherwise, assume this is the name of a cert already in the db */
1850 if ( ctx->tc_using_pem ) {
1851 /* this sets ctx->tc_certname to the correct value */
1852 int rc = tlsm_add_cert_from_file( ctx, lt->lt_certfile, PR_FALSE /* not a ca */ );
1857 PL_strfree( ctx->tc_certname );
1858 ctx->tc_certname = PL_strdup( lt->lt_certfile );
1862 if ( lt->lt_keyfile ) {
1863 /* if using the PEM module, load the PEM file specified by lt_keyfile */
1864 /* otherwise, assume this is the pininfo for the key */
1865 if ( ctx->tc_using_pem ) {
1866 /* this sets ctx->tc_certname to the correct value */
1867 int rc = tlsm_add_key_from_file( ctx, lt->lt_keyfile );
1872 PL_strfree( ctx->tc_pin_file );
1873 ctx->tc_pin_file = PL_strdup( lt->lt_keyfile );
1877 /* Set up callbacks for use by clients */
1878 if ( !ctx->tc_is_server ) {
1879 if ( SSL_OptionSet( ctx->tc_model, SSL_NO_CACHE, PR_TRUE ) != SECSuccess ) {
1880 PRErrorCode err = PR_GetError();
1881 Debug( LDAP_DEBUG_ANY,
1882 "TLS: error: could not set nocache option for moznss - error %d:%s\n",
1883 err, PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ), NULL );
1887 if ( SSL_BadCertHook( ctx->tc_model, tlsm_bad_cert_handler, ctx ) != SECSuccess ) {
1888 PRErrorCode err = PR_GetError();
1889 Debug( LDAP_DEBUG_ANY,
1890 "TLS: error: could not set bad cert handler for moznss - error %d:%s\n",
1891 err, PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ), NULL );
1896 since a cert has been specified, assume the client wants to do cert auth
1898 if ( ctx->tc_certname ) {
1899 if ( tlsm_authenticate( ctx, ctx->tc_certname, ctx->tc_pin_file ) ) {
1900 Debug( LDAP_DEBUG_ANY,
1901 "TLS: error: unable to authenticate to the security device for certificate %s\n",
1902 ctx->tc_certname, 0, 0 );
1905 if ( tlsm_clientauth_init( ctx ) ) {
1906 Debug( LDAP_DEBUG_ANY,
1907 "TLS: error: unable to set up client certificate authentication using %s\n",
1908 ctx->tc_certname, 0, 0 );
1912 } else { /* set up secure server */
1914 CERTCertificate *serverCert;
1915 SECKEYPrivateKey *serverKey;
1918 /* must have a certificate for the server to use */
1919 if ( !ctx->tc_certname ) {
1920 Debug( LDAP_DEBUG_ANY,
1921 "TLS: error: no server certificate: must specify a certificate for the server to use\n",
1926 /* authenticate to the server's token - this will do nothing
1927 if the key/cert db is not password protected */
1928 if ( tlsm_authenticate( ctx, ctx->tc_certname, ctx->tc_pin_file ) ) {
1929 Debug( LDAP_DEBUG_ANY,
1930 "TLS: error: unable to authenticate to the security device for certificate %s\n",
1931 ctx->tc_certname, 0, 0 );
1935 /* get the server's key and cert */
1936 if ( tlsm_find_and_verify_cert_key( ctx, ctx->tc_model, ctx->tc_certname, ctx->tc_is_server,
1937 &serverCert, &serverKey ) ) {
1938 Debug( LDAP_DEBUG_ANY,
1939 "TLS: error: unable to find and verify server's cert and key for certificate %s\n",
1940 ctx->tc_certname, 0, 0 );
1944 certKEA = NSS_FindCertKEAType( serverCert );
1945 /* configure the socket to be a secure server socket */
1946 status = SSL_ConfigSecureServer( ctx->tc_model, serverCert, serverKey, certKEA );
1947 /* SSL_ConfigSecureServer copies these */
1948 CERT_DestroyCertificate( serverCert );
1949 SECKEY_DestroyPrivateKey( serverKey );
1951 if ( SECSuccess != status ) {
1952 PRErrorCode err = PR_GetError();
1953 Debug( LDAP_DEBUG_ANY,
1954 "TLS: error: unable to configure secure server using certificate %s - error %d:%s\n",
1955 ctx->tc_certname, err, PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ) );
1960 /* Callback for authenticating certificate */
1961 if ( SSL_AuthCertificateHook( ctx->tc_model, tlsm_auth_cert_handler,
1962 ctx->tc_certdb ) != SECSuccess ) {
1963 PRErrorCode err = PR_GetError();
1964 Debug( LDAP_DEBUG_ANY,
1965 "TLS: error: could not set auth cert handler for moznss - error %d:%s\n",
1966 err, PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ), NULL );
1974 tlsm_session *session;
1975 Sockbuf_IO_Desc *sbiod;
1976 /* there seems to be no portable way to determine if the
1977 sockbuf sd has been set to nonblocking mode - the
1978 call to ber_pvt_socket_set_nonblock() takes place
1979 before the tls socket is set up, so we cannot
1980 intercept that call either.
1981 On systems where fcntl is available, we can just
1982 F_GETFL and test for O_NONBLOCK. On other systems,
1983 we will just see if the IO op returns EAGAIN or EWOULDBLOCK,
1984 and just set this flag */
1989 tlsm_is_io_ready( PRFileDesc *fd, PRInt16 in_flags, PRInt16 *out_flags )
1992 PRFileDesc *pollfd = NULL;
1994 PRPollDesc polldesc;
1997 myfd = PR_GetIdentitiesLayer( fd, tlsm_layer_id );
2003 p = (struct tls_data *)myfd->secret;
2005 if ( p == NULL || p->sbiod == NULL ) {
2009 /* wrap the sockbuf fd with a NSPR FD created especially
2010 for use with polling, and only with polling */
2011 pollfd = PR_CreateSocketPollFd( p->sbiod->sbiod_sb->sb_fd );
2012 polldesc.fd = pollfd;
2013 polldesc.in_flags = in_flags;
2014 polldesc.out_flags = 0;
2016 /* do the poll - no waiting, no blocking */
2017 rc = PR_Poll( &polldesc, 1, PR_INTERVAL_NO_WAIT );
2019 /* unwrap the socket */
2020 PR_DestroySocketPollFd( pollfd );
2022 /* rc will be either 1 if IO is ready, 0 if IO is not
2023 ready, or -1 if there was some error (and the caller
2024 should use PR_GetError() to figure out what */
2026 *out_flags = polldesc.out_flags;
2031 static tls_session *
2032 tlsm_session_new ( tls_ctx * ctx, int is_server )
2034 tlsm_ctx *c = (tlsm_ctx *)ctx;
2035 tlsm_session *session;
2039 c->tc_is_server = is_server;
2040 status = PR_CallOnceWithArg( &c->tc_callonce, tlsm_deferred_ctx_init, c );
2041 if ( PR_SUCCESS != status ) {
2042 PRErrorCode err = PR_GetError();
2043 Debug( LDAP_DEBUG_ANY,
2044 "TLS: error: could not initialize moznss security context - error %d:%s\n",
2045 err, PR_ErrorToString(err, PR_LANGUAGE_I_DEFAULT), NULL );
2049 fd = PR_CreateIOLayerStub( tlsm_layer_id, &tlsm_PR_methods );
2054 session = SSL_ImportFD( c->tc_model, fd );
2061 /* 0 means use the defaults here */
2062 SSL_ConfigServerSessionIDCache( 0, 0, 0, NULL );
2065 return (tls_session *)session;
2069 tlsm_session_accept( tls_session *session )
2071 tlsm_session *s = (tlsm_session *)session;
2074 int waitcounter = 0;
2076 rc = SSL_ResetHandshake( s, PR_TRUE /* server */ );
2078 err = PR_GetError();
2079 Debug( LDAP_DEBUG_TRACE,
2080 "TLS: error: accept - reset handshake failure %d - error %d:%s\n",
2082 err ? PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ) : "unknown" );
2091 rc = SSL_ForceHandshake( s );
2092 if (rc == SECSuccess) {
2096 err = PR_GetError();
2097 if ( errno == EAGAIN || errno == EWOULDBLOCK ) {
2099 in_flags = PR_POLL_READ | PR_POLL_EXCEPT;
2102 filesReady = tlsm_is_io_ready( s, in_flags, &out_flags );
2103 if ( filesReady < 0 ) {
2104 err = PR_GetError();
2105 Debug( LDAP_DEBUG_ANY,
2106 "TLS: error: accept - error waiting for socket to be ready: %d - error %d:%s\n",
2108 err ? PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ) : "unknown" );
2110 break; /* hard error */
2111 } else if ( out_flags & PR_POLL_NVAL ) {
2112 PR_SetError(PR_BAD_DESCRIPTOR_ERROR, 0);
2113 Debug( LDAP_DEBUG_ANY,
2114 "TLS: error: accept failure - invalid socket\n",
2118 } else if ( out_flags & PR_POLL_EXCEPT ) {
2119 err = PR_GetError();
2120 Debug( LDAP_DEBUG_ANY,
2121 "TLS: error: accept - error waiting for socket to be ready: %d - error %d:%s\n",
2123 err ? PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ) : "unknown" );
2125 break; /* hard error */
2127 } else { /* hard error */
2128 err = PR_GetError();
2129 Debug( LDAP_DEBUG_ANY,
2130 "TLS: error: accept - force handshake failure: %d - error %d:%s\n",
2132 err ? PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ) : "unknown" );
2134 break; /* hard error */
2136 } while (rc == SECFailure);
2138 Debug( LDAP_DEBUG_TRACE,
2139 "TLS: accept completed after %d waits\n", waitcounter, NULL, NULL );
2145 tlsm_session_connect( LDAP *ld, tls_session *session )
2147 tlsm_session *s = (tlsm_session *)session;
2151 rc = SSL_ResetHandshake( s, PR_FALSE /* server */ );
2153 err = PR_GetError();
2154 Debug( LDAP_DEBUG_TRACE,
2155 "TLS: error: connect - reset handshake failure %d - error %d:%s\n",
2157 err ? PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ) : "unknown" );
2160 rc = SSL_ForceHandshake( s );
2162 err = PR_GetError();
2163 Debug( LDAP_DEBUG_TRACE,
2164 "TLS: error: connect - force handshake failure %d - error %d:%s\n",
2166 err ? PR_ErrorToString( err, PR_LANGUAGE_I_DEFAULT ) : "unknown" );
2173 tlsm_session_upflags( Sockbuf *sb, tls_session *session, int rc )
2175 /* Should never happen */
2178 if ( rc != PR_PENDING_INTERRUPT_ERROR && rc != PR_WOULD_BLOCK_ERROR )
2184 tlsm_session_errmsg( tls_session *sess, int rc, char *buf, size_t len )
2189 i = PR_GetErrorTextLength();
2191 char *msg = LDAP_MALLOC( i+1 );
2192 PR_GetErrorText( msg );
2193 memcpy( buf, msg, len );
2196 PR_GetErrorText( buf );
2199 return i ? buf : NULL;
2203 tlsm_session_my_dn( tls_session *session, struct berval *der_dn )
2205 tlsm_session *s = (tlsm_session *)session;
2206 CERTCertificate *cert;
2208 cert = SSL_LocalCertificate( s );
2209 if (!cert) return LDAP_INVALID_CREDENTIALS;
2211 der_dn->bv_val = (char *)cert->derSubject.data;
2212 der_dn->bv_len = cert->derSubject.len;
2213 CERT_DestroyCertificate( cert );
2218 tlsm_session_peer_dn( tls_session *session, struct berval *der_dn )
2220 tlsm_session *s = (tlsm_session *)session;
2221 CERTCertificate *cert;
2223 cert = SSL_PeerCertificate( s );
2224 if (!cert) return LDAP_INVALID_CREDENTIALS;
2226 der_dn->bv_val = (char *)cert->derSubject.data;
2227 der_dn->bv_len = cert->derSubject.len;
2228 CERT_DestroyCertificate( cert );
2232 /* what kind of hostname were we given? */
2238 tlsm_session_chkhost( LDAP *ld, tls_session *session, const char *name_in )
2240 tlsm_session *s = (tlsm_session *)session;
2241 CERTCertificate *cert;
2242 const char *name, *domain = NULL, *ptr;
2243 int ret, ntype = IS_DNS, nlen, dlen;
2244 #ifdef LDAP_PF_INET6
2245 struct in6_addr addr;
2247 struct in_addr addr;
2252 if( ldap_int_hostname &&
2253 ( !name_in || !strcasecmp( name_in, "localhost" ) ) )
2255 name = ldap_int_hostname;
2259 nlen = strlen( name );
2261 cert = SSL_PeerCertificate( s );
2263 Debug( LDAP_DEBUG_ANY,
2264 "TLS: unable to get peer certificate.\n",
2266 /* if this was a fatal condition, things would have
2267 * aborted long before now.
2269 return LDAP_SUCCESS;
2272 #ifdef LDAP_PF_INET6
2273 if (name[0] == '[' && strchr(name, ']')) {
2274 char *n2 = ldap_strdup(name+1);
2275 *strchr(n2, ']') = 0;
2276 if (inet_pton(AF_INET6, n2, &addr))
2281 if ((ptr = strrchr(name, '.')) && isdigit((unsigned char)ptr[1])) {
2282 if (inet_aton(name, (struct in_addr *)&addr)) ntype = IS_IP4;
2284 if (ntype == IS_DNS ) {
2285 domain = strchr( name, '.' );
2287 dlen = nlen - ( domain - name );
2290 ret = LDAP_LOCAL_ERROR;
2292 rv = CERT_FindCertExtension( cert, SEC_OID_X509_SUBJECT_ALT_NAME,
2294 if ( rv == SECSuccess && altname.data ) {
2296 CERTGeneralName *names, *cur;
2298 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
2300 ret = LDAP_NO_MEMORY;
2304 names = cur = CERT_DecodeAltNameExtension(arena, &altname);
2313 if ( !cur->name.other.len ) continue;
2315 host = (char *)cur->name.other.data;
2316 hlen = cur->name.other.len;
2318 if ( cur->type == certDNSName ) {
2319 if ( ntype != IS_DNS ) continue;
2321 /* is this an exact match? */
2322 if ( nlen == hlen && !strncasecmp( name, host, nlen )) {
2327 /* is this a wildcard match? */
2328 if ( domain && host[0] == '*' && host[1] == '.' &&
2329 dlen == hlen-1 && !strncasecmp( domain, host+1, dlen )) {
2333 } else if ( cur->type == certIPAddress ) {
2334 if ( ntype == IS_DNS ) continue;
2336 #ifdef LDAP_PF_INET6
2337 if (ntype == IS_IP6 && hlen != sizeof(struct in6_addr)) {
2341 if (ntype == IS_IP4 && hlen != sizeof(struct in_addr)) {
2344 if (!memcmp(host, &addr, hlen)) {
2349 } while (( cur = CERT_GetNextGeneralName( cur )) != names );
2351 PORT_FreeArena( arena, PR_FALSE );
2352 SECITEM_FreeItem( &altname, PR_FALSE );
2354 /* no altnames matched, try the CN */
2355 if ( ret != LDAP_SUCCESS ) {
2356 /* find the last CN */
2357 CERTRDN *rdn, **rdns;
2358 CERTAVA *lastava = NULL;
2362 rdns = cert->subject.rdns;
2363 while ( rdns && ( rdn = *rdns++ )) {
2364 CERTAVA *ava, **avas = rdn->avas;
2365 while ( avas && ( ava = *avas++ )) {
2366 if ( CERT_GetAVATag( ava ) == SEC_OID_AVA_COMMON_NAME )
2371 SECItem *av = CERT_DecodeAVAValue( &lastava->value );
2373 if ( av->len == nlen && !strncasecmp( name, (char *)av->data, nlen )) {
2375 } else if ( av->data[0] == '*' && av->data[1] == '.' &&
2376 domain && dlen == av->len - 1 && !strncasecmp( name,
2377 (char *)(av->data+1), dlen )) {
2381 if ( len >= sizeof(buf) )
2382 len = sizeof(buf)-1;
2383 memcpy( buf, av->data, len );
2386 SECITEM_FreeItem( av, PR_TRUE );
2389 if ( ret != LDAP_SUCCESS ) {
2390 Debug( LDAP_DEBUG_ANY, "TLS: hostname (%s) does not match "
2391 "common name in certificate (%s).\n",
2393 ret = LDAP_CONNECT_ERROR;
2394 if ( ld->ld_error ) {
2395 LDAP_FREE( ld->ld_error );
2397 ld->ld_error = LDAP_STRDUP(
2398 _("TLS: hostname does not match CN in peer certificate"));
2403 CERT_DestroyCertificate( cert );
2408 tlsm_session_strength( tls_session *session )
2410 tlsm_session *s = (tlsm_session *)session;
2413 rc = SSL_SecurityStatus( s, NULL, NULL, NULL, &keySize,
2415 return rc ? 0 : keySize;
2419 * TLS support for LBER Sockbufs
2422 static PRStatus PR_CALLBACK
2423 tlsm_PR_Close(PRFileDesc *fd)
2425 int rc = PR_SUCCESS;
2427 /* we don't need to actually close anything here, just
2428 pop our io layer off the stack */
2429 fd->secret = NULL; /* must have been freed before calling PR_Close */
2431 fd = PR_PopIOLayer( fd, tlsm_layer_id );
2432 /* if we are not the last layer, pass the close along */
2437 rc = fd->methods->close( fd );
2440 /* we are the last layer - just call our dtor */
2447 static PRStatus PR_CALLBACK
2448 tlsm_PR_Shutdown(PRFileDesc *fd, PRShutdownHow how)
2450 int rc = PR_SUCCESS;
2453 rc = PR_Shutdown( fd->lower, how );
2459 static int PR_CALLBACK
2460 tlsm_PR_Recv(PRFileDesc *fd, void *buf, PRInt32 len, PRIntn flags,
2461 PRIntervalTime timeout)
2466 if ( buf == NULL || len <= 0 ) return 0;
2468 p = (struct tls_data *)fd->secret;
2470 if ( p == NULL || p->sbiod == NULL ) {
2474 rc = LBER_SBIOD_READ_NEXT( p->sbiod, buf, len );
2476 tlsm_map_error( errno );
2477 if ( errno == EAGAIN || errno == EWOULDBLOCK ) {
2478 p->nonblock = PR_TRUE; /* fd is using non-blocking io */
2479 } else if ( errno ) { /* real error */
2480 Debug( LDAP_DEBUG_TRACE,
2481 "TLS: error: tlsm_PR_Recv returned %d - error %d:%s\n",
2482 rc, errno, STRERROR(errno) );
2489 static int PR_CALLBACK
2490 tlsm_PR_Send(PRFileDesc *fd, const void *buf, PRInt32 len, PRIntn flags,
2491 PRIntervalTime timeout)
2496 if ( buf == NULL || len <= 0 ) return 0;
2498 p = (struct tls_data *)fd->secret;
2500 if ( p == NULL || p->sbiod == NULL ) {
2504 rc = LBER_SBIOD_WRITE_NEXT( p->sbiod, (char *)buf, len );
2506 tlsm_map_error( errno );
2507 if ( errno == EAGAIN || errno == EWOULDBLOCK ) {
2508 p->nonblock = PR_TRUE;
2509 } else if ( errno ) { /* real error */
2510 Debug( LDAP_DEBUG_TRACE,
2511 "TLS: error: tlsm_PR_Send returned %d - error %d:%s\n",
2512 rc, errno, STRERROR(errno) );
2519 static int PR_CALLBACK
2520 tlsm_PR_Read(PRFileDesc *fd, void *buf, PRInt32 len)
2522 return tlsm_PR_Recv( fd, buf, len, 0, PR_INTERVAL_NO_TIMEOUT );
2525 static int PR_CALLBACK
2526 tlsm_PR_Write(PRFileDesc *fd, const void *buf, PRInt32 len)
2528 return tlsm_PR_Send( fd, buf, len, 0, PR_INTERVAL_NO_TIMEOUT );
2531 static PRStatus PR_CALLBACK
2532 tlsm_PR_GetPeerName(PRFileDesc *fd, PRNetAddr *addr)
2537 p = (struct tls_data *)fd->secret;
2539 if ( p == NULL || p->sbiod == NULL ) {
2542 len = sizeof(PRNetAddr);
2543 return getpeername( p->sbiod->sbiod_sb->sb_fd, (struct sockaddr *)addr, &len );
2546 static PRStatus PR_CALLBACK
2547 tlsm_PR_GetSocketOption(PRFileDesc *fd, PRSocketOptionData *data)
2550 p = (struct tls_data *)fd->secret;
2556 /* only the nonblocking option is supported at this time
2557 MozNSS SSL code needs it */
2558 if ( data->option != PR_SockOpt_Nonblocking ) {
2559 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
2563 int flags = fcntl( p->sbiod->sbiod_sb->sb_fd, F_GETFL );
2564 data->value.non_blocking = (flags & O_NONBLOCK) ? PR_TRUE : PR_FALSE;
2566 data->value.non_blocking = p->nonblock;
2571 static PRStatus PR_CALLBACK
2574 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
2578 static PRFileDesc * PR_CALLBACK
2581 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
2585 static PRInt16 PR_CALLBACK
2588 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
2592 static PRInt32 PR_CALLBACK
2595 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
2599 static PRInt64 PR_CALLBACK
2604 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
2609 static const PRIOMethods tlsm_PR_methods = {
2611 tlsm_PR_Close, /* close */
2612 tlsm_PR_Read, /* read */
2613 tlsm_PR_Write, /* write */
2614 tlsm_PR_i32_unimp, /* available */
2615 tlsm_PR_i64_unimp, /* available64 */
2616 tlsm_PR_prs_unimp, /* fsync */
2617 tlsm_PR_i32_unimp, /* seek */
2618 tlsm_PR_i64_unimp, /* seek64 */
2619 tlsm_PR_prs_unimp, /* fileInfo */
2620 tlsm_PR_prs_unimp, /* fileInfo64 */
2621 tlsm_PR_i32_unimp, /* writev */
2622 tlsm_PR_prs_unimp, /* connect */
2623 tlsm_PR_pfd_unimp, /* accept */
2624 tlsm_PR_prs_unimp, /* bind */
2625 tlsm_PR_prs_unimp, /* listen */
2626 (PRShutdownFN)tlsm_PR_Shutdown, /* shutdown */
2627 tlsm_PR_Recv, /* recv */
2628 tlsm_PR_Send, /* send */
2629 tlsm_PR_i32_unimp, /* recvfrom */
2630 tlsm_PR_i32_unimp, /* sendto */
2631 (PRPollFN)tlsm_PR_i16_unimp, /* poll */
2632 tlsm_PR_i32_unimp, /* acceptread */
2633 tlsm_PR_i32_unimp, /* transmitfile */
2634 tlsm_PR_prs_unimp, /* getsockname */
2635 tlsm_PR_GetPeerName, /* getpeername */
2636 tlsm_PR_i32_unimp, /* getsockopt OBSOLETE */
2637 tlsm_PR_i32_unimp, /* setsockopt OBSOLETE */
2638 tlsm_PR_GetSocketOption, /* getsocketoption */
2639 tlsm_PR_i32_unimp, /* setsocketoption */
2640 tlsm_PR_i32_unimp, /* Send a (partial) file with header/trailer*/
2641 (PRConnectcontinueFN)tlsm_PR_prs_unimp, /* connectcontinue */
2642 tlsm_PR_i32_unimp, /* reserved for future use */
2643 tlsm_PR_i32_unimp, /* reserved for future use */
2644 tlsm_PR_i32_unimp, /* reserved for future use */
2645 tlsm_PR_i32_unimp /* reserved for future use */
2649 * Initialize TLS subsystem. Should be called only once.
2650 * See tlsm_deferred_init for the bulk of the init process
2657 tlsm_layer_id = PR_GetUniqueIdentity( "OpenLDAP" );
2663 tlsm_sb_setup( Sockbuf_IO_Desc *sbiod, void *arg )
2666 tlsm_session *session = arg;
2669 assert( sbiod != NULL );
2671 p = LBER_MALLOC( sizeof( *p ) );
2676 fd = PR_GetIdentitiesLayer( session, tlsm_layer_id );
2682 fd->secret = (PRFilePrivate *)p;
2683 p->session = session;
2685 sbiod->sbiod_pvt = p;
2690 tlsm_sb_remove( Sockbuf_IO_Desc *sbiod )
2694 assert( sbiod != NULL );
2695 assert( sbiod->sbiod_pvt != NULL );
2697 p = (struct tls_data *)sbiod->sbiod_pvt;
2698 PR_Close( p->session );
2699 LBER_FREE( sbiod->sbiod_pvt );
2700 sbiod->sbiod_pvt = NULL;
2705 tlsm_sb_close( Sockbuf_IO_Desc *sbiod )
2709 assert( sbiod != NULL );
2710 assert( sbiod->sbiod_pvt != NULL );
2712 p = (struct tls_data *)sbiod->sbiod_pvt;
2713 PR_Shutdown( p->session, PR_SHUTDOWN_BOTH );
2718 tlsm_sb_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
2722 assert( sbiod != NULL );
2723 assert( sbiod->sbiod_pvt != NULL );
2725 p = (struct tls_data *)sbiod->sbiod_pvt;
2727 if ( opt == LBER_SB_OPT_GET_SSL ) {
2728 *((tlsm_session **)arg) = p->session;
2731 } else if ( opt == LBER_SB_OPT_DATA_READY ) {
2732 if ( tlsm_is_io_ready( p->session, PR_POLL_READ, NULL ) > 0 ) {
2738 return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg );
2742 tlsm_sb_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
2748 assert( sbiod != NULL );
2749 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
2751 p = (struct tls_data *)sbiod->sbiod_pvt;
2753 ret = PR_Recv( p->session, buf, len, 0, PR_INTERVAL_NO_TIMEOUT );
2755 err = PR_GetError();
2756 if ( err == PR_PENDING_INTERRUPT_ERROR || err == PR_WOULD_BLOCK_ERROR ) {
2757 sbiod->sbiod_sb->sb_trans_needs_read = 1;
2758 sock_errset(EWOULDBLOCK);
2761 sbiod->sbiod_sb->sb_trans_needs_read = 0;
2767 tlsm_sb_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
2773 assert( sbiod != NULL );
2774 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
2776 p = (struct tls_data *)sbiod->sbiod_pvt;
2778 ret = PR_Send( p->session, (char *)buf, len, 0, PR_INTERVAL_NO_TIMEOUT );
2780 err = PR_GetError();
2781 if ( err == PR_PENDING_INTERRUPT_ERROR || err == PR_WOULD_BLOCK_ERROR ) {
2782 sbiod->sbiod_sb->sb_trans_needs_write = 1;
2783 sock_errset(EWOULDBLOCK);
2787 sbiod->sbiod_sb->sb_trans_needs_write = 0;
2792 static Sockbuf_IO tlsm_sbio =
2794 tlsm_sb_setup, /* sbi_setup */
2795 tlsm_sb_remove, /* sbi_remove */
2796 tlsm_sb_ctrl, /* sbi_ctrl */
2797 tlsm_sb_read, /* sbi_read */
2798 tlsm_sb_write, /* sbi_write */
2799 tlsm_sb_close /* sbi_close */
2802 tls_impl ldap_int_tls_impl = {
2814 tlsm_session_connect,
2815 tlsm_session_accept,
2816 tlsm_session_upflags,
2817 tlsm_session_errmsg,
2819 tlsm_session_peer_dn,
2820 tlsm_session_chkhost,
2821 tlsm_session_strength,
2825 #ifdef LDAP_R_COMPILE
2834 #endif /* HAVE_MOZNSS */