blob: ddd1be7a69645f2e281e7beac088a4c31322fafc [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
Gilles Peskinedb09ef62020-06-03 01:43:33 +020022#include "common.h"
Paul Bakker9d781402011-05-09 16:17:09 +000023
Janos Follathdf587ee2019-12-18 13:16:46 +000024#if defined(MBEDTLS_ERROR_STRERROR_DUMMY)
Manuel Pégourié-Gonnardf5dc8ec2015-02-13 14:32:17 +000025#include <string.h>
Paul Bakker0464dd92014-07-09 10:16:18 +020026#endif
27
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evans6aa04bc2015-01-30 11:18:42 +000030#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#define mbedtls_snprintf snprintf
Simon Butcherdb0feca2016-05-17 00:03:14 +010032#define mbedtls_time_t time_t
Rich Evans6aa04bc2015-01-30 11:18:42 +000033#endif
34
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_ERROR_C)
Paul Bakkerb2a11402013-06-24 13:02:12 +020036
Paul Bakkerdaae3b72015-02-08 15:49:54 +010037#include <stdio.h>
Paul Bakker9d781402011-05-09 16:17:09 +000038
Manuel Pégourié-Gonnardf5dc8ec2015-02-13 14:32:17 +000039HEADER_INCLUDED
Paul Bakkerd0a345e2011-11-10 13:03:42 +000040
Gaurav Aggarwala9f64002020-04-09 01:44:52 -070041const char * mbedtls_high_level_strerr( int error_code )
42{
Gaurav Aggarwal6ea4fc72020-04-20 16:03:46 -070043 int high_level_error_code;
Gaurav Aggarwala9f64002020-04-09 01:44:52 -070044
Gaurav Aggarwal6ea4fc72020-04-20 16:03:46 -070045 if( error_code < 0 )
46 error_code = -error_code;
47
48 /* Extract the high-level part from the error code. */
49 high_level_error_code = error_code & 0xFF80;
50
51 switch( high_level_error_code )
Gaurav Aggarwala9f64002020-04-09 01:44:52 -070052 {
Gaurav Aggarwal3d02db22020-04-11 17:14:03 -070053 /* Begin Auto-Generated Code. */
Gaurav Aggarwala4a2aa52020-04-09 11:39:04 -070054HIGH_LEVEL_CODE_CHECKS
Gaurav Aggarwal3d02db22020-04-11 17:14:03 -070055 /* End Auto-Generated Code. */
Gaurav Aggarwala4a2aa52020-04-09 11:39:04 -070056
57 default:
Gaurav Aggarwala9f64002020-04-09 01:44:52 -070058 break;
Gaurav Aggarwala9f64002020-04-09 01:44:52 -070059 }
60
Gaurav Aggarwalcabde252020-04-22 08:13:25 -070061 return( NULL );
Gaurav Aggarwala9f64002020-04-09 01:44:52 -070062}
63
64const char * mbedtls_low_level_strerr( int error_code )
65{
Gaurav Aggarwal6ea4fc72020-04-20 16:03:46 -070066 int low_level_error_code;
Gaurav Aggarwala9f64002020-04-09 01:44:52 -070067
Gaurav Aggarwal6ea4fc72020-04-20 16:03:46 -070068 if( error_code < 0 )
69 error_code = -error_code;
70
71 /* Extract the low-level part from the error code. */
72 low_level_error_code = error_code & ~0xFF80;
73
74 switch( low_level_error_code )
Gaurav Aggarwala9f64002020-04-09 01:44:52 -070075 {
Gaurav Aggarwal3d02db22020-04-11 17:14:03 -070076 /* Begin Auto-Generated Code. */
Gaurav Aggarwala4a2aa52020-04-09 11:39:04 -070077LOW_LEVEL_CODE_CHECKS
Gaurav Aggarwal3d02db22020-04-11 17:14:03 -070078 /* End Auto-Generated Code. */
Gaurav Aggarwala4a2aa52020-04-09 11:39:04 -070079
80 default:
Gaurav Aggarwala9f64002020-04-09 01:44:52 -070081 break;
Gaurav Aggarwala9f64002020-04-09 01:44:52 -070082 }
83
Gaurav Aggarwalcabde252020-04-22 08:13:25 -070084 return( NULL );
Gaurav Aggarwala9f64002020-04-09 01:44:52 -070085}
86
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087void mbedtls_strerror( int ret, char *buf, size_t buflen )
Paul Bakker9d781402011-05-09 16:17:09 +000088{
89 size_t len;
90 int use_ret;
Gaurav Aggarwala9f64002020-04-09 01:44:52 -070091 const char * high_level_error_description = NULL;
92 const char * low_level_error_description = NULL;
Paul Bakker9d781402011-05-09 16:17:09 +000093
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020094 if( buflen == 0 )
95 return;
96
Paul Bakker9d781402011-05-09 16:17:09 +000097 memset( buf, 0x00, buflen );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020098
Paul Bakker9d781402011-05-09 16:17:09 +000099 if( ret < 0 )
100 ret = -ret;
101
102 if( ret & 0xFF80 )
103 {
104 use_ret = ret & 0xFF80;
105
Gaurav Aggarwala9f64002020-04-09 01:44:52 -0700106 // Translate high level error code.
Gaurav Aggarwal6ea4fc72020-04-20 16:03:46 -0700107 high_level_error_description = mbedtls_high_level_strerr( ret );
Manuel Pégourié-Gonnardfe671f42014-04-11 18:06:44 +0200108
Gaurav Aggarwala9f64002020-04-09 01:44:52 -0700109 if( high_level_error_description == NULL )
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200110 mbedtls_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", (unsigned int) use_ret );
Gaurav Aggarwala9f64002020-04-09 01:44:52 -0700111 else
112 mbedtls_snprintf( buf, buflen, "%s", high_level_error_description );
113
Gaurav Aggarwala4a2aa52020-04-09 11:39:04 -0700114#if defined(MBEDTLS_SSL_TLS_C)
Gaurav Aggarwala9f64002020-04-09 01:44:52 -0700115 // Early return in case of a fatal error - do not try to translate low
116 // level code.
117 if(use_ret == -(MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE))
118 return;
Gaurav Aggarwala4a2aa52020-04-09 11:39:04 -0700119#endif /* MBEDTLS_SSL_TLS_C */
Paul Bakker9d781402011-05-09 16:17:09 +0000120 }
121
122 use_ret = ret & ~0xFF80;
123
124 if( use_ret == 0 )
125 return;
126
127 // If high level code is present, make a concatenation between both
128 // error strings.
129 //
130 len = strlen( buf );
131
132 if( len > 0 )
133 {
134 if( buflen - len < 5 )
135 return;
136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 mbedtls_snprintf( buf + len, buflen - len, " : " );
Paul Bakker9d781402011-05-09 16:17:09 +0000138
139 buf += len + 3;
140 buflen -= len + 3;
141 }
142
Gaurav Aggarwala9f64002020-04-09 01:44:52 -0700143 // Translate low level error code.
Gaurav Aggarwal6ea4fc72020-04-20 16:03:46 -0700144 low_level_error_description = mbedtls_low_level_strerr( ret );
Manuel Pégourié-Gonnardfe671f42014-04-11 18:06:44 +0200145
Gaurav Aggarwala9f64002020-04-09 01:44:52 -0700146 if( low_level_error_description == NULL )
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200147 mbedtls_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", (unsigned int) use_ret );
Gaurav Aggarwala9f64002020-04-09 01:44:52 -0700148 else
149 mbedtls_snprintf( buf, buflen, "%s", low_level_error_description );
Paul Bakker9d781402011-05-09 16:17:09 +0000150}
151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152#else /* MBEDTLS_ERROR_C */
Paul Bakkera0234372013-03-20 14:42:21 +0100153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154#if defined(MBEDTLS_ERROR_STRERROR_DUMMY)
Paul Bakkera0234372013-03-20 14:42:21 +0100155
Paul Bakkera0234372013-03-20 14:42:21 +0100156/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 * Provide an non-function in case MBEDTLS_ERROR_C is not defined
Paul Bakkera0234372013-03-20 14:42:21 +0100158 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159void mbedtls_strerror( int ret, char *buf, size_t buflen )
Paul Bakkera0234372013-03-20 14:42:21 +0100160{
161 ((void) ret);
162
163 if( buflen > 0 )
164 buf[0] = '\0';
165}
166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167#endif /* MBEDTLS_ERROR_STRERROR_DUMMY */
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169#endif /* MBEDTLS_ERROR_C */