2 * Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include "internal/cryptlib.h"
12 #include <openssl/asn1t.h>
14 #define COPY_SIZE(a, b) (sizeof(a) < sizeof(b) ? sizeof(a) : sizeof(b))
17 * Custom primitive type for long handling. This converts between an
18 * ASN1_INTEGER and a long directly.
21 static int long_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
22 static void long_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
24 static int long_i2c(const ASN1_VALUE **pval, unsigned char *cont, int *putype,
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);
31 static ASN1_PRIMITIVE_FUNCS long_pf = {
35 long_free, /* Clear should set to initial value */
42 ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &long_pf, ASN1_LONG_UNDEF, "LONG"
45 ASN1_ITEM_start(ZLONG)
46 ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &long_pf, 0, "ZLONG"
49 static int long_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
51 memcpy(pval, &it->size, COPY_SIZE(*pval, it->size));
55 static void long_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
57 memcpy(pval, &it->size, COPY_SIZE(*pval, it->size));
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...
66 static int num_bits_ulong(unsigned long value)
69 unsigned long ret = 0;
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
77 for (i = 0; i < sizeof(value) * 8; i++) {
85 static int long_i2c(const ASN1_VALUE **pval, unsigned char *cont, int *putype,
89 unsigned long utmp, sign;
92 memcpy(<mp, pval, COPY_SIZE(*pval, ltmp));
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
102 utmp = 0 - (unsigned long)ltmp - 1;
107 clen = num_bits_ulong(utmp);
108 /* If MSB of leading octet set we need to pad */
114 /* Convert number of bits to number of octets */
115 clen = (clen + 7) >> 3;
119 *cont++ = (unsigned char)sign;
120 for (i = clen - 1; i >= 0; i--) {
121 cont[i] = (unsigned char)(utmp ^ sign);
128 static int long_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
129 int utype, char *free_cont, const ASN1_ITEM *it)
133 unsigned long utmp = 0, sign = 0x100;
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.
154 if (len > (int)sizeof(long)) {
155 ASN1err(ASN1_F_LONG_C2I, ASN1_R_INTEGER_TOO_LARGE_FOR_LONG);
160 /* Is it negative? */
161 if (len && (cont[0] & 0x80))
165 } else if (((sign ^ cont[0]) & 0x80) == 0) { /* same sign bit? */
166 ASN1err(ASN1_F_LONG_C2I, ASN1_R_ILLEGAL_PADDING);
170 for (i = 0; i < len; i++) {
172 utmp |= cont[i] ^ sign;
176 ASN1err(ASN1_F_LONG_C2I, ASN1_R_INTEGER_TOO_LARGE_FOR_LONG);
181 if (ltmp == it->size) {
182 ASN1err(ASN1_F_LONG_C2I, ASN1_R_INTEGER_TOO_LARGE_FOR_LONG);
185 memcpy(pval, <mp, COPY_SIZE(*pval, ltmp));
189 static int long_print(BIO *out, const ASN1_VALUE **pval, const ASN1_ITEM *it,
190 int indent, const ASN1_PCTX *pctx)
194 memcpy(&l, pval, COPY_SIZE(*pval, l));
195 return BIO_printf(out, "%ld\n", l);