tls2.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702
  1. /* tls.c - Handle tls/ssl. */
  2. /* $OpenLDAP$ */
  3. /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  4. *
  5. * Copyright 1998-2022 The OpenLDAP Foundation.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted only as authorized by the OpenLDAP
  10. * Public License.
  11. *
  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>.
  15. */
  16. /* ACKNOWLEDGEMENTS: restructured by Howard Chu.
  17. */
  18. #include "portable.h"
  19. #include "ldap_config.h"
  20. #include <stdio.h>
  21. #include <ac/stdlib.h>
  22. #include <ac/errno.h>
  23. #include <ac/socket.h>
  24. #include <ac/string.h>
  25. #include <ac/ctype.h>
  26. #include <ac/time.h>
  27. #include <ac/unistd.h>
  28. #include <ac/param.h>
  29. #include <ac/dirent.h>
  30. #include "ldap-int.h"
  31. #ifdef HAVE_TLS
  32. #include "ldap-tls.h"
  33. static tls_impl *tls_imp = &ldap_int_tls_impl;
  34. #define HAS_TLS( sb ) ber_sockbuf_ctrl( sb, LBER_SB_OPT_HAS_IO, \
  35. (void *)tls_imp->ti_sbio )
  36. #endif /* HAVE_TLS */
  37. /* RFC2459 minimum required set of supported attribute types
  38. * in a certificate DN
  39. */
  40. typedef struct oid_name {
  41. struct berval oid;
  42. struct berval name;
  43. } oid_name;
  44. static oid_name oids[] = {
  45. { BER_BVC("2.5.4.3"), BER_BVC("cn") },
  46. { BER_BVC("2.5.4.4"), BER_BVC("sn") },
  47. { BER_BVC("2.5.4.6"), BER_BVC("c") },
  48. { BER_BVC("2.5.4.7"), BER_BVC("l") },
  49. { BER_BVC("2.5.4.8"), BER_BVC("st") },
  50. { BER_BVC("2.5.4.10"), BER_BVC("o") },
  51. { BER_BVC("2.5.4.11"), BER_BVC("ou") },
  52. { BER_BVC("2.5.4.12"), BER_BVC("title") },
  53. { BER_BVC("2.5.4.41"), BER_BVC("name") },
  54. { BER_BVC("2.5.4.42"), BER_BVC("givenName") },
  55. { BER_BVC("2.5.4.43"), BER_BVC("initials") },
  56. { BER_BVC("2.5.4.44"), BER_BVC("generationQualifier") },
  57. { BER_BVC("2.5.4.46"), BER_BVC("dnQualifier") },
  58. { BER_BVC("1.2.840.113549.1.9.1"), BER_BVC("email") },
  59. { BER_BVC("0.9.2342.19200300.100.1.25"), BER_BVC("dc") },
  60. { BER_BVNULL, BER_BVNULL }
  61. };
  62. #ifdef HAVE_TLS
  63. LDAP_F(int) ldap_pvt_tls_check_hostname LDAP_P(( LDAP *ld, void *s, const char *name_in ));
  64. LDAP_F(int) ldap_pvt_tls_get_peercert LDAP_P(( void *s, struct berval *der ));
  65. void
  66. ldap_pvt_tls_ctx_free ( void *c )
  67. {
  68. if ( !c ) return;
  69. tls_imp->ti_ctx_free( c );
  70. }
  71. static void
  72. tls_ctx_ref( tls_ctx *ctx )
  73. {
  74. if ( !ctx ) return;
  75. tls_imp->ti_ctx_ref( ctx );
  76. }
  77. #ifdef LDAP_R_COMPILE
  78. /*
  79. * an extra mutex for the default ctx.
  80. */
  81. static ldap_pvt_thread_mutex_t tls_def_ctx_mutex;
  82. #endif
  83. void
  84. ldap_int_tls_destroy( struct ldapoptions *lo )
  85. {
  86. if ( lo->ldo_tls_ctx ) {
  87. ldap_pvt_tls_ctx_free( lo->ldo_tls_ctx );
  88. lo->ldo_tls_ctx = NULL;
  89. }
  90. if ( lo->ldo_tls_certfile ) {
  91. LDAP_FREE( lo->ldo_tls_certfile );
  92. lo->ldo_tls_certfile = NULL;
  93. }
  94. if ( lo->ldo_tls_keyfile ) {
  95. LDAP_FREE( lo->ldo_tls_keyfile );
  96. lo->ldo_tls_keyfile = NULL;
  97. }
  98. if ( lo->ldo_tls_dhfile ) {
  99. LDAP_FREE( lo->ldo_tls_dhfile );
  100. lo->ldo_tls_dhfile = NULL;
  101. }
  102. if ( lo->ldo_tls_ecname ) {
  103. LDAP_FREE( lo->ldo_tls_ecname );
  104. lo->ldo_tls_ecname = NULL;
  105. }
  106. if ( lo->ldo_tls_cacertfile ) {
  107. LDAP_FREE( lo->ldo_tls_cacertfile );
  108. lo->ldo_tls_cacertfile = NULL;
  109. }
  110. if ( lo->ldo_tls_cacertdir ) {
  111. LDAP_FREE( lo->ldo_tls_cacertdir );
  112. lo->ldo_tls_cacertdir = NULL;
  113. }
  114. if ( lo->ldo_tls_ciphersuite ) {
  115. LDAP_FREE( lo->ldo_tls_ciphersuite );
  116. lo->ldo_tls_ciphersuite = NULL;
  117. }
  118. if ( lo->ldo_tls_crlfile ) {
  119. LDAP_FREE( lo->ldo_tls_crlfile );
  120. lo->ldo_tls_crlfile = NULL;
  121. }
  122. /* tls_pin_hashalg and tls_pin share the same buffer */
  123. if ( lo->ldo_tls_pin_hashalg ) {
  124. LDAP_FREE( lo->ldo_tls_pin_hashalg );
  125. lo->ldo_tls_pin_hashalg = NULL;
  126. } else {
  127. LDAP_FREE( lo->ldo_tls_pin.bv_val );
  128. }
  129. BER_BVZERO( &lo->ldo_tls_pin );
  130. }
  131. /*
  132. * Tear down the TLS subsystem. Should only be called once.
  133. */
  134. void
  135. ldap_pvt_tls_destroy( void )
  136. {
  137. struct ldapoptions *lo = LDAP_INT_GLOBAL_OPT();
  138. ldap_int_tls_destroy( lo );
  139. tls_imp->ti_tls_destroy();
  140. }
  141. static void
  142. ldap_exit_tls_destroy( void )
  143. {
  144. struct ldapoptions *lo = LDAP_INT_GLOBAL_OPT();
  145. ldap_int_tls_destroy( lo );
  146. }
  147. /*
  148. * Initialize a particular TLS implementation.
  149. * Called once per implementation.
  150. */
  151. static int
  152. tls_init(tls_impl *impl, int do_threads )
  153. {
  154. static int tls_initialized = 0;
  155. int rc;
  156. if ( !tls_initialized++ ) {
  157. #ifdef LDAP_R_COMPILE
  158. ldap_pvt_thread_mutex_init( &tls_def_ctx_mutex );
  159. #endif
  160. }
  161. if ( impl->ti_inited++ ) return 0;
  162. if ( do_threads ) {
  163. #ifdef LDAP_R_COMPILE
  164. impl->ti_thr_init();
  165. #endif
  166. }
  167. rc = impl->ti_tls_init();
  168. atexit( ldap_exit_tls_destroy );
  169. return rc;
  170. }
  171. /*
  172. * Initialize TLS subsystem. Called once per implementation.
  173. */
  174. int
  175. ldap_pvt_tls_init( int do_threads )
  176. {
  177. return tls_init( tls_imp, do_threads );
  178. }
  179. /*
  180. * initialize a new TLS context
  181. */
  182. static int
  183. ldap_int_tls_init_ctx( struct ldapoptions *lo, int is_server, char *errmsg )
  184. {
  185. int rc = 0;
  186. tls_impl *ti = tls_imp;
  187. struct ldaptls lts = lo->ldo_tls_info;
  188. if ( lo->ldo_tls_ctx )
  189. return 0;
  190. tls_init( ti, 0 );
  191. if ( is_server && !lts.lt_certfile && !lts.lt_keyfile &&
  192. !lts.lt_cacertfile && !lts.lt_cacertdir &&
  193. !lts.lt_cacert.bv_val && !lts.lt_cert.bv_val &&
  194. !lts.lt_key.bv_val ) {
  195. /* minimum configuration not provided */
  196. return LDAP_NOT_SUPPORTED;
  197. }
  198. #ifdef HAVE_EBCDIC
  199. /* This ASCII/EBCDIC handling is a real pain! */
  200. if ( lts.lt_ciphersuite ) {
  201. lts.lt_ciphersuite = LDAP_STRDUP( lts.lt_ciphersuite );
  202. __atoe( lts.lt_ciphersuite );
  203. }
  204. if ( lts.lt_cacertfile ) {
  205. lts.lt_cacertfile = LDAP_STRDUP( lts.lt_cacertfile );
  206. __atoe( lts.lt_cacertfile );
  207. }
  208. if ( lts.lt_certfile ) {
  209. lts.lt_certfile = LDAP_STRDUP( lts.lt_certfile );
  210. __atoe( lts.lt_certfile );
  211. }
  212. if ( lts.lt_keyfile ) {
  213. lts.lt_keyfile = LDAP_STRDUP( lts.lt_keyfile );
  214. __atoe( lts.lt_keyfile );
  215. }
  216. if ( lts.lt_crlfile ) {
  217. lts.lt_crlfile = LDAP_STRDUP( lts.lt_crlfile );
  218. __atoe( lts.lt_crlfile );
  219. }
  220. if ( lts.lt_cacertdir ) {
  221. lts.lt_cacertdir = LDAP_STRDUP( lts.lt_cacertdir );
  222. __atoe( lts.lt_cacertdir );
  223. }
  224. if ( lts.lt_dhfile ) {
  225. lts.lt_dhfile = LDAP_STRDUP( lts.lt_dhfile );
  226. __atoe( lts.lt_dhfile );
  227. }
  228. if ( lts.lt_ecname ) {
  229. lts.lt_ecname = LDAP_STRDUP( lts.lt_ecname );
  230. __atoe( lts.lt_ecname );
  231. }
  232. #endif
  233. lo->ldo_tls_ctx = ti->ti_ctx_new( lo );
  234. if ( lo->ldo_tls_ctx == NULL ) {
  235. Debug0( LDAP_DEBUG_ANY,
  236. "TLS: could not allocate default ctx.\n" );
  237. rc = -1;
  238. goto error_exit;
  239. }
  240. rc = ti->ti_ctx_init( lo, &lts, is_server, errmsg );
  241. error_exit:
  242. if ( rc < 0 && lo->ldo_tls_ctx != NULL ) {
  243. ldap_pvt_tls_ctx_free( lo->ldo_tls_ctx );
  244. lo->ldo_tls_ctx = NULL;
  245. }
  246. #ifdef HAVE_EBCDIC
  247. LDAP_FREE( lts.lt_ciphersuite );
  248. LDAP_FREE( lts.lt_cacertfile );
  249. LDAP_FREE( lts.lt_certfile );
  250. LDAP_FREE( lts.lt_keyfile );
  251. LDAP_FREE( lts.lt_crlfile );
  252. LDAP_FREE( lts.lt_cacertdir );
  253. LDAP_FREE( lts.lt_dhfile );
  254. LDAP_FREE( lts.lt_ecname );
  255. #endif
  256. return rc;
  257. }
  258. /*
  259. * initialize the default context
  260. */
  261. int
  262. ldap_pvt_tls_init_def_ctx( int is_server )
  263. {
  264. struct ldapoptions *lo = LDAP_INT_GLOBAL_OPT();
  265. char errmsg[ERRBUFSIZE];
  266. int rc;
  267. errmsg[0] = 0;
  268. LDAP_MUTEX_LOCK( &tls_def_ctx_mutex );
  269. rc = ldap_int_tls_init_ctx( lo, is_server, errmsg );
  270. LDAP_MUTEX_UNLOCK( &tls_def_ctx_mutex );
  271. if ( rc ) {
  272. Debug1( LDAP_DEBUG_ANY,"TLS: init_def_ctx: %s.\n", errmsg );
  273. }
  274. return rc;
  275. }
  276. static tls_session *
  277. alloc_handle( void *ctx_arg, int is_server )
  278. {
  279. tls_ctx *ctx;
  280. tls_session *ssl;
  281. if ( ctx_arg ) {
  282. ctx = ctx_arg;
  283. } else {
  284. struct ldapoptions *lo = LDAP_INT_GLOBAL_OPT();
  285. if ( ldap_pvt_tls_init_def_ctx( is_server ) < 0 ) return NULL;
  286. ctx = lo->ldo_tls_ctx;
  287. }
  288. ssl = tls_imp->ti_session_new( ctx, is_server );
  289. if ( ssl == NULL ) {
  290. Debug0( LDAP_DEBUG_ANY,"TLS: can't create ssl handle.\n" );
  291. return NULL;
  292. }
  293. return ssl;
  294. }
  295. static int
  296. update_flags( Sockbuf *sb, tls_session * ssl, int rc )
  297. {
  298. sb->sb_trans_needs_read = 0;
  299. sb->sb_trans_needs_write = 0;
  300. return tls_imp->ti_session_upflags( sb, ssl, rc );
  301. }
  302. /*
  303. * Call this to do a TLS connect on a sockbuf. ctx_arg can be
  304. * a SSL_CTX * or NULL, in which case the default ctx is used.
  305. *
  306. * Return value:
  307. *
  308. * 0 - Success. Connection is ready for communication.
  309. * <0 - Error. Can't create a TLS stream.
  310. * >0 - Partial success.
  311. * Do a select (using information from lber_pvt_sb_needs_{read,write}
  312. * and call again.
  313. */
  314. static int
  315. ldap_int_tls_connect( LDAP *ld, LDAPConn *conn, const char *host )
  316. {
  317. Sockbuf *sb = conn->lconn_sb;
  318. int err;
  319. tls_session *ssl = NULL;
  320. const char *sni = host;
  321. if ( HAS_TLS( sb )) {
  322. ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_SSL, (void *)&ssl );
  323. } else {
  324. struct ldapoptions *lo;
  325. tls_ctx *ctx;
  326. ctx = ld->ld_options.ldo_tls_ctx;
  327. ssl = alloc_handle( ctx, 0 );
  328. if ( ssl == NULL ) return -1;
  329. #ifdef LDAP_DEBUG
  330. ber_sockbuf_add_io( sb, &ber_sockbuf_io_debug,
  331. LBER_SBIOD_LEVEL_TRANSPORT, (void *)"tls_" );
  332. #endif
  333. ber_sockbuf_add_io( sb, tls_imp->ti_sbio,
  334. LBER_SBIOD_LEVEL_TRANSPORT, (void *)ssl );
  335. lo = LDAP_INT_GLOBAL_OPT();
  336. if( ctx == NULL ) {
  337. ctx = lo->ldo_tls_ctx;
  338. ld->ld_options.ldo_tls_ctx = ctx;
  339. tls_ctx_ref( ctx );
  340. }
  341. if ( ld->ld_options.ldo_tls_connect_cb )
  342. ld->ld_options.ldo_tls_connect_cb( ld, ssl, ctx,
  343. ld->ld_options.ldo_tls_connect_arg );
  344. if ( lo && lo->ldo_tls_connect_cb && lo->ldo_tls_connect_cb !=
  345. ld->ld_options.ldo_tls_connect_cb )
  346. lo->ldo_tls_connect_cb( ld, ssl, ctx, lo->ldo_tls_connect_arg );
  347. }
  348. /* pass hostname for SNI, but only if it's an actual name
  349. * and not a numeric address
  350. */
  351. {
  352. int numeric = 1;
  353. unsigned char *c;
  354. for ( c = (unsigned char *)sni; *c; c++ ) {
  355. if ( *c == ':' ) /* IPv6 address */
  356. break;
  357. if ( *c == '.' )
  358. continue;
  359. if ( !isdigit( *c )) {
  360. numeric = 0;
  361. break;
  362. }
  363. }
  364. if ( numeric )
  365. sni = NULL;
  366. }
  367. err = tls_imp->ti_session_connect( ld, ssl, sni );
  368. #ifdef HAVE_WINSOCK
  369. errno = WSAGetLastError();
  370. #endif
  371. if ( err == 0 ) {
  372. err = ldap_pvt_tls_check_hostname( ld, ssl, host );
  373. }
  374. if ( err < 0 )
  375. {
  376. char buf[256], *msg;
  377. if ( update_flags( sb, ssl, err )) {
  378. return 1;
  379. }
  380. msg = tls_imp->ti_session_errmsg( ssl, err, buf, sizeof(buf) );
  381. if ( msg ) {
  382. if ( ld->ld_error ) {
  383. LDAP_FREE( ld->ld_error );
  384. }
  385. ld->ld_error = LDAP_STRDUP( msg );
  386. #ifdef HAVE_EBCDIC
  387. if ( ld->ld_error ) __etoa(ld->ld_error);
  388. #endif
  389. }
  390. Debug1( LDAP_DEBUG_ANY,"TLS: can't connect: %s.\n",
  391. ld->ld_error ? ld->ld_error : "" );
  392. ber_sockbuf_remove_io( sb, tls_imp->ti_sbio,
  393. LBER_SBIOD_LEVEL_TRANSPORT );
  394. #ifdef LDAP_DEBUG
  395. ber_sockbuf_remove_io( sb, &ber_sockbuf_io_debug,
  396. LBER_SBIOD_LEVEL_TRANSPORT );
  397. #endif
  398. return -1;
  399. }
  400. return 0;
  401. }
  402. int
  403. ldap_pvt_tls_connect( LDAP *ld, Sockbuf *sb, const char *host )
  404. {
  405. LDAPConn conn = { .lconn_sb = sb };
  406. return ldap_int_tls_connect( ld, &conn, host );
  407. }
  408. /*
  409. * Call this to do a TLS accept on a sockbuf.
  410. * Everything else is the same as with tls_connect.
  411. */
  412. int
  413. ldap_pvt_tls_accept( Sockbuf *sb, void *ctx_arg )
  414. {
  415. int err;
  416. tls_session *ssl = NULL;
  417. if ( HAS_TLS( sb )) {
  418. ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_SSL, (void *)&ssl );
  419. } else {
  420. ssl = alloc_handle( ctx_arg, 1 );
  421. if ( ssl == NULL ) return -1;
  422. #ifdef LDAP_DEBUG
  423. ber_sockbuf_add_io( sb, &ber_sockbuf_io_debug,
  424. LBER_SBIOD_LEVEL_TRANSPORT, (void *)"tls_" );
  425. #endif
  426. ber_sockbuf_add_io( sb, tls_imp->ti_sbio,
  427. LBER_SBIOD_LEVEL_TRANSPORT, (void *)ssl );
  428. }
  429. err = tls_imp->ti_session_accept( ssl );
  430. #ifdef HAVE_WINSOCK
  431. errno = WSAGetLastError();
  432. #endif
  433. if ( err < 0 )
  434. {
  435. if ( update_flags( sb, ssl, err )) return 1;
  436. if ( DebugTest( LDAP_DEBUG_ANY ) ) {
  437. char buf[256], *msg;
  438. msg = tls_imp->ti_session_errmsg( ssl, err, buf, sizeof(buf) );
  439. Debug1( LDAP_DEBUG_ANY,"TLS: can't accept: %s.\n",
  440. msg ? msg : "(unknown)" );
  441. }
  442. ber_sockbuf_remove_io( sb, tls_imp->ti_sbio,
  443. LBER_SBIOD_LEVEL_TRANSPORT );
  444. #ifdef LDAP_DEBUG
  445. ber_sockbuf_remove_io( sb, &ber_sockbuf_io_debug,
  446. LBER_SBIOD_LEVEL_TRANSPORT );
  447. #endif
  448. return -1;
  449. }
  450. return 0;
  451. }
  452. int
  453. ldap_pvt_tls_inplace ( Sockbuf *sb )
  454. {
  455. return HAS_TLS( sb ) ? 1 : 0;
  456. }
  457. int
  458. ldap_tls_inplace( LDAP *ld )
  459. {
  460. Sockbuf *sb = NULL;
  461. if ( ld->ld_defconn && ld->ld_defconn->lconn_sb ) {
  462. sb = ld->ld_defconn->lconn_sb;
  463. } else if ( ld->ld_sb ) {
  464. sb = ld->ld_sb;
  465. } else {
  466. return 0;
  467. }
  468. return ldap_pvt_tls_inplace( sb );
  469. }
  470. int
  471. ldap_pvt_tls_get_peer_dn( void *s, struct berval *dn,
  472. LDAPDN_rewrite_dummy *func, unsigned flags )
  473. {
  474. tls_session *session = s;
  475. struct berval bvdn;
  476. int rc;
  477. rc = tls_imp->ti_session_peer_dn( session, &bvdn );
  478. if ( rc ) return rc;
  479. rc = ldap_X509dn2bv( &bvdn, dn,
  480. (LDAPDN_rewrite_func *)func, flags);
  481. return rc;
  482. }
  483. int
  484. ldap_pvt_tls_check_hostname( LDAP *ld, void *s, const char *name_in )
  485. {
  486. tls_session *session = s;
  487. if (ld->ld_options.ldo_tls_require_cert != LDAP_OPT_X_TLS_NEVER &&
  488. ld->ld_options.ldo_tls_require_cert != LDAP_OPT_X_TLS_ALLOW) {
  489. ld->ld_errno = tls_imp->ti_session_chkhost( ld, session, name_in );
  490. if (ld->ld_errno != LDAP_SUCCESS) {
  491. return ld->ld_errno;
  492. }
  493. }
  494. /*
  495. * If instructed to do pinning, do it now
  496. */
  497. if ( !BER_BVISNULL( &ld->ld_options.ldo_tls_pin ) ) {
  498. ld->ld_errno = tls_imp->ti_session_pinning( ld, s,
  499. ld->ld_options.ldo_tls_pin_hashalg,
  500. &ld->ld_options.ldo_tls_pin );
  501. if (ld->ld_errno != LDAP_SUCCESS) {
  502. return ld->ld_errno;
  503. }
  504. }
  505. return LDAP_SUCCESS;
  506. }
  507. int
  508. ldap_pvt_tls_config( LDAP *ld, int option, const char *arg )
  509. {
  510. int i;
  511. switch( option ) {
  512. case LDAP_OPT_X_TLS_CACERTFILE:
  513. case LDAP_OPT_X_TLS_CACERTDIR:
  514. case LDAP_OPT_X_TLS_CERTFILE:
  515. case LDAP_OPT_X_TLS_KEYFILE:
  516. case LDAP_OPT_X_TLS_RANDOM_FILE:
  517. case LDAP_OPT_X_TLS_CIPHER_SUITE:
  518. case LDAP_OPT_X_TLS_DHFILE:
  519. case LDAP_OPT_X_TLS_PEERKEY_HASH:
  520. case LDAP_OPT_X_TLS_ECNAME:
  521. case LDAP_OPT_X_TLS_CRLFILE: /* GnuTLS only */
  522. return ldap_pvt_tls_set_option( ld, option, (void *) arg );
  523. case LDAP_OPT_X_TLS_REQUIRE_CERT:
  524. case LDAP_OPT_X_TLS_REQUIRE_SAN:
  525. case LDAP_OPT_X_TLS:
  526. i = -1;
  527. if ( strcasecmp( arg, "never" ) == 0 ) {
  528. i = LDAP_OPT_X_TLS_NEVER ;
  529. } else if ( strcasecmp( arg, "demand" ) == 0 ) {
  530. i = LDAP_OPT_X_TLS_DEMAND ;
  531. } else if ( strcasecmp( arg, "allow" ) == 0 ) {
  532. i = LDAP_OPT_X_TLS_ALLOW ;
  533. } else if ( strcasecmp( arg, "try" ) == 0 ) {
  534. i = LDAP_OPT_X_TLS_TRY ;
  535. } else if ( ( strcasecmp( arg, "hard" ) == 0 ) ||
  536. ( strcasecmp( arg, "on" ) == 0 ) ||
  537. ( strcasecmp( arg, "yes" ) == 0) ||
  538. ( strcasecmp( arg, "true" ) == 0 ) )
  539. {
  540. i = LDAP_OPT_X_TLS_HARD ;
  541. }
  542. if (i >= 0) {
  543. return ldap_pvt_tls_set_option( ld, option, &i );
  544. }
  545. return -1;
  546. case LDAP_OPT_X_TLS_PROTOCOL_MAX:
  547. case LDAP_OPT_X_TLS_PROTOCOL_MIN: {
  548. char *next;
  549. long l;
  550. l = strtol( arg, &next, 10 );
  551. if ( l < 0 || l > 0xff || next == arg ||
  552. ( *next != '\0' && *next != '.' ) )
  553. return -1;
  554. i = l << 8;
  555. if (*next == '.') {
  556. arg = next + 1;
  557. l = strtol( arg, &next, 10 );
  558. if ( l < 0 || l > 0xff || next == arg || *next != '\0' )
  559. return -1;
  560. i += l;
  561. }
  562. return ldap_pvt_tls_set_option( ld, option, &i );
  563. }
  564. #ifdef HAVE_OPENSSL
  565. case LDAP_OPT_X_TLS_CRLCHECK: /* OpenSSL only */
  566. i = -1;
  567. if ( strcasecmp( arg, "none" ) == 0 ) {
  568. i = LDAP_OPT_X_TLS_CRL_NONE ;
  569. } else if ( strcasecmp( arg, "peer" ) == 0 ) {
  570. i = LDAP_OPT_X_TLS_CRL_PEER ;
  571. } else if ( strcasecmp( arg, "all" ) == 0 ) {
  572. i = LDAP_OPT_X_TLS_CRL_ALL ;
  573. }
  574. if (i >= 0) {
  575. return ldap_pvt_tls_set_option( ld, option, &i );
  576. }
  577. return -1;
  578. #endif
  579. }
  580. return -1;
  581. }
  582. int
  583. ldap_pvt_tls_get_option( LDAP *ld, int option, void *arg )
  584. {
  585. struct ldapoptions *lo;
  586. if( option == LDAP_OPT_X_TLS_PACKAGE ) {
  587. *(char **)arg = LDAP_STRDUP( tls_imp->ti_name );
  588. return 0;
  589. }
  590. if( ld != NULL ) {
  591. assert( LDAP_VALID( ld ) );
  592. if( !LDAP_VALID( ld ) ) {
  593. return LDAP_OPT_ERROR;
  594. }
  595. lo = &ld->ld_options;
  596. } else {
  597. /* Get pointer to global option structure */
  598. lo = LDAP_INT_GLOBAL_OPT();
  599. if ( lo == NULL ) {
  600. return LDAP_NO_MEMORY;
  601. }
  602. }
  603. switch( option ) {
  604. case LDAP_OPT_X_TLS:
  605. *(int *)arg = lo->ldo_tls_mode;
  606. break;
  607. case LDAP_OPT_X_TLS_CTX:
  608. *(void **)arg = lo->ldo_tls_ctx;
  609. if ( lo->ldo_tls_ctx ) {
  610. tls_ctx_ref( lo->ldo_tls_ctx );
  611. }
  612. break;
  613. case LDAP_OPT_X_TLS_CACERTFILE:
  614. *(char **)arg = lo->ldo_tls_cacertfile ?
  615. LDAP_STRDUP( lo->ldo_tls_cacertfile ) : NULL;
  616. break;
  617. case LDAP_OPT_X_TLS_CACERTDIR:
  618. *(char **)arg = lo->ldo_tls_cacertdir ?
  619. LDAP_STRDUP( lo->ldo_tls_cacertdir ) : NULL;
  620. break;
  621. case LDAP_OPT_X_TLS_CERTFILE:
  622. *(char **)arg = lo->ldo_tls_certfile ?
  623. LDAP_STRDUP( lo->ldo_tls_certfile ) : NULL;
  624. break;
  625. case LDAP_OPT_X_TLS_KEYFILE:
  626. *(char **)arg = lo->ldo_tls_keyfile ?
  627. LDAP_STRDUP( lo->ldo_tls_keyfile ) : NULL;
  628. break;
  629. case LDAP_OPT_X_TLS_DHFILE:
  630. *(char **)arg = lo->ldo_tls_dhfile ?
  631. LDAP_STRDUP( lo->ldo_tls_dhfile ) : NULL;
  632. break;
  633. case LDAP_OPT_X_TLS_ECNAME:
  634. *(char **)arg = lo->ldo_tls_ecname ?
  635. LDAP_STRDUP( lo->ldo_tls_ecname ) : NULL;
  636. break;
  637. case LDAP_OPT_X_TLS_CRLFILE: /* GnuTLS only */
  638. *(char **)arg = lo->ldo_tls_crlfile ?
  639. LDAP_STRDUP( lo->ldo_tls_crlfile ) : NULL;
  640. break;
  641. case LDAP_OPT_X_TLS_REQUIRE_CERT:
  642. *(int *)arg = lo->ldo_tls_require_cert;
  643. break;
  644. case LDAP_OPT_X_TLS_REQUIRE_SAN:
  645. *(int *)arg = lo->ldo_tls_require_san;
  646. break;
  647. #ifdef HAVE_OPENSSL
  648. case LDAP_OPT_X_TLS_CRLCHECK: /* OpenSSL only */
  649. *(int *)arg = lo->ldo_tls_crlcheck;
  650. break;
  651. #endif
  652. case LDAP_OPT_X_TLS_CIPHER_SUITE:
  653. *(char **)arg = lo->ldo_tls_ciphersuite ?
  654. LDAP_STRDUP( lo->ldo_tls_ciphersuite ) : NULL;
  655. break;
  656. case LDAP_OPT_X_TLS_PROTOCOL_MIN:
  657. *(int *)arg = lo->ldo_tls_protocol_min;
  658. break;
  659. case LDAP_OPT_X_TLS_PROTOCOL_MAX:
  660. *(int *)arg = lo->ldo_tls_protocol_max;
  661. break;
  662. case LDAP_OPT_X_TLS_RANDOM_FILE:
  663. *(char **)arg = lo->ldo_tls_randfile ?
  664. LDAP_STRDUP( lo->ldo_tls_randfile ) : NULL;
  665. break;
  666. case LDAP_OPT_X_TLS_SSL_CTX: {
  667. void *retval = 0;
  668. if ( ld != NULL ) {
  669. LDAPConn *conn = ld->ld_defconn;
  670. if ( conn != NULL ) {
  671. Sockbuf *sb = conn->lconn_sb;
  672. retval = ldap_pvt_tls_sb_ctx( sb );
  673. }
  674. }
  675. *(void **)arg = retval;
  676. break;
  677. }
  678. case LDAP_OPT_X_TLS_CONNECT_CB:
  679. *(LDAP_TLS_CONNECT_CB **)arg = lo->ldo_tls_connect_cb;
  680. break;
  681. case LDAP_OPT_X_TLS_CONNECT_ARG:
  682. *(void **)arg = lo->ldo_tls_connect_arg;
  683. break;
  684. case LDAP_OPT_X_TLS_VERSION: {
  685. void *sess = NULL;
  686. const char *retval = NULL;
  687. if ( ld != NULL ) {
  688. LDAPConn *conn = ld->ld_defconn;
  689. if ( conn != NULL ) {
  690. Sockbuf *sb = conn->lconn_sb;
  691. sess = ldap_pvt_tls_sb_ctx( sb );
  692. if ( sess != NULL )
  693. retval = ldap_pvt_tls_get_version( sess );
  694. }
  695. }
  696. *(char **)arg = retval ? LDAP_STRDUP( retval ) : NULL;
  697. break;
  698. }
  699. case LDAP_OPT_X_TLS_CIPHER: {
  700. void *sess = NULL;
  701. const char *retval = NULL;
  702. if ( ld != NULL ) {
  703. LDAPConn *conn = ld->ld_defconn;
  704. if ( conn != NULL ) {
  705. Sockbuf *sb = conn->lconn_sb;
  706. sess = ldap_pvt_tls_sb_ctx( sb );
  707. if ( sess != NULL )
  708. retval = ldap_pvt_tls_get_cipher( sess );
  709. }
  710. }
  711. *(char **)arg = retval ? LDAP_STRDUP( retval ) : NULL;
  712. break;
  713. }
  714. case LDAP_OPT_X_TLS_PEERCERT: {
  715. void *sess = NULL;
  716. struct berval *bv = arg;
  717. bv->bv_len = 0;
  718. bv->bv_val = NULL;
  719. if ( ld != NULL ) {
  720. LDAPConn *conn = ld->ld_defconn;
  721. if ( conn != NULL ) {
  722. Sockbuf *sb = conn->lconn_sb;
  723. sess = ldap_pvt_tls_sb_ctx( sb );
  724. if ( sess != NULL )
  725. return ldap_pvt_tls_get_peercert( sess, bv );
  726. }
  727. }
  728. break;
  729. }
  730. case LDAP_OPT_X_TLS_CACERT: {
  731. struct berval *bv = arg;
  732. if ( lo->ldo_tls_cacert.bv_val ) {
  733. ber_dupbv( bv, &lo->ldo_tls_cacert );
  734. } else {
  735. BER_BVZERO( bv );
  736. }
  737. break;
  738. }
  739. case LDAP_OPT_X_TLS_CERT: {
  740. struct berval *bv = arg;
  741. if ( lo->ldo_tls_cert.bv_val ) {
  742. ber_dupbv( bv, &lo->ldo_tls_cert );
  743. } else {
  744. BER_BVZERO( bv );
  745. }
  746. break;
  747. }
  748. case LDAP_OPT_X_TLS_KEY: {
  749. struct berval *bv = arg;
  750. if ( lo->ldo_tls_key.bv_val ) {
  751. ber_dupbv( bv, &lo->ldo_tls_key );
  752. } else {
  753. BER_BVZERO( bv );
  754. }
  755. break;
  756. }
  757. default:
  758. return -1;
  759. }
  760. return 0;
  761. }
  762. int
  763. ldap_pvt_tls_set_option( LDAP *ld, int option, void *arg )
  764. {
  765. struct ldapoptions *lo;
  766. if( ld != NULL ) {
  767. assert( LDAP_VALID( ld ) );
  768. if( !LDAP_VALID( ld ) ) {
  769. return LDAP_OPT_ERROR;
  770. }
  771. lo = &ld->ld_options;
  772. } else {
  773. /* Get pointer to global option structure */
  774. lo = LDAP_INT_GLOBAL_OPT();
  775. if ( lo == NULL ) {
  776. return LDAP_NO_MEMORY;
  777. }
  778. }
  779. switch( option ) {
  780. case LDAP_OPT_X_TLS:
  781. if ( !arg ) return -1;
  782. switch( *(int *) arg ) {
  783. case LDAP_OPT_X_TLS_NEVER:
  784. case LDAP_OPT_X_TLS_DEMAND:
  785. case LDAP_OPT_X_TLS_ALLOW:
  786. case LDAP_OPT_X_TLS_TRY:
  787. case LDAP_OPT_X_TLS_HARD:
  788. if (lo != NULL) {
  789. lo->ldo_tls_mode = *(int *)arg;
  790. }
  791. return 0;
  792. }
  793. return -1;
  794. case LDAP_OPT_X_TLS_CTX:
  795. if ( lo->ldo_tls_ctx )
  796. ldap_pvt_tls_ctx_free( lo->ldo_tls_ctx );
  797. lo->ldo_tls_ctx = arg;
  798. tls_ctx_ref( lo->ldo_tls_ctx );
  799. return 0;
  800. case LDAP_OPT_X_TLS_CONNECT_CB:
  801. lo->ldo_tls_connect_cb = (LDAP_TLS_CONNECT_CB *)arg;
  802. return 0;
  803. case LDAP_OPT_X_TLS_CONNECT_ARG:
  804. lo->ldo_tls_connect_arg = arg;
  805. return 0;
  806. case LDAP_OPT_X_TLS_CACERTFILE:
  807. if ( lo->ldo_tls_cacertfile ) LDAP_FREE( lo->ldo_tls_cacertfile );
  808. lo->ldo_tls_cacertfile = (arg && *(char *)arg) ? LDAP_STRDUP( (char *) arg ) : NULL;
  809. return 0;
  810. case LDAP_OPT_X_TLS_CACERTDIR:
  811. if ( lo->ldo_tls_cacertdir ) LDAP_FREE( lo->ldo_tls_cacertdir );
  812. lo->ldo_tls_cacertdir = (arg && *(char *)arg) ? LDAP_STRDUP( (char *) arg ) : NULL;
  813. return 0;
  814. case LDAP_OPT_X_TLS_CERTFILE:
  815. if ( lo->ldo_tls_certfile ) LDAP_FREE( lo->ldo_tls_certfile );
  816. lo->ldo_tls_certfile = (arg && *(char *)arg) ? LDAP_STRDUP( (char *) arg ) : NULL;
  817. return 0;
  818. case LDAP_OPT_X_TLS_KEYFILE:
  819. if ( lo->ldo_tls_keyfile ) LDAP_FREE( lo->ldo_tls_keyfile );
  820. lo->ldo_tls_keyfile = (arg && *(char *)arg) ? LDAP_STRDUP( (char *) arg ) : NULL;
  821. return 0;
  822. case LDAP_OPT_X_TLS_DHFILE:
  823. if ( lo->ldo_tls_dhfile ) LDAP_FREE( lo->ldo_tls_dhfile );
  824. lo->ldo_tls_dhfile = (arg && *(char *)arg) ? LDAP_STRDUP( (char *) arg ) : NULL;
  825. return 0;
  826. case LDAP_OPT_X_TLS_ECNAME:
  827. if ( lo->ldo_tls_ecname ) LDAP_FREE( lo->ldo_tls_ecname );
  828. lo->ldo_tls_ecname = (arg && *(char *)arg) ? LDAP_STRDUP( (char *) arg ) : NULL;
  829. return 0;
  830. case LDAP_OPT_X_TLS_CRLFILE: /* GnuTLS only */
  831. if ( lo->ldo_tls_crlfile ) LDAP_FREE( lo->ldo_tls_crlfile );
  832. lo->ldo_tls_crlfile = (arg && *(char *)arg) ? LDAP_STRDUP( (char *) arg ) : NULL;
  833. return 0;
  834. case LDAP_OPT_X_TLS_REQUIRE_CERT:
  835. if ( !arg ) return -1;
  836. switch( *(int *) arg ) {
  837. case LDAP_OPT_X_TLS_NEVER:
  838. case LDAP_OPT_X_TLS_DEMAND:
  839. case LDAP_OPT_X_TLS_ALLOW:
  840. case LDAP_OPT_X_TLS_TRY:
  841. case LDAP_OPT_X_TLS_HARD:
  842. lo->ldo_tls_require_cert = * (int *) arg;
  843. return 0;
  844. }
  845. return -1;
  846. case LDAP_OPT_X_TLS_REQUIRE_SAN:
  847. if ( !arg ) return -1;
  848. switch( *(int *) arg ) {
  849. case LDAP_OPT_X_TLS_NEVER:
  850. case LDAP_OPT_X_TLS_DEMAND:
  851. case LDAP_OPT_X_TLS_ALLOW:
  852. case LDAP_OPT_X_TLS_TRY:
  853. case LDAP_OPT_X_TLS_HARD:
  854. lo->ldo_tls_require_san = * (int *) arg;
  855. return 0;
  856. }
  857. return -1;
  858. #ifdef HAVE_OPENSSL
  859. case LDAP_OPT_X_TLS_CRLCHECK: /* OpenSSL only */
  860. if ( !arg ) return -1;
  861. switch( *(int *) arg ) {
  862. case LDAP_OPT_X_TLS_CRL_NONE:
  863. case LDAP_OPT_X_TLS_CRL_PEER:
  864. case LDAP_OPT_X_TLS_CRL_ALL:
  865. lo->ldo_tls_crlcheck = * (int *) arg;
  866. return 0;
  867. }
  868. return -1;
  869. #endif
  870. case LDAP_OPT_X_TLS_CIPHER_SUITE:
  871. if ( lo->ldo_tls_ciphersuite ) LDAP_FREE( lo->ldo_tls_ciphersuite );
  872. lo->ldo_tls_ciphersuite = (arg && *(char *)arg) ? LDAP_STRDUP( (char *) arg ) : NULL;
  873. return 0;
  874. case LDAP_OPT_X_TLS_PROTOCOL_MIN:
  875. if ( !arg ) return -1;
  876. lo->ldo_tls_protocol_min = *(int *)arg;
  877. return 0;
  878. case LDAP_OPT_X_TLS_PROTOCOL_MAX:
  879. if ( !arg ) return -1;
  880. lo->ldo_tls_protocol_max = *(int *)arg;
  881. return 0;
  882. case LDAP_OPT_X_TLS_RANDOM_FILE:
  883. if ( ld != NULL )
  884. return -1;
  885. if ( lo->ldo_tls_randfile ) LDAP_FREE (lo->ldo_tls_randfile );
  886. lo->ldo_tls_randfile = (arg && *(char *)arg) ? LDAP_STRDUP( (char *) arg ) : NULL;
  887. break;
  888. case LDAP_OPT_X_TLS_NEWCTX: {
  889. int rc;
  890. char errmsg[ERRBUFSIZE];
  891. if ( !arg ) return -1;
  892. if ( lo->ldo_tls_ctx )
  893. ldap_pvt_tls_ctx_free( lo->ldo_tls_ctx );
  894. lo->ldo_tls_ctx = NULL;
  895. errmsg[0] = 0;
  896. rc = ldap_int_tls_init_ctx( lo, *(int *)arg, errmsg );
  897. if ( rc && errmsg[0] && ld ) {
  898. if ( ld->ld_error )
  899. LDAP_FREE( ld->ld_error );
  900. ld->ld_error = LDAP_STRDUP( errmsg );
  901. }
  902. return rc;
  903. }
  904. case LDAP_OPT_X_TLS_CACERT:
  905. if ( lo->ldo_tls_cacert.bv_val )
  906. LDAP_FREE( lo->ldo_tls_cacert.bv_val );
  907. if ( arg ) {
  908. lo->ldo_tls_cacert.bv_len = ((struct berval *)arg)->bv_len;
  909. lo->ldo_tls_cacert.bv_val = LDAP_MALLOC( lo->ldo_tls_cacert.bv_len );
  910. if ( !lo->ldo_tls_cacert.bv_val )
  911. return -1;
  912. AC_MEMCPY( lo->ldo_tls_cacert.bv_val, ((struct berval *)arg)->bv_val, lo->ldo_tls_cacert.bv_len );
  913. } else {
  914. BER_BVZERO( &lo->ldo_tls_cacert );
  915. }
  916. break;
  917. case LDAP_OPT_X_TLS_CERT:
  918. if ( lo->ldo_tls_cert.bv_val )
  919. LDAP_FREE( lo->ldo_tls_cert.bv_val );
  920. if ( arg ) {
  921. lo->ldo_tls_cert.bv_len = ((struct berval *)arg)->bv_len;
  922. lo->ldo_tls_cert.bv_val = LDAP_MALLOC( lo->ldo_tls_cert.bv_len );
  923. if ( !lo->ldo_tls_cert.bv_val )
  924. return -1;
  925. AC_MEMCPY( lo->ldo_tls_cert.bv_val, ((struct berval *)arg)->bv_val, lo->ldo_tls_cert.bv_len );
  926. } else {
  927. BER_BVZERO( &lo->ldo_tls_cert );
  928. }
  929. break;
  930. case LDAP_OPT_X_TLS_KEY:
  931. if ( lo->ldo_tls_key.bv_val )
  932. LDAP_FREE( lo->ldo_tls_key.bv_val );
  933. if ( arg ) {
  934. lo->ldo_tls_key.bv_len = ((struct berval *)arg)->bv_len;
  935. lo->ldo_tls_key.bv_val = LDAP_MALLOC( lo->ldo_tls_key.bv_len );
  936. if ( !lo->ldo_tls_key.bv_val )
  937. return -1;
  938. AC_MEMCPY( lo->ldo_tls_key.bv_val, ((struct berval *)arg)->bv_val, lo->ldo_tls_key.bv_len );
  939. } else {
  940. BER_BVZERO( &lo->ldo_tls_key );
  941. }
  942. break;
  943. case LDAP_OPT_X_TLS_PEERKEY_HASH: {
  944. /* arg = "[hashalg:]pubkey_hash" */
  945. struct berval bv;
  946. char *p, *pin = arg;
  947. int rc = LDAP_SUCCESS;
  948. if ( !tls_imp->ti_session_pinning ) return -1;
  949. if ( !pin || !*pin ) {
  950. if ( lo->ldo_tls_pin_hashalg ) {
  951. LDAP_FREE( lo->ldo_tls_pin_hashalg );
  952. } else if ( lo->ldo_tls_pin.bv_val ) {
  953. LDAP_FREE( lo->ldo_tls_pin.bv_val );
  954. }
  955. lo->ldo_tls_pin_hashalg = NULL;
  956. BER_BVZERO( &lo->ldo_tls_pin );
  957. return rc;
  958. }
  959. pin = LDAP_STRDUP( pin );
  960. p = strchr( pin, ':' );
  961. /* pubkey (its hash) goes in bv, alg in p */
  962. if ( p ) {
  963. *p = '\0';
  964. bv.bv_val = p+1;
  965. p = pin;
  966. } else {
  967. bv.bv_val = pin;
  968. }
  969. bv.bv_len = strlen(bv.bv_val);
  970. if ( ldap_int_decode_b64_inplace( &bv ) ) {
  971. LDAP_FREE( pin );
  972. return -1;
  973. }
  974. if ( ld != NULL ) {
  975. LDAPConn *conn = ld->ld_defconn;
  976. if ( conn != NULL ) {
  977. Sockbuf *sb = conn->lconn_sb;
  978. void *sess = ldap_pvt_tls_sb_ctx( sb );
  979. if ( sess != NULL ) {
  980. rc = tls_imp->ti_session_pinning( ld, sess, p, &bv );
  981. }
  982. }
  983. }
  984. if ( rc == LDAP_SUCCESS ) {
  985. if ( lo->ldo_tls_pin_hashalg ) {
  986. LDAP_FREE( lo->ldo_tls_pin_hashalg );
  987. } else if ( lo->ldo_tls_pin.bv_val ) {
  988. LDAP_FREE( lo->ldo_tls_pin.bv_val );
  989. }
  990. lo->ldo_tls_pin_hashalg = p;
  991. lo->ldo_tls_pin = bv;
  992. } else {
  993. LDAP_FREE( pin );
  994. }
  995. return rc;
  996. }
  997. default:
  998. return -1;
  999. }
  1000. return 0;
  1001. }
  1002. int
  1003. ldap_int_tls_start ( LDAP *ld, LDAPConn *conn, LDAPURLDesc *srv )
  1004. {
  1005. Sockbuf *sb;
  1006. char *host;
  1007. void *ssl;
  1008. int ret, async;
  1009. struct timeval start_time_tv, tv, tv0;
  1010. ber_socket_t sd = AC_SOCKET_ERROR;
  1011. if ( !conn )
  1012. return LDAP_PARAM_ERROR;
  1013. sb = conn->lconn_sb;
  1014. if( srv ) {
  1015. host = srv->lud_host;
  1016. } else {
  1017. host = conn->lconn_server->lud_host;
  1018. }
  1019. /* avoid NULL host */
  1020. if( host == NULL ) {
  1021. host = "localhost";
  1022. }
  1023. (void) tls_init( tls_imp, 0 );
  1024. /*
  1025. * Use non-blocking io during SSL Handshake when a timeout is configured
  1026. */
  1027. async = LDAP_BOOL_GET( &ld->ld_options, LDAP_BOOL_CONNECT_ASYNC );
  1028. if ( ld->ld_options.ldo_tm_net.tv_sec >= 0 ) {
  1029. if ( !async ) {
  1030. /* if async, this has already been set */
  1031. ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_NONBLOCK, (void*)1 );
  1032. }
  1033. ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
  1034. tv = ld->ld_options.ldo_tm_net;
  1035. tv0 = tv;
  1036. #ifdef HAVE_GETTIMEOFDAY
  1037. gettimeofday( &start_time_tv, NULL );
  1038. #else /* ! HAVE_GETTIMEOFDAY */
  1039. time( &start_time_tv.tv_sec );
  1040. start_time_tv.tv_usec = 0;
  1041. #endif /* ! HAVE_GETTIMEOFDAY */
  1042. }
  1043. ld->ld_errno = LDAP_SUCCESS;
  1044. ret = ldap_int_tls_connect( ld, conn, host );
  1045. /* this mainly only happens for non-blocking io
  1046. * but can also happen when the handshake is too
  1047. * big for a single network message.
  1048. */
  1049. while ( ret > 0 ) {
  1050. if ( async ) {
  1051. struct timeval curr_time_tv, delta_tv;
  1052. int wr=0;
  1053. if ( sb->sb_trans_needs_read ) {
  1054. wr=0;
  1055. } else if ( sb->sb_trans_needs_write ) {
  1056. wr=1;
  1057. }
  1058. Debug1( LDAP_DEBUG_TRACE, "ldap_int_tls_start: ldap_int_tls_connect needs %s\n",
  1059. wr ? "write": "read" );
  1060. /* This is mostly copied from result.c:wait4msg(), should
  1061. * probably be moved into a separate function */
  1062. #ifdef HAVE_GETTIMEOFDAY
  1063. gettimeofday( &curr_time_tv, NULL );
  1064. #else /* ! HAVE_GETTIMEOFDAY */
  1065. time( &curr_time_tv.tv_sec );
  1066. curr_time_tv.tv_usec = 0;
  1067. #endif /* ! HAVE_GETTIMEOFDAY */
  1068. /* delta = curr - start */
  1069. delta_tv.tv_sec = curr_time_tv.tv_sec - start_time_tv.tv_sec;
  1070. delta_tv.tv_usec = curr_time_tv.tv_usec - start_time_tv.tv_usec;
  1071. if ( delta_tv.tv_usec < 0 ) {
  1072. delta_tv.tv_sec--;
  1073. delta_tv.tv_usec += 1000000;
  1074. }
  1075. /* tv0 < delta ? */
  1076. if ( ( tv0.tv_sec < delta_tv.tv_sec ) ||
  1077. ( ( tv0.tv_sec == delta_tv.tv_sec ) &&
  1078. ( tv0.tv_usec < delta_tv.tv_usec ) ) )
  1079. {
  1080. ret = -1;
  1081. ld->ld_errno = LDAP_TIMEOUT;
  1082. break;
  1083. }
  1084. /* timeout -= delta_time */
  1085. tv0.tv_sec -= delta_tv.tv_sec;
  1086. tv0.tv_usec -= delta_tv.tv_usec;
  1087. if ( tv0.tv_usec < 0 ) {
  1088. tv0.tv_sec--;
  1089. tv0.tv_usec += 1000000;
  1090. }
  1091. start_time_tv.tv_sec = curr_time_tv.tv_sec;
  1092. start_time_tv.tv_usec = curr_time_tv.tv_usec;
  1093. tv = tv0;
  1094. Debug3( LDAP_DEBUG_TRACE, "ldap_int_tls_start: ld %p %ld s %ld us to go\n",
  1095. (void *)ld, (long) tv.tv_sec, (long) tv.tv_usec );
  1096. ret = ldap_int_poll( ld, sd, &tv, wr);
  1097. if ( ret < 0 ) {
  1098. ld->ld_errno = LDAP_TIMEOUT;
  1099. break;
  1100. }
  1101. }
  1102. ret = ldap_int_tls_connect( ld, conn, host );
  1103. }
  1104. if ( ret < 0 ) {
  1105. if ( ld->ld_errno == LDAP_SUCCESS )
  1106. ld->ld_errno = LDAP_CONNECT_ERROR;
  1107. return (ld->ld_errno);
  1108. }
  1109. return LDAP_SUCCESS;
  1110. }
  1111. void *
  1112. ldap_pvt_tls_sb_ctx( Sockbuf *sb )
  1113. {
  1114. void *p = NULL;
  1115. ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_SSL, (void *)&p );
  1116. return p;
  1117. }
  1118. int
  1119. ldap_pvt_tls_get_strength( void *s )
  1120. {
  1121. tls_session *session = s;
  1122. return tls_imp->ti_session_strength( session );
  1123. }
  1124. int
  1125. ldap_pvt_tls_get_my_dn( void *s, struct berval *dn, LDAPDN_rewrite_dummy *func, unsigned flags )
  1126. {
  1127. tls_session *session = s;
  1128. struct berval der_dn;
  1129. int rc;
  1130. rc = tls_imp->ti_session_my_dn( session, &der_dn );
  1131. if ( rc == LDAP_SUCCESS )
  1132. rc = ldap_X509dn2bv(&der_dn, dn, (LDAPDN_rewrite_func *)func, flags );
  1133. return rc;
  1134. }
  1135. int
  1136. ldap_pvt_tls_get_unique( void *s, struct berval *buf, int is_server )
  1137. {
  1138. tls_session *session = s;
  1139. return tls_imp->ti_session_unique( session, buf, is_server );
  1140. }
  1141. int
  1142. ldap_pvt_tls_get_endpoint( void *s, struct berval *buf, int is_server )
  1143. {
  1144. tls_session *session = s;
  1145. return tls_imp->ti_session_endpoint( session, buf, is_server );
  1146. }
  1147. const char *
  1148. ldap_pvt_tls_get_version( void *s )
  1149. {
  1150. tls_session *session = s;
  1151. return tls_imp->ti_session_version( session );
  1152. }
  1153. const char *
  1154. ldap_pvt_tls_get_cipher( void *s )
  1155. {
  1156. tls_session *session = s;
  1157. return tls_imp->ti_session_cipher( session );
  1158. }
  1159. int
  1160. ldap_pvt_tls_get_peercert( void *s, struct berval *der )
  1161. {
  1162. tls_session *session = s;
  1163. return tls_imp->ti_session_peercert( session, der );
  1164. }
  1165. #endif /* HAVE_TLS */
  1166. int
  1167. ldap_start_tls( LDAP *ld,
  1168. LDAPControl **serverctrls,
  1169. LDAPControl **clientctrls,
  1170. int *msgidp )
  1171. {
  1172. return ldap_extended_operation( ld, LDAP_EXOP_START_TLS,
  1173. NULL, serverctrls, clientctrls, msgidp );
  1174. }
  1175. int
  1176. ldap_install_tls( LDAP *ld )
  1177. {
  1178. #ifndef HAVE_TLS
  1179. return LDAP_NOT_SUPPORTED;
  1180. #else
  1181. if ( ldap_tls_inplace( ld ) ) {
  1182. return LDAP_LOCAL_ERROR;
  1183. }
  1184. return ldap_int_tls_start( ld, ld->ld_defconn, NULL );
  1185. #endif
  1186. }
  1187. int
  1188. ldap_start_tls_s ( LDAP *ld,
  1189. LDAPControl **serverctrls,
  1190. LDAPControl **clientctrls )
  1191. {
  1192. #ifndef HAVE_TLS
  1193. return LDAP_NOT_SUPPORTED;
  1194. #else
  1195. int rc;
  1196. char *rspoid = NULL;
  1197. struct berval *rspdata = NULL;
  1198. /* XXYYZ: this initiates operation only on default connection! */
  1199. if ( ldap_tls_inplace( ld ) ) {
  1200. return LDAP_LOCAL_ERROR;
  1201. }
  1202. rc = ldap_extended_operation_s( ld, LDAP_EXOP_START_TLS,
  1203. NULL, serverctrls, clientctrls, &rspoid, &rspdata );
  1204. if ( rspoid != NULL ) {
  1205. LDAP_FREE(rspoid);
  1206. }
  1207. if ( rspdata != NULL ) {
  1208. ber_bvfree( rspdata );
  1209. }
  1210. if ( rc == LDAP_SUCCESS ) {
  1211. rc = ldap_int_tls_start( ld, ld->ld_defconn, NULL );
  1212. }
  1213. return rc;
  1214. #endif
  1215. }
  1216. /* These tags probably all belong in lber.h, but they're
  1217. * not normally encountered when processing LDAP, so maybe
  1218. * they belong somewhere else instead.
  1219. */
  1220. #define LBER_TAG_OID ((ber_tag_t) 0x06UL)
  1221. /* Tags for string types used in a DirectoryString.
  1222. *
  1223. * Note that IA5string is not one of the defined choices for
  1224. * DirectoryString in X.520, but it gets used for email AVAs.
  1225. */
  1226. #define LBER_TAG_UTF8 ((ber_tag_t) 0x0cUL)
  1227. #define LBER_TAG_PRINTABLE ((ber_tag_t) 0x13UL)
  1228. #define LBER_TAG_TELETEX ((ber_tag_t) 0x14UL)
  1229. #define LBER_TAG_IA5 ((ber_tag_t) 0x16UL)
  1230. #define LBER_TAG_UNIVERSAL ((ber_tag_t) 0x1cUL)
  1231. #define LBER_TAG_BMP ((ber_tag_t) 0x1eUL)
  1232. static oid_name *
  1233. find_oid( struct berval *oid )
  1234. {
  1235. int i;
  1236. for ( i=0; !BER_BVISNULL( &oids[i].oid ); i++ ) {
  1237. if ( oids[i].oid.bv_len != oid->bv_len ) continue;
  1238. if ( !strcmp( oids[i].oid.bv_val, oid->bv_val ))
  1239. return &oids[i];
  1240. }
  1241. return NULL;
  1242. }
  1243. /* Converts BER Bitstring value to LDAP BitString value (RFC4517)
  1244. *
  1245. * berValue : IN
  1246. * rfc4517Value: OUT
  1247. *
  1248. * berValue and ldapValue should not be NULL
  1249. */
  1250. #define BITS_PER_BYTE 8
  1251. #define SQUOTE_LENGTH 1
  1252. #define B_CHAR_LENGTH 1
  1253. #define STR_OVERHEAD (2*SQUOTE_LENGTH + B_CHAR_LENGTH)
  1254. static int
  1255. der_to_ldap_BitString (struct berval *berValue,
  1256. struct berval *ldapValue)
  1257. {
  1258. ber_len_t bitPadding=0;
  1259. ber_len_t bits, maxBits;
  1260. char *tmpStr;
  1261. unsigned char byte;
  1262. ber_len_t bitLength;
  1263. ber_len_t valLen;
  1264. unsigned char* valPtr;
  1265. ldapValue->bv_len=0;
  1266. ldapValue->bv_val=NULL;
  1267. /* Gets padding and points to binary data */
  1268. valLen=berValue->bv_len;
  1269. valPtr=(unsigned char*)berValue->bv_val;
  1270. if (valLen) {
  1271. bitPadding=(ber_len_t)(valPtr[0]);
  1272. valLen--;
  1273. valPtr++;
  1274. }
  1275. /* If Block is non DER encoding fixes to DER encoding */
  1276. if (bitPadding >= BITS_PER_BYTE) {
  1277. if (valLen*BITS_PER_BYTE > bitPadding ) {
  1278. valLen-=(bitPadding/BITS_PER_BYTE);
  1279. bitPadding%=BITS_PER_BYTE;
  1280. } else {
  1281. valLen=0;
  1282. bitPadding=0;
  1283. }
  1284. }
  1285. /* Just in case bad encoding */
  1286. if (valLen*BITS_PER_BYTE < bitPadding ) {
  1287. bitPadding=0;
  1288. valLen=0;
  1289. }
  1290. /* Gets buffer to hold RFC4517 Bit String format */
  1291. bitLength=valLen*BITS_PER_BYTE-bitPadding;
  1292. tmpStr=LDAP_MALLOC(bitLength + STR_OVERHEAD + 1);
  1293. if (!tmpStr)
  1294. return LDAP_NO_MEMORY;
  1295. ldapValue->bv_val=tmpStr;
  1296. ldapValue->bv_len=bitLength + STR_OVERHEAD;
  1297. /* Formatting in '*binary-digit'B format */
  1298. maxBits=BITS_PER_BYTE;
  1299. *tmpStr++ ='\'';
  1300. while(valLen) {
  1301. byte=*valPtr;
  1302. if (valLen==1)
  1303. maxBits-=bitPadding;
  1304. for (bits=0; bits<maxBits; bits++) {
  1305. if (0x80 & byte)
  1306. *tmpStr='1';
  1307. else
  1308. *tmpStr='0';
  1309. tmpStr++;
  1310. byte<<=1;
  1311. }
  1312. valPtr++;
  1313. valLen--;
  1314. }
  1315. *tmpStr++ ='\'';
  1316. *tmpStr++ ='B';
  1317. *tmpStr=0;
  1318. return LDAP_SUCCESS;
  1319. }
  1320. /* Convert a structured DN from an X.509 certificate into an LDAPV3 DN.
  1321. * x509_name must be raw DER. If func is non-NULL, the
  1322. * constructed DN will use numeric OIDs to identify attributeTypes,
  1323. * and the func() will be invoked to rewrite the DN with the given
  1324. * flags.
  1325. *
  1326. * Otherwise the DN will use shortNames from a hardcoded table.
  1327. */
  1328. int
  1329. ldap_X509dn2bv( void *x509_name, struct berval *bv, LDAPDN_rewrite_func *func,
  1330. unsigned flags )
  1331. {
  1332. LDAPDN newDN;
  1333. LDAPRDN newRDN;
  1334. LDAPAVA *newAVA, *baseAVA;
  1335. BerElementBuffer berbuf;
  1336. BerElement *ber = (BerElement *)&berbuf;
  1337. char oids[8192], *oidptr = oids, *oidbuf = NULL;
  1338. void *ptrs[2048];
  1339. char *dn_end, *rdn_end;
  1340. int i, navas, nrdns, rc = LDAP_SUCCESS;
  1341. size_t dnsize, oidrem = sizeof(oids), oidsize = 0;
  1342. int csize;
  1343. ber_tag_t tag;
  1344. ber_len_t len;
  1345. oid_name *oidname;
  1346. struct berval Oid, Val, oid2, *in = x509_name;
  1347. assert( bv != NULL );
  1348. bv->bv_len = 0;
  1349. bv->bv_val = NULL;
  1350. navas = 0;
  1351. nrdns = 0;
  1352. /* A DN is a SEQUENCE of RDNs. An RDN is a SET of AVAs.
  1353. * An AVA is a SEQUENCE of attr and value.
  1354. * Count the number of AVAs and RDNs
  1355. */
  1356. ber_init2( ber, in, LBER_USE_DER );
  1357. tag = ber_peek_tag( ber, &len );
  1358. if ( tag != LBER_SEQUENCE )
  1359. return LDAP_DECODING_ERROR;
  1360. for ( tag = ber_first_element( ber, &len, &dn_end );
  1361. tag == LBER_SET;
  1362. tag = ber_next_element( ber, &len, dn_end )) {
  1363. nrdns++;
  1364. for ( tag = ber_first_element( ber, &len, &rdn_end );
  1365. tag == LBER_SEQUENCE;
  1366. tag = ber_next_element( ber, &len, rdn_end )) {
  1367. if ( rdn_end > dn_end )
  1368. return LDAP_DECODING_ERROR;
  1369. tag = ber_skip_tag( ber, &len );
  1370. ber_skip_data( ber, len );
  1371. navas++;
  1372. }
  1373. }
  1374. /* Rewind and prepare to extract */
  1375. ber_rewind( ber );
  1376. tag = ber_first_element( ber, &len, &dn_end );
  1377. if ( tag != LBER_SET )
  1378. return LDAP_DECODING_ERROR;
  1379. /* Allocate the DN/RDN/AVA stuff as a single block */
  1380. dnsize = sizeof(LDAPRDN) * (nrdns+1);
  1381. dnsize += sizeof(LDAPAVA *) * (navas+nrdns);
  1382. dnsize += sizeof(LDAPAVA) * navas;
  1383. if (dnsize > sizeof(ptrs)) {
  1384. newDN = (LDAPDN)LDAP_MALLOC( dnsize );
  1385. if ( newDN == NULL )
  1386. return LDAP_NO_MEMORY;
  1387. } else {
  1388. newDN = (LDAPDN)(char *)ptrs;
  1389. }
  1390. newDN[nrdns] = NULL;
  1391. newRDN = (LDAPRDN)(newDN + nrdns+1);
  1392. newAVA = (LDAPAVA *)(newRDN + navas + nrdns);
  1393. baseAVA = newAVA;
  1394. for ( i = nrdns - 1; i >= 0; i-- ) {
  1395. newDN[i] = newRDN;
  1396. for ( tag = ber_first_element( ber, &len, &rdn_end );
  1397. tag == LBER_SEQUENCE;
  1398. tag = ber_next_element( ber, &len, rdn_end )) {
  1399. *newRDN++ = newAVA;
  1400. tag = ber_skip_tag( ber, &len );
  1401. tag = ber_get_stringbv( ber, &Oid, LBER_BV_NOTERM );
  1402. if ( tag != LBER_TAG_OID ) {
  1403. rc = LDAP_DECODING_ERROR;
  1404. goto nomem;
  1405. }
  1406. oid2.bv_val = oidptr;
  1407. oid2.bv_len = oidrem;
  1408. if ( ber_decode_oid( &Oid, &oid2 ) < 0 ) {
  1409. rc = LDAP_DECODING_ERROR;
  1410. goto nomem;
  1411. }
  1412. oidname = find_oid( &oid2 );
  1413. if ( !oidname ) {
  1414. newAVA->la_attr = oid2;
  1415. oidptr += oid2.bv_len + 1;
  1416. oidrem -= oid2.bv_len + 1;
  1417. /* Running out of OID buffer space? */
  1418. if (oidrem < 128) {
  1419. if ( oidsize == 0 ) {
  1420. oidsize = sizeof(oids) * 2;
  1421. oidrem = oidsize;
  1422. oidbuf = LDAP_MALLOC( oidsize );
  1423. if ( oidbuf == NULL ) goto nomem;
  1424. oidptr = oidbuf;
  1425. } else {
  1426. char *old = oidbuf;
  1427. oidbuf = LDAP_REALLOC( oidbuf, oidsize*2 );
  1428. if ( oidbuf == NULL ) goto nomem;
  1429. /* Buffer moved! Fix AVA pointers */
  1430. if ( old != oidbuf ) {
  1431. LDAPAVA *a;
  1432. long dif = oidbuf - old;
  1433. for (a=baseAVA; a<=newAVA; a++){
  1434. if (a->la_attr.bv_val >= old &&
  1435. a->la_attr.bv_val <= (old + oidsize))
  1436. a->la_attr.bv_val += dif;
  1437. }
  1438. }
  1439. oidptr = oidbuf + oidsize - oidrem;
  1440. oidrem += oidsize;
  1441. oidsize *= 2;
  1442. }
  1443. }
  1444. } else {
  1445. if ( func ) {
  1446. newAVA->la_attr = oidname->oid;
  1447. } else {
  1448. newAVA->la_attr = oidname->name;
  1449. }
  1450. }
  1451. newAVA->la_private = NULL;
  1452. newAVA->la_flags = LDAP_AVA_STRING;
  1453. tag = ber_get_stringbv( ber, &Val, LBER_BV_NOTERM );
  1454. switch(tag) {
  1455. case LBER_TAG_UNIVERSAL:
  1456. /* This uses 32-bit ISO 10646-1 */
  1457. csize = 4; goto to_utf8;
  1458. case LBER_TAG_BMP:
  1459. /* This uses 16-bit ISO 10646-1 */
  1460. csize = 2; goto to_utf8;
  1461. case LBER_TAG_TELETEX:
  1462. /* This uses 8-bit, assume ISO 8859-1 */
  1463. csize = 1;
  1464. to_utf8: rc = ldap_ucs_to_utf8s( &Val, csize, &newAVA->la_value );
  1465. newAVA->la_flags |= LDAP_AVA_NONPRINTABLE;
  1466. allocd:
  1467. newAVA->la_flags |= LDAP_AVA_FREE_VALUE;
  1468. if (rc != LDAP_SUCCESS) goto nomem;
  1469. break;
  1470. case LBER_TAG_UTF8:
  1471. newAVA->la_flags |= LDAP_AVA_NONPRINTABLE;
  1472. /* This is already in UTF-8 encoding */
  1473. case LBER_TAG_IA5:
  1474. case LBER_TAG_PRINTABLE:
  1475. /* These are always 7-bit strings */
  1476. newAVA->la_value = Val;
  1477. break;
  1478. case LBER_BITSTRING:
  1479. /* X.690 bitString value converted to RFC4517 Bit String */
  1480. rc = der_to_ldap_BitString( &Val, &newAVA->la_value );
  1481. goto allocd;
  1482. case LBER_DEFAULT:
  1483. /* decode error */
  1484. rc = LDAP_DECODING_ERROR;
  1485. goto nomem;
  1486. default:
  1487. /* Not a string type at all */
  1488. newAVA->la_flags = 0;
  1489. newAVA->la_value = Val;
  1490. break;
  1491. }
  1492. newAVA++;
  1493. }
  1494. *newRDN++ = NULL;
  1495. tag = ber_next_element( ber, &len, dn_end );
  1496. }
  1497. if ( func ) {
  1498. rc = func( newDN, flags, NULL );
  1499. if ( rc != LDAP_SUCCESS )
  1500. goto nomem;
  1501. }
  1502. rc = ldap_dn2bv_x( newDN, bv, LDAP_DN_FORMAT_LDAPV3, NULL );
  1503. nomem:
  1504. for (;baseAVA < newAVA; baseAVA++) {
  1505. if (baseAVA->la_flags & LDAP_AVA_FREE_ATTR)
  1506. LDAP_FREE( baseAVA->la_attr.bv_val );
  1507. if (baseAVA->la_flags & LDAP_AVA_FREE_VALUE)
  1508. LDAP_FREE( baseAVA->la_value.bv_val );
  1509. }
  1510. if ( oidsize != 0 )
  1511. LDAP_FREE( oidbuf );
  1512. if ( newDN != (LDAPDN)(char *) ptrs )
  1513. LDAP_FREE( newDN );
  1514. return rc;
  1515. }