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