/src/openssl30/crypto/asn1/x_long.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved. | 
| 3 |  |  * | 
| 4 |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use | 
| 5 |  |  * this file except in compliance with the License.  You can obtain a copy | 
| 6 |  |  * in the file LICENSE in the source distribution or at | 
| 7 |  |  * https://www.openssl.org/source/license.html | 
| 8 |  |  */ | 
| 9 |  |  | 
| 10 |  | #include <stdio.h> | 
| 11 |  | #include "internal/cryptlib.h" | 
| 12 |  | #include <openssl/asn1t.h> | 
| 13 |  |  | 
| 14 | 83.2k | #define COPY_SIZE(a, b) (sizeof(a) < sizeof(b) ? sizeof(a) : sizeof(b)) | 
| 15 |  |  | 
| 16 |  | /* | 
| 17 |  |  * Custom primitive type for long handling. This converts between an | 
| 18 |  |  * ASN1_INTEGER and a long directly. | 
| 19 |  |  */ | 
| 20 |  |  | 
| 21 |  | static int long_new(ASN1_VALUE **pval, const ASN1_ITEM *it); | 
| 22 |  | static void long_free(ASN1_VALUE **pval, const ASN1_ITEM *it); | 
| 23 |  |  | 
| 24 |  | static int long_i2c(const ASN1_VALUE **pval, unsigned char *cont, int *putype, | 
| 25 |  |                     const ASN1_ITEM *it); | 
| 26 |  | static int long_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, | 
| 27 |  |                     int utype, char *free_cont, const ASN1_ITEM *it); | 
| 28 |  | static int long_print(BIO *out, const ASN1_VALUE **pval, const ASN1_ITEM *it, | 
| 29 |  |                       int indent, const ASN1_PCTX *pctx); | 
| 30 |  |  | 
| 31 |  | static ASN1_PRIMITIVE_FUNCS long_pf = { | 
| 32 |  |     NULL, 0, | 
| 33 |  |     long_new, | 
| 34 |  |     long_free, | 
| 35 |  |     long_free,                  /* Clear should set to initial value */ | 
| 36 |  |     long_c2i, | 
| 37 |  |     long_i2c, | 
| 38 |  |     long_print | 
| 39 |  | }; | 
| 40 |  |  | 
| 41 | 24.0k | ASN1_ITEM_start(LONG) | 
| 42 | 24.0k |         ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &long_pf, ASN1_LONG_UNDEF, "LONG" | 
| 43 | 24.0k | ASN1_ITEM_end(LONG) | 
| 44 |  |  | 
| 45 | 24.0k | ASN1_ITEM_start(ZLONG) | 
| 46 | 24.0k |         ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &long_pf, 0, "ZLONG" | 
| 47 | 24.0k | ASN1_ITEM_end(ZLONG) | 
| 48 |  |  | 
| 49 |  | static int long_new(ASN1_VALUE **pval, const ASN1_ITEM *it) | 
| 50 | 0 | { | 
| 51 | 0 |     memcpy(pval, &it->size, COPY_SIZE(*pval, it->size)); | 
| 52 | 0 |     return 1; | 
| 53 | 0 | } | 
| 54 |  |  | 
| 55 |  | static void long_free(ASN1_VALUE **pval, const ASN1_ITEM *it) | 
| 56 | 73.1k | { | 
| 57 | 73.1k |     memcpy(pval, &it->size, COPY_SIZE(*pval, it->size)); | 
| 58 | 73.1k | } | 
| 59 |  |  | 
| 60 |  | /* | 
| 61 |  |  * Originally BN_num_bits_word was called to perform this operation, but | 
| 62 |  |  * trouble is that there is no guarantee that sizeof(long) equals to | 
| 63 |  |  * sizeof(BN_ULONG). BN_ULONG is a configurable type that can be as wide | 
| 64 |  |  * as long, but also double or half... | 
| 65 |  |  */ | 
| 66 |  | static int num_bits_ulong(unsigned long value) | 
| 67 | 6.00k | { | 
| 68 | 6.00k |     size_t i; | 
| 69 | 6.00k |     unsigned long ret = 0; | 
| 70 |  |  | 
| 71 |  |     /* | 
| 72 |  |      * It is argued that *on average* constant counter loop performs | 
| 73 |  |      * not worse [if not better] than one with conditional break or | 
| 74 |  |      * mask-n-table-lookup-style, because of branch misprediction | 
| 75 |  |      * penalties. | 
| 76 |  |      */ | 
| 77 | 390k |     for (i = 0; i < sizeof(value) * 8; i++) { | 
| 78 | 384k |         ret += (value != 0); | 
| 79 | 384k |         value >>= 1; | 
| 80 | 384k |     } | 
| 81 |  |  | 
| 82 | 6.00k |     return (int)ret; | 
| 83 | 6.00k | } | 
| 84 |  |  | 
| 85 |  | static int long_i2c(const ASN1_VALUE **pval, unsigned char *cont, int *putype, | 
| 86 |  |                     const ASN1_ITEM *it) | 
| 87 | 6.00k | { | 
| 88 | 6.00k |     long ltmp; | 
| 89 | 6.00k |     unsigned long utmp, sign; | 
| 90 | 6.00k |     int clen, pad, i; | 
| 91 |  |  | 
| 92 | 6.00k |     memcpy(<mp, pval, COPY_SIZE(*pval, ltmp)); | 
| 93 | 6.00k |     if (ltmp == it->size) | 
| 94 | 0 |         return -1; | 
| 95 |  |     /* | 
| 96 |  |      * Convert the long to positive: we subtract one if negative so we can | 
| 97 |  |      * cleanly handle the padding if only the MSB of the leading octet is | 
| 98 |  |      * set. | 
| 99 |  |      */ | 
| 100 | 6.00k |     if (ltmp < 0) { | 
| 101 | 2.71k |         sign = 0xff; | 
| 102 | 2.71k |         utmp = 0 - (unsigned long)ltmp - 1; | 
| 103 | 3.29k |     } else { | 
| 104 | 3.29k |         sign = 0; | 
| 105 | 3.29k |         utmp = ltmp; | 
| 106 | 3.29k |     } | 
| 107 | 6.00k |     clen = num_bits_ulong(utmp); | 
| 108 |  |     /* If MSB of leading octet set we need to pad */ | 
| 109 | 6.00k |     if (!(clen & 0x7)) | 
| 110 | 1.05k |         pad = 1; | 
| 111 | 4.95k |     else | 
| 112 | 4.95k |         pad = 0; | 
| 113 |  |  | 
| 114 |  |     /* Convert number of bits to number of octets */ | 
| 115 | 6.00k |     clen = (clen + 7) >> 3; | 
| 116 |  |  | 
| 117 | 6.00k |     if (cont != NULL) { | 
| 118 | 2.00k |         if (pad) | 
| 119 | 352 |             *cont++ = (unsigned char)sign; | 
| 120 | 11.0k |         for (i = clen - 1; i >= 0; i--) { | 
| 121 | 9.00k |             cont[i] = (unsigned char)(utmp ^ sign); | 
| 122 | 9.00k |             utmp >>= 8; | 
| 123 | 9.00k |         } | 
| 124 | 2.00k |     } | 
| 125 | 6.00k |     return clen + pad; | 
| 126 | 6.00k | } | 
| 127 |  |  | 
| 128 |  | static int long_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, | 
| 129 |  |                     int utype, char *free_cont, const ASN1_ITEM *it) | 
| 130 | 3.34k | { | 
| 131 | 3.34k |     int i; | 
| 132 | 3.34k |     long ltmp; | 
| 133 | 3.34k |     unsigned long utmp = 0, sign = 0x100; | 
| 134 |  |  | 
| 135 | 3.34k |     if (len > 1) { | 
| 136 |  |         /* | 
| 137 |  |          * Check possible pad byte.  Worst case, we're skipping past actual | 
| 138 |  |          * content, but since that's only with 0x00 and 0xff and we set neg | 
| 139 |  |          * accordingly, the result will be correct in the end anyway. | 
| 140 |  |          */ | 
| 141 | 3.17k |         switch (cont[0]) { | 
| 142 | 800 |         case 0xff: | 
| 143 | 800 |             cont++; | 
| 144 | 800 |             len--; | 
| 145 | 800 |             sign = 0xff; | 
| 146 | 800 |             break; | 
| 147 | 652 |         case 0: | 
| 148 | 652 |             cont++; | 
| 149 | 652 |             len--; | 
| 150 | 652 |             sign = 0; | 
| 151 | 652 |             break; | 
| 152 | 3.17k |         } | 
| 153 | 3.17k |     } | 
| 154 | 3.34k |     if (len > (int)sizeof(long)) { | 
| 155 | 306 |         ERR_raise(ERR_LIB_ASN1, ASN1_R_INTEGER_TOO_LARGE_FOR_LONG); | 
| 156 | 306 |         return 0; | 
| 157 | 306 |     } | 
| 158 |  |  | 
| 159 | 3.03k |     if (sign == 0x100) { | 
| 160 |  |         /* Is it negative? */ | 
| 161 | 1.67k |         if (len && (cont[0] & 0x80)) | 
| 162 | 782 |             sign = 0xff; | 
| 163 | 892 |         else | 
| 164 | 892 |             sign = 0; | 
| 165 | 1.67k |     } else if (((sign ^ cont[0]) & 0x80) == 0) { /* same sign bit? */ | 
| 166 | 426 |         ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_PADDING); | 
| 167 | 426 |         return 0; | 
| 168 | 426 |     } | 
| 169 | 2.60k |     utmp = 0; | 
| 170 | 16.3k |     for (i = 0; i < len; i++) { | 
| 171 | 13.7k |         utmp <<= 8; | 
| 172 | 13.7k |         utmp |= cont[i] ^ sign; | 
| 173 | 13.7k |     } | 
| 174 | 2.60k |     ltmp = (long)utmp; | 
| 175 | 2.60k |     if (ltmp < 0) { | 
| 176 | 588 |         ERR_raise(ERR_LIB_ASN1, ASN1_R_INTEGER_TOO_LARGE_FOR_LONG); | 
| 177 | 588 |         return 0; | 
| 178 | 588 |     } | 
| 179 | 2.02k |     if (sign) | 
| 180 | 906 |         ltmp = -ltmp - 1; | 
| 181 | 2.02k |     if (ltmp == it->size) { | 
| 182 | 10 |         ERR_raise(ERR_LIB_ASN1, ASN1_R_INTEGER_TOO_LARGE_FOR_LONG); | 
| 183 | 10 |         return 0; | 
| 184 | 10 |     } | 
| 185 | 2.01k |     memcpy(pval, <mp, COPY_SIZE(*pval, ltmp)); | 
| 186 | 2.01k |     return 1; | 
| 187 | 2.02k | } | 
| 188 |  |  | 
| 189 |  | static int long_print(BIO *out, const ASN1_VALUE **pval, const ASN1_ITEM *it, | 
| 190 |  |                       int indent, const ASN1_PCTX *pctx) | 
| 191 | 2.00k | { | 
| 192 | 2.00k |     long l; | 
| 193 |  |  | 
| 194 | 2.00k |     memcpy(&l, pval, COPY_SIZE(*pval, l)); | 
| 195 | 2.00k |     return BIO_printf(out, "%ld\n", l); | 
| 196 | 2.00k | } |