A few train of thoughts about the build procedure mess
[openssl.git] / crypto / evp / p_lib.c
1 /* crypto/evp/p_lib.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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"
61 #include "objects.h"
62 #include "evp.h"
63 #include "asn1_mac.h"
64 #include "x509.h"
65
66 /* EVPerr(EVP_F_D2I_PKEY,EVP_R_UNSUPPORTED_CIPHER); */
67 /* EVPerr(EVP_F_D2I_PKEY,EVP_R_IV_TOO_LARGE); */
68
69 #ifndef NOPROTO
70 static void EVP_PKEY_free_it(EVP_PKEY *x);
71 #else
72 static void EVP_PKEY_free_it();
73 #endif
74
75 int EVP_PKEY_bits(pkey)
76 EVP_PKEY *pkey;
77         {
78 #ifndef NO_RSA
79         if (pkey->type == EVP_PKEY_RSA)
80                 return(BN_num_bits(pkey->pkey.rsa->n));
81         else
82 #endif
83 #ifndef NO_DSA
84                 if (pkey->type == EVP_PKEY_DSA)
85                 return(BN_num_bits(pkey->pkey.dsa->p));
86 #endif
87         return(0);
88         }
89
90 int EVP_PKEY_size(pkey)
91 EVP_PKEY *pkey;
92         {
93         if (pkey == NULL)
94                 return(0);
95 #ifndef NO_RSA
96         if (pkey->type == EVP_PKEY_RSA)
97                 return(RSA_size(pkey->pkey.rsa));
98         else
99 #endif
100 #ifndef NO_DSA
101                 if (pkey->type == EVP_PKEY_DSA)
102                 return(DSA_size(pkey->pkey.dsa));
103 #endif
104         return(0);
105         }
106
107 int EVP_PKEY_save_parameters(pkey,mode)
108 EVP_PKEY *pkey;
109 int mode;
110         {
111 #ifndef NO_DSA
112         if (pkey->type == EVP_PKEY_DSA)
113                 {
114                 int ret=pkey->save_parameters=mode;
115
116                 if (mode >= 0)
117                         pkey->save_parameters=mode;
118                 return(ret);
119                 }
120 #endif
121         return(0);
122         }
123
124 int EVP_PKEY_copy_parameters(to,from)
125 EVP_PKEY *to,*from;
126         {
127         if (to->type != from->type)
128                 {
129                 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS,EVP_R_DIFFERENT_KEY_TYPES);
130                 goto err;
131                 }
132
133         if (EVP_PKEY_missing_parameters(from))
134                 {
135                 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS,EVP_R_MISSING_PARMATERS);
136                 goto err;
137                 }
138 #ifndef NO_DSA
139         if (to->type == EVP_PKEY_DSA)
140                 {
141                 BIGNUM *a;
142
143                 if ((a=BN_dup(from->pkey.dsa->p)) == NULL) goto err;
144                 if (to->pkey.dsa->p != NULL) BN_free(to->pkey.dsa->p);
145                 to->pkey.dsa->p=a;
146
147                 if ((a=BN_dup(from->pkey.dsa->q)) == NULL) goto err;
148                 if (to->pkey.dsa->q != NULL) BN_free(to->pkey.dsa->q);
149                 to->pkey.dsa->q=a;
150
151                 if ((a=BN_dup(from->pkey.dsa->g)) == NULL) goto err;
152                 if (to->pkey.dsa->g != NULL) BN_free(to->pkey.dsa->g);
153                 to->pkey.dsa->g=a;
154                 }
155 #endif
156         return(1);
157 err:
158         return(0);
159         }
160
161 int EVP_PKEY_missing_parameters(pkey)
162 EVP_PKEY *pkey;
163         {
164 #ifndef NO_DSA
165         if (pkey->type == EVP_PKEY_DSA)
166                 {
167                 DSA *dsa;
168
169                 dsa=pkey->pkey.dsa;
170                 if ((dsa->p == NULL) || (dsa->q == NULL) || (dsa->g == NULL))
171                         return(1);
172                 }
173 #endif
174         return(0);
175         }
176
177 int EVP_PKEY_cmp_parameters(a,b)
178 EVP_PKEY *a,*b;
179         {
180 #ifndef NO_DSA
181         if ((a->type == EVP_PKEY_DSA) && (b->type == EVP_PKEY_DSA))
182                 {
183                 if (    BN_cmp(a->pkey.dsa->p,b->pkey.dsa->p) ||
184                         BN_cmp(a->pkey.dsa->q,b->pkey.dsa->q) ||
185                         BN_cmp(a->pkey.dsa->g,b->pkey.dsa->g))
186                         return(0);
187                 else
188                         return(1);
189                 }
190 #endif
191         return(-1);
192         }
193
194 EVP_PKEY *EVP_PKEY_new()
195         {
196         EVP_PKEY *ret;
197
198         ret=(EVP_PKEY *)Malloc(sizeof(EVP_PKEY));
199         if (ret == NULL)
200                 {
201                 EVPerr(EVP_F_EVP_PKEY_NEW,ERR_R_MALLOC_FAILURE);
202                 return(NULL);
203                 }
204         ret->type=EVP_PKEY_NONE;
205         ret->references=1;
206         ret->pkey.ptr=NULL;
207         ret->attributes=NULL;
208         ret->save_parameters=1;
209         return(ret);
210         }
211
212 int EVP_PKEY_assign(pkey,type,key)
213 EVP_PKEY *pkey;
214 int type;
215 char *key;
216         {
217         if (pkey == NULL) return(0);
218         if (pkey->pkey.ptr != NULL)
219                 EVP_PKEY_free_it(pkey);
220         pkey->type=EVP_PKEY_type(type);
221         pkey->save_type=type;
222         pkey->pkey.ptr=key;
223         return(1);
224         }
225
226 int EVP_PKEY_type(type)
227 int type;
228         {
229         switch (type)
230                 {
231         case EVP_PKEY_RSA:
232         case EVP_PKEY_RSA2:
233                 return(EVP_PKEY_RSA);
234         case EVP_PKEY_DSA:
235         case EVP_PKEY_DSA1:
236         case EVP_PKEY_DSA2:
237         case EVP_PKEY_DSA3:
238         case EVP_PKEY_DSA4:
239                 return(EVP_PKEY_DSA);
240         case EVP_PKEY_DH:
241                 return(EVP_PKEY_DH);
242         default:
243                 return(NID_undef);
244                 }
245         }
246
247 void EVP_PKEY_free(x)
248 EVP_PKEY *x;
249         {
250         int i;
251
252         if (x == NULL) return;
253
254         i=CRYPTO_add(&x->references,-1,CRYPTO_LOCK_EVP_PKEY);
255 #ifdef REF_PRINT
256         REF_PRINT("EVP_PKEY",x);
257 #endif
258         if (i > 0) return;
259 #ifdef REF_CHECK
260         if (i < 0)
261                 {
262                 fprintf(stderr,"EVP_PKEY_free, bad reference count\n");
263                 abort();
264                 }
265 #endif
266         EVP_PKEY_free_it(x);
267         Free((char *)x);
268         }
269
270 static void EVP_PKEY_free_it(x)
271 EVP_PKEY *x;
272         {
273         switch (x->type)
274                 {
275 #ifndef NO_RSA
276         case EVP_PKEY_RSA:
277         case EVP_PKEY_RSA2:
278                 RSA_free(x->pkey.rsa);
279                 break;
280 #endif
281 #ifndef NO_DSA
282         case EVP_PKEY_DSA:
283         case EVP_PKEY_DSA2:
284         case EVP_PKEY_DSA3:
285         case EVP_PKEY_DSA4:
286                 DSA_free(x->pkey.dsa);
287                 break;
288 #endif
289 #ifndef NO_DH
290         case EVP_PKEY_DH:
291                 DH_free(x->pkey.dh);
292                 break;
293 #endif
294                 }
295         }
296