cyassl.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. /*
  23. * Source file for all CyaSSL-specific code for the TLS/SSL layer. No code
  24. * but vtls.c should ever call or use these functions.
  25. *
  26. */
  27. #include "curl_setup.h"
  28. #ifdef USE_CYASSL
  29. #ifdef HAVE_LIMITS_H
  30. #include <limits.h>
  31. #endif
  32. #include "urldata.h"
  33. #include "sendf.h"
  34. #include "inet_pton.h"
  35. #include "cyassl.h"
  36. #include "vtls.h"
  37. #include "parsedate.h"
  38. #include "connect.h" /* for the connect timeout */
  39. #include "select.h"
  40. #include "rawstr.h"
  41. #define _MPRINTF_REPLACE /* use our functions only */
  42. #include <curl/mprintf.h>
  43. #include "curl_memory.h"
  44. #include <cyassl/ssl.h>
  45. #ifdef HAVE_CYASSL_ERROR_SSL_H
  46. #include <cyassl/error-ssl.h>
  47. #else
  48. #include <cyassl/error.h>
  49. #endif
  50. #include <cyassl/ctaocrypt/random.h>
  51. /* The last #include file should be: */
  52. #include "memdebug.h"
  53. static Curl_recv cyassl_recv;
  54. static Curl_send cyassl_send;
  55. static int do_file_type(const char *type)
  56. {
  57. if(!type || !type[0])
  58. return SSL_FILETYPE_PEM;
  59. if(Curl_raw_equal(type, "PEM"))
  60. return SSL_FILETYPE_PEM;
  61. if(Curl_raw_equal(type, "DER"))
  62. return SSL_FILETYPE_ASN1;
  63. return -1;
  64. }
  65. /*
  66. * This function loads all the client/CA certificates and CRLs. Setup the TLS
  67. * layer and do all necessary magic.
  68. */
  69. static CURLcode
  70. cyassl_connect_step1(struct connectdata *conn,
  71. int sockindex)
  72. {
  73. struct SessionHandle *data = conn->data;
  74. struct ssl_connect_data* conssl = &conn->ssl[sockindex];
  75. SSL_METHOD* req_method = NULL;
  76. void* ssl_sessionid = NULL;
  77. curl_socket_t sockfd = conn->sock[sockindex];
  78. if(conssl->state == ssl_connection_complete)
  79. return CURLE_OK;
  80. /* CyaSSL doesn't support SSLv2 */
  81. if(data->set.ssl.version == CURL_SSLVERSION_SSLv2) {
  82. failf(data, "CyaSSL does not support SSLv2");
  83. return CURLE_SSL_CONNECT_ERROR;
  84. }
  85. /* check to see if we've been told to use an explicit SSL/TLS version */
  86. switch(data->set.ssl.version) {
  87. default:
  88. case CURL_SSLVERSION_DEFAULT:
  89. case CURL_SSLVERSION_TLSv1:
  90. infof(data, "CyaSSL cannot be configured to use TLS 1.0-1.2, "
  91. "TLS 1.0 is used exclusively\n");
  92. req_method = TLSv1_client_method();
  93. break;
  94. case CURL_SSLVERSION_TLSv1_0:
  95. req_method = TLSv1_client_method();
  96. break;
  97. case CURL_SSLVERSION_TLSv1_1:
  98. req_method = TLSv1_1_client_method();
  99. break;
  100. case CURL_SSLVERSION_TLSv1_2:
  101. req_method = TLSv1_2_client_method();
  102. break;
  103. case CURL_SSLVERSION_SSLv3:
  104. req_method = SSLv3_client_method();
  105. break;
  106. }
  107. if(!req_method) {
  108. failf(data, "SSL: couldn't create a method!");
  109. return CURLE_OUT_OF_MEMORY;
  110. }
  111. if(conssl->ctx)
  112. SSL_CTX_free(conssl->ctx);
  113. conssl->ctx = SSL_CTX_new(req_method);
  114. if(!conssl->ctx) {
  115. failf(data, "SSL: couldn't create a context!");
  116. return CURLE_OUT_OF_MEMORY;
  117. }
  118. #ifndef NO_FILESYSTEM
  119. /* load trusted cacert */
  120. if(data->set.str[STRING_SSL_CAFILE]) {
  121. if(!SSL_CTX_load_verify_locations(conssl->ctx,
  122. data->set.str[STRING_SSL_CAFILE],
  123. data->set.str[STRING_SSL_CAPATH])) {
  124. if(data->set.ssl.verifypeer) {
  125. /* Fail if we insist on successfully verifying the server. */
  126. failf(data,"error setting certificate verify locations:\n"
  127. " CAfile: %s\n CApath: %s",
  128. data->set.str[STRING_SSL_CAFILE]?
  129. data->set.str[STRING_SSL_CAFILE]: "none",
  130. data->set.str[STRING_SSL_CAPATH]?
  131. data->set.str[STRING_SSL_CAPATH] : "none");
  132. return CURLE_SSL_CACERT_BADFILE;
  133. }
  134. else {
  135. /* Just continue with a warning if no strict certificate
  136. verification is required. */
  137. infof(data, "error setting certificate verify locations,"
  138. " continuing anyway:\n");
  139. }
  140. }
  141. else {
  142. /* Everything is fine. */
  143. infof(data, "successfully set certificate verify locations:\n");
  144. }
  145. infof(data,
  146. " CAfile: %s\n"
  147. " CApath: %s\n",
  148. data->set.str[STRING_SSL_CAFILE] ? data->set.str[STRING_SSL_CAFILE]:
  149. "none",
  150. data->set.str[STRING_SSL_CAPATH] ? data->set.str[STRING_SSL_CAPATH]:
  151. "none");
  152. }
  153. /* Load the client certificate, and private key */
  154. if(data->set.str[STRING_CERT] && data->set.str[STRING_KEY]) {
  155. int file_type = do_file_type(data->set.str[STRING_CERT_TYPE]);
  156. if(SSL_CTX_use_certificate_file(conssl->ctx, data->set.str[STRING_CERT],
  157. file_type) != 1) {
  158. failf(data, "unable to use client certificate (no key or wrong pass"
  159. " phrase?)");
  160. return CURLE_SSL_CONNECT_ERROR;
  161. }
  162. file_type = do_file_type(data->set.str[STRING_KEY_TYPE]);
  163. if(SSL_CTX_use_PrivateKey_file(conssl->ctx, data->set.str[STRING_KEY],
  164. file_type) != 1) {
  165. failf(data, "unable to set private key");
  166. return CURLE_SSL_CONNECT_ERROR;
  167. }
  168. }
  169. #else
  170. if(CyaSSL_no_filesystem_verify(conssl->ctx)!= SSL_SUCCESS) {
  171. return CURLE_SSL_CONNECT_ERROR;
  172. }
  173. #endif /* NO_FILESYSTEM */
  174. /* SSL always tries to verify the peer, this only says whether it should
  175. * fail to connect if the verification fails, or if it should continue
  176. * anyway. In the latter case the result of the verification is checked with
  177. * SSL_get_verify_result() below. */
  178. SSL_CTX_set_verify(conssl->ctx,
  179. data->set.ssl.verifypeer?SSL_VERIFY_PEER:SSL_VERIFY_NONE,
  180. NULL);
  181. /* Let's make an SSL structure */
  182. if(conssl->handle)
  183. SSL_free(conssl->handle);
  184. conssl->handle = SSL_new(conssl->ctx);
  185. if(!conssl->handle) {
  186. failf(data, "SSL: couldn't create a context (handle)!");
  187. return CURLE_OUT_OF_MEMORY;
  188. }
  189. /* Check if there's a cached ID we can/should use here! */
  190. if(!Curl_ssl_getsessionid(conn, &ssl_sessionid, NULL)) {
  191. /* we got a session id, use it! */
  192. if(!SSL_set_session(conssl->handle, ssl_sessionid)) {
  193. failf(data, "SSL: SSL_set_session failed: %s",
  194. ERR_error_string(SSL_get_error(conssl->handle, 0),NULL));
  195. return CURLE_SSL_CONNECT_ERROR;
  196. }
  197. /* Informational message */
  198. infof (data, "SSL re-using session ID\n");
  199. }
  200. /* pass the raw socket into the SSL layer */
  201. if(!SSL_set_fd(conssl->handle, (int)sockfd)) {
  202. failf(data, "SSL: SSL_set_fd failed");
  203. return CURLE_SSL_CONNECT_ERROR;
  204. }
  205. conssl->connecting_state = ssl_connect_2;
  206. return CURLE_OK;
  207. }
  208. static CURLcode
  209. cyassl_connect_step2(struct connectdata *conn,
  210. int sockindex)
  211. {
  212. int ret = -1;
  213. struct SessionHandle *data = conn->data;
  214. struct ssl_connect_data* conssl = &conn->ssl[sockindex];
  215. infof(data, "CyaSSL: Connecting to %s:%d\n",
  216. conn->host.name, conn->remote_port);
  217. conn->recv[sockindex] = cyassl_recv;
  218. conn->send[sockindex] = cyassl_send;
  219. /* Enable RFC2818 checks */
  220. if(data->set.ssl.verifyhost) {
  221. ret = CyaSSL_check_domain_name(conssl->handle, conn->host.name);
  222. if(ret == SSL_FAILURE)
  223. return CURLE_OUT_OF_MEMORY;
  224. }
  225. ret = SSL_connect(conssl->handle);
  226. if(ret != 1) {
  227. char error_buffer[80];
  228. int detail = SSL_get_error(conssl->handle, ret);
  229. if(SSL_ERROR_WANT_READ == detail) {
  230. conssl->connecting_state = ssl_connect_2_reading;
  231. return CURLE_OK;
  232. }
  233. else if(SSL_ERROR_WANT_WRITE == detail) {
  234. conssl->connecting_state = ssl_connect_2_writing;
  235. return CURLE_OK;
  236. }
  237. /* There is no easy way to override only the CN matching.
  238. * This will enable the override of both mismatching SubjectAltNames
  239. * as also mismatching CN fields */
  240. else if(DOMAIN_NAME_MISMATCH == detail) {
  241. #if 1
  242. failf(data, "\tsubject alt name(s) or common name do not match \"%s\"\n",
  243. conn->host.dispname);
  244. return CURLE_PEER_FAILED_VERIFICATION;
  245. #else
  246. /* When the CyaSSL_check_domain_name() is used and you desire to continue
  247. * on a DOMAIN_NAME_MISMATCH, i.e. 'data->set.ssl.verifyhost == 0',
  248. * CyaSSL version 2.4.0 will fail with an INCOMPLETE_DATA error. The only
  249. * way to do this is currently to switch the CyaSSL_check_domain_name()
  250. * in and out based on the 'data->set.ssl.verifyhost' value. */
  251. if(data->set.ssl.verifyhost) {
  252. failf(data,
  253. "\tsubject alt name(s) or common name do not match \"%s\"\n",
  254. conn->host.dispname);
  255. return CURLE_PEER_FAILED_VERIFICATION;
  256. }
  257. else {
  258. infof(data,
  259. "\tsubject alt name(s) and/or common name do not match \"%s\"\n",
  260. conn->host.dispname);
  261. return CURLE_OK;
  262. }
  263. #endif
  264. }
  265. #if LIBCYASSL_VERSION_HEX >= 0x02007000 /* 2.7.0 */
  266. else if(ASN_NO_SIGNER_E == detail) {
  267. if(data->set.ssl.verifypeer) {
  268. failf(data, "\tCA signer not available for verification\n");
  269. return CURLE_SSL_CACERT_BADFILE;
  270. }
  271. else {
  272. /* Just continue with a warning if no strict certificate
  273. verification is required. */
  274. infof(data, "CA signer not available for verification, "
  275. "continuing anyway\n");
  276. }
  277. }
  278. #endif
  279. else {
  280. failf(data, "SSL_connect failed with error %d: %s", detail,
  281. ERR_error_string(detail, error_buffer));
  282. return CURLE_SSL_CONNECT_ERROR;
  283. }
  284. }
  285. conssl->connecting_state = ssl_connect_3;
  286. infof(data, "SSL connected\n");
  287. return CURLE_OK;
  288. }
  289. static CURLcode
  290. cyassl_connect_step3(struct connectdata *conn,
  291. int sockindex)
  292. {
  293. CURLcode retcode = CURLE_OK;
  294. void *old_ssl_sessionid=NULL;
  295. struct SessionHandle *data = conn->data;
  296. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  297. int incache;
  298. SSL_SESSION *our_ssl_sessionid;
  299. DEBUGASSERT(ssl_connect_3 == connssl->connecting_state);
  300. our_ssl_sessionid = SSL_get_session(connssl->handle);
  301. incache = !(Curl_ssl_getsessionid(conn, &old_ssl_sessionid, NULL));
  302. if(incache) {
  303. if(old_ssl_sessionid != our_ssl_sessionid) {
  304. infof(data, "old SSL session ID is stale, removing\n");
  305. Curl_ssl_delsessionid(conn, old_ssl_sessionid);
  306. incache = FALSE;
  307. }
  308. }
  309. if(!incache) {
  310. retcode = Curl_ssl_addsessionid(conn, our_ssl_sessionid,
  311. 0 /* unknown size */);
  312. if(retcode) {
  313. failf(data, "failed to store ssl session");
  314. return retcode;
  315. }
  316. }
  317. connssl->connecting_state = ssl_connect_done;
  318. return retcode;
  319. }
  320. static ssize_t cyassl_send(struct connectdata *conn,
  321. int sockindex,
  322. const void *mem,
  323. size_t len,
  324. CURLcode *curlcode)
  325. {
  326. char error_buffer[80];
  327. int memlen = (len > (size_t)INT_MAX) ? INT_MAX : (int)len;
  328. int rc = SSL_write(conn->ssl[sockindex].handle, mem, memlen);
  329. if(rc < 0) {
  330. int err = SSL_get_error(conn->ssl[sockindex].handle, rc);
  331. switch(err) {
  332. case SSL_ERROR_WANT_READ:
  333. case SSL_ERROR_WANT_WRITE:
  334. /* there's data pending, re-invoke SSL_write() */
  335. *curlcode = CURLE_AGAIN;
  336. return -1;
  337. default:
  338. failf(conn->data, "SSL write: %s, errno %d",
  339. ERR_error_string(err, error_buffer),
  340. SOCKERRNO);
  341. *curlcode = CURLE_SEND_ERROR;
  342. return -1;
  343. }
  344. }
  345. return rc;
  346. }
  347. void Curl_cyassl_close_all(struct SessionHandle *data)
  348. {
  349. (void)data;
  350. }
  351. void Curl_cyassl_close(struct connectdata *conn, int sockindex)
  352. {
  353. struct ssl_connect_data *conssl = &conn->ssl[sockindex];
  354. if(conssl->handle) {
  355. (void)SSL_shutdown(conssl->handle);
  356. SSL_free (conssl->handle);
  357. conssl->handle = NULL;
  358. }
  359. if(conssl->ctx) {
  360. SSL_CTX_free (conssl->ctx);
  361. conssl->ctx = NULL;
  362. }
  363. }
  364. static ssize_t cyassl_recv(struct connectdata *conn,
  365. int num,
  366. char *buf,
  367. size_t buffersize,
  368. CURLcode *curlcode)
  369. {
  370. char error_buffer[80];
  371. int buffsize = (buffersize > (size_t)INT_MAX) ? INT_MAX : (int)buffersize;
  372. int nread = SSL_read(conn->ssl[num].handle, buf, buffsize);
  373. if(nread < 0) {
  374. int err = SSL_get_error(conn->ssl[num].handle, nread);
  375. switch(err) {
  376. case SSL_ERROR_ZERO_RETURN: /* no more data */
  377. break;
  378. case SSL_ERROR_WANT_READ:
  379. case SSL_ERROR_WANT_WRITE:
  380. /* there's data pending, re-invoke SSL_read() */
  381. *curlcode = CURLE_AGAIN;
  382. return -1;
  383. default:
  384. failf(conn->data, "SSL read: %s, errno %d",
  385. ERR_error_string(err, error_buffer),
  386. SOCKERRNO);
  387. *curlcode = CURLE_RECV_ERROR;
  388. return -1;
  389. }
  390. }
  391. return nread;
  392. }
  393. void Curl_cyassl_session_free(void *ptr)
  394. {
  395. (void)ptr;
  396. /* CyaSSL reuses sessions on own, no free */
  397. }
  398. size_t Curl_cyassl_version(char *buffer, size_t size)
  399. {
  400. #ifdef CYASSL_VERSION
  401. return snprintf(buffer, size, "CyaSSL/%s", CYASSL_VERSION);
  402. #else
  403. return snprintf(buffer, size, "CyaSSL/%s", "<1.8.8");
  404. #endif
  405. }
  406. int Curl_cyassl_init(void)
  407. {
  408. if(CyaSSL_Init() == 0)
  409. return 1;
  410. return -1;
  411. }
  412. bool Curl_cyassl_data_pending(const struct connectdata* conn, int connindex)
  413. {
  414. if(conn->ssl[connindex].handle) /* SSL is in use */
  415. return (0 != SSL_pending(conn->ssl[connindex].handle)) ? TRUE : FALSE;
  416. else
  417. return FALSE;
  418. }
  419. /*
  420. * This function is called to shut down the SSL layer but keep the
  421. * socket open (CCC - Clear Command Channel)
  422. */
  423. int Curl_cyassl_shutdown(struct connectdata *conn, int sockindex)
  424. {
  425. int retval = 0;
  426. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  427. if(connssl->handle) {
  428. SSL_free (connssl->handle);
  429. connssl->handle = NULL;
  430. }
  431. return retval;
  432. }
  433. static CURLcode
  434. cyassl_connect_common(struct connectdata *conn,
  435. int sockindex,
  436. bool nonblocking,
  437. bool *done)
  438. {
  439. CURLcode retcode;
  440. struct SessionHandle *data = conn->data;
  441. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  442. curl_socket_t sockfd = conn->sock[sockindex];
  443. long timeout_ms;
  444. int what;
  445. /* check if the connection has already been established */
  446. if(ssl_connection_complete == connssl->state) {
  447. *done = TRUE;
  448. return CURLE_OK;
  449. }
  450. if(ssl_connect_1==connssl->connecting_state) {
  451. /* Find out how much more time we're allowed */
  452. timeout_ms = Curl_timeleft(data, NULL, TRUE);
  453. if(timeout_ms < 0) {
  454. /* no need to continue if time already is up */
  455. failf(data, "SSL connection timeout");
  456. return CURLE_OPERATION_TIMEDOUT;
  457. }
  458. retcode = cyassl_connect_step1(conn, sockindex);
  459. if(retcode)
  460. return retcode;
  461. }
  462. while(ssl_connect_2 == connssl->connecting_state ||
  463. ssl_connect_2_reading == connssl->connecting_state ||
  464. ssl_connect_2_writing == connssl->connecting_state) {
  465. /* check allowed time left */
  466. timeout_ms = Curl_timeleft(data, NULL, TRUE);
  467. if(timeout_ms < 0) {
  468. /* no need to continue if time already is up */
  469. failf(data, "SSL connection timeout");
  470. return CURLE_OPERATION_TIMEDOUT;
  471. }
  472. /* if ssl is expecting something, check if it's available. */
  473. if(connssl->connecting_state == ssl_connect_2_reading
  474. || connssl->connecting_state == ssl_connect_2_writing) {
  475. curl_socket_t writefd = ssl_connect_2_writing==
  476. connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
  477. curl_socket_t readfd = ssl_connect_2_reading==
  478. connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
  479. what = Curl_socket_ready(readfd, writefd, nonblocking?0:timeout_ms);
  480. if(what < 0) {
  481. /* fatal error */
  482. failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
  483. return CURLE_SSL_CONNECT_ERROR;
  484. }
  485. else if(0 == what) {
  486. if(nonblocking) {
  487. *done = FALSE;
  488. return CURLE_OK;
  489. }
  490. else {
  491. /* timeout */
  492. failf(data, "SSL connection timeout");
  493. return CURLE_OPERATION_TIMEDOUT;
  494. }
  495. }
  496. /* socket is readable or writable */
  497. }
  498. /* Run transaction, and return to the caller if it failed or if
  499. * this connection is part of a multi handle and this loop would
  500. * execute again. This permits the owner of a multi handle to
  501. * abort a connection attempt before step2 has completed while
  502. * ensuring that a client using select() or epoll() will always
  503. * have a valid fdset to wait on.
  504. */
  505. retcode = cyassl_connect_step2(conn, sockindex);
  506. if(retcode || (nonblocking &&
  507. (ssl_connect_2 == connssl->connecting_state ||
  508. ssl_connect_2_reading == connssl->connecting_state ||
  509. ssl_connect_2_writing == connssl->connecting_state)))
  510. return retcode;
  511. } /* repeat step2 until all transactions are done. */
  512. if(ssl_connect_3==connssl->connecting_state) {
  513. retcode = cyassl_connect_step3(conn, sockindex);
  514. if(retcode)
  515. return retcode;
  516. }
  517. if(ssl_connect_done==connssl->connecting_state) {
  518. connssl->state = ssl_connection_complete;
  519. conn->recv[sockindex] = cyassl_recv;
  520. conn->send[sockindex] = cyassl_send;
  521. *done = TRUE;
  522. }
  523. else
  524. *done = FALSE;
  525. /* Reset our connect state machine */
  526. connssl->connecting_state = ssl_connect_1;
  527. return CURLE_OK;
  528. }
  529. CURLcode
  530. Curl_cyassl_connect_nonblocking(struct connectdata *conn,
  531. int sockindex,
  532. bool *done)
  533. {
  534. return cyassl_connect_common(conn, sockindex, TRUE, done);
  535. }
  536. CURLcode
  537. Curl_cyassl_connect(struct connectdata *conn,
  538. int sockindex)
  539. {
  540. CURLcode retcode;
  541. bool done = FALSE;
  542. retcode = cyassl_connect_common(conn, sockindex, FALSE, &done);
  543. if(retcode)
  544. return retcode;
  545. DEBUGASSERT(done);
  546. return CURLE_OK;
  547. }
  548. int Curl_cyassl_random(struct SessionHandle *data,
  549. unsigned char *entropy,
  550. size_t length)
  551. {
  552. RNG rng;
  553. (void)data;
  554. if(InitRng(&rng))
  555. return 1;
  556. if(RNG_GenerateBlock(&rng, entropy, length))
  557. return 1;
  558. return 0;
  559. }
  560. #endif