a_bitstr.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright 1995-2016 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 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 *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;
  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. ret->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear */
  100. ret->flags |= (ASN1_STRING_FLAG_BITS_LEFT | i); /* set */
  101. if (len-- > 1) { /* using one because of the bits left byte */
  102. s = OPENSSL_malloc((int)len);
  103. if (s == NULL) {
  104. i = ERR_R_MALLOC_FAILURE;
  105. goto err;
  106. }
  107. memcpy(s, p, (int)len);
  108. s[len - 1] &= (0xff << i);
  109. p += len;
  110. } else
  111. s = NULL;
  112. ret->length = (int)len;
  113. OPENSSL_free(ret->data);
  114. ret->data = s;
  115. ret->type = V_ASN1_BIT_STRING;
  116. if (a != NULL)
  117. (*a) = ret;
  118. *pp = p;
  119. return ret;
  120. err:
  121. ASN1err(ASN1_F_C2I_ASN1_BIT_STRING, i);
  122. if ((a == NULL) || (*a != ret))
  123. ASN1_BIT_STRING_free(ret);
  124. return NULL;
  125. }
  126. /*
  127. * These next 2 functions from Goetz Babin-Ebell.
  128. */
  129. int ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value)
  130. {
  131. int w, v, iv;
  132. unsigned char *c;
  133. w = n / 8;
  134. v = 1 << (7 - (n & 0x07));
  135. iv = ~v;
  136. if (!value)
  137. v = 0;
  138. if (a == NULL)
  139. return 0;
  140. a->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear, set on write */
  141. if ((a->length < (w + 1)) || (a->data == NULL)) {
  142. if (!value)
  143. return 1; /* Don't need to set */
  144. c = OPENSSL_clear_realloc(a->data, a->length, w + 1);
  145. if (c == NULL) {
  146. ASN1err(ASN1_F_ASN1_BIT_STRING_SET_BIT, ERR_R_MALLOC_FAILURE);
  147. return 0;
  148. }
  149. if (w + 1 - a->length > 0)
  150. memset(c + a->length, 0, w + 1 - a->length);
  151. a->data = c;
  152. a->length = w + 1;
  153. }
  154. a->data[w] = ((a->data[w]) & iv) | v;
  155. while ((a->length > 0) && (a->data[a->length - 1] == 0))
  156. a->length--;
  157. return 1;
  158. }
  159. int ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING *a, int n)
  160. {
  161. int w, v;
  162. w = n / 8;
  163. v = 1 << (7 - (n & 0x07));
  164. if ((a == NULL) || (a->length < (w + 1)) || (a->data == NULL))
  165. return 0;
  166. return ((a->data[w] & v) != 0);
  167. }
  168. /*
  169. * Checks if the given bit string contains only bits specified by
  170. * the flags vector. Returns 0 if there is at least one bit set in 'a'
  171. * which is not specified in 'flags', 1 otherwise.
  172. * 'len' is the length of 'flags'.
  173. */
  174. int ASN1_BIT_STRING_check(const ASN1_BIT_STRING *a,
  175. const unsigned char *flags, int flags_len)
  176. {
  177. int i, ok;
  178. /* Check if there is one bit set at all. */
  179. if (!a || !a->data)
  180. return 1;
  181. /*
  182. * Check each byte of the internal representation of the bit string.
  183. */
  184. ok = 1;
  185. for (i = 0; i < a->length && ok; ++i) {
  186. unsigned char mask = i < flags_len ? ~flags[i] : 0xff;
  187. /* We are done if there is an unneeded bit set. */
  188. ok = (a->data[i] & mask) == 0;
  189. }
  190. return ok;
  191. }