]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/asn1/x_pubkey.c
Remove temporary files when done.
[thirdparty/openssl.git] / crypto / asn1 / x_pubkey.c
CommitLineData
d02b48c6 1/* crypto/asn1/x_pubkey.c */
58964a49 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
d02b48c6
RE
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
9d6b1ce6 61#include <openssl/asn1t.h>
f0e8ae72 62#include <openssl/x509.h>
d02b48c6 63
9d6b1ce6
DSH
64/* Minor tweak to operation: free up EVP_PKEY */
65static int pubkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it)
66{
67 if(operation == ASN1_OP_FREE_POST) {
68 X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
69 EVP_PKEY_free(pubkey->pkey);
d02b48c6 70 }
9d6b1ce6
DSH
71 return 1;
72}
d02b48c6 73
9d6b1ce6
DSH
74ASN1_SEQUENCE_cb(X509_PUBKEY, pubkey_cb) = {
75 ASN1_SIMPLE(X509_PUBKEY, algor, X509_ALGOR),
76 ASN1_SIMPLE(X509_PUBKEY, public_key, ASN1_BIT_STRING)
77} ASN1_SEQUENCE_END_cb(X509_PUBKEY, X509_PUBKEY);
d02b48c6 78
9d6b1ce6 79IMPLEMENT_ASN1_FUNCTIONS(X509_PUBKEY)
d02b48c6 80
6b691a5c 81int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
d02b48c6
RE
82 {
83 int ok=0;
84 X509_PUBKEY *pk;
85 X509_ALGOR *a;
86 ASN1_OBJECT *o;
87 unsigned char *s,*p;
88 int i;
89
90 if (x == NULL) return(0);
91
92 if ((pk=X509_PUBKEY_new()) == NULL) goto err;
93 a=pk->algor;
94
95 /* set the algorithm id */
96 if ((o=OBJ_nid2obj(pkey->type)) == NULL) goto err;
97 ASN1_OBJECT_free(a->algorithm);
98 a->algorithm=o;
99
100 /* Set the parameter list */
101 if (!pkey->save_parameters || (pkey->type == EVP_PKEY_RSA))
102 {
103 if ((a->parameter == NULL) ||
104 (a->parameter->type != V_ASN1_NULL))
105 {
106 ASN1_TYPE_free(a->parameter);
107 a->parameter=ASN1_TYPE_new();
108 a->parameter->type=V_ASN1_NULL;
109 }
110 }
111 else
112#ifndef NO_DSA
113 if (pkey->type == EVP_PKEY_DSA)
114 {
115 unsigned char *pp;
116 DSA *dsa;
117
118 dsa=pkey->pkey.dsa;
119 dsa->write_params=0;
120 ASN1_TYPE_free(a->parameter);
121 i=i2d_DSAparams(dsa,NULL);
26a3a48d 122 p=(unsigned char *)OPENSSL_malloc(i);
d02b48c6
RE
123 pp=p;
124 i2d_DSAparams(dsa,&pp);
125 a->parameter=ASN1_TYPE_new();
126 a->parameter->type=V_ASN1_SEQUENCE;
127 a->parameter->value.sequence=ASN1_STRING_new();
128 ASN1_STRING_set(a->parameter->value.sequence,p,i);
26a3a48d 129 OPENSSL_free(p);
d02b48c6
RE
130 }
131 else
132#endif
133 {
134 X509err(X509_F_X509_PUBKEY_SET,X509_R_UNSUPPORTED_ALGORITHM);
135 goto err;
136 }
137
ad65ce75 138 if ((i=i2d_PublicKey(pkey,NULL)) <= 0) goto err;
26a3a48d 139 if ((s=(unsigned char *)OPENSSL_malloc(i+1)) == NULL) goto err;
d02b48c6
RE
140 p=s;
141 i2d_PublicKey(pkey,&p);
08e9c1af 142 if (!M_ASN1_BIT_STRING_set(pk->public_key,s,i)) goto err;
c35f549e
DSH
143 /* Set number of unused bits to zero */
144 pk->public_key->flags&= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07);
145 pk->public_key->flags|=ASN1_STRING_FLAG_BITS_LEFT;
146
26a3a48d 147 OPENSSL_free(s);
d02b48c6 148
e77066ea 149#if 0
d02b48c6
RE
150 CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY);
151 pk->pkey=pkey;
e77066ea 152#endif
d02b48c6
RE
153
154 if (*x != NULL)
155 X509_PUBKEY_free(*x);
156
157 *x=pk;
158 pk=NULL;
159
160 ok=1;
161err:
162 if (pk != NULL) X509_PUBKEY_free(pk);
163 return(ok);
164 }
165
6b691a5c 166EVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key)
d02b48c6
RE
167 {
168 EVP_PKEY *ret=NULL;
169 long j;
170 int type;
171 unsigned char *p;
a4aba800 172 const unsigned char *cp;
58964a49 173#ifndef NO_DSA
d02b48c6 174 X509_ALGOR *a;
58964a49
RE
175#endif
176
177 if (key == NULL) goto err;
178
cdbb8c2f
BL
179 if (key->pkey != NULL)
180 {
181 CRYPTO_add(&key->pkey->references,1,CRYPTO_LOCK_EVP_PKEY);
182 return(key->pkey);
183 }
58964a49
RE
184
185 if (key->public_key == NULL) goto err;
d02b48c6 186
d02b48c6
RE
187 type=OBJ_obj2nid(key->algor->algorithm);
188 p=key->public_key->data;
189 j=key->public_key->length;
190 if ((ret=d2i_PublicKey(type,NULL,&p,(long)j)) == NULL)
191 {
192 X509err(X509_F_X509_PUBKEY_GET,X509_R_ERR_ASN1_LIB);
193 goto err;
194 }
195 ret->save_parameters=0;
196
197#ifndef NO_DSA
198 a=key->algor;
199 if (ret->type == EVP_PKEY_DSA)
200 {
201 if (a->parameter->type == V_ASN1_SEQUENCE)
202 {
203 ret->pkey.dsa->write_params=0;
a4aba800 204 cp=p=a->parameter->value.sequence->data;
d02b48c6 205 j=a->parameter->value.sequence->length;
a4aba800 206 if (!d2i_DSAparams(&ret->pkey.dsa,&cp,(long)j))
d02b48c6
RE
207 goto err;
208 }
209 ret->save_parameters=1;
210 }
211#endif
212 key->pkey=ret;
cdbb8c2f 213 CRYPTO_add(&ret->references,1,CRYPTO_LOCK_EVP_PKEY);
d02b48c6
RE
214 return(ret);
215err:
216 if (ret != NULL)
217 EVP_PKEY_free(ret);
218 return(NULL);
219 }
220
52664f50
DSH
221/* Now two pseudo ASN1 routines that take an EVP_PKEY structure
222 * and encode or decode as X509_PUBKEY
223 */
224
225EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, unsigned char **pp,
226 long length)
227{
228 X509_PUBKEY *xpk;
229 EVP_PKEY *pktmp;
230 xpk = d2i_X509_PUBKEY(NULL, pp, length);
231 if(!xpk) return NULL;
232 pktmp = X509_PUBKEY_get(xpk);
233 X509_PUBKEY_free(xpk);
234 if(!pktmp) return NULL;
235 if(a) {
236 EVP_PKEY_free(*a);
237 *a = pktmp;
238 }
239 return pktmp;
240}
241
242int i2d_PUBKEY(EVP_PKEY *a, unsigned char **pp)
243{
244 X509_PUBKEY *xpk=NULL;
245 int ret;
246 if(!a) return 0;
247 if(!X509_PUBKEY_set(&xpk, a)) return 0;
248 ret = i2d_X509_PUBKEY(xpk, pp);
249 X509_PUBKEY_free(xpk);
250 return ret;
251}
252
253/* The following are equivalents but which return RSA and DSA
254 * keys
255 */
12aefe78 256#ifndef NO_RSA
52664f50
DSH
257RSA *d2i_RSA_PUBKEY(RSA **a, unsigned char **pp,
258 long length)
259{
260 EVP_PKEY *pkey;
261 RSA *key;
262 unsigned char *q;
263 q = *pp;
264 pkey = d2i_PUBKEY(NULL, &q, length);
265 if(!pkey) return NULL;
c7cb16a8 266 key = EVP_PKEY_get1_RSA(pkey);
52664f50
DSH
267 EVP_PKEY_free(pkey);
268 if(!key) return NULL;
269 *pp = q;
270 if(a) {
271 RSA_free(*a);
272 *a = key;
273 }
274 return key;
275}
276
277int i2d_RSA_PUBKEY(RSA *a, unsigned char **pp)
278{
279 EVP_PKEY *pktmp;
280 int ret;
281 if(!a) return 0;
282 pktmp = EVP_PKEY_new();
283 if(!pktmp) {
284 ASN1err(ASN1_F_I2D_RSA_PUBKEY, ERR_R_MALLOC_FAILURE);
285 return 0;
286 }
c7cb16a8 287 EVP_PKEY_set1_RSA(pktmp, a);
52664f50
DSH
288 ret = i2d_PUBKEY(pktmp, pp);
289 EVP_PKEY_free(pktmp);
290 return ret;
291}
12aefe78 292#endif
52664f50 293
12aefe78 294#ifndef NO_DSA
52664f50
DSH
295DSA *d2i_DSA_PUBKEY(DSA **a, unsigned char **pp,
296 long length)
297{
298 EVP_PKEY *pkey;
299 DSA *key;
300 unsigned char *q;
301 q = *pp;
302 pkey = d2i_PUBKEY(NULL, &q, length);
303 if(!pkey) return NULL;
c7cb16a8 304 key = EVP_PKEY_get1_DSA(pkey);
52664f50
DSH
305 EVP_PKEY_free(pkey);
306 if(!key) return NULL;
307 *pp = q;
308 if(a) {
309 DSA_free(*a);
310 *a = key;
311 }
312 return key;
313}
314
315int i2d_DSA_PUBKEY(DSA *a, unsigned char **pp)
316{
317 EVP_PKEY *pktmp;
318 int ret;
319 if(!a) return 0;
320 pktmp = EVP_PKEY_new();
321 if(!pktmp) {
322 ASN1err(ASN1_F_I2D_DSA_PUBKEY, ERR_R_MALLOC_FAILURE);
323 return 0;
324 }
c7cb16a8 325 EVP_PKEY_set1_DSA(pktmp, a);
52664f50
DSH
326 ret = i2d_PUBKEY(pktmp, pp);
327 EVP_PKEY_free(pktmp);
328 return ret;
329}
12aefe78 330#endif