1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci#include <stdio.h>
11e1051a39Sopenharmony_ci#include <time.h>
12e1051a39Sopenharmony_ci#include <errno.h>
13e1051a39Sopenharmony_ci
14e1051a39Sopenharmony_ci#include "internal/cryptlib.h"
15e1051a39Sopenharmony_ci#include <openssl/buffer.h>
16e1051a39Sopenharmony_ci#include <openssl/evp.h>
17e1051a39Sopenharmony_ci#include <openssl/asn1.h>
18e1051a39Sopenharmony_ci#include <openssl/x509.h>
19e1051a39Sopenharmony_ci#include <openssl/objects.h>
20e1051a39Sopenharmony_ci
21e1051a39Sopenharmony_ciconst char *X509_verify_cert_error_string(long n)
22e1051a39Sopenharmony_ci{
23e1051a39Sopenharmony_ci    switch ((int)n) {
24e1051a39Sopenharmony_ci    case X509_V_OK:
25e1051a39Sopenharmony_ci        return "ok";
26e1051a39Sopenharmony_ci    case X509_V_ERR_UNSPECIFIED:
27e1051a39Sopenharmony_ci        return "unspecified certificate verification error";
28e1051a39Sopenharmony_ci    case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
29e1051a39Sopenharmony_ci        return "unable to get issuer certificate";
30e1051a39Sopenharmony_ci    case X509_V_ERR_UNABLE_TO_GET_CRL:
31e1051a39Sopenharmony_ci        return "unable to get certificate CRL";
32e1051a39Sopenharmony_ci    case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
33e1051a39Sopenharmony_ci        return "unable to decrypt certificate's signature";
34e1051a39Sopenharmony_ci    case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
35e1051a39Sopenharmony_ci        return "unable to decrypt CRL's signature";
36e1051a39Sopenharmony_ci    case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
37e1051a39Sopenharmony_ci        return "unable to decode issuer public key";
38e1051a39Sopenharmony_ci    case X509_V_ERR_CERT_SIGNATURE_FAILURE:
39e1051a39Sopenharmony_ci        return "certificate signature failure";
40e1051a39Sopenharmony_ci    case X509_V_ERR_CRL_SIGNATURE_FAILURE:
41e1051a39Sopenharmony_ci        return "CRL signature failure";
42e1051a39Sopenharmony_ci    case X509_V_ERR_CERT_NOT_YET_VALID:
43e1051a39Sopenharmony_ci        return "certificate is not yet valid";
44e1051a39Sopenharmony_ci    case X509_V_ERR_CERT_HAS_EXPIRED:
45e1051a39Sopenharmony_ci        return "certificate has expired";
46e1051a39Sopenharmony_ci    case X509_V_ERR_CRL_NOT_YET_VALID:
47e1051a39Sopenharmony_ci        return "CRL is not yet valid";
48e1051a39Sopenharmony_ci    case X509_V_ERR_CRL_HAS_EXPIRED:
49e1051a39Sopenharmony_ci        return "CRL has expired";
50e1051a39Sopenharmony_ci    case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
51e1051a39Sopenharmony_ci        return "format error in certificate's notBefore field";
52e1051a39Sopenharmony_ci    case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
53e1051a39Sopenharmony_ci        return "format error in certificate's notAfter field";
54e1051a39Sopenharmony_ci    case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
55e1051a39Sopenharmony_ci        return "format error in CRL's lastUpdate field";
56e1051a39Sopenharmony_ci    case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
57e1051a39Sopenharmony_ci        return "format error in CRL's nextUpdate field";
58e1051a39Sopenharmony_ci    case X509_V_ERR_OUT_OF_MEM:
59e1051a39Sopenharmony_ci        return "out of memory";
60e1051a39Sopenharmony_ci    case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
61e1051a39Sopenharmony_ci        return "self-signed certificate";
62e1051a39Sopenharmony_ci    case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
63e1051a39Sopenharmony_ci        return "self-signed certificate in certificate chain";
64e1051a39Sopenharmony_ci    case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
65e1051a39Sopenharmony_ci        return "unable to get local issuer certificate";
66e1051a39Sopenharmony_ci    case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
67e1051a39Sopenharmony_ci        return "unable to verify the first certificate";
68e1051a39Sopenharmony_ci    case X509_V_ERR_CERT_CHAIN_TOO_LONG:
69e1051a39Sopenharmony_ci        return "certificate chain too long";
70e1051a39Sopenharmony_ci    case X509_V_ERR_CERT_REVOKED:
71e1051a39Sopenharmony_ci        return "certificate revoked";
72e1051a39Sopenharmony_ci    case X509_V_ERR_NO_ISSUER_PUBLIC_KEY:
73e1051a39Sopenharmony_ci        return "issuer certificate doesn't have a public key";
74e1051a39Sopenharmony_ci    case X509_V_ERR_PATH_LENGTH_EXCEEDED:
75e1051a39Sopenharmony_ci        return "path length constraint exceeded";
76e1051a39Sopenharmony_ci    case X509_V_ERR_INVALID_PURPOSE:
77e1051a39Sopenharmony_ci        return "unsuitable certificate purpose";
78e1051a39Sopenharmony_ci    case X509_V_ERR_CERT_UNTRUSTED:
79e1051a39Sopenharmony_ci        return "certificate not trusted";
80e1051a39Sopenharmony_ci    case X509_V_ERR_CERT_REJECTED:
81e1051a39Sopenharmony_ci        return "certificate rejected";
82e1051a39Sopenharmony_ci    case X509_V_ERR_SUBJECT_ISSUER_MISMATCH:
83e1051a39Sopenharmony_ci        return "subject issuer mismatch";
84e1051a39Sopenharmony_ci    case X509_V_ERR_AKID_SKID_MISMATCH:
85e1051a39Sopenharmony_ci        return "authority and subject key identifier mismatch";
86e1051a39Sopenharmony_ci    case X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH:
87e1051a39Sopenharmony_ci        return "authority and issuer serial number mismatch";
88e1051a39Sopenharmony_ci    case X509_V_ERR_KEYUSAGE_NO_CERTSIGN:
89e1051a39Sopenharmony_ci        return "key usage does not include certificate signing";
90e1051a39Sopenharmony_ci    case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
91e1051a39Sopenharmony_ci        return "unable to get CRL issuer certificate";
92e1051a39Sopenharmony_ci    case X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION:
93e1051a39Sopenharmony_ci        return "unhandled critical extension";
94e1051a39Sopenharmony_ci    case X509_V_ERR_KEYUSAGE_NO_CRL_SIGN:
95e1051a39Sopenharmony_ci        return "key usage does not include CRL signing";
96e1051a39Sopenharmony_ci    case X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION:
97e1051a39Sopenharmony_ci        return "unhandled critical CRL extension";
98e1051a39Sopenharmony_ci    case X509_V_ERR_INVALID_NON_CA:
99e1051a39Sopenharmony_ci        return "invalid non-CA certificate (has CA markings)";
100e1051a39Sopenharmony_ci    case X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED:
101e1051a39Sopenharmony_ci        return "proxy path length constraint exceeded";
102e1051a39Sopenharmony_ci    case X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE:
103e1051a39Sopenharmony_ci        return "key usage does not include digital signature";
104e1051a39Sopenharmony_ci    case X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED:
105e1051a39Sopenharmony_ci        return
106e1051a39Sopenharmony_ci            "proxy certificates not allowed, please set the appropriate flag";
107e1051a39Sopenharmony_ci    case X509_V_ERR_INVALID_EXTENSION:
108e1051a39Sopenharmony_ci        return "invalid or inconsistent certificate extension";
109e1051a39Sopenharmony_ci    case X509_V_ERR_INVALID_POLICY_EXTENSION:
110e1051a39Sopenharmony_ci        return "invalid or inconsistent certificate policy extension";
111e1051a39Sopenharmony_ci    case X509_V_ERR_NO_EXPLICIT_POLICY:
112e1051a39Sopenharmony_ci        return "no explicit policy";
113e1051a39Sopenharmony_ci    case X509_V_ERR_DIFFERENT_CRL_SCOPE:
114e1051a39Sopenharmony_ci        return "different CRL scope";
115e1051a39Sopenharmony_ci    case X509_V_ERR_UNSUPPORTED_EXTENSION_FEATURE:
116e1051a39Sopenharmony_ci        return "unsupported extension feature";
117e1051a39Sopenharmony_ci    case X509_V_ERR_UNNESTED_RESOURCE:
118e1051a39Sopenharmony_ci        return "RFC 3779 resource not subset of parent's resources";
119e1051a39Sopenharmony_ci    case X509_V_ERR_PERMITTED_VIOLATION:
120e1051a39Sopenharmony_ci        return "permitted subtree violation";
121e1051a39Sopenharmony_ci    case X509_V_ERR_EXCLUDED_VIOLATION:
122e1051a39Sopenharmony_ci        return "excluded subtree violation";
123e1051a39Sopenharmony_ci    case X509_V_ERR_SUBTREE_MINMAX:
124e1051a39Sopenharmony_ci        return "name constraints minimum and maximum not supported";
125e1051a39Sopenharmony_ci    case X509_V_ERR_APPLICATION_VERIFICATION:
126e1051a39Sopenharmony_ci        return "application verification failure";
127e1051a39Sopenharmony_ci    case X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE:
128e1051a39Sopenharmony_ci        return "unsupported name constraint type";
129e1051a39Sopenharmony_ci    case X509_V_ERR_UNSUPPORTED_CONSTRAINT_SYNTAX:
130e1051a39Sopenharmony_ci        return "unsupported or invalid name constraint syntax";
131e1051a39Sopenharmony_ci    case X509_V_ERR_UNSUPPORTED_NAME_SYNTAX:
132e1051a39Sopenharmony_ci        return "unsupported or invalid name syntax";
133e1051a39Sopenharmony_ci    case X509_V_ERR_CRL_PATH_VALIDATION_ERROR:
134e1051a39Sopenharmony_ci        return "CRL path validation error";
135e1051a39Sopenharmony_ci    case X509_V_ERR_PATH_LOOP:
136e1051a39Sopenharmony_ci        return "path loop";
137e1051a39Sopenharmony_ci    case X509_V_ERR_SUITE_B_INVALID_VERSION:
138e1051a39Sopenharmony_ci        return "Suite B: certificate version invalid";
139e1051a39Sopenharmony_ci    case X509_V_ERR_SUITE_B_INVALID_ALGORITHM:
140e1051a39Sopenharmony_ci        return "Suite B: invalid public key algorithm";
141e1051a39Sopenharmony_ci    case X509_V_ERR_SUITE_B_INVALID_CURVE:
142e1051a39Sopenharmony_ci        return "Suite B: invalid ECC curve";
143e1051a39Sopenharmony_ci    case X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM:
144e1051a39Sopenharmony_ci        return "Suite B: invalid signature algorithm";
145e1051a39Sopenharmony_ci    case X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED:
146e1051a39Sopenharmony_ci        return "Suite B: curve not allowed for this LOS";
147e1051a39Sopenharmony_ci    case X509_V_ERR_SUITE_B_CANNOT_SIGN_P_384_WITH_P_256:
148e1051a39Sopenharmony_ci        return "Suite B: cannot sign P-384 with P-256";
149e1051a39Sopenharmony_ci    case X509_V_ERR_HOSTNAME_MISMATCH:
150e1051a39Sopenharmony_ci        return "hostname mismatch";
151e1051a39Sopenharmony_ci    case X509_V_ERR_EMAIL_MISMATCH:
152e1051a39Sopenharmony_ci        return "email address mismatch";
153e1051a39Sopenharmony_ci    case X509_V_ERR_IP_ADDRESS_MISMATCH:
154e1051a39Sopenharmony_ci        return "IP address mismatch";
155e1051a39Sopenharmony_ci    case X509_V_ERR_DANE_NO_MATCH:
156e1051a39Sopenharmony_ci        return "no matching DANE TLSA records";
157e1051a39Sopenharmony_ci    case X509_V_ERR_EE_KEY_TOO_SMALL:
158e1051a39Sopenharmony_ci        return "EE certificate key too weak";
159e1051a39Sopenharmony_ci    case X509_V_ERR_CA_KEY_TOO_SMALL:
160e1051a39Sopenharmony_ci        return "CA certificate key too weak";
161e1051a39Sopenharmony_ci    case X509_V_ERR_CA_MD_TOO_WEAK:
162e1051a39Sopenharmony_ci        return "CA signature digest algorithm too weak";
163e1051a39Sopenharmony_ci    case X509_V_ERR_INVALID_CALL:
164e1051a39Sopenharmony_ci        return "invalid certificate verification context";
165e1051a39Sopenharmony_ci    case X509_V_ERR_STORE_LOOKUP:
166e1051a39Sopenharmony_ci        return "issuer certificate lookup error";
167e1051a39Sopenharmony_ci    case X509_V_ERR_NO_VALID_SCTS:
168e1051a39Sopenharmony_ci        return "Certificate Transparency required, but no valid SCTs found";
169e1051a39Sopenharmony_ci    case X509_V_ERR_PROXY_SUBJECT_NAME_VIOLATION:
170e1051a39Sopenharmony_ci        return "proxy subject name violation";
171e1051a39Sopenharmony_ci    case X509_V_ERR_OCSP_VERIFY_NEEDED:
172e1051a39Sopenharmony_ci        return "OCSP verification needed";
173e1051a39Sopenharmony_ci    case X509_V_ERR_OCSP_VERIFY_FAILED:
174e1051a39Sopenharmony_ci        return "OCSP verification failed";
175e1051a39Sopenharmony_ci    case X509_V_ERR_OCSP_CERT_UNKNOWN:
176e1051a39Sopenharmony_ci        return "OCSP unknown cert";
177e1051a39Sopenharmony_ci    case X509_V_ERR_UNSUPPORTED_SIGNATURE_ALGORITHM:
178e1051a39Sopenharmony_ci        return "Cannot find certificate signature algorithm";
179e1051a39Sopenharmony_ci    case X509_V_ERR_SIGNATURE_ALGORITHM_MISMATCH:
180e1051a39Sopenharmony_ci        return "subject signature algorithm and issuer public key algorithm mismatch";
181e1051a39Sopenharmony_ci    case X509_V_ERR_SIGNATURE_ALGORITHM_INCONSISTENCY:
182e1051a39Sopenharmony_ci        return "cert info signature and signature algorithm mismatch";
183e1051a39Sopenharmony_ci    case X509_V_ERR_INVALID_CA:
184e1051a39Sopenharmony_ci        return "invalid CA certificate";
185e1051a39Sopenharmony_ci    case X509_V_ERR_PATHLEN_INVALID_FOR_NON_CA:
186e1051a39Sopenharmony_ci        return "Path length invalid for non-CA cert";
187e1051a39Sopenharmony_ci    case X509_V_ERR_PATHLEN_WITHOUT_KU_KEY_CERT_SIGN:
188e1051a39Sopenharmony_ci        return "Path length given without key usage keyCertSign";
189e1051a39Sopenharmony_ci    case X509_V_ERR_KU_KEY_CERT_SIGN_INVALID_FOR_NON_CA:
190e1051a39Sopenharmony_ci        return "Key usage keyCertSign invalid for non-CA cert";
191e1051a39Sopenharmony_ci    case X509_V_ERR_ISSUER_NAME_EMPTY:
192e1051a39Sopenharmony_ci        return "Issuer name empty";
193e1051a39Sopenharmony_ci    case X509_V_ERR_SUBJECT_NAME_EMPTY:
194e1051a39Sopenharmony_ci        return "Subject name empty";
195e1051a39Sopenharmony_ci    case X509_V_ERR_MISSING_AUTHORITY_KEY_IDENTIFIER:
196e1051a39Sopenharmony_ci        return "Missing Authority Key Identifier";
197e1051a39Sopenharmony_ci    case X509_V_ERR_MISSING_SUBJECT_KEY_IDENTIFIER:
198e1051a39Sopenharmony_ci        return "Missing Subject Key Identifier";
199e1051a39Sopenharmony_ci    case X509_V_ERR_EMPTY_SUBJECT_ALT_NAME:
200e1051a39Sopenharmony_ci        return "Empty Subject Alternative Name extension";
201e1051a39Sopenharmony_ci    case X509_V_ERR_CA_BCONS_NOT_CRITICAL:
202e1051a39Sopenharmony_ci        return "Basic Constraints of CA cert not marked critical";
203e1051a39Sopenharmony_ci    case X509_V_ERR_EMPTY_SUBJECT_SAN_NOT_CRITICAL:
204e1051a39Sopenharmony_ci        return "Subject empty and Subject Alt Name extension not critical";
205e1051a39Sopenharmony_ci    case X509_V_ERR_AUTHORITY_KEY_IDENTIFIER_CRITICAL:
206e1051a39Sopenharmony_ci        return "Authority Key Identifier marked critical";
207e1051a39Sopenharmony_ci    case X509_V_ERR_SUBJECT_KEY_IDENTIFIER_CRITICAL:
208e1051a39Sopenharmony_ci        return "Subject Key Identifier marked critical";
209e1051a39Sopenharmony_ci    case X509_V_ERR_CA_CERT_MISSING_KEY_USAGE:
210e1051a39Sopenharmony_ci        return "CA cert does not include key usage extension";
211e1051a39Sopenharmony_ci    case X509_V_ERR_EXTENSIONS_REQUIRE_VERSION_3:
212e1051a39Sopenharmony_ci        return "Using cert extension requires at least X509v3";
213e1051a39Sopenharmony_ci    case X509_V_ERR_EC_KEY_EXPLICIT_PARAMS:
214e1051a39Sopenharmony_ci        return "Certificate public key has explicit ECC parameters";
215e1051a39Sopenharmony_ci
216e1051a39Sopenharmony_ci        /*
217e1051a39Sopenharmony_ci         * Entries must be kept consistent with include/openssl/x509_vfy.h.in
218e1051a39Sopenharmony_ci         * and with doc/man3/X509_STORE_CTX_get_error.pod
219e1051a39Sopenharmony_ci         */
220e1051a39Sopenharmony_ci
221e1051a39Sopenharmony_ci    default:
222e1051a39Sopenharmony_ci        /* Printing an error number into a static buffer is not thread-safe */
223e1051a39Sopenharmony_ci        return "unknown certificate verification error";
224e1051a39Sopenharmony_ci    }
225e1051a39Sopenharmony_ci}
226