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 |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 7 | * |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 23 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 24 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 25 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 26 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 27 | #endif |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 28 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 29 | #if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 30 | #include "mbedtls/error.h" |
Manuel Pégourié-Gonnard | f5dc8ec | 2015-02-13 14:32:17 +0000 | [diff] [blame] | 31 | #include <string.h> |
Paul Bakker | 0464dd9 | 2014-07-09 10:16:18 +0200 | [diff] [blame] | 32 | #endif |
| 33 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 34 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 35 | #include "mbedtls/platform.h" |
Rich Evans | 6aa04bc | 2015-01-30 11:18:42 +0000 | [diff] [blame] | 36 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 37 | #define mbedtls_snprintf snprintf |
Rich Evans | 6aa04bc | 2015-01-30 11:18:42 +0000 | [diff] [blame] | 38 | #endif |
| 39 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 40 | #if defined(MBEDTLS_ERROR_C) |
Paul Bakker | b2a1140 | 2013-06-24 13:02:12 +0200 | [diff] [blame] | 41 | |
Paul Bakker | daae3b7 | 2015-02-08 15:49:54 +0100 | [diff] [blame] | 42 | #include <stdio.h> |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 43 | |
Manuel Pégourié-Gonnard | f5dc8ec | 2015-02-13 14:32:17 +0000 | [diff] [blame] | 44 | HEADER_INCLUDED |
Paul Bakker | d0a345e | 2011-11-10 13:03:42 +0000 | [diff] [blame] | 45 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 46 | void mbedtls_strerror( int ret, char *buf, size_t buflen ) |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 47 | { |
| 48 | size_t len; |
| 49 | int use_ret; |
| 50 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 51 | if( buflen == 0 ) |
| 52 | return; |
| 53 | |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 54 | memset( buf, 0x00, buflen ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 55 | |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 56 | if( ret < 0 ) |
| 57 | ret = -ret; |
| 58 | |
| 59 | if( ret & 0xFF80 ) |
| 60 | { |
| 61 | use_ret = ret & 0xFF80; |
| 62 | |
| 63 | // High level error codes |
| 64 | // |
Manuel Pégourié-Gonnard | fe671f4 | 2014-04-11 18:06:44 +0200 | [diff] [blame] | 65 | // BEGIN generated code |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 66 | HIGH_LEVEL_CODE_CHECKS |
Manuel Pégourié-Gonnard | fe671f4 | 2014-04-11 18:06:44 +0200 | [diff] [blame] | 67 | // END generated code |
| 68 | |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 69 | if( strlen( buf ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 70 | mbedtls_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret ); |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | use_ret = ret & ~0xFF80; |
| 74 | |
| 75 | if( use_ret == 0 ) |
| 76 | return; |
| 77 | |
| 78 | // If high level code is present, make a concatenation between both |
| 79 | // error strings. |
| 80 | // |
| 81 | len = strlen( buf ); |
| 82 | |
| 83 | if( len > 0 ) |
| 84 | { |
| 85 | if( buflen - len < 5 ) |
| 86 | return; |
| 87 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 88 | mbedtls_snprintf( buf + len, buflen - len, " : " ); |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 89 | |
| 90 | buf += len + 3; |
| 91 | buflen -= len + 3; |
| 92 | } |
| 93 | |
| 94 | // Low level error codes |
| 95 | // |
Manuel Pégourié-Gonnard | fe671f4 | 2014-04-11 18:06:44 +0200 | [diff] [blame] | 96 | // BEGIN generated code |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 97 | LOW_LEVEL_CODE_CHECKS |
Manuel Pégourié-Gonnard | fe671f4 | 2014-04-11 18:06:44 +0200 | [diff] [blame] | 98 | // END generated code |
| 99 | |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 100 | if( strlen( buf ) != 0 ) |
| 101 | return; |
| 102 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 103 | mbedtls_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret ); |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 106 | #else /* MBEDTLS_ERROR_C */ |
Paul Bakker | a023437 | 2013-03-20 14:42:21 +0100 | [diff] [blame] | 107 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 108 | #if defined(MBEDTLS_ERROR_STRERROR_DUMMY) |
Paul Bakker | a023437 | 2013-03-20 14:42:21 +0100 | [diff] [blame] | 109 | |
Paul Bakker | a023437 | 2013-03-20 14:42:21 +0100 | [diff] [blame] | 110 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 111 | * Provide an non-function in case MBEDTLS_ERROR_C is not defined |
Paul Bakker | a023437 | 2013-03-20 14:42:21 +0100 | [diff] [blame] | 112 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 113 | void mbedtls_strerror( int ret, char *buf, size_t buflen ) |
Paul Bakker | a023437 | 2013-03-20 14:42:21 +0100 | [diff] [blame] | 114 | { |
| 115 | ((void) ret); |
| 116 | |
| 117 | if( buflen > 0 ) |
| 118 | buf[0] = '\0'; |
| 119 | } |
| 120 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 121 | #endif /* MBEDTLS_ERROR_STRERROR_DUMMY */ |
Paul Bakker | e2ab84f | 2013-06-29 18:24:32 +0200 | [diff] [blame] | 122 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 123 | #endif /* MBEDTLS_ERROR_C */ |