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