control_auth.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  2. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  3. /* See LICENSE for licensing information */
  4. /**
  5. * \file control_auth.c
  6. * \brief Authentication for Tor's control-socket interface.
  7. **/
  8. #include "core/or/or.h"
  9. #include "app/config/config.h"
  10. #include "core/mainloop/connection.h"
  11. #include "feature/control/control.h"
  12. #include "feature/control/control_cmd.h"
  13. #include "feature/control/control_auth.h"
  14. #include "feature/control/control_cmd_args_st.h"
  15. #include "feature/control/control_connection_st.h"
  16. #include "feature/control/control_proto.h"
  17. #include "lib/crypt_ops/crypto_rand.h"
  18. #include "lib/crypt_ops/crypto_util.h"
  19. #include "lib/encoding/confline.h"
  20. #include "lib/encoding/kvline.h"
  21. #include "lib/encoding/qstring.h"
  22. #include "lib/crypt_ops/crypto_s2k.h"
  23. /** If we're using cookie-type authentication, how long should our cookies be?
  24. */
  25. #define AUTHENTICATION_COOKIE_LEN 32
  26. /** If true, we've set authentication_cookie to a secret code and
  27. * stored it to disk. */
  28. static int authentication_cookie_is_set = 0;
  29. /** If authentication_cookie_is_set, a secret cookie that we've stored to disk
  30. * and which we're using to authenticate controllers. (If the controller can
  31. * read it off disk, it has permission to connect.) */
  32. static uint8_t *authentication_cookie = NULL;
  33. #define SAFECOOKIE_SERVER_TO_CONTROLLER_CONSTANT \
  34. "Tor safe cookie authentication server-to-controller hash"
  35. #define SAFECOOKIE_CONTROLLER_TO_SERVER_CONSTANT \
  36. "Tor safe cookie authentication controller-to-server hash"
  37. #define SAFECOOKIE_SERVER_NONCE_LEN DIGEST256_LEN
  38. /** Helper: Return a newly allocated string containing a path to the
  39. * file where we store our authentication cookie. */
  40. char *
  41. get_controller_cookie_file_name(void)
  42. {
  43. const or_options_t *options = get_options();
  44. if (options->CookieAuthFile && strlen(options->CookieAuthFile)) {
  45. return tor_strdup(options->CookieAuthFile);
  46. } else {
  47. return get_datadir_fname("control_auth_cookie");
  48. }
  49. }
  50. /* Initialize the cookie-based authentication system of the
  51. * ControlPort. If <b>enabled</b> is 0, then disable the cookie
  52. * authentication system. */
  53. int
  54. init_control_cookie_authentication(int enabled)
  55. {
  56. char *fname = NULL;
  57. int retval;
  58. if (!enabled) {
  59. authentication_cookie_is_set = 0;
  60. return 0;
  61. }
  62. fname = get_controller_cookie_file_name();
  63. retval = init_cookie_authentication(fname, "", /* no header */
  64. AUTHENTICATION_COOKIE_LEN,
  65. get_options()->CookieAuthFileGroupReadable,
  66. &authentication_cookie,
  67. &authentication_cookie_is_set);
  68. tor_free(fname);
  69. return retval;
  70. }
  71. /** Decode the hashed, base64'd passwords stored in <b>passwords</b>.
  72. * Return a smartlist of acceptable passwords (unterminated strings of
  73. * length S2K_RFC2440_SPECIFIER_LEN+DIGEST_LEN) on success, or NULL on
  74. * failure.
  75. */
  76. smartlist_t *
  77. decode_hashed_passwords(config_line_t *passwords)
  78. {
  79. char decoded[64];
  80. config_line_t *cl;
  81. smartlist_t *sl = smartlist_new();
  82. tor_assert(passwords);
  83. for (cl = passwords; cl; cl = cl->next) {
  84. const char *hashed = cl->value;
  85. if (!strcmpstart(hashed, "16:")) {
  86. if (base16_decode(decoded, sizeof(decoded), hashed+3, strlen(hashed+3))
  87. != S2K_RFC2440_SPECIFIER_LEN + DIGEST_LEN
  88. || strlen(hashed+3) != (S2K_RFC2440_SPECIFIER_LEN+DIGEST_LEN)*2) {
  89. goto err;
  90. }
  91. } else {
  92. if (base64_decode(decoded, sizeof(decoded), hashed, strlen(hashed))
  93. != S2K_RFC2440_SPECIFIER_LEN+DIGEST_LEN) {
  94. goto err;
  95. }
  96. }
  97. smartlist_add(sl,
  98. tor_memdup(decoded, S2K_RFC2440_SPECIFIER_LEN+DIGEST_LEN));
  99. }
  100. return sl;
  101. err:
  102. SMARTLIST_FOREACH(sl, char*, cp, tor_free(cp));
  103. smartlist_free(sl);
  104. return NULL;
  105. }
  106. const control_cmd_syntax_t authchallenge_syntax = {
  107. .min_args = 1,
  108. .max_args = 1,
  109. .accept_keywords=true,
  110. .kvline_flags=KV_OMIT_KEYS|KV_QUOTED_QSTRING,
  111. .store_raw_body=true
  112. };
  113. /** Called when we get an AUTHCHALLENGE command. */
  114. int
  115. handle_control_authchallenge(control_connection_t *conn,
  116. const control_cmd_args_t *args)
  117. {
  118. char *client_nonce;
  119. size_t client_nonce_len;
  120. char server_hash[DIGEST256_LEN];
  121. char server_hash_encoded[HEX_DIGEST256_LEN+1];
  122. char server_nonce[SAFECOOKIE_SERVER_NONCE_LEN];
  123. char server_nonce_encoded[(2*SAFECOOKIE_SERVER_NONCE_LEN) + 1];
  124. if (strcasecmp(smartlist_get(args->args, 0), "SAFECOOKIE")) {
  125. connection_write_str_to_buf("513 AUTHCHALLENGE only supports SAFECOOKIE "
  126. "authentication\r\n", conn);
  127. goto fail;
  128. }
  129. if (!authentication_cookie_is_set) {
  130. connection_write_str_to_buf("515 Cookie authentication is disabled\r\n",
  131. conn);
  132. goto fail;
  133. }
  134. if (args->kwargs == NULL || args->kwargs->next != NULL) {
  135. /* connection_write_str_to_buf("512 AUTHCHALLENGE requires exactly "
  136. "2 arguments.\r\n", conn);
  137. */
  138. connection_printf_to_buf(conn,
  139. "512 AUTHCHALLENGE dislikes argument list %s\r\n",
  140. escaped(args->raw_body));
  141. goto fail;
  142. }
  143. if (strcmp(args->kwargs->key, "")) {
  144. connection_write_str_to_buf("512 AUTHCHALLENGE does not accept keyword "
  145. "arguments.\r\n", conn);
  146. goto fail;
  147. }
  148. bool contains_quote = strchr(args->raw_body, '\"');
  149. if (contains_quote) {
  150. /* The nonce was quoted */
  151. client_nonce = tor_strdup(args->kwargs->value);
  152. client_nonce_len = strlen(client_nonce);
  153. } else {
  154. /* The nonce was should be in hex. */
  155. const char *hex_nonce = args->kwargs->value;
  156. client_nonce_len = strlen(hex_nonce) / 2;
  157. client_nonce = tor_malloc(client_nonce_len);
  158. if (base16_decode(client_nonce, client_nonce_len, hex_nonce,
  159. strlen(hex_nonce)) != (int)client_nonce_len) {
  160. connection_write_str_to_buf("513 Invalid base16 client nonce\r\n",
  161. conn);
  162. tor_free(client_nonce);
  163. goto fail;
  164. }
  165. }
  166. crypto_rand(server_nonce, SAFECOOKIE_SERVER_NONCE_LEN);
  167. /* Now compute and send the server-to-controller response, and the
  168. * server's nonce. */
  169. tor_assert(authentication_cookie != NULL);
  170. {
  171. size_t tmp_len = (AUTHENTICATION_COOKIE_LEN +
  172. client_nonce_len +
  173. SAFECOOKIE_SERVER_NONCE_LEN);
  174. char *tmp = tor_malloc_zero(tmp_len);
  175. char *client_hash = tor_malloc_zero(DIGEST256_LEN);
  176. memcpy(tmp, authentication_cookie, AUTHENTICATION_COOKIE_LEN);
  177. memcpy(tmp + AUTHENTICATION_COOKIE_LEN, client_nonce, client_nonce_len);
  178. memcpy(tmp + AUTHENTICATION_COOKIE_LEN + client_nonce_len,
  179. server_nonce, SAFECOOKIE_SERVER_NONCE_LEN);
  180. crypto_hmac_sha256(server_hash,
  181. SAFECOOKIE_SERVER_TO_CONTROLLER_CONSTANT,
  182. strlen(SAFECOOKIE_SERVER_TO_CONTROLLER_CONSTANT),
  183. tmp,
  184. tmp_len);
  185. crypto_hmac_sha256(client_hash,
  186. SAFECOOKIE_CONTROLLER_TO_SERVER_CONSTANT,
  187. strlen(SAFECOOKIE_CONTROLLER_TO_SERVER_CONSTANT),
  188. tmp,
  189. tmp_len);
  190. conn->safecookie_client_hash = client_hash;
  191. tor_free(tmp);
  192. }
  193. base16_encode(server_hash_encoded, sizeof(server_hash_encoded),
  194. server_hash, sizeof(server_hash));
  195. base16_encode(server_nonce_encoded, sizeof(server_nonce_encoded),
  196. server_nonce, sizeof(server_nonce));
  197. connection_printf_to_buf(conn,
  198. "250 AUTHCHALLENGE SERVERHASH=%s "
  199. "SERVERNONCE=%s\r\n",
  200. server_hash_encoded,
  201. server_nonce_encoded);
  202. tor_free(client_nonce);
  203. return 0;
  204. fail:
  205. connection_mark_for_close(TO_CONN(conn));
  206. return -1;
  207. }
  208. const control_cmd_syntax_t authenticate_syntax = {
  209. .max_args = 0,
  210. .accept_keywords=true,
  211. .kvline_flags=KV_OMIT_KEYS|KV_QUOTED_QSTRING,
  212. .store_raw_body=true
  213. };
  214. /** Called when we get an AUTHENTICATE message. Check whether the
  215. * authentication is valid, and if so, update the connection's state to
  216. * OPEN. Reply with DONE or ERROR.
  217. */
  218. int
  219. handle_control_authenticate(control_connection_t *conn,
  220. const control_cmd_args_t *args)
  221. {
  222. bool used_quoted_string = false;
  223. const or_options_t *options = get_options();
  224. const char *errstr = "Unknown error";
  225. char *password;
  226. size_t password_len;
  227. int bad_cookie=0, bad_password=0;
  228. smartlist_t *sl = NULL;
  229. if (args->kwargs == NULL) {
  230. password = tor_strdup("");
  231. password_len = 0;
  232. } else if (args->kwargs->next) {
  233. connection_write_str_to_buf(
  234. "512 Too many arguments to AUTHENTICATE.\r\n", conn);
  235. connection_mark_for_close(TO_CONN(conn));
  236. return 0;
  237. } else if (strcmp(args->kwargs->key, "")) {
  238. connection_write_str_to_buf(
  239. "512 AUTHENTICATE does not accept keyword arguments.\r\n", conn);
  240. connection_mark_for_close(TO_CONN(conn));
  241. return 0;
  242. } else if (strchr(args->raw_body, '\"')) {
  243. used_quoted_string = true;
  244. password = tor_strdup(args->kwargs->value);
  245. password_len = strlen(password);
  246. } else {
  247. const char *hex_passwd = args->kwargs->value;
  248. password_len = strlen(hex_passwd) / 2;
  249. password = tor_malloc(password_len+1);
  250. if (base16_decode(password, password_len+1, hex_passwd, strlen(hex_passwd))
  251. != (int) password_len) {
  252. connection_write_str_to_buf(
  253. "551 Invalid hexadecimal encoding. Maybe you tried a plain text "
  254. "password? If so, the standard requires that you put it in "
  255. "double quotes.\r\n", conn);
  256. connection_mark_for_close(TO_CONN(conn));
  257. tor_free(password);
  258. return 0;
  259. }
  260. }
  261. if (conn->safecookie_client_hash != NULL) {
  262. /* The controller has chosen safe cookie authentication; the only
  263. * acceptable authentication value is the controller-to-server
  264. * response. */
  265. tor_assert(authentication_cookie_is_set);
  266. if (password_len != DIGEST256_LEN) {
  267. log_warn(LD_CONTROL,
  268. "Got safe cookie authentication response with wrong length "
  269. "(%d)", (int)password_len);
  270. errstr = "Wrong length for safe cookie response.";
  271. goto err;
  272. }
  273. if (tor_memneq(conn->safecookie_client_hash, password, DIGEST256_LEN)) {
  274. log_warn(LD_CONTROL,
  275. "Got incorrect safe cookie authentication response");
  276. errstr = "Safe cookie response did not match expected value.";
  277. goto err;
  278. }
  279. tor_free(conn->safecookie_client_hash);
  280. goto ok;
  281. }
  282. if (!options->CookieAuthentication && !options->HashedControlPassword &&
  283. !options->HashedControlSessionPassword) {
  284. /* if Tor doesn't demand any stronger authentication, then
  285. * the controller can get in with anything. */
  286. goto ok;
  287. }
  288. if (options->CookieAuthentication) {
  289. int also_password = options->HashedControlPassword != NULL ||
  290. options->HashedControlSessionPassword != NULL;
  291. if (password_len != AUTHENTICATION_COOKIE_LEN) {
  292. if (!also_password) {
  293. log_warn(LD_CONTROL, "Got authentication cookie with wrong length "
  294. "(%d)", (int)password_len);
  295. errstr = "Wrong length on authentication cookie.";
  296. goto err;
  297. }
  298. bad_cookie = 1;
  299. } else if (tor_memneq(authentication_cookie, password, password_len)) {
  300. if (!also_password) {
  301. log_warn(LD_CONTROL, "Got mismatched authentication cookie");
  302. errstr = "Authentication cookie did not match expected value.";
  303. goto err;
  304. }
  305. bad_cookie = 1;
  306. } else {
  307. goto ok;
  308. }
  309. }
  310. if (options->HashedControlPassword ||
  311. options->HashedControlSessionPassword) {
  312. int bad = 0;
  313. smartlist_t *sl_tmp;
  314. char received[DIGEST_LEN];
  315. int also_cookie = options->CookieAuthentication;
  316. sl = smartlist_new();
  317. if (options->HashedControlPassword) {
  318. sl_tmp = decode_hashed_passwords(options->HashedControlPassword);
  319. if (!sl_tmp)
  320. bad = 1;
  321. else {
  322. smartlist_add_all(sl, sl_tmp);
  323. smartlist_free(sl_tmp);
  324. }
  325. }
  326. if (options->HashedControlSessionPassword) {
  327. sl_tmp = decode_hashed_passwords(options->HashedControlSessionPassword);
  328. if (!sl_tmp)
  329. bad = 1;
  330. else {
  331. smartlist_add_all(sl, sl_tmp);
  332. smartlist_free(sl_tmp);
  333. }
  334. }
  335. if (bad) {
  336. if (!also_cookie) {
  337. log_warn(LD_BUG,
  338. "Couldn't decode HashedControlPassword: invalid base16");
  339. errstr="Couldn't decode HashedControlPassword value in configuration.";
  340. goto err;
  341. }
  342. bad_password = 1;
  343. SMARTLIST_FOREACH(sl, char *, str, tor_free(str));
  344. smartlist_free(sl);
  345. sl = NULL;
  346. } else {
  347. SMARTLIST_FOREACH(sl, char *, expected,
  348. {
  349. secret_to_key_rfc2440(received,DIGEST_LEN,
  350. password,password_len,expected);
  351. if (tor_memeq(expected + S2K_RFC2440_SPECIFIER_LEN,
  352. received, DIGEST_LEN))
  353. goto ok;
  354. });
  355. SMARTLIST_FOREACH(sl, char *, str, tor_free(str));
  356. smartlist_free(sl);
  357. sl = NULL;
  358. if (used_quoted_string)
  359. errstr = "Password did not match HashedControlPassword value from "
  360. "configuration";
  361. else
  362. errstr = "Password did not match HashedControlPassword value from "
  363. "configuration. Maybe you tried a plain text password? "
  364. "If so, the standard requires that you put it in double quotes.";
  365. bad_password = 1;
  366. if (!also_cookie)
  367. goto err;
  368. }
  369. }
  370. /** We only get here if both kinds of authentication failed. */
  371. tor_assert(bad_password && bad_cookie);
  372. log_warn(LD_CONTROL, "Bad password or authentication cookie on controller.");
  373. errstr = "Password did not match HashedControlPassword *or* authentication "
  374. "cookie.";
  375. err:
  376. tor_free(password);
  377. connection_printf_to_buf(conn, "515 Authentication failed: %s\r\n", errstr);
  378. connection_mark_for_close(TO_CONN(conn));
  379. if (sl) { /* clean up */
  380. SMARTLIST_FOREACH(sl, char *, str, tor_free(str));
  381. smartlist_free(sl);
  382. }
  383. return 0;
  384. ok:
  385. log_info(LD_CONTROL, "Authenticated control connection ("TOR_SOCKET_T_FORMAT
  386. ")", conn->base_.s);
  387. send_control_done(conn);
  388. conn->base_.state = CONTROL_CONN_STATE_OPEN;
  389. tor_free(password);
  390. if (sl) { /* clean up */
  391. SMARTLIST_FOREACH(sl, char *, str, tor_free(str));
  392. smartlist_free(sl);
  393. }
  394. return 0;
  395. }
  396. void
  397. control_auth_free_all(void)
  398. {
  399. if (authentication_cookie) /* Free the auth cookie */
  400. tor_free(authentication_cookie);
  401. authentication_cookie_is_set = 0;
  402. }