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