diff options
| author | jsing <> | 2021-12-25 07:48:09 +0000 |
|---|---|---|
| committer | jsing <> | 2021-12-25 07:48:09 +0000 |
| commit | 9bcf656c985ee0377aa1efaa225459e511cda945 (patch) | |
| tree | 68a3da4cb7576478b9c6fd4a2df6e20ee0dc2f00 /src | |
| parent | 45bffde06d55be8341ae2ab97fd9f6d173eea785 (diff) | |
| download | openbsd-9bcf656c985ee0377aa1efaa225459e511cda945.tar.gz openbsd-9bcf656c985ee0377aa1efaa225459e511cda945.tar.bz2 openbsd-9bcf656c985ee0377aa1efaa225459e511cda945.zip | |
Move ASN1_<type>_* functions to the top, encoding/decoding to the bottom.
No functional change.
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/libcrypto/asn1/a_bitstr.c | 244 | ||||
| -rw-r--r-- | src/lib/libcrypto/asn1/a_int.c | 286 | ||||
| -rw-r--r-- | src/lib/libcrypto/asn1/a_object.c | 100 | ||||
| -rw-r--r-- | src/lib/libcrypto/asn1/a_time.c | 28 |
4 files changed, 329 insertions, 329 deletions
diff --git a/src/lib/libcrypto/asn1/a_bitstr.c b/src/lib/libcrypto/asn1/a_bitstr.c index 68cefee4a2..207d80ab38 100644 --- a/src/lib/libcrypto/asn1/a_bitstr.c +++ b/src/lib/libcrypto/asn1/a_bitstr.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: a_bitstr.c,v 1.31 2021/12/15 18:00:31 jsing Exp $ */ | 1 | /* $OpenBSD: a_bitstr.c,v 1.32 2021/12/25 07:48:09 jsing Exp $ */ |
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 3 | * All rights reserved. | 3 | * All rights reserved. |
| 4 | * | 4 | * |
| @@ -71,6 +71,127 @@ ASN1_BIT_STRING_set(ASN1_BIT_STRING *x, unsigned char *d, int len) | |||
| 71 | } | 71 | } |
| 72 | 72 | ||
| 73 | int | 73 | int |
| 74 | ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value) | ||
| 75 | { | ||
| 76 | int w, v, iv; | ||
| 77 | unsigned char *c; | ||
| 78 | |||
| 79 | w = n/8; | ||
| 80 | v = 1 << (7 - (n & 0x07)); | ||
| 81 | iv = ~v; | ||
| 82 | if (!value) | ||
| 83 | v = 0; | ||
| 84 | |||
| 85 | if (a == NULL) | ||
| 86 | return 0; | ||
| 87 | |||
| 88 | a->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear, set on write */ | ||
| 89 | |||
| 90 | if ((a->length < (w + 1)) || (a->data == NULL)) { | ||
| 91 | if (!value) | ||
| 92 | return(1); /* Don't need to set */ | ||
| 93 | if ((c = recallocarray(a->data, a->length, w + 1, 1)) == NULL) { | ||
| 94 | ASN1error(ERR_R_MALLOC_FAILURE); | ||
| 95 | return 0; | ||
| 96 | } | ||
| 97 | a->data = c; | ||
| 98 | a->length = w + 1; | ||
| 99 | } | ||
| 100 | a->data[w] = ((a->data[w]) & iv) | v; | ||
| 101 | while ((a->length > 0) && (a->data[a->length - 1] == 0)) | ||
| 102 | a->length--; | ||
| 103 | |||
| 104 | return (1); | ||
| 105 | } | ||
| 106 | |||
| 107 | int | ||
| 108 | ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING *a, int n) | ||
| 109 | { | ||
| 110 | int w, v; | ||
| 111 | |||
| 112 | w = n / 8; | ||
| 113 | v = 1 << (7 - (n & 0x07)); | ||
| 114 | if ((a == NULL) || (a->length < (w + 1)) || (a->data == NULL)) | ||
| 115 | return (0); | ||
| 116 | return ((a->data[w] & v) != 0); | ||
| 117 | } | ||
| 118 | |||
| 119 | /* | ||
| 120 | * Checks if the given bit string contains only bits specified by | ||
| 121 | * the flags vector. Returns 0 if there is at least one bit set in 'a' | ||
| 122 | * which is not specified in 'flags', 1 otherwise. | ||
| 123 | * 'len' is the length of 'flags'. | ||
| 124 | */ | ||
| 125 | int | ||
| 126 | ASN1_BIT_STRING_check(const ASN1_BIT_STRING *a, const unsigned char *flags, | ||
| 127 | int flags_len) | ||
| 128 | { | ||
| 129 | int i, ok; | ||
| 130 | |||
| 131 | /* Check if there is one bit set at all. */ | ||
| 132 | if (!a || !a->data) | ||
| 133 | return 1; | ||
| 134 | |||
| 135 | /* Check each byte of the internal representation of the bit string. */ | ||
| 136 | ok = 1; | ||
| 137 | for (i = 0; i < a->length && ok; ++i) { | ||
| 138 | unsigned char mask = i < flags_len ? ~flags[i] : 0xff; | ||
| 139 | /* We are done if there is an unneeded bit set. */ | ||
| 140 | ok = (a->data[i] & mask) == 0; | ||
| 141 | } | ||
| 142 | return ok; | ||
| 143 | } | ||
| 144 | |||
| 145 | int | ||
| 146 | ASN1_BIT_STRING_name_print(BIO *out, ASN1_BIT_STRING *bs, | ||
| 147 | BIT_STRING_BITNAME *tbl, int indent) | ||
| 148 | { | ||
| 149 | BIT_STRING_BITNAME *bnam; | ||
| 150 | char first = 1; | ||
| 151 | |||
| 152 | BIO_printf(out, "%*s", indent, ""); | ||
| 153 | for (bnam = tbl; bnam->lname; bnam++) { | ||
| 154 | if (ASN1_BIT_STRING_get_bit(bs, bnam->bitnum)) { | ||
| 155 | if (!first) | ||
| 156 | BIO_puts(out, ", "); | ||
| 157 | BIO_puts(out, bnam->lname); | ||
| 158 | first = 0; | ||
| 159 | } | ||
| 160 | } | ||
| 161 | BIO_puts(out, "\n"); | ||
| 162 | return 1; | ||
| 163 | } | ||
| 164 | |||
| 165 | int | ||
| 166 | ASN1_BIT_STRING_set_asc(ASN1_BIT_STRING *bs, const char *name, int value, | ||
| 167 | BIT_STRING_BITNAME *tbl) | ||
| 168 | { | ||
| 169 | int bitnum; | ||
| 170 | |||
| 171 | bitnum = ASN1_BIT_STRING_num_asc(name, tbl); | ||
| 172 | if (bitnum < 0) | ||
| 173 | return 0; | ||
| 174 | if (bs) { | ||
| 175 | if (!ASN1_BIT_STRING_set_bit(bs, bitnum, value)) | ||
| 176 | return 0; | ||
| 177 | } | ||
| 178 | return 1; | ||
| 179 | } | ||
| 180 | |||
| 181 | int | ||
| 182 | ASN1_BIT_STRING_num_asc(const char *name, BIT_STRING_BITNAME *tbl) | ||
| 183 | { | ||
| 184 | BIT_STRING_BITNAME *bnam; | ||
| 185 | |||
| 186 | for (bnam = tbl; bnam->lname; bnam++) { | ||
| 187 | if (!strcmp(bnam->sname, name) || | ||
| 188 | !strcmp(bnam->lname, name)) | ||
| 189 | return bnam->bitnum; | ||
| 190 | } | ||
| 191 | return -1; | ||
| 192 | } | ||
| 193 | |||
| 194 | int | ||
| 74 | i2c_ASN1_BIT_STRING(ASN1_BIT_STRING *a, unsigned char **pp) | 195 | i2c_ASN1_BIT_STRING(ASN1_BIT_STRING *a, unsigned char **pp) |
| 75 | { | 196 | { |
| 76 | int ret, j, bits, len; | 197 | int ret, j, bits, len; |
| @@ -192,124 +313,3 @@ c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a, const unsigned char **pp, long len) | |||
| 192 | 313 | ||
| 193 | return (NULL); | 314 | return (NULL); |
| 194 | } | 315 | } |
| 195 | |||
| 196 | int | ||
| 197 | ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value) | ||
| 198 | { | ||
| 199 | int w, v, iv; | ||
| 200 | unsigned char *c; | ||
| 201 | |||
| 202 | w = n/8; | ||
| 203 | v = 1 << (7 - (n & 0x07)); | ||
| 204 | iv = ~v; | ||
| 205 | if (!value) | ||
| 206 | v = 0; | ||
| 207 | |||
| 208 | if (a == NULL) | ||
| 209 | return 0; | ||
| 210 | |||
| 211 | a->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear, set on write */ | ||
| 212 | |||
| 213 | if ((a->length < (w + 1)) || (a->data == NULL)) { | ||
| 214 | if (!value) | ||
| 215 | return(1); /* Don't need to set */ | ||
| 216 | if ((c = recallocarray(a->data, a->length, w + 1, 1)) == NULL) { | ||
| 217 | ASN1error(ERR_R_MALLOC_FAILURE); | ||
| 218 | return 0; | ||
| 219 | } | ||
| 220 | a->data = c; | ||
| 221 | a->length = w + 1; | ||
| 222 | } | ||
| 223 | a->data[w] = ((a->data[w]) & iv) | v; | ||
| 224 | while ((a->length > 0) && (a->data[a->length - 1] == 0)) | ||
| 225 | a->length--; | ||
| 226 | |||
| 227 | return (1); | ||
| 228 | } | ||
| 229 | |||
| 230 | int | ||
| 231 | ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING *a, int n) | ||
| 232 | { | ||
| 233 | int w, v; | ||
| 234 | |||
| 235 | w = n / 8; | ||
| 236 | v = 1 << (7 - (n & 0x07)); | ||
| 237 | if ((a == NULL) || (a->length < (w + 1)) || (a->data == NULL)) | ||
| 238 | return (0); | ||
| 239 | return ((a->data[w] & v) != 0); | ||
| 240 | } | ||
| 241 | |||
| 242 | /* | ||
| 243 | * Checks if the given bit string contains only bits specified by | ||
| 244 | * the flags vector. Returns 0 if there is at least one bit set in 'a' | ||
| 245 | * which is not specified in 'flags', 1 otherwise. | ||
| 246 | * 'len' is the length of 'flags'. | ||
| 247 | */ | ||
| 248 | int | ||
| 249 | ASN1_BIT_STRING_check(const ASN1_BIT_STRING *a, const unsigned char *flags, | ||
| 250 | int flags_len) | ||
| 251 | { | ||
| 252 | int i, ok; | ||
| 253 | |||
| 254 | /* Check if there is one bit set at all. */ | ||
| 255 | if (!a || !a->data) | ||
| 256 | return 1; | ||
| 257 | |||
| 258 | /* Check each byte of the internal representation of the bit string. */ | ||
| 259 | ok = 1; | ||
| 260 | for (i = 0; i < a->length && ok; ++i) { | ||
| 261 | unsigned char mask = i < flags_len ? ~flags[i] : 0xff; | ||
| 262 | /* We are done if there is an unneeded bit set. */ | ||
| 263 | ok = (a->data[i] & mask) == 0; | ||
| 264 | } | ||
| 265 | return ok; | ||
| 266 | } | ||
| 267 | |||
| 268 | int | ||
| 269 | ASN1_BIT_STRING_name_print(BIO *out, ASN1_BIT_STRING *bs, | ||
| 270 | BIT_STRING_BITNAME *tbl, int indent) | ||
| 271 | { | ||
| 272 | BIT_STRING_BITNAME *bnam; | ||
| 273 | char first = 1; | ||
| 274 | |||
| 275 | BIO_printf(out, "%*s", indent, ""); | ||
| 276 | for (bnam = tbl; bnam->lname; bnam++) { | ||
| 277 | if (ASN1_BIT_STRING_get_bit(bs, bnam->bitnum)) { | ||
| 278 | if (!first) | ||
| 279 | BIO_puts(out, ", "); | ||
| 280 | BIO_puts(out, bnam->lname); | ||
| 281 | first = 0; | ||
| 282 | } | ||
| 283 | } | ||
| 284 | BIO_puts(out, "\n"); | ||
| 285 | return 1; | ||
| 286 | } | ||
| 287 | |||
| 288 | int | ||
| 289 | ASN1_BIT_STRING_set_asc(ASN1_BIT_STRING *bs, const char *name, int value, | ||
| 290 | BIT_STRING_BITNAME *tbl) | ||
| 291 | { | ||
| 292 | int bitnum; | ||
| 293 | |||
| 294 | bitnum = ASN1_BIT_STRING_num_asc(name, tbl); | ||
| 295 | if (bitnum < 0) | ||
| 296 | return 0; | ||
| 297 | if (bs) { | ||
| 298 | if (!ASN1_BIT_STRING_set_bit(bs, bitnum, value)) | ||
| 299 | return 0; | ||
| 300 | } | ||
| 301 | return 1; | ||
| 302 | } | ||
| 303 | |||
| 304 | int | ||
| 305 | ASN1_BIT_STRING_num_asc(const char *name, BIT_STRING_BITNAME *tbl) | ||
| 306 | { | ||
| 307 | BIT_STRING_BITNAME *bnam; | ||
| 308 | |||
| 309 | for (bnam = tbl; bnam->lname; bnam++) { | ||
| 310 | if (!strcmp(bnam->sname, name) || | ||
| 311 | !strcmp(bnam->lname, name)) | ||
| 312 | return bnam->bitnum; | ||
| 313 | } | ||
| 314 | return -1; | ||
| 315 | } | ||
diff --git a/src/lib/libcrypto/asn1/a_int.c b/src/lib/libcrypto/asn1/a_int.c index 314bd2b369..9218a17c11 100644 --- a/src/lib/libcrypto/asn1/a_int.c +++ b/src/lib/libcrypto/asn1/a_int.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: a_int.c,v 1.35 2021/12/15 18:00:31 jsing Exp $ */ | 1 | /* $OpenBSD: a_int.c,v 1.36 2021/12/25 07:48:09 jsing Exp $ */ |
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 3 | * All rights reserved. | 3 | * All rights reserved. |
| 4 | * | 4 | * |
| @@ -103,6 +103,148 @@ ASN1_INTEGER_cmp(const ASN1_INTEGER *x, const ASN1_INTEGER *y) | |||
| 103 | } | 103 | } |
| 104 | 104 | ||
| 105 | int | 105 | int |
| 106 | ASN1_INTEGER_set(ASN1_INTEGER *a, long v) | ||
| 107 | { | ||
| 108 | int j, k; | ||
| 109 | unsigned int i; | ||
| 110 | unsigned char buf[sizeof(long) + 1]; | ||
| 111 | long d; | ||
| 112 | |||
| 113 | a->type = V_ASN1_INTEGER; | ||
| 114 | /* XXX ssl/ssl_asn1.c:i2d_SSL_SESSION() depends upon this bound vae */ | ||
| 115 | if (a->length < (int)(sizeof(long) + 1)) { | ||
| 116 | free(a->data); | ||
| 117 | a->data = calloc(1, sizeof(long) + 1); | ||
| 118 | } | ||
| 119 | if (a->data == NULL) { | ||
| 120 | ASN1error(ERR_R_MALLOC_FAILURE); | ||
| 121 | return (0); | ||
| 122 | } | ||
| 123 | d = v; | ||
| 124 | if (d < 0) { | ||
| 125 | d = -d; | ||
| 126 | a->type = V_ASN1_NEG_INTEGER; | ||
| 127 | } | ||
| 128 | |||
| 129 | for (i = 0; i < sizeof(long); i++) { | ||
| 130 | if (d == 0) | ||
| 131 | break; | ||
| 132 | buf[i] = (int)d & 0xff; | ||
| 133 | d >>= 8; | ||
| 134 | } | ||
| 135 | j = 0; | ||
| 136 | for (k = i - 1; k >= 0; k--) | ||
| 137 | a->data[j++] = buf[k]; | ||
| 138 | a->length = j; | ||
| 139 | return (1); | ||
| 140 | } | ||
| 141 | |||
| 142 | /* | ||
| 143 | * XXX this particular API is a gibbering eidrich horror that makes it | ||
| 144 | * impossible to determine valid return cases from errors.. "a bit | ||
| 145 | * ugly" is preserved for posterity, unfortunately this is probably | ||
| 146 | * unfixable without changing public API | ||
| 147 | */ | ||
| 148 | long | ||
| 149 | ASN1_INTEGER_get(const ASN1_INTEGER *a) | ||
| 150 | { | ||
| 151 | int neg = 0, i; | ||
| 152 | unsigned long r = 0; | ||
| 153 | |||
| 154 | if (a == NULL) | ||
| 155 | return (0L); | ||
| 156 | i = a->type; | ||
| 157 | if (i == V_ASN1_NEG_INTEGER) | ||
| 158 | neg = 1; | ||
| 159 | else if (i != V_ASN1_INTEGER) | ||
| 160 | return -1; | ||
| 161 | |||
| 162 | if (!ASN1_INTEGER_valid(a)) | ||
| 163 | return -1; /* XXX best effort */ | ||
| 164 | |||
| 165 | if (a->length > (int)sizeof(long)) { | ||
| 166 | /* hmm... a bit ugly, return all ones */ | ||
| 167 | return -1; | ||
| 168 | } | ||
| 169 | if (a->data == NULL) | ||
| 170 | return 0; | ||
| 171 | |||
| 172 | for (i = 0; i < a->length; i++) { | ||
| 173 | r <<= 8; | ||
| 174 | r |= (unsigned char)a->data[i]; | ||
| 175 | } | ||
| 176 | |||
| 177 | if (r > LONG_MAX) | ||
| 178 | return -1; | ||
| 179 | |||
| 180 | if (neg) | ||
| 181 | return -(long)r; | ||
| 182 | return (long)r; | ||
| 183 | } | ||
| 184 | |||
| 185 | ASN1_INTEGER * | ||
| 186 | BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai) | ||
| 187 | { | ||
| 188 | ASN1_INTEGER *ret; | ||
| 189 | int len, j; | ||
| 190 | |||
| 191 | if (ai == NULL) | ||
| 192 | ret = ASN1_INTEGER_new(); | ||
| 193 | else | ||
| 194 | ret = ai; | ||
| 195 | if (ret == NULL) { | ||
| 196 | ASN1error(ERR_R_NESTED_ASN1_ERROR); | ||
| 197 | goto err; | ||
| 198 | } | ||
| 199 | |||
| 200 | if (!ASN1_INTEGER_valid(ret)) | ||
| 201 | goto err; | ||
| 202 | |||
| 203 | if (BN_is_negative(bn)) | ||
| 204 | ret->type = V_ASN1_NEG_INTEGER; | ||
| 205 | else | ||
| 206 | ret->type = V_ASN1_INTEGER; | ||
| 207 | j = BN_num_bits(bn); | ||
| 208 | len = ((j == 0) ? 0 : ((j / 8) + 1)); | ||
| 209 | if (ret->length < len + 4) { | ||
| 210 | unsigned char *new_data = realloc(ret->data, len + 4); | ||
| 211 | if (!new_data) { | ||
| 212 | ASN1error(ERR_R_MALLOC_FAILURE); | ||
| 213 | goto err; | ||
| 214 | } | ||
| 215 | ret->data = new_data; | ||
| 216 | } | ||
| 217 | ret->length = BN_bn2bin(bn, ret->data); | ||
| 218 | |||
| 219 | /* Correct zero case */ | ||
| 220 | if (!ret->length) { | ||
| 221 | ret->data[0] = 0; | ||
| 222 | ret->length = 1; | ||
| 223 | } | ||
| 224 | return (ret); | ||
| 225 | |||
| 226 | err: | ||
| 227 | if (ret != ai) | ||
| 228 | ASN1_INTEGER_free(ret); | ||
| 229 | return (NULL); | ||
| 230 | } | ||
| 231 | |||
| 232 | BIGNUM * | ||
| 233 | ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn) | ||
| 234 | { | ||
| 235 | BIGNUM *ret; | ||
| 236 | |||
| 237 | if (!ASN1_INTEGER_valid(ai)) | ||
| 238 | return (NULL); | ||
| 239 | |||
| 240 | if ((ret = BN_bin2bn(ai->data, ai->length, bn)) == NULL) | ||
| 241 | ASN1error(ASN1_R_BN_LIB); | ||
| 242 | else if (ai->type == V_ASN1_NEG_INTEGER) | ||
| 243 | BN_set_negative(ret, 1); | ||
| 244 | return (ret); | ||
| 245 | } | ||
| 246 | |||
| 247 | int | ||
| 106 | i2a_ASN1_INTEGER(BIO *bp, const ASN1_INTEGER *a) | 248 | i2a_ASN1_INTEGER(BIO *bp, const ASN1_INTEGER *a) |
| 107 | { | 249 | { |
| 108 | int i, n = 0; | 250 | int i, n = 0; |
| @@ -499,145 +641,3 @@ err: | |||
| 499 | ASN1_INTEGER_free(ret); | 641 | ASN1_INTEGER_free(ret); |
| 500 | return (NULL); | 642 | return (NULL); |
| 501 | } | 643 | } |
| 502 | |||
| 503 | int | ||
| 504 | ASN1_INTEGER_set(ASN1_INTEGER *a, long v) | ||
| 505 | { | ||
| 506 | int j, k; | ||
| 507 | unsigned int i; | ||
| 508 | unsigned char buf[sizeof(long) + 1]; | ||
| 509 | long d; | ||
| 510 | |||
| 511 | a->type = V_ASN1_INTEGER; | ||
| 512 | /* XXX ssl/ssl_asn1.c:i2d_SSL_SESSION() depends upon this bound vae */ | ||
| 513 | if (a->length < (int)(sizeof(long) + 1)) { | ||
| 514 | free(a->data); | ||
| 515 | a->data = calloc(1, sizeof(long) + 1); | ||
| 516 | } | ||
| 517 | if (a->data == NULL) { | ||
| 518 | ASN1error(ERR_R_MALLOC_FAILURE); | ||
| 519 | return (0); | ||
| 520 | } | ||
| 521 | d = v; | ||
| 522 | if (d < 0) { | ||
| 523 | d = -d; | ||
| 524 | a->type = V_ASN1_NEG_INTEGER; | ||
| 525 | } | ||
| 526 | |||
| 527 | for (i = 0; i < sizeof(long); i++) { | ||
| 528 | if (d == 0) | ||
| 529 | break; | ||
| 530 | buf[i] = (int)d & 0xff; | ||
| 531 | d >>= 8; | ||
| 532 | } | ||
| 533 | j = 0; | ||
| 534 | for (k = i - 1; k >= 0; k--) | ||
| 535 | a->data[j++] = buf[k]; | ||
| 536 | a->length = j; | ||
| 537 | return (1); | ||
| 538 | } | ||
| 539 | |||
| 540 | /* | ||
| 541 | * XXX this particular API is a gibbering eidrich horror that makes it | ||
| 542 | * impossible to determine valid return cases from errors.. "a bit | ||
| 543 | * ugly" is preserved for posterity, unfortunately this is probably | ||
| 544 | * unfixable without changing public API | ||
| 545 | */ | ||
| 546 | long | ||
| 547 | ASN1_INTEGER_get(const ASN1_INTEGER *a) | ||
| 548 | { | ||
| 549 | int neg = 0, i; | ||
| 550 | unsigned long r = 0; | ||
| 551 | |||
| 552 | if (a == NULL) | ||
| 553 | return (0L); | ||
| 554 | i = a->type; | ||
| 555 | if (i == V_ASN1_NEG_INTEGER) | ||
| 556 | neg = 1; | ||
| 557 | else if (i != V_ASN1_INTEGER) | ||
| 558 | return -1; | ||
| 559 | |||
| 560 | if (!ASN1_INTEGER_valid(a)) | ||
| 561 | return -1; /* XXX best effort */ | ||
| 562 | |||
| 563 | if (a->length > (int)sizeof(long)) { | ||
| 564 | /* hmm... a bit ugly, return all ones */ | ||
| 565 | return -1; | ||
| 566 | } | ||
| 567 | if (a->data == NULL) | ||
| 568 | return 0; | ||
| 569 | |||
| 570 | for (i = 0; i < a->length; i++) { | ||
| 571 | r <<= 8; | ||
| 572 | r |= (unsigned char)a->data[i]; | ||
| 573 | } | ||
| 574 | |||
| 575 | if (r > LONG_MAX) | ||
| 576 | return -1; | ||
| 577 | |||
| 578 | if (neg) | ||
| 579 | return -(long)r; | ||
| 580 | return (long)r; | ||
| 581 | } | ||
| 582 | |||
| 583 | ASN1_INTEGER * | ||
| 584 | BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai) | ||
| 585 | { | ||
| 586 | ASN1_INTEGER *ret; | ||
| 587 | int len, j; | ||
| 588 | |||
| 589 | if (ai == NULL) | ||
| 590 | ret = ASN1_INTEGER_new(); | ||
| 591 | else | ||
| 592 | ret = ai; | ||
| 593 | if (ret == NULL) { | ||
| 594 | ASN1error(ERR_R_NESTED_ASN1_ERROR); | ||
| 595 | goto err; | ||
| 596 | } | ||
| 597 | |||
| 598 | if (!ASN1_INTEGER_valid(ret)) | ||
| 599 | goto err; | ||
| 600 | |||
| 601 | if (BN_is_negative(bn)) | ||
| 602 | ret->type = V_ASN1_NEG_INTEGER; | ||
| 603 | else | ||
| 604 | ret->type = V_ASN1_INTEGER; | ||
| 605 | j = BN_num_bits(bn); | ||
| 606 | len = ((j == 0) ? 0 : ((j / 8) + 1)); | ||
| 607 | if (ret->length < len + 4) { | ||
| 608 | unsigned char *new_data = realloc(ret->data, len + 4); | ||
| 609 | if (!new_data) { | ||
| 610 | ASN1error(ERR_R_MALLOC_FAILURE); | ||
| 611 | goto err; | ||
| 612 | } | ||
| 613 | ret->data = new_data; | ||
| 614 | } | ||
| 615 | ret->length = BN_bn2bin(bn, ret->data); | ||
| 616 | |||
| 617 | /* Correct zero case */ | ||
| 618 | if (!ret->length) { | ||
| 619 | ret->data[0] = 0; | ||
| 620 | ret->length = 1; | ||
| 621 | } | ||
| 622 | return (ret); | ||
| 623 | |||
| 624 | err: | ||
| 625 | if (ret != ai) | ||
| 626 | ASN1_INTEGER_free(ret); | ||
| 627 | return (NULL); | ||
| 628 | } | ||
| 629 | |||
| 630 | BIGNUM * | ||
| 631 | ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn) | ||
| 632 | { | ||
| 633 | BIGNUM *ret; | ||
| 634 | |||
| 635 | if (!ASN1_INTEGER_valid(ai)) | ||
| 636 | return (NULL); | ||
| 637 | |||
| 638 | if ((ret = BN_bin2bn(ai->data, ai->length, bn)) == NULL) | ||
| 639 | ASN1error(ASN1_R_BN_LIB); | ||
| 640 | else if (ai->type == V_ASN1_NEG_INTEGER) | ||
| 641 | BN_set_negative(ret, 1); | ||
| 642 | return (ret); | ||
| 643 | } | ||
diff --git a/src/lib/libcrypto/asn1/a_object.c b/src/lib/libcrypto/asn1/a_object.c index 9b3bae0442..cc4f5dd424 100644 --- a/src/lib/libcrypto/asn1/a_object.c +++ b/src/lib/libcrypto/asn1/a_object.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: a_object.c,v 1.33 2021/12/03 16:58:11 jsing Exp $ */ | 1 | /* $OpenBSD: a_object.c,v 1.34 2021/12/25 07:48:09 jsing Exp $ */ |
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 3 | * All rights reserved. | 3 | * All rights reserved. |
| 4 | * | 4 | * |
| @@ -66,6 +66,55 @@ | |||
| 66 | #include <openssl/buffer.h> | 66 | #include <openssl/buffer.h> |
| 67 | #include <openssl/objects.h> | 67 | #include <openssl/objects.h> |
| 68 | 68 | ||
| 69 | ASN1_OBJECT * | ||
| 70 | ASN1_OBJECT_new(void) | ||
| 71 | { | ||
| 72 | ASN1_OBJECT *a; | ||
| 73 | |||
| 74 | if ((a = calloc(1, sizeof(ASN1_OBJECT))) == NULL) { | ||
| 75 | ASN1error(ERR_R_MALLOC_FAILURE); | ||
| 76 | return (NULL); | ||
| 77 | } | ||
| 78 | a->flags = ASN1_OBJECT_FLAG_DYNAMIC; | ||
| 79 | |||
| 80 | return a; | ||
| 81 | } | ||
| 82 | |||
| 83 | void | ||
| 84 | ASN1_OBJECT_free(ASN1_OBJECT *a) | ||
| 85 | { | ||
| 86 | if (a == NULL) | ||
| 87 | return; | ||
| 88 | if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) { | ||
| 89 | free((void *)a->sn); | ||
| 90 | free((void *)a->ln); | ||
| 91 | a->sn = a->ln = NULL; | ||
| 92 | } | ||
| 93 | if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) { | ||
| 94 | freezero((void *)a->data, a->length); | ||
| 95 | a->data = NULL; | ||
| 96 | a->length = 0; | ||
| 97 | } | ||
| 98 | if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC) | ||
| 99 | free(a); | ||
| 100 | } | ||
| 101 | |||
| 102 | ASN1_OBJECT * | ||
| 103 | ASN1_OBJECT_create(int nid, unsigned char *data, int len, | ||
| 104 | const char *sn, const char *ln) | ||
| 105 | { | ||
| 106 | ASN1_OBJECT o; | ||
| 107 | |||
| 108 | o.sn = sn; | ||
| 109 | o.ln = ln; | ||
| 110 | o.data = data; | ||
| 111 | o.nid = nid; | ||
| 112 | o.length = len; | ||
| 113 | o.flags = ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS | | ||
| 114 | ASN1_OBJECT_FLAG_DYNAMIC_DATA; | ||
| 115 | return (OBJ_dup(&o)); | ||
| 116 | } | ||
| 117 | |||
| 69 | int | 118 | int |
| 70 | i2d_ASN1_OBJECT(const ASN1_OBJECT *a, unsigned char **pp) | 119 | i2d_ASN1_OBJECT(const ASN1_OBJECT *a, unsigned char **pp) |
| 71 | { | 120 | { |
| @@ -350,52 +399,3 @@ err: | |||
| 350 | ASN1_OBJECT_free(ret); | 399 | ASN1_OBJECT_free(ret); |
| 351 | return (NULL); | 400 | return (NULL); |
| 352 | } | 401 | } |
| 353 | |||
| 354 | ASN1_OBJECT * | ||
| 355 | ASN1_OBJECT_new(void) | ||
| 356 | { | ||
| 357 | ASN1_OBJECT *a; | ||
| 358 | |||
| 359 | if ((a = calloc(1, sizeof(ASN1_OBJECT))) == NULL) { | ||
| 360 | ASN1error(ERR_R_MALLOC_FAILURE); | ||
| 361 | return (NULL); | ||
| 362 | } | ||
| 363 | a->flags = ASN1_OBJECT_FLAG_DYNAMIC; | ||
| 364 | |||
| 365 | return a; | ||
| 366 | } | ||
| 367 | |||
| 368 | void | ||
| 369 | ASN1_OBJECT_free(ASN1_OBJECT *a) | ||
| 370 | { | ||
| 371 | if (a == NULL) | ||
| 372 | return; | ||
| 373 | if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) { | ||
| 374 | free((void *)a->sn); | ||
| 375 | free((void *)a->ln); | ||
| 376 | a->sn = a->ln = NULL; | ||
| 377 | } | ||
| 378 | if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) { | ||
| 379 | freezero((void *)a->data, a->length); | ||
| 380 | a->data = NULL; | ||
| 381 | a->length = 0; | ||
| 382 | } | ||
| 383 | if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC) | ||
| 384 | free(a); | ||
| 385 | } | ||
| 386 | |||
| 387 | ASN1_OBJECT * | ||
| 388 | ASN1_OBJECT_create(int nid, unsigned char *data, int len, | ||
| 389 | const char *sn, const char *ln) | ||
| 390 | { | ||
| 391 | ASN1_OBJECT o; | ||
| 392 | |||
| 393 | o.sn = sn; | ||
| 394 | o.ln = ln; | ||
| 395 | o.data = data; | ||
| 396 | o.nid = nid; | ||
| 397 | o.length = len; | ||
| 398 | o.flags = ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS | | ||
| 399 | ASN1_OBJECT_FLAG_DYNAMIC_DATA; | ||
| 400 | return (OBJ_dup(&o)); | ||
| 401 | } | ||
diff --git a/src/lib/libcrypto/asn1/a_time.c b/src/lib/libcrypto/asn1/a_time.c index 79bcbffa26..cd6a790cac 100644 --- a/src/lib/libcrypto/asn1/a_time.c +++ b/src/lib/libcrypto/asn1/a_time.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: a_time.c,v 1.32 2021/11/06 07:52:22 jsing Exp $ */ | 1 | /* $OpenBSD: a_time.c,v 1.33 2021/12/25 07:48:09 jsing Exp $ */ |
| 2 | /* ==================================================================== | 2 | /* ==================================================================== |
| 3 | * Copyright (c) 1999 The OpenSSL Project. All rights reserved. | 3 | * Copyright (c) 1999 The OpenSSL Project. All rights reserved. |
| 4 | * | 4 | * |
| @@ -81,19 +81,6 @@ const ASN1_ITEM ASN1_TIME_it = { | |||
| 81 | }; | 81 | }; |
| 82 | 82 | ||
| 83 | ASN1_TIME * | 83 | ASN1_TIME * |
| 84 | d2i_ASN1_TIME(ASN1_TIME **a, const unsigned char **in, long len) | ||
| 85 | { | ||
| 86 | return (ASN1_TIME *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, | ||
| 87 | &ASN1_TIME_it); | ||
| 88 | } | ||
| 89 | |||
| 90 | int | ||
| 91 | i2d_ASN1_TIME(ASN1_TIME *a, unsigned char **out) | ||
| 92 | { | ||
| 93 | return ASN1_item_i2d((ASN1_VALUE *)a, out, &ASN1_TIME_it); | ||
| 94 | } | ||
| 95 | |||
| 96 | ASN1_TIME * | ||
| 97 | ASN1_TIME_new(void) | 84 | ASN1_TIME_new(void) |
| 98 | { | 85 | { |
| 99 | return (ASN1_TIME *)ASN1_item_new(&ASN1_TIME_it); | 86 | return (ASN1_TIME *)ASN1_item_new(&ASN1_TIME_it); |
| @@ -132,3 +119,16 @@ ASN1_TIME_diff(int *pday, int *psec, const ASN1_TIME *from, const ASN1_TIME *to) | |||
| 132 | 119 | ||
| 133 | return OPENSSL_gmtime_diff(pday, psec, &tm_from, &tm_to); | 120 | return OPENSSL_gmtime_diff(pday, psec, &tm_from, &tm_to); |
| 134 | } | 121 | } |
| 122 | |||
| 123 | ASN1_TIME * | ||
| 124 | d2i_ASN1_TIME(ASN1_TIME **a, const unsigned char **in, long len) | ||
| 125 | { | ||
| 126 | return (ASN1_TIME *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, | ||
| 127 | &ASN1_TIME_it); | ||
| 128 | } | ||
| 129 | |||
| 130 | int | ||
| 131 | i2d_ASN1_TIME(ASN1_TIME *a, unsigned char **out) | ||
| 132 | { | ||
| 133 | return ASN1_item_i2d((ASN1_VALUE *)a, out, &ASN1_TIME_it); | ||
| 134 | } | ||
