1 /* crypto/x509/x509_vfy.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
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)"
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
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.]
64 #include <openssl/crypto.h>
65 #include <openssl/lhash.h>
66 #include <openssl/buffer.h>
67 #include <openssl/evp.h>
68 #include <openssl/asn1.h>
69 #include <openssl/x509.h>
70 #include <openssl/x509v3.h>
71 #include <openssl/objects.h>
73 static int null_callback(int ok,X509_STORE_CTX *e);
74 static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer);
75 static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x);
76 static int check_chain_purpose(X509_STORE_CTX *ctx);
77 static int check_trust(X509_STORE_CTX *ctx);
78 static int internal_verify(X509_STORE_CTX *ctx);
79 const char *X509_version="X.509" OPENSSL_VERSION_PTEXT;
81 static STACK_OF(CRYPTO_EX_DATA_FUNCS) *x509_store_ctx_method=NULL;
82 static int x509_store_ctx_num=0;
85 static int null_callback(int ok, X509_STORE_CTX *e)
91 static int x509_subject_cmp(X509 **a, X509 **b)
93 return X509_subject_name_cmp(*a,*b);
97 int X509_verify_cert(X509_STORE_CTX *ctx)
99 X509 *x,*xtmp,*chain_ss=NULL;
104 STACK_OF(X509) *sktmp=NULL;
106 if (ctx->cert == NULL)
108 X509err(X509_F_X509_VERIFY_CERT,X509_R_NO_CERT_SET_FOR_US_TO_VERIFY);
113 if (cb == NULL) cb=null_callback;
115 /* first we make sure the chain we are going to build is
116 * present and that the first entry is in place */
117 if (ctx->chain == NULL)
119 if ( ((ctx->chain=sk_X509_new_null()) == NULL) ||
120 (!sk_X509_push(ctx->chain,ctx->cert)))
122 X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE);
125 CRYPTO_add(&ctx->cert->references,1,CRYPTO_LOCK_X509);
126 ctx->last_untrusted=1;
129 /* We use a temporary STACK so we can chop and hack at it */
130 if (ctx->untrusted != NULL
131 && (sktmp=sk_X509_dup(ctx->untrusted)) == NULL)
133 X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE);
137 num=sk_X509_num(ctx->chain);
138 x=sk_X509_value(ctx->chain,num-1);
144 /* If we have enough, we break */
145 if (depth < num) break; /* FIXME: If this happens, we should take
146 * note of it and, if appropriate, use the
147 * X509_V_ERR_CERT_CHAIN_TOO_LONG error
151 /* If we are self signed, we break */
152 xn=X509_get_issuer_name(x);
153 if (ctx->check_issued(ctx, x,x)) break;
155 /* If we were passed a cert chain, use it first */
156 if (ctx->untrusted != NULL)
158 xtmp=find_issuer(ctx, sktmp,x);
161 if (!sk_X509_push(ctx->chain,xtmp))
163 X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE);
166 CRYPTO_add(&xtmp->references,1,CRYPTO_LOCK_X509);
167 sk_X509_delete_ptr(sktmp,xtmp);
168 ctx->last_untrusted++;
171 /* reparse the full chain for
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. */
183 /* Examine last certificate in chain and see if it
187 i=sk_X509_num(ctx->chain);
188 x=sk_X509_value(ctx->chain,i-1);
189 xn = X509_get_subject_name(x);
190 if (ctx->check_issued(ctx, x, x))
192 /* we have a self signed certificate */
193 if (sk_X509_num(ctx->chain) == 1)
195 /* We have a single self signed certificate: see if
196 * we can find it in the store. We must have an exact
197 * match to avoid possible impersonation.
199 ok = ctx->get_issuer(&xtmp, ctx, x);
200 if ((ok <= 0) || X509_cmp(x, xtmp))
202 ctx->error=X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT;
204 ctx->error_depth=i-1;
205 if (ok == 1) X509_free(xtmp);
211 /* We have a match: replace certificate with store version
212 * so we get any trust settings.
216 sk_X509_set(ctx->chain, i - 1, x);
217 ctx->last_untrusted=0;
222 /* extract and save self signed certificate for later use */
223 chain_ss=sk_X509_pop(ctx->chain);
224 ctx->last_untrusted--;
226 x=sk_X509_value(ctx->chain,num-1);
230 /* We now lookup certs from the certificate store */
233 /* If we have enough, we break */
234 if (depth < num) break;
236 /* If we are self signed, we break */
237 xn=X509_get_issuer_name(x);
238 if (ctx->check_issued(ctx,x,x)) break;
240 ok = ctx->get_issuer(&xtmp, ctx, x);
242 if (ok < 0) return ok;
246 if (!sk_X509_push(ctx->chain,x))
249 X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE);
255 /* we now have our chain, lets check it... */
256 xn=X509_get_issuer_name(x);
258 /* Is last certificate looked up self signed? */
259 if (!ctx->check_issued(ctx,x,x))
261 if ((chain_ss == NULL) || !ctx->check_issued(ctx, x, chain_ss))
263 if (ctx->last_untrusted >= num)
264 ctx->error=X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;
266 ctx->error=X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT;
272 sk_X509_push(ctx->chain,chain_ss);
274 ctx->last_untrusted=num;
275 ctx->current_cert=chain_ss;
276 ctx->error=X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
280 ctx->error_depth=num-1;
285 /* We have the chain complete: now we need to check its purpose */
286 if (ctx->purpose > 0) ok = check_chain_purpose(ctx);
290 /* The chain extensions are OK: check trust */
292 if (ctx->trust > 0) ok = check_trust(ctx);
296 /* We may as well copy down any DSA parameters that are required */
297 X509_get_pubkey_parameters(NULL,ctx->chain);
299 /* At this point, we have a chain and just need to verify it */
300 if (ctx->verify != NULL)
303 ok=internal_verify(ctx);
307 X509_get_pubkey_parameters(NULL,ctx->chain);
309 if (sktmp != NULL) sk_X509_free(sktmp);
310 if (chain_ss != NULL) X509_free(chain_ss);
315 /* Given a STACK_OF(X509) find the issuer of cert (if any)
318 static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x)
322 for (i = 0; i < sk_X509_num(sk); i++)
324 issuer = sk_X509_value(sk, i);
325 if (ctx->check_issued(ctx, x, issuer))
331 /* Given a possible certificate and issuer check them */
333 static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer)
336 ret = X509_check_issued(issuer, x);
337 if (ret == X509_V_OK)
339 /* If we haven't asked for issuer errors don't set ctx */
340 if (!(ctx->flags & X509_V_FLAG_CB_ISSUER_CHECK))
344 ctx->current_cert = x;
345 ctx->current_issuer = issuer;
347 return ctx->verify_cb(0, ctx);
351 /* Alternative lookup method: look from a STACK stored in other_ctx */
353 static int get_issuer_sk(X509 **issuer, X509_STORE_CTX *ctx, X509 *x)
355 *issuer = find_issuer(ctx, ctx->other_ctx, x);
358 CRYPTO_add(&(*issuer)->references,1,CRYPTO_LOCK_X509);
366 /* Check a certificate chains extensions for consistency
367 * with the supplied purpose
370 static int check_chain_purpose(X509_STORE_CTX *ctx)
372 #ifdef NO_CHAIN_VERIFY
379 if (cb == NULL) cb=null_callback;
380 /* Check all untrusted certificates */
381 for (i = 0; i < ctx->last_untrusted; i++)
383 x = sk_X509_value(ctx->chain, i);
384 if (!X509_check_purpose(x, ctx->purpose, i))
387 ctx->error = X509_V_ERR_INVALID_CA;
389 ctx->error = X509_V_ERR_INVALID_PURPOSE;
390 ctx->error_depth = i;
391 ctx->current_cert = x;
396 if ((i > 1) && (x->ex_pathlen != -1)
397 && (i > (x->ex_pathlen + 1)))
399 ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED;
400 ctx->error_depth = i;
401 ctx->current_cert = x;
412 static int check_trust(X509_STORE_CTX *ctx)
414 #ifdef NO_CHAIN_VERIFY
421 if (cb == NULL) cb=null_callback;
422 /* For now just check the last certificate in the chain */
423 i = sk_X509_num(ctx->chain) - 1;
424 x = sk_X509_value(ctx->chain, i);
425 ok = X509_check_trust(x, ctx->trust, 0);
426 if (ok == X509_TRUST_TRUSTED)
428 ctx->error_depth = sk_X509_num(ctx->chain) - 1;
429 ctx->current_cert = x;
430 if (ok == X509_TRUST_REJECTED)
431 ctx->error = X509_V_ERR_CERT_REJECTED;
433 ctx->error = X509_V_ERR_CERT_UNTRUSTED;
439 static int internal_verify(X509_STORE_CTX *ctx)
448 if (cb == NULL) cb=null_callback;
450 n=sk_X509_num(ctx->chain);
451 ctx->error_depth=n-1;
453 xi=sk_X509_value(ctx->chain,n);
454 if (ctx->flags & X509_V_FLAG_USE_CHECK_TIME)
455 ptime = &ctx->check_time;
458 if (ctx->check_issued(ctx, xi, xi))
464 ctx->error=X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE;
465 ctx->current_cert=xi;
473 xs=sk_X509_value(ctx->chain,n);
477 /* ctx->error=0; not needed */
483 if ((pkey=X509_get_pubkey(xi)) == NULL)
485 ctx->error=X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
486 ctx->current_cert=xi;
490 if (X509_verify(xs,pkey) <= 0)
492 ctx->error=X509_V_ERR_CERT_SIGNATURE_FAILURE;
493 ctx->current_cert=xs;
504 i=X509_cmp_time(X509_get_notBefore(xs), ptime);
507 ctx->error=X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD;
508 ctx->current_cert=xs;
514 ctx->error=X509_V_ERR_CERT_NOT_YET_VALID;
515 ctx->current_cert=xs;
522 i=X509_cmp_time(X509_get_notAfter(xs), ptime);
525 ctx->error=X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD;
526 ctx->current_cert=xs;
533 ctx->error=X509_V_ERR_CERT_HAS_EXPIRED;
534 ctx->current_cert=xs;
541 /* The last error (if any) is still in the error value */
542 ctx->current_cert=xs;
550 xs=sk_X509_value(ctx->chain,n);
558 int X509_cmp_current_time(ASN1_TIME *ctm)
560 return X509_cmp_time(ctm, NULL);
563 int X509_cmp_time(ASN1_TIME *ctm, time_t *cmp_time)
568 char buff1[24],buff2[24],*p;
573 str=(char *)ctm->data;
574 if (ctm->type == V_ASN1_UTCTIME)
576 if ((i < 11) || (i > 17)) return 0;
583 if (i < 13) return 0;
589 if ((*str == 'Z') || (*str == '-') || (*str == '+'))
590 { *(p++)='0'; *(p++)='0'; }
595 /* Skip any fractional seconds... */
599 while ((*str >= '0') && (*str <= '9')) str++;
610 if ((*str != '+') && (str[5] != '-'))
612 offset=((str[1]-'0')*10+(str[2]-'0'))*60;
613 offset+=(str[3]-'0')*10+(str[4]-'0');
618 atm.length=sizeof(buff2);
619 atm.data=(unsigned char *)buff2;
621 X509_time_adj(&atm,-offset*60, cmp_time);
623 if (ctm->type == V_ASN1_UTCTIME)
625 i=(buff1[0]-'0')*10+(buff1[1]-'0');
626 if (i < 50) i+=100; /* cf. RFC 2459 */
627 j=(buff2[0]-'0')*10+(buff2[1]-'0');
630 if (i < j) return -1;
633 i=strcmp(buff1,buff2);
634 if (i == 0) /* wait a second then return younger :-) */
640 ASN1_TIME *X509_gmtime_adj(ASN1_TIME *s, long adj)
642 return X509_time_adj(s, adj, NULL);
645 ASN1_TIME *X509_time_adj(ASN1_TIME *s, long adj, time_t *in_tm)
650 if (in_tm) t = *in_tm;
654 if (s) type = s->type;
655 if (type == V_ASN1_UTCTIME) return ASN1_UTCTIME_set(s,t);
656 if (type == V_ASN1_GENERALIZEDTIME) return ASN1_GENERALIZEDTIME_set(s, t);
657 return ASN1_TIME_set(s, t);
660 int X509_get_pubkey_parameters(EVP_PKEY *pkey, STACK_OF(X509) *chain)
662 EVP_PKEY *ktmp=NULL,*ktmp2;
665 if ((pkey != NULL) && !EVP_PKEY_missing_parameters(pkey)) return 1;
667 for (i=0; i<sk_X509_num(chain); i++)
669 ktmp=X509_get_pubkey(sk_X509_value(chain,i));
672 X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY);
675 if (!EVP_PKEY_missing_parameters(ktmp))
685 X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN);
689 /* first, populate the other certs */
690 for (j=i-1; j >= 0; j--)
692 ktmp2=X509_get_pubkey(sk_X509_value(chain,j));
693 EVP_PKEY_copy_parameters(ktmp2,ktmp);
694 EVP_PKEY_free(ktmp2);
697 if (pkey != NULL) EVP_PKEY_copy_parameters(pkey,ktmp);
702 int X509_STORE_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
703 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
705 /* This function is (usually) called only once, by
706 * SSL_get_ex_data_X509_STORE_CTX_idx (ssl/ssl_cert.c).
707 * That function uses locking, so we don't (usually)
708 * have to worry about locking here. For the whole cruel
709 * truth, see crypto/ex_data.c */
710 x509_store_ctx_num++;
711 return CRYPTO_get_ex_new_index(x509_store_ctx_num-1,
712 &x509_store_ctx_method,
713 argl,argp,new_func,dup_func,free_func);
716 int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx, void *data)
718 return CRYPTO_set_ex_data(&ctx->ex_data,idx,data);
721 void *X509_STORE_CTX_get_ex_data(X509_STORE_CTX *ctx, int idx)
723 return CRYPTO_get_ex_data(&ctx->ex_data,idx);
726 int X509_STORE_CTX_get_error(X509_STORE_CTX *ctx)
731 void X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int err)
736 int X509_STORE_CTX_get_error_depth(X509_STORE_CTX *ctx)
738 return ctx->error_depth;
741 X509 *X509_STORE_CTX_get_current_cert(X509_STORE_CTX *ctx)
743 return ctx->current_cert;
746 STACK_OF(X509) *X509_STORE_CTX_get_chain(X509_STORE_CTX *ctx)
751 STACK_OF(X509) *X509_STORE_CTX_get1_chain(X509_STORE_CTX *ctx)
755 STACK_OF(X509) *chain;
756 if (!ctx->chain || !(chain = sk_X509_dup(ctx->chain))) return NULL;
757 for (i = 0; i < sk_X509_num(chain); i++)
759 x = sk_X509_value(chain, i);
760 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
765 void X509_STORE_CTX_set_cert(X509_STORE_CTX *ctx, X509 *x)
770 void X509_STORE_CTX_set_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
775 int X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose)
777 return X509_STORE_CTX_purpose_inherit(ctx, 0, purpose, 0);
780 int X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust)
782 return X509_STORE_CTX_purpose_inherit(ctx, 0, 0, trust);
785 /* This function is used to set the X509_STORE_CTX purpose and trust
786 * values. This is intended to be used when another structure has its
787 * own trust and purpose values which (if set) will be inherited by
788 * the ctx. If they aren't set then we will usually have a default
789 * purpose in mind which should then be used to set the trust value.
790 * An example of this is SSL use: an SSL structure will have its own
791 * purpose and trust settings which the application can set: if they
792 * aren't set then we use the default of SSL client/server.
795 int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx, int def_purpose,
796 int purpose, int trust)
799 /* If purpose not set use default */
800 if (!purpose) purpose = def_purpose;
801 /* If we have a purpose then check it is valid */
805 idx = X509_PURPOSE_get_by_id(purpose);
808 X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
809 X509_R_UNKNOWN_PURPOSE_ID);
812 ptmp = X509_PURPOSE_get0(idx);
813 if (ptmp->trust == X509_TRUST_DEFAULT)
815 idx = X509_PURPOSE_get_by_id(def_purpose);
818 X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
819 X509_R_UNKNOWN_PURPOSE_ID);
822 ptmp = X509_PURPOSE_get0(idx);
824 /* If trust not set then get from purpose default */
825 if (!trust) trust = ptmp->trust;
829 idx = X509_TRUST_get_by_id(trust);
832 X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
833 X509_R_UNKNOWN_TRUST_ID);
838 if (purpose) ctx->purpose = purpose;
839 if (trust) ctx->trust = trust;
843 X509_STORE_CTX *X509_STORE_CTX_new(void)
846 ctx = (X509_STORE_CTX *)OPENSSL_malloc(sizeof(X509_STORE_CTX));
847 if (ctx) memset(ctx, 0, sizeof(X509_STORE_CTX));
851 void X509_STORE_CTX_free(X509_STORE_CTX *ctx)
853 X509_STORE_CTX_cleanup(ctx);
857 void X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509,
858 STACK_OF(X509) *chain)
861 ctx->current_method=0;
863 ctx->untrusted=chain;
864 ctx->last_untrusted=0;
875 ctx->current_cert=NULL;
876 ctx->current_issuer=NULL;
877 ctx->check_issued = check_issued;
878 ctx->get_issuer = X509_STORE_CTX_get1_issuer;
879 ctx->verify_cb = store->verify_cb;
880 ctx->verify = store->verify;
882 memset(&(ctx->ex_data),0,sizeof(CRYPTO_EX_DATA));
885 /* Set alternative lookup method: just a STACK of trusted certificates.
886 * This avoids X509_STORE nastiness where it isn't needed.
889 void X509_STORE_CTX_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
892 ctx->get_issuer = get_issuer_sk;
895 void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx)
897 if (ctx->cleanup) ctx->cleanup(ctx);
898 if (ctx->chain != NULL)
900 sk_X509_pop_free(ctx->chain,X509_free);
903 CRYPTO_free_ex_data(x509_store_ctx_method,ctx,&(ctx->ex_data));
904 memset(&ctx->ex_data,0,sizeof(CRYPTO_EX_DATA));
907 void X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx, long flags)
912 void X509_STORE_CTX_set_time(X509_STORE_CTX *ctx, long flags, time_t t)
915 ctx->flags |= X509_V_FLAG_USE_CHECK_TIME;
918 IMPLEMENT_STACK_OF(X509)
919 IMPLEMENT_ASN1_SET_OF(X509)
921 IMPLEMENT_STACK_OF(X509_NAME)
923 IMPLEMENT_STACK_OF(X509_ATTRIBUTE)
924 IMPLEMENT_ASN1_SET_OF(X509_ATTRIBUTE)