tls_srp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. ERR_raise(ERR_LIB_SSL, 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. ERR_raise(ERR_LIB_SSL, 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. ERR_raise(ERR_LIB_SSL, 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, ERR_R_MALLOC_FAILURE);
  240. goto err;
  241. }
  242. BN_bn2bin(K, tmp);
  243. /* Calls SSLfatal() as required */
  244. ret = ssl_generate_master_secret(s, tmp, tmp_len, 1);
  245. err:
  246. BN_clear_free(K);
  247. BN_clear_free(u);
  248. return ret;
  249. }
  250. /* client side */
  251. int srp_generate_client_master_secret(SSL *s)
  252. {
  253. BIGNUM *x = NULL, *u = NULL, *K = NULL;
  254. int ret = -1, tmp_len = 0;
  255. char *passwd = NULL;
  256. unsigned char *tmp = NULL;
  257. /*
  258. * Checks if b % n == 0
  259. */
  260. if (SRP_Verify_B_mod_N(s->srp_ctx.B, s->srp_ctx.N) == 0
  261. || (u = SRP_Calc_u_ex(s->srp_ctx.A, s->srp_ctx.B, s->srp_ctx.N,
  262. s->ctx->libctx, s->ctx->propq))
  263. == NULL
  264. || s->srp_ctx.SRP_give_srp_client_pwd_callback == NULL) {
  265. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  266. goto err;
  267. }
  268. if ((passwd = s->srp_ctx.SRP_give_srp_client_pwd_callback(s,
  269. s->srp_ctx.SRP_cb_arg))
  270. == NULL) {
  271. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CALLBACK_FAILED);
  272. goto err;
  273. }
  274. if ((x = SRP_Calc_x_ex(s->srp_ctx.s, s->srp_ctx.login, passwd,
  275. s->ctx->libctx, s->ctx->propq)) == NULL
  276. || (K = SRP_Calc_client_key_ex(s->srp_ctx.N, s->srp_ctx.B,
  277. s->srp_ctx.g, x,
  278. s->srp_ctx.a, u,
  279. s->ctx->libctx,
  280. s->ctx->propq)) == NULL) {
  281. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  282. goto err;
  283. }
  284. tmp_len = BN_num_bytes(K);
  285. if ((tmp = OPENSSL_malloc(tmp_len)) == NULL) {
  286. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
  287. goto err;
  288. }
  289. BN_bn2bin(K, tmp);
  290. /* Calls SSLfatal() as required */
  291. ret = ssl_generate_master_secret(s, tmp, tmp_len, 1);
  292. err:
  293. BN_clear_free(K);
  294. BN_clear_free(x);
  295. if (passwd != NULL)
  296. OPENSSL_clear_free(passwd, strlen(passwd));
  297. BN_clear_free(u);
  298. return ret;
  299. }
  300. int srp_verify_server_param(SSL *s)
  301. {
  302. SRP_CTX *srp = &s->srp_ctx;
  303. /*
  304. * Sanity check parameters: we can quickly check B % N == 0 by checking B
  305. * != 0 since B < N
  306. */
  307. if (BN_ucmp(srp->g, srp->N) >= 0 || BN_ucmp(srp->B, srp->N) >= 0
  308. || BN_is_zero(srp->B)) {
  309. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_DATA);
  310. return 0;
  311. }
  312. if (BN_num_bits(srp->N) < srp->strength) {
  313. SSLfatal(s, SSL_AD_INSUFFICIENT_SECURITY, SSL_R_INSUFFICIENT_SECURITY);
  314. return 0;
  315. }
  316. if (srp->SRP_verify_param_callback) {
  317. if (srp->SRP_verify_param_callback(s, srp->SRP_cb_arg) <= 0) {
  318. SSLfatal(s, SSL_AD_INSUFFICIENT_SECURITY, SSL_R_CALLBACK_FAILED);
  319. return 0;
  320. }
  321. } else if (!SRP_check_known_gN_param(srp->g, srp->N)) {
  322. SSLfatal(s, SSL_AD_INSUFFICIENT_SECURITY,
  323. SSL_R_INSUFFICIENT_SECURITY);
  324. return 0;
  325. }
  326. return 1;
  327. }
  328. int SRP_Calc_A_param(SSL *s)
  329. {
  330. unsigned char rnd[SSL_MAX_MASTER_KEY_LENGTH];
  331. if (RAND_priv_bytes_ex(s->ctx->libctx, rnd, sizeof(rnd)) <= 0)
  332. return 0;
  333. s->srp_ctx.a = BN_bin2bn(rnd, sizeof(rnd), s->srp_ctx.a);
  334. OPENSSL_cleanse(rnd, sizeof(rnd));
  335. if (!(s->srp_ctx.A = SRP_Calc_A(s->srp_ctx.a, s->srp_ctx.N, s->srp_ctx.g)))
  336. return 0;
  337. return 1;
  338. }
  339. BIGNUM *SSL_get_srp_g(SSL *s)
  340. {
  341. if (s->srp_ctx.g != NULL)
  342. return s->srp_ctx.g;
  343. return s->ctx->srp_ctx.g;
  344. }
  345. BIGNUM *SSL_get_srp_N(SSL *s)
  346. {
  347. if (s->srp_ctx.N != NULL)
  348. return s->srp_ctx.N;
  349. return s->ctx->srp_ctx.N;
  350. }
  351. char *SSL_get_srp_username(SSL *s)
  352. {
  353. if (s->srp_ctx.login != NULL)
  354. return s->srp_ctx.login;
  355. return s->ctx->srp_ctx.login;
  356. }
  357. char *SSL_get_srp_userinfo(SSL *s)
  358. {
  359. if (s->srp_ctx.info != NULL)
  360. return s->srp_ctx.info;
  361. return s->ctx->srp_ctx.info;
  362. }
  363. # define tls1_ctx_ctrl ssl3_ctx_ctrl
  364. # define tls1_ctx_callback_ctrl ssl3_ctx_callback_ctrl
  365. int SSL_CTX_set_srp_username(SSL_CTX *ctx, char *name)
  366. {
  367. return tls1_ctx_ctrl(ctx, SSL_CTRL_SET_TLS_EXT_SRP_USERNAME, 0, name);
  368. }
  369. int SSL_CTX_set_srp_password(SSL_CTX *ctx, char *password)
  370. {
  371. return tls1_ctx_ctrl(ctx, SSL_CTRL_SET_TLS_EXT_SRP_PASSWORD, 0, password);
  372. }
  373. int SSL_CTX_set_srp_strength(SSL_CTX *ctx, int strength)
  374. {
  375. return tls1_ctx_ctrl(ctx, SSL_CTRL_SET_TLS_EXT_SRP_STRENGTH, strength,
  376. NULL);
  377. }
  378. int SSL_CTX_set_srp_verify_param_callback(SSL_CTX *ctx,
  379. int (*cb) (SSL *, void *))
  380. {
  381. return tls1_ctx_callback_ctrl(ctx, SSL_CTRL_SET_SRP_VERIFY_PARAM_CB,
  382. (void (*)(void))cb);
  383. }
  384. int SSL_CTX_set_srp_cb_arg(SSL_CTX *ctx, void *arg)
  385. {
  386. return tls1_ctx_ctrl(ctx, SSL_CTRL_SET_SRP_ARG, 0, arg);
  387. }
  388. int SSL_CTX_set_srp_username_callback(SSL_CTX *ctx,
  389. int (*cb) (SSL *, int *, void *))
  390. {
  391. return tls1_ctx_callback_ctrl(ctx, SSL_CTRL_SET_TLS_EXT_SRP_USERNAME_CB,
  392. (void (*)(void))cb);
  393. }
  394. int SSL_CTX_set_srp_client_pwd_callback(SSL_CTX *ctx,
  395. char *(*cb) (SSL *, void *))
  396. {
  397. return tls1_ctx_callback_ctrl(ctx, SSL_CTRL_SET_SRP_GIVE_CLIENT_PWD_CB,
  398. (void (*)(void))cb);
  399. }
  400. #endif