tls_g.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  1. /* tls_g.c - Handle tls/ssl using GNUTLS. */
  2. /* $OpenLDAP$ */
  3. /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  4. *
  5. * Copyright 2008-2024 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: GNUTLS support written by Howard Chu and
  17. * Emily Backes; sponsored by The Written Word (thewrittenword.com)
  18. * and Stanford University (stanford.edu).
  19. */
  20. #include "portable.h"
  21. #ifdef HAVE_GNUTLS
  22. #include "ldap_config.h"
  23. #include <stdio.h>
  24. #include <ac/stdlib.h>
  25. #include <ac/errno.h>
  26. #include <ac/socket.h>
  27. #include <ac/string.h>
  28. #include <ac/ctype.h>
  29. #include <ac/time.h>
  30. #include <ac/unistd.h>
  31. #include <ac/param.h>
  32. #include <ac/dirent.h>
  33. #include <sys/stat.h>
  34. #include <fcntl.h>
  35. #include "ldap-int.h"
  36. #include "ldap-tls.h"
  37. #error #include <gnutls/gnutls.h>
  38. #error #include <gnutls/x509.h>
  39. #error #include <gnutls/abstract.h>
  40. #error #include <gnutls/crypto.h>
  41. typedef struct tlsg_ctx {
  42. gnutls_certificate_credentials_t cred;
  43. gnutls_dh_params_t dh_params;
  44. unsigned long verify_depth;
  45. int refcount;
  46. int reqcert;
  47. gnutls_priority_t prios;
  48. #ifdef LDAP_R_COMPILE
  49. ldap_pvt_thread_mutex_t ref_mutex;
  50. #endif
  51. } tlsg_ctx;
  52. typedef struct tlsg_session {
  53. gnutls_session_t session;
  54. tlsg_ctx *ctx;
  55. struct berval peer_der_dn;
  56. } tlsg_session;
  57. static int tlsg_parse_ciphers( tlsg_ctx *ctx, char *suites );
  58. static int tlsg_cert_verify( tlsg_session *s );
  59. #ifdef LDAP_R_COMPILE
  60. static void
  61. tlsg_thr_init( void )
  62. {
  63. /* do nothing */
  64. }
  65. #endif /* LDAP_R_COMPILE */
  66. /*
  67. * Initialize TLS subsystem. Should be called only once.
  68. */
  69. static int
  70. tlsg_init( void )
  71. {
  72. gnutls_global_init();
  73. return 0;
  74. }
  75. /*
  76. * Tear down the TLS subsystem. Should only be called once.
  77. */
  78. static void
  79. tlsg_destroy( void )
  80. {
  81. gnutls_global_deinit();
  82. }
  83. static tls_ctx *
  84. tlsg_ctx_new ( struct ldapoptions *lo )
  85. {
  86. tlsg_ctx *ctx;
  87. ctx = ber_memcalloc ( 1, sizeof (*ctx) );
  88. if ( ctx ) {
  89. if ( gnutls_certificate_allocate_credentials( &ctx->cred )) {
  90. ber_memfree( ctx );
  91. return NULL;
  92. }
  93. ctx->refcount = 1;
  94. gnutls_priority_init( &ctx->prios, "NORMAL", NULL );
  95. #ifdef LDAP_R_COMPILE
  96. ldap_pvt_thread_mutex_init( &ctx->ref_mutex );
  97. #endif
  98. }
  99. return (tls_ctx *)ctx;
  100. }
  101. static void
  102. tlsg_ctx_ref( tls_ctx *ctx )
  103. {
  104. tlsg_ctx *c = (tlsg_ctx *)ctx;
  105. LDAP_MUTEX_LOCK( &c->ref_mutex );
  106. c->refcount++;
  107. LDAP_MUTEX_UNLOCK( &c->ref_mutex );
  108. }
  109. static void
  110. tlsg_ctx_free ( tls_ctx *ctx )
  111. {
  112. tlsg_ctx *c = (tlsg_ctx *)ctx;
  113. int refcount;
  114. if ( !c ) return;
  115. LDAP_MUTEX_LOCK( &c->ref_mutex );
  116. refcount = --c->refcount;
  117. LDAP_MUTEX_UNLOCK( &c->ref_mutex );
  118. if ( refcount )
  119. return;
  120. gnutls_priority_deinit( c->prios );
  121. gnutls_certificate_free_credentials( c->cred );
  122. if ( c->dh_params )
  123. gnutls_dh_params_deinit( c->dh_params );
  124. ber_memfree ( c );
  125. }
  126. static int
  127. tlsg_getfile( const char *path, gnutls_datum_t *buf )
  128. {
  129. int rc = -1, fd;
  130. struct stat st;
  131. char ebuf[128];
  132. fd = open( path, O_RDONLY );
  133. if ( fd < 0 ) {
  134. Debug2( LDAP_DEBUG_ANY,
  135. "TLS: opening `%s' failed: %s\n",
  136. path,
  137. AC_STRERROR_R( errno, ebuf, sizeof ebuf ));
  138. return -1;
  139. }
  140. if ( fstat( fd, &st ) == 0 ) {
  141. buf->size = st.st_size;
  142. buf->data = LDAP_MALLOC( st.st_size + 1 );
  143. if ( buf->data ) {
  144. rc = read( fd, buf->data, st.st_size );
  145. close( fd );
  146. if ( rc < st.st_size )
  147. rc = -1;
  148. else
  149. rc = 0;
  150. }
  151. }
  152. return rc;
  153. }
  154. /* This is the GnuTLS default */
  155. #define VERIFY_DEPTH 6
  156. /*
  157. * initialize a new TLS context
  158. */
  159. static int
  160. tlsg_ctx_init( struct ldapoptions *lo, struct ldaptls *lt, int is_server, char *errmsg )
  161. {
  162. tlsg_ctx *ctx = lo->ldo_tls_ctx;
  163. int rc;
  164. if ( lo->ldo_tls_ciphersuite &&
  165. tlsg_parse_ciphers( ctx, lt->lt_ciphersuite )) {
  166. Debug1( LDAP_DEBUG_ANY,
  167. "TLS: could not set cipher list %s.\n",
  168. lo->ldo_tls_ciphersuite );
  169. return -1;
  170. }
  171. if (lo->ldo_tls_cacertdir != NULL) {
  172. char **dirs = ldap_str2charray( lt->lt_cacertdir, CERTPATHSEP );
  173. int i;
  174. for ( i=0; dirs[i]; i++ ) {
  175. rc = gnutls_certificate_set_x509_trust_dir(
  176. ctx->cred,
  177. dirs[i],
  178. GNUTLS_X509_FMT_PEM );
  179. if ( rc > 0 ) {
  180. Debug2( LDAP_DEBUG_TRACE,
  181. "TLS: loaded %d CA certificates from directory `%s'.\n",
  182. rc, dirs[i] );
  183. } else {
  184. Debug1( LDAP_DEBUG_ANY,
  185. "TLS: warning: no certificate found in CA certificate directory `%s'.\n",
  186. dirs[i] );
  187. /* only warn, no return */
  188. strncpy( errmsg, gnutls_strerror( rc ), ERRBUFSIZE );
  189. }
  190. }
  191. ldap_charray_free( dirs );
  192. }
  193. if (lo->ldo_tls_cacertfile != NULL) {
  194. rc = gnutls_certificate_set_x509_trust_file(
  195. ctx->cred,
  196. lt->lt_cacertfile,
  197. GNUTLS_X509_FMT_PEM );
  198. if ( rc < 0 ) {
  199. Debug3( LDAP_DEBUG_ANY,
  200. "TLS: could not use CA certificate file `%s': %s (%d)\n",
  201. lo->ldo_tls_cacertfile,
  202. gnutls_strerror( rc ),
  203. rc );
  204. return -1;
  205. } else if ( rc == 0 ) {
  206. Debug1( LDAP_DEBUG_ANY,
  207. "TLS: warning: no certificate loaded from CA certificate file `%s'.\n",
  208. lo->ldo_tls_cacertfile );
  209. /* only warn, no return */
  210. strncpy( errmsg, gnutls_strerror( rc ), ERRBUFSIZE );
  211. }
  212. }
  213. if (lo->ldo_tls_cacert.bv_val != NULL ) {
  214. gnutls_datum_t buf;
  215. buf.data = (unsigned char *)lo->ldo_tls_cacert.bv_val;
  216. buf.size = lo->ldo_tls_cacert.bv_len;
  217. rc = gnutls_certificate_set_x509_trust_mem(
  218. ctx->cred,
  219. &buf,
  220. GNUTLS_X509_FMT_DER );
  221. if ( rc < 0 ) {
  222. Debug2( LDAP_DEBUG_ANY,
  223. "TLS: could not use CA certificate: %s (%d)\n",
  224. gnutls_strerror( rc ),
  225. rc );
  226. strncpy( errmsg, gnutls_strerror( rc ), ERRBUFSIZE );
  227. return -1;
  228. }
  229. }
  230. if (( lo->ldo_tls_certfile && lo->ldo_tls_keyfile ) ||
  231. ( lo->ldo_tls_cert.bv_val && lo->ldo_tls_key.bv_val )) {
  232. gnutls_x509_privkey_t key;
  233. gnutls_datum_t buf;
  234. gnutls_x509_crt_t certs[VERIFY_DEPTH];
  235. unsigned int max = VERIFY_DEPTH;
  236. rc = gnutls_x509_privkey_init( &key );
  237. if ( rc ) {
  238. strncpy( errmsg, gnutls_strerror( rc ), ERRBUFSIZE );
  239. return -1;
  240. }
  241. /* OpenSSL builds the cert chain for us, but GnuTLS
  242. * expects it to be present in the certfile. If it's
  243. * not, we have to build it ourselves. So we have to
  244. * do some special checks here...
  245. */
  246. if ( lo->ldo_tls_key.bv_val ) {
  247. buf.data = (unsigned char *)lo->ldo_tls_key.bv_val;
  248. buf.size = lo->ldo_tls_key.bv_len;
  249. rc = gnutls_x509_privkey_import( key, &buf,
  250. GNUTLS_X509_FMT_DER );
  251. } else {
  252. rc = tlsg_getfile( lt->lt_keyfile, &buf );
  253. if ( rc ) {
  254. Debug1( LDAP_DEBUG_ANY,
  255. "TLS: could not use private key file `%s`.\n",
  256. lt->lt_keyfile);
  257. return -1;
  258. }
  259. rc = gnutls_x509_privkey_import( key, &buf,
  260. GNUTLS_X509_FMT_PEM );
  261. LDAP_FREE( buf.data );
  262. }
  263. if ( rc < 0 ) {
  264. Debug2( LDAP_DEBUG_ANY,
  265. "TLS: could not use private key: %s (%d)\n",
  266. gnutls_strerror( rc ),
  267. rc );
  268. strncpy( errmsg, gnutls_strerror( rc ), ERRBUFSIZE );
  269. return rc;
  270. }
  271. if ( lo->ldo_tls_cert.bv_val ) {
  272. buf.data = (unsigned char *)lo->ldo_tls_cert.bv_val;
  273. buf.size = lo->ldo_tls_cert.bv_len;
  274. rc = gnutls_x509_crt_list_import( certs, &max, &buf,
  275. GNUTLS_X509_FMT_DER, 0 );
  276. } else {
  277. rc = tlsg_getfile( lt->lt_certfile, &buf );
  278. if ( rc ) {
  279. Debug1( LDAP_DEBUG_ANY,
  280. "TLS: could not use certificate file `%s`.\n",
  281. lt->lt_certfile);
  282. return -1;
  283. }
  284. rc = gnutls_x509_crt_list_import( certs, &max, &buf,
  285. GNUTLS_X509_FMT_PEM, 0 );
  286. LDAP_FREE( buf.data );
  287. }
  288. if ( rc < 0 ) {
  289. Debug2( LDAP_DEBUG_ANY,
  290. "TLS: could not use certificate: %s (%d)\n",
  291. gnutls_strerror( rc ),
  292. rc );
  293. strncpy( errmsg, gnutls_strerror( rc ), ERRBUFSIZE );
  294. return rc;
  295. }
  296. /* If there's only one cert and it's not self-signed,
  297. * then we have to build the cert chain.
  298. */
  299. if ( max == 1 && !gnutls_x509_crt_check_issuer( certs[0], certs[0] )) {
  300. unsigned int i;
  301. for ( i = 1; i<VERIFY_DEPTH; i++ ) {
  302. if ( gnutls_certificate_get_issuer( ctx->cred, certs[i-1], &certs[i], 0 ))
  303. break;
  304. max++;
  305. /* If this CA is self-signed, we're done */
  306. if ( gnutls_x509_crt_check_issuer( certs[i], certs[i] ))
  307. break;
  308. }
  309. }
  310. rc = gnutls_certificate_set_x509_key( ctx->cred, certs, max, key );
  311. if ( rc ) {
  312. Debug2( LDAP_DEBUG_ANY,
  313. "TLS: could not use certificate with key: %s (%d)\n",
  314. gnutls_strerror( rc ),
  315. rc );
  316. strncpy( errmsg, gnutls_strerror( rc ), ERRBUFSIZE );
  317. return -1;
  318. }
  319. } else if (( lo->ldo_tls_certfile || lo->ldo_tls_keyfile )) {
  320. Debug0( LDAP_DEBUG_ANY,
  321. "TLS: only one of certfile and keyfile specified\n" );
  322. return -1;
  323. } else if (( lo->ldo_tls_cert.bv_val || lo->ldo_tls_key.bv_val )) {
  324. Debug0( LDAP_DEBUG_ANY,
  325. "TLS: only one of cert and key specified\n" );
  326. return -1;
  327. }
  328. if ( lo->ldo_tls_crlfile ) {
  329. rc = gnutls_certificate_set_x509_crl_file(
  330. ctx->cred,
  331. lt->lt_crlfile,
  332. GNUTLS_X509_FMT_PEM );
  333. if ( rc < 0 ) {
  334. strncpy( errmsg, gnutls_strerror( rc ), ERRBUFSIZE );
  335. return -1;
  336. }
  337. rc = 0;
  338. }
  339. /* FIXME: ITS#5992 - this should be configurable,
  340. * and V1 CA certs should be phased out ASAP.
  341. */
  342. gnutls_certificate_set_verify_flags( ctx->cred,
  343. GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT );
  344. if ( is_server && lo->ldo_tls_dhfile ) {
  345. gnutls_datum_t buf;
  346. rc = tlsg_getfile( lo->ldo_tls_dhfile, &buf );
  347. if ( rc ) return -1;
  348. rc = gnutls_dh_params_init( &ctx->dh_params );
  349. if ( rc == 0 )
  350. rc = gnutls_dh_params_import_pkcs3( ctx->dh_params, &buf,
  351. GNUTLS_X509_FMT_PEM );
  352. LDAP_FREE( buf.data );
  353. if ( rc ) {
  354. strncpy( errmsg, gnutls_strerror( rc ), ERRBUFSIZE );
  355. return -1;
  356. }
  357. gnutls_certificate_set_dh_params( ctx->cred, ctx->dh_params );
  358. }
  359. ctx->reqcert = lo->ldo_tls_require_cert;
  360. return 0;
  361. }
  362. static tls_session *
  363. tlsg_session_new ( tls_ctx * ctx, int is_server )
  364. {
  365. tlsg_ctx *c = (tlsg_ctx *)ctx;
  366. tlsg_session *session;
  367. session = ber_memcalloc ( 1, sizeof (*session) );
  368. if ( !session )
  369. return NULL;
  370. session->ctx = c;
  371. gnutls_init( &session->session, is_server ? GNUTLS_SERVER : GNUTLS_CLIENT );
  372. gnutls_priority_set( session->session, c->prios );
  373. if ( c->cred )
  374. gnutls_credentials_set( session->session, GNUTLS_CRD_CERTIFICATE, c->cred );
  375. if ( is_server ) {
  376. int flag = 0;
  377. if ( c->reqcert ) {
  378. flag = GNUTLS_CERT_REQUEST;
  379. if ( c->reqcert == LDAP_OPT_X_TLS_DEMAND ||
  380. c->reqcert == LDAP_OPT_X_TLS_HARD )
  381. flag = GNUTLS_CERT_REQUIRE;
  382. gnutls_certificate_server_set_request( session->session, flag );
  383. }
  384. }
  385. return (tls_session *)session;
  386. }
  387. static int
  388. tlsg_session_accept( tls_session *session )
  389. {
  390. tlsg_session *s = (tlsg_session *)session;
  391. int rc;
  392. rc = gnutls_handshake( s->session );
  393. if ( rc == 0 && s->ctx->reqcert != LDAP_OPT_X_TLS_NEVER ) {
  394. const gnutls_datum_t *peer_cert_list;
  395. unsigned int list_size;
  396. peer_cert_list = gnutls_certificate_get_peers( s->session,
  397. &list_size );
  398. if ( !peer_cert_list && s->ctx->reqcert == LDAP_OPT_X_TLS_TRY )
  399. rc = 0;
  400. else {
  401. rc = tlsg_cert_verify( s );
  402. if ( rc && s->ctx->reqcert == LDAP_OPT_X_TLS_ALLOW )
  403. rc = 0;
  404. }
  405. }
  406. return rc;
  407. }
  408. static int
  409. tlsg_session_connect( LDAP *ld, tls_session *session, const char *name_in )
  410. {
  411. tlsg_session *s = (tlsg_session *)session;
  412. int rc;
  413. if ( name_in ) {
  414. rc = gnutls_server_name_set( s->session, GNUTLS_NAME_DNS, name_in, strlen(name_in) );
  415. if ( rc != GNUTLS_E_SUCCESS ) {
  416. return rc;
  417. }
  418. }
  419. return tlsg_session_accept( session);
  420. }
  421. static int
  422. tlsg_session_upflags( Sockbuf *sb, tls_session *session, int rc )
  423. {
  424. tlsg_session *s = (tlsg_session *)session;
  425. if ( rc != GNUTLS_E_INTERRUPTED && rc != GNUTLS_E_AGAIN )
  426. return 0;
  427. switch (gnutls_record_get_direction (s->session)) {
  428. case 0:
  429. sb->sb_trans_needs_read = 1;
  430. return 1;
  431. case 1:
  432. sb->sb_trans_needs_write = 1;
  433. return 1;
  434. }
  435. return 0;
  436. }
  437. static char *
  438. tlsg_session_errmsg( tls_session *sess, int rc, char *buf, size_t len )
  439. {
  440. return (char *)gnutls_strerror( rc );
  441. }
  442. static void
  443. tlsg_x509_cert_dn( struct berval *cert, struct berval *dn, int get_subject )
  444. {
  445. BerElementBuffer berbuf;
  446. BerElement *ber = (BerElement *)&berbuf;
  447. ber_tag_t tag;
  448. ber_len_t len;
  449. ber_int_t i;
  450. ber_init2( ber, cert, LBER_USE_DER );
  451. tag = ber_skip_tag( ber, &len ); /* Sequence */
  452. tag = ber_skip_tag( ber, &len ); /* Sequence */
  453. tag = ber_peek_tag( ber, &len ); /* Context + Constructed (version) */
  454. if ( tag == 0xa0 ) { /* Version is optional */
  455. tag = ber_skip_tag( ber, &len );
  456. tag = ber_get_int( ber, &i ); /* Int: Version */
  457. }
  458. tag = ber_skip_tag( ber, &len ); /* Int: Serial (can be longer than ber_int_t) */
  459. ber_skip_data( ber, len );
  460. tag = ber_skip_tag( ber, &len ); /* Sequence: Signature */
  461. ber_skip_data( ber, len );
  462. if ( !get_subject ) {
  463. tag = ber_peek_tag( ber, &len ); /* Sequence: Issuer DN */
  464. } else {
  465. tag = ber_skip_tag( ber, &len );
  466. ber_skip_data( ber, len );
  467. tag = ber_skip_tag( ber, &len ); /* Sequence: Validity */
  468. ber_skip_data( ber, len );
  469. tag = ber_peek_tag( ber, &len ); /* Sequence: Subject DN */
  470. }
  471. len = ber_ptrlen( ber );
  472. dn->bv_val = cert->bv_val + len;
  473. dn->bv_len = cert->bv_len - len;
  474. }
  475. static int
  476. tlsg_session_my_dn( tls_session *session, struct berval *der_dn )
  477. {
  478. tlsg_session *s = (tlsg_session *)session;
  479. const gnutls_datum_t *x;
  480. struct berval bv;
  481. x = gnutls_certificate_get_ours( s->session );
  482. if (!x) return LDAP_INVALID_CREDENTIALS;
  483. bv.bv_val = (char *) x->data;
  484. bv.bv_len = x->size;
  485. tlsg_x509_cert_dn( &bv, der_dn, 1 );
  486. return 0;
  487. }
  488. static int
  489. tlsg_session_peer_dn( tls_session *session, struct berval *der_dn )
  490. {
  491. tlsg_session *s = (tlsg_session *)session;
  492. if ( !s->peer_der_dn.bv_val ) {
  493. const gnutls_datum_t *peer_cert_list;
  494. unsigned int list_size;
  495. struct berval bv;
  496. peer_cert_list = gnutls_certificate_get_peers( s->session,
  497. &list_size );
  498. if ( !peer_cert_list ) return LDAP_INVALID_CREDENTIALS;
  499. bv.bv_len = peer_cert_list->size;
  500. bv.bv_val = (char *) peer_cert_list->data;
  501. tlsg_x509_cert_dn( &bv, &s->peer_der_dn, 1 );
  502. }
  503. *der_dn = s->peer_der_dn;
  504. return 0;
  505. }
  506. /* what kind of hostname were we given? */
  507. #define IS_DNS 0
  508. #define IS_IP4 1
  509. #define IS_IP6 2
  510. #define CN_OID "2.5.4.3"
  511. static int
  512. tlsg_session_chkhost( LDAP *ld, tls_session *session, const char *name_in )
  513. {
  514. tlsg_session *s = (tlsg_session *)session;
  515. int i, ret;
  516. int chkSAN = ld->ld_options.ldo_tls_require_san, gotSAN = 0;
  517. const gnutls_datum_t *peer_cert_list;
  518. unsigned int list_size;
  519. char altname[NI_MAXHOST];
  520. size_t altnamesize;
  521. gnutls_x509_crt_t cert;
  522. const char *name;
  523. char *ptr;
  524. char *domain = NULL;
  525. #ifdef LDAP_PF_INET6
  526. struct in6_addr addr;
  527. #else
  528. struct in_addr addr;
  529. #endif
  530. int len1 = 0, len2 = 0;
  531. int ntype = IS_DNS;
  532. if( ldap_int_hostname &&
  533. ( !name_in || !strcasecmp( name_in, "localhost" ) ) )
  534. {
  535. name = ldap_int_hostname;
  536. } else {
  537. name = name_in;
  538. }
  539. peer_cert_list = gnutls_certificate_get_peers( s->session,
  540. &list_size );
  541. if ( !peer_cert_list ) {
  542. Debug0( LDAP_DEBUG_ANY,
  543. "TLS: unable to get peer certificate.\n" );
  544. /* If this was a fatal condition, things would have
  545. * aborted long before now.
  546. */
  547. return LDAP_SUCCESS;
  548. }
  549. ret = gnutls_x509_crt_init( &cert );
  550. if ( ret < 0 )
  551. return LDAP_LOCAL_ERROR;
  552. ret = gnutls_x509_crt_import( cert, peer_cert_list, GNUTLS_X509_FMT_DER );
  553. if ( ret ) {
  554. gnutls_x509_crt_deinit( cert );
  555. return LDAP_LOCAL_ERROR;
  556. }
  557. #ifdef LDAP_PF_INET6
  558. if (inet_pton(AF_INET6, name, &addr)) {
  559. ntype = IS_IP6;
  560. } else
  561. #endif
  562. if ((ptr = strrchr(name, '.')) && isdigit((unsigned char)ptr[1])) {
  563. if (inet_aton(name, (struct in_addr *)&addr)) ntype = IS_IP4;
  564. }
  565. if (ntype == IS_DNS) {
  566. len1 = strlen(name);
  567. domain = strchr(name, '.');
  568. if (domain) {
  569. len2 = len1 - (domain-name);
  570. }
  571. }
  572. if (chkSAN) {
  573. for ( i=0, ret=0; ret >= 0; i++ ) {
  574. altnamesize = sizeof(altname);
  575. ret = gnutls_x509_crt_get_subject_alt_name( cert, i,
  576. altname, &altnamesize, NULL );
  577. if ( ret < 0 ) break;
  578. gotSAN = 1;
  579. /* ignore empty */
  580. if ( altnamesize == 0 ) continue;
  581. if ( ret == GNUTLS_SAN_DNSNAME ) {
  582. if (ntype != IS_DNS) continue;
  583. /* Is this an exact match? */
  584. if ((len1 == altnamesize) && !strncasecmp(name, altname, len1)) {
  585. break;
  586. }
  587. /* Is this a wildcard match? */
  588. if (domain && (altname[0] == '*') && (altname[1] == '.') &&
  589. (len2 == altnamesize-1) && !strncasecmp(domain, &altname[1], len2))
  590. {
  591. break;
  592. }
  593. } else if ( ret == GNUTLS_SAN_IPADDRESS ) {
  594. if (ntype == IS_DNS) continue;
  595. #ifdef LDAP_PF_INET6
  596. if (ntype == IS_IP6 && altnamesize != sizeof(struct in6_addr)) {
  597. continue;
  598. } else
  599. #endif
  600. if (ntype == IS_IP4 && altnamesize != sizeof(struct in_addr)) {
  601. continue;
  602. }
  603. if (!memcmp(altname, &addr, altnamesize)) {
  604. break;
  605. }
  606. }
  607. }
  608. if ( ret >= 0 ) {
  609. ret = LDAP_SUCCESS;
  610. }
  611. }
  612. if (ret != LDAP_SUCCESS && chkSAN) {
  613. switch(chkSAN) {
  614. case LDAP_OPT_X_TLS_DEMAND:
  615. case LDAP_OPT_X_TLS_HARD:
  616. if (!gotSAN) {
  617. Debug0( LDAP_DEBUG_ANY,
  618. "TLS: unable to get subjectAltName from peer certificate.\n" );
  619. ret = LDAP_CONNECT_ERROR;
  620. if ( ld->ld_error ) {
  621. LDAP_FREE( ld->ld_error );
  622. }
  623. ld->ld_error = LDAP_STRDUP(
  624. _("TLS: unable to get subjectAltName from peer certificate"));
  625. goto done;
  626. }
  627. /* FALLTHRU */
  628. case LDAP_OPT_X_TLS_TRY:
  629. if (gotSAN) {
  630. Debug1( LDAP_DEBUG_ANY, "TLS: hostname (%s) does not match "
  631. "subjectAltName in certificate.\n",
  632. name );
  633. ret = LDAP_CONNECT_ERROR;
  634. if ( ld->ld_error ) {
  635. LDAP_FREE( ld->ld_error );
  636. }
  637. ld->ld_error = LDAP_STRDUP(
  638. _("TLS: hostname does not match subjectAltName in peer certificate"));
  639. goto done;
  640. }
  641. break;
  642. case LDAP_OPT_X_TLS_ALLOW:
  643. break;
  644. }
  645. }
  646. if ( ret != LDAP_SUCCESS ){
  647. /* find the last CN */
  648. i=0;
  649. do {
  650. altnamesize = 0;
  651. ret = gnutls_x509_crt_get_dn_by_oid( cert, CN_OID,
  652. i, 1, altname, &altnamesize );
  653. if ( ret == GNUTLS_E_SHORT_MEMORY_BUFFER )
  654. i++;
  655. else
  656. break;
  657. } while ( 1 );
  658. if ( i ) {
  659. altnamesize = sizeof(altname);
  660. ret = gnutls_x509_crt_get_dn_by_oid( cert, CN_OID,
  661. i-1, 0, altname, &altnamesize );
  662. }
  663. if ( ret < 0 ) {
  664. Debug0( LDAP_DEBUG_ANY,
  665. "TLS: unable to get common name from peer certificate.\n" );
  666. ret = LDAP_CONNECT_ERROR;
  667. if ( ld->ld_error ) {
  668. LDAP_FREE( ld->ld_error );
  669. }
  670. ld->ld_error = LDAP_STRDUP(
  671. _("TLS: unable to get CN from peer certificate"));
  672. } else {
  673. ret = LDAP_LOCAL_ERROR;
  674. if ( !len1 ) len1 = strlen( name );
  675. if ( len1 == altnamesize && strncasecmp(name, altname, altnamesize) == 0 ) {
  676. ret = LDAP_SUCCESS;
  677. } else if (( altname[0] == '*' ) && ( altname[1] == '.' )) {
  678. /* Is this a wildcard match? */
  679. if( domain &&
  680. (len2 == altnamesize-1) && !strncasecmp(domain, &altname[1], len2)) {
  681. ret = LDAP_SUCCESS;
  682. }
  683. }
  684. }
  685. if( ret == LDAP_LOCAL_ERROR ) {
  686. altname[altnamesize] = '\0';
  687. Debug2( LDAP_DEBUG_ANY, "TLS: hostname (%s) does not match "
  688. "common name in certificate (%s).\n",
  689. name, altname );
  690. ret = LDAP_CONNECT_ERROR;
  691. if ( ld->ld_error ) {
  692. LDAP_FREE( ld->ld_error );
  693. }
  694. ld->ld_error = LDAP_STRDUP(
  695. _("TLS: hostname does not match name in peer certificate"));
  696. }
  697. }
  698. done:
  699. gnutls_x509_crt_deinit( cert );
  700. return ret;
  701. }
  702. static int
  703. tlsg_session_strength( tls_session *session )
  704. {
  705. tlsg_session *s = (tlsg_session *)session;
  706. gnutls_cipher_algorithm_t c;
  707. c = gnutls_cipher_get( s->session );
  708. return gnutls_cipher_get_key_size( c ) * 8;
  709. }
  710. static int
  711. tlsg_session_unique( tls_session *sess, struct berval *buf, int is_server)
  712. {
  713. tlsg_session *s = (tlsg_session *)sess;
  714. gnutls_datum_t cb;
  715. int rc;
  716. rc = gnutls_session_channel_binding( s->session, GNUTLS_CB_TLS_UNIQUE, &cb );
  717. if ( rc == 0 ) {
  718. int len = cb.size;
  719. if ( len > buf->bv_len )
  720. len = buf->bv_len;
  721. buf->bv_len = len;
  722. memcpy( buf->bv_val, cb.data, len );
  723. return len;
  724. }
  725. return 0;
  726. }
  727. static int
  728. tlsg_session_endpoint( tls_session *sess, struct berval *buf, int is_server )
  729. {
  730. tlsg_session *s = (tlsg_session *)sess;
  731. const gnutls_datum_t *cert_data;
  732. gnutls_x509_crt_t server_cert;
  733. gnutls_digest_algorithm_t md;
  734. int sign_algo, md_len, rc;
  735. if ( is_server )
  736. cert_data = gnutls_certificate_get_ours( s->session );
  737. else
  738. cert_data = gnutls_certificate_get_peers( s->session, NULL );
  739. if ( cert_data == NULL )
  740. return 0;
  741. rc = gnutls_x509_crt_init( &server_cert );
  742. if ( rc != GNUTLS_E_SUCCESS )
  743. return 0;
  744. rc = gnutls_x509_crt_import( server_cert, cert_data, GNUTLS_X509_FMT_DER );
  745. if ( rc != GNUTLS_E_SUCCESS ) {
  746. gnutls_x509_crt_deinit( server_cert );
  747. return 0;
  748. }
  749. sign_algo = gnutls_x509_crt_get_signature_algorithm( server_cert );
  750. gnutls_x509_crt_deinit( server_cert );
  751. if ( sign_algo <= GNUTLS_SIGN_UNKNOWN )
  752. return 0;
  753. md = gnutls_sign_get_hash_algorithm( sign_algo );
  754. if ( md == GNUTLS_DIG_UNKNOWN )
  755. return 0;
  756. /* See RFC 5929 */
  757. switch (md) {
  758. case GNUTLS_DIG_NULL:
  759. case GNUTLS_DIG_MD2:
  760. case GNUTLS_DIG_MD5:
  761. case GNUTLS_DIG_SHA1:
  762. md = GNUTLS_DIG_SHA256;
  763. }
  764. md_len = gnutls_hash_get_len( md );
  765. if ( md_len == 0 || md_len > buf->bv_len )
  766. return 0;
  767. rc = gnutls_hash_fast( md, cert_data->data, cert_data->size, buf->bv_val );
  768. if ( rc != GNUTLS_E_SUCCESS )
  769. return 0;
  770. buf->bv_len = md_len;
  771. return md_len;
  772. }
  773. static const char *
  774. tlsg_session_version( tls_session *sess )
  775. {
  776. tlsg_session *s = (tlsg_session *)sess;
  777. return gnutls_protocol_get_name(gnutls_protocol_get_version( s->session ));
  778. }
  779. static const char *
  780. tlsg_session_cipher( tls_session *sess )
  781. {
  782. tlsg_session *s = (tlsg_session *)sess;
  783. return gnutls_cipher_get_name(gnutls_cipher_get( s->session ));
  784. }
  785. static int
  786. tlsg_session_peercert( tls_session *sess, struct berval *der )
  787. {
  788. tlsg_session *s = (tlsg_session *)sess;
  789. const gnutls_datum_t *peer_cert_list;
  790. unsigned int list_size;
  791. peer_cert_list = gnutls_certificate_get_peers( s->session, &list_size );
  792. if (!peer_cert_list)
  793. return -1;
  794. der->bv_len = peer_cert_list[0].size;
  795. der->bv_val = LDAP_MALLOC( der->bv_len );
  796. if (!der->bv_val)
  797. return -1;
  798. memcpy(der->bv_val, peer_cert_list[0].data, der->bv_len);
  799. return 0;
  800. }
  801. static int
  802. tlsg_session_pinning( LDAP *ld, tls_session *sess, char *hashalg, struct berval *hash )
  803. {
  804. tlsg_session *s = (tlsg_session *)sess;
  805. const gnutls_datum_t *cert_list;
  806. unsigned int cert_list_size = 0;
  807. gnutls_x509_crt_t crt;
  808. gnutls_pubkey_t pubkey;
  809. gnutls_datum_t key = {};
  810. gnutls_digest_algorithm_t alg;
  811. struct berval keyhash;
  812. size_t len;
  813. int rc = -1;
  814. if ( hashalg ) {
  815. alg = gnutls_digest_get_id( hashalg );
  816. if ( alg == GNUTLS_DIG_UNKNOWN ) {
  817. Debug1( LDAP_DEBUG_ANY, "tlsg_session_pinning: "
  818. "unknown hashing algorithm for GnuTLS: '%s'\n",
  819. hashalg );
  820. return rc;
  821. }
  822. }
  823. cert_list = gnutls_certificate_get_peers( s->session, &cert_list_size );
  824. if ( cert_list_size == 0 ) {
  825. return rc;
  826. }
  827. if ( gnutls_x509_crt_init( &crt ) < 0 ) {
  828. return rc;
  829. }
  830. if ( gnutls_x509_crt_import( crt, &cert_list[0], GNUTLS_X509_FMT_DER ) ) {
  831. goto done;
  832. }
  833. if ( gnutls_pubkey_init( &pubkey ) ) {
  834. goto done;
  835. }
  836. if ( gnutls_pubkey_import_x509( pubkey, crt, 0 ) < 0 ) {
  837. goto done;
  838. }
  839. gnutls_pubkey_export( pubkey, GNUTLS_X509_FMT_DER, key.data, &len );
  840. if ( len <= 0 ) {
  841. goto done;
  842. }
  843. key.data = LDAP_MALLOC( len );
  844. if ( !key.data ) {
  845. goto done;
  846. }
  847. key.size = len;
  848. if ( gnutls_pubkey_export( pubkey, GNUTLS_X509_FMT_DER,
  849. key.data, &len ) < 0 ) {
  850. goto done;
  851. }
  852. if ( hashalg ) {
  853. len = gnutls_hash_get_len( alg );
  854. keyhash.bv_val = LDAP_MALLOC( len );
  855. if ( !keyhash.bv_val || gnutls_fingerprint( alg, &key,
  856. keyhash.bv_val, &len ) < 0 ) {
  857. goto done;
  858. }
  859. keyhash.bv_len = len;
  860. } else {
  861. keyhash.bv_val = (char *)key.data;
  862. keyhash.bv_len = key.size;
  863. }
  864. if ( ber_bvcmp( hash, &keyhash ) ) {
  865. rc = LDAP_CONNECT_ERROR;
  866. Debug0( LDAP_DEBUG_ANY, "tlsg_session_pinning: "
  867. "public key hash does not match provided pin.\n" );
  868. if ( ld->ld_error ) {
  869. LDAP_FREE( ld->ld_error );
  870. }
  871. ld->ld_error = LDAP_STRDUP(
  872. _("TLS: public key hash does not match provided pin"));
  873. } else {
  874. rc = LDAP_SUCCESS;
  875. }
  876. done:
  877. if ( pubkey ) {
  878. gnutls_pubkey_deinit( pubkey );
  879. }
  880. if ( crt ) {
  881. gnutls_x509_crt_deinit( crt );
  882. }
  883. if ( keyhash.bv_val != (char *)key.data ) {
  884. LDAP_FREE( keyhash.bv_val );
  885. }
  886. if ( key.data ) {
  887. LDAP_FREE( key.data );
  888. }
  889. return rc;
  890. }
  891. /* suites is a string of colon-separated cipher suite names. */
  892. static int
  893. tlsg_parse_ciphers( tlsg_ctx *ctx, char *suites )
  894. {
  895. const char *err;
  896. int rc = gnutls_priority_init( &ctx->prios, suites, &err );
  897. if ( rc )
  898. ctx->prios = NULL;
  899. return rc;
  900. }
  901. /*
  902. * TLS support for LBER Sockbufs
  903. */
  904. struct tls_data {
  905. tlsg_session *session;
  906. Sockbuf_IO_Desc *sbiod;
  907. };
  908. static ssize_t
  909. tlsg_recv( gnutls_transport_ptr_t ptr, void *buf, size_t len )
  910. {
  911. struct tls_data *p;
  912. if ( buf == NULL || len <= 0 ) return 0;
  913. p = (struct tls_data *)ptr;
  914. if ( p == NULL || p->sbiod == NULL ) {
  915. return 0;
  916. }
  917. return LBER_SBIOD_READ_NEXT( p->sbiod, buf, len );
  918. }
  919. static ssize_t
  920. tlsg_send( gnutls_transport_ptr_t ptr, const void *buf, size_t len )
  921. {
  922. struct tls_data *p;
  923. if ( buf == NULL || len <= 0 ) return 0;
  924. p = (struct tls_data *)ptr;
  925. if ( p == NULL || p->sbiod == NULL ) {
  926. return 0;
  927. }
  928. return LBER_SBIOD_WRITE_NEXT( p->sbiod, (char *)buf, len );
  929. }
  930. static int
  931. tlsg_sb_setup( Sockbuf_IO_Desc *sbiod, void *arg )
  932. {
  933. struct tls_data *p;
  934. tlsg_session *session = arg;
  935. assert( sbiod != NULL );
  936. p = LBER_MALLOC( sizeof( *p ) );
  937. if ( p == NULL ) {
  938. return -1;
  939. }
  940. gnutls_transport_set_ptr( session->session, (gnutls_transport_ptr_t)p );
  941. gnutls_transport_set_pull_function( session->session, tlsg_recv );
  942. gnutls_transport_set_push_function( session->session, tlsg_send );
  943. p->session = session;
  944. p->sbiod = sbiod;
  945. sbiod->sbiod_pvt = p;
  946. return 0;
  947. }
  948. static int
  949. tlsg_sb_remove( Sockbuf_IO_Desc *sbiod )
  950. {
  951. struct tls_data *p;
  952. assert( sbiod != NULL );
  953. assert( sbiod->sbiod_pvt != NULL );
  954. p = (struct tls_data *)sbiod->sbiod_pvt;
  955. gnutls_deinit ( p->session->session );
  956. LBER_FREE( p->session );
  957. LBER_FREE( sbiod->sbiod_pvt );
  958. sbiod->sbiod_pvt = NULL;
  959. return 0;
  960. }
  961. static int
  962. tlsg_sb_close( Sockbuf_IO_Desc *sbiod )
  963. {
  964. struct tls_data *p;
  965. assert( sbiod != NULL );
  966. assert( sbiod->sbiod_pvt != NULL );
  967. p = (struct tls_data *)sbiod->sbiod_pvt;
  968. gnutls_bye ( p->session->session, GNUTLS_SHUT_WR );
  969. return 0;
  970. }
  971. static int
  972. tlsg_sb_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
  973. {
  974. struct tls_data *p;
  975. assert( sbiod != NULL );
  976. assert( sbiod->sbiod_pvt != NULL );
  977. p = (struct tls_data *)sbiod->sbiod_pvt;
  978. if ( opt == LBER_SB_OPT_GET_SSL ) {
  979. *((tlsg_session **)arg) = p->session;
  980. return 1;
  981. } else if ( opt == LBER_SB_OPT_DATA_READY ) {
  982. if( gnutls_record_check_pending( p->session->session ) > 0 ) {
  983. return 1;
  984. }
  985. }
  986. return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg );
  987. }
  988. static ber_slen_t
  989. tlsg_sb_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
  990. {
  991. struct tls_data *p;
  992. ber_slen_t ret;
  993. assert( sbiod != NULL );
  994. assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
  995. p = (struct tls_data *)sbiod->sbiod_pvt;
  996. ret = gnutls_record_recv ( p->session->session, buf, len );
  997. switch (ret) {
  998. case GNUTLS_E_INTERRUPTED:
  999. case GNUTLS_E_AGAIN:
  1000. sbiod->sbiod_sb->sb_trans_needs_read = 1;
  1001. sock_errset(EWOULDBLOCK);
  1002. ret = 0;
  1003. break;
  1004. case GNUTLS_E_REHANDSHAKE:
  1005. for ( ret = gnutls_handshake ( p->session->session );
  1006. ret == GNUTLS_E_INTERRUPTED || ret == GNUTLS_E_AGAIN;
  1007. ret = gnutls_handshake ( p->session->session ) );
  1008. sbiod->sbiod_sb->sb_trans_needs_read = 1;
  1009. ret = 0;
  1010. break;
  1011. default:
  1012. sbiod->sbiod_sb->sb_trans_needs_read = 0;
  1013. }
  1014. return ret;
  1015. }
  1016. static ber_slen_t
  1017. tlsg_sb_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
  1018. {
  1019. struct tls_data *p;
  1020. ber_slen_t ret;
  1021. assert( sbiod != NULL );
  1022. assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
  1023. p = (struct tls_data *)sbiod->sbiod_pvt;
  1024. ret = gnutls_record_send ( p->session->session, (char *)buf, len );
  1025. if ( ret == GNUTLS_E_INTERRUPTED || ret == GNUTLS_E_AGAIN ) {
  1026. sbiod->sbiod_sb->sb_trans_needs_write = 1;
  1027. sock_errset(EWOULDBLOCK);
  1028. ret = 0;
  1029. } else {
  1030. sbiod->sbiod_sb->sb_trans_needs_write = 0;
  1031. }
  1032. return ret;
  1033. }
  1034. static Sockbuf_IO tlsg_sbio =
  1035. {
  1036. tlsg_sb_setup, /* sbi_setup */
  1037. tlsg_sb_remove, /* sbi_remove */
  1038. tlsg_sb_ctrl, /* sbi_ctrl */
  1039. tlsg_sb_read, /* sbi_read */
  1040. tlsg_sb_write, /* sbi_write */
  1041. tlsg_sb_close /* sbi_close */
  1042. };
  1043. /* Certs are not automatically verified during the handshake */
  1044. static int
  1045. tlsg_cert_verify( tlsg_session *ssl )
  1046. {
  1047. unsigned int status = 0;
  1048. int err;
  1049. time_t now = time(0);
  1050. time_t peertime;
  1051. err = gnutls_certificate_verify_peers2( ssl->session, &status );
  1052. if ( err < 0 ) {
  1053. Debug1( LDAP_DEBUG_ANY,"TLS: gnutls_certificate_verify_peers2 failed %d\n",
  1054. err );
  1055. return -1;
  1056. }
  1057. if ( status ) {
  1058. Debug1( LDAP_DEBUG_TRACE,"TLS: peer cert untrusted or revoked (0x%x)\n",
  1059. status );
  1060. return -1;
  1061. }
  1062. peertime = gnutls_certificate_expiration_time_peers( ssl->session );
  1063. if ( peertime == (time_t) -1 ) {
  1064. Debug0( LDAP_DEBUG_ANY, "TLS: gnutls_certificate_expiration_time_peers failed\n" );
  1065. return -1;
  1066. }
  1067. if ( peertime < now ) {
  1068. Debug0( LDAP_DEBUG_ANY, "TLS: peer certificate is expired\n" );
  1069. return -1;
  1070. }
  1071. peertime = gnutls_certificate_activation_time_peers( ssl->session );
  1072. if ( peertime == (time_t) -1 ) {
  1073. Debug0( LDAP_DEBUG_ANY, "TLS: gnutls_certificate_activation_time_peers failed\n" );
  1074. return -1;
  1075. }
  1076. if ( peertime > now ) {
  1077. Debug0( LDAP_DEBUG_ANY, "TLS: peer certificate not yet active\n" );
  1078. return -1;
  1079. }
  1080. return 0;
  1081. }
  1082. tls_impl ldap_int_tls_impl = {
  1083. "GnuTLS",
  1084. tlsg_init,
  1085. tlsg_destroy,
  1086. tlsg_ctx_new,
  1087. tlsg_ctx_ref,
  1088. tlsg_ctx_free,
  1089. tlsg_ctx_init,
  1090. tlsg_session_new,
  1091. tlsg_session_connect,
  1092. tlsg_session_accept,
  1093. tlsg_session_upflags,
  1094. tlsg_session_errmsg,
  1095. tlsg_session_my_dn,
  1096. tlsg_session_peer_dn,
  1097. tlsg_session_chkhost,
  1098. tlsg_session_strength,
  1099. tlsg_session_unique,
  1100. tlsg_session_endpoint,
  1101. tlsg_session_version,
  1102. tlsg_session_cipher,
  1103. tlsg_session_peercert,
  1104. tlsg_session_pinning,
  1105. &tlsg_sbio,
  1106. #ifdef LDAP_R_COMPILE
  1107. tlsg_thr_init,
  1108. #else
  1109. NULL,
  1110. #endif
  1111. 0
  1112. };
  1113. #endif /* HAVE_GNUTLS */