1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580
|
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998-1999
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Sun Microsystems
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/*
* ldapsinit.c
*/
#if defined(NET_SSL)
#if defined( _WINDOWS )
#include <windows.h>
#endif
/* XXX:mhein The following is a workaround for the redefinition of */
/* const problem on OSF. Fix to be provided by NSS */
/* This is a pretty benign workaround for us which */
/* should not cause problems in the future even if */
/* we forget to take it out :-) */
#ifdef OSF1V4D
#ifndef __STDC__
# define __STDC__
#endif /* __STDC__ */
#endif /* OSF1V4D */
#include <errno.h>
#include <nspr.h>
#include <cert.h>
#include <key.h>
#include <ssl.h>
#include <sslproto.h>
#include <sslerr.h>
#include <prnetdb.h>
#include <ldap.h>
#include <ldap_ssl.h>
#include <ldappr.h>
#include <pk11func.h>
/*
* Macro that determines how many SSL options we support. As of June, 2002
* NSS supports 14 options numbered 1-14 (see nss/ssl.h). We allow some
* room for expansion.
*/
#define LDAPSSL_MAX_SSL_OPTION 20
/*
* Data structure to hold the standard NSPR I/O function pointers set by
* libprldap. We save them in our session data structure so we can call
* them from our own I/O functions (we add functionality to support SSL
* while using libprldap's functions as much as possible).
*/
typedef struct ldapssl_std_functions {
LDAP_X_EXTIOF_CLOSE_CALLBACK *lssf_close_fn;
LDAP_X_EXTIOF_CONNECT_CALLBACK *lssf_connect_fn;
LDAP_X_EXTIOF_DISPOSEHANDLE_CALLBACK *lssf_disposehdl_fn;
} LDAPSSLStdFunctions;
/*
* LDAP session data structure.
*/
typedef struct ldapssl_session_info {
int lssei_using_pcks_fns;
int lssei_ssl_strength;
PRBool lssei_ssl_ready;
PRBool lssei_tls_init; /* indicates startTLS success */
PRBool lssei_client_auth;
PRBool lssei_ssl_option_value[LDAPSSL_MAX_SSL_OPTION+1];
PRBool lssei_ssl_option_isset[LDAPSSL_MAX_SSL_OPTION+1];
char *lssei_certnickname;
char *lssei_keypasswd; /* if NULL, assume pre-auth. */
LDAPSSLStdFunctions lssei_std_functions;
CERTCertDBHandle *lssei_certdbh;
} LDAPSSLSessionInfo;
/*
* LDAP socket data structure.
*/
typedef struct ldapssl_socket_info {
LDAPSSLSessionInfo *soi_sessioninfo; /* session info */
} LDAPSSLSocketInfo;
/*
* XXXceb This is a hack until the new IO functions are done.
* this function MUST be called before ldapssl_enable_clientauth.
* right now, this function is called in ldapssl_pkcs_init();
*/
static int using_pkcs_functions = 0;
void set_using_pkcs_functions( int val )
{
using_pkcs_functions = val;
}
/*
* Utility functions:
*/
static int set_ssl_options( PRFileDesc *sslfd, PRBool *optval,
PRBool *optisset );
static void ldapssl_free_session_info( LDAPSSLSessionInfo **ssipp );
static void ldapssl_free_socket_info( LDAPSSLSocketInfo **soipp );
static char *ldapssl_libldap_compat_strdup(const char *s1);
/*
* SSL Stuff
*/
static int ldapssl_AuthCertificate(void *sessionarg, PRFileDesc *fd,
PRBool checkSig, PRBool isServer);
/*
* client auth stuff
*/
static SECStatus get_clientauth_data( void *sessionarg, PRFileDesc *prfd,
CERTDistNames *caNames, CERTCertificate **pRetCert,
SECKEYPrivateKey **pRetKey );
static SECStatus get_keyandcert( LDAPSSLSessionInfo *ssip,
CERTCertificate **pRetCert, SECKEYPrivateKey **pRetKey,
char **errmsgp );
static SECStatus check_clientauth_nicknames_and_passwd( LDAP *ld,
LDAPSSLSessionInfo *ssip );
static char *get_keypassword( PK11SlotInfo *slot, PRBool retry,
void *sessionarg );
/*
* Static variables.
*/
/* SSL strength setting for new LDAPS sessions */
static int default_ssl_strength = LDAPSSL_AUTH_CERT;
/*
* Arrays to track global defaults for SSL options. These are used for
* new LDAPS sessions. For each option, we track both the option value
* and a Boolean that indicates whether the value has been set using
* the ldapssl_set_option() call. If an option has not been set, we
* don't make any NSS calls to set it; that way, the default NSS option
* values are used. Similar arrays are included in the LDAPSSLSessionInfo
* structure so options can be set on a per-LDAP session basis as well.
*/
static PRBool default_ssl_option_value[LDAPSSL_MAX_SSL_OPTION+1] = {0};
static PRBool default_ssl_option_isset[LDAPSSL_MAX_SSL_OPTION+1] = {0};
/*
* Like ldap_init(), except also install I/O routines from libsec so we
* can support SSL. If defsecure is non-zero, SSL is enabled for the
* default connection as well.
*/
LDAP *
LDAP_CALL
ldapssl_init( const char *defhost, int defport, int defsecure )
{
LDAP *ld;
if (0 ==defport)
defport = LDAPS_PORT;
if (( ld = ldap_init( defhost, defport )) == NULL ) {
return( NULL );
}
if ( ldapssl_install_routines( ld ) < 0 || ldap_set_option( ld,
LDAP_OPT_SSL, defsecure ? LDAP_OPT_ON : LDAP_OPT_OFF ) != 0 ) {
PR_SetError( PR_GetError(), EINVAL ); /* XXXmcs: just a guess! */
ldap_unbind( ld );
return( NULL );
}
return( ld );
}
static int
ldapssl_close(int s, struct lextiof_socket_private *socketarg)
{
PRLDAPSocketInfo soi;
LDAPSSLSocketInfo *ssoip;
LDAPSSLSessionInfo *sseip;
memset( &soi, 0, sizeof(soi));
soi.soinfo_size = PRLDAP_SOCKETINFO_SIZE;
if ( prldap_get_socket_info( s, socketarg, &soi ) != LDAP_SUCCESS ) {
return( -1 );
}
ssoip = (LDAPSSLSocketInfo *)soi.soinfo_appdata;
sseip = ssoip->soi_sessioninfo;
ldapssl_free_socket_info( (LDAPSSLSocketInfo **)&soi.soinfo_appdata );
return( (*(sseip->lssei_std_functions.lssf_close_fn))( s, socketarg ));
}
static int
ldapssl_connect(const char *hostlist, int defport, int timeout,
unsigned long options, struct lextiof_session_private *sessionarg,
struct lextiof_socket_private **socketargp )
{
int intfd = -1;
PRBool secure;
PRLDAPSessionInfo sei;
PRLDAPSocketInfo soi;
LDAPSSLSocketInfo *ssoip = NULL;
LDAPSSLSessionInfo *sseip;
PRFileDesc *sslfd = NULL;
/*
* Determine if secure option is set. Also, clear secure bit in options
* the we pass to the standard connect() function (since it doesn't know
* how to handle the secure option).
*/
if ( 0 != ( options & LDAP_X_EXTIOF_OPT_SECURE )) {
secure = PR_TRUE;
options &= ~LDAP_X_EXTIOF_OPT_SECURE;
} else {
secure = PR_FALSE;
}
/*
* Retrieve session info. so we can store a pointer to our session info.
* in our socket info. later.
*/
memset( &sei, 0, sizeof(sei));
sei.seinfo_size = PRLDAP_SESSIONINFO_SIZE;
if ( prldap_get_session_info( NULL, sessionarg, &sei ) != LDAP_SUCCESS ) {
return( -1 );
}
sseip = (LDAPSSLSessionInfo *)sei.seinfo_appdata;
/*
* Call the standard connect() callback to make the TCP connection. If it
* succeeds, *socketargp is set.
*/
intfd = (*(sseip->lssei_std_functions.lssf_connect_fn))( hostlist, defport,
timeout, options, sessionarg, socketargp );
if ( intfd < 0 ) {
return( intfd );
}
/*
* Retrieve socket info. so we have the PRFileDesc.
*/
memset( &soi, 0, sizeof(soi));
soi.soinfo_size = PRLDAP_SOCKETINFO_SIZE;
if ( prldap_get_socket_info( intfd, *socketargp, &soi ) != LDAP_SUCCESS ) {
goto close_socket_and_exit_with_error;
}
/*
* Allocate a structure to hold our socket-specific data.
*/
if ( NULL == ( ssoip = PR_Calloc( 1, sizeof( LDAPSSLSocketInfo )))) {
goto close_socket_and_exit_with_error;
}
ssoip->soi_sessioninfo = sseip;
/*
* Add SSL layer and enable SSL.
*/
if (( sslfd = SSL_ImportFD( NULL, soi.soinfo_prfd )) == NULL ) {
goto close_socket_and_exit_with_error;
}
if ( SSL_OptionSet( sslfd, SSL_SECURITY, secure ) != SECSuccess ||
SSL_OptionSet( sslfd, SSL_HANDSHAKE_AS_CLIENT, secure )
!= SECSuccess || ( secure && SSL_ResetHandshake( sslfd,
PR_FALSE ) != SECSuccess )) {
goto close_socket_and_exit_with_error;
}
/*
* Set hostname which will be retrieved (depending on ssl strength) when
* using client or server auth.
*/
if ( SSL_SetURL( sslfd, hostlist ) != SECSuccess ) {
goto close_socket_and_exit_with_error;
}
/*
* Set any SSL options that were modified by a previous call to
* the ldapssl_set_option() function.
*/
if ( set_ssl_options( sslfd, sseip->lssei_ssl_option_value,
sseip->lssei_ssl_option_isset ) < 0 ) {
goto close_socket_and_exit_with_error;
}
/*
* If using client auth., arrange to use a separate SSL session cache
* for each distinct local client certificate nickname.
*/
if ( sseip->lssei_client_auth &&
sseip->lssei_certnickname && sseip->lssei_certnickname[0] &&
SSL_SetSockPeerID( sslfd, sseip->lssei_certnickname) != SECSuccess ) {
goto close_socket_and_exit_with_error;
}
/*
* Let the standard NSPR to LDAP layer know about the new socket and
* our own socket-specific data.
*/
soi.soinfo_prfd = sslfd;
soi.soinfo_appdata = (void *)ssoip;
if ( prldap_set_socket_info( intfd, *socketargp, &soi ) != LDAP_SUCCESS ) {
goto close_socket_and_exit_with_error;
}
sslfd = NULL; /* so we don't close the socket twice upon error */
/*
* Install certificate hook function.
*/
SSL_AuthCertificateHook( soi.soinfo_prfd,
(SSLAuthCertificate)ldapssl_AuthCertificate,
(void *)sseip );
if ( SSL_GetClientAuthDataHook( soi.soinfo_prfd,
get_clientauth_data, sseip->lssei_client_auth ? sseip : NULL ) != 0 ) {
goto close_socket_and_exit_with_error;
}
return( intfd ); /* success */
close_socket_and_exit_with_error:
if ( NULL != sslfd && sslfd != soi.soinfo_prfd ) {
PR_Close( sslfd );
}
if ( NULL != ssoip ) {
ldapssl_free_socket_info( &ssoip );
}
if ( intfd >= 0 && NULL != *socketargp ) {
(*(sseip->lssei_std_functions.lssf_close_fn))( intfd, *socketargp );
}
return( -1 );
}
static void
ldapssl_disposehandle(LDAP *ld, struct lextiof_session_private *sessionarg)
{
PRLDAPSessionInfo sei;
LDAPSSLSessionInfo *sseip;
LDAP_X_EXTIOF_DISPOSEHANDLE_CALLBACK *disposehdl_fn;
memset( &sei, 0, sizeof( sei ));
sei.seinfo_size = PRLDAP_SESSIONINFO_SIZE;
if ( prldap_get_session_info( ld, NULL, &sei ) == LDAP_SUCCESS ) {
sseip = (LDAPSSLSessionInfo *)sei.seinfo_appdata;
disposehdl_fn = sseip->lssei_std_functions.lssf_disposehdl_fn;
ldapssl_free_session_info( &sseip );
(*disposehdl_fn)( ld, sessionarg );
}
}
static
LDAPSSLSessionInfo *
ldapssl_alloc_sessioninfo()
{
LDAPSSLSessionInfo *ssip;
/*
* Allocate our own session information.
*/
if ( NULL == ( ssip = (LDAPSSLSessionInfo *)PR_Calloc( 1,
sizeof( LDAPSSLSessionInfo )))) {
return( NULL );
}
/*
* Initialize session info.
* XXX: it would be nice to be able to set these on a per-session basis:
* lssei_using_pcks_fns
* lssei_certdbh
*/
ssip->lssei_ssl_strength = default_ssl_strength;
memcpy( ssip->lssei_ssl_option_value, default_ssl_option_value,
sizeof(ssip->lssei_ssl_option_value));
memcpy( ssip->lssei_ssl_option_isset, default_ssl_option_isset,
sizeof(ssip->lssei_ssl_option_isset));
ssip->lssei_using_pcks_fns = using_pkcs_functions;
ssip->lssei_certdbh = CERT_GetDefaultCertDB();
ssip->lssei_ssl_ready = PR_TRUE;
return( ssip );
}
/*
* Install I/O routines from libsec and NSPR into libldap to allow libldap
* to do SSL.
*
* We rely on libprldap to provide most of the functions, and then we override
* a few of them to support SSL.
*/
int
LDAP_CALL
ldapssl_install_routines( LDAP *ld )
{
struct ldap_x_ext_io_fns iofns;
LDAPSSLSessionInfo *ssip;
PRLDAPSessionInfo sei;
/* install standard NSPR functions */
if ( prldap_install_routines(
ld,
1 /* shared -- we have to assume it is */ )
!= LDAP_SUCCESS ) {
return( -1 );
}
/*
* Allocate session information.
*/
if ( (ssip = ldapssl_alloc_sessioninfo()) == NULL )
{
ldap_set_lderrno( ld, LDAP_NO_MEMORY, NULL, NULL );
return( -1 );
}
/*
* override a few functions, saving a pointer to the standard function
* in each case so we can call it from our SSL savvy functions.
*/
memset( &iofns, 0, sizeof(iofns));
iofns.lextiof_size = LDAP_X_EXTIO_FNS_SIZE;
if ( ldap_get_option( ld, LDAP_X_OPT_EXTIO_FN_PTRS, (void *)&iofns ) < 0 ) {
ldapssl_free_session_info( &ssip );
return( -1 );
}
/* override socket, connect, and disposehandle */
ssip->lssei_std_functions.lssf_connect_fn = iofns.lextiof_connect;
iofns.lextiof_connect = ldapssl_connect;
ssip->lssei_std_functions.lssf_close_fn = iofns.lextiof_close;
iofns.lextiof_close = ldapssl_close;
ssip->lssei_std_functions.lssf_disposehdl_fn = iofns.lextiof_disposehandle;
iofns.lextiof_disposehandle = ldapssl_disposehandle;
if ( ldap_set_option( ld, LDAP_X_OPT_EXTIO_FN_PTRS, (void *)&iofns ) < 0 ) {
ldapssl_free_session_info( &ssip );
return( -1 );
}
/*
* Store session info. for later retrieval.
*/
sei.seinfo_size = PRLDAP_SESSIONINFO_SIZE;
sei.seinfo_appdata = (void *)ssip;
if ( prldap_set_session_info( ld, NULL, &sei ) != LDAP_SUCCESS ) {
ldapssl_free_session_info( &ssip );
return( -1 );
}
return( 0 );
}
/* Sets up SSL for the NSPR layered connection (or plain Text connection
* with startTLS extended I/O request already acknowledged by the server)
* Call this function after the call to ldaptls_setup()
*
* Returns an LDAP API result code (LDAP_SUCCESS if all goes well).
*/
static int
ldaptls_complete(LDAP *ld)
{
PRBool secure = 1;
PRLDAPSessionInfo sei;
PRLDAPSocketInfo soi;
LDAPSSLSocketInfo *ssoip = NULL;
LDAPSSLSessionInfo *sseip = NULL;
PRFileDesc *sslfd = NULL;
LBER_SOCKET intfd = -1;
int rc = LDAP_LOCAL_ERROR;
char *hostlist = NULL;
struct lextiof_socket_private *socketargp = NULL;
/*
* Get hostlist from LDAP Handle
*/
if ( ldap_get_option(ld, LDAP_OPT_HOST_NAME, &hostlist) < 0 ) {
rc = ldap_get_lderrno( ld, NULL, NULL );
goto close_socket_and_exit_with_error;
}
/*
* Get File Desc from current connection
*/
if ( ldap_get_option(ld, LDAP_OPT_DESC, &intfd) < 0 ) {
rc = ldap_get_lderrno( ld, NULL, NULL );
goto close_socket_and_exit_with_error;
}
/*
* Get Socket Arg Pointer
*/
if ( ldap_get_option(ld, LDAP_X_OPT_SOCKETARG, &socketargp) < 0 ) {
rc = ldap_get_lderrno( ld, NULL, NULL );
goto close_socket_and_exit_with_error;
}
/*
* Retrieve session info. so we can store a pointer to our session info.
* in our socket info. later.
*/
memset( &sei, 0, sizeof(sei));
sei.seinfo_size = PRLDAP_SESSIONINFO_SIZE;
if (LDAP_SUCCESS != (rc = prldap_get_session_info(ld, NULL, &sei))) {
goto close_socket_and_exit_with_error;
}
sseip = (LDAPSSLSessionInfo *)sei.seinfo_appdata;
/*
* Retrieve socket info.
*/
memset( &soi, 0, sizeof(soi));
soi.soinfo_size = PRLDAP_SOCKETINFO_SIZE;
if (LDAP_SUCCESS !=
(rc = prldap_get_socket_info(intfd, socketargp, &soi))) {
goto close_socket_and_exit_with_error;
}
/*
* Allocate a structure to hold our socket-specific data.
*/
if ( NULL == ( ssoip = PR_Calloc( 1, sizeof( LDAPSSLSocketInfo )))) {
rc = LDAP_NO_MEMORY;
goto close_socket_and_exit_with_error;
}
ssoip->soi_sessioninfo = sseip;
/*
* Add SSL layer and enable SSL.
*/
if (( sslfd = SSL_ImportFD( NULL, soi.soinfo_prfd )) == NULL ) {
rc = LDAP_LOCAL_ERROR;
goto close_socket_and_exit_with_error;
}
if ( SSL_OptionSet( sslfd, SSL_SECURITY, secure ) != SECSuccess ||
SSL_OptionSet( sslfd, SSL_ENABLE_TLS, secure ) != SECSuccess ||
SSL_OptionSet( sslfd, SSL_HANDSHAKE_AS_CLIENT, secure ) != SECSuccess ||
( secure && SSL_ResetHandshake( sslfd, PR_FALSE ) != SECSuccess ) ) {
rc = LDAP_LOCAL_ERROR;
goto close_socket_and_exit_with_error;
}
/*
* Set hostname which will be retrieved (depending on ssl strength) when
* using client or server auth.
*/
if ( SSL_SetURL( sslfd, hostlist ) != SECSuccess ) {
rc = LDAP_LOCAL_ERROR;
goto close_socket_and_exit_with_error;
}
ldap_memfree(hostlist);
hostlist = NULL;
/*
* Set any SSL options that were modified by a previous call to
* the ldapssl_set_option() function.
*/
if ( set_ssl_options( sslfd, sseip->lssei_ssl_option_value,
sseip->lssei_ssl_option_isset ) < 0 ) {
rc = LDAP_LOCAL_ERROR;
goto close_socket_and_exit_with_error;
}
/*
* If using client auth., arrange to use a separate SSL session cache
* for each distinct local client certificate nickname.
*/
if ( sseip->lssei_client_auth &&
sseip->lssei_certnickname && sseip->lssei_certnickname[0] &&
SSL_SetSockPeerID( sslfd, sseip->lssei_certnickname) != SECSuccess ) {
rc = LDAP_LOCAL_ERROR;
goto close_socket_and_exit_with_error;
}
/*
* Let the standard NSPR to LDAP layer know about the new socket and
* our own socket-specific data.
*/
soi.soinfo_prfd = sslfd;
soi.soinfo_appdata = (void *)ssoip;
if ( LDAP_SUCCESS !=
(rc = prldap_set_socket_info( intfd, socketargp, &soi ))) {
goto close_socket_and_exit_with_error;
}
sslfd = NULL; /* so we don't close the socket twice upon error */
/*
* Install certificate hook function.
*/
SSL_AuthCertificateHook( soi.soinfo_prfd,
(SSLAuthCertificate)ldapssl_AuthCertificate,
(void *)sseip );
if ( SSL_GetClientAuthDataHook( soi.soinfo_prfd,
get_clientauth_data, sseip->lssei_client_auth ? sseip : NULL ) != 0 ) {
rc = LDAP_LOCAL_ERROR;
goto close_socket_and_exit_with_error;
}
return( LDAP_SUCCESS ); /* success */
close_socket_and_exit_with_error:
ldap_memfree(hostlist);
hostlist = NULL;
if ( NULL != sslfd && sslfd != soi.soinfo_prfd ) {
PR_Close( sslfd );
}
if ( NULL != ssoip ) {
ldapssl_free_socket_info( &ssoip );
}
if ( intfd >= 0 && NULL != socketargp && sseip != NULL ) {
(*(sseip->lssei_std_functions.lssf_close_fn))( intfd,
socketargp );
}
return( rc );
} /* ldaptls_complete() */
/*
* Install I/O routines from libsec and NSPR into libldap to allow libldap
* to do TLS.
*
* We rely on libprldap to provide most of the functions.
* Returns an LDAP API result code (LDAP_SUCCESS if all goes well).
*/
static int
ldaptls_setup( LDAP *ld )
{
int rc = LDAP_LOCAL_ERROR;
LDAPSSLSessionInfo *ssip;
PRLDAPSessionInfo sei;
/* Check for valid input parameters. */
if ( ld == NULL ) {
ldap_set_lderrno( ld, LDAP_PARAM_ERROR, NULL, NULL );
return( LDAP_PARAM_ERROR );
}
/* Check if NSPR Layer is Installed */
if ( !prldap_is_installed(ld) ) {
/* No NSPR Layer installed,
* Call prldap_import_connection() that installs the NSPR I/O layer
* and imports connection details from Clear-text connection
*/
if (LDAP_SUCCESS != (rc = prldap_import_connection( ld ))) {
return( rc );
}
}
memset( &sei, 0, sizeof(sei));
sei.seinfo_size = PRLDAP_SESSIONINFO_SIZE;
if ( (rc = prldap_get_session_info( ld, NULL, &sei )) == LDAP_SUCCESS ) {
ssip = (LDAPSSLSessionInfo *)sei.seinfo_appdata;
} else {
return( rc );
}
if ( NULL == ssip ) {
if ( (ssip = ldapssl_alloc_sessioninfo()) == NULL ) {
ldap_set_lderrno( ld, LDAP_NO_MEMORY, NULL, NULL );
return( LDAP_NO_MEMORY );
}
/*
* Store session info. for later retrieval.
*/
sei.seinfo_size = PRLDAP_SESSIONINFO_SIZE;
sei.seinfo_appdata = (void *)ssip;
if (LDAP_SUCCESS != (rc = prldap_set_session_info( ld, NULL, &sei ))) {
ldapssl_free_session_info( &ssip );
return( rc );
}
}
ssip->lssei_tls_init= PR_TRUE;
return( LDAP_SUCCESS );
} /* ldaptls_setup()*/
/* Function; ldap_start_tls_s()
* startTLS request
* Returns an LDAP API result code (LDAP_SUCCESS if all goes well).
*
*/
int
LDAP_CALL
ldap_start_tls_s(LDAP *ld,
LDAPControl **serverctrls,
LDAPControl **clientctrls)
{
int rc = -1;
int version = LDAP_VERSION3;
/* Error check on LDAP handle */
if ( ld == NULL ) {
ldap_set_lderrno( ld, LDAP_PARAM_ERROR, NULL, NULL );
return( LDAP_PARAM_ERROR );
}
/* Make sure we are dealing with LDAPv3 */
if ( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) < 0 ) {
return( ldap_get_lderrno( ld, NULL, NULL ));
}
/* Issue the Start TLS extended operation */
rc = ldap_extended_operation_s( ld, LDAP_EXOP_START_TLS, NULL, serverctrls,
clientctrls, NULL, NULL );
if ( rc != LDAP_SUCCESS )
{
return( rc );
}
/* Initialize TLS and enable SSL on the LDAP session */
if ( LDAP_SUCCESS == ( rc = ldaptls_setup( ld ) ) &&
LDAP_SUCCESS == ( rc = ldaptls_complete( ld ) )) {
if (ldap_set_option( ld, LDAP_OPT_SSL, LDAP_OPT_ON ) < 0 ) {
rc = ldap_get_lderrno( ld, NULL, NULL );
}
}
if (LDAP_SUCCESS != rc) {
/* Allow to proceed in clear text if secure session fails */
ldap_set_option( ld, LDAP_OPT_SSL, LDAP_OPT_OFF );
}
return( rc );
}
/*ARGSUSED*/
int
LDAP_CALL
ldapssl_enable_clientauth( LDAP *ld, char *keynickname,
char *keypasswd, char *certnickname )
{
LDAPSSLSessionInfo *ssip;
PRLDAPSessionInfo sei;
int new_session_allocated = 0;
/*
* Check parameters
* allow keypasswd to be NULL in case PK11_SetPasswordFunc()
* already set by the user to their own private pin callback.
* there is no proper way to test if PK11_SetPasswordFunc()
* callback is already set apart from NSS private interfaces
*/
if ( certnickname == NULL ) {
ldap_set_lderrno( ld, LDAP_PARAM_ERROR, NULL,
ldapssl_libldap_compat_strdup(
"A non-NULL certnickname is required" ));
return( -1 );
}
/*
* Get session info. data structure.
*/
memset( &sei, 0, sizeof(sei));
sei.seinfo_size = PRLDAP_SESSIONINFO_SIZE;
if ( prldap_get_session_info( ld, NULL, &sei ) == LDAP_SUCCESS ) {
ssip = (LDAPSSLSessionInfo *)sei.seinfo_appdata;
} else {
return( -1 );
}
if ( NULL == ssip ) { /* Failed to get ssl session info pointer */
/*
* Allocate our own session information.
*/
if ( NULL == ( ssip = ldapssl_alloc_sessioninfo())) {
ldap_set_lderrno( ld, LDAP_NO_MEMORY, NULL, NULL );
return( -1 );
}
/*
* Store session info. for later retrieval.
*/
sei.seinfo_size = PRLDAP_SESSIONINFO_SIZE;
sei.seinfo_appdata = (void *)ssip;
if ( prldap_set_session_info( ld, NULL, &sei ) != LDAP_SUCCESS ) {
return( -1 );
}
new_session_allocated = 1;
}
if ( !(ssip->lssei_ssl_ready) && !new_session_allocated ) {
/* standard SSL setup has not yet done */
ldap_set_lderrno( ld, LDAP_PARAM_ERROR, NULL,
ldapssl_libldap_compat_strdup(
"An SSL-ready LDAP session handle is required" ));
return( -1 );
}
/*
* Update session info. data structure.
*/
ssip->lssei_certnickname = PL_strdup( certnickname );
if ( keypasswd ) {
ssip->lssei_keypasswd = PL_strdup( keypasswd );
} else {
/* set lssei_using_pcks_fns to prevent our own PK11_SetPasswordFunc()
* callback being installed in get_keyandcert() if keypasswd is NULL
* workaround for now til NSS comes up with proper check interface
*/
ssip->lssei_using_pcks_fns = 1;
ssip->lssei_keypasswd = NULL; /* assume pre-authenticated */
}
if ( NULL == ssip->lssei_certnickname ||
( keypasswd && ( NULL == ssip->lssei_keypasswd ) ) ) {
ldap_set_lderrno( ld, LDAP_NO_MEMORY, NULL, NULL );
return( -1 );
}
if ( check_clientauth_nicknames_and_passwd( ld, ssip ) != SECSuccess ) {
/* LDAP errno is set by check_clientauth_nicknames_and_passwd() */
return( -1 );
}
/* Mark the session as client auth enabled */
ssip->lssei_client_auth = PR_TRUE;
return( LDAP_SUCCESS );
}
/*
* Set the SSL strength for an existing SSL-enabled LDAP session handle.
*
* See the description of ldapssl_serverauth_init() above for valid
* sslstrength values. If ld is NULL, the default for new LDAP session
* handles is set.
*
* Returns 0 if all goes well and -1 if an error occurs.
*/
int
LDAP_CALL
ldapssl_set_strength( LDAP *ld, int sslstrength )
{
int rc = 0; /* assume success */
if ( sslstrength != LDAPSSL_AUTH_WEAK &&
sslstrength != LDAPSSL_AUTH_CERT &&
sslstrength != LDAPSSL_AUTH_CNCHECK ) {
rc = -1;
} else {
if ( NULL == ld ) { /* set default strength */
default_ssl_strength = sslstrength;
} else { /* set session-specific strength */
PRLDAPSessionInfo sei;
LDAPSSLSessionInfo *sseip;
memset( &sei, 0, sizeof( sei ));
sei.seinfo_size = PRLDAP_SESSIONINFO_SIZE;
if ( prldap_get_session_info( ld, NULL, &sei ) == LDAP_SUCCESS ) {
sseip = (LDAPSSLSessionInfo *)sei.seinfo_appdata;
sseip->lssei_ssl_strength = sslstrength;
} else {
rc = -1;
}
}
}
return( rc );
}
/*
* Set SSL options for an existing SSL-enabled LDAP session handle.
* If ld is NULL, the default options used for all future LDAP SSL sessions
* are the ones affected. The option values are specific to the underlying
* SSL provider; see ssl.h within the Network Security Services (NSS)
* distribution for the options supported by NSS (the default SSL provider).
*
* This function should be called before any LDAP connections are created.
*
* Returns: 0 if all goes well.
*/
int
LDAP_CALL
ldapssl_set_option( LDAP *ld, int option, int on )
{
int rc = 0; /* assume success */
if ( option < 0 || option > LDAPSSL_MAX_SSL_OPTION ) {
ldap_set_lderrno( ld, LDAP_PARAM_ERROR, NULL, NULL );
rc = -1;
} else {
if ( NULL == ld ) {
/* set default options for new LDAP sessions */
default_ssl_option_value[option] = on;
default_ssl_option_isset[option] = PR_TRUE;
} else {
/* set session options */
PRLDAPSessionInfo sei;
LDAPSSLSessionInfo *sseip;
memset( &sei, 0, sizeof( sei ));
sei.seinfo_size = PRLDAP_SESSIONINFO_SIZE;
if ( prldap_get_session_info( ld, NULL, &sei ) == LDAP_SUCCESS ) {
sseip = (LDAPSSLSessionInfo *)sei.seinfo_appdata;
sseip->lssei_ssl_option_value[option] = on;
sseip->lssei_ssl_option_isset[option] = PR_TRUE;
} else {
rc = -1;
}
}
}
return( rc );
}
/*
* Retrieve SSL options for an existing SSL-enabled LDAP session handle.
* If ld is NULL, the default options to be used for all future LDAP SSL
* sessions are retrieved. The option values are specific to the underlying
* SSL provider; see ssl.h within the Network Security Services (NSS)
* distribution for the options supported by NSS (the default SSL provider).
*
* Returns: 0 if all goes well.
*/
int
LDAP_CALL
ldapssl_get_option( LDAP *ld, int option, int *onp )
{
int rc = 0; /* assume success */
if ( option < 0 || option > LDAPSSL_MAX_SSL_OPTION || onp == NULL ) {
ldap_set_lderrno( ld, LDAP_PARAM_ERROR, NULL, NULL );
rc = -1;
} else {
int rv = 0, set_rv = 0;
if ( NULL == ld ) {
/* return default options for new LDAP sessions */
if ( default_ssl_option_isset[option] ) {
rv = default_ssl_option_value[option];
set_rv = 1;
}
} else {
/* return session options */
PRLDAPSessionInfo sei;
LDAPSSLSessionInfo *sseip;
memset( &sei, 0, sizeof( sei ));
sei.seinfo_size = PRLDAP_SESSIONINFO_SIZE;
if ( prldap_get_session_info( ld, NULL, &sei )
== LDAP_SUCCESS ) {
sseip = (LDAPSSLSessionInfo *)sei.seinfo_appdata;
if ( sseip->lssei_ssl_option_isset[option] ) {
rv = sseip->lssei_ssl_option_value[option];
set_rv = 1;
}
} else {
rc = -1;
}
}
if ( !set_rv ) {
PRBool pron = PR_FALSE;
if ( rc == 0 && SSL_OptionGetDefault( (PRInt32)option, &pron )
!= SECSuccess ) {
rc = -1;
}
rv = pron;
}
*onp = rv; /* always return a value */
}
return( rc );
}
#ifdef LDAPSSL_DEBUG
struct optitem {
PRInt32 om_option;
const char *om_string;
} optmap[] = {
{ SSL_SECURITY, "SSL_SECURITY" },
{ SSL_SOCKS, "SSL_SOCKS" },
{ SSL_REQUEST_CERTIFICATE, "SSL_REQUEST_CERTIFICATE" },
{ SSL_HANDSHAKE_AS_CLIENT, "SSL_HANDSHAKE_AS_CLIENT" },
{ SSL_HANDSHAKE_AS_SERVER, "SSL_HANDSHAKE_AS_SERVER" },
{ SSL_ENABLE_SSL2, "SSL_ENABLE_SSL2" },
{ SSL_ENABLE_SSL3, "SSL_ENABLE_SSL3" },
{ SSL_NO_CACHE, "SSL_NO_CACHE" },
{ SSL_REQUIRE_CERTIFICATE, "SSL_REQUIRE_CERTIFICATE" },
{ SSL_ENABLE_FDX, "SSL_ENABLE_FDX" },
{ SSL_V2_COMPATIBLE_HELLO, "SSL_V2_COMPATIBLE_HELLO" },
{ SSL_ENABLE_TLS, "SSL_ENABLE_TLS" },
{ SSL_ROLLBACK_DETECTION, "SSL_ROLLBACK_DETECTION" },
{ -1, NULL },
};
static const char *
sslopt2string( PRInt32 option )
{
int i;
const char *s = "unknown";
for ( i = 0; optmap[i].om_option != -1; ++i ) {
if ( optmap[i].om_option == option ) {
s = optmap[i].om_string;
break;
}
}
return( s );
}
#endif /* LDAPSSL_DEBUG */
static int
set_ssl_options( PRFileDesc *sslfd, PRBool *optval, PRBool *optisset )
{
SECStatus secrc = SECSuccess;
PRInt32 option;
for ( option = 0;
( secrc == SECSuccess ) && ( option < LDAPSSL_MAX_SSL_OPTION );
++option ) {
if ( optisset[ option ] ) {
#ifdef LDAPSSL_DEBUG
fprintf( stderr,
"set_ssl_options: setting option %d - %s to %d (%s)\n",
option, sslopt2string(option), optval[ option ],
optval[ option ] ? "ON" : "OFF" );
#endif /* LDAPSSL_DEBUG */
secrc = SSL_OptionSet( sslfd, option, optval[ option ] );
}
}
if ( secrc == SECSuccess ) {
return( 0 );
}
PR_SetError( PR_GetError(), EINVAL ); /* set OS error only */
return( -1 );
}
static void
ldapssl_free_session_info( LDAPSSLSessionInfo **ssipp )
{
if ( NULL != ssipp && NULL != *ssipp ) {
if ( NULL != (*ssipp)->lssei_certnickname ) {
PL_strfree( (*ssipp)->lssei_certnickname );
(*ssipp)->lssei_certnickname = NULL;
}
if ( NULL != (*ssipp)->lssei_keypasswd ) {
PL_strfree( (*ssipp)->lssei_keypasswd );
(*ssipp)->lssei_keypasswd = NULL;
}
PR_Free( *ssipp );
*ssipp = NULL;
}
}
static void
ldapssl_free_socket_info( LDAPSSLSocketInfo **soipp )
{
if ( NULL != soipp && NULL != *soipp ) {
PR_Free( *soipp );
*soipp = NULL;
}
}
/* this function provides cert authentication. This is called during
* the SSL_Handshake process. Once the cert has been retrieved from
* the server, the it is checked, using VerifyCertNow(), then
* the cn is checked against the host name, set with SSL_SetURL()
*/
static int
ldapssl_AuthCertificate(void *sessionarg, PRFileDesc *fd, PRBool checkSig,
PRBool isServer)
{
SECStatus rv = SECFailure;
LDAPSSLSessionInfo *sseip;
CERTCertificate *cert;
SECCertUsage certUsage;
char *hostname = (char *)0;
if (!sessionarg || !fd) {
return rv;
}
sseip = (LDAPSSLSessionInfo *)sessionarg;
if (LDAPSSL_AUTH_WEAK == sseip->lssei_ssl_strength ) { /* no check */
return SECSuccess;
}
if ( isServer ) {
certUsage = certUsageSSLClient;
} else {
certUsage = certUsageSSLServer;
}
cert = SSL_PeerCertificate( fd );
rv = CERT_VerifyCertNow(sseip->lssei_certdbh, cert, checkSig,
certUsage, NULL);
if ( rv != SECSuccess || isServer ) {
/* must destroy cert to avoid mem leak */
CERT_DestroyCertificate(cert);
return rv;
}
if ( LDAPSSL_AUTH_CNCHECK == sseip->lssei_ssl_strength ) {
/* cert is OK. This is the client side of an SSL connection.
* Now check the name field in the cert against the desired hostname.
* NB: This is our only defense against Man-In-The-Middle (MITM)
* attacks!
*/
hostname = SSL_RevealURL( fd );
if (hostname && hostname[0]) {
rv = CERT_VerifyCertName(cert, hostname);
} else {
rv = SECFailure;
}
if (hostname) {
PL_strfree(hostname);
}
if (rv != SECSuccess) {
PORT_SetError(SSL_ERROR_BAD_CERT_DOMAIN);
}
}
/* must destroy cert to avoid mem leak */
CERT_DestroyCertificate(cert);
return((int)rv);
}
/*
* called during SSL client auth. when server wants our cert and key.
* returns: SECSuccess if we succeeded and set *pRetCert and *pRetKey,
* SECFailure otherwise.
* if SECFailure is returned SSL will proceed without sending a cert.
*/
/*ARGSUSED*/
static SECStatus
get_clientauth_data( void *sessionarg, PRFileDesc *prfd,
CERTDistNames *caNames, CERTCertificate **pRetCert,
SECKEYPrivateKey **pRetKey )
{
LDAPSSLSessionInfo *ssip;
if (( ssip = (LDAPSSLSessionInfo *)sessionarg ) == NULL ) {
return( SECFailure ); /* client auth. not enabled */
}
return( get_keyandcert( ssip, pRetCert, pRetKey, NULL ));
}
static SECStatus
get_keyandcert( LDAPSSLSessionInfo *ssip,
CERTCertificate **pRetCert, SECKEYPrivateKey **pRetKey,
char **errmsgp )
{
CERTCertificate *cert;
SECKEYPrivateKey *key;
if ( !ssip->lssei_using_pcks_fns && (NULL != ssip->lssei_keypasswd) ) {
/*
* XXX: This function should be called only once, and probably
* in one of the ldapssl_.*_init() calls.
*/
PK11_SetPasswordFunc( get_keypassword );
}
if (( cert = CERT_FindUserCertByUsage( CERT_GetDefaultCertDB(),
ssip->lssei_certnickname, certUsageSSLClient,
PR_FALSE, (void *)ssip )) == NULL ) {
if ( errmsgp != NULL ) {
*errmsgp = "unable to find certificate";
}
return( SECFailure );
}
if (( key = PK11_FindKeyByAnyCert( cert, (void *)ssip )) == NULL ) {
CERT_DestroyCertificate( cert );
if ( errmsgp != NULL ) {
*errmsgp = "bad key or key password";
}
return( SECFailure );
}
*pRetCert = cert;
*pRetKey = key;
return( SECSuccess );
}
/*
* This function returns the password to NSS.
* This function is enable through PK11_SetPasswordFunc
* only if pkcs functions are not being used.
*/
/*ARGSUSED*/
static char *
get_keypassword( PK11SlotInfo *slot, PRBool retry, void *sessionarg )
{
LDAPSSLSessionInfo *ssip;
if ( retry)
return (NULL);
ssip = (LDAPSSLSessionInfo *)sessionarg;
if ( NULL == ssip || NULL == ssip->lssei_keypasswd ) {
return( NULL );
}
return( PL_strdup(ssip->lssei_keypasswd) );
}
/*
* performs some basic checks on clientauth cert and key/password
*
* XXXmcs: could perform additional checks... see servers/slapd/ssl.c
* 1) check expiration
* 2) check that public key in cert matches private key
* see ns/netsite/ldap/servers/slapd/ssl.c:slapd_ssl_init() for example code.
*/
static SECStatus
check_clientauth_nicknames_and_passwd( LDAP *ld, LDAPSSLSessionInfo *ssip )
{
char *errmsg = NULL;
CERTCertificate *cert = NULL;
SECKEYPrivateKey *key = NULL;
SECStatus rv;
rv = get_keyandcert( ssip, &cert, &key, &errmsg );
if ( rv != SECSuccess ) {
if ( errmsg != NULL ) {
errmsg = ldapssl_libldap_compat_strdup( errmsg );
}
ldap_set_lderrno( ld, LDAP_PARAM_ERROR, NULL, errmsg );
return( rv );
}
if ( cert != NULL ) {
CERT_DestroyCertificate( cert );
}
if ( key != NULL ) {
SECKEY_DestroyPrivateKey( key );
}
return( SECSuccess );
}
/*
* A strdup() work-alike that uses libldap's memory allocator.
*/
static char *
ldapssl_libldap_compat_strdup(const char *s1)
{
char *s2;
if ( NULL == s1 ) {
s2 = NULL;
} else {
size_t len = strlen( s1 );
if ( NULL != ( s2 = (char *)ldap_x_malloc( len + 1 ))) {
strcpy( s2, s1 );
}
}
return s2;
}
/* there are patches and kludges. this is both. force some linkers to
* link this stuff in
*/
int stubs_o_stuff( void )
{
PRExplodedTime exploded;
PLArenaPool pool;
const char *name ="t";
PRUint32 size = 0, align = 0;
PR_ImplodeTime( &exploded );
PL_InitArenaPool( &pool, name, size, align);
PR_Cleanup();
PR_fprintf((PRFileDesc*)stderr, "Bad IDEA!!");
return 0;
}
/*
* Import the file descriptor corresponding to the socket of an already
* open LDAP connection into SSL, and update the socket and session
* information accordingly. Returns 0 if all goes well.
*/
int
LDAP_CALL
ldapssl_import_fd ( LDAP *ld, int secure )
{
PRLDAPSessionInfo sei;
PRLDAPSocketInfo soi;
LDAPSSLSocketInfo *ssoip = NULL;
LDAPSSLSessionInfo *sseip;
PRFileDesc *sslfd = NULL;
LBER_SOCKET intfd = -1;
char *hostlist;
struct lextiof_socket_private *socketargp;
/*
* Get hostlist from LDAP Handle
*/
if ( ldap_get_option(ld, LDAP_OPT_HOST_NAME, &hostlist) < 0 ) {
return( -1 );
}
/*
* Get File Desc from current connection
*/
if ( ldap_get_option(ld, LDAP_OPT_DESC, &intfd) < 0 ) {
return( -1 );
}
/*
* Get Socket Arg Pointer
*/
if ( ldap_get_option(ld, LDAP_X_OPT_SOCKETARG, &socketargp) < 0 ) {
return( -1 );
}
/*
* Retrieve session info. so we can store a pointer to our session info.
* in our socket info. later.
*/
memset( &sei, 0, sizeof(sei));
sei.seinfo_size = PRLDAP_SESSIONINFO_SIZE;
if ( prldap_get_session_info( ld, NULL, &sei ) != LDAP_SUCCESS ) {
return( -1 );
}
sseip = (LDAPSSLSessionInfo *)sei.seinfo_appdata;
/*
* Retrieve socket info. so we have the PRFileDesc.
*/
memset( &soi, 0, sizeof(soi));
soi.soinfo_size = PRLDAP_SOCKETINFO_SIZE;
if ( prldap_get_socket_info( intfd, socketargp, &soi ) != LDAP_SUCCESS ) {
return( -1 );
}
/*
* Allocate a structure to hold our socket-specific data.
*/
if ( NULL == ( ssoip = PR_Calloc( 1, sizeof( LDAPSSLSocketInfo )))) {
goto reset_socket_and_exit_with_error;
}
ssoip->soi_sessioninfo = sseip;
/*
* Add SSL layer and let the standard NSPR to LDAP layer and enable SSL.
*/
if (( sslfd = SSL_ImportFD( NULL, soi.soinfo_prfd )) == NULL ) {
goto reset_socket_and_exit_with_error;
}
if ( SSL_OptionSet( sslfd, SSL_SECURITY, secure ) != SECSuccess ||
SSL_OptionSet( sslfd, SSL_ENABLE_TLS, secure ) ||
SSL_OptionSet( sslfd, SSL_HANDSHAKE_AS_CLIENT, secure )
!= SECSuccess || ( secure && SSL_ResetHandshake( sslfd,
PR_FALSE ) != SECSuccess )) {
goto reset_socket_and_exit_with_error;
}
/*
* Set hostname which will be retrieved (depending on ssl strength) when
* using client or server auth.
*/
if ( SSL_SetURL( sslfd, hostlist ) != SECSuccess ) {
goto reset_socket_and_exit_with_error;
}
/*
* Set any SSL options that were modified by a previous call to
* the ldapssl_set_option() function.
*/
if ( set_ssl_options( sslfd, sseip->lssei_ssl_option_value,
sseip->lssei_ssl_option_isset ) < 0 ) {
goto reset_socket_and_exit_with_error;
}
/*
* If using client auth., arrange to use a separate SSL session cache
* for each distinct local client certificate nickname.
*/
if ( sseip->lssei_client_auth &&
sseip->lssei_certnickname && sseip->lssei_certnickname[0] &&
SSL_SetSockPeerID( sslfd, sseip->lssei_certnickname) != SECSuccess ) {
goto reset_socket_and_exit_with_error;
}
/*
* Let the standard NSPR to LDAP layer know about the new socket and
* our own socket-specific data.
*/
soi.soinfo_prfd = sslfd;
soi.soinfo_appdata = (void *)ssoip;
if ( prldap_set_default_socket_info( ld, &soi ) != LDAP_SUCCESS ) {
goto reset_socket_and_exit_with_error;
}
/*
* Install certificate hook function.
*/
if ( SSL_AuthCertificateHook( soi.soinfo_prfd,
(SSLAuthCertificate)ldapssl_AuthCertificate,
(void *)sseip) != 0 ) {
goto reset_socket_and_exit_with_error;
}
if ( SSL_GetClientAuthDataHook( soi.soinfo_prfd,
get_clientauth_data, sseip->lssei_certnickname ? sseip : NULL )
!= 0 ) {
goto reset_socket_and_exit_with_error;
}
return 0;
reset_socket_and_exit_with_error:
if ( NULL != sslfd ) {
/*
* "Unimport" the socket from SSL, i.e. get rid of the upper layer of
* the file descriptor stack, which represents SSL.
*/
soi.soinfo_prfd = sslfd;
sslfd = PR_PopIOLayer( soi.soinfo_prfd, PR_TOP_IO_LAYER );
sslfd->dtor( sslfd );
}
if ( NULL != ssoip ) {
ldapssl_free_socket_info( &ssoip );
soi.soinfo_appdata = NULL;
}
prldap_set_default_socket_info( ld, &soi );
return( -1 );
}
/*
* Reset an LDAP session from SSL to a non-secure status. Basically,
* this function undoes the work done by ldapssl_install_routines.
* Returns 0 if all goes well.
*/
int
LDAP_CALL
ldapssl_reset_to_nonsecure ( LDAP *ld )
{
PRLDAPSessionInfo sei;
LDAPSSLSessionInfo *sseip;
struct ldap_x_ext_io_fns iofns;
int rc = 0;
/*
* Retrieve session info.
*/
memset( &sei, 0, sizeof(sei));
sei.seinfo_size = PRLDAP_SESSIONINFO_SIZE;
if ( prldap_get_session_info( ld, NULL, &sei ) != LDAP_SUCCESS ) {
return( -1 );
}
sseip = (LDAPSSLSessionInfo *)sei.seinfo_appdata;
if ( sseip != NULL ) {
/*
* Reset the standard extended io functions.
*/
memset( &iofns, 0, sizeof(iofns));
iofns.lextiof_size = LDAP_X_EXTIO_FNS_SIZE;
if ( ldap_get_option( ld, LDAP_X_OPT_EXTIO_FN_PTRS, (void *)&iofns )
< 0) {
rc = -1;
goto free_session_info;
}
/* reset socket, connect, and ioctl */
iofns.lextiof_connect = sseip->lssei_std_functions.lssf_connect_fn;
iofns.lextiof_close = sseip->lssei_std_functions.lssf_close_fn;
iofns.lextiof_disposehandle =
sseip->lssei_std_functions.lssf_disposehdl_fn;
if ( ldap_set_option( ld, LDAP_X_OPT_EXTIO_FN_PTRS, (void *)&iofns )
< 0) {
rc = -1;
goto free_session_info;
}
free_session_info:
ldapssl_free_session_info( &sseip );
sei.seinfo_appdata = NULL;
if ( prldap_set_session_info( ld, NULL, &sei ) != LDAP_SUCCESS ) {
rc = -1;
}
} /* if ( sseip && *sseip ) */
if ( ldap_set_option( ld, LDAP_OPT_SSL, LDAP_OPT_OFF ) < 0 ) {
return (-1);
}
return rc;
}
#endif /* NET_SSL */
|