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