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>
65 #include <openssl/crypto.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>
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
;
78 static STACK
*x509_store_ctx_method
=NULL
;
79 static int x509_store_ctx_num
=0;
81 static int x509_store_num
=1;
82 static STACK
*x509_store_method
=NULL
;
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
;
105 STACK_OF(X509
) *sktmp
=NULL
;
107 if (ctx
->cert
== NULL
)
109 X509err(X509_F_X509_VERIFY_CERT
,X509_R_NO_CERT_SET_FOR_US_TO_VERIFY
);
113 cb
=ctx
->ctx
->verify_cb
;
114 if (cb
== NULL
) cb
=null_callback
;
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
)
120 if ( ((ctx
->chain
=sk_new_null()) == NULL
) ||
121 (!sk_push(ctx
->chain
,(char *)ctx
->cert
)))
123 X509err(X509_F_X509_VERIFY_CERT
,ERR_R_MALLOC_FAILURE
);
126 CRYPTO_add(&ctx
->cert
->references
,1,CRYPTO_LOCK_X509
);
127 ctx
->last_untrusted
=1;
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
)
134 X509err(X509_F_X509_VERIFY_CERT
,ERR_R_MALLOC_FAILURE
);
138 num
=sk_num(ctx
->chain
);
139 x
=(X509
*)sk_value(ctx
->chain
,num
-1);
145 /* If we have enough, we break */
146 if (depth
< num
) break;
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)
153 /* If we were passed a cert chain, use it first */
154 if (ctx
->untrusted
!= NULL
)
156 xtmp
=X509_find_by_subject(sktmp
,xn
);
159 if (!sk_push(ctx
->chain
,(char *)xtmp
))
161 X509err(X509_F_X509_VERIFY_CERT
,ERR_R_MALLOC_FAILURE
);
164 CRYPTO_add(&xtmp
->references
,1,CRYPTO_LOCK_X509
);
165 sk_X509_delete_ptr(sktmp
,xtmp
);
166 ctx
->last_untrusted
++;
169 /* reparse the full chain for
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. */
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
))
186 /* we have a self signed certificate */
187 if (sk_num(ctx
->chain
) == 1)
189 ctx
->error
=X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
;
191 ctx
->error_depth
=i
-1;
197 /* worry more about this one elsewhere */
198 chain_ss
=(X509
*)sk_pop(ctx
->chain
);
199 ctx
->last_untrusted
--;
201 x
=(X509
*)sk_value(ctx
->chain
,num
-1);
205 /* We now lookup certs from the certificate store */
208 /* If we have enough, we break */
209 if (depth
< num
) break;
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)
216 ok
=X509_STORE_get_by_subject(ctx
,X509_LU_X509
,xn
,&obj
);
217 if (ok
!= X509_LU_X509
)
219 if (ok
== X509_LU_RETRY
)
221 X509_OBJECT_free_contents(&obj
);
222 X509err(X509_F_X509_VERIFY_CERT
,X509_R_SHOULD_RETRY
);
225 else if (ok
!= X509_LU_FAIL
)
227 X509_OBJECT_free_contents(&obj
);
228 /* not good :-(, break anyway */
234 if (!sk_push(ctx
->chain
,(char *)obj
.data
.x509
))
236 X509_OBJECT_free_contents(&obj
);
237 X509err(X509_F_X509_VERIFY_CERT
,ERR_R_MALLOC_FAILURE
);
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)
247 if ((chain_ss
== NULL
) || (X509_NAME_cmp(X509_get_subject_name(chain_ss
),xn
) != 0))
249 if (ctx
->last_untrusted
>= num
)
250 ctx
->error
=X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
;
252 ctx
->error
=X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT
;
258 sk_push(ctx
->chain
,(char *)chain_ss
);
260 ctx
->last_untrusted
=num
;
261 ctx
->current_cert
=chain_ss
;
262 ctx
->error
=X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN
;
266 ctx
->error_depth
=num
-1;
271 /* We may as well copy down any DSA parameters that are required */
272 X509_get_pubkey_parameters(NULL
,ctx
->chain
);
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
);
278 ok
=internal_verify(ctx
);
282 X509_get_pubkey_parameters(NULL
,ctx
->chain
);
284 if (sktmp
!= NULL
) sk_X509_free(sktmp
);
285 if (chain_ss
!= NULL
) X509_free(chain_ss
);
289 static int internal_verify(X509_STORE_CTX
*ctx
)
296 cb
=ctx
->ctx
->verify_cb
;
297 if (cb
== NULL
) cb
=null_callback
;
299 n
=sk_num(ctx
->chain
);
300 ctx
->error_depth
=n
-1;
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)
310 ctx
->error
=X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE
;
311 ctx
->current_cert
=xi
;
319 xs
=(X509
*)sk_value(ctx
->chain
,n
);
323 /* ctx->error=0; not needed */
329 if ((pkey
=X509_get_pubkey(xi
)) == NULL
)
331 ctx
->error
=X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY
;
332 ctx
->current_cert
=xi
;
336 if (X509_verify(xs
,pkey
) <= 0)
339 ctx
->error
=X509_V_ERR_CERT_SIGNATURE_FAILURE
;
340 ctx
->current_cert
=xs
;
347 i
=X509_cmp_current_time(X509_get_notBefore(xs
));
350 ctx
->error
=X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD
;
351 ctx
->current_cert
=xs
;
357 ctx
->error
=X509_V_ERR_CERT_NOT_YET_VALID
;
358 ctx
->current_cert
=xs
;
365 i
=X509_cmp_current_time(X509_get_notAfter(xs
));
368 ctx
->error
=X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD
;
369 ctx
->current_cert
=xs
;
376 ctx
->error
=X509_V_ERR_CERT_HAS_EXPIRED
;
377 ctx
->current_cert
=xs
;
384 /* The last error (if any) is still in the error value */
385 ctx
->current_cert
=xs
;
393 xs
=(X509
*)sk_value(ctx
->chain
,n
);
401 int X509_cmp_current_time(ASN1_UTCTIME
*ctm
)
406 char buff1
[24],buff2
[24],*p
;
411 str
=(char *)ctm
->data
;
412 if ((i
< 11) || (i
> 17)) return(0);
417 if ((*str
== 'Z') || (*str
== '-') || (*str
== '+'))
418 { *(p
++)='0'; *(p
++)='0'; }
419 else { *(p
++)= *(str
++); *(p
++)= *(str
++); }
427 if ((*str
!= '+') && (str
[5] != '-'))
429 offset
=((str
[1]-'0')*10+(str
[2]-'0'))*60;
430 offset
+=(str
[3]-'0')*10+(str
[4]-'0');
434 atm
.type
=V_ASN1_UTCTIME
;
435 atm
.length
=sizeof(buff2
);
436 atm
.data
=(unsigned char *)buff2
;
438 X509_gmtime_adj(&atm
,-offset
);
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');
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 :-) */
454 ASN1_UTCTIME
*X509_gmtime_adj(ASN1_UTCTIME
*s
, long adj
)
460 return(ASN1_UTCTIME_set(s
,t
));
463 int X509_get_pubkey_parameters(EVP_PKEY
*pkey
, STACK
*chain
)
465 EVP_PKEY
*ktmp
=NULL
,*ktmp2
;
468 if ((pkey
!= NULL
) && !EVP_PKEY_missing_parameters(pkey
)) return(1);
470 for (i
=0; i
<sk_num(chain
); i
++)
472 ktmp
=X509_get_pubkey((X509
*)sk_value(chain
,i
));
475 X509err(X509_F_X509_GET_PUBKEY_PARAMETERS
,X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY
);
478 if (!EVP_PKEY_missing_parameters(ktmp
))
488 X509err(X509_F_X509_GET_PUBKEY_PARAMETERS
,X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN
);
492 /* first, populate the other certs */
493 for (j
=i
-1; j
>= 0; j
--)
495 ktmp2
=X509_get_pubkey((X509
*)sk_value(chain
,j
));
496 EVP_PKEY_copy_parameters(ktmp2
,ktmp
);
497 EVP_PKEY_free(ktmp2
);
500 if (pkey
!= NULL
) EVP_PKEY_copy_parameters(pkey
,ktmp
);
505 int X509_STORE_add_cert(X509_STORE
*ctx
, X509
*x
)
510 if (x
== NULL
) return(0);
511 obj
=(X509_OBJECT
*)Malloc(sizeof(X509_OBJECT
));
514 X509err(X509_F_X509_STORE_ADD_CERT
,ERR_R_MALLOC_FAILURE
);
517 obj
->type
=X509_LU_X509
;
520 CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE
);
522 X509_OBJECT_up_ref_count(obj
);
524 r
=(X509_OBJECT
*)lh_insert(ctx
->certs
,(char *)obj
);
526 { /* oops, put it back */
527 lh_delete(ctx
->certs
,(char *)obj
);
528 X509_OBJECT_free_contents(obj
);
530 lh_insert(ctx
->certs
,(char *)r
);
531 X509err(X509_F_X509_STORE_ADD_CERT
,X509_R_CERT_ALREADY_IN_HASH_TABLE
);
535 CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE
);
540 int X509_STORE_add_crl(X509_STORE
*ctx
, X509_CRL
*x
)
545 if (x
== NULL
) return(0);
546 obj
=(X509_OBJECT
*)Malloc(sizeof(X509_OBJECT
));
549 X509err(X509_F_X509_STORE_ADD_CRL
,ERR_R_MALLOC_FAILURE
);
552 obj
->type
=X509_LU_CRL
;
555 CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE
);
557 X509_OBJECT_up_ref_count(obj
);
559 r
=(X509_OBJECT
*)lh_insert(ctx
->certs
,(char *)obj
);
561 { /* oops, put it back */
562 lh_delete(ctx
->certs
,(char *)obj
);
563 X509_OBJECT_free_contents(obj
);
565 lh_insert(ctx
->certs
,(char *)r
);
566 X509err(X509_F_X509_STORE_ADD_CRL
,X509_R_CERT_ALREADY_IN_HASH_TABLE
);
570 CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE
);
575 int X509_STORE_CTX_get_ex_new_index(long argl
, char *argp
, int (*new_func
)(),
576 int (*dup_func
)(), void (*free_func
)())
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
));
584 int X509_STORE_CTX_set_ex_data(X509_STORE_CTX
*ctx
, int idx
, void *data
)
586 return(CRYPTO_set_ex_data(&ctx
->ex_data
,idx
,data
));
589 void *X509_STORE_CTX_get_ex_data(X509_STORE_CTX
*ctx
, int idx
)
591 return(CRYPTO_get_ex_data(&ctx
->ex_data
,idx
));
594 int X509_STORE_CTX_get_error(X509_STORE_CTX
*ctx
)
599 void X509_STORE_CTX_set_error(X509_STORE_CTX
*ctx
, int err
)
604 int X509_STORE_CTX_get_error_depth(X509_STORE_CTX
*ctx
)
606 return(ctx
->error_depth
);
609 X509
*X509_STORE_CTX_get_current_cert(X509_STORE_CTX
*ctx
)
611 return(ctx
->current_cert
);
614 STACK
*X509_STORE_CTX_get_chain(X509_STORE_CTX
*ctx
)
619 void X509_STORE_CTX_set_cert(X509_STORE_CTX
*ctx
, X509
*x
)
624 void X509_STORE_CTX_set_chain(X509_STORE_CTX
*ctx
, STACK_OF(X509
) *sk
)
629 IMPLEMENT_STACK_OF(X509
)
630 IMPLEMENT_ASN1_SET_OF(X509
)
631 IMPLEMENT_STACK_OF(X509_NAME
)
632 IMPLEMENT_STACK_OF(X509_ATTRIBUTE
)