PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/ssl/tls_srp.c

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