evp_asn1.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright 1995-2021 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. * if passing NULL in data, nothing is copied but the necessary length
  28. * for it is returned.
  29. */
  30. int ASN1_TYPE_get_octetstring(const ASN1_TYPE *a, unsigned char *data, int max_len)
  31. {
  32. int ret, num;
  33. const unsigned char *p;
  34. if ((a->type != V_ASN1_OCTET_STRING) || (a->value.octet_string == NULL)) {
  35. ERR_raise(ERR_LIB_ASN1, ASN1_R_DATA_IS_WRONG);
  36. return -1;
  37. }
  38. p = ASN1_STRING_get0_data(a->value.octet_string);
  39. ret = ASN1_STRING_length(a->value.octet_string);
  40. if (ret < max_len)
  41. num = ret;
  42. else
  43. num = max_len;
  44. if (num > 0 && data != NULL)
  45. memcpy(data, p, num);
  46. return ret;
  47. }
  48. static ossl_inline void asn1_type_init_oct(ASN1_OCTET_STRING *oct,
  49. unsigned char *data, int len)
  50. {
  51. oct->data = data;
  52. oct->type = V_ASN1_OCTET_STRING;
  53. oct->length = len;
  54. oct->flags = 0;
  55. }
  56. static int asn1_type_get_int_oct(ASN1_OCTET_STRING *oct, int32_t anum,
  57. long *num, unsigned char *data, int max_len)
  58. {
  59. int ret = ASN1_STRING_length(oct), n;
  60. if (num != NULL)
  61. *num = anum;
  62. if (max_len > ret)
  63. n = ret;
  64. else
  65. n = max_len;
  66. if (data != NULL)
  67. memcpy(data, ASN1_STRING_get0_data(oct), n);
  68. return ret;
  69. }
  70. typedef struct {
  71. int32_t num;
  72. ASN1_OCTET_STRING *oct;
  73. } asn1_int_oct;
  74. ASN1_SEQUENCE(asn1_int_oct) = {
  75. ASN1_EMBED(asn1_int_oct, num, INT32),
  76. ASN1_SIMPLE(asn1_int_oct, oct, ASN1_OCTET_STRING)
  77. } static_ASN1_SEQUENCE_END(asn1_int_oct)
  78. DECLARE_ASN1_ITEM(asn1_int_oct)
  79. int ASN1_TYPE_set_int_octetstring(ASN1_TYPE *a, long num, unsigned char *data,
  80. int len)
  81. {
  82. asn1_int_oct atmp;
  83. ASN1_OCTET_STRING oct;
  84. atmp.num = num;
  85. atmp.oct = &oct;
  86. asn1_type_init_oct(&oct, data, len);
  87. if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(asn1_int_oct), &atmp, &a))
  88. return 1;
  89. return 0;
  90. }
  91. int ASN1_TYPE_get_int_octetstring(const ASN1_TYPE *a, long *num,
  92. unsigned char *data, int max_len)
  93. {
  94. asn1_int_oct *atmp = NULL;
  95. int ret = -1;
  96. if ((a->type != V_ASN1_SEQUENCE) || (a->value.sequence == NULL)) {
  97. goto err;
  98. }
  99. atmp = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(asn1_int_oct), a);
  100. if (atmp == NULL)
  101. goto err;
  102. ret = asn1_type_get_int_oct(atmp->oct, atmp->num, num, data, max_len);
  103. if (ret == -1) {
  104. err:
  105. ERR_raise(ERR_LIB_ASN1, ASN1_R_DATA_IS_WRONG);
  106. }
  107. M_ASN1_free_of(atmp, asn1_int_oct);
  108. return ret;
  109. }
  110. typedef struct {
  111. ASN1_OCTET_STRING *oct;
  112. int32_t num;
  113. } asn1_oct_int;
  114. /*
  115. * Defined in RFC 5084 -
  116. * Section 2. "Content-Authenticated Encryption Algorithms"
  117. */
  118. ASN1_SEQUENCE(asn1_oct_int) = {
  119. ASN1_SIMPLE(asn1_oct_int, oct, ASN1_OCTET_STRING),
  120. ASN1_EMBED(asn1_oct_int, num, INT32)
  121. } static_ASN1_SEQUENCE_END(asn1_oct_int)
  122. DECLARE_ASN1_ITEM(asn1_oct_int)
  123. int ossl_asn1_type_set_octetstring_int(ASN1_TYPE *a, long num,
  124. unsigned char *data, int len)
  125. {
  126. asn1_oct_int atmp;
  127. ASN1_OCTET_STRING oct;
  128. atmp.num = num;
  129. atmp.oct = &oct;
  130. asn1_type_init_oct(&oct, data, len);
  131. if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(asn1_oct_int), &atmp, &a))
  132. return 1;
  133. return 0;
  134. }
  135. int ossl_asn1_type_get_octetstring_int(const ASN1_TYPE *a, long *num,
  136. unsigned char *data, int max_len)
  137. {
  138. asn1_oct_int *atmp = NULL;
  139. int ret = -1;
  140. if ((a->type != V_ASN1_SEQUENCE) || (a->value.sequence == NULL))
  141. goto err;
  142. atmp = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(asn1_oct_int), a);
  143. if (atmp == NULL)
  144. goto err;
  145. ret = asn1_type_get_int_oct(atmp->oct, atmp->num, num, data, max_len);
  146. if (ret == -1) {
  147. err:
  148. ERR_raise(ERR_LIB_ASN1, ASN1_R_DATA_IS_WRONG);
  149. }
  150. M_ASN1_free_of(atmp, asn1_oct_int);
  151. return ret;
  152. }