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