evp_asn1.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/asn1.h>
  12. #include <openssl/asn1t.h>
  13. #include "crypto/asn1.h"
  14. int ASN1_TYPE_set_octetstring(ASN1_TYPE *a, unsigned char *data, int len)
  15. {
  16. ASN1_STRING *os;
  17. if ((os = ASN1_OCTET_STRING_new()) == NULL)
  18. return 0;
  19. if (!ASN1_OCTET_STRING_set(os, data, len)) {
  20. ASN1_OCTET_STRING_free(os);
  21. return 0;
  22. }
  23. ASN1_TYPE_set(a, V_ASN1_OCTET_STRING, os);
  24. return 1;
  25. }
  26. /* int max_len: for returned value */
  27. int ASN1_TYPE_get_octetstring(const ASN1_TYPE *a, unsigned char *data, int max_len)
  28. {
  29. int ret, num;
  30. const unsigned char *p;
  31. if ((a->type != V_ASN1_OCTET_STRING) || (a->value.octet_string == NULL)) {
  32. ERR_raise(ERR_LIB_ASN1, ASN1_R_DATA_IS_WRONG);
  33. return -1;
  34. }
  35. p = ASN1_STRING_get0_data(a->value.octet_string);
  36. ret = ASN1_STRING_length(a->value.octet_string);
  37. if (ret < max_len)
  38. num = ret;
  39. else
  40. num = max_len;
  41. memcpy(data, p, num);
  42. return ret;
  43. }
  44. static ossl_inline void asn1_type_init_oct(ASN1_OCTET_STRING *oct,
  45. unsigned char *data, int len)
  46. {
  47. oct->data = data;
  48. oct->type = V_ASN1_OCTET_STRING;
  49. oct->length = len;
  50. oct->flags = 0;
  51. }
  52. static int asn1_type_get_int_oct(ASN1_OCTET_STRING *oct, int32_t anum,
  53. long *num, unsigned char *data, int max_len)
  54. {
  55. int ret = ASN1_STRING_length(oct), n;
  56. if (num != NULL)
  57. *num = anum;
  58. if (max_len > ret)
  59. n = ret;
  60. else
  61. n = max_len;
  62. if (data != NULL)
  63. memcpy(data, ASN1_STRING_get0_data(oct), n);
  64. return ret;
  65. }
  66. typedef struct {
  67. int32_t num;
  68. ASN1_OCTET_STRING *oct;
  69. } asn1_int_oct;
  70. ASN1_SEQUENCE(asn1_int_oct) = {
  71. ASN1_EMBED(asn1_int_oct, num, INT32),
  72. ASN1_SIMPLE(asn1_int_oct, oct, ASN1_OCTET_STRING)
  73. } static_ASN1_SEQUENCE_END(asn1_int_oct)
  74. DECLARE_ASN1_ITEM(asn1_int_oct)
  75. int ASN1_TYPE_set_int_octetstring(ASN1_TYPE *a, long num, unsigned char *data,
  76. int len)
  77. {
  78. asn1_int_oct atmp;
  79. ASN1_OCTET_STRING oct;
  80. atmp.num = num;
  81. atmp.oct = &oct;
  82. asn1_type_init_oct(&oct, data, len);
  83. if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(asn1_int_oct), &atmp, &a))
  84. return 1;
  85. return 0;
  86. }
  87. int ASN1_TYPE_get_int_octetstring(const ASN1_TYPE *a, long *num,
  88. unsigned char *data, int max_len)
  89. {
  90. asn1_int_oct *atmp = NULL;
  91. int ret = -1;
  92. if ((a->type != V_ASN1_SEQUENCE) || (a->value.sequence == NULL)) {
  93. goto err;
  94. }
  95. atmp = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(asn1_int_oct), a);
  96. if (atmp == NULL)
  97. goto err;
  98. ret = asn1_type_get_int_oct(atmp->oct, atmp->num, num, data, max_len);
  99. if (ret == -1) {
  100. err:
  101. ERR_raise(ERR_LIB_ASN1, ASN1_R_DATA_IS_WRONG);
  102. }
  103. M_ASN1_free_of(atmp, asn1_int_oct);
  104. return ret;
  105. }
  106. typedef struct {
  107. ASN1_OCTET_STRING *oct;
  108. int32_t num;
  109. } asn1_oct_int;
  110. /*
  111. * Defined in RFC 5084 -
  112. * Section 2. "Content-Authenticated Encryption Algorithms"
  113. */
  114. ASN1_SEQUENCE(asn1_oct_int) = {
  115. ASN1_SIMPLE(asn1_oct_int, oct, ASN1_OCTET_STRING),
  116. ASN1_EMBED(asn1_oct_int, num, INT32)
  117. } static_ASN1_SEQUENCE_END(asn1_oct_int)
  118. DECLARE_ASN1_ITEM(asn1_oct_int)
  119. int asn1_type_set_octetstring_int(ASN1_TYPE *a, long num, unsigned char *data,
  120. int len)
  121. {
  122. asn1_oct_int atmp;
  123. ASN1_OCTET_STRING oct;
  124. atmp.num = num;
  125. atmp.oct = &oct;
  126. asn1_type_init_oct(&oct, data, len);
  127. if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(asn1_oct_int), &atmp, &a))
  128. return 1;
  129. return 0;
  130. }
  131. int asn1_type_get_octetstring_int(const ASN1_TYPE *a, long *num,
  132. unsigned char *data, int max_len)
  133. {
  134. asn1_oct_int *atmp = NULL;
  135. int ret = -1;
  136. if ((a->type != V_ASN1_SEQUENCE) || (a->value.sequence == NULL))
  137. goto err;
  138. atmp = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(asn1_oct_int), a);
  139. if (atmp == NULL)
  140. goto err;
  141. ret = asn1_type_get_int_oct(atmp->oct, atmp->num, num, data, max_len);
  142. if (ret == -1) {
  143. err:
  144. ERR_raise(ERR_LIB_ASN1, ASN1_R_DATA_IS_WRONG);
  145. }
  146. M_ASN1_free_of(atmp, asn1_oct_int);
  147. return ret;
  148. }