Path: blob/main/crypto/openssl/apps/lib/tlssrp_depr.c
34890 views
/*1* Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved.2* Copyright 2005 Nokia. All rights reserved.3*4* Licensed under the Apache License 2.0 (the "License"). You may not use5* this file except in compliance with the License. You can obtain a copy6* in the file LICENSE in the source distribution or at7* https://www.openssl.org/source/license.html8*/910/*11* This file is to enable backwards compatibility for the SRP features of12* s_client, s_server and ciphers. All of those features are deprecated and will13* eventually disappear. In the meantime, to continue to support them, we14* need to access deprecated SRP APIs.15*/16#define OPENSSL_SUPPRESS_DEPRECATED1718#include <openssl/bn.h>19#include <openssl/bio.h>20#include <openssl/ssl.h>21#include <openssl/srp.h>22#include "apps_ui.h"23#include "apps.h"24#include "s_apps.h"2526static int srp_Verify_N_and_g(const BIGNUM *N, const BIGNUM *g)27{28BN_CTX *bn_ctx = BN_CTX_new();29BIGNUM *p = BN_new();30BIGNUM *r = BN_new();31int ret =32g != NULL && N != NULL && bn_ctx != NULL && BN_is_odd(N) &&33BN_check_prime(N, bn_ctx, NULL) == 1 &&34p != NULL && BN_rshift1(p, N) &&35/* p = (N-1)/2 */36BN_check_prime(p, bn_ctx, NULL) == 1 &&37r != NULL &&38/* verify g^((N-1)/2) == -1 (mod N) */39BN_mod_exp(r, g, p, N, bn_ctx) &&40BN_add_word(r, 1) && BN_cmp(r, N) == 0;4142BN_free(r);43BN_free(p);44BN_CTX_free(bn_ctx);45return ret;46}4748/*-49* This callback is used here for two purposes:50* - extended debugging51* - making some primality tests for unknown groups52* The callback is only called for a non default group.53*54* An application does not need the call back at all if55* only the standard groups are used. In real life situations,56* client and server already share well known groups,57* thus there is no need to verify them.58* Furthermore, in case that a server actually proposes a group that59* is not one of those defined in RFC 5054, it is more appropriate60* to add the group to a static list and then compare since61* primality tests are rather cpu consuming.62*/6364static int ssl_srp_verify_param_cb(SSL *s, void *arg)65{66SRP_ARG *srp_arg = (SRP_ARG *)arg;67BIGNUM *N = NULL, *g = NULL;6869if (((N = SSL_get_srp_N(s)) == NULL) || ((g = SSL_get_srp_g(s)) == NULL))70return 0;71if (srp_arg->debug || srp_arg->msg || srp_arg->amp == 1) {72BIO_printf(bio_err, "SRP parameters:\n");73BIO_printf(bio_err, "\tN=");74BN_print(bio_err, N);75BIO_printf(bio_err, "\n\tg=");76BN_print(bio_err, g);77BIO_printf(bio_err, "\n");78}7980if (SRP_check_known_gN_param(g, N))81return 1;8283if (srp_arg->amp == 1) {84if (srp_arg->debug)85BIO_printf(bio_err,86"SRP param N and g are not known params, going to check deeper.\n");8788/*89* The srp_moregroups is a real debugging feature. Implementers90* should rather add the value to the known ones. The minimal size91* has already been tested.92*/93if (BN_num_bits(g) <= BN_BITS && srp_Verify_N_and_g(N, g))94return 1;95}96BIO_printf(bio_err, "SRP param N and g rejected.\n");97return 0;98}99100#define PWD_STRLEN 1024101102static char *ssl_give_srp_client_pwd_cb(SSL *s, void *arg)103{104SRP_ARG *srp_arg = (SRP_ARG *)arg;105char *pass = app_malloc(PWD_STRLEN + 1, "SRP password buffer");106PW_CB_DATA cb_tmp;107int l;108109cb_tmp.password = (char *)srp_arg->srppassin;110cb_tmp.prompt_info = "SRP user";111if ((l = password_callback(pass, PWD_STRLEN, 0, &cb_tmp)) < 0) {112BIO_printf(bio_err, "Can't read Password\n");113OPENSSL_free(pass);114return NULL;115}116*(pass + l) = '\0';117118return pass;119}120121int set_up_srp_arg(SSL_CTX *ctx, SRP_ARG *srp_arg, int srp_lateuser, int c_msg,122int c_debug)123{124if (!srp_lateuser && !SSL_CTX_set_srp_username(ctx, srp_arg->srplogin)) {125BIO_printf(bio_err, "Unable to set SRP username\n");126return 0;127}128srp_arg->msg = c_msg;129srp_arg->debug = c_debug;130SSL_CTX_set_srp_cb_arg(ctx, &srp_arg);131SSL_CTX_set_srp_client_pwd_callback(ctx, ssl_give_srp_client_pwd_cb);132SSL_CTX_set_srp_strength(ctx, srp_arg->strength);133if (c_msg || c_debug || srp_arg->amp == 0)134SSL_CTX_set_srp_verify_param_callback(ctx, ssl_srp_verify_param_cb);135136return 1;137}138139static char *dummy_srp(SSL *ssl, void *arg)140{141return "";142}143144void set_up_dummy_srp(SSL_CTX *ctx)145{146SSL_CTX_set_srp_client_pwd_callback(ctx, dummy_srp);147}148149/*150* This callback pretends to require some asynchronous logic in order to151* obtain a verifier. When the callback is called for a new connection we152* return with a negative value. This will provoke the accept etc to return153* with an LOOKUP_X509. The main logic of the reinvokes the suspended call154* (which would normally occur after a worker has finished) and we set the155* user parameters.156*/157static int ssl_srp_server_param_cb(SSL *s, int *ad, void *arg)158{159srpsrvparm *p = (srpsrvparm *) arg;160int ret = SSL3_AL_FATAL;161162if (p->login == NULL && p->user == NULL) {163p->login = SSL_get_srp_username(s);164BIO_printf(bio_err, "SRP username = \"%s\"\n", p->login);165return -1;166}167168if (p->user == NULL) {169BIO_printf(bio_err, "User %s doesn't exist\n", p->login);170goto err;171}172173if (SSL_set_srp_server_param174(s, p->user->N, p->user->g, p->user->s, p->user->v,175p->user->info) < 0) {176*ad = SSL_AD_INTERNAL_ERROR;177goto err;178}179BIO_printf(bio_err,180"SRP parameters set: username = \"%s\" info=\"%s\"\n",181p->login, p->user->info);182ret = SSL_ERROR_NONE;183184err:185SRP_user_pwd_free(p->user);186p->user = NULL;187p->login = NULL;188return ret;189}190191int set_up_srp_verifier_file(SSL_CTX *ctx, srpsrvparm *srp_callback_parm,192char *srpuserseed, char *srp_verifier_file)193{194int ret;195196srp_callback_parm->vb = SRP_VBASE_new(srpuserseed);197srp_callback_parm->user = NULL;198srp_callback_parm->login = NULL;199200if (srp_callback_parm->vb == NULL) {201BIO_printf(bio_err, "Failed to initialize SRP verifier file\n");202return 0;203}204if ((ret =205SRP_VBASE_init(srp_callback_parm->vb,206srp_verifier_file)) != SRP_NO_ERROR) {207BIO_printf(bio_err,208"Cannot initialize SRP verifier file \"%s\":ret=%d\n",209srp_verifier_file, ret);210return 0;211}212SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, verify_callback);213SSL_CTX_set_srp_cb_arg(ctx, &srp_callback_parm);214SSL_CTX_set_srp_username_callback(ctx, ssl_srp_server_param_cb);215216return 1;217}218219void lookup_srp_user(srpsrvparm *srp_callback_parm, BIO *bio_s_out)220{221SRP_user_pwd_free(srp_callback_parm->user);222srp_callback_parm->user = SRP_VBASE_get1_by_user(srp_callback_parm->vb,223srp_callback_parm->login);224225if (srp_callback_parm->user != NULL)226BIO_printf(bio_s_out, "LOOKUP done %s\n",227srp_callback_parm->user->info);228else229BIO_printf(bio_s_out, "LOOKUP not successful\n");230}231232233