a_bitstr.c
Go to the documentation of this file.
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to. The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  * notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  * notice, this list of conditions and the following disclaimer in the
29  * documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  * must display the following acknowledgement:
32  * "This product includes cryptographic software written by
33  * Eric Young (eay@cryptsoft.com)"
34  * The word 'cryptographic' can be left out if the rouines from the library
35  * being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  * the apps directory (application code) you must include an acknowledgement:
38  * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed. i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.] */
56 
57 #include <openssl/asn1.h>
58 
59 #include <limits.h>
60 #include <string.h>
61 
62 #include <openssl/err.h>
63 #include <openssl/mem.h>
64 
65 #include "../internal.h"
66 #include "internal.h"
67 
68 
69 int ASN1_BIT_STRING_set(ASN1_BIT_STRING *x, const unsigned char *d, int len)
70 {
71  return ASN1_STRING_set(x, d, len);
72 }
73 
75  uint8_t *out_padding_bits) {
76  int len = str->length;
77  if (str->flags & ASN1_STRING_FLAG_BITS_LEFT) {
78  // If the string is already empty, it cannot have padding bits.
79  *out_padding_bits = len == 0 ? 0 : str->flags & 0x07;
80  return len;
81  }
82 
83  // TODO(https://crbug.com/boringssl/447): If we move this logic to
84  // |ASN1_BIT_STRING_set_bit|, can we remove this representation?
85  while (len > 0 && str->data[len - 1] == 0) {
86  len--;
87  }
88  uint8_t padding_bits = 0;
89  if (len > 0) {
90  uint8_t last = str->data[len - 1];
91  assert(last != 0);
92  for (; padding_bits < 7; padding_bits++) {
93  if (last & (1 << padding_bits)) {
94  break;
95  }
96  }
97  }
98  *out_padding_bits = padding_bits;
99  return len;
100 }
101 
103  uint8_t padding_bits;
104  int len = asn1_bit_string_length(str, &padding_bits);
105  if (padding_bits != 0) {
106  return 0;
107  }
108  *out = len;
109  return 1;
110 }
111 
112 int i2c_ASN1_BIT_STRING(const ASN1_BIT_STRING *a, unsigned char **pp)
113 {
114  if (a == NULL) {
115  return 0;
116  }
117 
118  uint8_t bits;
119  int len = asn1_bit_string_length(a, &bits);
120  int ret = 1 + len;
121  if (pp == NULL) {
122  return ret;
123  }
124 
125  uint8_t *p = *pp;
126  *(p++) = bits;
127  OPENSSL_memcpy(p, a->data, len);
128  if (len > 0) {
129  p[len - 1] &= (0xff << bits);
130  }
131  p += len;
132  *pp = p;
133  return (ret);
134 }
135 
137  const unsigned char **pp, long len)
138 {
139  ASN1_BIT_STRING *ret = NULL;
140  const unsigned char *p;
141  unsigned char *s;
142  int padding;
143 
144  if (len < 1) {
146  goto err;
147  }
148 
149  if (len > INT_MAX) {
151  goto err;
152  }
153 
154  if ((a == NULL) || ((*a) == NULL)) {
155  if ((ret = ASN1_BIT_STRING_new()) == NULL)
156  return (NULL);
157  } else
158  ret = (*a);
159 
160  p = *pp;
161  padding = *(p++);
162  len--;
163  if (padding > 7) {
165  goto err;
166  }
167 
168  /* Unused bits in a BIT STRING must be zero. */
169  uint8_t padding_mask = (1 << padding) - 1;
170  if (padding != 0 &&
171  (len < 1 || (p[len - 1] & padding_mask) != 0)) {
173  goto err;
174  }
175 
176  /*
177  * We do this to preserve the settings. If we modify the settings, via
178  * the _set_bit function, we will recalculate on output
179  */
180  ret->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear */
181  ret->flags |= (ASN1_STRING_FLAG_BITS_LEFT | padding); /* set */
182 
183  if (len > 0) {
184  s = OPENSSL_memdup(p, len);
185  if (s == NULL) {
187  goto err;
188  }
189  p += len;
190  } else {
191  s = NULL;
192  }
193 
194  ret->length = (int)len;
195  OPENSSL_free(ret->data);
196  ret->data = s;
197  ret->type = V_ASN1_BIT_STRING;
198  if (a != NULL)
199  (*a) = ret;
200  *pp = p;
201  return (ret);
202  err:
203  if ((ret != NULL) && ((a == NULL) || (*a != ret)))
205  return (NULL);
206 }
207 
208 /*
209  * These next 2 functions from Goetz Babin-Ebell <babinebell@trustcenter.de>
210  */
212 {
213  int w, v, iv;
214  unsigned char *c;
215 
216  w = n / 8;
217  v = 1 << (7 - (n & 0x07));
218  iv = ~v;
219  if (!value)
220  v = 0;
221 
222  if (a == NULL)
223  return 0;
224 
225  a->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear, set on write */
226 
227  if ((a->length < (w + 1)) || (a->data == NULL)) {
228  if (!value)
229  return (1); /* Don't need to set */
230  if (a->data == NULL)
231  c = (unsigned char *)OPENSSL_malloc(w + 1);
232  else
233  c = (unsigned char *)OPENSSL_realloc(a->data, w + 1);
234  if (c == NULL) {
236  return 0;
237  }
238  if (w + 1 - a->length > 0)
239  OPENSSL_memset(c + a->length, 0, w + 1 - a->length);
240  a->data = c;
241  a->length = w + 1;
242  }
243  a->data[w] = ((a->data[w]) & iv) | v;
244  while ((a->length > 0) && (a->data[a->length - 1] == 0))
245  a->length--;
246  return (1);
247 }
248 
250 {
251  int w, v;
252 
253  w = n / 8;
254  v = 1 << (7 - (n & 0x07));
255  if ((a == NULL) || (a->length < (w + 1)) || (a->data == NULL))
256  return (0);
257  return ((a->data[w] & v) != 0);
258 }
259 
260 /*
261  * Checks if the given bit string contains only bits specified by
262  * the flags vector. Returns 0 if there is at least one bit set in 'a'
263  * which is not specified in 'flags', 1 otherwise.
264  * 'len' is the length of 'flags'.
265  */
267  const unsigned char *flags, int flags_len)
268 {
269  int i, ok;
270  /* Check if there is one bit set at all. */
271  if (!a || !a->data)
272  return 1;
273 
274  /*
275  * Check each byte of the internal representation of the bit string.
276  */
277  ok = 1;
278  for (i = 0; i < a->length && ok; ++i) {
279  unsigned char mask = i < flags_len ? ~flags[i] : 0xff;
280  /* We are done if there is an unneeded bit set. */
281  ok = (a->data[i] & mask) == 0;
282  }
283  return ok;
284 }
xds_interop_client.str
str
Definition: xds_interop_client.py:487
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
OPENSSL_PUT_ERROR
#define OPENSSL_PUT_ERROR(library, reason)
Definition: err.h:423
string.h
OPENSSL_realloc
#define OPENSSL_realloc
Definition: boringssl_prefix_symbols.h:1889
error_ref_leak.err
err
Definition: error_ref_leak.py:35
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
ASN1_R_INVALID_BIT_STRING_BITS_LEFT
#define ASN1_R_INVALID_BIT_STRING_BITS_LEFT
Definition: asn1.h:1984
xds_manager.p
p
Definition: xds_manager.py:60
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
OPENSSL_memset
static void * OPENSSL_memset(void *dst, int c, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:835
ASN1_R_STRING_TOO_LONG
#define ASN1_R_STRING_TOO_LONG
Definition: asn1.h:2016
ASN1_BIT_STRING_new
OPENSSL_EXPORT ASN1_BIT_STRING * ASN1_BIT_STRING_new(void)
c2i_ASN1_BIT_STRING
ASN1_BIT_STRING * c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a, const unsigned char **pp, long len)
Definition: a_bitstr.c:136
OPENSSL_malloc
#define OPENSSL_malloc
Definition: boringssl_prefix_symbols.h:1885
ASN1_STRING_set
#define ASN1_STRING_set
Definition: boringssl_prefix_symbols.h:688
c
void c(T a)
Definition: miscompile_with_no_unique_address_test.cc:40
internal.h
xds_interop_client.int
int
Definition: xds_interop_client.py:113
ASN1_BIT_STRING_check
int ASN1_BIT_STRING_check(const ASN1_BIT_STRING *a, const unsigned char *flags, int flags_len)
Definition: a_bitstr.c:266
setup.v
v
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
i2c_ASN1_BIT_STRING
int i2c_ASN1_BIT_STRING(const ASN1_BIT_STRING *a, unsigned char **pp)
Definition: a_bitstr.c:112
ASN1_BIT_STRING_set_bit
int ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value)
Definition: a_bitstr.c:211
OPENSSL_memcpy
static void * OPENSSL_memcpy(void *dst, const void *src, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:819
bits
OPENSSL_EXPORT ASN1_BIT_STRING * bits
Definition: x509v3.h:482
err.h
ASN1_BIT_STRING_get_bit
int ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING *a, int n)
Definition: a_bitstr.c:249
ASN1_STRING_FLAG_BITS_LEFT
#define ASN1_STRING_FLAG_BITS_LEFT
Definition: asn1.h:554
V_ASN1_BIT_STRING
#define V_ASN1_BIT_STRING
Definition: asn1.h:127
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
d
static const fe d
Definition: curve25519_tables.h:19
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
ASN1_BIT_STRING_num_bytes
int ASN1_BIT_STRING_num_bytes(const ASN1_BIT_STRING *str, size_t *out)
Definition: a_bitstr.c:102
value
const char * value
Definition: hpack_parser_table.cc:165
pp
const uint8_t ** pp
Definition: ssl_x509.cc:1020
absl::flags_internal
Definition: abseil-cpp/absl/flags/commandlineflag.h:40
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
OPENSSL_memdup
#define OPENSSL_memdup
Definition: boringssl_prefix_symbols.h:1887
ok
bool ok
Definition: async_end2end_test.cc:197
ASN1_R_STRING_TOO_SHORT
#define ASN1_R_STRING_TOO_SHORT
Definition: asn1.h:2017
asn1_bit_string_length
int asn1_bit_string_length(const ASN1_BIT_STRING *str, uint8_t *out_padding_bits)
Definition: a_bitstr.c:74
ASN1_BIT_STRING_free
OPENSSL_EXPORT void ASN1_BIT_STRING_free(ASN1_BIT_STRING *str)
mem.h
ASN1_R_INVALID_BIT_STRING_PADDING
#define ASN1_R_INVALID_BIT_STRING_PADDING
Definition: asn1.h:2037
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
ASN1_BIT_STRING_set
int ASN1_BIT_STRING_set(ASN1_BIT_STRING *x, const unsigned char *d, int len)
Definition: a_bitstr.c:69
OPENSSL_free
#define OPENSSL_free
Definition: boringssl_prefix_symbols.h:1869
asn1_string_st
Definition: asn1.h:543
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
asn1.h
ERR_R_MALLOC_FAILURE
#define ERR_R_MALLOC_FAILURE
Definition: err.h:371


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:28