More pissing about to get pem.h to behave properly.
[openssl.git] / crypto / x509 / x509_vfy.c
1 /* crypto/x509/x509_vfy.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 <time.h>
61 #include <errno.h>
62 #include <sys/types.h>
63 #include <sys/stat.h>
64
65 #include "crypto.h"
66 #include "cryptlib.h"
67 #include "lhash.h"
68 #include "buffer.h"
69 #include "evp.h"
70 #include "asn1.h"
71 #include "x509.h"
72 #include "objects.h"
73
74 #ifndef NOPROTO
75 static int null_callback(int ok,X509_STORE_CTX *e);
76 static int internal_verify(X509_STORE_CTX *ctx);
77 #else
78 static int null_callback();
79 static int internal_verify();
80 #endif
81
82 char *X509_version="X.509 part of OpenSSL 0.9.2 31-Dec-1998";
83 static STACK *x509_store_ctx_method=NULL;
84 static int x509_store_ctx_num=0;
85 #if 0
86 static int x509_store_num=1;
87 static STACK *x509_store_method=NULL;
88 #endif
89
90 static int null_callback(ok,e)
91 int ok;
92 X509_STORE_CTX *e;
93         {
94         return(ok);
95         }
96
97 #if 0
98 static int x509_subject_cmp(a,b)
99 X509 **a,**b;
100         {
101         return(X509_subject_name_cmp(*a,*b));
102         }
103 #endif
104
105 int X509_verify_cert(ctx)
106 X509_STORE_CTX *ctx;
107         {
108         X509 *x,*xtmp,*chain_ss=NULL;
109         X509_NAME *xn;
110         X509_OBJECT obj;
111         int depth,i,ok=0;
112         int num;
113         int (*cb)();
114         STACK *sktmp=NULL;
115
116         if (ctx->cert == NULL)
117                 {
118                 X509err(X509_F_X509_VERIFY_CERT,X509_R_NO_CERT_SET_FOR_US_TO_VERIFY);
119                 return(-1);
120                 }
121
122         cb=ctx->ctx->verify_cb;
123         if (cb == NULL) cb=null_callback;
124
125         /* first we make sure the chain we are going to build is
126          * present and that the first entry is in place */
127         if (ctx->chain == NULL)
128                 {
129                 if (    ((ctx->chain=sk_new_null()) == NULL) ||
130                         (!sk_push(ctx->chain,(char *)ctx->cert)))
131                         {
132                         X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE);
133                         goto end;
134                         }
135                 CRYPTO_add(&ctx->cert->references,1,CRYPTO_LOCK_X509);
136                 ctx->last_untrusted=1;
137                 }
138
139         /* We use a temporary so we can chop and hack at it */
140         if ((ctx->untrusted != NULL) && (sktmp=sk_dup(ctx->untrusted)) == NULL)
141                 {
142                 X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE);
143                 goto end;
144                 }
145
146         num=sk_num(ctx->chain);
147         x=(X509 *)sk_value(ctx->chain,num-1);
148         depth=ctx->depth;
149
150
151         for (;;)
152                 {
153                 /* If we have enough, we break */
154                 if (depth <= num) break;
155
156                 /* If we are self signed, we break */
157                 xn=X509_get_issuer_name(x);
158                 if (X509_NAME_cmp(X509_get_subject_name(x),xn) == 0)
159                         break;
160
161                 /* If we were passed a cert chain, use it first */
162                 if (ctx->untrusted != NULL)
163                         {
164                         xtmp=X509_find_by_subject(sktmp,xn);
165                         if (xtmp != NULL)
166                                 {
167                                 if (!sk_push(ctx->chain,(char *)xtmp))
168                                         {
169                                         X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE);
170                                         goto end;
171                                         }
172                                 CRYPTO_add(&xtmp->references,1,CRYPTO_LOCK_X509);
173                                 sk_delete_ptr(sktmp,(char *)xtmp);
174                                 ctx->last_untrusted++;
175                                 x=xtmp;
176                                 num++;
177                                 /* reparse the full chain for
178                                  * the next one */
179                                 continue;
180                                 }
181                         }
182                 break;
183                 }
184
185         /* at this point, chain should contain a list of untrusted
186          * certificates.  We now need to add at least one trusted one,
187          * if possible, otherwise we complain. */
188
189         i=sk_num(ctx->chain);
190         x=(X509 *)sk_value(ctx->chain,i-1);
191         if (X509_NAME_cmp(X509_get_subject_name(x),X509_get_issuer_name(x))
192                 == 0)
193                 {
194                 /* we have a self signed certificate */
195                 if (sk_num(ctx->chain) == 1)
196                         {
197                         ctx->error=X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT;
198                         ctx->current_cert=x;
199                         ctx->error_depth=i-1;
200                         ok=cb(0,ctx);
201                         if (!ok) goto end;
202                         }
203                 else
204                         {
205                         /* worry more about this one elsewhere */
206                         chain_ss=(X509 *)sk_pop(ctx->chain);
207                         ctx->last_untrusted--;
208                         num--;
209                         x=(X509 *)sk_value(ctx->chain,num-1);
210                         }
211                 }
212
213         /* We now lookup certs from the certificate store */
214         for (;;)
215                 {
216                 /* If we have enough, we break */
217                 if (depth <= num) break;
218
219                 /* If we are self signed, we break */
220                 xn=X509_get_issuer_name(x);
221                 if (X509_NAME_cmp(X509_get_subject_name(x),xn) == 0)
222                         break;
223
224                 ok=X509_STORE_get_by_subject(ctx,X509_LU_X509,xn,&obj);
225                 if (ok != X509_LU_X509)
226                         {
227                         if (ok == X509_LU_RETRY)
228                                 {
229                                 X509_OBJECT_free_contents(&obj);
230                                 X509err(X509_F_X509_VERIFY_CERT,X509_R_SHOULD_RETRY);
231                                 return(ok);
232                                 }
233                         else if (ok != X509_LU_FAIL)
234                                 {
235                                 X509_OBJECT_free_contents(&obj);
236                                 /* not good :-(, break anyway */
237                                 return(ok);
238                                 }
239                         break;
240                         }
241                 x=obj.data.x509;
242                 if (!sk_push(ctx->chain,(char *)obj.data.x509))
243                         {
244                         X509_OBJECT_free_contents(&obj);
245                         X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE);
246                         return(0);
247                         }
248                 num++;
249                 }
250
251         /* we now have our chain, lets check it... */
252         xn=X509_get_issuer_name(x);
253         if (X509_NAME_cmp(X509_get_subject_name(x),xn) != 0)
254                 {
255                 if ((chain_ss == NULL) || (X509_NAME_cmp(X509_get_subject_name(chain_ss),xn) != 0))
256                         {
257                         if (ctx->last_untrusted >= num)
258                                 ctx->error=X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;
259                         else
260                                 ctx->error=X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT;
261                         ctx->current_cert=x;
262                         }
263                 else
264                         {
265
266                         sk_push(ctx->chain,(char *)chain_ss);
267                         num++;
268                         ctx->last_untrusted=num;
269                         ctx->current_cert=chain_ss;
270                         ctx->error=X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
271                         chain_ss=NULL;
272                         }
273
274                 ctx->error_depth=num-1;
275                 ok=cb(0,ctx);
276                 if (!ok) goto end;
277                 }
278
279         /* We may as well copy down any DSA parameters that are required */
280         X509_get_pubkey_parameters(NULL,ctx->chain);
281
282         /* At this point, we have a chain and just need to verify it */
283         if (ctx->ctx->verify != NULL)
284                 ok=ctx->ctx->verify(ctx);
285         else
286                 ok=internal_verify(ctx);
287         if (0)
288                 {
289 end:
290                 X509_get_pubkey_parameters(NULL,ctx->chain);
291                 }
292         if (sktmp != NULL) sk_free(sktmp);
293         if (chain_ss != NULL) X509_free(chain_ss);
294         return(ok);
295         }
296
297 static int internal_verify(ctx)
298 X509_STORE_CTX *ctx;
299         {
300         int i,ok=0,n;
301         X509 *xs,*xi;
302         EVP_PKEY *pkey=NULL;
303         int (*cb)();
304
305         cb=ctx->ctx->verify_cb;
306         if (cb == NULL) cb=null_callback;
307
308         n=sk_num(ctx->chain);
309         ctx->error_depth=n-1;
310         n--;
311         xi=(X509 *)sk_value(ctx->chain,n);
312         if (X509_NAME_cmp(X509_get_subject_name(xi),
313                 X509_get_issuer_name(xi)) == 0)
314                 xs=xi;
315         else
316                 {
317                 if (n <= 0)
318                         {
319                         ctx->error=X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE;
320                         ctx->current_cert=xi;
321                         ok=cb(0,ctx);
322                         goto end;
323                         }
324                 else
325                         {
326                         n--;
327                         ctx->error_depth=n;
328                         xs=(X509 *)sk_value(ctx->chain,n);
329                         }
330                 }
331
332 /*      ctx->error=0;  not needed */
333         while (n >= 0)
334                 {
335                 ctx->error_depth=n;
336                 if (!xs->valid)
337                         {
338                         if ((pkey=X509_get_pubkey(xi)) == NULL)
339                                 {
340                                 ctx->error=X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
341                                 ctx->current_cert=xi;
342                                 ok=(*cb)(0,ctx);
343                                 if (!ok) goto end;
344                                 }
345                         if (X509_verify(xs,pkey) <= 0)
346                                 {
347                                 EVP_PKEY_free(pkey);
348                                 ctx->error=X509_V_ERR_CERT_SIGNATURE_FAILURE;
349                                 ctx->current_cert=xs;
350                                 ok=(*cb)(0,ctx);
351                                 if (!ok) goto end;
352                                 }
353                         EVP_PKEY_free(pkey);
354                         pkey=NULL;
355
356                         i=X509_cmp_current_time(X509_get_notBefore(xs));
357                         if (i == 0)
358                                 {
359                                 ctx->error=X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD;
360                                 ctx->current_cert=xs;
361                                 ok=(*cb)(0,ctx);
362                                 if (!ok) goto end;
363                                 }
364                         if (i > 0)
365                                 {
366                                 ctx->error=X509_V_ERR_CERT_NOT_YET_VALID;
367                                 ctx->current_cert=xs;
368                                 ok=(*cb)(0,ctx);
369                                 if (!ok) goto end;
370                                 }
371                         xs->valid=1;
372                         }
373
374                 i=X509_cmp_current_time(X509_get_notAfter(xs));
375                 if (i == 0)
376                         {
377                         ctx->error=X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD;
378                         ctx->current_cert=xs;
379                         ok=(*cb)(0,ctx);
380                         if (!ok) goto end;
381                         }
382
383                 if (i < 0)
384                         {
385                         ctx->error=X509_V_ERR_CERT_HAS_EXPIRED;
386                         ctx->current_cert=xs;
387                         ok=(*cb)(0,ctx);
388                         if (!ok) goto end;
389                         }
390
391                 /* CRL CHECK */
392
393                 /* The last error (if any) is still in the error value */
394                 ctx->current_cert=xs;
395                 ok=(*cb)(1,ctx);
396                 if (!ok) goto end;
397
398                 n--;
399                 if (n >= 0)
400                         {
401                         xi=xs;
402                         xs=(X509 *)sk_value(ctx->chain,n);
403                         }
404                 }
405         ok=1;
406 end:
407         return(ok);
408         }
409
410 int X509_cmp_current_time(ctm)
411 ASN1_UTCTIME *ctm;
412         {
413         char *str;
414         ASN1_UTCTIME atm;
415         time_t offset;
416         char buff1[24],buff2[24],*p;
417         int i,j;
418
419         p=buff1;
420         i=ctm->length;
421         str=(char *)ctm->data;
422         if ((i < 11) || (i > 17)) return(0);
423         memcpy(p,str,10);
424         p+=10;
425         str+=10;
426
427         if ((*str == 'Z') || (*str == '-') || (*str == '+'))
428                 { *(p++)='0'; *(p++)='0'; }
429         else    { *(p++)= *(str++); *(p++)= *(str++); }
430         *(p++)='Z';
431         *(p++)='\0';
432
433         if (*str == 'Z')
434                 offset=0;
435         else
436                 {
437                 if ((*str != '+') && (str[5] != '-'))
438                         return(0);
439                 offset=((str[1]-'0')*10+(str[2]-'0'))*60;
440                 offset+=(str[3]-'0')*10+(str[4]-'0');
441                 if (*str == '-')
442                         offset= -offset;
443                 }
444         atm.type=V_ASN1_UTCTIME;
445         atm.length=sizeof(buff2);
446         atm.data=(unsigned char *)buff2;
447
448         X509_gmtime_adj(&atm,-offset);
449
450         i=(buff1[0]-'0')*10+(buff1[1]-'0');
451         if (i < 70) i+=100;
452         j=(buff2[0]-'0')*10+(buff2[1]-'0');
453         if (j < 70) j+=100;
454
455         if (i < j) return (-1);
456         if (i > j) return (1);
457         i=strcmp(buff1,buff2);
458         if (i == 0) /* wait a second then return younger :-) */
459                 return(-1);
460         else
461                 return(i);
462         }
463
464 ASN1_UTCTIME *X509_gmtime_adj(s, adj)
465 ASN1_UTCTIME *s;
466 long adj;
467         {
468         time_t t;
469
470         time(&t);
471         t+=adj;
472         return(ASN1_UTCTIME_set(s,t));
473         }
474
475 int X509_get_pubkey_parameters(pkey,chain)
476 EVP_PKEY *pkey;
477 STACK *chain;
478         {
479         EVP_PKEY *ktmp=NULL,*ktmp2;
480         int i,j;
481
482         if ((pkey != NULL) && !EVP_PKEY_missing_parameters(pkey)) return(1);
483
484         for (i=0; i<sk_num(chain); i++)
485                 {
486                 ktmp=X509_get_pubkey((X509 *)sk_value(chain,i));
487                 if (ktmp == NULL)
488                         {
489                         X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY);
490                         return(0);
491                         }
492                 if (!EVP_PKEY_missing_parameters(ktmp))
493                         break;
494                 else
495                         {
496                         EVP_PKEY_free(ktmp);
497                         ktmp=NULL;
498                         }
499                 }
500         if (ktmp == NULL)
501                 {
502                 X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN);
503                 return(0);
504                 }
505
506         /* first, populate the other certs */
507         for (j=i-1; j >= 0; j--)
508                 {
509                 ktmp2=X509_get_pubkey((X509 *)sk_value(chain,j));
510                 EVP_PKEY_copy_parameters(ktmp2,ktmp);
511                 EVP_PKEY_free(ktmp2);
512                 }
513         
514         if (pkey != NULL) EVP_PKEY_copy_parameters(pkey,ktmp);
515         EVP_PKEY_free(ktmp);
516         return(1);
517         }
518
519 int X509_STORE_add_cert(ctx,x)
520 X509_STORE *ctx;
521 X509 *x;
522         {
523         X509_OBJECT *obj,*r;
524         int ret=1;
525
526         if (x == NULL) return(0);
527         obj=(X509_OBJECT *)Malloc(sizeof(X509_OBJECT));
528         if (obj == NULL)
529                 {
530                 X509err(X509_F_X509_STORE_ADD_CERT,ERR_R_MALLOC_FAILURE);
531                 return(0);
532                 }
533         obj->type=X509_LU_X509;
534         obj->data.x509=x;
535
536         CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE);
537
538         X509_OBJECT_up_ref_count(obj);
539
540         r=(X509_OBJECT *)lh_insert(ctx->certs,(char *)obj);
541         if (r != NULL)
542                 { /* oops, put it back */
543                 lh_delete(ctx->certs,(char *)obj);
544                 X509_OBJECT_free_contents(obj);
545                 Free(obj);
546                 lh_insert(ctx->certs,(char *)r);
547                 X509err(X509_F_X509_STORE_ADD_CERT,X509_R_CERT_ALREADY_IN_HASH_TABLE);
548                 ret=0;
549                 }
550
551         CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);
552
553         return(ret);    
554         }
555
556 int X509_STORE_add_crl(ctx,x)
557 X509_STORE *ctx;
558 X509_CRL *x;
559         {
560         X509_OBJECT *obj,*r;
561         int ret=1;
562
563         if (x == NULL) return(0);
564         obj=(X509_OBJECT *)Malloc(sizeof(X509_OBJECT));
565         if (obj == NULL)
566                 {
567                 X509err(X509_F_X509_STORE_ADD_CRL,ERR_R_MALLOC_FAILURE);
568                 return(0);
569                 }
570         obj->type=X509_LU_CRL;
571         obj->data.crl=x;
572
573         CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE);
574
575         X509_OBJECT_up_ref_count(obj);
576
577         r=(X509_OBJECT *)lh_insert(ctx->certs,(char *)obj);
578         if (r != NULL)
579                 { /* oops, put it back */
580                 lh_delete(ctx->certs,(char *)obj);
581                 X509_OBJECT_free_contents(obj);
582                 Free(obj);
583                 lh_insert(ctx->certs,(char *)r);
584                 X509err(X509_F_X509_STORE_ADD_CRL,X509_R_CERT_ALREADY_IN_HASH_TABLE);
585                 ret=0;
586                 }
587
588         CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);
589
590         return(ret);    
591         }
592
593 int X509_STORE_CTX_get_ex_new_index(argl,argp,new_func,dup_func,free_func)
594 long argl;
595 char *argp;
596 int (*new_func)();
597 int (*dup_func)();
598 void (*free_func)();
599         {
600         x509_store_ctx_num++;
601         return(CRYPTO_get_ex_new_index(x509_store_ctx_num-1,
602                 &x509_store_ctx_method,
603                 argl,argp,new_func,dup_func,free_func));
604         }
605
606 int X509_STORE_CTX_set_ex_data(ctx,idx,data)
607 X509_STORE_CTX *ctx;
608 int idx;
609 char *data;
610         {
611         return(CRYPTO_set_ex_data(&ctx->ex_data,idx,data));
612         }
613
614 char *X509_STORE_CTX_get_ex_data(ctx,idx)
615 X509_STORE_CTX *ctx;
616 int idx;
617         {
618         return(CRYPTO_get_ex_data(&ctx->ex_data,idx));
619         }
620
621 int X509_STORE_CTX_get_error(ctx)
622 X509_STORE_CTX *ctx;
623         {
624         return(ctx->error);
625         }
626
627 void X509_STORE_CTX_set_error(ctx,err)
628 X509_STORE_CTX *ctx;
629 int err;
630         {
631         ctx->error=err;
632         }
633
634 int X509_STORE_CTX_get_error_depth(ctx)
635 X509_STORE_CTX *ctx;
636         {
637         return(ctx->error_depth);
638         }
639
640 X509 *X509_STORE_CTX_get_current_cert(ctx)
641 X509_STORE_CTX *ctx;
642         {
643         return(ctx->current_cert);
644         }
645
646 STACK *X509_STORE_CTX_get_chain(ctx)
647 X509_STORE_CTX *ctx;
648         {
649         return(ctx->chain);
650         }
651
652 void X509_STORE_CTX_set_cert(ctx,x)
653 X509_STORE_CTX *ctx;
654 X509 *x;
655         {
656         ctx->cert=x;
657         }
658
659 void X509_STORE_CTX_set_chain(ctx,sk)
660 X509_STORE_CTX *ctx;
661 STACK *sk;
662         {
663         ctx->untrusted=sk;
664         }
665
666