blob: c54ab906f0495cfb50f0f092deb71f1f6dab84b5 [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 -070045typedef struct mbedtls_error
46{
47 int code; /* Error code. */
48 const char * description; /* Error description. */
49} mbedtls_error_t;
50
51static mbedtls_error_t high_level_errors[] =
52{
53HIGH_LEVEL_CODE_CHECKS
54};
55
56#define NUM_HIGH_LEVEL_ERRORS ( sizeof(high_level_errors)/sizeof(mbedtls_error_t) )
57
58static mbedtls_error_t low_level_errors[] =
59{
60LOW_LEVEL_CODE_CHECKS
61};
62
63#define NUM_LOW_LEVEL_ERRORS ( sizeof(low_level_errors)/sizeof(mbedtls_error_t) )
64
65const char * mbedtls_high_level_strerr( int error_code )
66{
67 size_t i;
68 const char *error_description = NULL;
69
70 for(i = 0; i < NUM_HIGH_LEVEL_ERRORS; i++ )
71 {
72 if( high_level_errors[i].code == error_code )
73 {
74 error_description = high_level_errors[i].description;
75 break;
76 }
77 }
78
79 return error_description;
80}
81
82const char * mbedtls_low_level_strerr( int error_code )
83{
84 size_t i;
85 const char *error_description = NULL;
86
87 for(i = 0; i < NUM_LOW_LEVEL_ERRORS; i++ )
88 {
89 if( low_level_errors[i].code == error_code )
90 {
91 error_description = low_level_errors[i].description;
92 break;
93 }
94 }
95
96 return error_description;
97}
98
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099void mbedtls_strerror( int ret, char *buf, size_t buflen )
Paul Bakker9d781402011-05-09 16:17:09 +0000100{
101 size_t len;
102 int use_ret;
Gaurav Aggarwala9f64002020-04-09 01:44:52 -0700103 const char * high_level_error_description = NULL;
104 const char * low_level_error_description = NULL;
Paul Bakker9d781402011-05-09 16:17:09 +0000105
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200106 if( buflen == 0 )
107 return;
108
Paul Bakker9d781402011-05-09 16:17:09 +0000109 memset( buf, 0x00, buflen );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200110
Paul Bakker9d781402011-05-09 16:17:09 +0000111 if( ret < 0 )
112 ret = -ret;
113
114 if( ret & 0xFF80 )
115 {
116 use_ret = ret & 0xFF80;
117
Gaurav Aggarwala9f64002020-04-09 01:44:52 -0700118 // Translate high level error code.
119 high_level_error_description = mbedtls_high_level_strerr(use_ret);
Manuel Pégourié-Gonnardfe671f42014-04-11 18:06:44 +0200120
Gaurav Aggarwala9f64002020-04-09 01:44:52 -0700121 if( high_level_error_description == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 mbedtls_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
Gaurav Aggarwala9f64002020-04-09 01:44:52 -0700123 else
124 mbedtls_snprintf( buf, buflen, "%s", high_level_error_description );
125
126 // Early return in case of a fatal error - do not try to translate low
127 // level code.
128 if(use_ret == -(MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE))
129 return;
Paul Bakker9d781402011-05-09 16:17:09 +0000130 }
131
132 use_ret = ret & ~0xFF80;
133
134 if( use_ret == 0 )
135 return;
136
137 // If high level code is present, make a concatenation between both
138 // error strings.
139 //
140 len = strlen( buf );
141
142 if( len > 0 )
143 {
144 if( buflen - len < 5 )
145 return;
146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_snprintf( buf + len, buflen - len, " : " );
Paul Bakker9d781402011-05-09 16:17:09 +0000148
149 buf += len + 3;
150 buflen -= len + 3;
151 }
152
Gaurav Aggarwala9f64002020-04-09 01:44:52 -0700153 // Translate low level error code.
154 low_level_error_description = mbedtls_low_level_strerr( use_ret );
Manuel Pégourié-Gonnardfe671f42014-04-11 18:06:44 +0200155
Gaurav Aggarwala9f64002020-04-09 01:44:52 -0700156 if( low_level_error_description == NULL )
157 mbedtls_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
158 else
159 mbedtls_snprintf( buf, buflen, "%s", low_level_error_description );
Paul Bakker9d781402011-05-09 16:17:09 +0000160}
161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162#else /* MBEDTLS_ERROR_C */
Paul Bakkera0234372013-03-20 14:42:21 +0100163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164#if defined(MBEDTLS_ERROR_STRERROR_DUMMY)
Paul Bakkera0234372013-03-20 14:42:21 +0100165
Paul Bakkera0234372013-03-20 14:42:21 +0100166/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 * Provide an non-function in case MBEDTLS_ERROR_C is not defined
Paul Bakkera0234372013-03-20 14:42:21 +0100168 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169void mbedtls_strerror( int ret, char *buf, size_t buflen )
Paul Bakkera0234372013-03-20 14:42:21 +0100170{
171 ((void) ret);
172
173 if( buflen > 0 )
174 buf[0] = '\0';
175}
176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177#endif /* MBEDTLS_ERROR_STRERROR_DUMMY */
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179#endif /* MBEDTLS_ERROR_C */