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
);
78 ASN1_BIT_STRING
*c2i_ASN1_BIT_STRING(ASN1_BIT_STRING
**a
,
79 const unsigned char **pp
, long len
)
81 ASN1_BIT_STRING
*ret
= NULL
;
82 const unsigned char *p
;
87 i
= ASN1_R_STRING_TOO_SHORT
;
91 if ((a
== NULL
) || ((*a
) == NULL
)) {
92 if ((ret
= ASN1_BIT_STRING_new()) == NULL
)
100 i
= ASN1_R_INVALID_BIT_STRING_BITS_LEFT
;
104 * We do this to preserve the settings. If we modify the settings, via
105 * the _set_bit function, we will recalculate on output
107 ret
->flags
&= ~(ASN1_STRING_FLAG_BITS_LEFT
| 0x07); /* clear */
108 ret
->flags
|= (ASN1_STRING_FLAG_BITS_LEFT
| i
); /* set */
110 if (len
-- > 1) { /* using one because of the bits left byte */
111 s
= OPENSSL_malloc((int)len
);
113 i
= ERR_R_MALLOC_FAILURE
;
116 memcpy(s
, p
, (int)len
);
117 s
[len
- 1] &= (0xff << i
);
122 ret
->length
= (int)len
;
123 OPENSSL_free(ret
->data
);
125 ret
->type
= V_ASN1_BIT_STRING
;
131 ASN1err(ASN1_F_C2I_ASN1_BIT_STRING
, i
);
132 if ((a
== NULL
) || (*a
!= ret
))
133 ASN1_BIT_STRING_free(ret
);
138 * These next 2 functions from Goetz Babin-Ebell <babinebell@trustcenter.de>
140 int ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING
*a
, int n
, int value
)
146 v
= 1 << (7 - (n
& 0x07));
154 a
->flags
&= ~(ASN1_STRING_FLAG_BITS_LEFT
| 0x07); /* clear, set on write */
156 if ((a
->length
< (w
+ 1)) || (a
->data
== NULL
)) {
158 return (1); /* Don't need to set */
159 c
= OPENSSL_clear_realloc(a
->data
, a
->length
, w
+ 1);
161 ASN1err(ASN1_F_ASN1_BIT_STRING_SET_BIT
, ERR_R_MALLOC_FAILURE
);
164 if (w
+ 1 - a
->length
> 0)
165 memset(c
+ a
->length
, 0, w
+ 1 - a
->length
);
169 a
->data
[w
] = ((a
->data
[w
]) & iv
) | v
;
170 while ((a
->length
> 0) && (a
->data
[a
->length
- 1] == 0))
175 int ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING
*a
, int n
)
180 v
= 1 << (7 - (n
& 0x07));
181 if ((a
== NULL
) || (a
->length
< (w
+ 1)) || (a
->data
== NULL
))
183 return ((a
->data
[w
] & v
) != 0);
187 * Checks if the given bit string contains only bits specified by
188 * the flags vector. Returns 0 if there is at least one bit set in 'a'
189 * which is not specified in 'flags', 1 otherwise.
190 * 'len' is the length of 'flags'.
192 int ASN1_BIT_STRING_check(const ASN1_BIT_STRING
*a
,
193 const unsigned char *flags
, int flags_len
)
196 /* Check if there is one bit set at all. */
201 * Check each byte of the internal representation of the bit string.
204 for (i
= 0; i
< a
->length
&& ok
; ++i
) {
205 unsigned char mask
= i
< flags_len
? ~flags
[i
] : 0xff;
206 /* We are done if there is an unneeded bit set. */
207 ok
= (a
->data
[i
] & mask
) == 0;