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