tls_srp.c 14 KB

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