Copyright consolidation 08/10
[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     memcpy(p, d, len);
70     p += len;
71     if (len > 0)
72         p[-1] &= (0xff << bits);
73     *pp = p;
74     return (ret);
75 }
76
77 ASN1_BIT_STRING *c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a,
78                                      const unsigned char **pp, long len)
79 {
80     ASN1_BIT_STRING *ret = NULL;
81     const unsigned char *p;
82     unsigned char *s;
83     int i;
84
85     if (len < 1) {
86         i = ASN1_R_STRING_TOO_SHORT;
87         goto err;
88     }
89
90     if ((a == NULL) || ((*a) == NULL)) {
91         if ((ret = ASN1_BIT_STRING_new()) == NULL)
92             return (NULL);
93     } else
94         ret = (*a);
95
96     p = *pp;
97     i = *(p++);
98     if (i > 7) {
99         i = ASN1_R_INVALID_BIT_STRING_BITS_LEFT;
100         goto err;
101     }
102     /*
103      * We do this to preserve the settings.  If we modify the settings, via
104      * the _set_bit function, we will recalculate on output
105      */
106     ret->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear */
107     ret->flags |= (ASN1_STRING_FLAG_BITS_LEFT | i); /* set */
108
109     if (len-- > 1) {            /* using one because of the bits left byte */
110         s = OPENSSL_malloc((int)len);
111         if (s == NULL) {
112             i = ERR_R_MALLOC_FAILURE;
113             goto err;
114         }
115         memcpy(s, p, (int)len);
116         s[len - 1] &= (0xff << i);
117         p += len;
118     } else
119         s = NULL;
120
121     ret->length = (int)len;
122     OPENSSL_free(ret->data);
123     ret->data = s;
124     ret->type = V_ASN1_BIT_STRING;
125     if (a != NULL)
126         (*a) = ret;
127     *pp = p;
128     return (ret);
129  err:
130     ASN1err(ASN1_F_C2I_ASN1_BIT_STRING, i);
131     if ((a == NULL) || (*a != ret))
132         ASN1_BIT_STRING_free(ret);
133     return (NULL);
134 }
135
136 /*
137  * These next 2 functions from Goetz Babin-Ebell <babinebell@trustcenter.de>
138  */
139 int ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value)
140 {
141     int w, v, iv;
142     unsigned char *c;
143
144     w = n / 8;
145     v = 1 << (7 - (n & 0x07));
146     iv = ~v;
147     if (!value)
148         v = 0;
149
150     if (a == NULL)
151         return 0;
152
153     a->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear, set on write */
154
155     if ((a->length < (w + 1)) || (a->data == NULL)) {
156         if (!value)
157             return (1);         /* Don't need to set */
158         c = OPENSSL_clear_realloc(a->data, a->length, w + 1);
159         if (c == NULL) {
160             ASN1err(ASN1_F_ASN1_BIT_STRING_SET_BIT, ERR_R_MALLOC_FAILURE);
161             return 0;
162         }
163         if (w + 1 - a->length > 0)
164             memset(c + a->length, 0, w + 1 - a->length);
165         a->data = c;
166         a->length = w + 1;
167     }
168     a->data[w] = ((a->data[w]) & iv) | v;
169     while ((a->length > 0) && (a->data[a->length - 1] == 0))
170         a->length--;
171     return (1);
172 }
173
174 int ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING *a, int n)
175 {
176     int w, v;
177
178     w = n / 8;
179     v = 1 << (7 - (n & 0x07));
180     if ((a == NULL) || (a->length < (w + 1)) || (a->data == NULL))
181         return (0);
182     return ((a->data[w] & v) != 0);
183 }
184
185 /*
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'.
190  */
191 int ASN1_BIT_STRING_check(const ASN1_BIT_STRING *a,
192                           const unsigned char *flags, int flags_len)
193 {
194     int i, ok;
195     /* Check if there is one bit set at all. */
196     if (!a || !a->data)
197         return 1;
198
199     /*
200      * Check each byte of the internal representation of the bit string.
201      */
202     ok = 1;
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;
207     }
208     return ok;
209 }