error.fmt 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Error message information
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * 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.
  18. */
  19. #include "common.h"
  20. #include "mbedtls/error.h"
  21. #if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY)
  22. #if defined(MBEDTLS_ERROR_C)
  23. #if defined(MBEDTLS_PLATFORM_C)
  24. #include "mbedtls/platform.h"
  25. #else
  26. #define mbedtls_snprintf snprintf
  27. #endif
  28. #include <stdio.h>
  29. #include <string.h>
  30. HEADER_INCLUDED
  31. const char * mbedtls_high_level_strerr( int error_code )
  32. {
  33. int high_level_error_code;
  34. if( error_code < 0 )
  35. error_code = -error_code;
  36. /* Extract the high-level part from the error code. */
  37. high_level_error_code = error_code & 0xFF80;
  38. switch( high_level_error_code )
  39. {
  40. /* Begin Auto-Generated Code. */
  41. HIGH_LEVEL_CODE_CHECKS
  42. /* End Auto-Generated Code. */
  43. default:
  44. break;
  45. }
  46. return( NULL );
  47. }
  48. const char * mbedtls_low_level_strerr( int error_code )
  49. {
  50. int low_level_error_code;
  51. if( error_code < 0 )
  52. error_code = -error_code;
  53. /* Extract the low-level part from the error code. */
  54. low_level_error_code = error_code & ~0xFF80;
  55. switch( low_level_error_code )
  56. {
  57. /* Begin Auto-Generated Code. */
  58. LOW_LEVEL_CODE_CHECKS
  59. /* End Auto-Generated Code. */
  60. default:
  61. break;
  62. }
  63. return( NULL );
  64. }
  65. void mbedtls_strerror( int ret, char *buf, size_t buflen )
  66. {
  67. size_t len;
  68. int use_ret;
  69. const char * high_level_error_description = NULL;
  70. const char * low_level_error_description = NULL;
  71. if( buflen == 0 )
  72. return;
  73. memset( buf, 0x00, buflen );
  74. if( ret < 0 )
  75. ret = -ret;
  76. if( ret & 0xFF80 )
  77. {
  78. use_ret = ret & 0xFF80;
  79. // Translate high level error code.
  80. high_level_error_description = mbedtls_high_level_strerr( ret );
  81. if( high_level_error_description == NULL )
  82. mbedtls_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", (unsigned int) use_ret );
  83. else
  84. mbedtls_snprintf( buf, buflen, "%s", high_level_error_description );
  85. #if defined(MBEDTLS_SSL_TLS_C)
  86. // Early return in case of a fatal error - do not try to translate low
  87. // level code.
  88. if(use_ret == -(MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE))
  89. return;
  90. #endif /* MBEDTLS_SSL_TLS_C */
  91. }
  92. use_ret = ret & ~0xFF80;
  93. if( use_ret == 0 )
  94. return;
  95. // If high level code is present, make a concatenation between both
  96. // error strings.
  97. //
  98. len = strlen( buf );
  99. if( len > 0 )
  100. {
  101. if( buflen - len < 5 )
  102. return;
  103. mbedtls_snprintf( buf + len, buflen - len, " : " );
  104. buf += len + 3;
  105. buflen -= len + 3;
  106. }
  107. // Translate low level error code.
  108. low_level_error_description = mbedtls_low_level_strerr( ret );
  109. if( low_level_error_description == NULL )
  110. mbedtls_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", (unsigned int) use_ret );
  111. else
  112. mbedtls_snprintf( buf, buflen, "%s", low_level_error_description );
  113. }
  114. #else /* MBEDTLS_ERROR_C */
  115. /*
  116. * Provide an non-function in case MBEDTLS_ERROR_C is not defined
  117. */
  118. void mbedtls_strerror( int ret, char *buf, size_t buflen )
  119. {
  120. ((void) ret);
  121. if( buflen > 0 )
  122. buf[0] = '\0';
  123. }
  124. #endif /* MBEDTLS_ERROR_C */
  125. #if defined(MBEDTLS_TEST_HOOKS)
  126. void (*mbedtls_test_hook_error_add)( int, int, const char *, int );
  127. #endif
  128. #endif /* MBEDTLS_ERROR_C || MBEDTLS_ERROR_STRERROR_DUMMY */