a_bitstr.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright 1995-2023 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 <limits.h>
  10. #include <stdio.h>
  11. #include "internal/cryptlib.h"
  12. #include <openssl/asn1.h>
  13. #include "asn1_local.h"
  14. int ASN1_BIT_STRING_set(ASN1_BIT_STRING *x, unsigned char *d, int len)
  15. {
  16. return ASN1_STRING_set(x, d, len);
  17. }
  18. int ossl_i2c_ASN1_BIT_STRING(ASN1_BIT_STRING *a, unsigned char **pp)
  19. {
  20. int ret, j, bits, len;
  21. unsigned char *p, *d;
  22. if (a == NULL)
  23. return 0;
  24. len = a->length;
  25. if (len > 0) {
  26. if (a->flags & ASN1_STRING_FLAG_BITS_LEFT) {
  27. bits = (int)a->flags & 0x07;
  28. } else {
  29. for (; len > 0; len--) {
  30. if (a->data[len - 1])
  31. break;
  32. }
  33. j = a->data[len - 1];
  34. if (j & 0x01)
  35. bits = 0;
  36. else if (j & 0x02)
  37. bits = 1;
  38. else if (j & 0x04)
  39. bits = 2;
  40. else if (j & 0x08)
  41. bits = 3;
  42. else if (j & 0x10)
  43. bits = 4;
  44. else if (j & 0x20)
  45. bits = 5;
  46. else if (j & 0x40)
  47. bits = 6;
  48. else if (j & 0x80)
  49. bits = 7;
  50. else
  51. bits = 0; /* should not happen */
  52. }
  53. } else
  54. bits = 0;
  55. ret = 1 + len;
  56. if (pp == NULL)
  57. return ret;
  58. p = *pp;
  59. *(p++) = (unsigned char)bits;
  60. d = a->data;
  61. if (len > 0) {
  62. memcpy(p, d, len);
  63. p += len;
  64. p[-1] &= (0xff << bits);
  65. }
  66. *pp = p;
  67. return ret;
  68. }
  69. ASN1_BIT_STRING *ossl_c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a,
  70. const unsigned char **pp, long len)
  71. {
  72. ASN1_BIT_STRING *ret = NULL;
  73. const unsigned char *p;
  74. unsigned char *s;
  75. int i = 0;
  76. if (len < 1) {
  77. i = ASN1_R_STRING_TOO_SHORT;
  78. goto err;
  79. }
  80. if (len > INT_MAX) {
  81. i = ASN1_R_STRING_TOO_LONG;
  82. goto err;
  83. }
  84. if ((a == NULL) || ((*a) == NULL)) {
  85. if ((ret = ASN1_BIT_STRING_new()) == NULL)
  86. return NULL;
  87. } else
  88. ret = (*a);
  89. p = *pp;
  90. i = *(p++);
  91. if (i > 7) {
  92. i = ASN1_R_INVALID_BIT_STRING_BITS_LEFT;
  93. goto err;
  94. }
  95. /*
  96. * We do this to preserve the settings. If we modify the settings, via
  97. * the _set_bit function, we will recalculate on output
  98. */
  99. ossl_asn1_string_set_bits_left(ret, i);
  100. if (len-- > 1) { /* using one because of the bits left byte */
  101. s = OPENSSL_malloc((int)len);
  102. if (s == NULL) {
  103. goto err;
  104. }
  105. memcpy(s, p, (int)len);
  106. s[len - 1] &= (0xff << i);
  107. p += len;
  108. } else
  109. s = NULL;
  110. ASN1_STRING_set0(ret, s, (int)len);
  111. ret->type = V_ASN1_BIT_STRING;
  112. if (a != NULL)
  113. (*a) = ret;
  114. *pp = p;
  115. return ret;
  116. err:
  117. if (i != 0)
  118. ERR_raise(ERR_LIB_ASN1, i);
  119. if ((a == NULL) || (*a != ret))
  120. ASN1_BIT_STRING_free(ret);
  121. return NULL;
  122. }
  123. /*
  124. * These next 2 functions from Goetz Babin-Ebell.
  125. */
  126. int ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value)
  127. {
  128. int w, v, iv;
  129. unsigned char *c;
  130. if (n < 0)
  131. return 0;
  132. w = n / 8;
  133. v = 1 << (7 - (n & 0x07));
  134. iv = ~v;
  135. if (!value)
  136. v = 0;
  137. if (a == NULL)
  138. return 0;
  139. a->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear, set on write */
  140. if ((a->length < (w + 1)) || (a->data == NULL)) {
  141. if (!value)
  142. return 1; /* Don't need to set */
  143. c = OPENSSL_clear_realloc(a->data, a->length, w + 1);
  144. if (c == NULL)
  145. return 0;
  146. if (w + 1 - a->length > 0)
  147. memset(c + a->length, 0, w + 1 - a->length);
  148. a->data = c;
  149. a->length = w + 1;
  150. }
  151. a->data[w] = ((a->data[w]) & iv) | v;
  152. while ((a->length > 0) && (a->data[a->length - 1] == 0))
  153. a->length--;
  154. return 1;
  155. }
  156. int ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING *a, int n)
  157. {
  158. int w, v;
  159. if (n < 0)
  160. return 0;
  161. w = n / 8;
  162. v = 1 << (7 - (n & 0x07));
  163. if ((a == NULL) || (a->length < (w + 1)) || (a->data == NULL))
  164. return 0;
  165. return ((a->data[w] & v) != 0);
  166. }
  167. /*
  168. * Checks if the given bit string contains only bits specified by
  169. * the flags vector. Returns 0 if there is at least one bit set in 'a'
  170. * which is not specified in 'flags', 1 otherwise.
  171. * 'len' is the length of 'flags'.
  172. */
  173. int ASN1_BIT_STRING_check(const ASN1_BIT_STRING *a,
  174. const unsigned char *flags, int flags_len)
  175. {
  176. int i, ok;
  177. /* Check if there is one bit set at all. */
  178. if (!a || !a->data)
  179. return 1;
  180. /*
  181. * Check each byte of the internal representation of the bit string.
  182. */
  183. ok = 1;
  184. for (i = 0; i < a->length && ok; ++i) {
  185. unsigned char mask = i < flags_len ? ~flags[i] : 0xff;
  186. /* We are done if there is an unneeded bit set. */
  187. ok = (a->data[i] & mask) == 0;
  188. }
  189. return ok;
  190. }