/src/openssl111/crypto/asn1/x_int64.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved. | 
| 3 |  |  * | 
| 4 |  |  * Licensed under the OpenSSL license (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 "internal/numbers.h" | 
| 13 |  | #include <openssl/asn1t.h> | 
| 14 |  | #include <openssl/bn.h> | 
| 15 |  | #include "asn1_local.h" | 
| 16 |  |  | 
| 17 |  | /* | 
| 18 |  |  * Custom primitive types for handling int32_t, int64_t, uint32_t, uint64_t. | 
| 19 |  |  * This converts between an ASN1_INTEGER and those types directly. | 
| 20 |  |  * This is preferred to using the LONG / ZLONG primitives. | 
| 21 |  |  */ | 
| 22 |  |  | 
| 23 |  | /* | 
| 24 |  |  * We abuse the ASN1_ITEM fields |size| as a flags field | 
| 25 |  |  */ | 
| 26 | 94.7k | #define INTxx_FLAG_ZERO_DEFAULT (1<<0) | 
| 27 | 125k | #define INTxx_FLAG_SIGNED       (1<<1) | 
| 28 |  |  | 
| 29 |  | static int uint64_new(ASN1_VALUE **pval, const ASN1_ITEM *it) | 
| 30 | 0 | { | 
| 31 | 0 |     if ((*pval = (ASN1_VALUE *)OPENSSL_zalloc(sizeof(uint64_t))) == NULL) { | 
| 32 | 0 |         ASN1err(ASN1_F_UINT64_NEW, ERR_R_MALLOC_FAILURE); | 
| 33 | 0 |         return 0; | 
| 34 | 0 |     } | 
| 35 | 0 |     return 1; | 
| 36 | 0 | } | 
| 37 |  |  | 
| 38 |  | static void uint64_free(ASN1_VALUE **pval, const ASN1_ITEM *it) | 
| 39 | 0 | { | 
| 40 | 0 |     OPENSSL_free(*pval); | 
| 41 | 0 |     *pval = NULL; | 
| 42 | 0 | } | 
| 43 |  |  | 
| 44 |  | static void uint64_clear(ASN1_VALUE **pval, const ASN1_ITEM *it) | 
| 45 | 0 | { | 
| 46 | 0 |     **(uint64_t **)pval = 0; | 
| 47 | 0 | } | 
| 48 |  |  | 
| 49 |  | static int uint64_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, | 
| 50 |  |                     const ASN1_ITEM *it) | 
| 51 | 0 | { | 
| 52 | 0 |     uint64_t utmp; | 
| 53 | 0 |     int neg = 0; | 
| 54 |  |     /* this exists to bypass broken gcc optimization */ | 
| 55 | 0 |     char *cp = (char *)*pval; | 
| 56 |  |  | 
| 57 |  |     /* use memcpy, because we may not be uint64_t aligned */ | 
| 58 | 0 |     memcpy(&utmp, cp, sizeof(utmp)); | 
| 59 |  | 
 | 
| 60 | 0 |     if ((it->size & INTxx_FLAG_ZERO_DEFAULT) == INTxx_FLAG_ZERO_DEFAULT | 
| 61 | 0 |         && utmp == 0) | 
| 62 | 0 |         return -1; | 
| 63 | 0 |     if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED | 
| 64 | 0 |         && (int64_t)utmp < 0) { | 
| 65 |  |         /* i2c_uint64_int() assumes positive values */ | 
| 66 | 0 |         utmp = 0 - utmp; | 
| 67 | 0 |         neg = 1; | 
| 68 | 0 |     } | 
| 69 |  | 
 | 
