xref: /freebsd/crypto/openssl/crypto/asn1/a_bitstr.c (revision 0d0c8621fd181e507f0fb50ffcca606faf66a8c2)
1 /*
2  * Copyright 1995-2023 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 
10 #include <limits.h>
11 #include <stdio.h>
12 #include "internal/cryptlib.h"
13 #include <openssl/asn1.h>
14 #include "asn1_local.h"
15 
ASN1_BIT_STRING_set(ASN1_BIT_STRING * x,unsigned char * d,int len)16 int ASN1_BIT_STRING_set(ASN1_BIT_STRING *x, unsigned char *d, int len)
17 {
18     return ASN1_STRING_set(x, d, len);
19 }
20 
ossl_i2c_ASN1_BIT_STRING(ASN1_BIT_STRING * a,unsigned char ** pp)21 int ossl_i2c_ASN1_BIT_STRING(ASN1_BIT_STRING *a, unsigned char **pp)
22 {
23     int ret, j, bits, len;
24     unsigned char *p, *d;
25 
26     if (a == NULL)
27         return 0;
28 
29     len = a->length;
30 
31     if (len > 0) {
32         if (a->flags & ASN1_STRING_FLAG_BITS_LEFT) {
33             bits = (int)a->flags & 0x07;
34         } else {
35             for (; len > 0; len--) {
36                 if (a->data[len - 1])
37                     break;
38             }
39 
40             if (len == 0) {
41                 bits = 0;
42             } else {
43                 j = a->data[len - 1];
44                 if (j & 0x01)
45                     bits = 0;
46                 else if (j & 0x02)
47                     bits = 1;
48                 else if (j & 0x04)
49                     bits = 2;
50                 else if (j & 0x08)
51                     bits = 3;
52                 else if (j & 0x10)
53                     bits = 4;
54                 else if (j & 0x20)
55                     bits = 5;
56                 else if (j & 0x40)
57                     bits = 6;
58                 else if (j & 0x80)
59                     bits = 7;
60                 else
61                     bits = 0;       /* should not happen */
62             }
63         }
64     } else
65         bits = 0;
66 
67     ret = 1 + len;
68     if (pp == NULL)
69         return ret;
70 
71     p = *pp;
72 
73     *(p++) = (unsigned char)bits;
74     d = a->data;
75     if (len > 0) {
76         memcpy(p, d, len);
77         p += len;
78         p[-1] &= (0xff << bits);
79     }
80     *pp = p;
81     return ret;
82 }
83 
ossl_c2i_ASN1_BIT_STRING(ASN1_BIT_STRING ** a,const unsigned char ** pp,long len)84 ASN1_BIT_STRING *ossl_c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a,
85                                           const unsigned char **pp, long len)
86 {
87     ASN1_BIT_STRING *ret = NULL;
88     const unsigned char *p;
89     unsigned char *s;
90     int i;
91 
92     if (len < 1) {
93         i = ASN1_R_STRING_TOO_SHORT;
94         goto err;
95     }
96 
97     if (len > INT_MAX) {
98         i = ASN1_R_STRING_TOO_LONG;
99         goto err;
100     }
101 
102     if ((a == NULL) || ((*a) == NULL)) {
103         if ((ret = ASN1_BIT_STRING_new()) == NULL)
104             return NULL;
105     } else
106         ret = (*a);
107 
108     p = *pp;
109     i = *(p++);
110     if (i > 7) {
111         i = ASN1_R_INVALID_BIT_STRING_BITS_LEFT;
112         goto err;
113     }
114     /*
115      * We do this to preserve the settings.  If we modify the settings, via
116      * the _set_bit function, we will recalculate on output
117      */
118     ret->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear */
119     ret->flags |= (ASN1_STRING_FLAG_BITS_LEFT | i); /* set */
120 
121     if (len-- > 1) {            /* using one because of the bits left byte */
122         s = OPENSSL_malloc((int)len);
123         if (s == NULL) {
124             i = ERR_R_MALLOC_FAILURE;
125             goto err;
126         }
127         memcpy(s, p, (int)len);
128         s[len - 1] &= (0xff << i);
129         p += len;
130     } else
131         s = NULL;
132 
133     ret->length = (int)len;
134     OPENSSL_free(ret->data);
135     ret->data = s;
136     ret->type = V_ASN1_BIT_STRING;
137     if (a != NULL)
138         (*a) = ret;
139     *pp = p;
140     return ret;
141  err:
142     ERR_raise(ERR_LIB_ASN1, i);
143     if ((a == NULL) || (*a != ret))
144         ASN1_BIT_STRING_free(ret);
145     return NULL;
146 }
147 
148 /*
149  * These next 2 functions from Goetz Babin-Ebell.
150  */
ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING * a,int n,int value)151 int ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value)
152 {
153     int w, v, iv;
154     unsigned char *c;
155 
156     if (n < 0)
157         return 0;
158 
159     w = n / 8;
160     v = 1 << (7 - (n & 0x07));
161     iv = ~v;
162     if (!value)
163         v = 0;
164 
165     if (a == NULL)
166         return 0;
167 
168     a->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear, set on write */
169 
170     if ((a->length < (w + 1)) || (a->data == NULL)) {
171         if (!value)
172             return 1;         /* Don't need to set */
173         c = OPENSSL_clear_realloc(a->data, a->length, w + 1);
174         if (c == NULL) {
175             ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
176             return 0;
177         }
178         if (w + 1 - a->length > 0)
179             memset(c + a->length, 0, w + 1 - a->length);
180         a->data = c;
181         a->length = w + 1;
182     }
183     a->data[w] = ((a->data[w]) & iv) | v;
184     while ((a->length > 0) && (a->data[a->length - 1] == 0))
185         a->length--;
186     return 1;
187 }
188 
ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING * a,int n)189 int ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING *a, int n)
190 {
191     int w, v;
192 
193     if (n < 0)
194         return 0;
195 
196     w = n / 8;
197     v = 1 << (7 - (n & 0x07));
198     if ((a == NULL) || (a->length < (w + 1)) || (a->data == NULL))
199         return 0;
200     return ((a->data[w] & v) != 0);
201 }
202 
203 /*
204  * Checks if the given bit string contains only bits specified by
205  * the flags vector. Returns 0 if there is at least one bit set in 'a'
206  * which is not specified in 'flags', 1 otherwise.
207  * 'len' is the length of 'flags'.
208  */
ASN1_BIT_STRING_check(const ASN1_BIT_STRING * a,const unsigned char * flags,int flags_len)209 int ASN1_BIT_STRING_check(const ASN1_BIT_STRING *a,
210                           const unsigned char *flags, int flags_len)
211 {
212     int i, ok;
213     /* Check if there is one bit set at all. */
214     if (!a || !a->data)
215         return 1;
216 
217     /*
218      * Check each byte of the internal representation of the bit string.
219      */
220     ok = 1;
221     for (i = 0; i < a->length && ok; ++i) {
222         unsigned char mask = i < flags_len ? ~flags[i] : 0xff;
223         /* We are done if there is an unneeded bit set. */
224         ok = (a->data[i] & mask) == 0;
225     }
226     return ok;
227 }
228