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