tls_g.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  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. keyhash.bv_len = gnutls_hash_get_len( alg );
  854. keyhash.bv_val = LDAP_MALLOC( keyhash.bv_len );
  855. if ( !keyhash.bv_val || gnutls_fingerprint( alg, &key,
  856. keyhash.bv_val, &keyhash.bv_len ) < 0 ) {
  857. goto done;
  858. }
  859. } else {
  860. keyhash.bv_val = (char *)key.data;
  861. keyhash.bv_len = key.size;
  862. }
  863. if ( ber_bvcmp( hash, &keyhash ) ) {
  864. rc = LDAP_CONNECT_ERROR;
  865. Debug0( LDAP_DEBUG_ANY, "tlsg_session_pinning: "
  866. "public key hash does not match provided pin.\n" );
  867. if ( ld->ld_error ) {
  868. LDAP_FREE( ld->ld_error );
  869. }
  870. ld->ld_error = LDAP_STRDUP(
  871. _("TLS: public key hash does not match provided pin"));
  872. } else {
  873. rc = LDAP_SUCCESS;
  874. }
  875. done:
  876. if ( pubkey ) {
  877. gnutls_pubkey_deinit( pubkey );
  878. }
  879. if ( crt ) {
  880. gnutls_x509_crt_deinit( crt );
  881. }
  882. if ( keyhash.bv_val != (char *)key.data ) {
  883. LDAP_FREE( keyhash.bv_val );
  884. }
  885. if ( key.data ) {
  886. LDAP_FREE( key.data );
  887. }
  888. return rc;
  889. }
  890. /* suites is a string of colon-separated cipher suite names. */
  891. static int
  892. tlsg_parse_ciphers( tlsg_ctx *ctx, char *suites )
  893. {
  894. const char *err;
  895. int rc = gnutls_priority_init( &ctx->prios, suites, &err );
  896. if ( rc )
  897. ctx->prios = NULL;
  898. return rc;
  899. }
  900. /*
  901. * TLS support for LBER Sockbufs
  902. */
  903. struct tls_data {
  904. tlsg_session *session;
  905. Sockbuf_IO_Desc *sbiod;
  906. };
  907. static ssize_t
  908. tlsg_recv( gnutls_transport_ptr_t ptr, void *buf, size_t len )
  909. {
  910. struct tls_data *p;
  911. if ( buf == NULL || len <= 0 ) return 0;
  912. p = (struct tls_data *)ptr;
  913. if ( p == NULL || p->sbiod == NULL ) {
  914. return 0;
  915. }
  916. return LBER_SBIOD_READ_NEXT( p->sbiod, buf, len );
  917. }
  918. static ssize_t
  919. tlsg_send( gnutls_transport_ptr_t ptr, const void *buf, size_t len )
  920. {
  921. struct tls_data *p;
  922. if ( buf == NULL || len <= 0 ) return 0;
  923. p = (struct tls_data *)ptr;
  924. if ( p == NULL || p->sbiod == NULL ) {
  925. return 0;
  926. }
  927. return LBER_SBIOD_WRITE_NEXT( p->sbiod, (char *)buf, len );
  928. }
  929. static int
  930. tlsg_sb_setup( Sockbuf_IO_Desc *sbiod, void *arg )
  931. {
  932. struct tls_data *p;
  933. tlsg_session *session = arg;
  934. assert( sbiod != NULL );
  935. p = LBER_MALLOC( sizeof( *p ) );
  936. if ( p == NULL ) {
  937. return -1;
  938. }
  939. gnutls_transport_set_ptr( session->session, (gnutls_transport_ptr_t)p );
  940. gnutls_transport_set_pull_function( session->session, tlsg_recv );
  941. gnutls_transport_set_push_function( session->session, tlsg_send );
  942. p->session = session;
  943. p->sbiod = sbiod;
  944. sbiod->sbiod_pvt = p;
  945. return 0;
  946. }
  947. static int
  948. tlsg_sb_remove( Sockbuf_IO_Desc *sbiod )
  949. {
  950. struct tls_data *p;
  951. assert( sbiod != NULL );
  952. assert( sbiod->sbiod_pvt != NULL );
  953. p = (struct tls_data *)sbiod->sbiod_pvt;
  954. gnutls_deinit ( p->session->session );
  955. LBER_FREE( p->session );
  956. LBER_FREE( sbiod->sbiod_pvt );
  957. sbiod->sbiod_pvt = NULL;
  958. return 0;
  959. }
  960. static int
  961. tlsg_sb_close( Sockbuf_IO_Desc *sbiod )
  962. {
  963. struct tls_data *p;
  964. assert( sbiod != NULL );
  965. assert( sbiod->sbiod_pvt != NULL );
  966. p = (struct tls_data *)sbiod->sbiod_pvt;
  967. gnutls_bye ( p->session->session, GNUTLS_SHUT_WR );
  968. return 0;
  969. }
  970. static int
  971. tlsg_sb_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
  972. {
  973. struct tls_data *p;
  974. assert( sbiod != NULL );
  975. assert( sbiod->sbiod_pvt != NULL );
  976. p = (struct tls_data *)sbiod->sbiod_pvt;
  977. if ( opt == LBER_SB_OPT_GET_SSL ) {
  978. *((tlsg_session **)arg) = p->session;
  979. return 1;
  980. } else if ( opt == LBER_SB_OPT_DATA_READY ) {
  981. if( gnutls_record_check_pending( p->session->session ) > 0 ) {
  982. return 1;
  983. }
  984. }
  985. return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg );
  986. }
  987. static ber_slen_t
  988. tlsg_sb_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
  989. {
  990. struct tls_data *p;
  991. ber_slen_t ret;
  992. assert( sbiod != NULL );
  993. assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
  994. p = (struct tls_data *)sbiod->sbiod_pvt;
  995. ret = gnutls_record_recv ( p->session->session, buf, len );
  996. switch (ret) {
  997. case GNUTLS_E_INTERRUPTED:
  998. case GNUTLS_E_AGAIN:
  999. sbiod->sbiod_sb->sb_trans_needs_read = 1;
  1000. sock_errset(EWOULDBLOCK);
  1001. ret = 0;
  1002. break;
  1003. case GNUTLS_E_REHANDSHAKE:
  1004. for ( ret = gnutls_handshake ( p->session->session );
  1005. ret == GNUTLS_E_INTERRUPTED || ret == GNUTLS_E_AGAIN;
  1006. ret = gnutls_handshake ( p->session->session ) );
  1007. sbiod->sbiod_sb->sb_trans_needs_read = 1;
  1008. ret = 0;
  1009. break;
  1010. default:
  1011. sbiod->sbiod_sb->sb_trans_needs_read = 0;
  1012. }
  1013. return ret;
  1014. }
  1015. static ber_slen_t
  1016. tlsg_sb_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
  1017. {
  1018. struct tls_data *p;
  1019. ber_slen_t ret;
  1020. assert( sbiod != NULL );
  1021. assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
  1022. p = (struct tls_data *)sbiod->sbiod_pvt;
  1023. ret = gnutls_record_send ( p->session->session, (char *)buf, len );
  1024. if ( ret == GNUTLS_E_INTERRUPTED || ret == GNUTLS_E_AGAIN ) {
  1025. sbiod->sbiod_sb->sb_trans_needs_write = 1;
  1026. sock_errset(EWOULDBLOCK);
  1027. ret = 0;
  1028. } else {
  1029. sbiod->sbiod_sb->sb_trans_needs_write = 0;
  1030. }
  1031. return ret;
  1032. }
  1033. static Sockbuf_IO tlsg_sbio =
  1034. {
  1035. tlsg_sb_setup, /* sbi_setup */
  1036. tlsg_sb_remove, /* sbi_remove */
  1037. tlsg_sb_ctrl, /* sbi_ctrl */
  1038. tlsg_sb_read, /* sbi_read */
  1039. tlsg_sb_write, /* sbi_write */
  1040. tlsg_sb_close /* sbi_close */
  1041. };
  1042. /* Certs are not automatically verified during the handshake */
  1043. static int
  1044. tlsg_cert_verify( tlsg_session *ssl )
  1045. {
  1046. unsigned int status = 0;
  1047. int err;
  1048. time_t now = time(0);
  1049. time_t peertime;
  1050. err = gnutls_certificate_verify_peers2( ssl->session, &status );
  1051. if ( err < 0 ) {
  1052. Debug1( LDAP_DEBUG_ANY,"TLS: gnutls_certificate_verify_peers2 failed %d\n",
  1053. err );
  1054. return -1;
  1055. }
  1056. if ( status ) {
  1057. Debug1( LDAP_DEBUG_TRACE,"TLS: peer cert untrusted or revoked (0x%x)\n",
  1058. status );
  1059. return -1;
  1060. }
  1061. peertime = gnutls_certificate_expiration_time_peers( ssl->session );
  1062. if ( peertime == (time_t) -1 ) {
  1063. Debug0( LDAP_DEBUG_ANY, "TLS: gnutls_certificate_expiration_time_peers failed\n" );
  1064. return -1;
  1065. }
  1066. if ( peertime < now ) {
  1067. Debug0( LDAP_DEBUG_ANY, "TLS: peer certificate is expired\n" );
  1068. return -1;
  1069. }
  1070. peertime = gnutls_certificate_activation_time_peers( ssl->session );
  1071. if ( peertime == (time_t) -1 ) {
  1072. Debug0( LDAP_DEBUG_ANY, "TLS: gnutls_certificate_activation_time_peers failed\n" );
  1073. return -1;
  1074. }
  1075. if ( peertime > now ) {
  1076. Debug0( LDAP_DEBUG_ANY, "TLS: peer certificate not yet active\n" );
  1077. return -1;
  1078. }
  1079. return 0;
  1080. }
  1081. tls_impl ldap_int_tls_impl = {
  1082. "GnuTLS",
  1083. tlsg_init,
  1084. tlsg_destroy,
  1085. tlsg_ctx_new,
  1086. tlsg_ctx_ref,
  1087. tlsg_ctx_free,
  1088. tlsg_ctx_init,
  1089. tlsg_session_new,
  1090. tlsg_session_connect,
  1091. tlsg_session_accept,
  1092. tlsg_session_upflags,
  1093. tlsg_session_errmsg,
  1094. tlsg_session_my_dn,
  1095. tlsg_session_peer_dn,
  1096. tlsg_session_chkhost,
  1097. tlsg_session_strength,
  1098. tlsg_session_unique,
  1099. tlsg_session_endpoint,
  1100. tlsg_session_version,
  1101. tlsg_session_cipher,
  1102. tlsg_session_peercert,
  1103. tlsg_session_pinning,
  1104. &tlsg_sbio,
  1105. #ifdef LDAP_R_COMPILE
  1106. tlsg_thr_init,
  1107. #else
  1108. NULL,
  1109. #endif
  1110. 0
  1111. };
  1112. #endif /* HAVE_GNUTLS */