blob: 07750030286888a7ce5c52dd6213978be8c21316 [file] [log] [blame]
Paul Bakker9d781402011-05-09 16:17:09 +00001/*
2 * Error message information
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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 Bakker9d781402011-05-09 16:17:09 +000018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker9d781402011-05-09 16:17:09 +000021
Gilles Peskine67aed9a2020-11-09 15:14:10 +010022#include "mbedtls/error.h"
23
24#if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY)
25
Gilles Peskineb64e0fe2020-11-09 15:14:10 +010026#if defined(MBEDTLS_ERROR_C)
Paul Bakker0464dd92014-07-09 10:16:18 +020027
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/platform.h"
Rich Evans6aa04bc2015-01-30 11:18:42 +000029
Paul Bakkerdaae3b72015-02-08 15:49:54 +010030#include <stdio.h>
Gilles Peskine67aed9a2020-11-09 15:14:10 +010031#include <string.h>
Paul Bakker9d781402011-05-09 16:17:09 +000032
Manuel Pégourié-Gonnardf5dc8ec2015-02-13 14:32:17 +000033HEADER_INCLUDED
Paul Bakkerd0a345e2011-11-10 13:03:42 +000034
David Horstmann8b6068b2023-01-05 15:42:32 +000035const char *mbedtls_high_level_strerr(int error_code)
Gaurav Aggarwala9f64002020-04-09 01:44:52 -070036{
Gaurav Aggarwal6ea4fc72020-04-20 16:03:46 -070037 int high_level_error_code;
Gaurav Aggarwala9f64002020-04-09 01:44:52 -070038
David Horstmann8b6068b2023-01-05 15:42:32 +000039 if (error_code < 0) {
Gaurav Aggarwal6ea4fc72020-04-20 16:03:46 -070040 error_code = -error_code;
David Horstmann8b6068b2023-01-05 15:42:32 +000041 }
Gaurav Aggarwal6ea4fc72020-04-20 16:03:46 -070042
43 /* Extract the high-level part from the error code. */
44 high_level_error_code = error_code & 0xFF80;
45
David Horstmann8b6068b2023-01-05 15:42:32 +000046 switch (high_level_error_code) {
47 /* Begin Auto-Generated Code. */
48 HIGH_LEVEL_CODE_CHECKS
Gaurav Aggarwal3d02db22020-04-11 17:14:03 -070049 /* End Auto-Generated Code. */
Gaurav Aggarwala4a2aa52020-04-09 11:39:04 -070050
51 default:
Gaurav Aggarwala9f64002020-04-09 01:44:52 -070052 break;
Gaurav Aggarwala9f64002020-04-09 01:44:52 -070053 }
54
David Horstmann8b6068b2023-01-05 15:42:32 +000055 return NULL;
Gaurav Aggarwala9f64002020-04-09 01:44:52 -070056}
57
David Horstmann8b6068b2023-01-05 15:42:32 +000058const char *mbedtls_low_level_strerr(int error_code)
Gaurav Aggarwala9f64002020-04-09 01:44:52 -070059{
Gaurav Aggarwal6ea4fc72020-04-20 16:03:46 -070060 int low_level_error_code;
Gaurav Aggarwala9f64002020-04-09 01:44:52 -070061
David Horstmann8b6068b2023-01-05 15:42:32 +000062 if (error_code < 0) {
Gaurav Aggarwal6ea4fc72020-04-20 16:03:46 -070063 error_code = -error_code;
David Horstmann8b6068b2023-01-05 15:42:32 +000064 }
Gaurav Aggarwal6ea4fc72020-04-20 16:03:46 -070065
66 /* Extract the low-level part from the error code. */
67 low_level_error_code = error_code & ~0xFF80;
68
David Horstmann8b6068b2023-01-05 15:42:32 +000069 switch (low_level_error_code) {
70 /* Begin Auto-Generated Code. */
71 LOW_LEVEL_CODE_CHECKS
Gaurav Aggarwal3d02db22020-04-11 17:14:03 -070072 /* End Auto-Generated Code. */
Gaurav Aggarwala4a2aa52020-04-09 11:39:04 -070073
74 default:
Gaurav Aggarwala9f64002020-04-09 01:44:52 -070075 break;
Gaurav Aggarwala9f64002020-04-09 01:44:52 -070076 }
77
David Horstmann8b6068b2023-01-05 15:42:32 +000078 return NULL;
Gaurav Aggarwala9f64002020-04-09 01:44:52 -070079}
80
David Horstmann8b6068b2023-01-05 15:42:32 +000081void mbedtls_strerror(int ret, char *buf, size_t buflen)
Paul Bakker9d781402011-05-09 16:17:09 +000082{
83 size_t len;
84 int use_ret;
David Horstmann8b6068b2023-01-05 15:42:32 +000085 const char *high_level_error_description = NULL;
86 const char *low_level_error_description = NULL;
Paul Bakker9d781402011-05-09 16:17:09 +000087
David Horstmann8b6068b2023-01-05 15:42:32 +000088 if (buflen == 0) {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020089 return;
David Horstmann8b6068b2023-01-05 15:42:32 +000090 }
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020091
David Horstmann8b6068b2023-01-05 15:42:32 +000092 memset(buf, 0x00, buflen);
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020093
David Horstmann8b6068b2023-01-05 15:42:32 +000094 if (ret < 0) {
Paul Bakker9d781402011-05-09 16:17:09 +000095 ret = -ret;
David Horstmann8b6068b2023-01-05 15:42:32 +000096 }
Paul Bakker9d781402011-05-09 16:17:09 +000097
David Horstmann8b6068b2023-01-05 15:42:32 +000098 if (ret & 0xFF80) {
Paul Bakker9d781402011-05-09 16:17:09 +000099 use_ret = ret & 0xFF80;
100
Gaurav Aggarwala9f64002020-04-09 01:44:52 -0700101 // Translate high level error code.
David Horstmann8b6068b2023-01-05 15:42:32 +0000102 high_level_error_description = mbedtls_high_level_strerr(ret);
Manuel Pégourié-Gonnardfe671f42014-04-11 18:06:44 +0200103
David Horstmann8b6068b2023-01-05 15:42:32 +0000104 if (high_level_error_description == NULL) {
105 mbedtls_snprintf(buf, buflen, "UNKNOWN ERROR CODE (%04X)", (unsigned int) use_ret);
106 } else {
107 mbedtls_snprintf(buf, buflen, "%s", high_level_error_description);
108 }
Gaurav Aggarwala9f64002020-04-09 01:44:52 -0700109
Gaurav Aggarwala4a2aa52020-04-09 11:39:04 -0700110#if defined(MBEDTLS_SSL_TLS_C)
Gaurav Aggarwala9f64002020-04-09 01:44:52 -0700111 // Early return in case of a fatal error - do not try to translate low
112 // level code.
David Horstmann8b6068b2023-01-05 15:42:32 +0000113 if (use_ret == -(MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE)) {
Gaurav Aggarwala9f64002020-04-09 01:44:52 -0700114 return;
David Horstmann8b6068b2023-01-05 15:42:32 +0000115 }
Gaurav Aggarwala4a2aa52020-04-09 11:39:04 -0700116#endif /* MBEDTLS_SSL_TLS_C */
Paul Bakker9d781402011-05-09 16:17:09 +0000117 }
118
119 use_ret = ret & ~0xFF80;
120
David Horstmann8b6068b2023-01-05 15:42:32 +0000121 if (use_ret == 0) {
Paul Bakker9d781402011-05-09 16:17:09 +0000122 return;
David Horstmann8b6068b2023-01-05 15:42:32 +0000123 }
Paul Bakker9d781402011-05-09 16:17:09 +0000124
125 // If high level code is present, make a concatenation between both
126 // error strings.
127 //
David Horstmann8b6068b2023-01-05 15:42:32 +0000128 len = strlen(buf);
Paul Bakker9d781402011-05-09 16:17:09 +0000129
David Horstmann8b6068b2023-01-05 15:42:32 +0000130 if (len > 0) {
131 if (buflen - len < 5) {
Paul Bakker9d781402011-05-09 16:17:09 +0000132 return;
David Horstmann8b6068b2023-01-05 15:42:32 +0000133 }
Paul Bakker9d781402011-05-09 16:17:09 +0000134
David Horstmann8b6068b2023-01-05 15:42:32 +0000135 mbedtls_snprintf(buf + len, buflen - len, " : ");
Paul Bakker9d781402011-05-09 16:17:09 +0000136
137 buf += len + 3;
138 buflen -= len + 3;
139 }
140
Gaurav Aggarwala9f64002020-04-09 01:44:52 -0700141 // Translate low level error code.
David Horstmann8b6068b2023-01-05 15:42:32 +0000142 low_level_error_description = mbedtls_low_level_strerr(ret);
Manuel Pégourié-Gonnardfe671f42014-04-11 18:06:44 +0200143
David Horstmann8b6068b2023-01-05 15:42:32 +0000144 if (low_level_error_description == NULL) {
145 mbedtls_snprintf(buf, buflen, "UNKNOWN ERROR CODE (%04X)", (unsigned int) use_ret);
146 } else {
147 mbedtls_snprintf(buf, buflen, "%s", low_level_error_description);
148 }
Paul Bakker9d781402011-05-09 16:17:09 +0000149}
150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151#else /* MBEDTLS_ERROR_C */
Paul Bakkera0234372013-03-20 14:42:21 +0100152
Paul Bakkera0234372013-03-20 14:42:21 +0100153/*
Tom Cosgrovece7f18c2022-07-28 05:50:56 +0100154 * Provide a dummy implementation when MBEDTLS_ERROR_C is not defined
Paul Bakkera0234372013-03-20 14:42:21 +0100155 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000156void mbedtls_strerror(int ret, char *buf, size_t buflen)
Paul Bakkera0234372013-03-20 14:42:21 +0100157{
158 ((void) ret);
159
David Horstmann8b6068b2023-01-05 15:42:32 +0000160 if (buflen > 0) {
Paul Bakkera0234372013-03-20 14:42:21 +0100161 buf[0] = '\0';
David Horstmann8b6068b2023-01-05 15:42:32 +0000162 }
Paul Bakkera0234372013-03-20 14:42:21 +0100163}
164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165#endif /* MBEDTLS_ERROR_C */
Gilles Peskine67aed9a2020-11-09 15:14:10 +0100166
Chris Jonesef018522021-04-12 17:27:18 +0100167#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann8b6068b2023-01-05 15:42:32 +0000168void (*mbedtls_test_hook_error_add)(int, int, const char *, int);
Chris Jonesef018522021-04-12 17:27:18 +0100169#endif
170
Gilles Peskine67aed9a2020-11-09 15:14:10 +0100171#endif /* MBEDTLS_ERROR_C || MBEDTLS_ERROR_STRERROR_DUMMY */