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