diff options
Diffstat (limited to 'src/lib/libcrypto/asn1/a_int.c')
| -rw-r--r-- | src/lib/libcrypto/asn1/a_int.c | 347 |
1 files changed, 189 insertions, 158 deletions
diff --git a/src/lib/libcrypto/asn1/a_int.c b/src/lib/libcrypto/asn1/a_int.c index cc2b985fa1..ef47eb106b 100644 --- a/src/lib/libcrypto/asn1/a_int.c +++ b/src/lib/libcrypto/asn1/a_int.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 |
| @@ -61,12 +61,17 @@ | |||
| 61 | #include <openssl/asn1.h> | 61 | #include <openssl/asn1.h> |
| 62 | #include <openssl/bn.h> | 62 | #include <openssl/bn.h> |
| 63 | 63 | ||
| 64 | ASN1_INTEGER *ASN1_INTEGER_dup(const ASN1_INTEGER *x) | 64 | ASN1_INTEGER * |
| 65 | { return M_ASN1_INTEGER_dup(x);} | 65 | ASN1_INTEGER_dup(const ASN1_INTEGER *x) |
| 66 | { | ||
| 67 | return M_ASN1_INTEGER_dup(x); | ||
| 68 | } | ||
| 66 | 69 | ||
| 67 | int ASN1_INTEGER_cmp(const ASN1_INTEGER *x, const ASN1_INTEGER *y) | 70 | int |
| 68 | { | 71 | ASN1_INTEGER_cmp(const ASN1_INTEGER *x, const ASN1_INTEGER *y) |
| 72 | { | ||
| 69 | int neg, ret; | 73 | int neg, ret; |
| 74 | |||
| 70 | /* Compare signs */ | 75 | /* Compare signs */ |
| 71 | neg = x->type & V_ASN1_NEG; | 76 | neg = x->type & V_ASN1_NEG; |
| 72 | if (neg != (y->type & V_ASN1_NEG)) { | 77 | if (neg != (y->type & V_ASN1_NEG)) { |
| @@ -83,23 +88,23 @@ int ASN1_INTEGER_cmp(const ASN1_INTEGER *x, const ASN1_INTEGER *y) | |||
| 83 | else | 88 | else |
| 84 | return ret; | 89 | return ret; |
| 85 | } | 90 | } |
| 86 | |||
| 87 | 91 | ||
| 88 | /* | 92 | |
| 93 | /* | ||
| 89 | * This converts an ASN1 INTEGER into its content encoding. | 94 | * This converts an ASN1 INTEGER into its content encoding. |
| 90 | * The internal representation is an ASN1_STRING whose data is a big endian | 95 | * The internal representation is an ASN1_STRING whose data is a big endian |
| 91 | * representation of the value, ignoring the sign. The sign is determined by | 96 | * representation of the value, ignoring the sign. The sign is determined by |
| 92 | * the type: V_ASN1_INTEGER for positive and V_ASN1_NEG_INTEGER for negative. | 97 | * the type: V_ASN1_INTEGER for positive and V_ASN1_NEG_INTEGER for negative. |
| 93 | * | 98 | * |
| 94 | * Positive integers are no problem: they are almost the same as the DER | 99 | * Positive integers are no problem: they are almost the same as the DER |
| 95 | * encoding, except if the first byte is >= 0x80 we need to add a zero pad. | 100 | * encoding, except if the first byte is >= 0x80 we need to add a zero pad. |
| 96 | * | 101 | * |
| 97 | * Negative integers are a bit trickier... | 102 | * Negative integers are a bit trickier... |
| 98 | * The DER representation of negative integers is in 2s complement form. | 103 | * The DER representation of negative integers is in 2s complement form. |
| 99 | * The internal form is converted by complementing each octet and finally | 104 | * The internal form is converted by complementing each octet and finally |
| 100 | * adding one to the result. This can be done less messily with a little trick. | 105 | * adding one to the result. This can be done less messily with a little trick. |
| 101 | * If the internal form has trailing zeroes then they will become FF by the | 106 | * If the internal form has trailing zeroes then they will become FF by the |
| 102 | * complement and 0 by the add one (due to carry) so just copy as many trailing | 107 | * complement and 0 by the add one (due to carry) so just copy as many trailing |
| 103 | * zeros to the destination as there are in the source. The carry will add one | 108 | * zeros to the destination as there are in the source. The carry will add one |
| 104 | * to the last none zero octet: so complement this octet and add one and finally | 109 | * to the last none zero octet: so complement this octet and add one and finally |
| 105 | * complement any left over until you get to the start of the string. | 110 | * complement any left over until you get to the start of the string. |
| @@ -110,102 +115,109 @@ int ASN1_INTEGER_cmp(const ASN1_INTEGER *x, const ASN1_INTEGER *y) | |||
| 110 | * followed by optional zeros isn't padded. | 115 | * followed by optional zeros isn't padded. |
| 111 | */ | 116 | */ |
| 112 | 117 | ||
| 113 | int i2c_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp) | 118 | int |
| 119 | i2c_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp) | ||
| 114 | { | 120 | { |
| 115 | int pad=0,ret,i,neg; | 121 | int pad = 0, ret, i, neg; |
| 116 | unsigned char *p,*n,pb=0; | 122 | unsigned char *p, *n, pb = 0; |
| 117 | 123 | ||
| 118 | if (a == NULL) return(0); | 124 | if (a == NULL) |
| 119 | neg=a->type & V_ASN1_NEG; | 125 | return (0); |
| 126 | neg = a->type & V_ASN1_NEG; | ||
| 120 | if (a->length == 0) | 127 | if (a->length == 0) |
| 121 | ret=1; | 128 | ret = 1; |
| 122 | else { | 129 | else { |
| 123 | ret=a->length; | 130 | ret = a->length; |
| 124 | i=a->data[0]; | 131 | i = a->data[0]; |
| 125 | if (!neg && (i > 127)) { | 132 | if (!neg && (i > 127)) { |
| 126 | pad=1; | 133 | pad = 1; |
| 127 | pb=0; | 134 | pb = 0; |
| 128 | } else if(neg) { | 135 | } else if (neg) { |
| 129 | if(i>128) { | 136 | if (i > 128) { |
| 130 | pad=1; | 137 | pad = 1; |
| 131 | pb=0xFF; | 138 | pb = 0xFF; |
| 132 | } else if(i == 128) { | 139 | } else if (i == 128) { |
| 133 | /* | 140 | /* |
| 134 | * Special case: if any other bytes non zero we pad: | 141 | * Special case: if any other bytes non zero we pad: |
| 135 | * otherwise we don't. | 142 | * otherwise we don't. |
| 136 | */ | 143 | */ |
| 137 | for(i = 1; i < a->length; i++) if(a->data[i]) { | 144 | for (i = 1; i < a->length; i++) if (a->data[i]) { |
| 138 | pad=1; | 145 | pad = 1; |
| 139 | pb=0xFF; | 146 | pb = 0xFF; |
| 140 | break; | 147 | break; |
| 148 | } | ||
| 141 | } | 149 | } |
| 142 | } | 150 | } |
| 151 | ret += pad; | ||
| 143 | } | 152 | } |
| 144 | ret+=pad; | 153 | if (pp == NULL) |
| 145 | } | 154 | return (ret); |
| 146 | if (pp == NULL) return(ret); | ||
| 147 | p= *pp; | 155 | p= *pp; |
| 148 | 156 | ||
| 149 | if (pad) *(p++)=pb; | 157 | if (pad) |
| 150 | if (a->length == 0) *(p++)=0; | 158 | *(p++) = pb; |
| 151 | else if (!neg) memcpy(p,a->data,(unsigned int)a->length); | 159 | if (a->length == 0) |
| 160 | *(p++) = 0; | ||
| 161 | else if (!neg) | ||
| 162 | memcpy(p, a->data, (unsigned int)a->length); | ||
| 152 | else { | 163 | else { |
| 153 | /* Begin at the end of the encoding */ | 164 | /* Begin at the end of the encoding */ |
| 154 | n=a->data + a->length - 1; | 165 | n = a->data + a->length - 1; |
| 155 | p += a->length - 1; | 166 | p += a->length - 1; |
| 156 | i = a->length; | 167 | i = a->length; |
| 157 | /* Copy zeros to destination as long as source is zero */ | 168 | /* Copy zeros to destination as long as source is zero */ |
| 158 | while(!*n) { | 169 | while (!*n) { |
| 159 | *(p--) = 0; | 170 | *(p--) = 0; |
| 160 | n--; | 171 | n--; |
| 161 | i--; | 172 | i--; |
| 162 | } | 173 | } |
| 163 | /* Complement and increment next octet */ | 174 | /* Complement and increment next octet */ |
| 164 | *(p--) = ((*(n--)) ^ 0xff) + 1; | 175 | *(p--) = ((*(n--)) ^ 0xff) + 1; |
| 165 | i--; | 176 | i--; |
| 166 | /* Complement any octets left */ | 177 | /* Complement any octets left */ |
| 167 | for(;i > 0; i--) *(p--) = *(n--) ^ 0xff; | 178 | for(; i > 0; i--) |
| 168 | } | 179 | *(p--) = *(n--) ^ 0xff; |
| 180 | } | ||
| 169 | 181 | ||
| 170 | *pp+=ret; | 182 | *pp += ret; |
| 171 | return(ret); | 183 | return (ret); |
| 172 | } | 184 | } |
| 173 | 185 | ||
| 174 | /* Convert just ASN1 INTEGER content octets to ASN1_INTEGER structure */ | 186 | /* Convert just ASN1 INTEGER content octets to ASN1_INTEGER structure */ |
| 175 | 187 | ||
| 176 | ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp, | 188 | ASN1_INTEGER * |
| 177 | long len) | 189 | c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp, long len) |
| 178 | { | 190 | { |
| 179 | ASN1_INTEGER *ret=NULL; | 191 | ASN1_INTEGER *ret = NULL; |
| 180 | const unsigned char *p, *pend; | 192 | const unsigned char *p, *pend; |
| 181 | unsigned char *to,*s; | 193 | unsigned char *to, *s; |
| 182 | int i; | 194 | int i; |
| 183 | 195 | ||
| 184 | if ((a == NULL) || ((*a) == NULL)) { | 196 | if ((a == NULL) || ((*a) == NULL)) { |
| 185 | if ((ret=M_ASN1_INTEGER_new()) == NULL) return(NULL); | 197 | if ((ret = M_ASN1_INTEGER_new()) == NULL) |
| 186 | ret->type=V_ASN1_INTEGER; | 198 | return (NULL); |
| 187 | } | 199 | ret->type = V_ASN1_INTEGER; |
| 188 | else | 200 | } else |
| 189 | ret=(*a); | 201 | ret = (*a); |
| 190 | 202 | ||
| 191 | p= *pp; | 203 | p= *pp; |
| 192 | pend = p + len; | 204 | pend = p + len; |
| 193 | 205 | ||
| 194 | /* We must malloc stuff, even for 0 bytes otherwise it | 206 | /* We must malloc stuff, even for 0 bytes otherwise it |
| 195 | * signifies a missing NULL parameter. */ | 207 | * signifies a missing NULL parameter. */ |
| 196 | s=(unsigned char *)malloc((int)len+1); | 208 | s = (unsigned char *)malloc((int)len + 1); |
| 197 | if (s == NULL) { | 209 | if (s == NULL) { |
| 198 | i=ERR_R_MALLOC_FAILURE; | 210 | i = ERR_R_MALLOC_FAILURE; |
| 199 | goto err; | 211 | goto err; |
| 200 | } | 212 | } |
| 201 | to=s; | 213 | to = s; |
| 202 | if(!len) { | 214 | if (!len) { |
| 203 | /* Strictly speaking this is an illegal INTEGER but we | 215 | /* Strictly speaking this is an illegal INTEGER but we |
| 204 | * tolerate it. | 216 | * tolerate it. |
| 205 | */ | 217 | */ |
| 206 | ret->type=V_ASN1_INTEGER; | 218 | ret->type = V_ASN1_INTEGER; |
| 207 | } else if (*p & 0x80) /* a negative number */ { | 219 | } else if (*p & 0x80) /* a negative number */ { |
| 208 | ret->type=V_ASN1_NEG_INTEGER; | 220 | ret->type = V_ASN1_NEG_INTEGER; |
| 209 | if ((*p == 0xff) && (len != 1)) { | 221 | if ((*p == 0xff) && (len != 1)) { |
| 210 | p++; | 222 | p++; |
| 211 | len--; | 223 | len--; |
| @@ -225,35 +237,39 @@ ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp, | |||
| 225 | * a 1. This is treated separately because it is the only case | 237 | * a 1. This is treated separately because it is the only case |
| 226 | * where the number of bytes is larger than len. | 238 | * where the number of bytes is larger than len. |
| 227 | */ | 239 | */ |
| 228 | if(!i) { | 240 | if (!i) { |
| 229 | *s = 1; | 241 | *s = 1; |
| 230 | s[len] = 0; | 242 | s[len] = 0; |
| 231 | len++; | 243 | len++; |
| 232 | } else { | 244 | } else { |
| 233 | *(to--) = (*(p--) ^ 0xff) + 1; | 245 | *(to--) = (*(p--) ^ 0xff) + 1; |
| 234 | i--; | 246 | i--; |
| 235 | for(;i > 0; i--) *(to--) = *(p--) ^ 0xff; | 247 | for(; i > 0; i--) |
| 248 | *(to--) = *(p--) ^ 0xff; | ||
| 236 | } | 249 | } |
| 237 | } else { | 250 | } else { |
| 238 | ret->type=V_ASN1_INTEGER; | 251 | ret->type = V_ASN1_INTEGER; |
| 239 | if ((*p == 0) && (len != 1)) { | 252 | if ((*p == 0) && (len != 1)) { |
| 240 | p++; | 253 | p++; |
| 241 | len--; | 254 | len--; |
| 242 | } | 255 | } |
| 243 | memcpy(s,p,(int)len); | 256 | memcpy(s, p, (int)len); |
| 244 | } | 257 | } |
| 245 | 258 | ||
| 246 | if (ret->data != NULL) free(ret->data); | 259 | if (ret->data != NULL) |
| 247 | ret->data=s; | 260 | free(ret->data); |
| 248 | ret->length=(int)len; | 261 | ret->data = s; |
| 249 | if (a != NULL) (*a)=ret; | 262 | ret->length = (int)len; |
| 250 | *pp=pend; | 263 | if (a != NULL) |
| 251 | return(ret); | 264 | (*a) = ret; |
| 265 | *pp = pend; | ||
| 266 | return (ret); | ||
| 267 | |||
| 252 | err: | 268 | err: |
| 253 | ASN1err(ASN1_F_C2I_ASN1_INTEGER,i); | 269 | ASN1err(ASN1_F_C2I_ASN1_INTEGER, i); |
| 254 | if ((ret != NULL) && ((a == NULL) || (*a != ret))) | 270 | if ((ret != NULL) && ((a == NULL) || (*a != ret))) |
| 255 | M_ASN1_INTEGER_free(ret); | 271 | M_ASN1_INTEGER_free(ret); |
| 256 | return(NULL); | 272 | return (NULL); |
| 257 | } | 273 | } |
| 258 | 274 | ||
| 259 | 275 | ||
| @@ -262,112 +278,120 @@ err: | |||
| 262 | * with its MSB set as negative (it doesn't add a padding zero). | 278 | * with its MSB set as negative (it doesn't add a padding zero). |
| 263 | */ | 279 | */ |
| 264 | 280 | ||
| 265 | ASN1_INTEGER *d2i_ASN1_UINTEGER(ASN1_INTEGER **a, const unsigned char **pp, | 281 | ASN1_INTEGER * |
| 266 | long length) | 282 | d2i_ASN1_UINTEGER(ASN1_INTEGER **a, const unsigned char **pp, long length) |
| 267 | { | 283 | { |
| 268 | ASN1_INTEGER *ret=NULL; | 284 | ASN1_INTEGER *ret = NULL; |
| 269 | const unsigned char *p; | 285 | const unsigned char *p; |
| 270 | unsigned char *s; | 286 | unsigned char *s; |
| 271 | long len; | 287 | long len; |
| 272 | int inf,tag,xclass; | 288 | int inf, tag, xclass; |
| 273 | int i; | 289 | int i; |
| 274 | 290 | ||
| 275 | if ((a == NULL) || ((*a) == NULL)) { | 291 | if ((a == NULL) || ((*a) == NULL)) { |
| 276 | if ((ret=M_ASN1_INTEGER_new()) == NULL) return(NULL); | 292 | if ((ret = M_ASN1_INTEGER_new()) == NULL) |
| 277 | ret->type=V_ASN1_INTEGER; | 293 | return (NULL); |
| 294 | ret->type = V_ASN1_INTEGER; | ||
| 278 | } else | 295 | } else |
| 279 | ret=(*a); | 296 | ret = (*a); |
| 280 | 297 | ||
| 281 | p= *pp; | 298 | p = *pp; |
| 282 | inf=ASN1_get_object(&p,&len,&tag,&xclass,length); | 299 | inf = ASN1_get_object(&p, &len, &tag, &xclass, length); |
| 283 | if (inf & 0x80) { | 300 | if (inf & 0x80) { |
| 284 | i=ASN1_R_BAD_OBJECT_HEADER; | 301 | i = ASN1_R_BAD_OBJECT_HEADER; |
| 285 | goto err; | 302 | goto err; |
| 286 | } | 303 | } |
| 287 | 304 | ||
| 288 | if (tag != V_ASN1_INTEGER) { | 305 | if (tag != V_ASN1_INTEGER) { |
| 289 | i=ASN1_R_EXPECTING_AN_INTEGER; | 306 | i = ASN1_R_EXPECTING_AN_INTEGER; |
| 290 | goto err; | 307 | goto err; |
| 291 | } | 308 | } |
| 292 | 309 | ||
| 293 | /* We must malloc stuff, even for 0 bytes otherwise it | 310 | /* We must malloc stuff, even for 0 bytes otherwise it |
| 294 | * signifies a missing NULL parameter. */ | 311 | * signifies a missing NULL parameter. */ |
| 295 | s=(unsigned char *)malloc((int)len+1); | 312 | s = (unsigned char *)malloc((int)len + 1); |
| 296 | if (s == NULL) { | 313 | if (s == NULL) { |
| 297 | i=ERR_R_MALLOC_FAILURE; | 314 | i = ERR_R_MALLOC_FAILURE; |
| 298 | goto err; | 315 | goto err; |
| 299 | } | 316 | } |
| 300 | ret->type=V_ASN1_INTEGER; | 317 | ret->type = V_ASN1_INTEGER; |
| 301 | if(len) { | 318 | if (len) { |
| 302 | if ((*p == 0) && (len != 1)) { | 319 | if ((*p == 0) && (len != 1)) { |
| 303 | p++; | 320 | p++; |
| 304 | len--; | 321 | len--; |
| 305 | } | 322 | } |
| 306 | memcpy(s,p,(int)len); | 323 | memcpy(s, p, (int)len); |
| 307 | p+=len; | 324 | p += len; |
| 308 | } | 325 | } |
| 309 | 326 | ||
| 310 | if (ret->data != NULL) free(ret->data); | 327 | if (ret->data != NULL) |
| 311 | ret->data=s; | 328 | free(ret->data); |
| 312 | ret->length=(int)len; | 329 | ret->data = s; |
| 313 | if (a != NULL) (*a)=ret; | 330 | ret->length = (int)len; |
| 314 | *pp=p; | 331 | if (a != NULL) |
| 315 | return(ret); | 332 | (*a) = ret; |
| 333 | *pp = p; | ||
| 334 | return (ret); | ||
| 335 | |||
| 316 | err: | 336 | err: |
| 317 | ASN1err(ASN1_F_D2I_ASN1_UINTEGER,i); | 337 | ASN1err(ASN1_F_D2I_ASN1_UINTEGER, i); |
| 318 | if ((ret != NULL) && ((a == NULL) || (*a != ret))) | 338 | if ((ret != NULL) && ((a == NULL) || (*a != ret))) |
| 319 | M_ASN1_INTEGER_free(ret); | 339 | M_ASN1_INTEGER_free(ret); |
| 320 | return(NULL); | 340 | return (NULL); |
| 321 | } | 341 | } |
| 322 | 342 | ||
| 323 | int ASN1_INTEGER_set(ASN1_INTEGER *a, long v) | 343 | int |
| 344 | ASN1_INTEGER_set(ASN1_INTEGER *a, long v) | ||
| 324 | { | 345 | { |
| 325 | int j,k; | 346 | int j, k; |
| 326 | unsigned int i; | 347 | unsigned int i; |
| 327 | unsigned char buf[sizeof(long)+1]; | 348 | unsigned char buf[sizeof(long) + 1]; |
| 328 | long d; | 349 | long d; |
| 329 | 350 | ||
| 330 | a->type=V_ASN1_INTEGER; | 351 | a->type = V_ASN1_INTEGER; |
| 331 | if (a->length < (int)(sizeof(long)+1)) { | 352 | if (a->length < (int)(sizeof(long) + 1)) { |
| 332 | if (a->data != NULL) | 353 | if (a->data != NULL) |
| 333 | free(a->data); | 354 | free(a->data); |
| 334 | if ((a->data=(unsigned char *)malloc(sizeof(long)+1)) != NULL) | 355 | if ((a->data = (unsigned char *)malloc(sizeof(long) + 1)) != NULL) |
| 335 | memset((char *)a->data,0,sizeof(long)+1); | 356 | memset((char *)a->data, 0, sizeof(long) + 1); |
| 336 | } | 357 | } |
| 337 | if (a->data == NULL) { | 358 | if (a->data == NULL) { |
| 338 | ASN1err(ASN1_F_ASN1_INTEGER_SET,ERR_R_MALLOC_FAILURE); | 359 | ASN1err(ASN1_F_ASN1_INTEGER_SET, ERR_R_MALLOC_FAILURE); |
| 339 | return(0); | 360 | return (0); |
| 340 | } | 361 | } |
| 341 | d=v; | 362 | d = v; |
| 342 | if (d < 0) { | 363 | if (d < 0) { |
| 343 | d= -d; | 364 | d = -d; |
| 344 | a->type=V_ASN1_NEG_INTEGER; | 365 | a->type = V_ASN1_NEG_INTEGER; |
| 345 | } | 366 | } |
| 346 | 367 | ||
| 347 | for (i=0; i<sizeof(long); i++) { | 368 | for (i = 0; i < sizeof(long); i++) { |
| 348 | if (d == 0) break; | 369 | if (d == 0) |
| 349 | buf[i]=(int)d&0xff; | 370 | break; |
| 350 | d>>=8; | 371 | buf[i] = (int)d & 0xff; |
| 372 | d >>= 8; | ||
| 351 | } | 373 | } |
| 352 | j=0; | 374 | j = 0; |
| 353 | for (k=i-1; k >=0; k--) | 375 | for (k = i - 1; k >= 0; k--) |
| 354 | a->data[j++]=buf[k]; | 376 | a->data[j++] = buf[k]; |
| 355 | a->length=j; | 377 | a->length = j; |
| 356 | return(1); | 378 | return (1); |
| 357 | } | 379 | } |
| 358 | 380 | ||
| 359 | long ASN1_INTEGER_get(const ASN1_INTEGER *a) | 381 | long |
| 382 | ASN1_INTEGER_get(const ASN1_INTEGER *a) | ||
| 360 | { | 383 | { |
| 361 | int neg=0,i; | 384 | int neg = 0, i; |
| 362 | long r=0; | 385 | long r = 0; |
| 363 | 386 | ||
| 364 | if (a == NULL) return(0L); | 387 | if (a == NULL) |
| 365 | i=a->type; | 388 | return (0L); |
| 389 | i = a->type; | ||
| 366 | if (i == V_ASN1_NEG_INTEGER) | 390 | if (i == V_ASN1_NEG_INTEGER) |
| 367 | neg=1; | 391 | neg = 1; |
| 368 | else if (i != V_ASN1_INTEGER) | 392 | else if (i != V_ASN1_INTEGER) |
| 369 | return -1; | 393 | return -1; |
| 370 | 394 | ||
| 371 | if (a->length > (int)sizeof(long)) { | 395 | if (a->length > (int)sizeof(long)) { |
| 372 | /* hmm... a bit ugly, return all ones */ | 396 | /* hmm... a bit ugly, return all ones */ |
| 373 | return -1; | 397 | return -1; |
| @@ -375,61 +399,68 @@ long ASN1_INTEGER_get(const ASN1_INTEGER *a) | |||
| 375 | if (a->data == NULL) | 399 | if (a->data == NULL) |
| 376 | return 0; | 400 | return 0; |
| 377 | 401 | ||
| 378 | for (i=0; i<a->length; i++) { | 402 | for (i = 0; i < a->length; i++) { |
| 379 | r<<=8; | 403 | r <<= 8; |
| 380 | r|=(unsigned char)a->data[i]; | 404 | r |= (unsigned char)a->data[i]; |
| 381 | } | 405 | } |
| 382 | if (neg) r= -r; | 406 | if (neg) |
| 383 | return(r); | 407 | r = -r; |
| 408 | return (r); | ||
| 384 | } | 409 | } |
| 385 | 410 | ||
| 386 | ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai) | 411 | ASN1_INTEGER * |
| 412 | BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai) | ||
| 387 | { | 413 | { |
| 388 | ASN1_INTEGER *ret; | 414 | ASN1_INTEGER *ret; |
| 389 | int len,j; | 415 | int len, j; |
| 390 | 416 | ||
| 391 | if (ai == NULL) | 417 | if (ai == NULL) |
| 392 | ret=M_ASN1_INTEGER_new(); | 418 | ret = M_ASN1_INTEGER_new(); |
| 393 | else | 419 | else |
| 394 | ret=ai; | 420 | ret = ai; |
| 395 | if (ret == NULL) { | 421 | if (ret == NULL) { |
| 396 | ASN1err(ASN1_F_BN_TO_ASN1_INTEGER,ERR_R_NESTED_ASN1_ERROR); | 422 | ASN1err(ASN1_F_BN_TO_ASN1_INTEGER, ERR_R_NESTED_ASN1_ERROR); |
| 397 | goto err; | 423 | goto err; |
| 398 | } | 424 | } |
| 399 | if (BN_is_negative(bn)) | 425 | if (BN_is_negative(bn)) |
| 400 | ret->type = V_ASN1_NEG_INTEGER; | 426 | ret->type = V_ASN1_NEG_INTEGER; |
| 401 | else ret->type=V_ASN1_INTEGER; | 427 | else |
| 402 | j=BN_num_bits(bn); | 428 | ret->type = V_ASN1_INTEGER; |
| 403 | len=((j == 0)?0:((j/8)+1)); | 429 | j = BN_num_bits(bn); |
| 404 | if (ret->length < len+4) { | 430 | len = ((j == 0) ? 0 : ((j / 8) + 1)); |
| 405 | unsigned char *new_data=realloc(ret->data, len+4); | 431 | if (ret->length < len + 4) { |
| 432 | unsigned char *new_data = realloc(ret->data, len + 4); | ||
| 406 | if (!new_data) { | 433 | if (!new_data) { |
| 407 | ASN1err(ASN1_F_BN_TO_ASN1_INTEGER,ERR_R_MALLOC_FAILURE); | 434 | ASN1err(ASN1_F_BN_TO_ASN1_INTEGER, ERR_R_MALLOC_FAILURE); |
| 408 | goto err; | 435 | goto err; |
| 409 | } | 436 | } |
| 410 | ret->data=new_data; | 437 | ret->data = new_data; |
| 411 | } | 438 | } |
| 412 | ret->length=BN_bn2bin(bn,ret->data); | 439 | ret->length = BN_bn2bin(bn, ret->data); |
| 440 | |||
| 413 | /* Correct zero case */ | 441 | /* Correct zero case */ |
| 414 | if(!ret->length) { | 442 | if (!ret->length) { |
| 415 | ret->data[0] = 0; | 443 | ret->data[0] = 0; |
| 416 | ret->length = 1; | 444 | ret->length = 1; |
| 417 | } | 445 | } |
| 418 | return(ret); | 446 | return (ret); |
| 447 | |||
| 419 | err: | 448 | err: |
| 420 | if (ret != ai) M_ASN1_INTEGER_free(ret); | 449 | if (ret != ai) |
| 421 | return(NULL); | 450 | M_ASN1_INTEGER_free(ret); |
| 451 | return (NULL); | ||
| 422 | } | 452 | } |
| 423 | 453 | ||
| 424 | BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn) | 454 | BIGNUM * |
| 455 | ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn) | ||
| 425 | { | 456 | { |
| 426 | BIGNUM *ret; | 457 | BIGNUM *ret; |
| 427 | 458 | ||
| 428 | if ((ret=BN_bin2bn(ai->data,ai->length,bn)) == NULL) | 459 | if ((ret = BN_bin2bn(ai->data, ai->length, bn)) == NULL) |
| 429 | ASN1err(ASN1_F_ASN1_INTEGER_TO_BN,ASN1_R_BN_LIB); | 460 | ASN1err(ASN1_F_ASN1_INTEGER_TO_BN, ASN1_R_BN_LIB); |
| 430 | else if(ai->type == V_ASN1_NEG_INTEGER) | 461 | else if (ai->type == V_ASN1_NEG_INTEGER) |
| 431 | BN_set_negative(ret, 1); | 462 | BN_set_negative(ret, 1); |
| 432 | return(ret); | 463 | return (ret); |
| 433 | } | 464 | } |
| 434 | 465 | ||
| 435 | IMPLEMENT_STACK_OF(ASN1_INTEGER) | 466 | IMPLEMENT_STACK_OF(ASN1_INTEGER) |
