sslgen.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2011, 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. /* This file is for implementing all "generic" SSL functions that all libcurl
  23. internals should use. It is then responsible for calling the proper
  24. "backend" function.
  25. SSL-functions in libcurl should call functions in this source file, and not
  26. to any specific SSL-layer.
  27. Curl_ssl_ - prefix for generic ones
  28. Curl_ossl_ - prefix for OpenSSL ones
  29. Curl_gtls_ - prefix for GnuTLS ones
  30. Curl_nss_ - prefix for NSS ones
  31. Curl_polarssl_ - prefix for PolarSSL ones
  32. Curl_cyassl_ - prefix for CyaSSL ones
  33. Note that this source code uses curlssl_* functions, and they are all
  34. defines/macros #defined by the lib-specific header files.
  35. "SSL/TLS Strong Encryption: An Introduction"
  36. http://httpd.apache.org/docs-2.0/ssl/ssl_intro.html
  37. */
  38. #include "setup.h"
  39. #ifdef HAVE_SYS_SOCKET_H
  40. #include <sys/socket.h>
  41. #endif
  42. #include "urldata.h"
  43. #define SSLGEN_C
  44. #include "sslgen.h" /* generic SSL protos etc */
  45. #include "ssluse.h" /* OpenSSL versions */
  46. #include "gtls.h" /* GnuTLS versions */
  47. #include "nssg.h" /* NSS versions */
  48. #include "qssl.h" /* QSOSSL versions */
  49. #include "polarssl.h" /* PolarSSL versions */
  50. #include "axtls.h" /* axTLS versions */
  51. #include "cyassl.h" /* CyaSSL versions */
  52. #include "sendf.h"
  53. #include "rawstr.h"
  54. #include "url.h"
  55. #include "curl_memory.h"
  56. #include "progress.h"
  57. /* The last #include file should be: */
  58. #include "memdebug.h"
  59. static bool safe_strequal(char* str1, char* str2)
  60. {
  61. if(str1 && str2)
  62. /* both pointers point to something then compare them */
  63. return (0 != Curl_raw_equal(str1, str2)) ? TRUE : FALSE;
  64. else
  65. /* if both pointers are NULL then treat them as equal */
  66. return (!str1 && !str2) ? TRUE : FALSE;
  67. }
  68. bool
  69. Curl_ssl_config_matches(struct ssl_config_data* data,
  70. struct ssl_config_data* needle)
  71. {
  72. if((data->version == needle->version) &&
  73. (data->verifypeer == needle->verifypeer) &&
  74. (data->verifyhost == needle->verifyhost) &&
  75. safe_strequal(data->CApath, needle->CApath) &&
  76. safe_strequal(data->CAfile, needle->CAfile) &&
  77. safe_strequal(data->random_file, needle->random_file) &&
  78. safe_strequal(data->egdsocket, needle->egdsocket) &&
  79. safe_strequal(data->cipher_list, needle->cipher_list))
  80. return TRUE;
  81. return FALSE;
  82. }
  83. bool
  84. Curl_clone_ssl_config(struct ssl_config_data *source,
  85. struct ssl_config_data *dest)
  86. {
  87. dest->sessionid = source->sessionid;
  88. dest->verifyhost = source->verifyhost;
  89. dest->verifypeer = source->verifypeer;
  90. dest->version = source->version;
  91. if(source->CAfile) {
  92. dest->CAfile = strdup(source->CAfile);
  93. if(!dest->CAfile)
  94. return FALSE;
  95. }
  96. else
  97. dest->CAfile = NULL;
  98. if(source->CApath) {
  99. dest->CApath = strdup(source->CApath);
  100. if(!dest->CApath)
  101. return FALSE;
  102. }
  103. else
  104. dest->CApath = NULL;
  105. if(source->cipher_list) {
  106. dest->cipher_list = strdup(source->cipher_list);
  107. if(!dest->cipher_list)
  108. return FALSE;
  109. }
  110. else
  111. dest->cipher_list = NULL;
  112. if(source->egdsocket) {
  113. dest->egdsocket = strdup(source->egdsocket);
  114. if(!dest->egdsocket)
  115. return FALSE;
  116. }
  117. else
  118. dest->egdsocket = NULL;
  119. if(source->random_file) {
  120. dest->random_file = strdup(source->random_file);
  121. if(!dest->random_file)
  122. return FALSE;
  123. }
  124. else
  125. dest->random_file = NULL;
  126. return TRUE;
  127. }
  128. void Curl_free_ssl_config(struct ssl_config_data* sslc)
  129. {
  130. Curl_safefree(sslc->CAfile);
  131. Curl_safefree(sslc->CApath);
  132. Curl_safefree(sslc->cipher_list);
  133. Curl_safefree(sslc->egdsocket);
  134. Curl_safefree(sslc->random_file);
  135. }
  136. #ifdef USE_SSL
  137. /* "global" init done? */
  138. static bool init_ssl=FALSE;
  139. /**
  140. * Global SSL init
  141. *
  142. * @retval 0 error initializing SSL
  143. * @retval 1 SSL initialized successfully
  144. */
  145. int Curl_ssl_init(void)
  146. {
  147. /* make sure this is only done once */
  148. if(init_ssl)
  149. return 1;
  150. init_ssl = TRUE; /* never again */
  151. return curlssl_init();
  152. }
  153. /* Global cleanup */
  154. void Curl_ssl_cleanup(void)
  155. {
  156. if(init_ssl) {
  157. /* only cleanup if we did a previous init */
  158. curlssl_cleanup();
  159. init_ssl = FALSE;
  160. }
  161. }
  162. CURLcode
  163. Curl_ssl_connect(struct connectdata *conn, int sockindex)
  164. {
  165. CURLcode res;
  166. /* mark this is being ssl-enabled from here on. */
  167. conn->ssl[sockindex].use = TRUE;
  168. conn->ssl[sockindex].state = ssl_connection_negotiating;
  169. res = curlssl_connect(conn, sockindex);
  170. if(!res)
  171. Curl_pgrsTime(conn->data, TIMER_APPCONNECT); /* SSL is connected */
  172. return res;
  173. }
  174. CURLcode
  175. Curl_ssl_connect_nonblocking(struct connectdata *conn, int sockindex,
  176. bool *done)
  177. {
  178. #ifdef curlssl_connect_nonblocking
  179. CURLcode res;
  180. /* mark this is being ssl requested from here on. */
  181. conn->ssl[sockindex].use = TRUE;
  182. res = curlssl_connect_nonblocking(conn, sockindex, done);
  183. if(!res && *done)
  184. Curl_pgrsTime(conn->data, TIMER_APPCONNECT); /* SSL is connected */
  185. return res;
  186. #else
  187. *done = TRUE; /* fallback to BLOCKING */
  188. conn->ssl[sockindex].use = TRUE;
  189. return curlssl_connect(conn, sockindex);
  190. #endif /* non-blocking connect support */
  191. }
  192. /*
  193. * Check if there's a session ID for the given connection in the cache, and if
  194. * there's one suitable, it is provided. Returns TRUE when no entry matched.
  195. */
  196. int Curl_ssl_getsessionid(struct connectdata *conn,
  197. void **ssl_sessionid,
  198. size_t *idsize) /* set 0 if unknown */
  199. {
  200. struct curl_ssl_session *check;
  201. struct SessionHandle *data = conn->data;
  202. long i;
  203. if(!conn->ssl_config.sessionid)
  204. /* session ID re-use is disabled */
  205. return TRUE;
  206. for(i=0; i< data->set.ssl.numsessions; i++) {
  207. check = &data->state.session[i];
  208. if(!check->sessionid)
  209. /* not session ID means blank entry */
  210. continue;
  211. if(Curl_raw_equal(conn->host.name, check->name) &&
  212. (conn->remote_port == check->remote_port) &&
  213. Curl_ssl_config_matches(&conn->ssl_config, &check->ssl_config)) {
  214. /* yes, we have a session ID! */
  215. data->state.sessionage++; /* increase general age */
  216. check->age = data->state.sessionage; /* set this as used in this age */
  217. *ssl_sessionid = check->sessionid;
  218. if(idsize)
  219. *idsize = check->idsize;
  220. return FALSE;
  221. }
  222. }
  223. *ssl_sessionid = NULL;
  224. return TRUE;
  225. }
  226. /*
  227. * Kill a single session ID entry in the cache.
  228. */
  229. static int kill_session(struct curl_ssl_session *session)
  230. {
  231. if(session->sessionid) {
  232. /* defensive check */
  233. /* free the ID the SSL-layer specific way */
  234. curlssl_session_free(session->sessionid);
  235. session->sessionid=NULL;
  236. session->age = 0; /* fresh */
  237. Curl_free_ssl_config(&session->ssl_config);
  238. Curl_safefree(session->name);
  239. session->name = NULL; /* no name */
  240. return 0; /* ok */
  241. }
  242. else
  243. return 1;
  244. }
  245. /*
  246. * Delete the given session ID from the cache.
  247. */
  248. void Curl_ssl_delsessionid(struct connectdata *conn, void *ssl_sessionid)
  249. {
  250. int i;
  251. for(i=0; i< conn->data->set.ssl.numsessions; i++) {
  252. struct curl_ssl_session *check = &conn->data->state.session[i];
  253. if(check->sessionid == ssl_sessionid) {
  254. kill_session(check);
  255. break;
  256. }
  257. }
  258. }
  259. /*
  260. * Store session id in the session cache. The ID passed on to this function
  261. * must already have been extracted and allocated the proper way for the SSL
  262. * layer. Curl_XXXX_session_free() will be called to free/kill the session ID
  263. * later on.
  264. */
  265. CURLcode Curl_ssl_addsessionid(struct connectdata *conn,
  266. void *ssl_sessionid,
  267. size_t idsize)
  268. {
  269. long i;
  270. struct SessionHandle *data=conn->data; /* the mother of all structs */
  271. struct curl_ssl_session *store = &data->state.session[0];
  272. long oldest_age=data->state.session[0].age; /* zero if unused */
  273. char *clone_host;
  274. /* Even though session ID re-use might be disabled, that only disables USING
  275. IT. We still store it here in case the re-using is again enabled for an
  276. upcoming transfer */
  277. clone_host = strdup(conn->host.name);
  278. if(!clone_host)
  279. return CURLE_OUT_OF_MEMORY; /* bail out */
  280. /* Now we should add the session ID and the host name to the cache, (remove
  281. the oldest if necessary) */
  282. /* find an empty slot for us, or find the oldest */
  283. for(i=1; (i<data->set.ssl.numsessions) &&
  284. data->state.session[i].sessionid; i++) {
  285. if(data->state.session[i].age < oldest_age) {
  286. oldest_age = data->state.session[i].age;
  287. store = &data->state.session[i];
  288. }
  289. }
  290. if(i == data->set.ssl.numsessions)
  291. /* cache is full, we must "kill" the oldest entry! */
  292. kill_session(store);
  293. else
  294. store = &data->state.session[i]; /* use this slot */
  295. /* now init the session struct wisely */
  296. store->sessionid = ssl_sessionid;
  297. store->idsize = idsize;
  298. store->age = data->state.sessionage; /* set current age */
  299. if(store->name)
  300. /* free it if there's one already present */
  301. free(store->name);
  302. store->name = clone_host; /* clone host name */
  303. store->remote_port = conn->remote_port; /* port number */
  304. if(!Curl_clone_ssl_config(&conn->ssl_config, &store->ssl_config)) {
  305. store->sessionid = NULL; /* let caller free sessionid */
  306. free(clone_host);
  307. return CURLE_OUT_OF_MEMORY;
  308. }
  309. return CURLE_OK;
  310. }
  311. void Curl_ssl_close_all(struct SessionHandle *data)
  312. {
  313. long i;
  314. /* kill the session ID cache */
  315. if(data->state.session) {
  316. for(i=0; i< data->set.ssl.numsessions; i++)
  317. /* the single-killer function handles empty table slots */
  318. kill_session(&data->state.session[i]);
  319. /* free the cache data */
  320. free(data->state.session);
  321. data->state.session = NULL;
  322. }
  323. curlssl_close_all(data);
  324. }
  325. void Curl_ssl_close(struct connectdata *conn, int sockindex)
  326. {
  327. DEBUGASSERT((sockindex <= 1) && (sockindex >= -1));
  328. curlssl_close(conn, sockindex);
  329. }
  330. CURLcode Curl_ssl_shutdown(struct connectdata *conn, int sockindex)
  331. {
  332. if(curlssl_shutdown(conn, sockindex))
  333. return CURLE_SSL_SHUTDOWN_FAILED;
  334. conn->ssl[sockindex].use = FALSE; /* get back to ordinary socket usage */
  335. conn->ssl[sockindex].state = ssl_connection_none;
  336. conn->recv[sockindex] = Curl_recv_plain;
  337. conn->send[sockindex] = Curl_send_plain;
  338. return CURLE_OK;
  339. }
  340. /* Selects an SSL crypto engine
  341. */
  342. CURLcode Curl_ssl_set_engine(struct SessionHandle *data, const char *engine)
  343. {
  344. return curlssl_set_engine(data, engine);
  345. }
  346. /* Selects the default SSL crypto engine
  347. */
  348. CURLcode Curl_ssl_set_engine_default(struct SessionHandle *data)
  349. {
  350. return curlssl_set_engine_default(data);
  351. }
  352. /* Return list of OpenSSL crypto engine names. */
  353. struct curl_slist *Curl_ssl_engines_list(struct SessionHandle *data)
  354. {
  355. return curlssl_engines_list(data);
  356. }
  357. /*
  358. * This sets up a session ID cache to the specified size. Make sure this code
  359. * is agnostic to what underlying SSL technology we use.
  360. */
  361. CURLcode Curl_ssl_initsessions(struct SessionHandle *data, long amount)
  362. {
  363. struct curl_ssl_session *session;
  364. if(data->state.session)
  365. /* this is just a precaution to prevent multiple inits */
  366. return CURLE_OK;
  367. session = calloc(amount, sizeof(struct curl_ssl_session));
  368. if(!session)
  369. return CURLE_OUT_OF_MEMORY;
  370. /* store the info in the SSL section */
  371. data->set.ssl.numsessions = amount;
  372. data->state.session = session;
  373. data->state.sessionage = 1; /* this is brand new */
  374. return CURLE_OK;
  375. }
  376. size_t Curl_ssl_version(char *buffer, size_t size)
  377. {
  378. return curlssl_version(buffer, size);
  379. }
  380. /*
  381. * This function tries to determine connection status.
  382. *
  383. * Return codes:
  384. * 1 means the connection is still in place
  385. * 0 means the connection has been closed
  386. * -1 means the connection status is unknown
  387. */
  388. int Curl_ssl_check_cxn(struct connectdata *conn)
  389. {
  390. return curlssl_check_cxn(conn);
  391. }
  392. bool Curl_ssl_data_pending(const struct connectdata *conn,
  393. int connindex)
  394. {
  395. return curlssl_data_pending(conn, connindex);
  396. }
  397. void Curl_ssl_free_certinfo(struct SessionHandle *data)
  398. {
  399. int i;
  400. struct curl_certinfo *ci = &data->info.certs;
  401. if(ci->num_of_certs) {
  402. /* free all individual lists used */
  403. for(i=0; i<ci->num_of_certs; i++)
  404. curl_slist_free_all(ci->certinfo[i]);
  405. free(ci->certinfo); /* free the actual array too */
  406. ci->num_of_certs = 0;
  407. }
  408. }
  409. #endif /* USE_SSL */