blob: 7f9c12f58044ee0a88646da5008665929eba3b93 [file] [log] [blame]
Paul Bakker9d781402011-05-09 16:17:09 +00001/*
2 * Error message information
3 *
Manuel Pégourié-Gonnard7381ff02015-08-04 11:12:49 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * 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é-Gonnard37ff1402015-09-04 14:21:07 +020012 *
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 Bakker9d781402011-05-09 16:17:09 +000024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
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 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker9d781402011-05-09 16:17:09 +000047 */
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#endif
Paul Bakker9d781402011-05-09 16:17:09 +000054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/error.h"
Manuel Pégourié-Gonnardf5dc8ec2015-02-13 14:32:17 +000057#include <string.h>
Paul Bakker0464dd92014-07-09 10:16:18 +020058#endif
59
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061#include "mbedtls/platform.h"
Rich Evans6aa04bc2015-01-30 11:18:42 +000062#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#define mbedtls_snprintf snprintf
Simon Butcherdb0feca2016-05-17 00:03:14 +010064#define mbedtls_time_t time_t
Rich Evans6aa04bc2015-01-30 11:18:42 +000065#endif
66
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if defined(MBEDTLS_ERROR_C)
Paul Bakkerb2a11402013-06-24 13:02:12 +020068
Paul Bakkerdaae3b72015-02-08 15:49:54 +010069#include <stdio.h>
Paul Bakker9d781402011-05-09 16:17:09 +000070
Manuel Pégourié-Gonnardf5dc8ec2015-02-13 14:32:17 +000071HEADER_INCLUDED
Paul Bakkerd0a345e2011-11-10 13:03:42 +000072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073void mbedtls_strerror( int ret, char *buf, size_t buflen )
Paul Bakker9d781402011-05-09 16:17:09 +000074{
75 size_t len;
76 int use_ret;
77
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020078 if( buflen == 0 )
79 return;
80
Paul Bakker9d781402011-05-09 16:17:09 +000081 memset( buf, 0x00, buflen );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020082
Paul Bakker9d781402011-05-09 16:17:09 +000083 if( ret < 0 )
84 ret = -ret;
85
86 if( ret & 0xFF80 )
87 {
88 use_ret = ret & 0xFF80;
89
90 // High level error codes
91 //
Manuel Pégourié-Gonnardfe671f42014-04-11 18:06:44 +020092 // BEGIN generated code
Paul Bakker9d781402011-05-09 16:17:09 +000093HIGH_LEVEL_CODE_CHECKS
Manuel Pégourié-Gonnardfe671f42014-04-11 18:06:44 +020094 // END generated code
95
Paul Bakker9d781402011-05-09 16:17:09 +000096 if( strlen( buf ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 mbedtls_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
Paul Bakker9d781402011-05-09 16:17:09 +000098 }
99
100 use_ret = ret & ~0xFF80;
101
102 if( use_ret == 0 )
103 return;
104
105 // If high level code is present, make a concatenation between both
106 // error strings.
107 //
108 len = strlen( buf );
109
110 if( len > 0 )
111 {
112 if( buflen - len < 5 )
113 return;
114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 mbedtls_snprintf( buf + len, buflen - len, " : " );
Paul Bakker9d781402011-05-09 16:17:09 +0000116
117 buf += len + 3;
118 buflen -= len + 3;
119 }
120
121 // Low level error codes
122 //
Manuel Pégourié-Gonnardfe671f42014-04-11 18:06:44 +0200123 // BEGIN generated code
Paul Bakker9d781402011-05-09 16:17:09 +0000124LOW_LEVEL_CODE_CHECKS
Manuel Pégourié-Gonnardfe671f42014-04-11 18:06:44 +0200125 // END generated code
126
Paul Bakker9d781402011-05-09 16:17:09 +0000127 if( strlen( buf ) != 0 )
128 return;
129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 mbedtls_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
Paul Bakker9d781402011-05-09 16:17:09 +0000131}
132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133#else /* MBEDTLS_ERROR_C */
Paul Bakkera0234372013-03-20 14:42:21 +0100134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135#if defined(MBEDTLS_ERROR_STRERROR_DUMMY)
Paul Bakkera0234372013-03-20 14:42:21 +0100136
Paul Bakkera0234372013-03-20 14:42:21 +0100137/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 * Provide an non-function in case MBEDTLS_ERROR_C is not defined
Paul Bakkera0234372013-03-20 14:42:21 +0100139 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140void mbedtls_strerror( int ret, char *buf, size_t buflen )
Paul Bakkera0234372013-03-20 14:42:21 +0100141{
142 ((void) ret);
143
144 if( buflen > 0 )
145 buf[0] = '\0';
146}
147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148#endif /* MBEDTLS_ERROR_STRERROR_DUMMY */
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150#endif /* MBEDTLS_ERROR_C */