| 70 | 0 |     return i2c_uint64_int(cont, utmp, neg); | 
| 71 | 0 | } | 
| 72 |  |  | 
| 73 |  | static int uint64_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, | 
| 74 |  |                     int utype, char *free_cont, const ASN1_ITEM *it) | 
| 75 | 0 | { | 
| 76 | 0 |     uint64_t utmp = 0; | 
| 77 | 0 |     char *cp; | 
| 78 | 0 |     int neg = 0; | 
| 79 |  | 
 | 
| 80 | 0 |     if (*pval == NULL && !uint64_new(pval, it)) | 
| 81 | 0 |         return 0; | 
| 82 |  |  | 
| 83 | 0 |     cp = (char *)*pval; | 
| 84 |  |  | 
| 85 |  |     /* | 
| 86 |  |      * Strictly speaking, zero length is malformed.  However, long_c2i | 
| 87 |  |      * (x_long.c) encodes 0 as a zero length INTEGER (wrongly, of course), | 
| 88 |  |      * so for the sake of backward compatibility, we still decode zero | 
| 89 |  |      * length INTEGERs as the number zero. | 
| 90 |  |      */ | 
| 91 | 0 |     if (len == 0) | 
| 92 | 0 |         goto long_compat; | 
| 93 |  |  | 
| 94 | 0 |     if (!c2i_uint64_int(&utmp, &neg, &cont, len)) | 
| 95 | 0 |         return 0; | 
| 96 | 0 |     if ((it->size & INTxx_FLAG_SIGNED) == 0 && neg) { | 
| 97 | 0 |         ASN1err(ASN1_F_UINT64_C2I, ASN1_R_ILLEGAL_NEGATIVE_VALUE); | 
| 98 | 0 |         return 0; | 
| 99 | 0 |     } | 
| 100 | 0 |     if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED | 
| 101 | 0 |             && !neg && utmp > INT64_MAX) { | 
| 102 | 0 |         ASN1err(ASN1_F_UINT64_C2I, ASN1_R_TOO_LARGE); | 
| 103 | 0 |         return 0; | 
| 104 | 0 |     } | 
| 105 | 0 |     if (neg) | 
| 106 |  |         /* c2i_uint64_int() returns positive values */ | 
| 107 | 0 |         utmp = 0 - utmp; | 
| 108 |  | 
 | 
