| Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 1 | /* | 
|  | 2 | *  Error message information | 
|  | 3 | * | 
| Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | *  Copyright The Mbed TLS Contributors | 
| Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 5 | *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later | 
| Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 6 | */ | 
|  | 7 |  | 
| Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 8 | #include "common.h" | 
| Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 9 |  | 
| Gilles Peskine | 67aed9a | 2020-11-09 15:14:10 +0100 | [diff] [blame] | 10 | #include "mbedtls/error.h" | 
|  | 11 |  | 
|  | 12 | #if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY) | 
|  | 13 |  | 
| Gilles Peskine | b64e0fe | 2020-11-09 15:14:10 +0100 | [diff] [blame] | 14 | #if defined(MBEDTLS_ERROR_C) | 
| Paul Bakker | 0464dd9 | 2014-07-09 10:16:18 +0200 | [diff] [blame] | 15 |  | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 16 | #include "mbedtls/platform.h" | 
| Rich Evans | 6aa04bc | 2015-01-30 11:18:42 +0000 | [diff] [blame] | 17 |  | 
| Paul Bakker | daae3b7 | 2015-02-08 15:49:54 +0100 | [diff] [blame] | 18 | #include <stdio.h> | 
| Gilles Peskine | 67aed9a | 2020-11-09 15:14:10 +0100 | [diff] [blame] | 19 | #include <string.h> | 
| Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 20 |  | 
| Manuel Pégourié-Gonnard | f5dc8ec | 2015-02-13 14:32:17 +0000 | [diff] [blame] | 21 | HEADER_INCLUDED | 
| Paul Bakker | d0a345e | 2011-11-10 13:03:42 +0000 | [diff] [blame] | 22 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 23 | const char *mbedtls_high_level_strerr(int error_code) | 
| Gaurav Aggarwal | a9f6400 | 2020-04-09 01:44:52 -0700 | [diff] [blame] | 24 | { | 
| Gaurav Aggarwal | 6ea4fc7 | 2020-04-20 16:03:46 -0700 | [diff] [blame] | 25 | int high_level_error_code; | 
| Gaurav Aggarwal | a9f6400 | 2020-04-09 01:44:52 -0700 | [diff] [blame] | 26 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 27 | if (error_code < 0) { | 
| Gaurav Aggarwal | 6ea4fc7 | 2020-04-20 16:03:46 -0700 | [diff] [blame] | 28 | error_code = -error_code; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 29 | } | 
| Gaurav Aggarwal | 6ea4fc7 | 2020-04-20 16:03:46 -0700 | [diff] [blame] | 30 |  | 
|  | 31 | /* Extract the high-level part from the error code. */ | 
|  | 32 | high_level_error_code = error_code & 0xFF80; | 
|  | 33 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 34 | switch (high_level_error_code) { | 
|  | 35 | /* Begin Auto-Generated Code. */ | 
|  | 36 | HIGH_LEVEL_CODE_CHECKS | 
| Gaurav Aggarwal | 3d02db2 | 2020-04-11 17:14:03 -0700 | [diff] [blame] | 37 | /* End Auto-Generated Code. */ | 
| Gaurav Aggarwal | a4a2aa5 | 2020-04-09 11:39:04 -0700 | [diff] [blame] | 38 |  | 
|  | 39 | default: | 
| Gaurav Aggarwal | a9f6400 | 2020-04-09 01:44:52 -0700 | [diff] [blame] | 40 | break; | 
| Gaurav Aggarwal | a9f6400 | 2020-04-09 01:44:52 -0700 | [diff] [blame] | 41 | } | 
|  | 42 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 43 | return NULL; | 
| Gaurav Aggarwal | a9f6400 | 2020-04-09 01:44:52 -0700 | [diff] [blame] | 44 | } | 
|  | 45 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 46 | const char *mbedtls_low_level_strerr(int error_code) | 
| Gaurav Aggarwal | a9f6400 | 2020-04-09 01:44:52 -0700 | [diff] [blame] | 47 | { | 
| Gaurav Aggarwal | 6ea4fc7 | 2020-04-20 16:03:46 -0700 | [diff] [blame] | 48 | int low_level_error_code; | 
| Gaurav Aggarwal | a9f6400 | 2020-04-09 01:44:52 -0700 | [diff] [blame] | 49 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 50 | if (error_code < 0) { | 
| Gaurav Aggarwal | 6ea4fc7 | 2020-04-20 16:03:46 -0700 | [diff] [blame] | 51 | error_code = -error_code; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 52 | } | 
| Gaurav Aggarwal | 6ea4fc7 | 2020-04-20 16:03:46 -0700 | [diff] [blame] | 53 |  | 
|  | 54 | /* Extract the low-level part from the error code. */ | 
|  | 55 | low_level_error_code = error_code & ~0xFF80; | 
|  | 56 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 57 | switch (low_level_error_code) { | 
|  | 58 | /* Begin Auto-Generated Code. */ | 
|  | 59 | LOW_LEVEL_CODE_CHECKS | 
| Gaurav Aggarwal | 3d02db2 | 2020-04-11 17:14:03 -0700 | [diff] [blame] | 60 | /* End Auto-Generated Code. */ | 
| Gaurav Aggarwal | a4a2aa5 | 2020-04-09 11:39:04 -0700 | [diff] [blame] | 61 |  | 
|  | 62 | default: | 
| Gaurav Aggarwal | a9f6400 | 2020-04-09 01:44:52 -0700 | [diff] [blame] | 63 | break; | 
| Gaurav Aggarwal | a9f6400 | 2020-04-09 01:44:52 -0700 | [diff] [blame] | 64 | } | 
|  | 65 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 66 | return NULL; | 
| Gaurav Aggarwal | a9f6400 | 2020-04-09 01:44:52 -0700 | [diff] [blame] | 67 | } | 
|  | 68 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 69 | void mbedtls_strerror(int ret, char *buf, size_t buflen) | 
| Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 70 | { | 
|  | 71 | size_t len; | 
|  | 72 | int use_ret; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 73 | const char *high_level_error_description = NULL; | 
|  | 74 | const char *low_level_error_description = NULL; | 
| Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 75 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 76 | if (buflen == 0) { | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 77 | return; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 78 | } | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 79 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 80 | memset(buf, 0x00, buflen); | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 81 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 82 | if (ret < 0) { | 
| Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 83 | ret = -ret; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 84 | } | 
| Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 85 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 86 | if (ret & 0xFF80) { | 
| Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 87 | use_ret = ret & 0xFF80; | 
|  | 88 |  | 
| Gaurav Aggarwal | a9f6400 | 2020-04-09 01:44:52 -0700 | [diff] [blame] | 89 | // Translate high level error code. | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 90 | high_level_error_description = mbedtls_high_level_strerr(ret); | 
| Manuel Pégourié-Gonnard | fe671f4 | 2014-04-11 18:06:44 +0200 | [diff] [blame] | 91 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 92 | if (high_level_error_description == NULL) { | 
|  | 93 | mbedtls_snprintf(buf, buflen, "UNKNOWN ERROR CODE (%04X)", (unsigned int) use_ret); | 
|  | 94 | } else { | 
|  | 95 | mbedtls_snprintf(buf, buflen, "%s", high_level_error_description); | 
|  | 96 | } | 
| Gaurav Aggarwal | a9f6400 | 2020-04-09 01:44:52 -0700 | [diff] [blame] | 97 |  | 
| Gaurav Aggarwal | a4a2aa5 | 2020-04-09 11:39:04 -0700 | [diff] [blame] | 98 | #if defined(MBEDTLS_SSL_TLS_C) | 
| Gaurav Aggarwal | a9f6400 | 2020-04-09 01:44:52 -0700 | [diff] [blame] | 99 | // Early return in case of a fatal error - do not try to translate low | 
|  | 100 | // level code. | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 101 | if (use_ret == -(MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE)) { | 
| Gaurav Aggarwal | a9f6400 | 2020-04-09 01:44:52 -0700 | [diff] [blame] | 102 | return; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 103 | } | 
| Gaurav Aggarwal | a4a2aa5 | 2020-04-09 11:39:04 -0700 | [diff] [blame] | 104 | #endif /* MBEDTLS_SSL_TLS_C */ | 
| Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 105 | } | 
|  | 106 |  | 
|  | 107 | use_ret = ret & ~0xFF80; | 
|  | 108 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 109 | if (use_ret == 0) { | 
| Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 110 | return; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 111 | } | 
| Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 112 |  | 
|  | 113 | // If high level code is present, make a concatenation between both | 
|  | 114 | // error strings. | 
|  | 115 | // | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 116 | len = strlen(buf); | 
| Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 117 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 118 | if (len > 0) { | 
|  | 119 | if (buflen - len < 5) { | 
| Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 120 | return; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 121 | } | 
| Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 122 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 123 | mbedtls_snprintf(buf + len, buflen - len, " : "); | 
| Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 124 |  | 
|  | 125 | buf += len + 3; | 
|  | 126 | buflen -= len + 3; | 
|  | 127 | } | 
|  | 128 |  | 
| Gaurav Aggarwal | a9f6400 | 2020-04-09 01:44:52 -0700 | [diff] [blame] | 129 | // Translate low level error code. | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 130 | low_level_error_description = mbedtls_low_level_strerr(ret); | 
| Manuel Pégourié-Gonnard | fe671f4 | 2014-04-11 18:06:44 +0200 | [diff] [blame] | 131 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 132 | if (low_level_error_description == NULL) { | 
|  | 133 | mbedtls_snprintf(buf, buflen, "UNKNOWN ERROR CODE (%04X)", (unsigned int) use_ret); | 
|  | 134 | } else { | 
|  | 135 | mbedtls_snprintf(buf, buflen, "%s", low_level_error_description); | 
|  | 136 | } | 
| Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 137 | } | 
|  | 138 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 139 | #else /* MBEDTLS_ERROR_C */ | 
| Paul Bakker | a023437 | 2013-03-20 14:42:21 +0100 | [diff] [blame] | 140 |  | 
| Paul Bakker | a023437 | 2013-03-20 14:42:21 +0100 | [diff] [blame] | 141 | /* | 
| Tom Cosgrove | ce7f18c | 2022-07-28 05:50:56 +0100 | [diff] [blame] | 142 | * Provide a dummy implementation when MBEDTLS_ERROR_C is not defined | 
| Paul Bakker | a023437 | 2013-03-20 14:42:21 +0100 | [diff] [blame] | 143 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 144 | void mbedtls_strerror(int ret, char *buf, size_t buflen) | 
| Paul Bakker | a023437 | 2013-03-20 14:42:21 +0100 | [diff] [blame] | 145 | { | 
|  | 146 | ((void) ret); | 
|  | 147 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 148 | if (buflen > 0) { | 
| Paul Bakker | a023437 | 2013-03-20 14:42:21 +0100 | [diff] [blame] | 149 | buf[0] = '\0'; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 150 | } | 
| Paul Bakker | a023437 | 2013-03-20 14:42:21 +0100 | [diff] [blame] | 151 | } | 
|  | 152 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 153 | #endif /* MBEDTLS_ERROR_C */ | 
| Gilles Peskine | 67aed9a | 2020-11-09 15:14:10 +0100 | [diff] [blame] | 154 |  | 
|  | 155 | #endif /* MBEDTLS_ERROR_C || MBEDTLS_ERROR_STRERROR_DUMMY */ |