error.fmt 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. #include "mbedtls/platform.h"
  24. #include <stdio.h>
  25. #include <string.h>
  26. HEADER_INCLUDED
  27. const char *mbedtls_high_level_strerr(int error_code)
  28. {
  29. int high_level_error_code;
  30. if (error_code < 0) {
  31. error_code = -error_code;
  32. }
  33. /* Extract the high-level part from the error code. */
  34. high_level_error_code = error_code & 0xFF80;
  35. switch (high_level_error_code) {
  36. /* Begin Auto-Generated Code. */
  37. HIGH_LEVEL_CODE_CHECKS
  38. /* End Auto-Generated Code. */
  39. default:
  40. break;
  41. }
  42. return NULL;
  43. }
  44. const char *mbedtls_low_level_strerr(int error_code)
  45. {
  46. int low_level_error_code;
  47. if (error_code < 0) {
  48. error_code = -error_code;
  49. }
  50. /* Extract the low-level part from the error code. */
  51. low_level_error_code = error_code & ~0xFF80;
  52. switch (low_level_error_code) {
  53. /* Begin Auto-Generated Code. */
  54. LOW_LEVEL_CODE_CHECKS
  55. /* End Auto-Generated Code. */
  56. default:
  57. break;
  58. }
  59. return NULL;
  60. }
  61. void mbedtls_strerror(int ret, char *buf, size_t buflen)
  62. {
  63. size_t len;
  64. int use_ret;
  65. const char *high_level_error_description = NULL;
  66. const char *low_level_error_description = NULL;
  67. if (buflen == 0) {
  68. return;
  69. }
  70. memset(buf, 0x00, buflen);
  71. if (ret < 0) {
  72. ret = -ret;
  73. }
  74. if (ret & 0xFF80) {
  75. use_ret = ret & 0xFF80;
  76. // Translate high level error code.
  77. high_level_error_description = mbedtls_high_level_strerr(ret);
  78. if (high_level_error_description == NULL) {
  79. mbedtls_snprintf(buf, buflen, "UNKNOWN ERROR CODE (%04X)", (unsigned int) use_ret);
  80. } else {
  81. mbedtls_snprintf(buf, buflen, "%s", high_level_error_description);
  82. }
  83. #if defined(MBEDTLS_SSL_TLS_C)
  84. // Early return in case of a fatal error - do not try to translate low
  85. // level code.
  86. if (use_ret == -(MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE)) {
  87. return;
  88. }
  89. #endif /* MBEDTLS_SSL_TLS_C */
  90. }
  91. use_ret = ret & ~0xFF80;
  92. if (use_ret == 0) {
  93. return;
  94. }
  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. if (buflen - len < 5) {
  101. return;
  102. }
  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. }
  115. #else /* MBEDTLS_ERROR_C */
  116. /*
  117. * Provide a dummy implementation when MBEDTLS_ERROR_C is not defined
  118. */
  119. void mbedtls_strerror(int ret, char *buf, size_t buflen)
  120. {
  121. ((void) ret);
  122. if (buflen > 0) {
  123. buf[0] = '\0';
  124. }
  125. }
  126. #endif /* MBEDTLS_ERROR_C */
  127. #if defined(MBEDTLS_TEST_HOOKS)
  128. void (*mbedtls_test_hook_error_add)(int, int, const char *, int);
  129. #endif
  130. #endif /* MBEDTLS_ERROR_C || MBEDTLS_ERROR_STRERROR_DUMMY */