summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/asn1/x_pubkey.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/asn1/x_pubkey.c')
-rw-r--r--src/lib/libcrypto/asn1/x_pubkey.c118
1 files changed, 114 insertions, 4 deletions
diff --git a/src/lib/libcrypto/asn1/x_pubkey.c b/src/lib/libcrypto/asn1/x_pubkey.c
index 4ac32c59dd..81e9815222 100644
--- a/src/lib/libcrypto/asn1/x_pubkey.c
+++ b/src/lib/libcrypto/asn1/x_pubkey.c
@@ -100,7 +100,7 @@ X509_PUBKEY *X509_PUBKEY_new(void)
100 100
101 M_ASN1_New_Malloc(ret,X509_PUBKEY); 101 M_ASN1_New_Malloc(ret,X509_PUBKEY);
102 M_ASN1_New(ret->algor,X509_ALGOR_new); 102 M_ASN1_New(ret->algor,X509_ALGOR_new);
103 M_ASN1_New(ret->public_key,ASN1_BIT_STRING_new); 103 M_ASN1_New(ret->public_key,M_ASN1_BIT_STRING_new);
104 ret->pkey=NULL; 104 ret->pkey=NULL;
105 return(ret); 105 return(ret);
106 M_ASN1_New_Error(ASN1_F_X509_PUBKEY_NEW); 106 M_ASN1_New_Error(ASN1_F_X509_PUBKEY_NEW);
@@ -110,9 +110,9 @@ void X509_PUBKEY_free(X509_PUBKEY *a)
110 { 110 {
111 if (a == NULL) return; 111 if (a == NULL) return;
112 X509_ALGOR_free(a->algor); 112 X509_ALGOR_free(a->algor);
113 ASN1_BIT_STRING_free(a->public_key); 113 M_ASN1_BIT_STRING_free(a->public_key);
114 if (a->pkey != NULL) EVP_PKEY_free(a->pkey); 114 if (a->pkey != NULL) EVP_PKEY_free(a->pkey);
115 Free((char *)a); 115 Free(a);
116 } 116 }
117 117
118int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey) 118int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
@@ -176,7 +176,7 @@ int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
176 if ((s=(unsigned char *)Malloc(i+1)) == NULL) goto err; 176 if ((s=(unsigned char *)Malloc(i+1)) == NULL) goto err;
177 p=s; 177 p=s;
178 i2d_PublicKey(pkey,&p); 178 i2d_PublicKey(pkey,&p);
179 if (!ASN1_BIT_STRING_set(pk->public_key,s,i)) goto err; 179 if (!M_ASN1_BIT_STRING_set(pk->public_key,s,i)) goto err;
180 /* Set number of unused bits to zero */ 180 /* Set number of unused bits to zero */
181 pk->public_key->flags&= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07); 181 pk->public_key->flags&= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07);
182 pk->public_key->flags|=ASN1_STRING_FLAG_BITS_LEFT; 182 pk->public_key->flags|=ASN1_STRING_FLAG_BITS_LEFT;
@@ -252,3 +252,113 @@ err:
252 return(NULL); 252 return(NULL);
253 } 253 }
254 254
255/* Now two pseudo ASN1 routines that take an EVP_PKEY structure
256 * and encode or decode as X509_PUBKEY
257 */
258
259EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, unsigned char **pp,
260 long length)
261{
262 X509_PUBKEY *xpk;
263 EVP_PKEY *pktmp;
264 xpk = d2i_X509_PUBKEY(NULL, pp, length);
265 if(!xpk) return NULL;
266 pktmp = X509_PUBKEY_get(xpk);
267 X509_PUBKEY_free(xpk);
268 if(!pktmp) return NULL;
269 if(a) {
270 EVP_PKEY_free(*a);
271 *a = pktmp;
272 }
273 return pktmp;
274}
275
276int i2d_PUBKEY(EVP_PKEY *a, unsigned char **pp)
277{
278 X509_PUBKEY *xpk=NULL;
279 int ret;
280 if(!a) return 0;
281 if(!X509_PUBKEY_set(&xpk, a)) return 0;
282 ret = i2d_X509_PUBKEY(xpk, pp);
283 X509_PUBKEY_free(xpk);
284 return ret;
285}
286
287/* The following are equivalents but which return RSA and DSA
288 * keys
289 */
290#ifndef NO_RSA
291RSA *d2i_RSA_PUBKEY(RSA **a, unsigned char **pp,
292 long length)
293{
294 EVP_PKEY *pkey;
295 RSA *key;
296 unsigned char *q;
297 q = *pp;
298 pkey = d2i_PUBKEY(NULL, &q, length);
299 if(!pkey) return NULL;
300 key = EVP_PKEY_get1_RSA(pkey);
301 EVP_PKEY_free(pkey);
302 if(!key) return NULL;
303 *pp = q;
304 if(a) {
305 RSA_free(*a);
306 *a = key;
307 }
308 return key;
309}
310
311int i2d_RSA_PUBKEY(RSA *a, unsigned char **pp)
312{
313 EVP_PKEY *pktmp;
314 int ret;
315 if(!a) return 0;
316 pktmp = EVP_PKEY_new();
317 if(!pktmp) {
318 ASN1err(ASN1_F_I2D_RSA_PUBKEY, ERR_R_MALLOC_FAILURE);
319 return 0;
320 }
321 EVP_PKEY_set1_RSA(pktmp, a);
322 ret = i2d_PUBKEY(pktmp, pp);
323 EVP_PKEY_free(pktmp);
324 return ret;
325}
326#endif
327
328#ifndef NO_DSA
329DSA *d2i_DSA_PUBKEY(DSA **a, unsigned char **pp,
330 long length)
331{
332 EVP_PKEY *pkey;
333 DSA *key;
334 unsigned char *q;
335 q = *pp;
336 pkey = d2i_PUBKEY(NULL, &q, length);
337 if(!pkey) return NULL;
338 key = EVP_PKEY_get1_DSA(pkey);
339 EVP_PKEY_free(pkey);
340 if(!key) return NULL;
341 *pp = q;
342 if(a) {
343 DSA_free(*a);
344 *a = key;
345 }
346 return key;
347}
348
349int i2d_DSA_PUBKEY(DSA *a, unsigned char **pp)
350{
351 EVP_PKEY *pktmp;
352 int ret;
353 if(!a) return 0;
354 pktmp = EVP_PKEY_new();
355 if(!pktmp) {
356 ASN1err(ASN1_F_I2D_DSA_PUBKEY, ERR_R_MALLOC_FAILURE);
357 return 0;
358 }
359 EVP_PKEY_set1_DSA(pktmp, a);
360 ret = i2d_PUBKEY(pktmp, pp);
361 EVP_PKEY_free(pktmp);
362 return ret;
363}
364#endif