xref: /minix3/crypto/external/bsd/openssl/dist/ssl/tls_srp.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1ebfedea0SLionel Sambuc /* ssl/tls_srp.c */
2*0a6a1f1dSLionel Sambuc /*
3*0a6a1f1dSLionel Sambuc  * Written by Christophe Renou (christophe.renou@edelweb.fr) with the
4*0a6a1f1dSLionel Sambuc  * precious help of Peter Sylvester (peter.sylvester@edelweb.fr) for the
5*0a6a1f1dSLionel Sambuc  * EdelKey project and contributed to the OpenSSL project 2004.
6ebfedea0SLionel Sambuc  */
7ebfedea0SLionel Sambuc /* ====================================================================
8ebfedea0SLionel Sambuc  * Copyright (c) 2004-2011 The OpenSSL Project.  All rights reserved.
9ebfedea0SLionel Sambuc  *
10ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
11ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
12ebfedea0SLionel Sambuc  * are met:
13ebfedea0SLionel Sambuc  *
14ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
15ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
16ebfedea0SLionel Sambuc  *
17ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
18ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in
19ebfedea0SLionel Sambuc  *    the documentation and/or other materials provided with the
20ebfedea0SLionel Sambuc  *    distribution.
21ebfedea0SLionel Sambuc  *
22ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this
23ebfedea0SLionel Sambuc  *    software must display the following acknowledgment:
24ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
25ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26ebfedea0SLionel Sambuc  *
27ebfedea0SLionel Sambuc  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
28ebfedea0SLionel Sambuc  *    endorse or promote products derived from this software without
29ebfedea0SLionel Sambuc  *    prior written permission. For written permission, please contact
30ebfedea0SLionel Sambuc  *    licensing@OpenSSL.org.
31ebfedea0SLionel Sambuc  *
32ebfedea0SLionel Sambuc  * 5. Products derived from this software may not be called "OpenSSL"
33ebfedea0SLionel Sambuc  *    nor may "OpenSSL" appear in their names without prior written
34ebfedea0SLionel Sambuc  *    permission of the OpenSSL Project.
35ebfedea0SLionel Sambuc  *
36ebfedea0SLionel Sambuc  * 6. Redistributions of any form whatsoever must retain the following
37ebfedea0SLionel Sambuc  *    acknowledgment:
38ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
39ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40ebfedea0SLionel Sambuc  *
41ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
42ebfedea0SLionel Sambuc  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44ebfedea0SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
45ebfedea0SLionel Sambuc  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46ebfedea0SLionel Sambuc  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47ebfedea0SLionel Sambuc  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48ebfedea0SLionel Sambuc  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
50ebfedea0SLionel Sambuc  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51ebfedea0SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
52ebfedea0SLionel Sambuc  * OF THE POSSIBILITY OF SUCH DAMAGE.
53ebfedea0SLionel Sambuc  * ====================================================================
54ebfedea0SLionel Sambuc  *
55ebfedea0SLionel Sambuc  * This product includes cryptographic software written by Eric Young
56ebfedea0SLionel Sambuc  * (eay@cryptsoft.com).  This product includes software written by Tim
57ebfedea0SLionel Sambuc  * Hudson (tjh@cryptsoft.com).
58ebfedea0SLionel Sambuc  *
59ebfedea0SLionel Sambuc  */
60ebfedea0SLionel Sambuc #include "ssl_locl.h"
61ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SRP
62ebfedea0SLionel Sambuc 
63ebfedea0SLionel Sambuc # include <openssl/rand.h>
64ebfedea0SLionel Sambuc # include <openssl/srp.h>
65ebfedea0SLionel Sambuc # include <openssl/err.h>
66ebfedea0SLionel Sambuc 
SSL_CTX_SRP_CTX_free(struct ssl_ctx_st * ctx)67ebfedea0SLionel Sambuc int SSL_CTX_SRP_CTX_free(struct ssl_ctx_st *ctx)
68ebfedea0SLionel Sambuc {
69ebfedea0SLionel Sambuc     if (ctx == NULL)
70ebfedea0SLionel Sambuc         return 0;
71ebfedea0SLionel Sambuc     OPENSSL_free(ctx->srp_ctx.login);
72ebfedea0SLionel Sambuc     BN_free(ctx->srp_ctx.N);
73ebfedea0SLionel Sambuc     BN_free(ctx->srp_ctx.g);
74ebfedea0SLionel Sambuc     BN_free(ctx->srp_ctx.s);
75ebfedea0SLionel Sambuc     BN_free(ctx->srp_ctx.B);
76ebfedea0SLionel Sambuc     BN_free(ctx->srp_ctx.A);
77ebfedea0SLionel Sambuc     BN_free(ctx->srp_ctx.a);
78ebfedea0SLionel Sambuc     BN_free(ctx->srp_ctx.b);
79ebfedea0SLionel Sambuc     BN_free(ctx->srp_ctx.v);
80ebfedea0SLionel Sambuc     ctx->srp_ctx.TLS_ext_srp_username_callback = NULL;
81ebfedea0SLionel Sambuc     ctx->srp_ctx.SRP_cb_arg = NULL;
82ebfedea0SLionel Sambuc     ctx->srp_ctx.SRP_verify_param_callback = NULL;
83ebfedea0SLionel Sambuc     ctx->srp_ctx.SRP_give_srp_client_pwd_callback = NULL;
84ebfedea0SLionel Sambuc     ctx->srp_ctx.N = NULL;
85ebfedea0SLionel Sambuc     ctx->srp_ctx.g = NULL;
86ebfedea0SLionel Sambuc     ctx->srp_ctx.s = NULL;
87ebfedea0SLionel Sambuc     ctx->srp_ctx.B = NULL;
88ebfedea0SLionel Sambuc     ctx->srp_ctx.A = NULL;
89ebfedea0SLionel Sambuc     ctx->srp_ctx.a = NULL;
90ebfedea0SLionel Sambuc     ctx->srp_ctx.b = NULL;
91ebfedea0SLionel Sambuc     ctx->srp_ctx.v = NULL;
92ebfedea0SLionel Sambuc     ctx->srp_ctx.login = NULL;
93ebfedea0SLionel Sambuc     ctx->srp_ctx.info = NULL;
94ebfedea0SLionel Sambuc     ctx->srp_ctx.strength = SRP_MINIMAL_N;
95ebfedea0SLionel Sambuc     ctx->srp_ctx.srp_Mask = 0;
96ebfedea0SLionel Sambuc     return (1);
97ebfedea0SLionel Sambuc }
98ebfedea0SLionel Sambuc 
SSL_SRP_CTX_free(struct ssl_st * s)99ebfedea0SLionel Sambuc int SSL_SRP_CTX_free(struct ssl_st *s)
100ebfedea0SLionel Sambuc {
101ebfedea0SLionel Sambuc     if (s == NULL)
102ebfedea0SLionel Sambuc         return 0;
103ebfedea0SLionel Sambuc     OPENSSL_free(s->srp_ctx.login);
104ebfedea0SLionel Sambuc     BN_free(s->srp_ctx.N);
105ebfedea0SLionel Sambuc     BN_free(s->srp_ctx.g);
106ebfedea0SLionel Sambuc     BN_free(s->srp_ctx.s);
107ebfedea0SLionel Sambuc     BN_free(s->srp_ctx.B);
108ebfedea0SLionel Sambuc     BN_free(s->srp_ctx.A);
109ebfedea0SLionel Sambuc     BN_free(s->srp_ctx.a);
110ebfedea0SLionel Sambuc     BN_free(s->srp_ctx.b);
111ebfedea0SLionel Sambuc     BN_free(s->srp_ctx.v);
112ebfedea0SLionel Sambuc     s->srp_ctx.TLS_ext_srp_username_callback = NULL;
113ebfedea0SLionel Sambuc     s->srp_ctx.SRP_cb_arg = NULL;
114ebfedea0SLionel Sambuc     s->srp_ctx.SRP_verify_param_callback = NULL;
115ebfedea0SLionel Sambuc     s->srp_ctx.SRP_give_srp_client_pwd_callback = NULL;
116ebfedea0SLionel Sambuc     s->srp_ctx.N = NULL;
117ebfedea0SLionel Sambuc     s->srp_ctx.g = NULL;
118ebfedea0SLionel Sambuc     s->srp_ctx.s = NULL;
119ebfedea0SLionel Sambuc     s->srp_ctx.B = NULL;
120ebfedea0SLionel Sambuc     s->srp_ctx.A = NULL;
121ebfedea0SLionel Sambuc     s->srp_ctx.a = NULL;
122ebfedea0SLionel Sambuc     s->srp_ctx.b = NULL;
123ebfedea0SLionel Sambuc     s->srp_ctx.v = NULL;
124ebfedea0SLionel Sambuc     s->srp_ctx.login = NULL;
125ebfedea0SLionel Sambuc     s->srp_ctx.info = NULL;
126ebfedea0SLionel Sambuc     s->srp_ctx.strength = SRP_MINIMAL_N;
127ebfedea0SLionel Sambuc     s->srp_ctx.srp_Mask = 0;
128ebfedea0SLionel Sambuc     return (1);
129ebfedea0SLionel Sambuc }
130ebfedea0SLionel Sambuc 
SSL_SRP_CTX_init(struct ssl_st * s)131ebfedea0SLionel Sambuc int SSL_SRP_CTX_init(struct ssl_st *s)
132ebfedea0SLionel Sambuc {
133ebfedea0SLionel Sambuc     SSL_CTX *ctx;
134ebfedea0SLionel Sambuc 
135ebfedea0SLionel Sambuc     if ((s == NULL) || ((ctx = s->ctx) == NULL))
136ebfedea0SLionel Sambuc         return 0;
137ebfedea0SLionel Sambuc     s->srp_ctx.SRP_cb_arg = ctx->srp_ctx.SRP_cb_arg;
138ebfedea0SLionel Sambuc     /* set client Hello login callback */
139*0a6a1f1dSLionel Sambuc     s->srp_ctx.TLS_ext_srp_username_callback =
140*0a6a1f1dSLionel Sambuc         ctx->srp_ctx.TLS_ext_srp_username_callback;
141ebfedea0SLionel Sambuc     /* set SRP N/g param callback for verification */
142*0a6a1f1dSLionel Sambuc     s->srp_ctx.SRP_verify_param_callback =
143*0a6a1f1dSLionel Sambuc         ctx->srp_ctx.SRP_verify_param_callback;
144ebfedea0SLionel Sambuc     /* set SRP client passwd callback */
145*0a6a1f1dSLionel Sambuc     s->srp_ctx.SRP_give_srp_client_pwd_callback =
146*0a6a1f1dSLionel Sambuc         ctx->srp_ctx.SRP_give_srp_client_pwd_callback;
147ebfedea0SLionel Sambuc 
148ebfedea0SLionel Sambuc     s->srp_ctx.N = NULL;
149ebfedea0SLionel Sambuc     s->srp_ctx.g = NULL;
150ebfedea0SLionel Sambuc     s->srp_ctx.s = NULL;
151ebfedea0SLionel Sambuc     s->srp_ctx.B = NULL;
152ebfedea0SLionel Sambuc     s->srp_ctx.A = NULL;
153ebfedea0SLionel Sambuc     s->srp_ctx.a = NULL;
154ebfedea0SLionel Sambuc     s->srp_ctx.b = NULL;
155ebfedea0SLionel Sambuc     s->srp_ctx.v = NULL;
156ebfedea0SLionel Sambuc     s->srp_ctx.login = NULL;
157ebfedea0SLionel Sambuc     s->srp_ctx.info = ctx->srp_ctx.info;
158ebfedea0SLionel Sambuc     s->srp_ctx.strength = ctx->srp_ctx.strength;
159ebfedea0SLionel Sambuc 
160ebfedea0SLionel Sambuc     if (((ctx->srp_ctx.N != NULL) &&
161ebfedea0SLionel Sambuc          ((s->srp_ctx.N = BN_dup(ctx->srp_ctx.N)) == NULL)) ||
162ebfedea0SLionel Sambuc         ((ctx->srp_ctx.g != NULL) &&
163ebfedea0SLionel Sambuc          ((s->srp_ctx.g = BN_dup(ctx->srp_ctx.g)) == NULL)) ||
164ebfedea0SLionel Sambuc         ((ctx->srp_ctx.s != NULL) &&
165ebfedea0SLionel Sambuc          ((s->srp_ctx.s = BN_dup(ctx->srp_ctx.s)) == NULL)) ||
166ebfedea0SLionel Sambuc         ((ctx->srp_ctx.B != NULL) &&
167ebfedea0SLionel Sambuc          ((s->srp_ctx.B = BN_dup(ctx->srp_ctx.B)) == NULL)) ||
168ebfedea0SLionel Sambuc         ((ctx->srp_ctx.A != NULL) &&
169ebfedea0SLionel Sambuc          ((s->srp_ctx.A = BN_dup(ctx->srp_ctx.A)) == NULL)) ||
170ebfedea0SLionel Sambuc         ((ctx->srp_ctx.a != NULL) &&
171ebfedea0SLionel Sambuc          ((s->srp_ctx.a = BN_dup(ctx->srp_ctx.a)) == NULL)) ||
172ebfedea0SLionel Sambuc         ((ctx->srp_ctx.v != NULL) &&
173ebfedea0SLionel Sambuc          ((s->srp_ctx.v = BN_dup(ctx->srp_ctx.v)) == NULL)) ||
174ebfedea0SLionel Sambuc         ((ctx->srp_ctx.b != NULL) &&
175*0a6a1f1dSLionel Sambuc          ((s->srp_ctx.b = BN_dup(ctx->srp_ctx.b)) == NULL))) {
176ebfedea0SLionel Sambuc         SSLerr(SSL_F_SSL_SRP_CTX_INIT, ERR_R_BN_LIB);
177ebfedea0SLionel Sambuc         goto err;
178ebfedea0SLionel Sambuc     }
179ebfedea0SLionel Sambuc     if ((ctx->srp_ctx.login != NULL) &&
180*0a6a1f1dSLionel Sambuc         ((s->srp_ctx.login = BUF_strdup(ctx->srp_ctx.login)) == NULL)) {
181ebfedea0SLionel Sambuc         SSLerr(SSL_F_SSL_SRP_CTX_INIT, ERR_R_INTERNAL_ERROR);
182ebfedea0SLionel Sambuc         goto err;
183ebfedea0SLionel Sambuc     }
184ebfedea0SLionel Sambuc     s->srp_ctx.srp_Mask = ctx->srp_ctx.srp_Mask;
185ebfedea0SLionel Sambuc 
186ebfedea0SLionel Sambuc     return (1);
187ebfedea0SLionel Sambuc  err:
188ebfedea0SLionel Sambuc     OPENSSL_free(s->srp_ctx.login);
189ebfedea0SLionel Sambuc     BN_free(s->srp_ctx.N);
190ebfedea0SLionel Sambuc     BN_free(s->srp_ctx.g);
191ebfedea0SLionel Sambuc     BN_free(s->srp_ctx.s);
192ebfedea0SLionel Sambuc     BN_free(s->srp_ctx.B);
193ebfedea0SLionel Sambuc     BN_free(s->srp_ctx.A);
194ebfedea0SLionel Sambuc     BN_free(s->srp_ctx.a);
195ebfedea0SLionel Sambuc     BN_free(s->srp_ctx.b);
196ebfedea0SLionel Sambuc     BN_free(s->srp_ctx.v);
197ebfedea0SLionel Sambuc     return (0);
198ebfedea0SLionel Sambuc }
199ebfedea0SLionel Sambuc 
SSL_CTX_SRP_CTX_init(struct ssl_ctx_st * ctx)200ebfedea0SLionel Sambuc int SSL_CTX_SRP_CTX_init(struct ssl_ctx_st *ctx)
201ebfedea0SLionel Sambuc {
202ebfedea0SLionel Sambuc     if (ctx == NULL)
203ebfedea0SLionel Sambuc         return 0;
204ebfedea0SLionel Sambuc 
205ebfedea0SLionel Sambuc     ctx->srp_ctx.SRP_cb_arg = NULL;
206ebfedea0SLionel Sambuc     /* set client Hello login callback */
207ebfedea0SLionel Sambuc     ctx->srp_ctx.TLS_ext_srp_username_callback = NULL;
208ebfedea0SLionel Sambuc     /* set SRP N/g param callback for verification */
209ebfedea0SLionel Sambuc     ctx->srp_ctx.SRP_verify_param_callback = NULL;
210ebfedea0SLionel Sambuc     /* set SRP client passwd callback */
211ebfedea0SLionel Sambuc     ctx->srp_ctx.SRP_give_srp_client_pwd_callback = NULL;
212ebfedea0SLionel Sambuc 
213ebfedea0SLionel Sambuc     ctx->srp_ctx.N = NULL;
214ebfedea0SLionel Sambuc     ctx->srp_ctx.g = NULL;
215ebfedea0SLionel Sambuc     ctx->srp_ctx.s = NULL;
216ebfedea0SLionel Sambuc     ctx->srp_ctx.B = NULL;
217ebfedea0SLionel Sambuc     ctx->srp_ctx.A = NULL;
218ebfedea0SLionel Sambuc     ctx->srp_ctx.a = NULL;
219ebfedea0SLionel Sambuc     ctx->srp_ctx.b = NULL;
220ebfedea0SLionel Sambuc     ctx->srp_ctx.v = NULL;
221ebfedea0SLionel Sambuc     ctx->srp_ctx.login = NULL;
222ebfedea0SLionel Sambuc     ctx->srp_ctx.srp_Mask = 0;
223ebfedea0SLionel Sambuc     ctx->srp_ctx.info = NULL;
224ebfedea0SLionel Sambuc     ctx->srp_ctx.strength = SRP_MINIMAL_N;
225ebfedea0SLionel Sambuc 
226ebfedea0SLionel Sambuc     return (1);
227ebfedea0SLionel Sambuc }
228ebfedea0SLionel Sambuc 
229ebfedea0SLionel Sambuc /* server side */
SSL_srp_server_param_with_username(SSL * s,int * ad)230ebfedea0SLionel Sambuc int SSL_srp_server_param_with_username(SSL *s, int *ad)
231ebfedea0SLionel Sambuc {
232ebfedea0SLionel Sambuc     unsigned char b[SSL_MAX_MASTER_KEY_LENGTH];
233ebfedea0SLionel Sambuc     int al;
234ebfedea0SLionel Sambuc 
235ebfedea0SLionel Sambuc     *ad = SSL_AD_UNKNOWN_PSK_IDENTITY;
236ebfedea0SLionel Sambuc     if ((s->srp_ctx.TLS_ext_srp_username_callback != NULL) &&
237*0a6a1f1dSLionel Sambuc         ((al =
238*0a6a1f1dSLionel Sambuc           s->srp_ctx.TLS_ext_srp_username_callback(s, ad,
239*0a6a1f1dSLionel Sambuc                                                    s->srp_ctx.SRP_cb_arg)) !=
240*0a6a1f1dSLionel Sambuc          SSL_ERROR_NONE))
241ebfedea0SLionel Sambuc         return al;
242ebfedea0SLionel Sambuc 
243ebfedea0SLionel Sambuc     *ad = SSL_AD_INTERNAL_ERROR;
244ebfedea0SLionel Sambuc     if ((s->srp_ctx.N == NULL) ||
245ebfedea0SLionel Sambuc         (s->srp_ctx.g == NULL) ||
246*0a6a1f1dSLionel Sambuc         (s->srp_ctx.s == NULL) || (s->srp_ctx.v == NULL))
247ebfedea0SLionel Sambuc         return SSL3_AL_FATAL;
248ebfedea0SLionel Sambuc 
249ebfedea0SLionel Sambuc     if (RAND_bytes(b, sizeof(b)) <= 0)
250ebfedea0SLionel Sambuc         return SSL3_AL_FATAL;
251ebfedea0SLionel Sambuc     s->srp_ctx.b = BN_bin2bn(b, sizeof(b), NULL);
252ebfedea0SLionel Sambuc     OPENSSL_cleanse(b, sizeof(b));
253ebfedea0SLionel Sambuc 
254ebfedea0SLionel Sambuc     /* Calculate:  B = (kv + g^b) % N  */
255ebfedea0SLionel Sambuc 
256*0a6a1f1dSLionel Sambuc     return ((s->srp_ctx.B =
257*0a6a1f1dSLionel Sambuc              SRP_Calc_B(s->srp_ctx.b, s->srp_ctx.N, s->srp_ctx.g,
258*0a6a1f1dSLionel Sambuc                         s->srp_ctx.v)) !=
259*0a6a1f1dSLionel Sambuc             NULL) ? SSL_ERROR_NONE : SSL3_AL_FATAL;
260ebfedea0SLionel Sambuc }
261ebfedea0SLionel Sambuc 
262*0a6a1f1dSLionel Sambuc /*
263*0a6a1f1dSLionel Sambuc  * If the server just has the raw password, make up a verifier entry on the
264*0a6a1f1dSLionel Sambuc  * fly
265*0a6a1f1dSLionel Sambuc  */
SSL_set_srp_server_param_pw(SSL * s,const char * user,const char * pass,const char * grp)266*0a6a1f1dSLionel Sambuc int SSL_set_srp_server_param_pw(SSL *s, const char *user, const char *pass,
267*0a6a1f1dSLionel Sambuc                                 const char *grp)
268ebfedea0SLionel Sambuc {
269ebfedea0SLionel Sambuc     SRP_gN *GN = SRP_get_default_gN(grp);
270*0a6a1f1dSLionel Sambuc     if (GN == NULL)
271*0a6a1f1dSLionel Sambuc         return -1;
272ebfedea0SLionel Sambuc     s->srp_ctx.N = BN_dup(GN->N);
273ebfedea0SLionel Sambuc     s->srp_ctx.g = BN_dup(GN->g);
274*0a6a1f1dSLionel Sambuc     if (s->srp_ctx.v != NULL) {
275ebfedea0SLionel Sambuc         BN_clear_free(s->srp_ctx.v);
276ebfedea0SLionel Sambuc         s->srp_ctx.v = NULL;
277ebfedea0SLionel Sambuc     }
278*0a6a1f1dSLionel Sambuc     if (s->srp_ctx.s != NULL) {
279ebfedea0SLionel Sambuc         BN_clear_free(s->srp_ctx.s);
280ebfedea0SLionel Sambuc         s->srp_ctx.s = NULL;
281ebfedea0SLionel Sambuc     }
282*0a6a1f1dSLionel Sambuc     if (!SRP_create_verifier_BN
283*0a6a1f1dSLionel Sambuc         (user, pass, &s->srp_ctx.s, &s->srp_ctx.v, GN->N, GN->g))
284*0a6a1f1dSLionel Sambuc         return -1;
285ebfedea0SLionel Sambuc 
286ebfedea0SLionel Sambuc     return 1;
287ebfedea0SLionel Sambuc }
288ebfedea0SLionel Sambuc 
SSL_set_srp_server_param(SSL * s,const BIGNUM * N,const BIGNUM * g,BIGNUM * sa,BIGNUM * v,char * info)289ebfedea0SLionel Sambuc int SSL_set_srp_server_param(SSL *s, const BIGNUM *N, const BIGNUM *g,
290ebfedea0SLionel Sambuc                              BIGNUM *sa, BIGNUM *v, char *info)
291ebfedea0SLionel Sambuc {
292*0a6a1f1dSLionel Sambuc     if (N != NULL) {
293*0a6a1f1dSLionel Sambuc         if (s->srp_ctx.N != NULL) {
294*0a6a1f1dSLionel Sambuc             if (!BN_copy(s->srp_ctx.N, N)) {
295ebfedea0SLionel Sambuc                 BN_free(s->srp_ctx.N);
296ebfedea0SLionel Sambuc                 s->srp_ctx.N = NULL;
297ebfedea0SLionel Sambuc             }
298*0a6a1f1dSLionel Sambuc         } else
299ebfedea0SLionel Sambuc             s->srp_ctx.N = BN_dup(N);
300ebfedea0SLionel Sambuc     }
301*0a6a1f1dSLionel Sambuc     if (g != NULL) {
302*0a6a1f1dSLionel Sambuc         if (s->srp_ctx.g != NULL) {
303*0a6a1f1dSLionel Sambuc             if (!BN_copy(s->srp_ctx.g, g)) {
304ebfedea0SLionel Sambuc                 BN_free(s->srp_ctx.g);
305ebfedea0SLionel Sambuc                 s->srp_ctx.g = NULL;
306ebfedea0SLionel Sambuc             }
307*0a6a1f1dSLionel Sambuc         } else
308ebfedea0SLionel Sambuc             s->srp_ctx.g = BN_dup(g);
309ebfedea0SLionel Sambuc     }
310*0a6a1f1dSLionel Sambuc     if (sa != NULL) {
311*0a6a1f1dSLionel Sambuc         if (s->srp_ctx.s != NULL) {
312*0a6a1f1dSLionel Sambuc             if (!BN_copy(s->srp_ctx.s, sa)) {
313ebfedea0SLionel Sambuc                 BN_free(s->srp_ctx.s);
314ebfedea0SLionel Sambuc                 s->srp_ctx.s = NULL;
315ebfedea0SLionel Sambuc             }
316*0a6a1f1dSLionel Sambuc         } else
317ebfedea0SLionel Sambuc             s->srp_ctx.s = BN_dup(sa);
318ebfedea0SLionel Sambuc     }
319*0a6a1f1dSLionel Sambuc     if (v != NULL) {
320*0a6a1f1dSLionel Sambuc         if (s->srp_ctx.v != NULL) {
321*0a6a1f1dSLionel Sambuc             if (!BN_copy(s->srp_ctx.v, v)) {
322ebfedea0SLionel Sambuc                 BN_free(s->srp_ctx.v);
323ebfedea0SLionel Sambuc                 s->srp_ctx.v = NULL;
324ebfedea0SLionel Sambuc             }
325*0a6a1f1dSLionel Sambuc         } else
326ebfedea0SLionel Sambuc             s->srp_ctx.v = BN_dup(v);
327ebfedea0SLionel Sambuc     }
328ebfedea0SLionel Sambuc     s->srp_ctx.info = info;
329ebfedea0SLionel Sambuc 
330ebfedea0SLionel Sambuc     if (!(s->srp_ctx.N) ||
331*0a6a1f1dSLionel Sambuc         !(s->srp_ctx.g) || !(s->srp_ctx.s) || !(s->srp_ctx.v))
332ebfedea0SLionel Sambuc         return -1;
333ebfedea0SLionel Sambuc 
334ebfedea0SLionel Sambuc     return 1;
335ebfedea0SLionel Sambuc }
336ebfedea0SLionel Sambuc 
SRP_generate_server_master_secret(SSL * s,unsigned char * master_key)337ebfedea0SLionel Sambuc int SRP_generate_server_master_secret(SSL *s, unsigned char *master_key)
338ebfedea0SLionel Sambuc {
339ebfedea0SLionel Sambuc     BIGNUM *K = NULL, *u = NULL;
340ebfedea0SLionel Sambuc     int ret = -1, tmp_len;
341ebfedea0SLionel Sambuc     unsigned char *tmp = NULL;
342ebfedea0SLionel Sambuc 
343ebfedea0SLionel Sambuc     if (!SRP_Verify_A_mod_N(s->srp_ctx.A, s->srp_ctx.N))
344ebfedea0SLionel Sambuc         goto err;
345ebfedea0SLionel Sambuc     if (!(u = SRP_Calc_u(s->srp_ctx.A, s->srp_ctx.B, s->srp_ctx.N)))
346ebfedea0SLionel Sambuc         goto err;
347*0a6a1f1dSLionel Sambuc     if (!
348*0a6a1f1dSLionel Sambuc         (K =
349*0a6a1f1dSLionel Sambuc          SRP_Calc_server_key(s->srp_ctx.A, s->srp_ctx.v, u, s->srp_ctx.b,
350*0a6a1f1dSLionel Sambuc                              s->srp_ctx.N)))
351ebfedea0SLionel Sambuc         goto err;
352ebfedea0SLionel Sambuc 
353ebfedea0SLionel Sambuc     tmp_len = BN_num_bytes(K);
354ebfedea0SLionel Sambuc     if ((tmp = OPENSSL_malloc(tmp_len)) == NULL)
355ebfedea0SLionel Sambuc         goto err;
356ebfedea0SLionel Sambuc     BN_bn2bin(K, tmp);
357*0a6a1f1dSLionel Sambuc     ret =
358*0a6a1f1dSLionel Sambuc         s->method->ssl3_enc->generate_master_secret(s, master_key, tmp,
359*0a6a1f1dSLionel Sambuc                                                     tmp_len);
360ebfedea0SLionel Sambuc  err:
361*0a6a1f1dSLionel Sambuc     if (tmp) {
362ebfedea0SLionel Sambuc         OPENSSL_cleanse(tmp, tmp_len);
363ebfedea0SLionel Sambuc         OPENSSL_free(tmp);
364ebfedea0SLionel Sambuc     }
365ebfedea0SLionel Sambuc     BN_clear_free(K);
366ebfedea0SLionel Sambuc     BN_clear_free(u);
367ebfedea0SLionel Sambuc     return ret;
368ebfedea0SLionel Sambuc }
369ebfedea0SLionel Sambuc 
370ebfedea0SLionel Sambuc /* client side */
SRP_generate_client_master_secret(SSL * s,unsigned char * master_key)371ebfedea0SLionel Sambuc int SRP_generate_client_master_secret(SSL *s, unsigned char *master_key)
372ebfedea0SLionel Sambuc {
373ebfedea0SLionel Sambuc     BIGNUM *x = NULL, *u = NULL, *K = NULL;
374ebfedea0SLionel Sambuc     int ret = -1, tmp_len;
375ebfedea0SLionel Sambuc     char *passwd = NULL;
376ebfedea0SLionel Sambuc     unsigned char *tmp = NULL;
377ebfedea0SLionel Sambuc 
378*0a6a1f1dSLionel Sambuc     /*
379*0a6a1f1dSLionel Sambuc      * Checks if b % n == 0
380ebfedea0SLionel Sambuc      */
381*0a6a1f1dSLionel Sambuc     if (SRP_Verify_B_mod_N(s->srp_ctx.B, s->srp_ctx.N) == 0)
382*0a6a1f1dSLionel Sambuc         goto err;
383*0a6a1f1dSLionel Sambuc     if (!(u = SRP_Calc_u(s->srp_ctx.A, s->srp_ctx.B, s->srp_ctx.N)))
384*0a6a1f1dSLionel Sambuc         goto err;
385*0a6a1f1dSLionel Sambuc     if (s->srp_ctx.SRP_give_srp_client_pwd_callback == NULL)
386*0a6a1f1dSLionel Sambuc         goto err;
387*0a6a1f1dSLionel Sambuc     if (!
388*0a6a1f1dSLionel Sambuc         (passwd =
389*0a6a1f1dSLionel Sambuc          s->srp_ctx.SRP_give_srp_client_pwd_callback(s,
390*0a6a1f1dSLionel Sambuc                                                      s->srp_ctx.SRP_cb_arg)))
391*0a6a1f1dSLionel Sambuc         goto err;
392*0a6a1f1dSLionel Sambuc     if (!(x = SRP_Calc_x(s->srp_ctx.s, s->srp_ctx.login, passwd)))
393*0a6a1f1dSLionel Sambuc         goto err;
394*0a6a1f1dSLionel Sambuc     if (!
395*0a6a1f1dSLionel Sambuc         (K =
396*0a6a1f1dSLionel Sambuc          SRP_Calc_client_key(s->srp_ctx.N, s->srp_ctx.B, s->srp_ctx.g, x,
397*0a6a1f1dSLionel Sambuc                              s->srp_ctx.a, u)))
398*0a6a1f1dSLionel Sambuc         goto err;
399ebfedea0SLionel Sambuc 
400ebfedea0SLionel Sambuc     tmp_len = BN_num_bytes(K);
401*0a6a1f1dSLionel Sambuc     if ((tmp = OPENSSL_malloc(tmp_len)) == NULL)
402*0a6a1f1dSLionel Sambuc         goto err;
403ebfedea0SLionel Sambuc     BN_bn2bin(K, tmp);
404*0a6a1f1dSLionel Sambuc     ret =
405*0a6a1f1dSLionel Sambuc         s->method->ssl3_enc->generate_master_secret(s, master_key, tmp,
406*0a6a1f1dSLionel Sambuc                                                     tmp_len);
407ebfedea0SLionel Sambuc  err:
408*0a6a1f1dSLionel Sambuc     if (tmp) {
409ebfedea0SLionel Sambuc         OPENSSL_cleanse(tmp, tmp_len);
410ebfedea0SLionel Sambuc         OPENSSL_free(tmp);
411ebfedea0SLionel Sambuc     }
412ebfedea0SLionel Sambuc     BN_clear_free(K);
413ebfedea0SLionel Sambuc     BN_clear_free(x);
414*0a6a1f1dSLionel Sambuc     if (passwd) {
415ebfedea0SLionel Sambuc         OPENSSL_cleanse(passwd, strlen(passwd));
416ebfedea0SLionel Sambuc         OPENSSL_free(passwd);
417ebfedea0SLionel Sambuc     }
418ebfedea0SLionel Sambuc     BN_clear_free(u);
419ebfedea0SLionel Sambuc     return ret;
420ebfedea0SLionel Sambuc }
421ebfedea0SLionel Sambuc 
srp_verify_server_param(SSL * s,int * al)422*0a6a1f1dSLionel Sambuc int srp_verify_server_param(SSL *s, int *al)
423*0a6a1f1dSLionel Sambuc {
424*0a6a1f1dSLionel Sambuc     SRP_CTX *srp = &s->srp_ctx;
425*0a6a1f1dSLionel Sambuc     /*
426*0a6a1f1dSLionel Sambuc      * Sanity check parameters: we can quickly check B % N == 0 by checking B
427*0a6a1f1dSLionel Sambuc      * != 0 since B < N
428*0a6a1f1dSLionel Sambuc      */
429*0a6a1f1dSLionel Sambuc     if (BN_ucmp(srp->g, srp->N) >= 0 || BN_ucmp(srp->B, srp->N) >= 0
430*0a6a1f1dSLionel Sambuc         || BN_is_zero(srp->B)) {
431*0a6a1f1dSLionel Sambuc         *al = SSL3_AD_ILLEGAL_PARAMETER;
432*0a6a1f1dSLionel Sambuc         return 0;
433*0a6a1f1dSLionel Sambuc     }
434*0a6a1f1dSLionel Sambuc 
435*0a6a1f1dSLionel Sambuc     if (BN_num_bits(srp->N) < srp->strength) {
436*0a6a1f1dSLionel Sambuc         *al = TLS1_AD_INSUFFICIENT_SECURITY;
437*0a6a1f1dSLionel Sambuc         return 0;
438*0a6a1f1dSLionel Sambuc     }
439*0a6a1f1dSLionel Sambuc 
440*0a6a1f1dSLionel Sambuc     if (srp->SRP_verify_param_callback) {
441*0a6a1f1dSLionel Sambuc         if (srp->SRP_verify_param_callback(s, srp->SRP_cb_arg) <= 0) {
442*0a6a1f1dSLionel Sambuc             *al = TLS1_AD_INSUFFICIENT_SECURITY;
443*0a6a1f1dSLionel Sambuc             return 0;
444*0a6a1f1dSLionel Sambuc         }
445*0a6a1f1dSLionel Sambuc     } else if (!SRP_check_known_gN_param(srp->g, srp->N)) {
446*0a6a1f1dSLionel Sambuc         *al = TLS1_AD_INSUFFICIENT_SECURITY;
447*0a6a1f1dSLionel Sambuc         return 0;
448*0a6a1f1dSLionel Sambuc     }
449*0a6a1f1dSLionel Sambuc 
450*0a6a1f1dSLionel Sambuc     return 1;
451*0a6a1f1dSLionel Sambuc }
452*0a6a1f1dSLionel Sambuc 
SRP_Calc_A_param(SSL * s)453ebfedea0SLionel Sambuc int SRP_Calc_A_param(SSL *s)
454ebfedea0SLionel Sambuc {
455ebfedea0SLionel Sambuc     unsigned char rnd[SSL_MAX_MASTER_KEY_LENGTH];
456ebfedea0SLionel Sambuc 
457*0a6a1f1dSLionel Sambuc     if (RAND_bytes(rnd, sizeof(rnd)) <= 0)
458ebfedea0SLionel Sambuc         return -1;
459ebfedea0SLionel Sambuc     s->srp_ctx.a = BN_bin2bn(rnd, sizeof(rnd), s->srp_ctx.a);
460ebfedea0SLionel Sambuc     OPENSSL_cleanse(rnd, sizeof(rnd));
461ebfedea0SLionel Sambuc 
462*0a6a1f1dSLionel Sambuc     if (!
463*0a6a1f1dSLionel Sambuc         (s->srp_ctx.A = SRP_Calc_A(s->srp_ctx.a, s->srp_ctx.N, s->srp_ctx.g)))
464ebfedea0SLionel Sambuc         return -1;
465ebfedea0SLionel Sambuc 
466ebfedea0SLionel Sambuc     return 1;
467ebfedea0SLionel Sambuc }
468ebfedea0SLionel Sambuc 
SSL_get_srp_g(SSL * s)469ebfedea0SLionel Sambuc BIGNUM *SSL_get_srp_g(SSL *s)
470ebfedea0SLionel Sambuc {
471ebfedea0SLionel Sambuc     if (s->srp_ctx.g != NULL)
472ebfedea0SLionel Sambuc         return s->srp_ctx.g;
473ebfedea0SLionel Sambuc     return s->ctx->srp_ctx.g;
474ebfedea0SLionel Sambuc }
475ebfedea0SLionel Sambuc 
SSL_get_srp_N(SSL * s)476ebfedea0SLionel Sambuc BIGNUM *SSL_get_srp_N(SSL *s)
477ebfedea0SLionel Sambuc {
478ebfedea0SLionel Sambuc     if (s->srp_ctx.N != NULL)
479ebfedea0SLionel Sambuc         return s->srp_ctx.N;
480ebfedea0SLionel Sambuc     return s->ctx->srp_ctx.N;
481ebfedea0SLionel Sambuc }
482ebfedea0SLionel Sambuc 
SSL_get_srp_username(SSL * s)483ebfedea0SLionel Sambuc char *SSL_get_srp_username(SSL *s)
484ebfedea0SLionel Sambuc {
485ebfedea0SLionel Sambuc     if (s->srp_ctx.login != NULL)
486ebfedea0SLionel Sambuc         return s->srp_ctx.login;
487ebfedea0SLionel Sambuc     return s->ctx->srp_ctx.login;
488ebfedea0SLionel Sambuc }
489ebfedea0SLionel Sambuc 
SSL_get_srp_userinfo(SSL * s)490ebfedea0SLionel Sambuc char *SSL_get_srp_userinfo(SSL *s)
491ebfedea0SLionel Sambuc {
492ebfedea0SLionel Sambuc     if (s->srp_ctx.info != NULL)
493ebfedea0SLionel Sambuc         return s->srp_ctx.info;
494ebfedea0SLionel Sambuc     return s->ctx->srp_ctx.info;
495ebfedea0SLionel Sambuc }
496ebfedea0SLionel Sambuc 
497ebfedea0SLionel Sambuc # define tls1_ctx_ctrl ssl3_ctx_ctrl
498ebfedea0SLionel Sambuc # define tls1_ctx_callback_ctrl ssl3_ctx_callback_ctrl
499ebfedea0SLionel Sambuc 
SSL_CTX_set_srp_username(SSL_CTX * ctx,char * name)500ebfedea0SLionel Sambuc int SSL_CTX_set_srp_username(SSL_CTX *ctx, char *name)
501ebfedea0SLionel Sambuc {
502ebfedea0SLionel Sambuc     return tls1_ctx_ctrl(ctx, SSL_CTRL_SET_TLS_EXT_SRP_USERNAME, 0, name);
503ebfedea0SLionel Sambuc }
504ebfedea0SLionel Sambuc 
SSL_CTX_set_srp_password(SSL_CTX * ctx,char * password)505ebfedea0SLionel Sambuc int SSL_CTX_set_srp_password(SSL_CTX *ctx, char *password)
506ebfedea0SLionel Sambuc {
507ebfedea0SLionel Sambuc     return tls1_ctx_ctrl(ctx, SSL_CTRL_SET_TLS_EXT_SRP_PASSWORD, 0, password);
508ebfedea0SLionel Sambuc }
509ebfedea0SLionel Sambuc 
SSL_CTX_set_srp_strength(SSL_CTX * ctx,int strength)510ebfedea0SLionel Sambuc int SSL_CTX_set_srp_strength(SSL_CTX *ctx, int strength)
511ebfedea0SLionel Sambuc {
512ebfedea0SLionel Sambuc     return tls1_ctx_ctrl(ctx, SSL_CTRL_SET_TLS_EXT_SRP_STRENGTH, strength,
513ebfedea0SLionel Sambuc                          NULL);
514ebfedea0SLionel Sambuc }
515ebfedea0SLionel Sambuc 
SSL_CTX_set_srp_verify_param_callback(SSL_CTX * ctx,int (* cb)(SSL *,void *))516*0a6a1f1dSLionel Sambuc int SSL_CTX_set_srp_verify_param_callback(SSL_CTX *ctx,
517*0a6a1f1dSLionel Sambuc                                           int (*cb) (SSL *, void *))
518ebfedea0SLionel Sambuc {
519ebfedea0SLionel Sambuc     return tls1_ctx_callback_ctrl(ctx, SSL_CTRL_SET_SRP_VERIFY_PARAM_CB,
520ebfedea0SLionel Sambuc                                   (void (*)(void))cb);
521ebfedea0SLionel Sambuc }
522ebfedea0SLionel Sambuc 
SSL_CTX_set_srp_cb_arg(SSL_CTX * ctx,void * arg)523ebfedea0SLionel Sambuc int SSL_CTX_set_srp_cb_arg(SSL_CTX *ctx, void *arg)
524ebfedea0SLionel Sambuc {
525ebfedea0SLionel Sambuc     return tls1_ctx_ctrl(ctx, SSL_CTRL_SET_SRP_ARG, 0, arg);
526ebfedea0SLionel Sambuc }
527ebfedea0SLionel Sambuc 
SSL_CTX_set_srp_username_callback(SSL_CTX * ctx,int (* cb)(SSL *,int *,void *))528ebfedea0SLionel Sambuc int SSL_CTX_set_srp_username_callback(SSL_CTX *ctx,
529ebfedea0SLionel Sambuc                                       int (*cb) (SSL *, int *, void *))
530ebfedea0SLionel Sambuc {
531ebfedea0SLionel Sambuc     return tls1_ctx_callback_ctrl(ctx, SSL_CTRL_SET_TLS_EXT_SRP_USERNAME_CB,
532ebfedea0SLionel Sambuc                                   (void (*)(void))cb);
533ebfedea0SLionel Sambuc }
534ebfedea0SLionel Sambuc 
SSL_CTX_set_srp_client_pwd_callback(SSL_CTX * ctx,char * (* cb)(SSL *,void *))535*0a6a1f1dSLionel Sambuc int SSL_CTX_set_srp_client_pwd_callback(SSL_CTX *ctx,
536*0a6a1f1dSLionel Sambuc                                         char *(*cb) (SSL *, void *))
537ebfedea0SLionel Sambuc {
538ebfedea0SLionel Sambuc     return tls1_ctx_callback_ctrl(ctx, SSL_CTRL_SET_SRP_GIVE_CLIENT_PWD_CB,
539ebfedea0SLionel Sambuc                                   (void (*)(void))cb);
540ebfedea0SLionel Sambuc }
541ebfedea0SLionel Sambuc 
542ebfedea0SLionel Sambuc #endif
543