cal.c 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/cal/cal.h>
  6. #include <aws/common/common.h>
  7. #include <aws/common/error.h>
  8. #define AWS_DEFINE_ERROR_INFO_CAL(CODE, STR) [(CODE)-0x1C00] = AWS_DEFINE_ERROR_INFO(CODE, STR, "aws-c-cal")
  9. static struct aws_error_info s_errors[] = {
  10. AWS_DEFINE_ERROR_INFO_CAL(AWS_ERROR_CAL_SIGNATURE_VALIDATION_FAILED, "Verify on a cryptographic signature failed."),
  11. AWS_DEFINE_ERROR_INFO_CAL(
  12. AWS_ERROR_CAL_MISSING_REQUIRED_KEY_COMPONENT,
  13. "An attempt was made to perform an "
  14. "Asymmetric cryptographic operation with the"
  15. "wrong key component. For example, attempt to"
  16. "verify a signature with a private key or "
  17. "sign a message with a public key."),
  18. AWS_DEFINE_ERROR_INFO_CAL(
  19. AWS_ERROR_CAL_INVALID_KEY_LENGTH_FOR_ALGORITHM,
  20. "A key length was used for an algorithm that needs a different key length"),
  21. AWS_DEFINE_ERROR_INFO_CAL(
  22. AWS_ERROR_CAL_UNKNOWN_OBJECT_IDENTIFIER,
  23. "An ASN.1 OID was encountered that wasn't expected or understood. Most likely, an unsupported algorithm was "
  24. "encountered."),
  25. AWS_DEFINE_ERROR_INFO_CAL(
  26. AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED,
  27. "An ASN.1 DER decoding operation failed on malformed input."),
  28. AWS_DEFINE_ERROR_INFO_CAL(
  29. AWS_ERROR_CAL_MISMATCHED_DER_TYPE,
  30. "An invalid DER type was requested during encoding/decoding"),
  31. AWS_DEFINE_ERROR_INFO_CAL(
  32. AWS_ERROR_CAL_UNSUPPORTED_ALGORITHM,
  33. "The specified algorithim is unsupported on this platform."),
  34. AWS_DEFINE_ERROR_INFO_CAL(
  35. AWS_ERROR_CAL_BUFFER_TOO_LARGE_FOR_ALGORITHM,
  36. "The input passed to a cipher algorithm was too large for that algorithm. Consider breaking the input into "
  37. "smaller chunks."),
  38. AWS_DEFINE_ERROR_INFO_CAL(
  39. AWS_ERROR_CAL_INVALID_CIPHER_MATERIAL_SIZE_FOR_ALGORITHM,
  40. "A cipher material such as an initialization vector or tag was an incorrect size for the selected algorithm."),
  41. };
  42. static struct aws_error_info_list s_list = {
  43. .error_list = s_errors,
  44. .count = AWS_ARRAY_SIZE(s_errors),
  45. };
  46. static struct aws_log_subject_info s_cal_log_subject_infos[] = {
  47. DEFINE_LOG_SUBJECT_INFO(
  48. AWS_LS_CAL_GENERAL,
  49. "aws-c-cal",
  50. "Subject for Cal logging that doesn't belong to any particular category"),
  51. DEFINE_LOG_SUBJECT_INFO(AWS_LS_CAL_ECC, "ecc", "Subject for elliptic curve cryptography specific logging."),
  52. DEFINE_LOG_SUBJECT_INFO(AWS_LS_CAL_HASH, "hash", "Subject for hashing specific logging."),
  53. DEFINE_LOG_SUBJECT_INFO(AWS_LS_CAL_HMAC, "hmac", "Subject for hmac specific logging."),
  54. DEFINE_LOG_SUBJECT_INFO(AWS_LS_CAL_DER, "der", "Subject for der specific logging."),
  55. DEFINE_LOG_SUBJECT_INFO(
  56. AWS_LS_CAL_LIBCRYPTO_RESOLVE,
  57. "libcrypto_resolve",
  58. "Subject for libcrypto symbol resolution logging."),
  59. };
  60. static struct aws_log_subject_info_list s_cal_log_subject_list = {
  61. .subject_list = s_cal_log_subject_infos,
  62. .count = AWS_ARRAY_SIZE(s_cal_log_subject_infos),
  63. };
  64. #ifndef BYO_CRYPTO
  65. extern void aws_cal_platform_init(struct aws_allocator *allocator);
  66. extern void aws_cal_platform_clean_up(void);
  67. #endif /* BYO_CRYPTO */
  68. static bool s_cal_library_initialized = false;
  69. void aws_cal_library_init(struct aws_allocator *allocator) {
  70. if (!s_cal_library_initialized) {
  71. aws_common_library_init(allocator);
  72. aws_register_error_info(&s_list);
  73. aws_register_log_subject_info_list(&s_cal_log_subject_list);
  74. #ifndef BYO_CRYPTO
  75. aws_cal_platform_init(allocator);
  76. #endif /* BYO_CRYPTO */
  77. s_cal_library_initialized = true;
  78. }
  79. }
  80. void aws_cal_library_clean_up(void) {
  81. if (s_cal_library_initialized) {
  82. s_cal_library_initialized = false;
  83. #ifndef BYO_CRYPTO
  84. aws_cal_platform_clean_up();
  85. #endif /* BYO_CRYPTO */
  86. aws_unregister_log_subject_info_list(&s_cal_log_subject_list);
  87. aws_unregister_error_info(&s_list);
  88. aws_common_library_clean_up();
  89. }
  90. }