summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/rsa/rsa_lib.c
diff options
context:
space:
mode:
authormiod <>2014-07-09 08:20:08 +0000
committermiod <>2014-07-09 08:20:08 +0000
commit8cbe58f0d357b14b0ce292d336469d0554a567bc (patch)
tree07872a7ef59da8cea3b3b4a101fa3580e4d658c0 /src/lib/libcrypto/rsa/rsa_lib.c
parentbc1209e388500a20f5e75cab35d1b543ce0bbe74 (diff)
downloadopenbsd-8cbe58f0d357b14b0ce292d336469d0554a567bc.tar.gz
openbsd-8cbe58f0d357b14b0ce292d336469d0554a567bc.tar.bz2
openbsd-8cbe58f0d357b14b0ce292d336469d0554a567bc.zip
KNF
Diffstat (limited to 'src/lib/libcrypto/rsa/rsa_lib.c')
-rw-r--r--src/lib/libcrypto/rsa/rsa_lib.c243
1 files changed, 129 insertions, 114 deletions
diff --git a/src/lib/libcrypto/rsa/rsa_lib.c b/src/lib/libcrypto/rsa/rsa_lib.c
index 05eb53cc83..054690cee6 100644
--- a/src/lib/libcrypto/rsa/rsa_lib.c
+++ b/src/lib/libcrypto/rsa/rsa_lib.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: rsa_lib.c,v 1.20 2014/06/12 15:49:30 deraadt Exp $ */ 1/* $OpenBSD: rsa_lib.c,v 1.21 2014/07/09 08:20:08 miod Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -67,148 +67,150 @@
67#include <openssl/engine.h> 67#include <openssl/engine.h>
68#endif 68#endif
69 69
70const char RSA_version[]="RSA" OPENSSL_VERSION_PTEXT; 70const char RSA_version[] = "RSA" OPENSSL_VERSION_PTEXT;
71 71
72static const RSA_METHOD *default_RSA_meth=NULL; 72static const RSA_METHOD *default_RSA_meth = NULL;
73 73
74RSA *RSA_new(void) 74RSA *
75 { 75RSA_new(void)
76 RSA *r=RSA_new_method(NULL); 76{
77 RSA *r = RSA_new_method(NULL);
77 78
78 return r; 79 return r;
79 } 80}
80 81
81void RSA_set_default_method(const RSA_METHOD *meth) 82void
82 { 83RSA_set_default_method(const RSA_METHOD *meth)
84{
83 default_RSA_meth = meth; 85 default_RSA_meth = meth;
84 } 86}
85 87
86const RSA_METHOD *RSA_get_default_method(void) 88const RSA_METHOD *
87 { 89RSA_get_default_method(void)
88 if (default_RSA_meth == NULL) 90{
89 { 91 if (default_RSA_meth == NULL) {
90#ifdef RSA_NULL 92#ifdef RSA_NULL
91 default_RSA_meth=RSA_null_method(); 93 default_RSA_meth = RSA_null_method();
92#else 94#else
93 default_RSA_meth=RSA_PKCS1_SSLeay(); 95 default_RSA_meth = RSA_PKCS1_SSLeay();
94#endif 96#endif
95 } 97 }
96 98
97 return default_RSA_meth; 99 return default_RSA_meth;
98 } 100}
99 101
100const RSA_METHOD *RSA_get_method(const RSA *rsa) 102const RSA_METHOD *
101 { 103RSA_get_method(const RSA *rsa)
104{
102 return rsa->meth; 105 return rsa->meth;
103 } 106}
104 107
105int RSA_set_method(RSA *rsa, const RSA_METHOD *meth) 108int
106 { 109RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
107 /* NB: The caller is specifically setting a method, so it's not up to us 110{
108 * to deal with which ENGINE it comes from. */ 111 /*
112 * NB: The caller is specifically setting a method, so it's not up to us
113 * to deal with which ENGINE it comes from.
114 */
109 const RSA_METHOD *mtmp; 115 const RSA_METHOD *mtmp;
116
110 mtmp = rsa->meth; 117 mtmp = rsa->meth;
111 if (mtmp->finish) mtmp->finish(rsa); 118 if (mtmp->finish)
119 mtmp->finish(rsa);
112#ifndef OPENSSL_NO_ENGINE 120#ifndef OPENSSL_NO_ENGINE
113 if (rsa->engine) 121 if (rsa->engine) {
114 {
115 ENGINE_finish(rsa->engine); 122 ENGINE_finish(rsa->engine);
116 rsa->engine = NULL; 123 rsa->engine = NULL;
117 } 124 }
118#endif 125#endif
119 rsa->meth = meth; 126 rsa->meth = meth;
120 if (meth->init) meth->init(rsa); 127 if (meth->init)
128 meth->init(rsa);
121 return 1; 129 return 1;
122 } 130}
123 131
124RSA *RSA_new_method(ENGINE *engine) 132RSA *
125 { 133RSA_new_method(ENGINE *engine)
134{
126 RSA *ret; 135 RSA *ret;
127 136
128 ret = malloc(sizeof(RSA)); 137 ret = malloc(sizeof(RSA));
129 if (ret == NULL) 138 if (ret == NULL) {
130 { 139 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
131 RSAerr(RSA_F_RSA_NEW_METHOD,ERR_R_MALLOC_FAILURE);
132 return NULL; 140 return NULL;
133 } 141 }
134 142
135 ret->meth = RSA_get_default_method(); 143 ret->meth = RSA_get_default_method();
136#ifndef OPENSSL_NO_ENGINE 144#ifndef OPENSSL_NO_ENGINE
137 if (engine) 145 if (engine) {
138 { 146 if (!ENGINE_init(engine)) {
139 if (!ENGINE_init(engine))
140 {
141 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB); 147 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
142 free(ret); 148 free(ret);
143 return NULL; 149 return NULL;
144 }
145 ret->engine = engine;
146 } 150 }
147 else 151 ret->engine = engine;
152 } else
148 ret->engine = ENGINE_get_default_RSA(); 153 ret->engine = ENGINE_get_default_RSA();
149 if(ret->engine) 154 if(ret->engine) {
150 {
151 ret->meth = ENGINE_get_RSA(ret->engine); 155 ret->meth = ENGINE_get_RSA(ret->engine);
152 if(!ret->meth) 156 if (!ret->meth) {
153 { 157 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
154 RSAerr(RSA_F_RSA_NEW_METHOD,
155 ERR_R_ENGINE_LIB);
156 ENGINE_finish(ret->engine); 158 ENGINE_finish(ret->engine);
157 free(ret); 159 free(ret);
158 return NULL; 160 return NULL;
159 }
160 } 161 }
162 }
161#endif 163#endif
162 164
163 ret->pad=0; 165 ret->pad = 0;
164 ret->version=0; 166 ret->version = 0;
165 ret->n=NULL; 167 ret->n = NULL;
166 ret->e=NULL; 168 ret->e = NULL;
167 ret->d=NULL; 169 ret->d = NULL;
168 ret->p=NULL; 170 ret->p = NULL;
169 ret->q=NULL; 171 ret->q = NULL;
170 ret->dmp1=NULL; 172 ret->dmp1 = NULL;
171 ret->dmq1=NULL; 173 ret->dmq1 = NULL;
172 ret->iqmp=NULL; 174 ret->iqmp = NULL;
173 ret->references=1; 175 ret->references = 1;
174 ret->_method_mod_n=NULL; 176 ret->_method_mod_n = NULL;
175 ret->_method_mod_p=NULL; 177 ret->_method_mod_p = NULL;
176 ret->_method_mod_q=NULL; 178 ret->_method_mod_q = NULL;
177 ret->blinding=NULL; 179 ret->blinding = NULL;
178 ret->mt_blinding=NULL; 180 ret->mt_blinding = NULL;
179 ret->bignum_data=NULL; 181 ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
180 ret->flags=ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW; 182 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) {
181 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data))
182 {
183#ifndef OPENSSL_NO_ENGINE 183#ifndef OPENSSL_NO_ENGINE
184 if (ret->engine) 184 if (ret->engine)
185 ENGINE_finish(ret->engine); 185 ENGINE_finish(ret->engine);
186#endif 186#endif
187 free(ret); 187 free(ret);
188 return(NULL); 188 return NULL;
189 } 189 }
190 190
191 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) 191 if (ret->meth->init != NULL && !ret->meth->init(ret)) {
192 {
193#ifndef OPENSSL_NO_ENGINE 192#ifndef OPENSSL_NO_ENGINE
194 if (ret->engine) 193 if (ret->engine)
195 ENGINE_finish(ret->engine); 194 ENGINE_finish(ret->engine);
196#endif 195#endif
197 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data); 196 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);
198 free(ret); 197 free(ret);
199 ret=NULL; 198 ret = NULL;
200 }
201 return(ret);
202 } 199 }
200 return ret;
201}
203 202
204void RSA_free(RSA *r) 203void
205 { 204RSA_free(RSA *r)
205{
206 int i; 206 int i;
207 207
208 if (r == NULL) return; 208 if (r == NULL)
209 return;
209 210
210 i=CRYPTO_add(&r->references,-1,CRYPTO_LOCK_RSA); 211 i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_RSA);
211 if (i > 0) return; 212 if (i > 0)
213 return;
212 214
213 if (r->meth->finish) 215 if (r->meth->finish)
214 r->meth->finish(r); 216 r->meth->finish(r);
@@ -219,42 +221,55 @@ void RSA_free(RSA *r)
219 221
220 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data); 222 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
221 223
222 if (r->n != NULL) BN_clear_free(r->n); 224 if (r->n != NULL)
223 if (r->e != NULL) BN_clear_free(r->e); 225 BN_clear_free(r->n);
224 if (r->d != NULL) BN_clear_free(r->d); 226 if (r->e != NULL)
225 if (r->p != NULL) BN_clear_free(r->p); 227 BN_clear_free(r->e);
226 if (r->q != NULL) BN_clear_free(r->q); 228 if (r->d != NULL)
227 if (r->dmp1 != NULL) BN_clear_free(r->dmp1); 229 BN_clear_free(r->d);
228 if (r->dmq1 != NULL) BN_clear_free(r->dmq1); 230 if (r->p != NULL)
229 if (r->iqmp != NULL) BN_clear_free(r->iqmp); 231 BN_clear_free(r->p);
230 if (r->blinding != NULL) BN_BLINDING_free(r->blinding); 232 if (r->q != NULL)
231 if (r->mt_blinding != NULL) BN_BLINDING_free(r->mt_blinding); 233 BN_clear_free(r->q);
232 free(r->bignum_data); 234 if (r->dmp1 != NULL)
235 BN_clear_free(r->dmp1);
236 if (r->dmq1 != NULL)
237 BN_clear_free(r->dmq1);
238 if (r->iqmp != NULL)
239 BN_clear_free(r->iqmp);
240 if (r->blinding != NULL)
241 BN_BLINDING_free(r->blinding);
242 if (r->mt_blinding != NULL)
243 BN_BLINDING_free(r->mt_blinding);
233 free(r); 244 free(r);
234 } 245}
235 246
236int RSA_up_ref(RSA *r) 247int
237 { 248RSA_up_ref(RSA *r)
249{
238 int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_RSA); 250 int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_RSA);
239 return ((i > 1) ? 1 : 0); 251 return i > 1 ? 1 : 0;
240 } 252}
241 253
242int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, 254int
243 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) 255RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
244 { 256 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
257{
245 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_RSA, argl, argp, 258 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_RSA, argl, argp,
246 new_func, dup_func, free_func); 259 new_func, dup_func, free_func);
247 } 260}
248 261
249int RSA_set_ex_data(RSA *r, int idx, void *arg) 262int
250 { 263RSA_set_ex_data(RSA *r, int idx, void *arg)
251 return(CRYPTO_set_ex_data(&r->ex_data,idx,arg)); 264{
252 } 265 return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
266}
253 267
254void *RSA_get_ex_data(const RSA *r, int idx) 268void *
255 { 269RSA_get_ex_data(const RSA *r, int idx)
256 return(CRYPTO_get_ex_data(&r->ex_data,idx)); 270{
257 } 271 return CRYPTO_get_ex_data(&r->ex_data, idx);
272}
258 273
259int RSA_memory_lock(RSA *r) 274int RSA_memory_lock(RSA *r)
260 { 275 {