1 /*
2  *  Error message information
3  *
4  *  Copyright The Mbed TLS Contributors
5  *  SPDX-License-Identifier: Apache-2.0
6  *
7  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
8  *  not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *  http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  */
19 
20 #include "common.h"
21 
22 #include "mbedtls/error.h"
23 
24 #if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY)
25 
26 #if defined(MBEDTLS_ERROR_C)
27 
28 #include "mbedtls/platform.h"
29 
30 #include <stdio.h>
31 #include <string.h>
32 
33 #if defined(MBEDTLS_AES_C)
34 #include "mbedtls/aes.h"
35 #endif
36 
37 #if defined(MBEDTLS_ARIA_C)
38 #include "mbedtls/aria.h"
39 #endif
40 
41 #if defined(MBEDTLS_ASN1_PARSE_C)
42 #include "mbedtls/asn1.h"
43 #endif
44 
45 #if defined(MBEDTLS_BASE64_C)
46 #include "mbedtls/base64.h"
47 #endif
48 
49 #if defined(MBEDTLS_BIGNUM_C)
50 #include "mbedtls/bignum.h"
51 #endif
52 
53 #if defined(MBEDTLS_CAMELLIA_C)
54 #include "mbedtls/camellia.h"
55 #endif
56 
57 #if defined(MBEDTLS_CCM_C)
58 #include "mbedtls/ccm.h"
59 #endif
60 
61 #if defined(MBEDTLS_CHACHA20_C)
62 #include "mbedtls/chacha20.h"
63 #endif
64 
65 #if defined(MBEDTLS_CHACHAPOLY_C)
66 #include "mbedtls/chachapoly.h"
67 #endif
68 
69 #if defined(MBEDTLS_CIPHER_C)
70 #include "mbedtls/cipher.h"
71 #endif
72 
73 #if defined(MBEDTLS_CTR_DRBG_C)
74 #include "mbedtls/ctr_drbg.h"
75 #endif
76 
77 #if defined(MBEDTLS_DES_C)
78 #include "mbedtls/des.h"
79 #endif
80 
81 #if defined(MBEDTLS_DHM_C)
82 #include "mbedtls/dhm.h"
83 #endif
84 
85 #if defined(MBEDTLS_ECP_C)
86 #include "mbedtls/ecp.h"
87 #endif
88 
89 #if defined(MBEDTLS_ENTROPY_C)
90 #include "mbedtls/entropy.h"
91 #endif
92 
93 #if defined(MBEDTLS_ERROR_C)
94 #include "mbedtls/error.h"
95 #endif
96 
97 #if defined(MBEDTLS_PLATFORM_C)
98 #include "mbedtls/platform.h"
99 #endif
100 
101 #if defined(MBEDTLS_GCM_C)
102 #include "mbedtls/gcm.h"
103 #endif
104 
105 #if defined(MBEDTLS_HKDF_C)
106 #include "mbedtls/hkdf.h"
107 #endif
108 
109 #if defined(MBEDTLS_HMAC_DRBG_C)
110 #include "mbedtls/hmac_drbg.h"
111 #endif
112 
113 #if defined(MBEDTLS_LMS_C)
114 #include "mbedtls/lms.h"
115 #endif
116 
117 #if defined(MBEDTLS_MD_C)
118 #include "mbedtls/md.h"
119 #endif
120 
121 #if defined(MBEDTLS_NET_C)
122 #include "mbedtls/net_sockets.h"
123 #endif
124 
125 #if defined(MBEDTLS_OID_C)
126 #include "mbedtls/oid.h"
127 #endif
128 
129 #if defined(MBEDTLS_PEM_PARSE_C) || defined(MBEDTLS_PEM_WRITE_C)
130 #include "mbedtls/pem.h"
131 #endif
132 
133 #if defined(MBEDTLS_PK_C)
134 #include "mbedtls/pk.h"
135 #endif
136 
137 #if defined(MBEDTLS_PKCS12_C)
138 #include "mbedtls/pkcs12.h"
139 #endif
140 
141 #if defined(MBEDTLS_PKCS5_C)
142 #include "mbedtls/pkcs5.h"
143 #endif
144 
145 #if defined(MBEDTLS_PKCS7_C)
146 #include "mbedtls/pkcs7.h"
147 #endif
148 
149 #if defined(MBEDTLS_POLY1305_C)
150 #include "mbedtls/poly1305.h"
151 #endif
152 
153 #if defined(MBEDTLS_RSA_C)
154 #include "mbedtls/rsa.h"
155 #endif
156 
157 #if defined(MBEDTLS_SHA1_C)
158 #include "mbedtls/sha1.h"
159 #endif
160 
161 #if defined(MBEDTLS_SHA256_C)
162 #include "mbedtls/sha256.h"
163 #endif
164 
165 #if defined(MBEDTLS_SHA512_C)
166 #include "mbedtls/sha512.h"
167 #endif
168 
169 #if defined(MBEDTLS_SSL_TLS_C)
170 #include "mbedtls/ssl.h"
171 #endif
172 
173 #if defined(MBEDTLS_THREADING_C)
174 #include "mbedtls/threading.h"
175 #endif
176 
177 #if defined(MBEDTLS_X509_USE_C) || defined(MBEDTLS_X509_CREATE_C)
178 #include "mbedtls/x509.h"
179 #endif
180 
181 
mbedtls_high_level_strerr(int error_code)182 const char *mbedtls_high_level_strerr(int error_code)
183 {
184     int high_level_error_code;
185 
186     if (error_code < 0) {
187         error_code = -error_code;
188     }
189 
190     /* Extract the high-level part from the error code. */
191     high_level_error_code = error_code & 0xFF80;
192 
193     switch (high_level_error_code) {
194     /* Begin Auto-Generated Code. */
195     #if defined(MBEDTLS_CIPHER_C)
196         case -(MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE):
197             return( "CIPHER - The selected feature is not available" );
198         case -(MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA):
199             return( "CIPHER - Bad input parameters" );
200         case -(MBEDTLS_ERR_CIPHER_ALLOC_FAILED):
201             return( "CIPHER - Failed to allocate memory" );
202         case -(MBEDTLS_ERR_CIPHER_INVALID_PADDING):
203             return( "CIPHER - Input data contains invalid padding and is rejected" );
204         case -(MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED):
205             return( "CIPHER - Decryption of block requires a full block" );
206         case -(MBEDTLS_ERR_CIPHER_AUTH_FAILED):
207             return( "CIPHER - Authentication failed (for AEAD modes)" );
208         case -(MBEDTLS_ERR_CIPHER_INVALID_CONTEXT):
209             return( "CIPHER - The context is invalid. For example, because it was freed" );
210 #endif /* MBEDTLS_CIPHER_C */
211 
212 #if defined(MBEDTLS_DHM_C)
213         case -(MBEDTLS_ERR_DHM_BAD_INPUT_DATA):
214             return( "DHM - Bad input parameters" );
215         case -(MBEDTLS_ERR_DHM_READ_PARAMS_FAILED):
216             return( "DHM - Reading of the DHM parameters failed" );
217         case -(MBEDTLS_ERR_DHM_MAKE_PARAMS_FAILED):
218             return( "DHM - Making of the DHM parameters failed" );
219         case -(MBEDTLS_ERR_DHM_READ_PUBLIC_FAILED):
220             return( "DHM - Reading of the public values failed" );
221         case -(MBEDTLS_ERR_DHM_MAKE_PUBLIC_FAILED):
222             return( "DHM - Making of the public value failed" );
223         case -(MBEDTLS_ERR_DHM_CALC_SECRET_FAILED):
224             return( "DHM - Calculation of the DHM secret failed" );
225         case -(MBEDTLS_ERR_DHM_INVALID_FORMAT):
226             return( "DHM - The ASN.1 data is not formatted correctly" );
227         case -(MBEDTLS_ERR_DHM_ALLOC_FAILED):
228             return( "DHM - Allocation of memory failed" );
229         case -(MBEDTLS_ERR_DHM_FILE_IO_ERROR):
230             return( "DHM - Read or write of file failed" );
231         case -(MBEDTLS_ERR_DHM_SET_GROUP_FAILED):
232             return( "DHM - Setting the modulus and generator failed" );
233 #endif /* MBEDTLS_DHM_C */
234 
235 #if defined(MBEDTLS_ECP_C)
236         case -(MBEDTLS_ERR_ECP_BAD_INPUT_DATA):
237             return( "ECP - Bad input parameters to function" );
238         case -(MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL):
239             return( "ECP - The buffer is too small to write to" );
240         case -(MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE):
241             return( "ECP - The requested feature is not available, for example, the requested curve is not supported" );
242         case -(MBEDTLS_ERR_ECP_VERIFY_FAILED):
243             return( "ECP - The signature is not valid" );
244         case -(MBEDTLS_ERR_ECP_ALLOC_FAILED):
245             return( "ECP - Memory allocation failed" );
246         case -(MBEDTLS_ERR_ECP_RANDOM_FAILED):
247             return( "ECP - Generation of random value, such as ephemeral key, failed" );
248         case -(MBEDTLS_ERR_ECP_INVALID_KEY):
249             return( "ECP - Invalid private or public key" );
250         case -(MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH):
251             return( "ECP - The buffer contains a valid signature followed by more data" );
252         case -(MBEDTLS_ERR_ECP_IN_PROGRESS):
253             return( "ECP - Operation in progress, call again with the same parameters to continue" );
254 #endif /* MBEDTLS_ECP_C */
255 
256 #if defined(MBEDTLS_MD_C)
257         case -(MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE):
258             return( "MD - The selected feature is not available" );
259         case -(MBEDTLS_ERR_MD_BAD_INPUT_DATA):
260             return( "MD - Bad input parameters to function" );
261         case -(MBEDTLS_ERR_MD_ALLOC_FAILED):
262             return( "MD - Failed to allocate memory" );
263         case -(MBEDTLS_ERR_MD_FILE_IO_ERROR):
264             return( "MD - Opening or reading of file failed" );
265 #endif /* MBEDTLS_MD_C */
266 
267 #if defined(MBEDTLS_PEM_PARSE_C) || defined(MBEDTLS_PEM_WRITE_C)
268         case -(MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT):
269             return( "PEM - No PEM header or footer found" );
270         case -(MBEDTLS_ERR_PEM_INVALID_DATA):
271             return( "PEM - PEM string is not as expected" );
272         case -(MBEDTLS_ERR_PEM_ALLOC_FAILED):
273             return( "PEM - Failed to allocate memory" );
274         case -(MBEDTLS_ERR_PEM_INVALID_ENC_IV):
275             return( "PEM - RSA IV is not in hex-format" );
276         case -(MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG):
277             return( "PEM - Unsupported key encryption algorithm" );
278         case -(MBEDTLS_ERR_PEM_PASSWORD_REQUIRED):
279             return( "PEM - Private key password can't be empty" );
280         case -(MBEDTLS_ERR_PEM_PASSWORD_MISMATCH):
281             return( "PEM - Given private key password does not allow for correct decryption" );
282         case -(MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE):
283             return( "PEM - Unavailable feature, e.g. hashing/encryption combination" );
284         case -(MBEDTLS_ERR_PEM_BAD_INPUT_DATA):
285             return( "PEM - Bad input parameters to function" );
286 #endif /* MBEDTLS_PEM_PARSE_C || MBEDTLS_PEM_WRITE_C */
287 
288 #if defined(MBEDTLS_PK_C)
289         case -(MBEDTLS_ERR_PK_ALLOC_FAILED):
290             return( "PK - Memory allocation failed" );
291         case -(MBEDTLS_ERR_PK_TYPE_MISMATCH):
292             return( "PK - Type mismatch, eg attempt to encrypt with an ECDSA key" );
293         case -(MBEDTLS_ERR_PK_BAD_INPUT_DATA):
294             return( "PK - Bad input parameters to function" );
295         case -(MBEDTLS_ERR_PK_FILE_IO_ERROR):
296             return( "PK - Read/write of file failed" );
297         case -(MBEDTLS_ERR_PK_KEY_INVALID_VERSION):
298             return( "PK - Unsupported key version" );
299         case -(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT):
300             return( "PK - Invalid key tag or value" );
301         case -(MBEDTLS_ERR_PK_UNKNOWN_PK_ALG):
302             return( "PK - Key algorithm is unsupported (only RSA and EC are supported)" );
303         case -(MBEDTLS_ERR_PK_PASSWORD_REQUIRED):
304             return( "PK - Private key password can't be empty" );
305         case -(MBEDTLS_ERR_PK_PASSWORD_MISMATCH):
306             return( "PK - Given private key password does not allow for correct decryption" );
307         case -(MBEDTLS_ERR_PK_INVALID_PUBKEY):
308             return( "PK - The pubkey tag or value is invalid (only RSA and EC are supported)" );
309         case -(MBEDTLS_ERR_PK_INVALID_ALG):
310             return( "PK - The algorithm tag or value is invalid" );
311         case -(MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE):
312             return( "PK - Elliptic curve is unsupported (only NIST curves are supported)" );
313         case -(MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE):
314             return( "PK - Unavailable feature, e.g. RSA disabled for RSA key" );
315         case -(MBEDTLS_ERR_PK_SIG_LEN_MISMATCH):
316             return( "PK - The buffer contains a valid signature followed by more data" );
317         case -(MBEDTLS_ERR_PK_BUFFER_TOO_SMALL):
318             return( "PK - The output buffer is too small" );
319 #endif /* MBEDTLS_PK_C */
320 
321 #if defined(MBEDTLS_PKCS12_C)
322         case -(MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA):
323             return( "PKCS12 - Bad input parameters to function" );
324         case -(MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE):
325             return( "PKCS12 - Feature not available, e.g. unsupported encryption scheme" );
326         case -(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT):
327             return( "PKCS12 - PBE ASN.1 data not as expected" );
328         case -(MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH):
329             return( "PKCS12 - Given private key password does not allow for correct decryption" );
330 #endif /* MBEDTLS_PKCS12_C */
331 
332 #if defined(MBEDTLS_PKCS5_C)
333         case -(MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA):
334             return( "PKCS5 - Bad input parameters to function" );
335         case -(MBEDTLS_ERR_PKCS5_INVALID_FORMAT):
336             return( "PKCS5 - Unexpected ASN.1 data" );
337         case -(MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE):
338             return( "PKCS5 - Requested encryption or digest alg not available" );
339         case -(MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH):
340             return( "PKCS5 - Given private key password does not allow for correct decryption" );
341 #endif /* MBEDTLS_PKCS5_C */
342 
343 #if defined(MBEDTLS_PKCS7_C)
344         case -(MBEDTLS_ERR_PKCS7_INVALID_FORMAT):
345             return( "PKCS7 - The format is invalid, e.g. different type expected" );
346         case -(MBEDTLS_ERR_PKCS7_FEATURE_UNAVAILABLE):
347             return( "PKCS7 - Unavailable feature, e.g. anything other than signed data" );
348         case -(MBEDTLS_ERR_PKCS7_INVALID_VERSION):
349             return( "PKCS7 - The PKCS #7 version element is invalid or cannot be parsed" );
350         case -(MBEDTLS_ERR_PKCS7_INVALID_CONTENT_INFO):
351             return( "PKCS7 - The PKCS #7 content info is invalid or cannot be parsed" );
352         case -(MBEDTLS_ERR_PKCS7_INVALID_ALG):
353             return( "PKCS7 - The algorithm tag or value is invalid or cannot be parsed" );
354         case -(MBEDTLS_ERR_PKCS7_INVALID_CERT):
355             return( "PKCS7 - The certificate tag or value is invalid or cannot be parsed" );
356         case -(MBEDTLS_ERR_PKCS7_INVALID_SIGNATURE):
357             return( "PKCS7 - Error parsing the signature" );
358         case -(MBEDTLS_ERR_PKCS7_INVALID_SIGNER_INFO):
359             return( "PKCS7 - Error parsing the signer's info" );
360         case -(MBEDTLS_ERR_PKCS7_BAD_INPUT_DATA):
361             return( "PKCS7 - Input invalid" );
362         case -(MBEDTLS_ERR_PKCS7_ALLOC_FAILED):
363             return( "PKCS7 - Allocation of memory failed" );
364         case -(MBEDTLS_ERR_PKCS7_VERIFY_FAIL):
365             return( "PKCS7 - Verification Failed" );
366         case -(MBEDTLS_ERR_PKCS7_CERT_DATE_INVALID):
367             return( "PKCS7 - The PKCS #7 date issued/expired dates are invalid" );
368 #endif /* MBEDTLS_PKCS7_C */
369 
370 #if defined(MBEDTLS_RSA_C)
371         case -(MBEDTLS_ERR_RSA_BAD_INPUT_DATA):
372             return( "RSA - Bad input parameters to function" );
373         case -(MBEDTLS_ERR_RSA_INVALID_PADDING):
374             return( "RSA - Input data contains invalid padding and is rejected" );
375         case -(MBEDTLS_ERR_RSA_KEY_GEN_FAILED):
376             return( "RSA - Something failed during generation of a key" );
377         case -(MBEDTLS_ERR_RSA_KEY_CHECK_FAILED):
378             return( "RSA - Key failed to pass the validity check of the library" );
379         case -(MBEDTLS_ERR_RSA_PUBLIC_FAILED):
380             return( "RSA - The public key operation failed" );
381         case -(MBEDTLS_ERR_RSA_PRIVATE_FAILED):
382             return( "RSA - The private key operation failed" );
383         case -(MBEDTLS_ERR_RSA_VERIFY_FAILED):
384             return( "RSA - The PKCS#1 verification failed" );
385         case -(MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE):
386             return( "RSA - The output buffer for decryption is not large enough" );
387         case -(MBEDTLS_ERR_RSA_RNG_FAILED):
388             return( "RSA - The random generator failed to generate non-zeros" );
389 #endif /* MBEDTLS_RSA_C */
390 
391 #if defined(MBEDTLS_SSL_TLS_C)
392         case -(MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS):
393             return( "SSL - A cryptographic operation is in progress. Try again later" );
394         case -(MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE):
395             return( "SSL - The requested feature is not available" );
396         case -(MBEDTLS_ERR_SSL_BAD_INPUT_DATA):
397             return( "SSL - Bad input parameters to function" );
398         case -(MBEDTLS_ERR_SSL_INVALID_MAC):
399             return( "SSL - Verification of the message MAC failed" );
400         case -(MBEDTLS_ERR_SSL_INVALID_RECORD):
401             return( "SSL - An invalid SSL record was received" );
402         case -(MBEDTLS_ERR_SSL_CONN_EOF):
403             return( "SSL - The connection indicated an EOF" );
404         case -(MBEDTLS_ERR_SSL_DECODE_ERROR):
405             return( "SSL - A message could not be parsed due to a syntactic error" );
406         case -(MBEDTLS_ERR_SSL_NO_RNG):
407             return( "SSL - No RNG was provided to the SSL module" );
408         case -(MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE):
409             return( "SSL - No client certification received from the client, but required by the authentication mode" );
410         case -(MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION):
411             return( "SSL - Client received an extended server hello containing an unsupported extension" );
412         case -(MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL):
413             return( "SSL - No ALPN protocols supported that the client advertises" );
414         case -(MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED):
415             return( "SSL - The own private key or pre-shared key is not set, but needed" );
416         case -(MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED):
417             return( "SSL - No CA Chain is set, but required to operate" );
418         case -(MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE):
419             return( "SSL - An unexpected message was received from our peer" );
420         case -(MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE):
421             return( "SSL - A fatal alert message was received from our peer" );
422         case -(MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME):
423             return( "SSL - No server could be identified matching the client's SNI" );
424         case -(MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY):
425             return( "SSL - The peer notified us that the connection is going to be closed" );
426         case -(MBEDTLS_ERR_SSL_BAD_CERTIFICATE):
427             return( "SSL - Processing of the Certificate handshake message failed" );
428         case -(MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET):
429             return( "SSL - * Received NewSessionTicket Post Handshake Message. This error code is experimental and may be changed or removed without notice" );
430         case -(MBEDTLS_ERR_SSL_CANNOT_READ_EARLY_DATA):
431             return( "SSL - Not possible to read early data" );
432         case -(MBEDTLS_ERR_SSL_CANNOT_WRITE_EARLY_DATA):
433             return( "SSL - Not possible to write early data" );
434         case -(MBEDTLS_ERR_SSL_ALLOC_FAILED):
435             return( "SSL - Memory allocation failed" );
436         case -(MBEDTLS_ERR_SSL_HW_ACCEL_FAILED):
437             return( "SSL - Hardware acceleration function returned with error" );
438         case -(MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH):
439             return( "SSL - Hardware acceleration function skipped / left alone data" );
440         case -(MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION):
441             return( "SSL - Handshake protocol not within min/max boundaries" );
442         case -(MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE):
443             return( "SSL - The handshake negotiation failed" );
444         case -(MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED):
445             return( "SSL - Session ticket has expired" );
446         case -(MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH):
447             return( "SSL - Public key type mismatch (eg, asked for RSA key exchange and presented EC key)" );
448         case -(MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY):
449             return( "SSL - Unknown identity received (eg, PSK identity)" );
450         case -(MBEDTLS_ERR_SSL_INTERNAL_ERROR):
451             return( "SSL - Internal error (eg, unexpected failure in lower-level module)" );
452         case -(MBEDTLS_ERR_SSL_COUNTER_WRAPPING):
453             return( "SSL - A counter would wrap (eg, too many messages exchanged)" );
454         case -(MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO):
455             return( "SSL - Unexpected message at ServerHello in renegotiation" );
456         case -(MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED):
457             return( "SSL - DTLS client must retry for hello verification" );
458         case -(MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL):
459             return( "SSL - A buffer is too small to receive or write a message" );
460         case -(MBEDTLS_ERR_SSL_WANT_READ):
461             return( "SSL - No data of requested type currently available on underlying transport" );
462         case -(MBEDTLS_ERR_SSL_WANT_WRITE):
463             return( "SSL - Connection requires a write call" );
464         case -(MBEDTLS_ERR_SSL_TIMEOUT):
465             return( "SSL - The operation timed out" );
466         case -(MBEDTLS_ERR_SSL_CLIENT_RECONNECT):
467             return( "SSL - The client initiated a reconnect from the same port" );
468         case -(MBEDTLS_ERR_SSL_UNEXPECTED_RECORD):
469             return( "SSL - Record header looks valid but is not expected" );
470         case -(MBEDTLS_ERR_SSL_NON_FATAL):
471             return( "SSL - The alert message received indicates a non-fatal error" );
472         case -(MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER):
473             return( "SSL - A field in a message was incorrect or inconsistent with other fields" );
474         case -(MBEDTLS_ERR_SSL_CONTINUE_PROCESSING):
475             return( "SSL - Internal-only message signaling that further message-processing should be done" );
476         case -(MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS):
477             return( "SSL - The asynchronous operation is not completed yet" );
478         case -(MBEDTLS_ERR_SSL_EARLY_MESSAGE):
479             return( "SSL - Internal-only message signaling that a message arrived early" );
480         case -(MBEDTLS_ERR_SSL_UNEXPECTED_CID):
481             return( "SSL - An encrypted DTLS-frame with an unexpected CID was received" );
482         case -(MBEDTLS_ERR_SSL_VERSION_MISMATCH):
483             return( "SSL - An operation failed due to an unexpected version or configuration" );
484         case -(MBEDTLS_ERR_SSL_BAD_CONFIG):
485             return( "SSL - Invalid value in SSL config" );
486 #endif /* MBEDTLS_SSL_TLS_C */
487 
488 #if defined(MBEDTLS_X509_USE_C) || defined(MBEDTLS_X509_CREATE_C)
489         case -(MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE):
490             return( "X509 - Unavailable feature, e.g. RSA hashing/encryption combination" );
491         case -(MBEDTLS_ERR_X509_UNKNOWN_OID):
492             return( "X509 - Requested OID is unknown" );
493         case -(MBEDTLS_ERR_X509_INVALID_FORMAT):
494             return( "X509 - The CRT/CRL/CSR format is invalid, e.g. different type expected" );
495         case -(MBEDTLS_ERR_X509_INVALID_VERSION):
496             return( "X509 - The CRT/CRL/CSR version element is invalid" );
497         case -(MBEDTLS_ERR_X509_INVALID_SERIAL):
498             return( "X509 - The serial tag or value is invalid" );
499         case -(MBEDTLS_ERR_X509_INVALID_ALG):
500             return( "X509 - The algorithm tag or value is invalid" );
501         case -(MBEDTLS_ERR_X509_INVALID_NAME):
502             return( "X509 - The name tag or value is invalid" );
503         case -(MBEDTLS_ERR_X509_INVALID_DATE):
504             return( "X509 - The date tag or value is invalid" );
505         case -(MBEDTLS_ERR_X509_INVALID_SIGNATURE):
506             return( "X509 - The signature tag or value invalid" );
507         case -(MBEDTLS_ERR_X509_INVALID_EXTENSIONS):
508             return( "X509 - The extension tag or value is invalid" );
509         case -(MBEDTLS_ERR_X509_UNKNOWN_VERSION):
510             return( "X509 - CRT/CRL/CSR has an unsupported version number" );
511         case -(MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG):
512             return( "X509 - Signature algorithm (oid) is unsupported" );
513         case -(MBEDTLS_ERR_X509_SIG_MISMATCH):
514             return( "X509 - Signature algorithms do not match. (see \\c ::mbedtls_x509_crt sig_oid)" );
515         case -(MBEDTLS_ERR_X509_CERT_VERIFY_FAILED):
516             return( "X509 - Certificate verification failed, e.g. CRL, CA or signature check failed" );
517         case -(MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT):
518             return( "X509 - Format not recognized as DER or PEM" );
519         case -(MBEDTLS_ERR_X509_BAD_INPUT_DATA):
520             return( "X509 - Input invalid" );
521         case -(MBEDTLS_ERR_X509_ALLOC_FAILED):
522             return( "X509 - Allocation of memory failed" );
523         case -(MBEDTLS_ERR_X509_FILE_IO_ERROR):
524             return( "X509 - Read/write of file failed" );
525         case -(MBEDTLS_ERR_X509_BUFFER_TOO_SMALL):
526             return( "X509 - Destination buffer is too small" );
527         case -(MBEDTLS_ERR_X509_FATAL_ERROR):
528             return( "X509 - A fatal error occurred, eg the chain is too long or the vrfy callback failed" );
529 #endif /* MBEDTLS_X509_USE_C || MBEDTLS_X509_CREATE_C */
530         /* End Auto-Generated Code. */
531 
532         default:
533             break;
534     }
535 
536     return NULL;
537 }
538 
mbedtls_low_level_strerr(int error_code)539 const char *mbedtls_low_level_strerr(int error_code)
540 {
541     int low_level_error_code;
542 
543     if (error_code < 0) {
544         error_code = -error_code;
545     }
546 
547     /* Extract the low-level part from the error code. */
548     low_level_error_code = error_code & ~0xFF80;
549 
550     switch (low_level_error_code) {
551     /* Begin Auto-Generated Code. */
552     #if defined(MBEDTLS_AES_C)
553         case -(MBEDTLS_ERR_AES_INVALID_KEY_LENGTH):
554             return( "AES - Invalid key length" );
555         case -(MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH):
556             return( "AES - Invalid data input length" );
557         case -(MBEDTLS_ERR_AES_BAD_INPUT_DATA):
558             return( "AES - Invalid input data" );
559 #endif /* MBEDTLS_AES_C */
560 
561 #if defined(MBEDTLS_ARIA_C)
562         case -(MBEDTLS_ERR_ARIA_BAD_INPUT_DATA):
563             return( "ARIA - Bad input data" );
564         case -(MBEDTLS_ERR_ARIA_INVALID_INPUT_LENGTH):
565             return( "ARIA - Invalid data input length" );
566 #endif /* MBEDTLS_ARIA_C */
567 
568 #if defined(MBEDTLS_ASN1_PARSE_C)
569         case -(MBEDTLS_ERR_ASN1_OUT_OF_DATA):
570             return( "ASN1 - Out of data when parsing an ASN1 data structure" );
571         case -(MBEDTLS_ERR_ASN1_UNEXPECTED_TAG):
572             return( "ASN1 - ASN1 tag was of an unexpected value" );
573         case -(MBEDTLS_ERR_ASN1_INVALID_LENGTH):
574             return( "ASN1 - Error when trying to determine the length or invalid length" );
575         case -(MBEDTLS_ERR_ASN1_LENGTH_MISMATCH):
576             return( "ASN1 - Actual length differs from expected length" );
577         case -(MBEDTLS_ERR_ASN1_INVALID_DATA):
578             return( "ASN1 - Data is invalid" );
579         case -(MBEDTLS_ERR_ASN1_ALLOC_FAILED):
580             return( "ASN1 - Memory allocation failed" );
581         case -(MBEDTLS_ERR_ASN1_BUF_TOO_SMALL):
582             return( "ASN1 - Buffer too small when writing ASN.1 data structure" );
583 #endif /* MBEDTLS_ASN1_PARSE_C */
584 
585 #if defined(MBEDTLS_BASE64_C)
586         case -(MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL):
587             return( "BASE64 - Output buffer too small" );
588         case -(MBEDTLS_ERR_BASE64_INVALID_CHARACTER):
589             return( "BASE64 - Invalid character in input" );
590 #endif /* MBEDTLS_BASE64_C */
591 
592 #if defined(MBEDTLS_BIGNUM_C)
593         case -(MBEDTLS_ERR_MPI_FILE_IO_ERROR):
594             return( "BIGNUM - An error occurred while reading from or writing to a file" );
595         case -(MBEDTLS_ERR_MPI_BAD_INPUT_DATA):
596             return( "BIGNUM - Bad input parameters to function" );
597         case -(MBEDTLS_ERR_MPI_INVALID_CHARACTER):
598             return( "BIGNUM - There is an invalid character in the digit string" );
599         case -(MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL):
600             return( "BIGNUM - The buffer is too small to write to" );
601         case -(MBEDTLS_ERR_MPI_NEGATIVE_VALUE):
602             return( "BIGNUM - The input arguments are negative or result in illegal output" );
603         case -(MBEDTLS_ERR_MPI_DIVISION_BY_ZERO):
604             return( "BIGNUM - The input argument for division is zero, which is not allowed" );
605         case -(MBEDTLS_ERR_MPI_NOT_ACCEPTABLE):
606             return( "BIGNUM - The input arguments are not acceptable" );
607         case -(MBEDTLS_ERR_MPI_ALLOC_FAILED):
608             return( "BIGNUM - Memory allocation failed" );
609 #endif /* MBEDTLS_BIGNUM_C */
610 
611 #if defined(MBEDTLS_CAMELLIA_C)
612         case -(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA):
613             return( "CAMELLIA - Bad input data" );
614         case -(MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH):
615             return( "CAMELLIA - Invalid data input length" );
616 #endif /* MBEDTLS_CAMELLIA_C */
617 
618 #if defined(MBEDTLS_CCM_C)
619         case -(MBEDTLS_ERR_CCM_BAD_INPUT):
620             return( "CCM - Bad input parameters to the function" );
621         case -(MBEDTLS_ERR_CCM_AUTH_FAILED):
622             return( "CCM - Authenticated decryption failed" );
623 #endif /* MBEDTLS_CCM_C */
624 
625 #if defined(MBEDTLS_CHACHA20_C)
626         case -(MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA):
627             return( "CHACHA20 - Invalid input parameter(s)" );
628 #endif /* MBEDTLS_CHACHA20_C */
629 
630 #if defined(MBEDTLS_CHACHAPOLY_C)
631         case -(MBEDTLS_ERR_CHACHAPOLY_BAD_STATE):
632             return( "CHACHAPOLY - The requested operation is not permitted in the current state" );
633         case -(MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED):
634             return( "CHACHAPOLY - Authenticated decryption failed: data was not authentic" );
635 #endif /* MBEDTLS_CHACHAPOLY_C */
636 
637 #if defined(MBEDTLS_CTR_DRBG_C)
638         case -(MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED):
639             return( "CTR_DRBG - The entropy source failed" );
640         case -(MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG):
641             return( "CTR_DRBG - The requested random buffer length is too big" );
642         case -(MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG):
643             return( "CTR_DRBG - The input (entropy + additional data) is too large" );
644         case -(MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR):
645             return( "CTR_DRBG - Read or write error in file" );
646 #endif /* MBEDTLS_CTR_DRBG_C */
647 
648 #if defined(MBEDTLS_DES_C)
649         case -(MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH):
650             return( "DES - The data input has an invalid length" );
651 #endif /* MBEDTLS_DES_C */
652 
653 #if defined(MBEDTLS_ENTROPY_C)
654         case -(MBEDTLS_ERR_ENTROPY_SOURCE_FAILED):
655             return( "ENTROPY - Critical entropy source failure" );
656         case -(MBEDTLS_ERR_ENTROPY_MAX_SOURCES):
657             return( "ENTROPY - No more sources can be added" );
658         case -(MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED):
659             return( "ENTROPY - No sources have been added to poll" );
660         case -(MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE):
661             return( "ENTROPY - No strong sources have been added to poll" );
662         case -(MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR):
663             return( "ENTROPY - Read/write error in file" );
664 #endif /* MBEDTLS_ENTROPY_C */
665 
666 #if defined(MBEDTLS_ERROR_C)
667         case -(MBEDTLS_ERR_ERROR_GENERIC_ERROR):
668             return( "ERROR - Generic error" );
669         case -(MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED):
670             return( "ERROR - This is a bug in the library" );
671 #endif /* MBEDTLS_ERROR_C */
672 
673 #if defined(MBEDTLS_PLATFORM_C)
674         case -(MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED):
675             return( "PLATFORM - Hardware accelerator failed" );
676         case -(MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED):
677             return( "PLATFORM - The requested feature is not supported by the platform" );
678 #endif /* MBEDTLS_PLATFORM_C */
679 
680 #if defined(MBEDTLS_GCM_C)
681         case -(MBEDTLS_ERR_GCM_AUTH_FAILED):
682             return( "GCM - Authenticated decryption failed" );
683         case -(MBEDTLS_ERR_GCM_BAD_INPUT):
684             return( "GCM - Bad input parameters to function" );
685         case -(MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL):
686             return( "GCM - An output buffer is too small" );
687 #endif /* MBEDTLS_GCM_C */
688 
689 #if defined(MBEDTLS_HKDF_C)
690         case -(MBEDTLS_ERR_HKDF_BAD_INPUT_DATA):
691             return( "HKDF - Bad input parameters to function" );
692 #endif /* MBEDTLS_HKDF_C */
693 
694 #if defined(MBEDTLS_HMAC_DRBG_C)
695         case -(MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG):
696             return( "HMAC_DRBG - Too many random requested in single call" );
697         case -(MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG):
698             return( "HMAC_DRBG - Input too large (Entropy + additional)" );
699         case -(MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR):
700             return( "HMAC_DRBG - Read/write error in file" );
701         case -(MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED):
702             return( "HMAC_DRBG - The entropy source failed" );
703 #endif /* MBEDTLS_HMAC_DRBG_C */
704 
705 #if defined(MBEDTLS_LMS_C)
706         case -(MBEDTLS_ERR_LMS_BAD_INPUT_DATA):
707             return( "LMS - Bad data has been input to an LMS function" );
708         case -(MBEDTLS_ERR_LMS_OUT_OF_PRIVATE_KEYS):
709             return( "LMS - Specified LMS key has utilised all of its private keys" );
710         case -(MBEDTLS_ERR_LMS_VERIFY_FAILED):
711             return( "LMS - LMS signature verification failed" );
712         case -(MBEDTLS_ERR_LMS_ALLOC_FAILED):
713             return( "LMS - LMS failed to allocate space for a private key" );
714         case -(MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL):
715             return( "LMS - Input/output buffer is too small to contain requited data" );
716 #endif /* MBEDTLS_LMS_C */
717 
718 #if defined(MBEDTLS_NET_C)
719         case -(MBEDTLS_ERR_NET_SOCKET_FAILED):
720             return( "NET - Failed to open a socket" );
721         case -(MBEDTLS_ERR_NET_CONNECT_FAILED):
722             return( "NET - The connection to the given server / port failed" );
723         case -(MBEDTLS_ERR_NET_BIND_FAILED):
724             return( "NET - Binding of the socket failed" );
725         case -(MBEDTLS_ERR_NET_LISTEN_FAILED):
726             return( "NET - Could not listen on the socket" );
727         case -(MBEDTLS_ERR_NET_ACCEPT_FAILED):
728             return( "NET - Could not accept the incoming connection" );
729         case -(MBEDTLS_ERR_NET_RECV_FAILED):
730             return( "NET - Reading information from the socket failed" );
731         case -(MBEDTLS_ERR_NET_SEND_FAILED):
732             return( "NET - Sending information through the socket failed" );
733         case -(MBEDTLS_ERR_NET_CONN_RESET):
734             return( "NET - Connection was reset by peer" );
735         case -(MBEDTLS_ERR_NET_UNKNOWN_HOST):
736             return( "NET - Failed to get an IP address for the given hostname" );
737         case -(MBEDTLS_ERR_NET_BUFFER_TOO_SMALL):
738             return( "NET - Buffer is too small to hold the data" );
739         case -(MBEDTLS_ERR_NET_INVALID_CONTEXT):
740             return( "NET - The context is invalid, eg because it was free()ed" );
741         case -(MBEDTLS_ERR_NET_POLL_FAILED):
742             return( "NET - Polling the net context failed" );
743         case -(MBEDTLS_ERR_NET_BAD_INPUT_DATA):
744             return( "NET - Input invalid" );
745 #endif /* MBEDTLS_NET_C */
746 
747 #if defined(MBEDTLS_OID_C)
748         case -(MBEDTLS_ERR_OID_NOT_FOUND):
749             return( "OID - OID is not found" );
750         case -(MBEDTLS_ERR_OID_BUF_TOO_SMALL):
751             return( "OID - output buffer is too small" );
752 #endif /* MBEDTLS_OID_C */
753 
754 #if defined(MBEDTLS_POLY1305_C)
755         case -(MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA):
756             return( "POLY1305 - Invalid input parameter(s)" );
757 #endif /* MBEDTLS_POLY1305_C */
758 
759 #if defined(MBEDTLS_SHA1_C)
760         case -(MBEDTLS_ERR_SHA1_BAD_INPUT_DATA):
761             return( "SHA1 - SHA-1 input data was malformed" );
762 #endif /* MBEDTLS_SHA1_C */
763 
764 #if defined(MBEDTLS_SHA256_C)
765         case -(MBEDTLS_ERR_SHA256_BAD_INPUT_DATA):
766             return( "SHA256 - SHA-256 input data was malformed" );
767 #endif /* MBEDTLS_SHA256_C */
768 
769 #if defined(MBEDTLS_SHA512_C)
770         case -(MBEDTLS_ERR_SHA512_BAD_INPUT_DATA):
771             return( "SHA512 - SHA-512 input data was malformed" );
772 #endif /* MBEDTLS_SHA512_C */
773 
774 #if defined(MBEDTLS_THREADING_C)
775         case -(MBEDTLS_ERR_THREADING_BAD_INPUT_DATA):
776             return( "THREADING - Bad input parameters to function" );
777         case -(MBEDTLS_ERR_THREADING_MUTEX_ERROR):
778             return( "THREADING - Locking / unlocking / free failed with error code" );
779 #endif /* MBEDTLS_THREADING_C */
780         /* End Auto-Generated Code. */
781 
782         default:
783             break;
784     }
785 
786     return NULL;
787 }
788 
mbedtls_strerror(int ret,char * buf,size_t buflen)789 void mbedtls_strerror(int ret, char *buf, size_t buflen)
790 {
791     size_t len;
792     int use_ret;
793     const char *high_level_error_description = NULL;
794     const char *low_level_error_description = NULL;
795 
796     if (buflen == 0) {
797         return;
798     }
799 
800     memset(buf, 0x00, buflen);
801 
802     if (ret < 0) {
803         ret = -ret;
804     }
805 
806     if (ret & 0xFF80) {
807         use_ret = ret & 0xFF80;
808 
809         // Translate high level error code.
810         high_level_error_description = mbedtls_high_level_strerr(ret);
811 
812         if (high_level_error_description == NULL) {
813             mbedtls_snprintf(buf, buflen, "UNKNOWN ERROR CODE (%04X)", (unsigned int) use_ret);
814         } else {
815             mbedtls_snprintf(buf, buflen, "%s", high_level_error_description);
816         }
817 
818 #if defined(MBEDTLS_SSL_TLS_C)
819         // Early return in case of a fatal error - do not try to translate low
820         // level code.
821         if (use_ret == -(MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE)) {
822             return;
823         }
824 #endif /* MBEDTLS_SSL_TLS_C */
825     }
826 
827     use_ret = ret & ~0xFF80;
828 
829     if (use_ret == 0) {
830         return;
831     }
832 
833     // If high level code is present, make a concatenation between both
834     // error strings.
835     //
836     len = strlen(buf);
837 
838     if (len > 0) {
839         if (buflen - len < 5) {
840             return;
841         }
842 
843         mbedtls_snprintf(buf + len, buflen - len, " : ");
844 
845         buf += len + 3;
846         buflen -= len + 3;
847     }
848 
849     // Translate low level error code.
850     low_level_error_description = mbedtls_low_level_strerr(ret);
851 
852     if (low_level_error_description == NULL) {
853         mbedtls_snprintf(buf, buflen, "UNKNOWN ERROR CODE (%04X)", (unsigned int) use_ret);
854     } else {
855         mbedtls_snprintf(buf, buflen, "%s", low_level_error_description);
856     }
857 }
858 
859 #else /* MBEDTLS_ERROR_C */
860 
861 /*
862  * Provide a dummy implementation when MBEDTLS_ERROR_C is not defined
863  */
mbedtls_strerror(int ret,char * buf,size_t buflen)864 void mbedtls_strerror(int ret, char *buf, size_t buflen)
865 {
866     ((void) ret);
867 
868     if (buflen > 0) {
869         buf[0] = '\0';
870     }
871 }
872 
873 #endif /* MBEDTLS_ERROR_C */
874 
875 #if defined(MBEDTLS_TEST_HOOKS)
876 void (*mbedtls_test_hook_error_add)(int, int, const char *, int);
877 #endif
878 
879 #endif /* MBEDTLS_ERROR_C || MBEDTLS_ERROR_STRERROR_DUMMY */
880