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 | typedef struct mbedtls_error |
| 46 | { |
| 47 | int code; /* Error code. */ |
| 48 | const char * description; /* Error description. */ |
| 49 | } mbedtls_error_t; |
| 50 | |
| 51 | static mbedtls_error_t high_level_errors[] = |
| 52 | { |
| 53 | HIGH_LEVEL_CODE_CHECKS |
| 54 | }; |
| 55 | |
| 56 | #define NUM_HIGH_LEVEL_ERRORS ( sizeof(high_level_errors)/sizeof(mbedtls_error_t) ) |
| 57 | |
| 58 | static mbedtls_error_t low_level_errors[] = |
| 59 | { |
| 60 | LOW_LEVEL_CODE_CHECKS |
| 61 | }; |
| 62 | |
| 63 | #define NUM_LOW_LEVEL_ERRORS ( sizeof(low_level_errors)/sizeof(mbedtls_error_t) ) |
| 64 | |
| 65 | const char * mbedtls_high_level_strerr( int error_code ) |
| 66 | { |
| 67 | size_t i; |
| 68 | const char *error_description = NULL; |
| 69 | |
| 70 | for(i = 0; i < NUM_HIGH_LEVEL_ERRORS; i++ ) |
| 71 | { |
| 72 | if( high_level_errors[i].code == error_code ) |
| 73 | { |
| 74 | error_description = high_level_errors[i].description; |
| 75 | break; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | return error_description; |
| 80 | } |
| 81 | |
| 82 | const char * mbedtls_low_level_strerr( int error_code ) |
| 83 | { |
| 84 | size_t i; |
| 85 | const char *error_description = NULL; |
| 86 | |
| 87 | for(i = 0; i < NUM_LOW_LEVEL_ERRORS; i++ ) |
| 88 | { |
| 89 | if( low_level_errors[i].code == error_code ) |
| 90 | { |
| 91 | error_description = low_level_errors[i].description; |
| 92 | break; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | return error_description; |
| 97 | } |
| 98 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 99 | void mbedtls_strerror( int ret, char *buf, size_t buflen ) |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 100 | { |
| 101 | size_t len; |
| 102 | int use_ret; |
Gaurav Aggarwal | a9f6400 | 2020-04-09 01:44:52 -0700 | [diff] [blame^] | 103 | const char * high_level_error_description = NULL; |
| 104 | const char * low_level_error_description = NULL; |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 105 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 106 | if( buflen == 0 ) |
| 107 | return; |
| 108 | |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 109 | memset( buf, 0x00, buflen ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 110 | |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 111 | if( ret < 0 ) |
| 112 | ret = -ret; |
| 113 | |
| 114 | if( ret & 0xFF80 ) |
| 115 | { |
| 116 | use_ret = ret & 0xFF80; |
| 117 | |
Gaurav Aggarwal | a9f6400 | 2020-04-09 01:44:52 -0700 | [diff] [blame^] | 118 | // Translate high level error code. |
| 119 | high_level_error_description = mbedtls_high_level_strerr(use_ret); |
Manuel Pégourié-Gonnard | fe671f4 | 2014-04-11 18:06:44 +0200 | [diff] [blame] | 120 | |
Gaurav Aggarwal | a9f6400 | 2020-04-09 01:44:52 -0700 | [diff] [blame^] | 121 | if( high_level_error_description == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 122 | mbedtls_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret ); |
Gaurav Aggarwal | a9f6400 | 2020-04-09 01:44:52 -0700 | [diff] [blame^] | 123 | else |
| 124 | mbedtls_snprintf( buf, buflen, "%s", high_level_error_description ); |
| 125 | |
| 126 | // Early return in case of a fatal error - do not try to translate low |
| 127 | // level code. |
| 128 | if(use_ret == -(MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE)) |
| 129 | return; |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | use_ret = ret & ~0xFF80; |
| 133 | |
| 134 | if( use_ret == 0 ) |
| 135 | return; |
| 136 | |
| 137 | // If high level code is present, make a concatenation between both |
| 138 | // error strings. |
| 139 | // |
| 140 | len = strlen( buf ); |
| 141 | |
| 142 | if( len > 0 ) |
| 143 | { |
| 144 | if( buflen - len < 5 ) |
| 145 | return; |
| 146 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 147 | mbedtls_snprintf( buf + len, buflen - len, " : " ); |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 148 | |
| 149 | buf += len + 3; |
| 150 | buflen -= len + 3; |
| 151 | } |
| 152 | |
Gaurav Aggarwal | a9f6400 | 2020-04-09 01:44:52 -0700 | [diff] [blame^] | 153 | // Translate low level error code. |
| 154 | low_level_error_description = mbedtls_low_level_strerr( use_ret ); |
Manuel Pégourié-Gonnard | fe671f4 | 2014-04-11 18:06:44 +0200 | [diff] [blame] | 155 | |
Gaurav Aggarwal | a9f6400 | 2020-04-09 01:44:52 -0700 | [diff] [blame^] | 156 | if( low_level_error_description == NULL ) |
| 157 | mbedtls_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret ); |
| 158 | else |
| 159 | mbedtls_snprintf( buf, buflen, "%s", low_level_error_description ); |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 162 | #else /* MBEDTLS_ERROR_C */ |
Paul Bakker | a023437 | 2013-03-20 14:42:21 +0100 | [diff] [blame] | 163 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 164 | #if defined(MBEDTLS_ERROR_STRERROR_DUMMY) |
Paul Bakker | a023437 | 2013-03-20 14:42:21 +0100 | [diff] [blame] | 165 | |
Paul Bakker | a023437 | 2013-03-20 14:42:21 +0100 | [diff] [blame] | 166 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 167 | * Provide an non-function in case MBEDTLS_ERROR_C is not defined |
Paul Bakker | a023437 | 2013-03-20 14:42:21 +0100 | [diff] [blame] | 168 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 169 | void mbedtls_strerror( int ret, char *buf, size_t buflen ) |
Paul Bakker | a023437 | 2013-03-20 14:42:21 +0100 | [diff] [blame] | 170 | { |
| 171 | ((void) ret); |
| 172 | |
| 173 | if( buflen > 0 ) |
| 174 | buf[0] = '\0'; |
| 175 | } |
| 176 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 177 | #endif /* MBEDTLS_ERROR_STRERROR_DUMMY */ |
Paul Bakker | e2ab84f | 2013-06-29 18:24:32 +0200 | [diff] [blame] | 178 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 179 | #endif /* MBEDTLS_ERROR_C */ |