/src/openssl111/crypto/asn1/x_long.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2000-2017 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 <openssl/asn1t.h> |
13 | | |
14 | | #if !(OPENSSL_API_COMPAT < 0x10200000L) |
15 | | NON_EMPTY_TRANSLATION_UNIT |
16 | | #else |
17 | | |
18 | 29.4k | #define COPY_SIZE(a, b) (sizeof(a) < sizeof(b) ? sizeof(a) : sizeof(b)) |
19 | | |
20 | | /* |
21 | | * Custom primitive type for long handling. This converts between an |
22 | | * ASN1_INTEGER and a long directly. |
23 | | */ |
24 | | |
25 | | static int long_new(ASN1_VALUE **pval, const ASN1_ITEM *it); |
26 | | static void long_free(ASN1_VALUE **pval, const ASN1_ITEM *it); |
27 | | |
28 | | static int long_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, |
29 | | const ASN1_ITEM *it); |
30 | | static int long_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, |
31 | | int utype, char *free_cont, const ASN1_ITEM *it); |
32 | | static int long_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it, |
33 | | int indent, const ASN1_PCTX *pctx); |
34 | | |
35 | | static ASN1_PRIMITIVE_FUNCS long_pf = { |
36 | | NULL, 0, |
37 | | long_new, |
38 | | long_free, |
39 | | long_free, /* Clear should set to initial value */ |
40 | | long_c2i, |
41 | | long_i2c, |
42 | | long_print |
43 | | }; |
44 | | |
45 | | ASN1_ITEM_start(LONG) |
46 | | ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &long_pf, ASN1_LONG_UNDEF, "LONG" |
47 | | ASN1_ITEM_end(LONG) |
48 | | |
49 | | ASN1_ITEM_start(ZLONG) |
50 | | ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &long_pf, 0, "ZLONG" |
51 | | ASN1_ITEM_end(ZLONG) |
52 | | |
53 | | static int long_new(ASN1_VALUE **pval, const ASN1_ITEM *it) |
54 | 0 | { |
55 | 0 | memcpy(pval, &it->size, COPY_SIZE(*pval, it->size)); |
56 | 0 | return 1; |
57 | 0 | } |
58 | | |
59 | | static void long_free(ASN1_VALUE **pval, const ASN1_ITEM *it) |
60 | 25.5k | { |
61 | 25.5k | memcpy(pval, &it->size, COPY_SIZE(*pval, it->size)); |
62 | 25.5k | } |
63 | | |
64 | | /* |
65 | | * Originally BN_num_bits_word was called to perform this operation, but |
66 | | * trouble is that there is no guarantee that sizeof(long) equals to |
67 | | * sizeof(BN_ULONG). BN_ULONG is a configurable type that can be as wide |
68 | | * as long, but also double or half... |
69 | | */ |
70 | | static int num_bits_ulong(unsigned long value) |
71 | 2.30k | { |
72 | 2.30k | size_t i; |
73 | 2.30k | unsigned long ret = 0; |
74 | | |
75 | | /* |
76 | | * It is argued that *on average* constant counter loop performs |
77 | | * not worse [if not better] than one with conditional break or |
78 | | * mask-n-table-lookup-style, because of branch misprediction |
79 | | * penalties. |
80 | | */ |
81 | 149k | for (i = 0; i < sizeof(value) * 8; i++) { |
82 | 147k | ret += (value != 0); |
83 | 147k | value >>= 1; |
84 | 147k | } |
85 | | |
86 | 2.30k | return (int)ret; |
87 | 2.30k | } |
88 | | |
89 | | static int long_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, |
90 | | const ASN1_ITEM *it) |
91 | 2.30k | { |
92 | 2.30k | long ltmp; |
93 | 2.30k | unsigned long utmp, sign; |
94 | 2.30k | int clen, pad, i; |
95 | | |
96 | 2.30k | memcpy(<mp, pval, COPY_SIZE(*pval, ltmp)); |
97 | 2.30k | if (ltmp == it->size) |
98 | 0 | return -1; |
99 | | /* |
100 | | * Convert the long to positive: we subtract one if negative so we can |
101 | | * cleanly handle the padding if only the MSB of the leading octet is |
102 | | * set. |
103 | | */ |
104 | 2.30k | if (ltmp < 0) { |
105 | 1.12k | sign = 0xff; |
106 | 1.12k | utmp = 0 - (unsigned long)ltmp - 1; |
107 | 1.17k | } else { |
108 | 1.17k | sign = 0; |
109 | 1.17k | utmp = ltmp; |
110 | 1.17k | } |
111 | 2.30k | clen = num_bits_ulong(utmp); |
112 | | /* If MSB of leading octet set we need to pad */ |
113 | 2.30k | if (!(clen & 0x7)) |
114 | 414 | pad = 1; |
115 | 1.89k | else |
116 | 1.89k | pad = 0; |
117 | | |
118 | | /* Convert number of bits to number of octets */ |
119 | 2.30k | clen = (clen + 7) >> 3; |
120 | | |
121 | 2.30k | if (cont != NULL) { |
122 | 769 | if (pad) |
123 | 138 | *cont++ = (unsigned char)sign; |
124 | 4.14k | for (i = clen - 1; i >= 0; i--) { |
125 | 3.37k | cont[i] = (unsigned char)(utmp ^ sign); |
126 | 3.37k | utmp >>= 8; |
127 | 3.37k | } |
128 | 769 | } |
129 | 2.30k | return clen + pad; |
130 | 2.30k | } |
131 | | |
132 | | static int long_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, |
133 | | int utype, char *free_cont, const ASN1_ITEM *it) |
134 | 1.31k | { |
135 | 1.31k | int i; |
136 | 1.31k | long ltmp; |
137 | 1.31k | unsigned long utmp = 0, sign = 0x100; |
138 | | |
139 | 1.31k | if (len > 1) { |
140 | | /* |
141 | | * Check possible pad byte. Worst case, we're skipping past actual |
142 | | * content, but since that's only with 0x00 and 0xff and we set neg |
143 | | * accordingly, the result will be correct in the end anyway. |
144 | | */ |
145 | 1.24k | switch (cont[0]) { |
146 | 334 | case 0xff: |
147 | 334 | cont++; |
148 | 334 | len--; |
149 | 334 | sign = 0xff; |
150 | 334 | break; |
151 | 242 | case 0: |
152 | 242 | cont++; |
153 | 242 | len--; |
154 | 242 | sign = 0; |
155 | 242 | break; |
156 | 1.24k | } |
157 | 1.24k | } |
158 | 1.31k | if (len > (int)sizeof(long)) { |
159 | 156 | ASN1err(ASN1_F_LONG_C2I, ASN1_R_INTEGER_TOO_LARGE_FOR_LONG); |
160 | 156 | return 0; |
161 | 156 | } |
162 | | |
163 | 1.15k | if (sign == 0x100) { |
164 | | /* Is it negative? */ |
165 | 638 | if (len && (cont[0] & 0x80)) |
166 | 322 | sign = 0xff; |
167 | 316 | else |
168 | 316 | sign = 0; |
169 | 638 | } else if (((sign ^ cont[0]) & 0x80) == 0) { /* same sign bit? */ |
170 | 166 | ASN1err(ASN1_F_LONG_C2I, ASN1_R_ILLEGAL_PADDING); |
171 | 166 | return 0; |
172 | 166 | } |
173 | 990 | utmp = 0; |
174 | 6.10k | for (i = 0; i < len; i++) { |
175 | 5.11k | utmp <<= 8; |
176 | 5.11k | utmp |= cont[i] ^ sign; |
177 | 5.11k | } |
178 | 990 | ltmp = (long)utmp; |
179 | 990 | if (ltmp < 0) { |
180 | 216 | ASN1err(ASN1_F_LONG_C2I, ASN1_R_INTEGER_TOO_LARGE_FOR_LONG); |
181 | 216 | return 0; |
182 | 216 | } |
183 | 774 | if (sign) |
184 | 376 | ltmp = -ltmp - 1; |
185 | 774 | if (ltmp == it->size) { |
186 | 3 | ASN1err(ASN1_F_LONG_C2I, ASN1_R_INTEGER_TOO_LARGE_FOR_LONG); |
187 | 3 | return 0; |
188 | 3 | } |
189 | 771 | memcpy(pval, <mp, COPY_SIZE(*pval, ltmp)); |
190 | 771 | return 1; |
191 | 774 | } |
192 | | |
193 | | static int long_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it, |
194 | | int indent, const ASN1_PCTX *pctx) |
195 | 769 | { |
196 | 769 | long l; |
197 | | |
198 | 769 | memcpy(&l, pval, COPY_SIZE(*pval, l)); |
199 | 769 | return BIO_printf(out, "%ld\n", l); |
200 | 769 | } |
201 | | #endif |