| 109 | 0 |  long_compat: | 
| 110 | 0 |     memcpy(cp, &utmp, sizeof(utmp)); | 
| 111 | 0 |     return 1; | 
| 112 | 0 | } | 
| 113 |  |  | 
| 114 |  | static int uint64_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it, | 
| 115 |  |                         int indent, const ASN1_PCTX *pctx) | 
| 116 | 0 | { | 
| 117 | 0 |     if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED) | 
| 118 | 0 |         return BIO_printf(out, "%jd\n", **(int64_t **)pval); | 
| 119 | 0 |     return BIO_printf(out, "%ju\n", **(uint64_t **)pval); | 
| 120 | 0 | } | 
| 121 |  |  | 
| 122 |  | /* 32-bit variants */ | 
| 123 |  |  | 
| 124 |  | static int uint32_new(ASN1_VALUE **pval, const ASN1_ITEM *it) | 
| 125 | 0 | { | 
| 126 | 0 |     if ((*pval = (ASN1_VALUE *)OPENSSL_zalloc(sizeof(uint32_t))) == NULL) { | 
| 127 | 0 |         ASN1err(ASN1_F_UINT32_NEW, ERR_R_MALLOC_FAILURE); | 
| 128 | 0 |         return 0; | 
| 129 | 0 |     } | 
| 130 | 0 |     return 1; | 
| 131 | 0 | } | 
| 132 |  |  | 
| 133 |  | static void uint32_free(ASN1_VALUE **pval, const ASN1_ITEM *it) | 
| 134 | 0 | { | 
| 135 | 0 |     OPENSSL_free(*pval); | 
| 136 | 0 |     *pval = NULL; | 
| 137 | 0 | } | 
| 138 |  |  | 
| 139 |  | static void uint32_clear(ASN1_VALUE **pval, const ASN1_ITEM *it) | 
| 140 | 30.3k | { | 
| 141 | 30.3k |     **(uint32_t **)pval = 0; | 
| 142 | 30.3k | } | 
| 143 |  |  | 
| 144 |  | static int uint32_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, | 
| 145 |  |                     const ASN1_ITEM *it) | 
| 146 | 31.5k | { | 
| 147 | 31.5k |     uint32_t utmp; | 
| 148 | 31.5k |     int neg = 0; | 
| 149 |  |     /* this exists to bypass broken gcc optimization */ | 
| 150 | 31.5k |     char *cp = (char *)*pval; | 
| 151 |  |  | 
| 152 |  |     /* use memcpy, because we may not be uint32_t aligned */ | 
| 153 | 31.5k |     memcpy(&utmp, cp, sizeof(utmp)); | 
| 154 |  |  | 
| 155 | 31.5k |     if ((it->size & INTxx_FLAG_ZERO_DEFAULT) == INTxx_FLAG_ZERO_DEFAULT | 
| 156 | 31.5k |         && utmp == 0) | 
| 157 | 0 |         return -1; | 
| 158 | 31.5k |     if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED | 
| 159 | 31.5k |         && (int32_t)utmp < 0) { | 
| 160 |  |         /* i2c_uint64_int() assumes positive values */ | 
| 161 | 7.41k |         utmp = 0 - utmp; | 
| 162 | 7.41k |         neg = 1; | 
| 163 | 7.41k |     } | 
| 164 |  |  | 
| 165 | 31.5k |     return i2c_uint64_int(cont, (uint64_t)utmp, neg); | 
| 166 | 31.5k | } | 
| 167 |  |  | 
| 168 |  | /* | 
| 169 |  |  * Absolute value of INT32_MIN: we can't just use -INT32_MIN as it produces | 
| 170 |  |  * overflow warnings. | 
| 171 |  |  */ | 
| 172 |  |  | 
| 173 | 5.74k | #define ABS_INT32_MIN ((uint32_t)INT32_MAX + 1) | 
| 174 |  |  | 
| 175 |  | static int uint32_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, | 
| 176 |  |                     int utype, char *free_cont, const ASN1_ITEM *it) | 
| 177 | 15.1k | { | 
| 178 | 15.1k |     uint64_t utmp = 0; | 
| 179 | 15.1k |     uint32_t utmp2 = 0; | 
| 180 | 15.1k |     char *cp; | 
| 181 | 15.1k |     int neg = 0; | 
| 182 |  |  | 
| 183 | 15.1k |     if (*pval == NULL && !uint64_new(pval, it)) | 
| 184 | 0 |         return 0; | 
| 185 |  |  | 
| 186 | 15.1k |     cp = (char *)*pval; | 
| 187 |  |  | 
| 188 |  |     /* | 
| 189 |  |      * Strictly speaking, zero length is malformed.  However, long_c2i | 
| 190 |  |      * (x_long.c) encodes 0 as a zero length INTEGER (wrongly, of course), | 
| 191 |  |      * so for the sake of backward compatibility, we still decode zero | 
| 192 |  |      * length INTEGERs as the number zero. | 
| 193 |  |      */ | 
| 194 | 15.1k |     if (len == 0) | 
| 195 | 972 |         goto long_compat; | 
| 196 |  |  | 
| 197 | 14.1k |     if (!c2i_uint64_int(&utmp, &neg, &cont, len)) | 
| 198 | 38 |         return 0; | 
| 199 | 14.1k |     if ((it->size & INTxx_FLAG_SIGNED) == 0 && neg) { | 
| 200 | 0 |         ASN1err(ASN1_F_UINT32_C2I, ASN1_R_ILLEGAL_NEGATIVE_VALUE); | 
| 201 | 0 |         return 0; | 
| 202 | 0 |     } | 
| 203 | 14.1k |     if (neg) { | 
| 204 | 5.74k |         if (utmp > ABS_INT32_MIN) { | 
| 205 | 69 |             ASN1err(ASN1_F_UINT32_C2I, ASN1_R_TOO_SMALL); | 
| 206 | 69 |             return 0; | 
| 207 | 69 |         } | 
| 208 | 5.67k |         utmp = 0 - utmp; | 
| 209 | 8.35k |     } else { | 
| 210 | 8.35k |         if (((it->size & INTxx_FLAG_SIGNED) != 0 && utmp > INT32_MAX) | 
| 211 | 8.35k |             || ((it->size & INTxx_FLAG_SIGNED) == 0 && utmp > UINT32_MAX)) { | 
| 212 | 68 |             ASN1err(ASN1_F_UINT32_C2I, ASN1_R_TOO_LARGE); | 
| 213 | 68 |             return 0; | 
| 214 | 68 |         } | 
| 215 | 8.35k |     } | 
| 216 |  |  | 
| 217 | 14.9k |  long_compat: | 
| 218 | 14.9k |     utmp2 = (uint32_t)utmp; | 
| 219 | 14.9k |     memcpy(cp, &utmp2, sizeof(utmp2)); | 
| 220 | 14.9k |     return 1; | 
| 221 | 14.1k | } | 
| 222 |  |  | 
| 223 |  | static int uint32_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it, | 
| 224 |  |                         int indent, const ASN1_PCTX *pctx) | 
| 225 | 0 | { | 
| 226 | 0 |     if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED) | 
| 227 | 0 |         return BIO_printf(out, "%d\n", **(int32_t **)pval); | 
| 228 | 0 |     return BIO_printf(out, "%u\n", **(uint32_t **)pval); | 
| 229 | 0 | } | 
| 230 |  |  | 
| 231 |  |  | 
| 232 |  | /* Define the primitives themselves */ | 
| 233 |  |  | 
| 234 |  | static ASN1_PRIMITIVE_FUNCS uint32_pf = { | 
| 235 |  |     NULL, 0, | 
| 236 |  |     uint32_new, | 
| 237 |  |     uint32_free, | 
| 238 |  |     uint32_clear, | 
| 239 |  |     uint32_c2i, | 
| 240 |  |     uint32_i2c, | 
| 241 |  |     uint32_print | 
| 242 |  | }; | 
| 243 |  |  | 
| 244 |  | static ASN1_PRIMITIVE_FUNCS uint64_pf = { | 
| 245 |  |     NULL, 0, | 
| 246 |  |     uint64_new, | 
| 247 |  |     uint64_free, | 
| 248 |  |     uint64_clear, | 
| 249 |  |     uint64_c2i, | 
| 250 |  |     uint64_i2c, | 
| 251 |  |     uint64_print | 
| 252 |  | }; | 
| 253 |  |  | 
| 254 |  | ASN1_ITEM_start(INT32) | 
| 255 |  |     ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint32_pf, | 
| 256 |  |     INTxx_FLAG_SIGNED, "INT32" | 
| 257 |  | ASN1_ITEM_end(INT32) | 
| 258 |  |  | 
| 259 |  | ASN1_ITEM_start(UINT32) | 
| 260 |  |     ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint32_pf, 0, "UINT32" | 
| 261 |  | ASN1_ITEM_end(UINT32) | 
| 262 |  |  | 
| 263 |  | ASN1_ITEM_start(INT64) | 
| 264 |  |     ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint64_pf, | 
| 265 |  |     INTxx_FLAG_SIGNED, "INT64" | 
| 266 |  | ASN1_ITEM_end(INT64) | 
| 267 |  |  | 
| 268 |  | ASN1_ITEM_start(UINT64) | 
| 269 |  |     ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint64_pf, 0, "UINT64" | 
| 270 |  | ASN1_ITEM_end(UINT64) | 
| 271 |  |  | 
| 272 |  | ASN1_ITEM_start(ZINT32) | 
| 273 |  |     ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint32_pf, | 
| 274 |  |     INTxx_FLAG_ZERO_DEFAULT|INTxx_FLAG_SIGNED, "ZINT32" | 
| 275 |  | ASN1_ITEM_end(ZINT32) | 
| 276 |  |  | 
| 277 |  | ASN1_ITEM_start(ZUINT32) | 
| 278 |  |     ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint32_pf, | 
| 279 |  |     INTxx_FLAG_ZERO_DEFAULT, "ZUINT32" | 
| 280 |  | ASN1_ITEM_end(ZUINT32) | 
| 281 |  |  | 
| 282 |  | ASN1_ITEM_start(ZINT64) | 
| 283 |  |     ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint64_pf, | 
| 284 |  |     INTxx_FLAG_ZERO_DEFAULT|INTxx_FLAG_SIGNED, "ZINT64" | 
| 285 |  | ASN1_ITEM_end(ZINT64) | 
| 286 |  |  | 
| 287 |  | ASN1_ITEM_start(ZUINT64) | 
| 288 |  |     ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint64_pf, | 
| 289 |  |     INTxx_FLAG_ZERO_DEFAULT, "ZUINT64" | 
| 290 |  | ASN1_ITEM_end(ZUINT64) | 
| 291 |  |  |