mbedtls_encoding.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* Copyright (C) 2019, Texas A&M University.
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #include <errno.h>
  14. #include "pal_crypto.h"
  15. #include "pal_error.h"
  16. #include "crypto/mbedtls/mbedtls/base64.h"
  17. #include "crypto/mbedtls/mbedtls/asn1.h"
  18. /*
  19. * Encoding a byte string in Base64 format. If "dst" is NULL, this function returns the
  20. * expected length after encoding.
  21. *
  22. * @src: The raw data for encoding.
  23. * @slen: The length of data
  24. * @dst: The buffer for storing the encoded data.
  25. * @dlen: Returns the length after encoding.
  26. */
  27. int lib_Base64Encode(const uint8_t* src, size_t slen, char* dst, size_t* dlen) {
  28. int ret = mbedtls_base64_encode((unsigned char*)dst, *dlen, dlen,
  29. (const unsigned char*)src, slen);
  30. if (ret == MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL) {
  31. return !dst ? 0 : -PAL_ERROR_OVERFLOW;
  32. } else if (ret != 0) {
  33. return -PAL_ERROR_INVAL;
  34. } else {
  35. return 0;
  36. }
  37. }
  38. /*
  39. * Decoding a byte string in Base64 format. If "dst" is NULL, this function returns the
  40. * expected length after decoding.
  41. *
  42. * @src: The Base64 string for decoding
  43. * @slen: The length of data
  44. * @dst: The buffer for storing the decoded data.
  45. * @dlen: Returns the length after decoding.
  46. */
  47. int lib_Base64Decode(const char* src, size_t slen, uint8_t* dst, size_t* dlen) {
  48. int ret = mbedtls_base64_decode((unsigned char*)dst, *dlen, dlen,
  49. (const unsigned char*)src, slen);
  50. if (ret == MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL) {
  51. return !dst ? 0 : -PAL_ERROR_OVERFLOW;
  52. } else if (ret != 0) {
  53. return -PAL_ERROR_INVAL;
  54. } else {
  55. return 0;
  56. }
  57. }
  58. /*
  59. * Retrieve the next serialized object in the ASN1 format.
  60. *
  61. * @ptr: Pass in the pointer for reading the ASN1 data. On success, will be updated
  62. * to the beginning of the next serialized object.
  63. * @end: The end of ASN1 data.
  64. * @tag: Returns the tag of the object.
  65. * @is_construct: Returns a boolean to represent whether the object is a construct object.
  66. * @buf: Returns the data field of the object.
  67. * @len: Returns the length of the data field.
  68. */
  69. int lib_ASN1GetSerial(uint8_t** ptr, const uint8_t* end, enum asn1_tag* tag, bool* is_construct,
  70. uint8_t** buf, size_t* len) {
  71. if (end - (*ptr) < 1)
  72. return -PAL_ERROR_ENDOFSTREAM;
  73. uint8_t t = *(*ptr)++;
  74. size_t l;
  75. int ret = mbedtls_asn1_get_len((unsigned char**)ptr, (const unsigned char*)end, &l);
  76. if (ret != 0)
  77. return -PAL_ERROR_INVAL;
  78. *tag = t & ~(MBEDTLS_ASN1_CONSTRUCTED|MBEDTLS_ASN1_CONTEXT_SPECIFIC);
  79. *is_construct = t & MBEDTLS_ASN1_CONSTRUCTED;
  80. *buf = *ptr;
  81. *len = l;
  82. *ptr += l;
  83. return 0;
  84. }
  85. /*
  86. * Retrieve the next ASN1 object which must be a large number (MBEDTLS_ASN1_INTEGER).
  87. * Returns -PAL_ERROR_INVAL if the object is not a large number.
  88. *
  89. * @ptr: Pass in the pointer for reading the ASN1 data. On sucess, will be updated
  90. * to the beginning of the next serialized object.
  91. * @end: The end of ASN1 data.
  92. * @len: Returns the length (number of bytes) of the large number.
  93. */
  94. int lib_ASN1GetLargeNumberLength(uint8_t** ptr, const uint8_t* end, size_t* len) {
  95. int ret = mbedtls_asn1_get_tag(ptr, end, len, MBEDTLS_ASN1_INTEGER);
  96. if (ret < 0)
  97. return -PAL_ERROR_INVAL;
  98. return 0;
  99. }
  100. /*
  101. * Retrieve the next ASN1 object which must be a bitstring. Returns -PAL_ERROR_INVAL if the
  102. * object is not a bitstring.
  103. *
  104. * @ptr: Pass in the pointer for reading the ASN1 data. On sucess, will be updated
  105. * to the beginning of the next serialized object.
  106. * @end: The end of ASN1 data.
  107. * @str: Returns the pointer to the bitstring.
  108. * @len: Returns the length of the bitstring.
  109. */
  110. int lib_ASN1GetBitstring(uint8_t** ptr, const uint8_t* end, uint8_t** str, size_t* len) {
  111. mbedtls_asn1_bitstring bs;
  112. int ret = mbedtls_asn1_get_bitstring((unsigned char**)ptr, (const unsigned char*)end, &bs);
  113. if (ret < 0)
  114. return -PAL_ERROR_INVAL;
  115. *str = (uint8_t*)bs.p;
  116. *len = bs.len;
  117. return 0;
  118. }