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