summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/asn1/a_bitstr.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/lib/libcrypto/asn1/a_bitstr.c274
1 files changed, 144 insertions, 130 deletions
diff --git a/src/lib/libcrypto/asn1/a_bitstr.c b/src/lib/libcrypto/asn1/a_bitstr.c
index e2b65bf2ac..5633cfca1c 100644
--- a/src/lib/libcrypto/asn1/a_bitstr.c
+++ b/src/lib/libcrypto/asn1/a_bitstr.c
@@ -5,21 +5,21 @@
5 * This package is an SSL implementation written 5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com). 6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL. 7 * The implementation was written so as to conform with Netscapes SSL.
8 * 8 *
9 * This library is free for commercial and non-commercial use as long as 9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions 10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA, 11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms 13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * 15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in 16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed. 17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution 18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used. 19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or 20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package. 21 * in documentation (online or textual) provided with the package.
22 * 22 *
23 * Redistribution and use in source and binary forms, with or without 23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions 24 * modification, are permitted provided that the following conditions
25 * are met: 25 * are met:
@@ -34,10 +34,10 @@
34 * Eric Young (eay@cryptsoft.com)" 34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library 35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-). 36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from 37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement: 38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * 40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -49,7 +49,7 @@
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE. 51 * SUCH DAMAGE.
52 * 52 *
53 * The licence and distribution terms for any publically available version or 53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be 54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence 55 * copied and put under another distribution licence
@@ -60,82 +60,91 @@
60#include "cryptlib.h" 60#include "cryptlib.h"
61#include <openssl/asn1.h> 61#include <openssl/asn1.h>
62 62
63int ASN1_BIT_STRING_set(ASN1_BIT_STRING *x, unsigned char *d, int len) 63int
64{ return M_ASN1_BIT_STRING_set(x, d, len); } 64ASN1_BIT_STRING_set(ASN1_BIT_STRING *x, unsigned char *d, int len)
65{
66 return M_ASN1_BIT_STRING_set(x, d, len);
67}
65 68
66int i2c_ASN1_BIT_STRING(ASN1_BIT_STRING *a, unsigned char **pp) 69int
67 { 70i2c_ASN1_BIT_STRING(ASN1_BIT_STRING *a, unsigned char **pp)
68 int ret,j,bits,len; 71{
69 unsigned char *p,*d; 72 int ret, j,bits, len;
73 unsigned char *p, *d;
70 74
71 if (a == NULL) return(0); 75 if (a == NULL)
76 return (0);
72 77
73 len=a->length; 78 len = a->length;
74 79
75 if (len > 0) 80 if (len > 0) {
76 { 81 if (a->flags & ASN1_STRING_FLAG_BITS_LEFT) {
77 if (a->flags & ASN1_STRING_FLAG_BITS_LEFT) 82 bits = (int)a->flags & 0x07;
78 { 83 } else {
79 bits=(int)a->flags&0x07; 84 for (; len > 0; len--) {
80 } 85 if (a->data[len - 1])
81 else 86 break;
82 {
83 for ( ; len > 0; len--)
84 {
85 if (a->data[len-1]) break;
86 }
87 j=a->data[len-1];
88 if (j & 0x01) bits=0;
89 else if (j & 0x02) bits=1;
90 else if (j & 0x04) bits=2;
91 else if (j & 0x08) bits=3;
92 else if (j & 0x10) bits=4;
93 else if (j & 0x20) bits=5;
94 else if (j & 0x40) bits=6;
95 else if (j & 0x80) bits=7;
96 else bits=0; /* should not happen */
97 } 87 }
88 j = a->data[len - 1];
89 if (j & 0x01)
90 bits = 0;
91 else if (j & 0x02)
92 bits = 1;
93 else if (j & 0x04)
94 bits = 2;
95 else if (j & 0x08)
96 bits = 3;
97 else if (j & 0x10)
98 bits = 4;
99 else if (j & 0x20)
100 bits = 5;
101 else if (j & 0x40)
102 bits = 6;
103 else if (j & 0x80)
104 bits = 7;
105 else
106 bits = 0; /* should not happen */
98 } 107 }
99 else 108 } else
100 bits=0; 109 bits = 0;
101 110
102 ret=1+len; 111 ret = 1 + len;
103 if (pp == NULL) return(ret); 112 if (pp == NULL)
113 return (ret);
104 114
105 p= *pp; 115 p= *pp;
106 116
107 *(p++)=(unsigned char)bits; 117 *(p++) = (unsigned char)bits;
108 d=a->data; 118 d = a->data;
109 memcpy(p,d,len); 119 memcpy(p, d, len);
110 p+=len; 120 p += len;
111 if (len > 0) p[-1]&=(0xff<<bits); 121 if (len > 0)
112 *pp=p; 122 p[-1]&=(0xff << bits);
113 return(ret); 123 *pp = p;
114 } 124 return (ret);
125}
115 126
116ASN1_BIT_STRING *c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a, 127ASN1_BIT_STRING *
117 const unsigned char **pp, long len) 128c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a, const unsigned char **pp, long len)
118 { 129{
119 ASN1_BIT_STRING *ret=NULL; 130 ASN1_BIT_STRING *ret = NULL;
120 const unsigned char *p; 131 const unsigned char *p;
121 unsigned char *s; 132 unsigned char *s;
122 int i; 133 int i;
123 134
124 if (len < 1) 135 if (len < 1) {
125 { 136 i = ASN1_R_STRING_TOO_SHORT;
126 i=ASN1_R_STRING_TOO_SHORT;
127 goto err; 137 goto err;
128 } 138 }
129 139
130 if ((a == NULL) || ((*a) == NULL)) 140 if ((a == NULL) || ((*a) == NULL)) {
131 { 141 if ((ret = M_ASN1_BIT_STRING_new()) == NULL)
132 if ((ret=M_ASN1_BIT_STRING_new()) == NULL) return(NULL); 142 return (NULL);
133 } 143 } else
134 else 144 ret = (*a);
135 ret=(*a);
136 145
137 p= *pp; 146 p = *pp;
138 i= *(p++); 147 i = *(p++);
139 /* We do this to preserve the settings. If we modify 148 /* We do this to preserve the settings. If we modify
140 * the settings, via the _set_bit function, we will recalculate 149 * the settings, via the _set_bit function, we will recalculate
141 * on output */ 150 * on output */
@@ -143,106 +152,111 @@ ASN1_BIT_STRING *c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a,
143 ret->flags|=(ASN1_STRING_FLAG_BITS_LEFT|(i&0x07)); /* set */ 152 ret->flags|=(ASN1_STRING_FLAG_BITS_LEFT|(i&0x07)); /* set */
144 153
145 if (len-- > 1) /* using one because of the bits left byte */ 154 if (len-- > 1) /* using one because of the bits left byte */
146 { 155 {
147 s=(unsigned char *)malloc((int)len); 156 s = (unsigned char *)malloc((int)len);
148 if (s == NULL) 157 if (s == NULL) {
149 { 158 i = ERR_R_MALLOC_FAILURE;
150 i=ERR_R_MALLOC_FAILURE;
151 goto err; 159 goto err;
152 }
153 memcpy(s,p,(int)len);
154 s[len-1]&=(0xff<<i);
155 p+=len;
156 } 160 }
157 else 161 memcpy(s, p, (int)len);
158 s=NULL; 162 s[len - 1] &= (0xff << i);
159 163 p += len;
160 ret->length=(int)len; 164 } else
161 if (ret->data != NULL) free(ret->data); 165 s = NULL;
162 ret->data=s; 166
163 ret->type=V_ASN1_BIT_STRING; 167 ret->length = (int)len;
164 if (a != NULL) (*a)=ret; 168 if (ret->data != NULL)
165 *pp=p; 169 free(ret->data);
166 return(ret); 170 ret->data = s;
171 ret->type = V_ASN1_BIT_STRING;
172 if (a != NULL)
173 (*a) = ret;
174 *pp = p;
175 return (ret);
176
167err: 177err:
168 ASN1err(ASN1_F_C2I_ASN1_BIT_STRING,i); 178 ASN1err(ASN1_F_C2I_ASN1_BIT_STRING, i);
169 if ((ret != NULL) && ((a == NULL) || (*a != ret))) 179 if ((ret != NULL) && ((a == NULL) || (*a != ret)))
170 M_ASN1_BIT_STRING_free(ret); 180 M_ASN1_BIT_STRING_free(ret);
171 return(NULL); 181 return (NULL);
172 } 182}
173 183
174/* These next 2 functions from Goetz Babin-Ebell <babinebell@trustcenter.de> 184/* These next 2 functions from Goetz Babin-Ebell <babinebell@trustcenter.de>
175 */ 185 */
176int ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value) 186int
177 { 187ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value)
178 int w,v,iv; 188{
189 int w, v, iv;
179 unsigned char *c; 190 unsigned char *c;
180 191
181 w=n/8; 192 w = n/8;
182 v=1<<(7-(n&0x07)); 193 v = 1 << (7 - (n & 0x07));
183 iv= ~v; 194 iv = ~v;
184 if (!value) v=0; 195 if (!value)
196 v = 0;
185 197
186 if (a == NULL) 198 if (a == NULL)
187 return 0; 199 return 0;
188 200
189 a->flags&= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07); /* clear, set on write */ 201 a->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07); /* clear, set on write */
190 202
191 if ((a->length < (w+1)) || (a->data == NULL)) 203 if ((a->length < (w + 1)) || (a->data == NULL)) {
192 { 204 if (!value)
193 if (!value) return(1); /* Don't need to set */ 205 return(1); /* Don't need to set */
194 if (a->data == NULL) 206 if (a->data == NULL)
195 c=(unsigned char *)malloc(w+1); 207 c = (unsigned char *)malloc(w + 1);
196 else 208 else
197 c=(unsigned char *)OPENSSL_realloc_clean(a->data, 209 c = (unsigned char *)OPENSSL_realloc_clean(a->data,
198 a->length, 210 a->length, w + 1);
199 w+1); 211 if (c == NULL) {
200 if (c == NULL) 212 ASN1err(ASN1_F_ASN1_BIT_STRING_SET_BIT, ERR_R_MALLOC_FAILURE);
201 {
202 ASN1err(ASN1_F_ASN1_BIT_STRING_SET_BIT,ERR_R_MALLOC_FAILURE);
203 return 0; 213 return 0;
204 } 214 }
205 if (w+1-a->length > 0) memset(c+a->length, 0, w+1-a->length); 215 if (w + 1 - a->length > 0)
206 a->data=c; 216 memset(c + a->length, 0, w + 1 - a->length);
207 a->length=w+1; 217 a->data = c;
218 a->length = w + 1;
208 } 219 }
209 a->data[w]=((a->data[w])&iv)|v; 220 a->data[w] = ((a->data[w]) & iv) | v;
210 while ((a->length > 0) && (a->data[a->length-1] == 0)) 221 while ((a->length > 0) && (a->data[a->length - 1] == 0))
211 a->length--; 222 a->length--;
212 return(1);
213 }
214 223
215int ASN1_BIT_STRING_get_bit(ASN1_BIT_STRING *a, int n) 224 return (1);
216 { 225}
217 int w,v;
218 226
219 w=n/8; 227int
220 v=1<<(7-(n&0x07)); 228ASN1_BIT_STRING_get_bit(ASN1_BIT_STRING *a, int n)
221 if ((a == NULL) || (a->length < (w+1)) || (a->data == NULL)) 229{
222 return(0); 230 int w, v;
223 return((a->data[w]&v) != 0); 231
224 } 232 w = n / 8;
233 v = 1 << (7 - (n & 0x07));
234 if ((a == NULL) || (a->length < (w + 1)) || (a->data == NULL))
235 return (0);
236 return ((a->data[w] & v) != 0);
237}
225 238
226/* 239/*
227 * Checks if the given bit string contains only bits specified by 240 * Checks if the given bit string contains only bits specified by
228 * the flags vector. Returns 0 if there is at least one bit set in 'a' 241 * the flags vector. Returns 0 if there is at least one bit set in 'a'
229 * which is not specified in 'flags', 1 otherwise. 242 * which is not specified in 'flags', 1 otherwise.
230 * 'len' is the length of 'flags'. 243 * 'len' is the length of 'flags'.
231 */ 244 */
232int ASN1_BIT_STRING_check(ASN1_BIT_STRING *a, 245int
233 unsigned char *flags, int flags_len) 246ASN1_BIT_STRING_check(ASN1_BIT_STRING *a, unsigned char *flags, int flags_len)
234 { 247{
235 int i, ok; 248 int i, ok;
249
236 /* Check if there is one bit set at all. */ 250 /* Check if there is one bit set at all. */
237 if (!a || !a->data) return 1; 251 if (!a || !a->data)
252 return 1;
238 253
239 /* Check each byte of the internal representation of the bit string. */ 254 /* Check each byte of the internal representation of the bit string. */
240 ok = 1; 255 ok = 1;
241 for (i = 0; i < a->length && ok; ++i) 256 for (i = 0; i < a->length && ok; ++i) {
242 {
243 unsigned char mask = i < flags_len ? ~flags[i] : 0xff; 257 unsigned char mask = i < flags_len ? ~flags[i] : 0xff;
244 /* We are done if there is an unneeded bit set. */ 258 /* We are done if there is an unneeded bit set. */
245 ok = (a->data[i] & mask) == 0; 259 ok = (a->data[i] & mask) == 0;
246 }
247 return ok;
248 } 260 }
261 return ok;
262}