blob: f65881bc836fc40ef0f04d44b62638b51df57352 [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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 Bakker9d781402011-05-09 16:17:09 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker9d781402011-05-09 16:17:09 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker9d781402011-05-09 16:17:09 +000027
Janos Follathdf587ee2019-12-18 13:16:46 +000028#if defined(MBEDTLS_ERROR_STRERROR_DUMMY)
Manuel Pégourié-Gonnardf5dc8ec2015-02-13 14:32:17 +000029#include <string.h>
Paul Bakker0464dd92014-07-09 10:16:18 +020030#endif
31
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/platform.h"
Rich Evans6aa04bc2015-01-30 11:18:42 +000034#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#define mbedtls_snprintf snprintf
Simon Butcherdb0feca2016-05-17 00:03:14 +010036#define mbedtls_time_t time_t
Rich Evans6aa04bc2015-01-30 11:18:42 +000037#endif
38
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_ERROR_C)
Paul Bakkerb2a11402013-06-24 13:02:12 +020040
Paul Bakkerdaae3b72015-02-08 15:49:54 +010041#include <stdio.h>
Paul Bakker9d781402011-05-09 16:17:09 +000042
Manuel Pégourié-Gonnardf5dc8ec2015-02-13 14:32:17 +000043HEADER_INCLUDED
Paul Bakkerd0a345e2011-11-10 13:03:42 +000044
Gaurav Aggarwala9f64002020-04-09 01:44:52 -070045const char * mbedtls_high_level_strerr( int error_code )
46{
Gaurav Aggarwal6ea4fc72020-04-20 16:03:46 -070047 int high_level_error_code;
Gaurav Aggarwala9f64002020-04-09 01:44:52 -070048
Gaurav Aggarwal6ea4fc72020-04-20 16:03:46 -070049 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 Aggarwala9f64002020-04-09 01:44:52 -070056 {
Gaurav Aggarwal3d02db22020-04-11 17:14:03 -070057 /* Begin Auto-Generated Code. */
Gaurav Aggarwala4a2aa52020-04-09 11:39:04 -070058HIGH_LEVEL_CODE_CHECKS
Gaurav Aggarwal3d02db22020-04-11 17:14:03 -070059 /* End Auto-Generated Code. */
Gaurav Aggarwala4a2aa52020-04-09 11:39:04 -070060
61 default:
Gaurav Aggarwala9f64002020-04-09 01:44:52 -070062 break;
Gaurav Aggarwala9f64002020-04-09 01:44:52 -070063 }
64
Gaurav Aggarwalcabde252020-04-22 08:13:25 -070065 return( NULL );
Gaurav Aggarwala9f64002020-04-09 01:44:52 -070066}
67
68const char * mbedtls_low_level_strerr( int error_code )
69{
Gaurav Aggarwal6ea4fc72020-04-20 16:03:46 -070070 int low_level_error_code;
Gaurav Aggarwala9f64002020-04-09 01:44:52 -070071
Gaurav Aggarwal6ea4fc72020-04-20 16:03:46 -070072 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 Aggarwala9f64002020-04-09 01:44:52 -070079 {
Gaurav Aggarwal3d02db22020-04-11 17:14:03 -070080 /* Begin Auto-Generated Code. */
Gaurav Aggarwala4a2aa52020-04-09 11:39:04 -070081LOW_LEVEL_CODE_CHECKS
Gaurav Aggarwal3d02db22020-04-11 17:14:03 -070082 /* End Auto-Generated Code. */
Gaurav Aggarwala4a2aa52020-04-09 11:39:04 -070083
84 default:
Gaurav Aggarwala9f64002020-04-09 01:44:52 -070085 break;
Gaurav Aggarwala9f64002020-04-09 01:44:52 -070086 }
87
Gaurav Aggarwalcabde252020-04-22 08:13:25 -070088 return( NULL );
Gaurav Aggarwala9f64002020-04-09 01:44:52 -070089}
90
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091void mbedtls_strerror( int ret, char *buf, size_t buflen )
Paul Bakker9d781402011-05-09 16:17:09 +000092{
93 size_t len;
94 int use_ret;
Gaurav Aggarwala9f64002020-04-09 01:44:52 -070095 const char * high_level_error_description = NULL;
96 const char * low_level_error_description = NULL;
Paul Bakker9d781402011-05-09 16:17:09 +000097
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020098 if( buflen == 0 )
99 return;
100
Paul Bakker9d781402011-05-09 16:17:09 +0000101 memset( buf, 0x00, buflen );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200102
Paul Bakker9d781402011-05-09 16:17:09 +0000103 if( ret < 0 )
104 ret = -ret;
105
106 if( ret & 0xFF80 )
107 {
108 use_ret = ret & 0xFF80;
109
Gaurav Aggarwala9f64002020-04-09 01:44:52 -0700110 // Translate high level error code.
Gaurav Aggarwal6ea4fc72020-04-20 16:03:46 -0700111 high_level_error_description = mbedtls_high_level_strerr( ret );
Manuel Pégourié-Gonnardfe671f42014-04-11 18:06:44 +0200112
Gaurav Aggarwala9f64002020-04-09 01:44:52 -0700113 if( high_level_error_description == NULL )
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200114 mbedtls_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", (unsigned int) use_ret );
Gaurav Aggarwala9f64002020-04-09 01:44:52 -0700115 else
116 mbedtls_snprintf( buf, buflen, "%s", high_level_error_description );
117
Gaurav Aggarwala4a2aa52020-04-09 11:39:04 -0700118#if defined(MBEDTLS_SSL_TLS_C)
Gaurav Aggarwala9f64002020-04-09 01:44:52 -0700119 // 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 Aggarwala4a2aa52020-04-09 11:39:04 -0700123#endif /* MBEDTLS_SSL_TLS_C */
Paul Bakker9d781402011-05-09 16:17:09 +0000124 }
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é-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 mbedtls_snprintf( buf + len, buflen - len, " : " );
Paul Bakker9d781402011-05-09 16:17:09 +0000142
143 buf += len + 3;
144 buflen -= len + 3;
145 }
146
Gaurav Aggarwala9f64002020-04-09 01:44:52 -0700147 // Translate low level error code.
Gaurav Aggarwal6ea4fc72020-04-20 16:03:46 -0700148 low_level_error_description = mbedtls_low_level_strerr( ret );
Manuel Pégourié-Gonnardfe671f42014-04-11 18:06:44 +0200149
Gaurav Aggarwala9f64002020-04-09 01:44:52 -0700150 if( low_level_error_description == NULL )
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200151 mbedtls_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", (unsigned int) use_ret );
Gaurav Aggarwala9f64002020-04-09 01:44:52 -0700152 else
153 mbedtls_snprintf( buf, buflen, "%s", low_level_error_description );
Paul Bakker9d781402011-05-09 16:17:09 +0000154}
155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156#else /* MBEDTLS_ERROR_C */
Paul Bakkera0234372013-03-20 14:42:21 +0100157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158#if defined(MBEDTLS_ERROR_STRERROR_DUMMY)
Paul Bakkera0234372013-03-20 14:42:21 +0100159
Paul Bakkera0234372013-03-20 14:42:21 +0100160/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 * Provide an non-function in case MBEDTLS_ERROR_C is not defined
Paul Bakkera0234372013-03-20 14:42:21 +0100162 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163void mbedtls_strerror( int ret, char *buf, size_t buflen )
Paul Bakkera0234372013-03-20 14:42:21 +0100164{
165 ((void) ret);
166
167 if( buflen > 0 )
168 buf[0] = '\0';
169}
170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171#endif /* MBEDTLS_ERROR_STRERROR_DUMMY */
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173#endif /* MBEDTLS_ERROR_C */