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