diff options
author | jsing <> | 2014-04-19 10:54:26 +0000 |
---|---|---|
committer | jsing <> | 2014-04-19 10:54:26 +0000 |
commit | 31b6140ba0edafd20b0588bc37e5d019bea48af0 (patch) | |
tree | 24d09976bf238ec468bfeede0dda2a22cf15c427 /src/lib/libcrypto/asn1/asn1_lib.c | |
parent | c377e608f97c1f161ba29565eb04dbcc272d3844 (diff) | |
download | openbsd-31b6140ba0edafd20b0588bc37e5d019bea48af0.tar.gz openbsd-31b6140ba0edafd20b0588bc37e5d019bea48af0.tar.bz2 openbsd-31b6140ba0edafd20b0588bc37e5d019bea48af0.zip |
More KNF.
Diffstat (limited to 'src/lib/libcrypto/asn1/asn1_lib.c')
-rw-r--r-- | src/lib/libcrypto/asn1/asn1_lib.c | 404 |
1 files changed, 226 insertions, 178 deletions
diff --git a/src/lib/libcrypto/asn1/asn1_lib.c b/src/lib/libcrypto/asn1/asn1_lib.c index 7693556281..49d650b125 100644 --- a/src/lib/libcrypto/asn1/asn1_lib.c +++ b/src/lib/libcrypto/asn1/asn1_lib.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 |
@@ -62,192 +62,214 @@ | |||
62 | #include <openssl/asn1.h> | 62 | #include <openssl/asn1.h> |
63 | #include <openssl/asn1_mac.h> | 63 | #include <openssl/asn1_mac.h> |
64 | 64 | ||
65 | static int asn1_get_length(const unsigned char **pp,int *inf,long *rl,int max); | 65 | static int asn1_get_length(const unsigned char **pp, int *inf, long *rl, int max); |
66 | static void asn1_put_length(unsigned char **pp, int length); | 66 | static void asn1_put_length(unsigned char **pp, int length); |
67 | const char ASN1_version[]="ASN.1" OPENSSL_VERSION_PTEXT; | 67 | const char ASN1_version[]="ASN.1" OPENSSL_VERSION_PTEXT; |
68 | 68 | ||
69 | static int _asn1_check_infinite_end(const unsigned char **p, long len) | 69 | static int |
70 | _asn1_check_infinite_end(const unsigned char **p, long len) | ||
70 | { | 71 | { |
71 | /* If there is 0 or 1 byte left, the length check should pick | 72 | /* If there is 0 or 1 byte left, the length check should pick |
72 | * things up */ | 73 | * things up */ |
73 | if (len <= 0) | 74 | if (len <= 0) |
74 | return(1); | 75 | return (1); |
75 | else if ((len >= 2) && ((*p)[0] == 0) && ((*p)[1] == 0)) { | 76 | else if ((len >= 2) && ((*p)[0] == 0) && ((*p)[1] == 0)) { |
76 | (*p)+=2; | 77 | (*p) += 2; |
77 | return(1); | 78 | return (1); |
78 | } | 79 | } |
79 | return(0); | 80 | return (0); |
80 | } | 81 | } |
81 | 82 | ||
82 | int ASN1_check_infinite_end(unsigned char **p, long len) | 83 | int |
84 | ASN1_check_infinite_end(unsigned char **p, long len) | ||
83 | { | 85 | { |
84 | return _asn1_check_infinite_end((const unsigned char **)p, len); | 86 | return _asn1_check_infinite_end((const unsigned char **)p, len); |
85 | } | 87 | } |
86 | 88 | ||
87 | int ASN1_const_check_infinite_end(const unsigned char **p, long len) | 89 | int |
90 | ASN1_const_check_infinite_end(const unsigned char **p, long len) | ||
88 | { | 91 | { |
89 | return _asn1_check_infinite_end(p, len); | 92 | return _asn1_check_infinite_end(p, len); |
90 | } | 93 | } |
91 | 94 | ||
92 | 95 | int | |
93 | int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag, | 96 | ASN1_get_object(const unsigned char **pp, long *plength, int *ptag, |
94 | int *pclass, long omax) | 97 | int *pclass, long omax) |
95 | { | 98 | { |
96 | int i,ret; | 99 | int i, ret; |
97 | long l; | 100 | long l; |
98 | const unsigned char *p= *pp; | 101 | const unsigned char *p= *pp; |
99 | int tag,xclass,inf; | 102 | int tag, xclass, inf; |
100 | long max=omax; | 103 | long max = omax; |
101 | 104 | ||
102 | if (!max) goto err; | 105 | if (!max) |
103 | ret=(*p&V_ASN1_CONSTRUCTED); | 106 | goto err; |
104 | xclass=(*p&V_ASN1_PRIVATE); | 107 | ret = (*p & V_ASN1_CONSTRUCTED); |
105 | i= *p&V_ASN1_PRIMITIVE_TAG; | 108 | xclass = (*p & V_ASN1_PRIVATE); |
109 | i= *p & V_ASN1_PRIMITIVE_TAG; | ||
106 | if (i == V_ASN1_PRIMITIVE_TAG) { /* high-tag */ | 110 | if (i == V_ASN1_PRIMITIVE_TAG) { /* high-tag */ |
107 | p++; | 111 | p++; |
108 | if (--max == 0) goto err; | 112 | if (--max == 0) |
109 | l=0; | 113 | goto err; |
110 | while (*p&0x80) { | 114 | l = 0; |
111 | l<<=7L; | 115 | while (*p & 0x80) { |
112 | l|= *(p++)&0x7f; | 116 | l <<= 7L; |
113 | if (--max == 0) goto err; | 117 | l |= *(p++) & 0x7f; |
114 | if (l > (INT_MAX >> 7L)) goto err; | 118 | if (--max == 0) |
119 | goto err; | ||
120 | if (l > (INT_MAX >> 7L)) | ||
121 | goto err; | ||
115 | } | 122 | } |
116 | l<<=7L; | 123 | l <<= 7L; |
117 | l|= *(p++)&0x7f; | 124 | l |= *(p++) & 0x7f; |
118 | tag=(int)l; | 125 | tag = (int)l; |
119 | if (--max == 0) goto err; | 126 | if (--max == 0) |
120 | } else { | 127 | goto err; |
121 | tag=i; | 128 | } else { |
129 | tag = i; | ||
122 | p++; | 130 | p++; |
123 | if (--max == 0) goto err; | 131 | if (--max == 0) |
132 | goto err; | ||
124 | } | 133 | } |
125 | *ptag=tag; | 134 | *ptag = tag; |
126 | *pclass=xclass; | 135 | *pclass = xclass; |
127 | if (!asn1_get_length(&p,&inf,plength,(int)max)) goto err; | 136 | if (!asn1_get_length(&p, &inf, plength, (int)max)) |
137 | goto err; | ||
128 | 138 | ||
129 | #if 0 | 139 | #if 0 |
130 | fprintf(stderr,"p=%d + *plength=%ld > omax=%ld + *pp=%d (%d > %d)\n", | 140 | fprintf(stderr, "p=%d + *plength=%ld > omax=%ld + *pp=%d (%d > %d)\n", |
131 | (int)p,*plength,omax,(int)*pp,(int)(p+ *plength), | 141 | (int)p, *plength, omax, (int)*pp, (int)(p+ *plength), |
132 | (int)(omax+ *pp)); | 142 | (int)(omax+ *pp)); |
133 | 143 | ||
134 | #endif | 144 | #endif |
135 | if (*plength > (omax - (p - *pp))) { | 145 | if (*plength > (omax - (p - *pp))) { |
136 | ASN1err(ASN1_F_ASN1_GET_OBJECT,ASN1_R_TOO_LONG); | 146 | ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_TOO_LONG); |
137 | /* Set this so that even if things are not long enough | 147 | /* Set this so that even if things are not long enough |
138 | * the values are set correctly */ | 148 | * the values are set correctly */ |
139 | ret|=0x80; | 149 | ret |= 0x80; |
140 | } | 150 | } |
141 | *pp=p; | 151 | *pp = p; |
142 | return(ret|inf); | 152 | return (ret | inf); |
153 | |||
143 | err: | 154 | err: |
144 | ASN1err(ASN1_F_ASN1_GET_OBJECT,ASN1_R_HEADER_TOO_LONG); | 155 | ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_HEADER_TOO_LONG); |
145 | return(0x80); | 156 | return (0x80); |
146 | } | 157 | } |
147 | 158 | ||
148 | static int asn1_get_length(const unsigned char **pp, int *inf, long *rl, int max) | 159 | static int |
160 | asn1_get_length(const unsigned char **pp, int *inf, long *rl, int max) | ||
149 | { | 161 | { |
150 | const unsigned char *p= *pp; | 162 | const unsigned char *p= *pp; |
151 | unsigned long ret=0; | 163 | unsigned long ret = 0; |
152 | unsigned int i; | 164 | unsigned int i; |
153 | 165 | ||
154 | if (max-- < 1) return(0); | 166 | if (max-- < 1) |
167 | return (0); | ||
155 | if (*p == 0x80) { | 168 | if (*p == 0x80) { |
156 | *inf=1; | 169 | *inf = 1; |
157 | ret=0; | 170 | ret = 0; |
158 | p++; | 171 | p++; |
159 | } else { | 172 | } else { |
160 | *inf=0; | 173 | *inf = 0; |
161 | i= *p&0x7f; | 174 | i= *p & 0x7f; |
162 | if (*(p++) & 0x80) { | 175 | if (*(p++) & 0x80) { |
163 | if (i > sizeof(long)) | 176 | if (i > sizeof(long)) |
164 | return 0; | 177 | return 0; |
165 | if (max-- == 0) return(0); | 178 | if (max-- == 0) |
179 | return (0); | ||
166 | while (i-- > 0) { | 180 | while (i-- > 0) { |
167 | ret<<=8L; | 181 | ret <<= 8L; |
168 | ret|= *(p++); | 182 | ret |= *(p++); |
169 | if (max-- == 0) return(0); | 183 | if (max-- == 0) |
184 | return (0); | ||
170 | } | 185 | } |
171 | } | 186 | } else |
172 | else | 187 | ret = i; |
173 | ret=i; | ||
174 | } | 188 | } |
175 | if (ret > LONG_MAX) | 189 | if (ret > LONG_MAX) |
176 | return 0; | 190 | return 0; |
177 | *pp=p; | 191 | *pp = p; |
178 | *rl=(long)ret; | 192 | *rl = (long)ret; |
179 | return(1); | 193 | return (1); |
180 | } | 194 | } |
181 | 195 | ||
182 | /* class 0 is constructed | 196 | /* class 0 is constructed |
183 | * constructed == 2 for indefinite length constructed */ | 197 | * constructed == 2 for indefinite length constructed */ |
184 | void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag, | 198 | void |
185 | int xclass) | 199 | ASN1_put_object(unsigned char **pp, int constructed, int length, int tag, |
200 | int xclass) | ||
186 | { | 201 | { |
187 | unsigned char *p= *pp; | 202 | unsigned char *p= *pp; |
188 | int i, ttag; | 203 | int i, ttag; |
189 | 204 | ||
190 | i=(constructed)?V_ASN1_CONSTRUCTED:0; | 205 | i = (constructed) ? V_ASN1_CONSTRUCTED : 0; |
191 | i|=(xclass&V_ASN1_PRIVATE); | 206 | i |= (xclass & V_ASN1_PRIVATE); |
192 | if (tag < 31) | 207 | if (tag < 31) |
193 | *(p++)=i|(tag&V_ASN1_PRIMITIVE_TAG); | 208 | *(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG); |
194 | else { | 209 | else { |
195 | *(p++)=i|V_ASN1_PRIMITIVE_TAG; | 210 | *(p++) = i | V_ASN1_PRIMITIVE_TAG; |
196 | for(i = 0, ttag = tag; ttag > 0; i++) ttag >>=7; | 211 | for(i = 0, ttag = tag; ttag > 0; i++) |
212 | ttag >>= 7; | ||
197 | ttag = i; | 213 | ttag = i; |
198 | while(i-- > 0) { | 214 | while (i-- > 0) { |
199 | p[i] = tag & 0x7f; | 215 | p[i] = tag & 0x7f; |
200 | if(i != (ttag - 1)) p[i] |= 0x80; | 216 | if (i != (ttag - 1)) |
217 | p[i] |= 0x80; | ||
201 | tag >>= 7; | 218 | tag >>= 7; |
202 | } | 219 | } |
203 | p += ttag; | 220 | p += ttag; |
204 | } | 221 | } |
205 | if (constructed == 2) | 222 | if (constructed == 2) |
206 | *(p++)=0x80; | 223 | *(p++) = 0x80; |
207 | else | 224 | else |
208 | asn1_put_length(&p,length); | 225 | asn1_put_length(&p, length); |
209 | *pp=p; | 226 | *pp = p; |
210 | } | 227 | } |
211 | 228 | ||
212 | int ASN1_put_eoc(unsigned char **pp) | 229 | int |
230 | ASN1_put_eoc(unsigned char **pp) | ||
213 | { | 231 | { |
214 | unsigned char *p = *pp; | 232 | unsigned char *p = *pp; |
233 | |||
215 | *p++ = 0; | 234 | *p++ = 0; |
216 | *p++ = 0; | 235 | *p++ = 0; |
217 | *pp = p; | 236 | *pp = p; |
218 | return 2; | 237 | return 2; |
219 | } | 238 | } |
220 | 239 | ||
221 | static void asn1_put_length(unsigned char **pp, int length) | 240 | static void |
241 | asn1_put_length(unsigned char **pp, int length) | ||
222 | { | 242 | { |
223 | unsigned char *p= *pp; | 243 | unsigned char *p= *pp; |
224 | int i,l; | 244 | |
245 | int i, l; | ||
225 | if (length <= 127) | 246 | if (length <= 127) |
226 | *(p++)=(unsigned char)length; | 247 | *(p++) = (unsigned char)length; |
227 | else { | 248 | else { |
228 | l=length; | 249 | l = length; |
229 | for (i=0; l > 0; i++) | 250 | for (i = 0; l > 0; i++) |
230 | l>>=8; | 251 | l >>= 8; |
231 | *(p++)=i|0x80; | 252 | *(p++) = i | 0x80; |
232 | l=i; | 253 | l = i; |
233 | while (i-- > 0) { | 254 | while (i-- > 0) { |
234 | p[i]=length&0xff; | 255 | p[i] = length & 0xff; |
235 | length>>=8; | 256 | length >>= 8; |
236 | } | 257 | } |
237 | p+=l; | 258 | p += l; |
238 | } | 259 | } |
239 | *pp=p; | 260 | *pp = p; |
240 | } | 261 | } |
241 | 262 | ||
242 | int ASN1_object_size(int constructed, int length, int tag) | 263 | int |
264 | ASN1_object_size(int constructed, int length, int tag) | ||
243 | { | 265 | { |
244 | int ret; | 266 | int ret; |
245 | 267 | ||
246 | ret=length; | 268 | ret = length; |
247 | ret++; | 269 | ret++; |
248 | if (tag >= 31) { | 270 | if (tag >= 31) { |
249 | while (tag > 0) { | 271 | while (tag > 0) { |
250 | tag>>=7; | 272 | tag >>= 7; |
251 | ret++; | 273 | ret++; |
252 | } | 274 | } |
253 | } | 275 | } |
@@ -256,125 +278,134 @@ int ASN1_object_size(int constructed, int length, int tag) | |||
256 | ret++; | 278 | ret++; |
257 | if (length > 127) { | 279 | if (length > 127) { |
258 | while (length > 0) { | 280 | while (length > 0) { |
259 | length>>=8; | 281 | length >>= 8; |
260 | ret++; | 282 | ret++; |
261 | } | 283 | } |
262 | } | 284 | } |
263 | return(ret); | 285 | return (ret); |
264 | } | 286 | } |
265 | 287 | ||
266 | static int _asn1_Finish(ASN1_const_CTX *c) | 288 | static int |
289 | _asn1_Finish(ASN1_const_CTX *c) | ||
267 | { | 290 | { |
268 | if ((c->inf == (1|V_ASN1_CONSTRUCTED)) && (!c->eos)) { | 291 | if ((c->inf == (1|V_ASN1_CONSTRUCTED)) && (!c->eos)) { |
269 | if (!ASN1_const_check_infinite_end(&c->p,c->slen)) { | 292 | if (!ASN1_const_check_infinite_end(&c->p, c->slen)) { |
270 | c->error=ERR_R_MISSING_ASN1_EOS; | 293 | c->error = ERR_R_MISSING_ASN1_EOS; |
271 | return(0); | 294 | return (0); |
272 | } | 295 | } |
273 | } | 296 | } |
274 | if ( ((c->slen != 0) && !(c->inf & 1)) || | 297 | if (((c->slen != 0) && !(c->inf & 1)) || |
275 | ((c->slen < 0) && (c->inf & 1))) { | 298 | ((c->slen < 0) && (c->inf & 1))) { |
276 | c->error=ERR_R_ASN1_LENGTH_MISMATCH; | 299 | c->error = ERR_R_ASN1_LENGTH_MISMATCH; |
277 | return(0); | 300 | return (0); |
278 | } | 301 | } |
279 | return(1); | 302 | return (1); |
280 | } | 303 | } |
281 | 304 | ||
282 | int asn1_Finish(ASN1_CTX *c) | 305 | int |
306 | asn1_Finish(ASN1_CTX *c) | ||
283 | { | 307 | { |
284 | return _asn1_Finish((ASN1_const_CTX *)c); | 308 | return _asn1_Finish((ASN1_const_CTX *)c); |
285 | } | 309 | } |
286 | 310 | ||
287 | int asn1_const_Finish(ASN1_const_CTX *c) | 311 | int |
312 | asn1_const_Finish(ASN1_const_CTX *c) | ||
288 | { | 313 | { |
289 | return _asn1_Finish(c); | 314 | return _asn1_Finish(c); |
290 | } | 315 | } |
291 | 316 | ||
292 | int asn1_GetSequence(ASN1_const_CTX *c, long *length) | 317 | int |
318 | asn1_GetSequence(ASN1_const_CTX *c, long *length) | ||
293 | { | 319 | { |
294 | const unsigned char *q; | 320 | const unsigned char *q; |
295 | 321 | ||
296 | q=c->p; | 322 | q = c->p; |
297 | c->inf=ASN1_get_object(&(c->p),&(c->slen),&(c->tag),&(c->xclass), | 323 | c->inf = ASN1_get_object(&(c->p), &(c->slen), &(c->tag), &(c->xclass), |
298 | *length); | 324 | *length); |
299 | if (c->inf & 0x80) { | 325 | if (c->inf & 0x80) { |
300 | c->error=ERR_R_BAD_GET_ASN1_OBJECT_CALL; | 326 | c->error = ERR_R_BAD_GET_ASN1_OBJECT_CALL; |
301 | return(0); | 327 | return (0); |
302 | } | 328 | } |
303 | if (c->tag != V_ASN1_SEQUENCE) { | 329 | if (c->tag != V_ASN1_SEQUENCE) { |
304 | c->error=ERR_R_EXPECTING_AN_ASN1_SEQUENCE; | 330 | c->error = ERR_R_EXPECTING_AN_ASN1_SEQUENCE; |
305 | return(0); | 331 | return (0); |
306 | } | 332 | } |
307 | (*length)-=(c->p-q); | 333 | (*length) -= (c->p - q); |
308 | if (c->max && (*length < 0)) { | 334 | if (c->max && (*length < 0)) { |
309 | c->error=ERR_R_ASN1_LENGTH_MISMATCH; | 335 | c->error = ERR_R_ASN1_LENGTH_MISMATCH; |
310 | return(0); | 336 | return (0); |
311 | } | 337 | } |
312 | if (c->inf == (1|V_ASN1_CONSTRUCTED)) | 338 | if (c->inf == (1|V_ASN1_CONSTRUCTED)) |
313 | c->slen= *length+ *(c->pp)-c->p; | 339 | c->slen= *length+ *(c->pp) - c->p; |
314 | c->eos=0; | 340 | c->eos = 0; |
315 | return(1); | 341 | return (1); |
316 | } | 342 | } |
317 | 343 | ||
318 | int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str) | 344 | int |
345 | ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str) | ||
319 | { | 346 | { |
320 | if (str == NULL) | 347 | if (str == NULL) |
321 | return 0; | 348 | return 0; |
322 | dst->type = str->type; | 349 | dst->type = str->type; |
323 | if (!ASN1_STRING_set(dst,str->data,str->length)) | 350 | if (!ASN1_STRING_set(dst, str->data, str->length)) |
324 | return 0; | 351 | return 0; |
325 | dst->flags = str->flags; | 352 | dst->flags = str->flags; |
326 | return 1; | 353 | return 1; |
327 | } | 354 | } |
328 | 355 | ||
329 | ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str) | 356 | ASN1_STRING * |
357 | ASN1_STRING_dup(const ASN1_STRING *str) | ||
330 | { | 358 | { |
331 | ASN1_STRING *ret; | 359 | ASN1_STRING *ret; |
360 | |||
332 | if (!str) | 361 | if (!str) |
333 | return NULL; | 362 | return NULL; |
334 | ret=ASN1_STRING_new(); | 363 | ret = ASN1_STRING_new(); |
335 | if (!ret) | 364 | if (!ret) |
336 | return NULL; | 365 | return NULL; |
337 | if (!ASN1_STRING_copy(ret,str)) { | 366 | if (!ASN1_STRING_copy(ret, str)) { |
338 | ASN1_STRING_free(ret); | 367 | ASN1_STRING_free(ret); |
339 | return NULL; | 368 | return NULL; |
340 | } | 369 | } |
341 | return ret; | 370 | return ret; |
342 | } | 371 | } |
343 | 372 | ||
344 | int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len) | 373 | int |
374 | ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len) | ||
345 | { | 375 | { |
346 | unsigned char *c; | 376 | unsigned char *c; |
347 | const char *data=_data; | 377 | const char *data = _data; |
348 | 378 | ||
349 | if (len < 0) { | 379 | if (len < 0) { |
350 | if (data == NULL) | 380 | if (data == NULL) |
351 | return(0); | 381 | return (0); |
352 | else | 382 | else |
353 | len=strlen(data); | 383 | len = strlen(data); |
354 | } | 384 | } |
355 | if ((str->length < len) || (str->data == NULL)) { | 385 | if ((str->length < len) || (str->data == NULL)) { |
356 | c=str->data; | 386 | c = str->data; |
357 | if (c == NULL) | 387 | if (c == NULL) |
358 | str->data=malloc(len+1); | 388 | str->data = malloc(len + 1); |
359 | else | 389 | else |
360 | str->data=realloc(c,len+1); | 390 | str->data = realloc(c, len + 1); |
361 | 391 | ||
362 | if (str->data == NULL) { | 392 | if (str->data == NULL) { |
363 | ASN1err(ASN1_F_ASN1_STRING_SET,ERR_R_MALLOC_FAILURE); | 393 | ASN1err(ASN1_F_ASN1_STRING_SET, ERR_R_MALLOC_FAILURE); |
364 | str->data=c; | 394 | str->data = c; |
365 | return(0); | 395 | return (0); |
366 | } | 396 | } |
367 | } | 397 | } |
368 | str->length=len; | 398 | str->length = len; |
369 | if (data != NULL) { | 399 | if (data != NULL) { |
370 | memcpy(str->data,data,len); | 400 | memcpy(str->data, data, len); |
371 | /* an allowance for strings :-) */ | 401 | /* an allowance for strings :-) */ |
372 | str->data[len]='\0'; | 402 | str->data[len]='\0'; |
373 | } | 403 | } |
374 | return(1); | 404 | return (1); |
375 | } | 405 | } |
376 | 406 | ||
377 | void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len) | 407 | void |
408 | ASN1_STRING_set0(ASN1_STRING *str, void *data, int len) | ||
378 | { | 409 | { |
379 | if (str->data) | 410 | if (str->data) |
380 | free(str->data); | 411 | free(str->data); |
@@ -382,69 +413,86 @@ void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len) | |||
382 | str->length = len; | 413 | str->length = len; |
383 | } | 414 | } |
384 | 415 | ||
385 | ASN1_STRING *ASN1_STRING_new(void) | 416 | ASN1_STRING * |
417 | ASN1_STRING_new(void) | ||
386 | { | 418 | { |
387 | return(ASN1_STRING_type_new(V_ASN1_OCTET_STRING)); | 419 | return (ASN1_STRING_type_new(V_ASN1_OCTET_STRING)); |
388 | } | 420 | } |
389 | 421 | ||
390 | 422 | ASN1_STRING * | |
391 | ASN1_STRING *ASN1_STRING_type_new(int type) | 423 | ASN1_STRING_type_new(int type) |
392 | { | 424 | { |
393 | ASN1_STRING *ret; | 425 | ASN1_STRING *ret; |
394 | 426 | ||
395 | ret=(ASN1_STRING *)malloc(sizeof(ASN1_STRING)); | 427 | ret = (ASN1_STRING *)malloc(sizeof(ASN1_STRING)); |
396 | if (ret == NULL) { | 428 | if (ret == NULL) { |
397 | ASN1err(ASN1_F_ASN1_STRING_TYPE_NEW,ERR_R_MALLOC_FAILURE); | 429 | ASN1err(ASN1_F_ASN1_STRING_TYPE_NEW, ERR_R_MALLOC_FAILURE); |
398 | return(NULL); | 430 | return (NULL); |
399 | } | 431 | } |
400 | ret->length=0; | 432 | ret->length = 0; |
401 | ret->type=type; | 433 | ret->type = type; |
402 | ret->data=NULL; | 434 | ret->data = NULL; |
403 | ret->flags=0; | 435 | ret->flags = 0; |
404 | return(ret); | 436 | return (ret); |
405 | } | 437 | } |
406 | 438 | ||
407 | void ASN1_STRING_free(ASN1_STRING *a) | 439 | void |
440 | ASN1_STRING_free(ASN1_STRING *a) | ||
408 | { | 441 | { |
409 | if (a == NULL) return; | 442 | if (a == NULL) |
443 | return; | ||
410 | if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF)) | 444 | if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF)) |
411 | free(a->data); | 445 | free(a->data); |
412 | free(a); | 446 | free(a); |
413 | } | 447 | } |
414 | 448 | ||
415 | int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b) | 449 | int |
450 | ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b) | ||
416 | { | 451 | { |
417 | int i; | 452 | int i; |
418 | 453 | ||
419 | i=(a->length-b->length); | 454 | i = (a->length - b->length); |
420 | if (i == 0) { | 455 | if (i == 0) { |
421 | i=memcmp(a->data,b->data,a->length); | 456 | i = memcmp(a->data, b->data, a->length); |
422 | if (i == 0) | 457 | if (i == 0) |
423 | return(a->type-b->type); | 458 | return (a->type - b->type); |
424 | else | 459 | else |
425 | return(i); | 460 | return (i); |
426 | } | 461 | } else |
427 | else | 462 | return (i); |
428 | return(i); | ||
429 | } | 463 | } |
430 | 464 | ||
431 | void asn1_add_error(const unsigned char *address, int offset) | 465 | void |
466 | asn1_add_error(const unsigned char *address, int offset) | ||
432 | { | 467 | { |
433 | char buf1[DECIMAL_SIZE(address)+1],buf2[DECIMAL_SIZE(offset)+1]; | 468 | char buf1[DECIMAL_SIZE(address) + 1], buf2[DECIMAL_SIZE(offset) + 1]; |
434 | 469 | ||
435 | (void) snprintf(buf1,sizeof buf1,"%lu",(unsigned long)address); | 470 | (void) snprintf(buf1, sizeof buf1, "%lu", (unsigned long)address); |
436 | (void) snprintf(buf2,sizeof buf2,"%d",offset); | 471 | (void) snprintf(buf2, sizeof buf2, "%d", offset); |
437 | ERR_add_error_data(4,"address=",buf1," offset=",buf2); | 472 | ERR_add_error_data(4, "address=", buf1, " offset=", buf2); |
438 | } | 473 | } |
439 | 474 | ||
440 | int ASN1_STRING_length(const ASN1_STRING *x) | 475 | int |
441 | { return M_ASN1_STRING_length(x); } | 476 | ASN1_STRING_length(const ASN1_STRING *x) |
477 | { | ||
478 | return M_ASN1_STRING_length(x); | ||
479 | } | ||
442 | 480 | ||
443 | void ASN1_STRING_length_set(ASN1_STRING *x, int len) | 481 | void |
444 | { M_ASN1_STRING_length_set(x, len); return; } | 482 | ASN1_STRING_length_set(ASN1_STRING *x, int len) |
483 | { | ||
484 | M_ASN1_STRING_length_set(x, len); | ||
485 | return; | ||
486 | } | ||
445 | 487 | ||
446 | int ASN1_STRING_type(ASN1_STRING *x) | 488 | int |
447 | { return M_ASN1_STRING_type(x); } | 489 | ASN1_STRING_type(ASN1_STRING *x) |
490 | { | ||
491 | return M_ASN1_STRING_type(x); | ||
492 | } | ||
448 | 493 | ||
449 | unsigned char * ASN1_STRING_data(ASN1_STRING *x) | 494 | unsigned char * |
450 | { return M_ASN1_STRING_data(x); } | 495 | ASN1_STRING_data(ASN1_STRING *x) |
496 | { | ||
497 | return M_ASN1_STRING_data(x); | ||
498 | } | ||