tls_srp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /*
  2. * Copyright 2004-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2004, EdelKey Project. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License 2.0 (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. *
  10. * Originally written by Christophe Renou and Peter Sylvester,
  11. * for the EdelKey project.
  12. */
  13. /*
  14. * We need to use the SRP deprecated APIs in order to implement the SSL SRP
  15. * APIs - which are themselves deprecated.
  16. */
  17. #define OPENSSL_SUPPRESS_DEPRECATED
  18. #include <openssl/crypto.h>
  19. #include <openssl/rand.h>
  20. #include <openssl/err.h>
  21. #include "ssl_local.h"
  22. #ifndef OPENSSL_NO_SRP
  23. # include <openssl/srp.h>
  24. /*
  25. * The public API SSL_CTX_SRP_CTX_free() is deprecated so we use
  26. * ssl_ctx_srp_ctx_free_intern() internally.
  27. */
  28. int ssl_ctx_srp_ctx_free_intern(SSL_CTX *ctx)
  29. {
  30. if (ctx == NULL)
  31. return 0;
  32. OPENSSL_free(ctx->srp_ctx.login);
  33. OPENSSL_free(ctx->srp_ctx.info);
  34. BN_free(ctx->srp_ctx.N);
  35. BN_free(ctx->srp_ctx.g);
  36. BN_free(ctx->srp_ctx.s);
  37. BN_free(ctx->srp_ctx.B);
  38. BN_free(ctx->srp_ctx.A);
  39. BN_free(ctx->srp_ctx.a);
  40. BN_free(ctx->srp_ctx.b);
  41. BN_free(ctx->srp_ctx.v);
  42. memset(&ctx->srp_ctx, 0, sizeof(ctx->srp_ctx));
  43. ctx->srp_ctx.strength = SRP_MINIMAL_N;
  44. return 1;
  45. }
  46. int SSL_CTX_SRP_CTX_free(SSL_CTX *ctx)
  47. {
  48. return ssl_ctx_srp_ctx_free_intern(ctx);
  49. }
  50. /*
  51. * The public API SSL_SRP_CTX_free() is deprecated so we use
  52. * ssl_srp_ctx_free_intern() internally.
  53. */
  54. int ssl_srp_ctx_free_intern(SSL *s)
  55. {
  56. if (s == NULL)
  57. return 0;
  58. OPENSSL_free(s->srp_ctx.login);
  59. OPENSSL_free(s->srp_ctx.info);
  60. BN_free(s->srp_ctx.N);
  61. BN_free(s->srp_ctx.g);
  62. BN_free(s->srp_ctx.s);
  63. BN_free(s->srp_ctx.B);
  64. BN_free(s->srp_ctx.A);
  65. BN_free(s->srp_ctx.a);
  66. BN_free(s->srp_ctx.b);
  67. BN_free(s->srp_ctx.v);
  68. memset(&s->srp_ctx, 0, sizeof(s->srp_ctx));
  69. s->srp_ctx.strength = SRP_MINIMAL_N;
  70. return 1;
  71. }
  72. int SSL_SRP_CTX_free(SSL *s)
  73. {
  74. return ssl_srp_ctx_free_intern(s);
  75. }
  76. /*
  77. * The public API SSL_SRP_CTX_init() is deprecated so we use
  78. * ssl_srp_ctx_init_intern() internally.
  79. */
  80. int ssl_srp_ctx_init_intern(SSL *s)
  81. {
  82. SSL_CTX *ctx;
  83. if ((s == NULL) || ((ctx = s->ctx) == NULL))
  84. return 0;
  85. memset(&s->srp_ctx, 0, sizeof(s->srp_ctx));
  86. s->srp_ctx.SRP_cb_arg = ctx->srp_ctx.SRP_cb_arg;
  87. /* set client Hello login callback */
  88. s->srp_ctx.TLS_ext_srp_username_callback =
  89. ctx->srp_ctx.TLS_ext_srp_username_callback;
  90. /* set SRP N/g param callback for verification */
  91. s->srp_ctx.SRP_verify_param_callback =
  92. ctx->srp_ctx.SRP_verify_param_callback;
  93. /* set SRP client passwd callback */
  94. s->srp_ctx.SRP_give_srp_client_pwd_callback =
  95. ctx->srp_ctx.SRP_give_srp_client_pwd_callback;
  96. s->srp_ctx.strength = ctx->srp_ctx.strength;
  97. if (((ctx->srp_ctx.N != NULL) &&
  98. ((s->srp_ctx.N = BN_dup(ctx->srp_ctx.N)) == NULL)) ||
  99. ((ctx->srp_ctx.g != NULL) &&
  100. ((s->srp_ctx.g = BN_dup(ctx->srp_ctx.g)) == NULL)) ||
  101. ((ctx->srp_ctx.s != NULL) &&
  102. ((s->srp_ctx.s = BN_dup(ctx->srp_ctx.s)) == NULL)) ||
  103. ((ctx->srp_ctx.B != NULL) &&
  104. ((s->srp_ctx.B = BN_dup(ctx->srp_ctx.B)) == NULL)) ||
  105. ((ctx->srp_ctx.A != NULL) &&
  106. ((s->srp_ctx.A = BN_dup(ctx->srp_ctx.A)) == NULL)) ||
  107. ((ctx->srp_ctx.a != NULL) &&
  108. ((s->srp_ctx.a = BN_dup(ctx->srp_ctx.a)) == NULL)) ||
  109. ((ctx->srp_ctx.v != NULL) &&
  110. ((s->srp_ctx.v = BN_dup(ctx->srp_ctx.v)) == NULL)) ||
  111. ((ctx->srp_ctx.b != NULL) &&
  112. ((s->srp_ctx.b = BN_dup(ctx->srp_ctx.b)) == NULL))) {
  113. ERR_raise(ERR_LIB_SSL, ERR_R_BN_LIB);
  114. goto err;
  115. }
  116. if ((ctx->srp_ctx.login != NULL) &&
  117. ((s->srp_ctx.login = OPENSSL_strdup(ctx->srp_ctx.login)) == NULL)) {
  118. ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
  119. goto err;
  120. }
  121. if ((ctx->srp_ctx.info != NULL) &&
  122. ((s->srp_ctx.info = OPENSSL_strdup(ctx->srp_ctx.info)) == NULL)) {
  123. ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
  124. goto err;
  125. }
  126. s->srp_ctx.srp_Mask = ctx->srp_ctx.srp_Mask;
  127. return 1;
  128. err:
  129. OPENSSL_free(s->srp_ctx.login);
  130. OPENSSL_free(s->srp_ctx.info);
  131. BN_free(s->srp_ctx.N);
  132. BN_free(s->srp_ctx.g);
  133. BN_free(s->srp_ctx.s);
  134. BN_free(s->srp_ctx.B);
  135. BN_free(s->srp_ctx.A);
  136. BN_free(s->srp_ctx.a);
  137. BN_free(s->srp_ctx.b);
  138. BN_free(s->srp_ctx.v);
  139. memset(&s->srp_ctx, 0, sizeof(s->srp_ctx));
  140. return 0;
  141. }
  142. int SSL_SRP_CTX_init(SSL *s)
  143. {
  144. return ssl_srp_ctx_init_intern(s);
  145. }
  146. /*
  147. * The public API SSL_CTX_SRP_CTX_init() is deprecated so we use
  148. * ssl_ctx_srp_ctx_init_intern() internally.
  149. */
  150. int ssl_ctx_srp_ctx_init_intern(SSL_CTX *ctx)
  151. {
  152. if (ctx == NULL)
  153. return 0;
  154. memset(&ctx->srp_ctx, 0, sizeof(ctx->srp_ctx));
  155. ctx->srp_ctx.strength = SRP_MINIMAL_N;
  156. return 1;
  157. }
  158. int SSL_CTX_SRP_CTX_init(SSL_CTX *ctx)
  159. {
  160. return ssl_ctx_srp_ctx_init_intern(ctx);
  161. }
  162. /* server side */
  163. /*
  164. * The public API SSL_srp_server_param_with_username() is deprecated so we use
  165. * ssl_srp_server_param_with_username_intern() internally.
  166. */
  167. int ssl_srp_server_param_with_username_intern(SSL *s, int *ad)
  168. {
  169. unsigned char b[SSL_MAX_MASTER_KEY_LENGTH];
  170. int al;
  171. *ad = SSL_AD_UNKNOWN_PSK_IDENTITY;
  172. if ((s->srp_ctx.TLS_ext_srp_username_callback != NULL) &&
  173. ((al =
  174. s->srp_ctx.TLS_ext_srp_username_callback(s, ad,
  175. s->srp_ctx.SRP_cb_arg)) !=
  176. SSL_ERROR_NONE))
  177. return al;
  178. *ad = SSL_AD_INTERNAL_ERROR;
  179. if ((s->srp_ctx.N == NULL) ||
  180. (s->srp_ctx.g == NULL) ||
  181. (s->srp_ctx.s == NULL) || (s->srp_ctx.v == NULL))
  182. return SSL3_AL_FATAL;
  183. if (RAND_priv_bytes_ex(s->ctx->libctx, b, sizeof(b), 0) <= 0)
  184. return SSL3_AL_FATAL;
  185. s->srp_ctx.b = BN_bin2bn(b, sizeof(b), NULL);
  186. OPENSSL_cleanse(b, sizeof(b));
  187. /* Calculate: B = (kv + g^b) % N */
  188. return ((s->srp_ctx.B =
  189. SRP_Calc_B_ex(s->srp_ctx.b, s->srp_ctx.N, s->srp_ctx.g,
  190. s->srp_ctx.v, s->ctx->libctx, s->ctx->propq)) !=
  191. NULL) ? SSL_ERROR_NONE : SSL3_AL_FATAL;
  192. }
  193. int SSL_srp_server_param_with_username(SSL *s, int *ad)
  194. {
  195. return ssl_srp_server_param_with_username_intern(s, ad);
  196. }
  197. /*
  198. * If the server just has the raw password, make up a verifier entry on the
  199. * fly
  200. */
  201. int SSL_set_srp_server_param_pw(SSL *s, const char *user, const char *pass,
  202. const char *grp)
  203. {
  204. SRP_gN *GN = SRP_get_default_gN(grp);
  205. if (GN == NULL)
  206. return -1;
  207. s->srp_ctx.N = BN_dup(GN->N);
  208. s->srp_ctx.g = BN_dup(GN->g);
  209. BN_clear_free(s->srp_ctx.v);
  210. s->srp_ctx.v = NULL;
  211. BN_clear_free(s->srp_ctx.s);
  212. s->srp_ctx.s = NULL;
  213. if (!SRP_create_verifier_BN_ex(user, pass, &s->srp_ctx.s, &s->srp_ctx.v,
  214. s->srp_ctx.N, s->srp_ctx.g, s->ctx->libctx,
  215. s->ctx->propq))
  216. return -1;
  217. return 1;
  218. }
  219. int SSL_set_srp_server_param(SSL *s, const BIGNUM *N, const BIGNUM *g,
  220. BIGNUM *sa, BIGNUM *v, char *info)
  221. {
  222. if (N != NULL) {
  223. if (s->srp_ctx.N != NULL) {
  224. if (!BN_copy(s->srp_ctx.N, N)) {
  225. BN_free(s->srp_ctx.N);
  226. s->srp_ctx.N = NULL;
  227. }
  228. } else
  229. s->srp_ctx.N = BN_dup(N);
  230. }
  231. if (g != NULL) {
  232. if (s->srp_ctx.g != NULL) {
  233. if (!BN_copy(s->srp_ctx.g, g)) {
  234. BN_free(s->srp_ctx.g);
  235. s->srp_ctx.g = NULL;
  236. }
  237. } else
  238. s->srp_ctx.g = BN_dup(g);
  239. }
  240. if (sa != NULL) {
  241. if (s->srp_ctx.s != NULL) {
  242. if (!BN_copy(s->srp_ctx.s, sa)) {
  243. BN_free(s->srp_ctx.s);
  244. s->srp_ctx.s = NULL;
  245. }
  246. } else
  247. s->srp_ctx.s = BN_dup(sa);
  248. }
  249. if (v != NULL) {
  250. if (s->srp_ctx.v != NULL) {
  251. if (!BN_copy(s->srp_ctx.v, v)) {
  252. BN_free(s->srp_ctx.v);
  253. s->srp_ctx.v = NULL;
  254. }
  255. } else
  256. s->srp_ctx.v = BN_dup(v);
  257. }
  258. if (info != NULL) {
  259. if (s->srp_ctx.info)
  260. OPENSSL_free(s->srp_ctx.info);
  261. if ((s->srp_ctx.info = OPENSSL_strdup(info)) == NULL)
  262. return -1;
  263. }
  264. if (!(s->srp_ctx.N) ||
  265. !(s->srp_ctx.g) || !(s->srp_ctx.s) || !(s->srp_ctx.v))
  266. return -1;
  267. return 1;
  268. }
  269. int srp_generate_server_master_secret(SSL *s)
  270. {
  271. BIGNUM *K = NULL, *u = NULL;
  272. int ret = 0, tmp_len = 0;
  273. unsigned char *tmp = NULL;
  274. if (!SRP_Verify_A_mod_N(s->srp_ctx.A, s->srp_ctx.N))
  275. goto err;
  276. if ((u = SRP_Calc_u_ex(s->srp_ctx.A, s->srp_ctx.B, s->srp_ctx.N,
  277. s->ctx->libctx, s->ctx->propq)) == NULL)
  278. goto err;
  279. if ((K = SRP_Calc_server_key(s->srp_ctx.A, s->srp_ctx.v, u, s->srp_ctx.b,
  280. s->srp_ctx.N)) == NULL)
  281. goto err;
  282. tmp_len = BN_num_bytes(K);
  283. if ((tmp = OPENSSL_malloc(tmp_len)) == NULL) {
  284. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
  285. goto err;
  286. }
  287. BN_bn2bin(K, tmp);
  288. /* Calls SSLfatal() as required */
  289. ret = ssl_generate_master_secret(s, tmp, tmp_len, 1);
  290. err:
  291. BN_clear_free(K);
  292. BN_clear_free(u);
  293. return ret;
  294. }
  295. /* client side */
  296. int srp_generate_client_master_secret(SSL *s)
  297. {
  298. BIGNUM *x = NULL, *u = NULL, *K = NULL;
  299. int ret = 0, tmp_len = 0;
  300. char *passwd = NULL;
  301. unsigned char *tmp = NULL;
  302. /*
  303. * Checks if b % n == 0
  304. */
  305. if (SRP_Verify_B_mod_N(s->srp_ctx.B, s->srp_ctx.N) == 0
  306. || (u = SRP_Calc_u_ex(s->srp_ctx.A, s->srp_ctx.B, s->srp_ctx.N,
  307. s->ctx->libctx, s->ctx->propq))
  308. == NULL
  309. || s->srp_ctx.SRP_give_srp_client_pwd_callback == NULL) {
  310. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  311. goto err;
  312. }
  313. if ((passwd = s->srp_ctx.SRP_give_srp_client_pwd_callback(s,
  314. s->srp_ctx.SRP_cb_arg))
  315. == NULL) {
  316. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CALLBACK_FAILED);
  317. goto err;
  318. }
  319. if ((x = SRP_Calc_x_ex(s->srp_ctx.s, s->srp_ctx.login, passwd,
  320. s->ctx->libctx, s->ctx->propq)) == NULL
  321. || (K = SRP_Calc_client_key_ex(s->srp_ctx.N, s->srp_ctx.B,
  322. s->srp_ctx.g, x,
  323. s->srp_ctx.a, u,
  324. s->ctx->libctx,
  325. s->ctx->propq)) == NULL) {
  326. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  327. goto err;
  328. }
  329. tmp_len = BN_num_bytes(K);
  330. if ((tmp = OPENSSL_malloc(tmp_len)) == NULL) {
  331. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
  332. goto err;
  333. }
  334. BN_bn2bin(K, tmp);
  335. /* Calls SSLfatal() as required */
  336. ret = ssl_generate_master_secret(s, tmp, tmp_len, 1);
  337. err:
  338. BN_clear_free(K);
  339. BN_clear_free(x);
  340. if (passwd != NULL)
  341. OPENSSL_clear_free(passwd, strlen(passwd));
  342. BN_clear_free(u);
  343. return ret;
  344. }
  345. int srp_verify_server_param(SSL *s)
  346. {
  347. SRP_CTX *srp = &s->srp_ctx;
  348. /*
  349. * Sanity check parameters: we can quickly check B % N == 0 by checking B
  350. * != 0 since B < N
  351. */
  352. if (BN_ucmp(srp->g, srp->N) >= 0 || BN_ucmp(srp->B, srp->N) >= 0
  353. || BN_is_zero(srp->B)) {
  354. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_DATA);
  355. return 0;
  356. }
  357. if (BN_num_bits(srp->N) < srp->strength) {
  358. SSLfatal(s, SSL_AD_INSUFFICIENT_SECURITY, SSL_R_INSUFFICIENT_SECURITY);
  359. return 0;
  360. }
  361. if (srp->SRP_verify_param_callback) {
  362. if (srp->SRP_verify_param_callback(s, srp->SRP_cb_arg) <= 0) {
  363. SSLfatal(s, SSL_AD_INSUFFICIENT_SECURITY, SSL_R_CALLBACK_FAILED);
  364. return 0;
  365. }
  366. } else if (!SRP_check_known_gN_param(srp->g, srp->N)) {
  367. SSLfatal(s, SSL_AD_INSUFFICIENT_SECURITY,
  368. SSL_R_INSUFFICIENT_SECURITY);
  369. return 0;
  370. }
  371. return 1;
  372. }
  373. /*
  374. * The public API SRP_Calc_A_param() is deprecated so we use
  375. * ssl_srp_calc_a_param_intern() internally.
  376. */
  377. int ssl_srp_calc_a_param_intern(SSL *s)
  378. {
  379. unsigned char rnd[SSL_MAX_MASTER_KEY_LENGTH];
  380. if (RAND_priv_bytes_ex(s->ctx->libctx, rnd, sizeof(rnd), 0) <= 0)
  381. return 0;
  382. s->srp_ctx.a = BN_bin2bn(rnd, sizeof(rnd), s->srp_ctx.a);
  383. OPENSSL_cleanse(rnd, sizeof(rnd));
  384. if (!(s->srp_ctx.A = SRP_Calc_A(s->srp_ctx.a, s->srp_ctx.N, s->srp_ctx.g)))
  385. return 0;
  386. return 1;
  387. }
  388. int SRP_Calc_A_param(SSL *s)
  389. {
  390. return ssl_srp_calc_a_param_intern(s);
  391. }
  392. BIGNUM *SSL_get_srp_g(SSL *s)
  393. {
  394. if (s->srp_ctx.g != NULL)
  395. return s->srp_ctx.g;
  396. return s->ctx->srp_ctx.g;
  397. }
  398. BIGNUM *SSL_get_srp_N(SSL *s)
  399. {
  400. if (s->srp_ctx.N != NULL)
  401. return s->srp_ctx.N;
  402. return s->ctx->srp_ctx.N;
  403. }
  404. char *SSL_get_srp_username(SSL *s)
  405. {
  406. if (s->srp_ctx.login != NULL)
  407. return s->srp_ctx.login;
  408. return s->ctx->srp_ctx.login;
  409. }
  410. char *SSL_get_srp_userinfo(SSL *s)
  411. {
  412. if (s->srp_ctx.info != NULL)
  413. return s->srp_ctx.info;
  414. return s->ctx->srp_ctx.info;
  415. }
  416. # define tls1_ctx_ctrl ssl3_ctx_ctrl
  417. # define tls1_ctx_callback_ctrl ssl3_ctx_callback_ctrl
  418. int SSL_CTX_set_srp_username(SSL_CTX *ctx, char *name)
  419. {
  420. return tls1_ctx_ctrl(ctx, SSL_CTRL_SET_TLS_EXT_SRP_USERNAME, 0, name);
  421. }
  422. int SSL_CTX_set_srp_password(SSL_CTX *ctx, char *password)
  423. {
  424. return tls1_ctx_ctrl(ctx, SSL_CTRL_SET_TLS_EXT_SRP_PASSWORD, 0, password);
  425. }
  426. int SSL_CTX_set_srp_strength(SSL_CTX *ctx, int strength)
  427. {
  428. return tls1_ctx_ctrl(ctx, SSL_CTRL_SET_TLS_EXT_SRP_STRENGTH, strength,
  429. NULL);
  430. }
  431. int SSL_CTX_set_srp_verify_param_callback(SSL_CTX *ctx,
  432. int (*cb) (SSL *, void *))
  433. {
  434. return tls1_ctx_callback_ctrl(ctx, SSL_CTRL_SET_SRP_VERIFY_PARAM_CB,
  435. (void (*)(void))cb);
  436. }
  437. int SSL_CTX_set_srp_cb_arg(SSL_CTX *ctx, void *arg)
  438. {
  439. return tls1_ctx_ctrl(ctx, SSL_CTRL_SET_SRP_ARG, 0, arg);
  440. }
  441. int SSL_CTX_set_srp_username_callback(SSL_CTX *ctx,
  442. int (*cb) (SSL *, int *, void *))
  443. {
  444. return tls1_ctx_callback_ctrl(ctx, SSL_CTRL_SET_TLS_EXT_SRP_USERNAME_CB,
  445. (void (*)(void))cb);
  446. }
  447. int SSL_CTX_set_srp_client_pwd_callback(SSL_CTX *ctx,
  448. char *(*cb) (SSL *, void *))
  449. {
  450. return tls1_ctx_callback_ctrl(ctx, SSL_CTRL_SET_SRP_GIVE_CLIENT_PWD_CB,
  451. (void (*)(void))cb);
  452. }
  453. #endif