xref: /freebsd/crypto/openssl/crypto/dh/dh_meth.c (revision f25b8c9fb4f58cf61adb47d7570abe7caa6d385d)
1 /*
2  * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 
10 /*
11  * DH low level APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15 
16 #include "dh_local.h"
17 #include <string.h>
18 #include <openssl/err.h>
19 
DH_meth_new(const char * name,int flags)20 DH_METHOD *DH_meth_new(const char *name, int flags)
21 {
22     DH_METHOD *dhm = OPENSSL_zalloc(sizeof(*dhm));
23 
24     if (dhm != NULL) {
25         dhm->flags = flags;
26 
27         dhm->name = OPENSSL_strdup(name);
28         if (dhm->name != NULL)
29             return dhm;
30 
31         OPENSSL_free(dhm);
32     }
33 
34     return NULL;
35 }
36 
DH_meth_free(DH_METHOD * dhm)37 void DH_meth_free(DH_METHOD *dhm)
38 {
39     if (dhm != NULL) {
40         OPENSSL_free(dhm->name);
41         OPENSSL_free(dhm);
42     }
43 }
44 
DH_meth_dup(const DH_METHOD * dhm)45 DH_METHOD *DH_meth_dup(const DH_METHOD *dhm)
46 {
47     DH_METHOD *ret = OPENSSL_malloc(sizeof(*ret));
48 
49     if (ret != NULL) {
50         memcpy(ret, dhm, sizeof(*dhm));
51 
52         ret->name = OPENSSL_strdup(dhm->name);
53         if (ret->name != NULL)
54             return ret;
55 
56         OPENSSL_free(ret);
57     }
58 
59     return NULL;
60 }
61 
DH_meth_get0_name(const DH_METHOD * dhm)62 const char *DH_meth_get0_name(const DH_METHOD *dhm)
63 {
64     return dhm->name;
65 }
66 
DH_meth_set1_name(DH_METHOD * dhm,const char * name)67 int DH_meth_set1_name(DH_METHOD *dhm, const char *name)
68 {
69     char *tmpname = OPENSSL_strdup(name);
70 
71     if (tmpname == NULL)
72         return 0;
73 
74     OPENSSL_free(dhm->name);
75     dhm->name = tmpname;
76 
77     return 1;
78 }
79 
DH_meth_get_flags(const DH_METHOD * dhm)80 int DH_meth_get_flags(const DH_METHOD *dhm)
81 {
82     return dhm->flags;
83 }
84 
DH_meth_set_flags(DH_METHOD * dhm,int flags)85 int DH_meth_set_flags(DH_METHOD *dhm, int flags)
86 {
87     dhm->flags = flags;
88     return 1;
89 }
90 
DH_meth_get0_app_data(const DH_METHOD * dhm)91 void *DH_meth_get0_app_data(const DH_METHOD *dhm)
92 {
93     return dhm->app_data;
94 }
95 
DH_meth_set0_app_data(DH_METHOD * dhm,void * app_data)96 int DH_meth_set0_app_data(DH_METHOD *dhm, void *app_data)
97 {
98     dhm->app_data = app_data;
99     return 1;
100 }
101 
DH_meth_get_generate_key(const DH_METHOD * dhm)102 int (*DH_meth_get_generate_key(const DH_METHOD *dhm))(DH *)
103 {
104     return dhm->generate_key;
105 }
106 
DH_meth_set_generate_key(DH_METHOD * dhm,int (* generate_key)(DH *))107 int DH_meth_set_generate_key(DH_METHOD *dhm, int (*generate_key)(DH *))
108 {
109     dhm->generate_key = generate_key;
110     return 1;
111 }
112 
DH_meth_get_compute_key(const DH_METHOD * dhm)113 int (*DH_meth_get_compute_key(const DH_METHOD *dhm))(unsigned char *key, const BIGNUM *pub_key, DH *dh)
114 {
115     return dhm->compute_key;
116 }
117 
DH_meth_set_compute_key(DH_METHOD * dhm,int (* compute_key)(unsigned char * key,const BIGNUM * pub_key,DH * dh))118 int DH_meth_set_compute_key(DH_METHOD *dhm,
119     int (*compute_key)(unsigned char *key, const BIGNUM *pub_key, DH *dh))
120 {
121     dhm->compute_key = compute_key;
122     return 1;
123 }
124 
DH_meth_get_bn_mod_exp(const DH_METHOD * dhm)125 int (*DH_meth_get_bn_mod_exp(const DH_METHOD *dhm))(const DH *, BIGNUM *, const BIGNUM *, const BIGNUM *, const BIGNUM *,
126     BN_CTX *, BN_MONT_CTX *)
127 {
128     return dhm->bn_mod_exp;
129 }
130 
DH_meth_set_bn_mod_exp(DH_METHOD * dhm,int (* bn_mod_exp)(const DH *,BIGNUM *,const BIGNUM *,const BIGNUM *,const BIGNUM *,BN_CTX *,BN_MONT_CTX *))131 int DH_meth_set_bn_mod_exp(DH_METHOD *dhm,
132     int (*bn_mod_exp)(const DH *, BIGNUM *, const BIGNUM *, const BIGNUM *,
133         const BIGNUM *, BN_CTX *, BN_MONT_CTX *))
134 {
135     dhm->bn_mod_exp = bn_mod_exp;
136     return 1;
137 }
138 
DH_meth_get_init(const DH_METHOD * dhm)139 int (*DH_meth_get_init(const DH_METHOD *dhm))(DH *)
140 {
141     return dhm->init;
142 }
143 
DH_meth_set_init(DH_METHOD * dhm,int (* init)(DH *))144 int DH_meth_set_init(DH_METHOD *dhm, int (*init)(DH *))
145 {
146     dhm->init = init;
147     return 1;
148 }
149 
DH_meth_get_finish(const DH_METHOD * dhm)150 int (*DH_meth_get_finish(const DH_METHOD *dhm))(DH *)
151 {
152     return dhm->finish;
153 }
154 
DH_meth_set_finish(DH_METHOD * dhm,int (* finish)(DH *))155 int DH_meth_set_finish(DH_METHOD *dhm, int (*finish)(DH *))
156 {
157     dhm->finish = finish;
158     return 1;
159 }
160 
DH_meth_get_generate_params(const DH_METHOD * dhm)161 int (*DH_meth_get_generate_params(const DH_METHOD *dhm))(DH *, int, int, BN_GENCB *)
162 {
163     return dhm->generate_params;
164 }
165 
DH_meth_set_generate_params(DH_METHOD * dhm,int (* generate_params)(DH *,int,int,BN_GENCB *))166 int DH_meth_set_generate_params(DH_METHOD *dhm,
167     int (*generate_params)(DH *, int, int, BN_GENCB *))
168 {
169     dhm->generate_params = generate_params;
170     return 1;
171 }
172