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.]
62 #include <sys/types.h>
76 static int null_callback(int ok
,X509_STORE_CTX
*e
);
77 static int internal_verify(X509_STORE_CTX
*ctx
);
79 static int null_callback();
80 static int internal_verify();
83 char *X509_version
="X.509 part of OpenSSL 0.9.1c 23-Dec-1998";
84 static STACK
*x509_store_ctx_method
=NULL
;
85 static int x509_store_ctx_num
=0;
87 static int x509_store_num
=1;
88 static STACK
*x509_store_method
=NULL
;
91 static int null_callback(ok
,e
)
99 static int x509_subject_cmp(a
,b
)
102 return(X509_subject_name_cmp(*a
,*b
));
106 int X509_verify_cert(ctx
)
109 X509
*x
,*xtmp
,*chain_ss
=NULL
;
117 if (ctx
->cert
== NULL
)
119 X509err(X509_F_X509_VERIFY_CERT
,X509_R_NO_CERT_SET_FOR_US_TO_VERIFY
);
123 cb
=ctx
->ctx
->verify_cb
;
124 if (cb
== NULL
) cb
=null_callback
;
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
)
130 if ( ((ctx
->chain
=sk_new_null()) == NULL
) ||
131 (!sk_push(ctx
->chain
,(char *)ctx
->cert
)))
133 X509err(X509_F_X509_VERIFY_CERT
,ERR_R_MALLOC_FAILURE
);
136 CRYPTO_add(&ctx
->cert
->references
,1,CRYPTO_LOCK_X509
);
137 ctx
->last_untrusted
=1;
140 /* We use a temporary so we can chop and hack at it */
141 if ((ctx
->untrusted
!= NULL
) && (sktmp
=sk_dup(ctx
->untrusted
)) == NULL
)
143 X509err(X509_F_X509_VERIFY_CERT
,ERR_R_MALLOC_FAILURE
);
147 num
=sk_num(ctx
->chain
);
148 x
=(X509
*)sk_value(ctx
->chain
,num
-1);
154 /* If we have enough, we break */
155 if (depth
<= num
) break;
157 /* If we are self signed, we break */
158 xn
=X509_get_issuer_name(x
);
159 if (X509_NAME_cmp(X509_get_subject_name(x
),xn
) == 0)
162 /* If we were passed a cert chain, use it first */
163 if (ctx
->untrusted
!= NULL
)
165 xtmp
=X509_find_by_subject(sktmp
,xn
);
168 if (!sk_push(ctx
->chain
,(char *)xtmp
))
170 X509err(X509_F_X509_VERIFY_CERT
,ERR_R_MALLOC_FAILURE
);
173 CRYPTO_add(&xtmp
->references
,1,CRYPTO_LOCK_X509
);
174 sk_delete_ptr(sktmp
,(char *)xtmp
);
175 ctx
->last_untrusted
++;
178 /* reparse the full chain for
186 /* at this point, chain should contain a list of untrusted
187 * certificates. We now need to add at least one trusted one,
188 * if possible, otherwise we complain. */
190 i
=sk_num(ctx
->chain
);
191 x
=(X509
*)sk_value(ctx
->chain
,i
-1);
192 if (X509_NAME_cmp(X509_get_subject_name(x
),X509_get_issuer_name(x
))
195 /* we have a self signed certificate */
196 if (sk_num(ctx
->chain
) == 1)
198 ctx
->error
=X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
;
200 ctx
->error_depth
=i
-1;
206 /* worry more about this one elsewhere */
207 chain_ss
=(X509
*)sk_pop(ctx
->chain
);
208 ctx
->last_untrusted
--;
210 x
=(X509
*)sk_value(ctx
->chain
,num
-1);
214 /* We now lookup certs from the certificate store */
217 /* If we have enough, we break */
218 if (depth
<= num
) break;
220 /* If we are self signed, we break */
221 xn
=X509_get_issuer_name(x
);
222 if (X509_NAME_cmp(X509_get_subject_name(x
),xn
) == 0)
225 ok
=X509_STORE_get_by_subject(ctx
,X509_LU_X509
,xn
,&obj
);
226 if (ok
!= X509_LU_X509
)
228 if (ok
== X509_LU_RETRY
)
230 X509_OBJECT_free_contents(&obj
);
231 X509err(X509_F_X509_VERIFY_CERT
,X509_R_SHOULD_RETRY
);
234 else if (ok
!= X509_LU_FAIL
)
236 X509_OBJECT_free_contents(&obj
);
237 /* not good :-(, break anyway */
243 if (!sk_push(ctx
->chain
,(char *)obj
.data
.x509
))
245 X509_OBJECT_free_contents(&obj
);
246 X509err(X509_F_X509_VERIFY_CERT
,ERR_R_MALLOC_FAILURE
);
252 /* we now have our chain, lets check it... */
253 xn
=X509_get_issuer_name(x
);
254 if (X509_NAME_cmp(X509_get_subject_name(x
),xn
) != 0)
256 if ((chain_ss
== NULL
) || (X509_NAME_cmp(X509_get_subject_name(chain_ss
),xn
) != 0))
258 if (ctx
->last_untrusted
>= num
)
259 ctx
->error
=X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
;
261 ctx
->error
=X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT
;
267 sk_push(ctx
->chain
,(char *)chain_ss
);
269 ctx
->last_untrusted
=num
;
270 ctx
->current_cert
=chain_ss
;
271 ctx
->error
=X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN
;
275 ctx
->error_depth
=num
-1;
280 /* We may as well copy down any DSA parameters that are required */
281 X509_get_pubkey_parameters(NULL
,ctx
->chain
);
283 /* At this point, we have a chain and just need to verify it */
284 if (ctx
->ctx
->verify
!= NULL
)
285 ok
=ctx
->ctx
->verify(ctx
);
287 ok
=internal_verify(ctx
);
291 X509_get_pubkey_parameters(NULL
,ctx
->chain
);
293 if (sktmp
!= NULL
) sk_free(sktmp
);
294 if (chain_ss
!= NULL
) X509_free(chain_ss
);
298 static int internal_verify(ctx
)
306 cb
=ctx
->ctx
->verify_cb
;
307 if (cb
== NULL
) cb
=null_callback
;
309 n
=sk_num(ctx
->chain
);
310 ctx
->error_depth
=n
-1;
312 xi
=(X509
*)sk_value(ctx
->chain
,n
);
313 if (X509_NAME_cmp(X509_get_subject_name(xi
),
314 X509_get_issuer_name(xi
)) == 0)
320 ctx
->error
=X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE
;
321 ctx
->current_cert
=xi
;
329 xs
=(X509
*)sk_value(ctx
->chain
,n
);
333 /* ctx->error=0; not needed */
339 if ((pkey
=X509_get_pubkey(xi
)) == NULL
)
341 ctx
->error
=X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY
;
342 ctx
->current_cert
=xi
;
346 if (X509_verify(xs
,pkey
) <= 0)
348 ctx
->error
=X509_V_ERR_CERT_SIGNATURE_FAILURE
;
349 ctx
->current_cert
=xs
;
355 i
=X509_cmp_current_time(X509_get_notBefore(xs
));
358 ctx
->error
=X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD
;
359 ctx
->current_cert
=xs
;
365 ctx
->error
=X509_V_ERR_CERT_NOT_YET_VALID
;
366 ctx
->current_cert
=xs
;
373 i
=X509_cmp_current_time(X509_get_notAfter(xs
));
376 ctx
->error
=X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD
;
377 ctx
->current_cert
=xs
;
384 ctx
->error
=X509_V_ERR_CERT_HAS_EXPIRED
;
385 ctx
->current_cert
=xs
;
392 /* The last error (if any) is still in the error value */
393 ctx
->current_cert
=xs
;
401 xs
=(X509
*)sk_value(ctx
->chain
,n
);
409 int X509_cmp_current_time(ctm
)
415 char buff1
[24],buff2
[24],*p
;
420 str
=(char *)ctm
->data
;
421 if ((i
< 11) || (i
> 17)) return(0);
426 if ((*str
== 'Z') || (*str
== '-') || (*str
== '+'))
427 { *(p
++)='0'; *(p
++)='0'; }
428 else { *(p
++)= *(str
++); *(p
++)= *(str
++); }
436 if ((*str
!= '+') && (str
[5] != '-'))
438 offset
=((str
[1]-'0')*10+(str
[2]-'0'))*60;
439 offset
+=(str
[3]-'0')*10+(str
[4]-'0');
443 atm
.type
=V_ASN1_UTCTIME
;
444 atm
.length
=sizeof(buff2
);
445 atm
.data
=(unsigned char *)buff2
;
447 X509_gmtime_adj(&atm
,-offset
);
449 i
=(buff1
[0]-'0')*10+(buff1
[1]-'0');
451 j
=(buff2
[0]-'0')*10+(buff2
[1]-'0');
454 if (i
< j
) return (-1);
455 if (i
> j
) return (1);
456 i
=strcmp(buff1
,buff2
);
457 if (i
== 0) /* wait a second then return younger :-) */
463 ASN1_UTCTIME
*X509_gmtime_adj(s
, adj
)
471 return(ASN1_UTCTIME_set(s
,t
));
474 int X509_get_pubkey_parameters(pkey
,chain
)
478 EVP_PKEY
*ktmp
=NULL
,*ktmp2
;
481 if ((pkey
!= NULL
) && !EVP_PKEY_missing_parameters(pkey
)) return(1);
483 for (i
=0; i
<sk_num(chain
); i
++)
485 ktmp
=X509_get_pubkey((X509
*)sk_value(chain
,i
));
488 X509err(X509_F_X509_GET_PUBKEY_PARAMETERS
,X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY
);
491 if (!EVP_PKEY_missing_parameters(ktmp
))
500 X509err(X509_F_X509_GET_PUBKEY_PARAMETERS
,X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN
);
504 /* first, populate the other certs */
505 for (j
=i
-1; j
>= 0; j
--)
507 ktmp2
=X509_get_pubkey((X509
*)sk_value(chain
,j
));
508 EVP_PKEY_copy_parameters(ktmp2
,ktmp
);
512 EVP_PKEY_copy_parameters(pkey
,ktmp
);
516 int X509_STORE_add_cert(ctx
,x
)
523 if (x
== NULL
) return(0);
524 obj
=(X509_OBJECT
*)Malloc(sizeof(X509_OBJECT
));
527 X509err(X509_F_X509_STORE_ADD_CERT
,ERR_R_MALLOC_FAILURE
);
530 obj
->type
=X509_LU_X509
;
533 CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE
);
535 X509_OBJECT_up_ref_count(obj
);
537 r
=(X509_OBJECT
*)lh_insert(ctx
->certs
,(char *)obj
);
539 { /* oops, put it back */
540 lh_delete(ctx
->certs
,(char *)obj
);
541 X509_OBJECT_free_contents(obj
);
543 lh_insert(ctx
->certs
,(char *)r
);
544 X509err(X509_F_X509_STORE_ADD_CERT
,X509_R_CERT_ALREADY_IN_HASH_TABLE
);
548 CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE
);
553 int X509_STORE_add_crl(ctx
,x
)
560 if (x
== NULL
) return(0);
561 obj
=(X509_OBJECT
*)Malloc(sizeof(X509_OBJECT
));
564 X509err(X509_F_X509_STORE_ADD_CRL
,ERR_R_MALLOC_FAILURE
);
567 obj
->type
=X509_LU_CRL
;
570 CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE
);
572 X509_OBJECT_up_ref_count(obj
);
574 r
=(X509_OBJECT
*)lh_insert(ctx
->certs
,(char *)obj
);
576 { /* oops, put it back */
577 lh_delete(ctx
->certs
,(char *)obj
);
578 X509_OBJECT_free_contents(obj
);
580 lh_insert(ctx
->certs
,(char *)r
);
581 X509err(X509_F_X509_STORE_ADD_CRL
,X509_R_CERT_ALREADY_IN_HASH_TABLE
);
585 CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE
);
590 int X509_STORE_CTX_get_ex_new_index(argl
,argp
,new_func
,dup_func
,free_func
)
597 x509_store_ctx_num
++;
598 return(CRYPTO_get_ex_new_index(x509_store_ctx_num
-1,
599 &x509_store_ctx_method
,
600 argl
,argp
,new_func
,dup_func
,free_func
));
603 int X509_STORE_CTX_set_ex_data(ctx
,idx
,data
)
608 return(CRYPTO_set_ex_data(&ctx
->ex_data
,idx
,data
));
611 char *X509_STORE_CTX_get_ex_data(ctx
,idx
)
615 return(CRYPTO_get_ex_data(&ctx
->ex_data
,idx
));
618 int X509_STORE_CTX_get_error(ctx
)
624 void X509_STORE_CTX_set_error(ctx
,err
)
631 int X509_STORE_CTX_get_error_depth(ctx
)
634 return(ctx
->error_depth
);
637 X509
*X509_STORE_CTX_get_current_cert(ctx
)
640 return(ctx
->current_cert
);
643 STACK
*X509_STORE_CTX_get_chain(ctx
)
649 void X509_STORE_CTX_set_cert(ctx
,x
)
656 void X509_STORE_CTX_set_chain(ctx
,sk
)