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