Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Error message information |
| 3 | * |
Bence Szépkúti | a2947ac | 2020-08-19 16:37:36 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 6 | * |
| 7 | * This file is provided under the Apache License 2.0, or the |
| 8 | * GNU General Public License v2.0 or later. |
| 9 | * |
| 10 | * ********** |
| 11 | * Apache License 2.0: |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 12 | * |
| 13 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 14 | * not use this file except in compliance with the License. |
| 15 | * You may obtain a copy of the License at |
| 16 | * |
| 17 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 18 | * |
| 19 | * Unless required by applicable law or agreed to in writing, software |
| 20 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 21 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 22 | * See the License for the specific language governing permissions and |
| 23 | * limitations under the License. |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 24 | * |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 25 | * ********** |
| 26 | * |
| 27 | * ********** |
| 28 | * GNU General Public License v2.0 or later: |
| 29 | * |
| 30 | * This program is free software; you can redistribute it and/or modify |
| 31 | * it under the terms of the GNU General Public License as published by |
| 32 | * the Free Software Foundation; either version 2 of the License, or |
| 33 | * (at your option) any later version. |
| 34 | * |
| 35 | * This program is distributed in the hope that it will be useful, |
| 36 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 37 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 38 | * GNU General Public License for more details. |
| 39 | * |
| 40 | * You should have received a copy of the GNU General Public License along |
| 41 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 42 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 43 | * |
| 44 | * ********** |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 45 | */ |
| 46 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 47 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 48 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 49 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 50 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 51 | #endif |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 52 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 53 | #if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY) |
Gilles Peskine | 076c97e | 2020-11-09 15:14:10 +0100 | [diff] [blame] | 54 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 55 | #include "mbedtls/error.h" |
Gilles Peskine | 076c97e | 2020-11-09 15:14:10 +0100 | [diff] [blame] | 56 | |
| 57 | #if defined(MBEDTLS_ERROR_C) |
Paul Bakker | 0464dd9 | 2014-07-09 10:16:18 +0200 | [diff] [blame] | 58 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 59 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 60 | #include "mbedtls/platform.h" |
Rich Evans | 6aa04bc | 2015-01-30 11:18:42 +0000 | [diff] [blame] | 61 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 62 | #define mbedtls_snprintf snprintf |
Rich Evans | 6aa04bc | 2015-01-30 11:18:42 +0000 | [diff] [blame] | 63 | #endif |
| 64 | |
Paul Bakker | daae3b7 | 2015-02-08 15:49:54 +0100 | [diff] [blame] | 65 | #include <stdio.h> |
Gilles Peskine | 076c97e | 2020-11-09 15:14:10 +0100 | [diff] [blame] | 66 | #include <string.h> |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 67 | |
Manuel Pégourié-Gonnard | f5dc8ec | 2015-02-13 14:32:17 +0000 | [diff] [blame] | 68 | HEADER_INCLUDED |
Paul Bakker | d0a345e | 2011-11-10 13:03:42 +0000 | [diff] [blame] | 69 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 70 | void mbedtls_strerror( int ret, char *buf, size_t buflen ) |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 71 | { |
| 72 | size_t len; |
| 73 | int use_ret; |
| 74 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 75 | if( buflen == 0 ) |
| 76 | return; |
| 77 | |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 78 | memset( buf, 0x00, buflen ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 79 | |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 80 | if( ret < 0 ) |
| 81 | ret = -ret; |
| 82 | |
| 83 | if( ret & 0xFF80 ) |
| 84 | { |
| 85 | use_ret = ret & 0xFF80; |
| 86 | |
| 87 | // High level error codes |
| 88 | // |
Manuel Pégourié-Gonnard | fe671f4 | 2014-04-11 18:06:44 +0200 | [diff] [blame] | 89 | // BEGIN generated code |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 90 | HIGH_LEVEL_CODE_CHECKS |
Manuel Pégourié-Gonnard | fe671f4 | 2014-04-11 18:06:44 +0200 | [diff] [blame] | 91 | // END generated code |
| 92 | |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 93 | if( strlen( buf ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 94 | mbedtls_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret ); |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | use_ret = ret & ~0xFF80; |
| 98 | |
| 99 | if( use_ret == 0 ) |
| 100 | return; |
| 101 | |
| 102 | // If high level code is present, make a concatenation between both |
| 103 | // error strings. |
| 104 | // |
| 105 | len = strlen( buf ); |
| 106 | |
| 107 | if( len > 0 ) |
| 108 | { |
| 109 | if( buflen - len < 5 ) |
| 110 | return; |
| 111 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 112 | mbedtls_snprintf( buf + len, buflen - len, " : " ); |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 113 | |
| 114 | buf += len + 3; |
| 115 | buflen -= len + 3; |
| 116 | } |
| 117 | |
| 118 | // Low level error codes |
| 119 | // |
Manuel Pégourié-Gonnard | fe671f4 | 2014-04-11 18:06:44 +0200 | [diff] [blame] | 120 | // BEGIN generated code |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 121 | LOW_LEVEL_CODE_CHECKS |
Manuel Pégourié-Gonnard | fe671f4 | 2014-04-11 18:06:44 +0200 | [diff] [blame] | 122 | // END generated code |
| 123 | |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 124 | if( strlen( buf ) != 0 ) |
| 125 | return; |
| 126 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 127 | mbedtls_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret ); |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 130 | #else /* MBEDTLS_ERROR_C */ |
Paul Bakker | a023437 | 2013-03-20 14:42:21 +0100 | [diff] [blame] | 131 | |
Paul Bakker | a023437 | 2013-03-20 14:42:21 +0100 | [diff] [blame] | 132 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 133 | * Provide an non-function in case MBEDTLS_ERROR_C is not defined |
Paul Bakker | a023437 | 2013-03-20 14:42:21 +0100 | [diff] [blame] | 134 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 135 | void mbedtls_strerror( int ret, char *buf, size_t buflen ) |
Paul Bakker | a023437 | 2013-03-20 14:42:21 +0100 | [diff] [blame] | 136 | { |
| 137 | ((void) ret); |
| 138 | |
| 139 | if( buflen > 0 ) |
| 140 | buf[0] = '\0'; |
| 141 | } |
| 142 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 143 | #endif /* MBEDTLS_ERROR_C */ |
Gilles Peskine | 076c97e | 2020-11-09 15:14:10 +0100 | [diff] [blame] | 144 | |
| 145 | #endif /* MBEDTLS_ERROR_C || MBEDTLS_ERROR_STRERROR_DUMMY */ |