a_bitstr.c 5.4 KB

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