1*de0e0e4dSAntonio Huete Jimenez /* $OpenBSD: x509_txt.c,v 1.20 2022/07/05 20:31:46 tb Exp $ */
2f5b1c8a1SJohn Marino /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3f5b1c8a1SJohn Marino  * All rights reserved.
4f5b1c8a1SJohn Marino  *
5f5b1c8a1SJohn Marino  * This package is an SSL implementation written
6f5b1c8a1SJohn Marino  * by Eric Young (eay@cryptsoft.com).
7f5b1c8a1SJohn Marino  * The implementation was written so as to conform with Netscapes SSL.
8f5b1c8a1SJohn Marino  *
9f5b1c8a1SJohn Marino  * This library is free for commercial and non-commercial use as long as
10f5b1c8a1SJohn Marino  * the following conditions are aheared to.  The following conditions
11f5b1c8a1SJohn Marino  * apply to all code found in this distribution, be it the RC4, RSA,
12f5b1c8a1SJohn Marino  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13f5b1c8a1SJohn Marino  * included with this distribution is covered by the same copyright terms
14f5b1c8a1SJohn Marino  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15f5b1c8a1SJohn Marino  *
16f5b1c8a1SJohn Marino  * Copyright remains Eric Young's, and as such any Copyright notices in
17f5b1c8a1SJohn Marino  * the code are not to be removed.
18f5b1c8a1SJohn Marino  * If this package is used in a product, Eric Young should be given attribution
19f5b1c8a1SJohn Marino  * as the author of the parts of the library used.
20f5b1c8a1SJohn Marino  * This can be in the form of a textual message at program startup or
21f5b1c8a1SJohn Marino  * in documentation (online or textual) provided with the package.
22f5b1c8a1SJohn Marino  *
23f5b1c8a1SJohn Marino  * Redistribution and use in source and binary forms, with or without
24f5b1c8a1SJohn Marino  * modification, are permitted provided that the following conditions
25f5b1c8a1SJohn Marino  * are met:
26f5b1c8a1SJohn Marino  * 1. Redistributions of source code must retain the copyright
27f5b1c8a1SJohn Marino  *    notice, this list of conditions and the following disclaimer.
28f5b1c8a1SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
29f5b1c8a1SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
30f5b1c8a1SJohn Marino  *    documentation and/or other materials provided with the distribution.
31f5b1c8a1SJohn Marino  * 3. All advertising materials mentioning features or use of this software
32f5b1c8a1SJohn Marino  *    must display the following acknowledgement:
33f5b1c8a1SJohn Marino  *    "This product includes cryptographic software written by
34f5b1c8a1SJohn Marino  *     Eric Young (eay@cryptsoft.com)"
35f5b1c8a1SJohn Marino  *    The word 'cryptographic' can be left out if the rouines from the library
36f5b1c8a1SJohn Marino  *    being used are not cryptographic related :-).
37f5b1c8a1SJohn Marino  * 4. If you include any Windows specific code (or a derivative thereof) from
38f5b1c8a1SJohn Marino  *    the apps directory (application code) you must include an acknowledgement:
39f5b1c8a1SJohn Marino  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40f5b1c8a1SJohn Marino  *
41f5b1c8a1SJohn Marino  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42f5b1c8a1SJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43f5b1c8a1SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44f5b1c8a1SJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45f5b1c8a1SJohn Marino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46f5b1c8a1SJohn Marino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47f5b1c8a1SJohn Marino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48f5b1c8a1SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49f5b1c8a1SJohn Marino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50f5b1c8a1SJohn Marino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51f5b1c8a1SJohn Marino  * SUCH DAMAGE.
52f5b1c8a1SJohn Marino  *
53f5b1c8a1SJohn Marino  * The licence and distribution terms for any publically available version or
54f5b1c8a1SJohn Marino  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55f5b1c8a1SJohn Marino  * copied and put under another distribution licence
56f5b1c8a1SJohn Marino  * [including the GNU Public Licence.]
57f5b1c8a1SJohn Marino  */
58f5b1c8a1SJohn Marino 
59f5b1c8a1SJohn Marino #include <errno.h>
60f5b1c8a1SJohn Marino #include <stdio.h>
61f5b1c8a1SJohn Marino #include <time.h>
62f5b1c8a1SJohn Marino 
63f5b1c8a1SJohn Marino #include <openssl/asn1.h>
64f5b1c8a1SJohn Marino #include <openssl/buffer.h>
65f5b1c8a1SJohn Marino #include <openssl/evp.h>
66f5b1c8a1SJohn Marino #include <openssl/lhash.h>
67f5b1c8a1SJohn Marino #include <openssl/objects.h>
68f5b1c8a1SJohn Marino #include <openssl/x509.h>
69f5b1c8a1SJohn Marino 
70f5b1c8a1SJohn Marino const char *
X509_verify_cert_error_string(long n)71f5b1c8a1SJohn Marino X509_verify_cert_error_string(long n)
72f5b1c8a1SJohn Marino {
73f5b1c8a1SJohn Marino 	static char buf[100];
74f5b1c8a1SJohn Marino 
75f5b1c8a1SJohn Marino 	switch ((int)n) {
76f5b1c8a1SJohn Marino 	case X509_V_OK:
77f5b1c8a1SJohn Marino 		return("ok");
78f5b1c8a1SJohn Marino 	case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
79f5b1c8a1SJohn Marino 		return("unable to get issuer certificate");
80f5b1c8a1SJohn Marino 	case X509_V_ERR_UNABLE_TO_GET_CRL:
81f5b1c8a1SJohn Marino 		return("unable to get certificate CRL");
82f5b1c8a1SJohn Marino 	case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
83f5b1c8a1SJohn Marino 		return("unable to decrypt certificate's signature");
84f5b1c8a1SJohn Marino 	case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
85f5b1c8a1SJohn Marino 		return("unable to decrypt CRL's signature");
86f5b1c8a1SJohn Marino 	case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
87f5b1c8a1SJohn Marino 		return("unable to decode issuer public key");
88f5b1c8a1SJohn Marino 	case X509_V_ERR_CERT_SIGNATURE_FAILURE:
89f5b1c8a1SJohn Marino 		return("certificate signature failure");
90f5b1c8a1SJohn Marino 	case X509_V_ERR_CRL_SIGNATURE_FAILURE:
91f5b1c8a1SJohn Marino 		return("CRL signature failure");
92f5b1c8a1SJohn Marino 	case X509_V_ERR_CERT_NOT_YET_VALID:
93f5b1c8a1SJohn Marino 		return("certificate is not yet valid");
94f5b1c8a1SJohn Marino 	case X509_V_ERR_CRL_NOT_YET_VALID:
95f5b1c8a1SJohn Marino 		return("CRL is not yet valid");
96f5b1c8a1SJohn Marino 	case X509_V_ERR_CERT_HAS_EXPIRED:
97f5b1c8a1SJohn Marino 		return("certificate has expired");
98f5b1c8a1SJohn Marino 	case X509_V_ERR_CRL_HAS_EXPIRED:
99f5b1c8a1SJohn Marino 		return("CRL has expired");
100f5b1c8a1SJohn Marino 	case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
101f5b1c8a1SJohn Marino 		return("format error in certificate's notBefore field");
102f5b1c8a1SJohn Marino 	case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
103f5b1c8a1SJohn Marino 		return("format error in certificate's notAfter field");
104f5b1c8a1SJohn Marino 	case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
105f5b1c8a1SJohn Marino 		return("format error in CRL's lastUpdate field");
106f5b1c8a1SJohn Marino 	case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
107f5b1c8a1SJohn Marino 		return("format error in CRL's nextUpdate field");
108f5b1c8a1SJohn Marino 	case X509_V_ERR_OUT_OF_MEM:
109f5b1c8a1SJohn Marino 		return("out of memory");
110f5b1c8a1SJohn Marino 	case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
111f5b1c8a1SJohn Marino 		return("self signed certificate");
112f5b1c8a1SJohn Marino 	case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
113f5b1c8a1SJohn Marino 		return("self signed certificate in certificate chain");
114f5b1c8a1SJohn Marino 	case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
115f5b1c8a1SJohn Marino 		return("unable to get local issuer certificate");
116f5b1c8a1SJohn Marino 	case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
117f5b1c8a1SJohn Marino 		return("unable to verify the first certificate");
118f5b1c8a1SJohn Marino 	case X509_V_ERR_CERT_CHAIN_TOO_LONG:
119f5b1c8a1SJohn Marino 		return("certificate chain too long");
120f5b1c8a1SJohn Marino 	case X509_V_ERR_CERT_REVOKED:
121f5b1c8a1SJohn Marino 		return("certificate revoked");
122f5b1c8a1SJohn Marino 	case X509_V_ERR_INVALID_CA:
123f5b1c8a1SJohn Marino 		return ("invalid CA certificate");
124f5b1c8a1SJohn Marino 	case X509_V_ERR_INVALID_NON_CA:
125f5b1c8a1SJohn Marino 		return ("invalid non-CA certificate (has CA markings)");
126f5b1c8a1SJohn Marino 	case X509_V_ERR_PATH_LENGTH_EXCEEDED:
127f5b1c8a1SJohn Marino 		return ("path length constraint exceeded");
128f5b1c8a1SJohn Marino 	case X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED:
129f5b1c8a1SJohn Marino 		return("proxy path length constraint exceeded");
130f5b1c8a1SJohn Marino 	case X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED:
131f5b1c8a1SJohn Marino 		return("proxy certificates not allowed, please set the appropriate flag");
132f5b1c8a1SJohn Marino 	case X509_V_ERR_INVALID_PURPOSE:
133f5b1c8a1SJohn Marino 		return ("unsupported certificate purpose");
134f5b1c8a1SJohn Marino 	case X509_V_ERR_CERT_UNTRUSTED:
135f5b1c8a1SJohn Marino 		return ("certificate not trusted");
136f5b1c8a1SJohn Marino 	case X509_V_ERR_CERT_REJECTED:
137f5b1c8a1SJohn Marino 		return ("certificate rejected");
138f5b1c8a1SJohn Marino 	case X509_V_ERR_APPLICATION_VERIFICATION:
139f5b1c8a1SJohn Marino 		return("application verification failure");
140f5b1c8a1SJohn Marino 	case X509_V_ERR_SUBJECT_ISSUER_MISMATCH:
141f5b1c8a1SJohn Marino 		return("subject issuer mismatch");
142f5b1c8a1SJohn Marino 	case X509_V_ERR_AKID_SKID_MISMATCH:
143f5b1c8a1SJohn Marino 		return("authority and subject key identifier mismatch");
144f5b1c8a1SJohn Marino 	case X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH:
145f5b1c8a1SJohn Marino 		return("authority and issuer serial number mismatch");
146f5b1c8a1SJohn Marino 	case X509_V_ERR_KEYUSAGE_NO_CERTSIGN:
147f5b1c8a1SJohn Marino 		return("key usage does not include certificate signing");
148f5b1c8a1SJohn Marino 	case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
149f5b1c8a1SJohn Marino 		return("unable to get CRL issuer certificate");
150f5b1c8a1SJohn Marino 	case X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION:
151f5b1c8a1SJohn Marino 		return("unhandled critical extension");
152f5b1c8a1SJohn Marino 	case X509_V_ERR_KEYUSAGE_NO_CRL_SIGN:
153f5b1c8a1SJohn Marino 		return("key usage does not include CRL signing");
154f5b1c8a1SJohn Marino 	case X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE:
155f5b1c8a1SJohn Marino 		return("key usage does not include digital signature");
156f5b1c8a1SJohn Marino 	case X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION:
157f5b1c8a1SJohn Marino 		return("unhandled critical CRL extension");
158f5b1c8a1SJohn Marino 	case X509_V_ERR_INVALID_EXTENSION:
159f5b1c8a1SJohn Marino 		return("invalid or inconsistent certificate extension");
160f5b1c8a1SJohn Marino 	case X509_V_ERR_INVALID_POLICY_EXTENSION:
161f5b1c8a1SJohn Marino 		return("invalid or inconsistent certificate policy extension");
162f5b1c8a1SJohn Marino 	case X509_V_ERR_NO_EXPLICIT_POLICY:
163f5b1c8a1SJohn Marino 		return("no explicit policy");
164f5b1c8a1SJohn Marino 	case X509_V_ERR_DIFFERENT_CRL_SCOPE:
165f5b1c8a1SJohn Marino 		return("Different CRL scope");
166f5b1c8a1SJohn Marino 	case X509_V_ERR_UNSUPPORTED_EXTENSION_FEATURE:
167f5b1c8a1SJohn Marino 		return("Unsupported extension feature");
168f5b1c8a1SJohn Marino 	case X509_V_ERR_UNNESTED_RESOURCE:
169f5b1c8a1SJohn Marino 		return("RFC 3779 resource not subset of parent's resources");
170f5b1c8a1SJohn Marino 	case X509_V_ERR_PERMITTED_VIOLATION:
171f5b1c8a1SJohn Marino 		return("permitted subtree violation");
172f5b1c8a1SJohn Marino 	case X509_V_ERR_EXCLUDED_VIOLATION:
173f5b1c8a1SJohn Marino 		return("excluded subtree violation");
174f5b1c8a1SJohn Marino 	case X509_V_ERR_SUBTREE_MINMAX:
175f5b1c8a1SJohn Marino 		return("name constraints minimum and maximum not supported");
176f5b1c8a1SJohn Marino 	case X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE:
177f5b1c8a1SJohn Marino 		return("unsupported name constraint type");
178f5b1c8a1SJohn Marino 	case X509_V_ERR_UNSUPPORTED_CONSTRAINT_SYNTAX:
179f5b1c8a1SJohn Marino 		return("unsupported or invalid name constraint syntax");
180f5b1c8a1SJohn Marino 	case X509_V_ERR_UNSUPPORTED_NAME_SYNTAX:
181f5b1c8a1SJohn Marino 		return("unsupported or invalid name syntax");
182f5b1c8a1SJohn Marino 	case X509_V_ERR_CRL_PATH_VALIDATION_ERROR:
183f5b1c8a1SJohn Marino 		return("CRL path validation error");
184*de0e0e4dSAntonio Huete Jimenez 	case X509_V_ERR_HOSTNAME_MISMATCH:
185*de0e0e4dSAntonio Huete Jimenez 		return("Hostname mismatch");
186*de0e0e4dSAntonio Huete Jimenez 	case X509_V_ERR_EMAIL_MISMATCH:
187*de0e0e4dSAntonio Huete Jimenez 		return("Email address mismatch");
188*de0e0e4dSAntonio Huete Jimenez 	case X509_V_ERR_IP_ADDRESS_MISMATCH:
189*de0e0e4dSAntonio Huete Jimenez 		return("IP address mismatch");
190*de0e0e4dSAntonio Huete Jimenez 	case X509_V_ERR_INVALID_CALL:
191*de0e0e4dSAntonio Huete Jimenez 		return("Invalid certificate verification context");
192*de0e0e4dSAntonio Huete Jimenez 	case X509_V_ERR_STORE_LOOKUP:
193*de0e0e4dSAntonio Huete Jimenez 		return("Issuer certificate lookup error");
194*de0e0e4dSAntonio Huete Jimenez 	case X509_V_ERR_EE_KEY_TOO_SMALL:
195*de0e0e4dSAntonio Huete Jimenez 		return("EE certificate key too weak");
196*de0e0e4dSAntonio Huete Jimenez 	case X509_V_ERR_CA_KEY_TOO_SMALL:
197*de0e0e4dSAntonio Huete Jimenez 		return("CA certificate key too weak");
198*de0e0e4dSAntonio Huete Jimenez 	case X509_V_ERR_CA_MD_TOO_WEAK:
199*de0e0e4dSAntonio Huete Jimenez 		return("CA signature digest algorithm too weak");
200f5b1c8a1SJohn Marino 
201f5b1c8a1SJohn Marino 	default:
202f5b1c8a1SJohn Marino 		(void) snprintf(buf, sizeof buf, "error number %ld", n);
203f5b1c8a1SJohn Marino 		return(buf);
204f5b1c8a1SJohn Marino 	}
205f5b1c8a1SJohn Marino }
206