2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
12 #include "internal/cryptlib.h"
13 #include <openssl/asn1.h>
14 #include "asn1_locl.h"
16 int ASN1_BIT_STRING_set(ASN1_BIT_STRING *x, unsigned char *d, int len)
18 return ASN1_STRING_set(x, d, len);
21 int i2c_ASN1_BIT_STRING(ASN1_BIT_STRING *a, unsigned char **pp)
23 int ret, j, bits, len;
32 if (a->flags & ASN1_STRING_FLAG_BITS_LEFT) {
33 bits = (int)a->flags & 0x07;
35 for (; len > 0; len--) {
57 bits = 0; /* should not happen */
68 *(p++) = (unsigned char)bits;
73 p[-1] &= (0xff << bits);
79 ASN1_BIT_STRING *c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a,
80 const unsigned char **pp, long len)
82 ASN1_BIT_STRING *ret = NULL;
83 const unsigned char *p;
88 i = ASN1_R_STRING_TOO_SHORT;
93 i = ASN1_R_STRING_TOO_LONG;
97 if ((a == NULL) || ((*a) == NULL)) {
98 if ((ret = ASN1_BIT_STRING_new()) == NULL)
106 i = ASN1_R_INVALID_BIT_STRING_BITS_LEFT;
110 * We do this to preserve the settings. If we modify the settings, via
111 * the _set_bit function, we will recalculate on output
113 ret->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear */
114 ret->flags |= (ASN1_STRING_FLAG_BITS_LEFT | i); /* set */
116 if (len-- > 1) { /* using one because of the bits left byte */
117 s = OPENSSL_malloc((int)len);
119 i = ERR_R_MALLOC_FAILURE;
122 memcpy(s, p, (int)len);
123 s[len - 1] &= (0xff << i);
128 ret->length = (int)len;
129 OPENSSL_free(ret->data);
131 ret->type = V_ASN1_BIT_STRING;
137 ASN1err(ASN1_F_C2I_ASN1_BIT_STRING, i);
138 if ((a == NULL) || (*a != ret))
139 ASN1_BIT_STRING_free(ret);
144 * These next 2 functions from Goetz Babin-Ebell.
146 int ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value)
152 v = 1 << (7 - (n & 0x07));
160 a->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear, set on write */
162 if ((a->length < (w + 1)) || (a->data == NULL)) {
164 return 1; /* Don't need to set */
165 c = OPENSSL_clear_realloc(a->data, a->length, w + 1);
167 ASN1err(ASN1_F_ASN1_BIT_STRING_SET_BIT, ERR_R_MALLOC_FAILURE);
170 if (w + 1 - a->length > 0)
171 memset(c + a->length, 0, w + 1 - a->length);
175 a->data[w] = ((a->data[w]) & iv) | v;
176 while ((a->length > 0) && (a->data[a->length - 1] == 0))
181 int ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING *a, int n)
186 v = 1 << (7 - (n & 0x07));
187 if ((a == NULL) || (a->length < (w + 1)) || (a->data == NULL))
189 return ((a->data[w] & v) != 0);
193 * Checks if the given bit string contains only bits specified by
194 * the flags vector. Returns 0 if there is at least one bit set in 'a'
195 * which is not specified in 'flags', 1 otherwise.
196 * 'len' is the length of 'flags'.
198 int ASN1_BIT_STRING_check(const ASN1_BIT_STRING *a,
199 const unsigned char *flags, int flags_len)
202 /* Check if there is one bit set at all. */
207 * Check each byte of the internal representation of the bit string.
210 for (i = 0; i < a->length && ok; ++i) {
211 unsigned char mask = i < flags_len ? ~flags[i] : 0xff;
212 /* We are done if there is an unneeded bit set. */
213 ok = (a->data[i] & mask) == 0;