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
11 #include "internal/cryptlib.h"
12 #include <openssl/asn1.h>
13 #include "asn1_locl.h"
15 int ASN1_BIT_STRING_set(ASN1_BIT_STRING *x, unsigned char *d, int len)
17 return ASN1_STRING_set(x, d, len);
20 int i2c_ASN1_BIT_STRING(ASN1_BIT_STRING *a, unsigned char **pp)
22 int ret, j, bits, len;
31 if (a->flags & ASN1_STRING_FLAG_BITS_LEFT) {
32 bits = (int)a->flags & 0x07;
34 for (; len > 0; len--) {
56 bits = 0; /* should not happen */
67 *(p++) = (unsigned char)bits;
72 p[-1] &= (0xff << bits);
77 ASN1_BIT_STRING *c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a,
78 const unsigned char **pp, long len)
80 ASN1_BIT_STRING *ret = NULL;
81 const unsigned char *p;
86 i = ASN1_R_STRING_TOO_SHORT;
90 if ((a == NULL) || ((*a) == NULL)) {
91 if ((ret = ASN1_BIT_STRING_new()) == NULL)
99 i = ASN1_R_INVALID_BIT_STRING_BITS_LEFT;
103 * We do this to preserve the settings. If we modify the settings, via
104 * the _set_bit function, we will recalculate on output
106 ret->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear */
107 ret->flags |= (ASN1_STRING_FLAG_BITS_LEFT | i); /* set */
109 if (len-- > 1) { /* using one because of the bits left byte */
110 s = OPENSSL_malloc((int)len);
112 i = ERR_R_MALLOC_FAILURE;
115 memcpy(s, p, (int)len);
116 s[len - 1] &= (0xff << i);
121 ret->length = (int)len;
122 OPENSSL_free(ret->data);
124 ret->type = V_ASN1_BIT_STRING;
130 ASN1err(ASN1_F_C2I_ASN1_BIT_STRING, i);
131 if ((a == NULL) || (*a != ret))
132 ASN1_BIT_STRING_free(ret);
137 * These next 2 functions from Goetz Babin-Ebell <babinebell@trustcenter.de>
139 int ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value)
145 v = 1 << (7 - (n & 0x07));
153 a->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear, set on write */
155 if ((a->length < (w + 1)) || (a->data == NULL)) {
157 return (1); /* Don't need to set */
158 c = OPENSSL_clear_realloc(a->data, a->length, w + 1);
160 ASN1err(ASN1_F_ASN1_BIT_STRING_SET_BIT, ERR_R_MALLOC_FAILURE);
163 if (w + 1 - a->length > 0)
164 memset(c + a->length, 0, w + 1 - a->length);
168 a->data[w] = ((a->data[w]) & iv) | v;
169 while ((a->length > 0) && (a->data[a->length - 1] == 0))
174 int ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING *a, int n)
179 v = 1 << (7 - (n & 0x07));
180 if ((a == NULL) || (a->length < (w + 1)) || (a->data == NULL))
182 return ((a->data[w] & v) != 0);
186 * Checks if the given bit string contains only bits specified by
187 * the flags vector. Returns 0 if there is at least one bit set in 'a'
188 * which is not specified in 'flags', 1 otherwise.
189 * 'len' is the length of 'flags'.
191 int ASN1_BIT_STRING_check(const ASN1_BIT_STRING *a,
192 const unsigned char *flags, int flags_len)
195 /* Check if there is one bit set at all. */
200 * Check each byte of the internal representation of the bit string.
203 for (i = 0; i < a->length && ok; ++i) {
204 unsigned char mask = i < flags_len ? ~flags[i] : 0xff;
205 /* We are done if there is an unneeded bit set. */
206 ok = (a->data[i] & mask) == 0;