diff options
author | beck <> | 1999-09-29 04:37:45 +0000 |
---|---|---|
committer | beck <> | 1999-09-29 04:37:45 +0000 |
commit | de8f24ea083384bb66b32ec105dc4743c5663cdf (patch) | |
tree | 1412176ae62a3cab2cf2b0b92150fcbceaac6092 /src/lib/libcrypto/asn1/a_int.c | |
parent | cb929d29896bcb87c2a97417fbd03e50078fc178 (diff) | |
download | openbsd-de8f24ea083384bb66b32ec105dc4743c5663cdf.tar.gz openbsd-de8f24ea083384bb66b32ec105dc4743c5663cdf.tar.bz2 openbsd-de8f24ea083384bb66b32ec105dc4743c5663cdf.zip |
OpenSSL 0.9.4 merge
Diffstat (limited to 'src/lib/libcrypto/asn1/a_int.c')
-rw-r--r-- | src/lib/libcrypto/asn1/a_int.c | 215 |
1 files changed, 163 insertions, 52 deletions
diff --git a/src/lib/libcrypto/asn1/a_int.c b/src/lib/libcrypto/asn1/a_int.c index df79cf99bb..d05436378b 100644 --- a/src/lib/libcrypto/asn1/a_int.c +++ b/src/lib/libcrypto/asn1/a_int.c | |||
@@ -58,17 +58,37 @@ | |||
58 | 58 | ||
59 | #include <stdio.h> | 59 | #include <stdio.h> |
60 | #include "cryptlib.h" | 60 | #include "cryptlib.h" |
61 | #include "asn1.h" | 61 | #include <openssl/asn1.h> |
62 | 62 | ||
63 | /* ASN1err(ASN1_F_D2I_ASN1_INTEGER,ASN1_R_EXPECTING_AN_INTEGER); | 63 | /* |
64 | * This converts an ASN1 INTEGER into its DER encoding. | ||
65 | * The internal representation is an ASN1_STRING whose data is a big endian | ||
66 | * representation of the value, ignoring the sign. The sign is determined by | ||
67 | * the type: V_ASN1_INTEGER for positive and V_ASN1_NEG_INTEGER for negative. | ||
68 | * | ||
69 | * Positive integers are no problem: they are almost the same as the DER | ||
70 | * encoding, except if the first byte is >= 0x80 we need to add a zero pad. | ||
71 | * | ||
72 | * Negative integers are a bit trickier... | ||
73 | * The DER representation of negative integers is in 2s complement form. | ||
74 | * The internal form is converted by complementing each octet and finally | ||
75 | * adding one to the result. This can be done less messily with a little trick. | ||
76 | * If the internal form has trailing zeroes then they will become FF by the | ||
77 | * complement and 0 by the add one (due to carry) so just copy as many trailing | ||
78 | * zeros to the destination as there are in the source. The carry will add one | ||
79 | * to the last none zero octet: so complement this octet and add one and finally | ||
80 | * complement any left over until you get to the start of the string. | ||
81 | * | ||
82 | * Padding is a little trickier too. If the first bytes is > 0x80 then we pad | ||
83 | * with 0xff. However if the first byte is 0x80 and one of the following bytes | ||
84 | * is non-zero we pad with 0xff. The reason for this distinction is that 0x80 | ||
85 | * followed by optional zeros isn't padded. | ||
64 | */ | 86 | */ |
65 | 87 | ||
66 | int i2d_ASN1_INTEGER(a,pp) | 88 | int i2d_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp) |
67 | ASN1_INTEGER *a; | ||
68 | unsigned char **pp; | ||
69 | { | 89 | { |
70 | int pad=0,ret,r,i,t; | 90 | int pad=0,ret,r,i,t; |
71 | unsigned char *p,*pt,*n,pb=0; | 91 | unsigned char *p,*n,pb=0; |
72 | 92 | ||
73 | if ((a == NULL) || (a->data == NULL)) return(0); | 93 | if ((a == NULL) || (a->data == NULL)) return(0); |
74 | t=a->type; | 94 | t=a->type; |
@@ -78,16 +98,25 @@ unsigned char **pp; | |||
78 | { | 98 | { |
79 | ret=a->length; | 99 | ret=a->length; |
80 | i=a->data[0]; | 100 | i=a->data[0]; |
81 | if ((t == V_ASN1_INTEGER) && (i > 127)) | 101 | if ((t == V_ASN1_INTEGER) && (i > 127)) { |
82 | { | ||
83 | pad=1; | 102 | pad=1; |
84 | pb=0; | 103 | pb=0; |
104 | } else if(t == V_ASN1_NEG_INTEGER) { | ||
105 | if(i>128) { | ||
106 | pad=1; | ||
107 | pb=0xFF; | ||
108 | } else if(i == 128) { | ||
109 | /* | ||
110 | * Special case: if any other bytes non zero we pad: | ||
111 | * otherwise we don't. | ||
112 | */ | ||
113 | for(i = 1; i < a->length; i++) if(a->data[i]) { | ||
114 | pad=1; | ||
115 | pb=0xFF; | ||
116 | break; | ||
117 | } | ||
85 | } | 118 | } |
86 | else if ((t == V_ASN1_NEG_INTEGER) && (i>128)) | 119 | } |
87 | { | ||
88 | pad=1; | ||
89 | pb=0xFF; | ||
90 | } | ||
91 | ret+=pad; | 120 | ret+=pad; |
92 | } | 121 | } |
93 | r=ASN1_object_size(0,ret,V_ASN1_INTEGER); | 122 | r=ASN1_object_size(0,ret,V_ASN1_INTEGER); |
@@ -96,33 +125,35 @@ unsigned char **pp; | |||
96 | 125 | ||
97 | ASN1_put_object(&p,0,ret,V_ASN1_INTEGER,V_ASN1_UNIVERSAL); | 126 | ASN1_put_object(&p,0,ret,V_ASN1_INTEGER,V_ASN1_UNIVERSAL); |
98 | if (pad) *(p++)=pb; | 127 | if (pad) *(p++)=pb; |
99 | if (a->length == 0) | 128 | if (a->length == 0) *(p++)=0; |
100 | *(p++)=0; | 129 | else if (t == V_ASN1_INTEGER) memcpy(p,a->data,(unsigned int)a->length); |
101 | else if (t == V_ASN1_INTEGER) | 130 | else { |
102 | { | 131 | /* Begin at the end of the encoding */ |
103 | memcpy(p,a->data,(unsigned int)a->length); | 132 | n=a->data + a->length - 1; |
104 | p+=a->length; | 133 | p += a->length - 1; |
105 | } | 134 | i = a->length; |
106 | else | 135 | /* Copy zeros to destination as long as source is zero */ |
107 | { | 136 | while(!*n) { |
108 | n=a->data; | 137 | *(p--) = 0; |
109 | pt=p; | 138 | n--; |
110 | for (i=a->length; i>0; i--) | 139 | i--; |
111 | *(p++)= (*(n++)^0xFF)+1; | ||
112 | if (!pad) *pt|=0x80; | ||
113 | } | 140 | } |
141 | /* Complement and increment next octet */ | ||
142 | *(p--) = ((*(n--)) ^ 0xff) + 1; | ||
143 | i--; | ||
144 | /* Complement any octets left */ | ||
145 | for(;i > 0; i--) *(p--) = *(n--) ^ 0xff; | ||
146 | } | ||
114 | 147 | ||
115 | *pp=p; | 148 | *pp+=r; |
116 | return(r); | 149 | return(r); |
117 | } | 150 | } |
118 | 151 | ||
119 | ASN1_INTEGER *d2i_ASN1_INTEGER(a, pp, length) | 152 | ASN1_INTEGER *d2i_ASN1_INTEGER(ASN1_INTEGER **a, unsigned char **pp, |
120 | ASN1_INTEGER **a; | 153 | long length) |
121 | unsigned char **pp; | ||
122 | long length; | ||
123 | { | 154 | { |
124 | ASN1_INTEGER *ret=NULL; | 155 | ASN1_INTEGER *ret=NULL; |
125 | unsigned char *p,*to,*s; | 156 | unsigned char *p,*to,*s, *pend; |
126 | long len; | 157 | long len; |
127 | int inf,tag,xclass; | 158 | int inf,tag,xclass; |
128 | int i; | 159 | int i; |
@@ -137,6 +168,7 @@ long length; | |||
137 | 168 | ||
138 | p= *pp; | 169 | p= *pp; |
139 | inf=ASN1_get_object(&p,&len,&tag,&xclass,length); | 170 | inf=ASN1_get_object(&p,&len,&tag,&xclass,length); |
171 | pend = p + len; | ||
140 | if (inf & 0x80) | 172 | if (inf & 0x80) |
141 | { | 173 | { |
142 | i=ASN1_R_BAD_OBJECT_HEADER; | 174 | i=ASN1_R_BAD_OBJECT_HEADER; |
@@ -161,16 +193,102 @@ long length; | |||
161 | if (*p & 0x80) /* a negative number */ | 193 | if (*p & 0x80) /* a negative number */ |
162 | { | 194 | { |
163 | ret->type=V_ASN1_NEG_INTEGER; | 195 | ret->type=V_ASN1_NEG_INTEGER; |
164 | if (*p == 0xff) | 196 | if ((*p == 0xff) && (len != 1)) { |
197 | p++; | ||
198 | len--; | ||
199 | } | ||
200 | i = len; | ||
201 | p += i - 1; | ||
202 | to += i - 1; | ||
203 | while((!*p) && i) { | ||
204 | *(to--) = 0; | ||
205 | i--; | ||
206 | p--; | ||
207 | } | ||
208 | /* Special case: if all zeros then the number will be of | ||
209 | * the form FF followed by n zero bytes: this corresponds to | ||
210 | * 1 followed by n zero bytes. We've already written n zeros | ||
211 | * so we just append an extra one and set the first byte to | ||
212 | * a 1. This is treated separately because it is the only case | ||
213 | * where the number of bytes is larger than len. | ||
214 | */ | ||
215 | if(!i) { | ||
216 | *s = 1; | ||
217 | s[len] = 0; | ||
218 | len++; | ||
219 | } else { | ||
220 | *(to--) = (*(p--) ^ 0xff) + 1; | ||
221 | i--; | ||
222 | for(;i > 0; i--) *(to--) = *(p--) ^ 0xff; | ||
223 | } | ||
224 | } else { | ||
225 | ret->type=V_ASN1_INTEGER; | ||
226 | if ((*p == 0) && (len != 1)) | ||
165 | { | 227 | { |
166 | p++; | 228 | p++; |
167 | len--; | 229 | len--; |
168 | } | 230 | } |
169 | for (i=(int)len; i>0; i--) | 231 | memcpy(s,p,(int)len); |
170 | *(to++)= (*(p++)^0xFF)+1; | 232 | } |
233 | |||
234 | if (ret->data != NULL) Free((char *)ret->data); | ||
235 | ret->data=s; | ||
236 | ret->length=(int)len; | ||
237 | if (a != NULL) (*a)=ret; | ||
238 | *pp=pend; | ||
239 | return(ret); | ||
240 | err: | ||
241 | ASN1err(ASN1_F_D2I_ASN1_INTEGER,i); | ||
242 | if ((ret != NULL) && ((a == NULL) || (*a != ret))) | ||
243 | ASN1_INTEGER_free(ret); | ||
244 | return(NULL); | ||
245 | } | ||
246 | |||
247 | /* This is a version of d2i_ASN1_INTEGER that ignores the sign bit of | ||
248 | * ASN1 integers: some broken software can encode a positive INTEGER | ||
249 | * with its MSB set as negative (it doesn't add a padding zero). | ||
250 | */ | ||
251 | |||
252 | ASN1_INTEGER *d2i_ASN1_UINTEGER(ASN1_INTEGER **a, unsigned char **pp, | ||
253 | long length) | ||
254 | { | ||
255 | ASN1_INTEGER *ret=NULL; | ||
256 | unsigned char *p,*to,*s; | ||
257 | long len; | ||
258 | int inf,tag,xclass; | ||
259 | int i; | ||
260 | |||
261 | if ((a == NULL) || ((*a) == NULL)) | ||
262 | { | ||
263 | if ((ret=ASN1_INTEGER_new()) == NULL) return(NULL); | ||
264 | ret->type=V_ASN1_INTEGER; | ||
171 | } | 265 | } |
172 | else | 266 | else |
267 | ret=(*a); | ||
268 | |||
269 | p= *pp; | ||
270 | inf=ASN1_get_object(&p,&len,&tag,&xclass,length); | ||
271 | if (inf & 0x80) | ||
272 | { | ||
273 | i=ASN1_R_BAD_OBJECT_HEADER; | ||
274 | goto err; | ||
275 | } | ||
276 | |||
277 | if (tag != V_ASN1_INTEGER) | ||
173 | { | 278 | { |
279 | i=ASN1_R_EXPECTING_AN_INTEGER; | ||
280 | goto err; | ||
281 | } | ||
282 | |||
283 | /* We must Malloc stuff, even for 0 bytes otherwise it | ||
284 | * signifies a missing NULL parameter. */ | ||
285 | s=(unsigned char *)Malloc((int)len+1); | ||
286 | if (s == NULL) | ||
287 | { | ||
288 | i=ERR_R_MALLOC_FAILURE; | ||
289 | goto err; | ||
290 | } | ||
291 | to=s; | ||
174 | ret->type=V_ASN1_INTEGER; | 292 | ret->type=V_ASN1_INTEGER; |
175 | if ((*p == 0) && (len != 1)) | 293 | if ((*p == 0) && (len != 1)) |
176 | { | 294 | { |
@@ -179,7 +297,6 @@ long length; | |||
179 | } | 297 | } |
180 | memcpy(s,p,(int)len); | 298 | memcpy(s,p,(int)len); |
181 | p+=len; | 299 | p+=len; |
182 | } | ||
183 | 300 | ||
184 | if (ret->data != NULL) Free((char *)ret->data); | 301 | if (ret->data != NULL) Free((char *)ret->data); |
185 | ret->data=s; | 302 | ret->data=s; |
@@ -188,15 +305,13 @@ long length; | |||
188 | *pp=p; | 305 | *pp=p; |
189 | return(ret); | 306 | return(ret); |
190 | err: | 307 | err: |
191 | ASN1err(ASN1_F_D2I_ASN1_INTEGER,i); | 308 | ASN1err(ASN1_F_D2I_ASN1_UINTEGER,i); |
192 | if ((ret != NULL) && ((a == NULL) || (*a != ret))) | 309 | if ((ret != NULL) && ((a == NULL) || (*a != ret))) |
193 | ASN1_INTEGER_free(ret); | 310 | ASN1_INTEGER_free(ret); |
194 | return(NULL); | 311 | return(NULL); |
195 | } | 312 | } |
196 | 313 | ||
197 | int ASN1_INTEGER_set(a,v) | 314 | int ASN1_INTEGER_set(ASN1_INTEGER *a, long v) |
198 | ASN1_INTEGER *a; | ||
199 | long v; | ||
200 | { | 315 | { |
201 | int i,j,k; | 316 | int i,j,k; |
202 | unsigned char buf[sizeof(long)+1]; | 317 | unsigned char buf[sizeof(long)+1]; |
@@ -229,15 +344,13 @@ long v; | |||
229 | d>>=8; | 344 | d>>=8; |
230 | } | 345 | } |
231 | j=0; | 346 | j=0; |
232 | if (v < 0) a->data[j++]=0; | ||
233 | for (k=i-1; k >=0; k--) | 347 | for (k=i-1; k >=0; k--) |
234 | a->data[j++]=buf[k]; | 348 | a->data[j++]=buf[k]; |
235 | a->length=j; | 349 | a->length=j; |
236 | return(1); | 350 | return(1); |
237 | } | 351 | } |
238 | 352 | ||
239 | long ASN1_INTEGER_get(a) | 353 | long ASN1_INTEGER_get(ASN1_INTEGER *a) |
240 | ASN1_INTEGER *a; | ||
241 | { | 354 | { |
242 | int neg=0,i; | 355 | int neg=0,i; |
243 | long r=0; | 356 | long r=0; |
@@ -266,9 +379,7 @@ ASN1_INTEGER *a; | |||
266 | return(r); | 379 | return(r); |
267 | } | 380 | } |
268 | 381 | ||
269 | ASN1_INTEGER *BN_to_ASN1_INTEGER(bn,ai) | 382 | ASN1_INTEGER *BN_to_ASN1_INTEGER(BIGNUM *bn, ASN1_INTEGER *ai) |
270 | BIGNUM *bn; | ||
271 | ASN1_INTEGER *ai; | ||
272 | { | 383 | { |
273 | ASN1_INTEGER *ret; | 384 | ASN1_INTEGER *ret; |
274 | int len,j; | 385 | int len,j; |
@@ -279,10 +390,11 @@ ASN1_INTEGER *ai; | |||
279 | ret=ai; | 390 | ret=ai; |
280 | if (ret == NULL) | 391 | if (ret == NULL) |
281 | { | 392 | { |
282 | ASN1err(ASN1_F_BN_TO_ASN1_INTEGER,ASN1_R_ERROR_STACK); | 393 | ASN1err(ASN1_F_BN_TO_ASN1_INTEGER,ERR_R_NESTED_ASN1_ERROR); |
283 | goto err; | 394 | goto err; |
284 | } | 395 | } |
285 | ret->type=V_ASN1_INTEGER; | 396 | if(bn->neg) ret->type = V_ASN1_NEG_INTEGER; |
397 | else ret->type=V_ASN1_INTEGER; | ||
286 | j=BN_num_bits(bn); | 398 | j=BN_num_bits(bn); |
287 | len=((j == 0)?0:((j/8)+1)); | 399 | len=((j == 0)?0:((j/8)+1)); |
288 | ret->data=(unsigned char *)Malloc(len+4); | 400 | ret->data=(unsigned char *)Malloc(len+4); |
@@ -293,13 +405,12 @@ err: | |||
293 | return(NULL); | 405 | return(NULL); |
294 | } | 406 | } |
295 | 407 | ||
296 | BIGNUM *ASN1_INTEGER_to_BN(ai,bn) | 408 | BIGNUM *ASN1_INTEGER_to_BN(ASN1_INTEGER *ai, BIGNUM *bn) |
297 | ASN1_INTEGER *ai; | ||
298 | BIGNUM *bn; | ||
299 | { | 409 | { |
300 | BIGNUM *ret; | 410 | BIGNUM *ret; |
301 | 411 | ||
302 | if ((ret=BN_bin2bn(ai->data,ai->length,bn)) == NULL) | 412 | if ((ret=BN_bin2bn(ai->data,ai->length,bn)) == NULL) |
303 | ASN1err(ASN1_F_ASN1_INTEGER_TO_BN,ASN1_R_BN_LIB); | 413 | ASN1err(ASN1_F_ASN1_INTEGER_TO_BN,ASN1_R_BN_LIB); |
414 | if(ai->type == V_ASN1_NEG_INTEGER) bn->neg = 1; | ||
304 | return(ret); | 415 | return(ret); |
305 | } | 416 | } |