tortls.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /* Copyright 2003 Roger Dingledine. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. /* TLS wrappers for The Onion Router. (Unlike other tor functions, these
  5. * are prefixed with tor_ in order to avoid conflicting with OpenSSL
  6. * functions and variables.)
  7. */
  8. #include "./crypto.h"
  9. #include "./tortls.h"
  10. #include "./util.h"
  11. #include <assert.h>
  12. #include <openssl/ssl.h>
  13. #include <openssl/err.h>
  14. #include <openssl/tls1.h>
  15. #include <openssl/asn1.h>
  16. #include <openssl/bio.h>
  17. struct tor_tls_context_st {
  18. SSL_CTX *ctx;
  19. };
  20. struct tor_tls_st {
  21. SSL *ssl;
  22. int socket;
  23. enum {
  24. TOR_TLS_ST_HANDSHAKE, TOR_TLS_ST_OPEN, TOR_TLS_ST_GOTCLOSE,
  25. TOR_TLS_ST_SENTCLOSE, TOR_TLS_ST_CLOSED
  26. } state;
  27. int isServer;
  28. };
  29. #define _TOR_TLS_SYSCALL -6
  30. #define _TOR_TLS_ZERORETURN -5
  31. static int
  32. tor_tls_get_error(tor_tls *tls, int r, int extra)
  33. {
  34. int err = SSL_get_error(tls->ssl, r);
  35. switch (err) {
  36. case SSL_ERROR_NONE:
  37. return TOR_TLS_DONE;
  38. case SSL_ERROR_WANT_READ:
  39. return TOR_TLS_WANTREAD;
  40. case SSL_ERROR_WANT_WRITE:
  41. return TOR_TLS_WANTWRITE;
  42. case SSL_ERROR_SYSCALL:
  43. return extra ? _TOR_TLS_SYSCALL : TOR_TLS_ERROR;
  44. case SSL_ERROR_ZERO_RETURN:
  45. return extra ? _TOR_TLS_ZERORETURN : TOR_TLS_ERROR;
  46. default:
  47. return TOR_TLS_ERROR;
  48. }
  49. }
  50. static int always_accept_verify_cb(int preverify_ok,
  51. X509_STORE_CTX *x509_ctx)
  52. {
  53. /* XXXX Actually, this needs to get more complicated. But for now,
  54. XXXX always accept peer certs. */
  55. return 1;
  56. }
  57. /* Generate a self-signed certificate with the private key 'rsa' and
  58. * commonName 'nickname', and write it, PEM-encoded, to the file named
  59. * by 'certfile'. Return 0 on success, -1 for failure.
  60. */
  61. int
  62. tor_tls_write_certificate(char *certfile, crypto_pk_env_t *rsa, char *nickname)
  63. {
  64. RSA *_rsa = NULL;
  65. time_t start_time, end_time;
  66. EVP_PKEY *pkey = NULL;
  67. X509 *x509 = NULL;
  68. X509_NAME *name = NULL;
  69. BIO *out = NULL;
  70. int nid;
  71. start_time = time(NULL);
  72. assert(rsa && rsa->type == CRYPTO_PK_RSA);
  73. if (!(_rsa = RSAPrivateKey_dup((RSA*)rsa->key)))
  74. return -1;
  75. if (!(pkey = EVP_PKEY_new()))
  76. return -1;
  77. if (!(EVP_PKEY_assign_RSA(pkey, _rsa)))
  78. return -1;
  79. if (!(x509 = X509_new()))
  80. return -1;
  81. if (!(X509_set_version(x509, 2)))
  82. return -1;
  83. if (!(ASN1_INTEGER_set(X509_get_serialNumber(x509), (long)start_time)))
  84. return -1;
  85. if (!(name = X509_NAME_new()))
  86. return -1;
  87. if ((nid = OBJ_txt2nid("organizationName")) != NID_undef) return -1;
  88. if (!(X509_NAME_add_entry_by_NID(name, nid, MBSTRING_ASC,
  89. "TOR", -1, -1, 0))) return -1;
  90. if ((nid = OBJ_txt2nid("commonName")) != NID_undef) return -1;
  91. if (!(X509_NAME_add_entry_by_NID(name, nid, MBSTRING_ASC,
  92. nickname, -1, -1, 0))) return -1;
  93. if (!(X509_set_issuer_name(x509, name)))
  94. return -1;
  95. if (!(X509_set_subject_name(x509, name)))
  96. return -1;
  97. if (!X509_time_adj(X509_get_notBefore(x509),0,&start_time))
  98. return -1;
  99. end_time = start_time + 24*60*60*365;
  100. if (!X509_time_adj(X509_get_notAfter(x509),0,&end_time))
  101. return -1;
  102. if (!X509_set_pubkey(x509, pkey))
  103. return -1;
  104. if (!X509_sign(x509, pkey, EVP_sha1()))
  105. return -1;
  106. if (!(out = BIO_new_file(certfile, "w")))
  107. return -1;
  108. if (!(PEM_write_bio_X509(out, x509)))
  109. return -1;
  110. BIO_free(out);
  111. X509_free(x509);
  112. EVP_PKEY_free(pkey);
  113. X509_NAME_free(name);
  114. return 0;
  115. }
  116. /* Create a new TLS context. If we are going to be using it as a
  117. * server, it must have isServer set to true, certfile set to a
  118. * filename for a certificate file, and RSA set to the private key
  119. * used for that certificate.
  120. */
  121. tor_tls_context *
  122. tor_tls_context_new(char *certfile, crypto_pk_env_t *rsa, int isServer)
  123. {
  124. crypto_dh_env_t *dh = NULL;
  125. RSA *_rsa = NULL;
  126. EVP_PKEY *pkey = NULL;
  127. tor_tls_context *result;
  128. assert(!rsa || rsa->type == CRYPTO_PK_RSA);
  129. assert((certfile && rsa) || (!certfile && !rsa));
  130. result = tor_malloc(sizeof(tor_tls_context));
  131. if (!(result->ctx = SSL_CTX_new(TLSv1_method())))
  132. return NULL;
  133. /* XXXX This should use AES, but we'll need to require OpenSSL 0.9.7 first */
  134. if (!SSL_CTX_set_cipher_list(result->ctx, TLS1_TXT_DHE_DSS_WITH_RC4_128_SHA))
  135. /* TLS1_TXT_DHE_RSA_WITH_AES_128_SHA)) */
  136. return NULL;
  137. if (certfile && !SSL_CTX_use_certificate_file(result->ctx,certfile,
  138. SSL_FILETYPE_PEM))
  139. return NULL;
  140. SSL_CTX_set_session_cache_mode(result->ctx, SSL_SESS_CACHE_OFF);
  141. if (rsa) {
  142. if (!(_rsa = RSAPrivateKey_dup((RSA*)rsa->key)))
  143. return NULL;
  144. if (!(pkey = EVP_PKEY_new()))
  145. return NULL;
  146. if (!EVP_PKEY_assign_RSA(pkey, _rsa))
  147. return NULL;
  148. if (!SSL_CTX_use_PrivateKey(result->ctx, pkey))
  149. return NULL;
  150. EVP_PKEY_free(pkey);
  151. if (certfile) {
  152. if (!SSL_CTX_check_private_key(result->ctx))
  153. return NULL;
  154. }
  155. }
  156. dh = crypto_dh_new();
  157. SSL_CTX_set_tmp_dh(result->ctx, dh->dh);
  158. crypto_dh_free(dh);
  159. SSL_CTX_set_verify(result->ctx, SSL_VERIFY_PEER,
  160. always_accept_verify_cb);
  161. return result;
  162. }
  163. /* Create a new TLS object from a TLS context, a filedescriptor, and
  164. * a flag to determine whether it is functioning as a server.
  165. */
  166. tor_tls *
  167. tor_tls_new(tor_tls_context *ctx, int sock, int isServer)
  168. {
  169. tor_tls *result = tor_malloc(sizeof(tor_tls));
  170. if (!(result->ssl = SSL_new(ctx->ctx)))
  171. return NULL;
  172. result->socket = sock;
  173. SSL_set_fd(result->ssl, sock);
  174. result->state = TOR_TLS_ST_HANDSHAKE;
  175. result->isServer = isServer;
  176. return result;
  177. }
  178. /* Release resources associated with a TLS object. Does not close the
  179. * underlying file descriptor.
  180. */
  181. void
  182. tor_tls_free(tor_tls *tls)
  183. {
  184. SSL_free(tls->ssl);
  185. free(tls);
  186. }
  187. /* Underlying function for TLS reading. Reads up to 'len' characters
  188. * from 'tls' into 'cp'. On success, returns the number of characters
  189. * read. On failure, returns TOR_TLS_ERROR, TOR_TLS_CLOSE,
  190. * TOR_TLS_WANTREAD, or TOR_TLS_WANTWRITE.
  191. */
  192. int
  193. tor_tls_read(tor_tls *tls, char *cp, int len)
  194. {
  195. int r, err;
  196. assert(tls && tls->ssl);
  197. assert(tls->state == TOR_TLS_ST_OPEN);
  198. r = SSL_read(tls->ssl, cp, len);
  199. if (r > 0)
  200. return r;
  201. err = tor_tls_get_error(tls, r, 1);
  202. if (err == _TOR_TLS_SYSCALL)
  203. return TOR_TLS_ERROR;
  204. else if (err == _TOR_TLS_ZERORETURN) {
  205. tls->state = TOR_TLS_ST_CLOSED;
  206. return TOR_TLS_CLOSE;
  207. } else {
  208. /* XXXX Make sure it's not TOR_TLS_DONE. */
  209. return err;
  210. }
  211. }
  212. /* Underlying function for TLS writing. Write up to 'n' characters
  213. * from 'cp' onto 'tls'. On success, returns the number of characters
  214. * written. On failure, returns TOR_TLS_ERROR, TOR_TLS_WANTREAD,
  215. * or TOR_TLS_WANTWRITE.
  216. */
  217. int
  218. tor_tls_write(tor_tls *tls, char *cp, int n)
  219. {
  220. int r, err;
  221. assert(tls && tls->ssl);
  222. assert(tls->state == TOR_TLS_ST_OPEN);
  223. r = SSL_write(tls->ssl, cp, n);
  224. err = tor_tls_get_error(tls, r, 1);
  225. if (err == _TOR_TLS_ZERORETURN) {
  226. /* should never happen XXXX */
  227. return 0;
  228. } else if (err == TOR_TLS_DONE) {
  229. return r;
  230. } else {
  231. return err;
  232. }
  233. }
  234. /* Perform initial handshake on 'tls'. When finished, returns
  235. * TOR_TLS_DONE. On failure, returns TOR_TLS_ERROR, TOR_TLS_WANTREAD,
  236. * or TOR_TLS_WANNTWRITE.
  237. */
  238. int
  239. tor_tls_handshake(tor_tls *tls)
  240. {
  241. int r;
  242. assert(tls && tls->ssl);
  243. assert(tls->state == TOR_TLS_ST_HANDSHAKE);
  244. if (tls->isServer) {
  245. r = SSL_accept(tls->ssl);
  246. } else {
  247. r = SSL_connect(tls->ssl);
  248. }
  249. r = tor_tls_get_error(tls,r,0);
  250. if (r == TOR_TLS_DONE) {
  251. tls->state = TOR_TLS_ST_OPEN;
  252. }
  253. return r;
  254. }
  255. /* Shut down an open tls connection 'tls'. When finished, returns
  256. * TOR_TLS_DONE. On failure, returns TOR_TLS_ERROR, TOR_TLS_WANTREAD,
  257. * or TOR_TLS_WANTWRITE.
  258. */
  259. int
  260. tor_tls_shutdown(tor_tls *tls)
  261. {
  262. int r, err;
  263. char buf[128];
  264. assert(tls && tls->ssl);
  265. if (tls->state == TOR_TLS_ST_SENTCLOSE) {
  266. do {
  267. r = SSL_read(tls->ssl, buf, 128);
  268. } while (r>0);
  269. err = tor_tls_get_error(tls, r, 1);
  270. if (err == _TOR_TLS_ZERORETURN) {
  271. tls->state = TOR_TLS_ST_GOTCLOSE;
  272. /* fall through */
  273. } else {
  274. if (err == _TOR_TLS_SYSCALL)
  275. err = TOR_TLS_ERROR;
  276. return err;
  277. }
  278. }
  279. r = SSL_shutdown(tls->ssl);
  280. if (r == 1) {
  281. tls->state = TOR_TLS_ST_CLOSED;
  282. return TOR_TLS_DONE;
  283. }
  284. err = tor_tls_get_error(tls, r, 1);
  285. if (err == _TOR_TLS_SYSCALL)
  286. return TOR_TLS_ST_CLOSED; /* XXXX is this right? */
  287. else if (err == _TOR_TLS_ZERORETURN) {
  288. if (tls->state == TOR_TLS_ST_GOTCLOSE ||
  289. tls->state == TOR_TLS_ST_SENTCLOSE) {
  290. /* XXXX log; unexpected. */
  291. return TOR_TLS_ERROR;
  292. }
  293. tls->state = TOR_TLS_ST_SENTCLOSE;
  294. return tor_tls_shutdown(tls);
  295. } else {
  296. /* XXXX log if not error. */
  297. return err;
  298. }
  299. }