diff options
author | jsing <> | 2022-06-26 13:10:15 +0000 |
---|---|---|
committer | jsing <> | 2022-06-26 13:10:15 +0000 |
commit | 40e8890ed2aa70fe008b1a19f9c95c2acf708db2 (patch) | |
tree | 8e4295e9d121593c408f9ea0ab62b671a26b7ff2 /src | |
parent | 04f7297a7faf857871e10ce5e829cddc1dbf3520 (diff) | |
download | openbsd-40e8890ed2aa70fe008b1a19f9c95c2acf708db2.tar.gz openbsd-40e8890ed2aa70fe008b1a19f9c95c2acf708db2.tar.bz2 openbsd-40e8890ed2aa70fe008b1a19f9c95c2acf708db2.zip |
Provide and use long_{get,set}()
Apparently at some point a LONG_it was misaligned - provide and use
long_{get,set}() so that we always memcpy() rather than doing it some times
but not others. While here provide long_clear() rather than abusing and
reusing long_free().
ok tb@
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/libcrypto/asn1/x_long.c | 46 |
1 files changed, 35 insertions, 11 deletions
diff --git a/src/lib/libcrypto/asn1/x_long.c b/src/lib/libcrypto/asn1/x_long.c index ff72338cc0..b51ea6f214 100644 --- a/src/lib/libcrypto/asn1/x_long.c +++ b/src/lib/libcrypto/asn1/x_long.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: x_long.c,v 1.16 2019/04/20 11:13:15 jsing Exp $ */ | 1 | /* $OpenBSD: x_long.c,v 1.17 2022/06/26 13:10:15 jsing Exp $ */ |
2 | /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL | 2 | /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL |
3 | * project 2000. | 3 | * project 2000. |
4 | */ | 4 | */ |
@@ -70,6 +70,7 @@ | |||
70 | 70 | ||
71 | static int long_new(ASN1_VALUE **pval, const ASN1_ITEM *it); | 71 | static int long_new(ASN1_VALUE **pval, const ASN1_ITEM *it); |
72 | static void long_free(ASN1_VALUE **pval, const ASN1_ITEM *it); | 72 | static void long_free(ASN1_VALUE **pval, const ASN1_ITEM *it); |
73 | static void long_clear(ASN1_VALUE **pval, const ASN1_ITEM *it); | ||
73 | 74 | ||
74 | static int long_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, | 75 | static int long_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, |
75 | const ASN1_ITEM *it); | 76 | const ASN1_ITEM *it); |
@@ -83,7 +84,7 @@ static ASN1_PRIMITIVE_FUNCS long_pf = { | |||
83 | .flags = 0, | 84 | .flags = 0, |
84 | .prim_new = long_new, | 85 | .prim_new = long_new, |
85 | .prim_free = long_free, | 86 | .prim_free = long_free, |
86 | .prim_clear = long_free, /* Clear should set to initial value */ | 87 | .prim_clear = long_clear, |
87 | .prim_c2i = long_c2i, | 88 | .prim_c2i = long_c2i, |
88 | .prim_i2c = long_i2c, | 89 | .prim_i2c = long_i2c, |
89 | .prim_print = long_print, | 90 | .prim_print = long_print, |
@@ -109,17 +110,37 @@ const ASN1_ITEM ZLONG_it = { | |||
109 | .sname = "ZLONG", | 110 | .sname = "ZLONG", |
110 | }; | 111 | }; |
111 | 112 | ||
113 | static void | ||
114 | long_get(ASN1_VALUE **pval, long *out_val) | ||
115 | { | ||
116 | memcpy(out_val, pval, sizeof(long)); | ||
117 | } | ||
118 | |||
119 | static void | ||
120 | long_set(ASN1_VALUE **pval, long val) | ||
121 | { | ||
122 | memcpy(pval, &val, sizeof(long)); | ||
123 | } | ||
124 | |||
112 | static int | 125 | static int |
113 | long_new(ASN1_VALUE **pval, const ASN1_ITEM *it) | 126 | long_new(ASN1_VALUE **pval, const ASN1_ITEM *it) |
114 | { | 127 | { |
115 | *(long *)pval = it->size; | 128 | long_clear(pval, it); |
129 | |||
116 | return 1; | 130 | return 1; |
117 | } | 131 | } |
118 | 132 | ||
119 | static void | 133 | static void |
120 | long_free(ASN1_VALUE **pval, const ASN1_ITEM *it) | 134 | long_free(ASN1_VALUE **pval, const ASN1_ITEM *it) |
121 | { | 135 | { |
122 | *(long *)pval = it->size; | 136 | long_clear(pval, it); |
137 | } | ||
138 | |||
139 | static void | ||
140 | long_clear(ASN1_VALUE **pval, const ASN1_ITEM *it) | ||
141 | { | ||
142 | /* Zero value. */ | ||
143 | long_set(pval, it->size); | ||
123 | } | 144 | } |
124 | 145 | ||
125 | static int | 146 | static int |
@@ -129,11 +150,8 @@ long_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, | |||
129 | long ltmp; | 150 | long ltmp; |
130 | unsigned long utmp; | 151 | unsigned long utmp; |
131 | int clen, pad, i; | 152 | int clen, pad, i; |
132 | /* this exists to bypass broken gcc optimization */ | ||
133 | char *cp = (char *)pval; | ||
134 | 153 | ||
135 | /* use memcpy, because we may not be long aligned */ | 154 | long_get(pval, <mp); |
136 | memcpy(<mp, cp, sizeof(long)); | ||
137 | 155 | ||
138 | if (ltmp == it->size) | 156 | if (ltmp == it->size) |
139 | return -1; | 157 | return -1; |
@@ -175,7 +193,7 @@ long_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, int utype, | |||
175 | int neg, i; | 193 | int neg, i; |
176 | long ltmp; | 194 | long ltmp; |
177 | unsigned long utmp = 0; | 195 | unsigned long utmp = 0; |
178 | char *cp = (char *)pval; | 196 | |
179 | if (len > (int)sizeof(long)) { | 197 | if (len > (int)sizeof(long)) { |
180 | ASN1error(ASN1_R_INTEGER_TOO_LARGE_FOR_LONG); | 198 | ASN1error(ASN1_R_INTEGER_TOO_LARGE_FOR_LONG); |
181 | return 0; | 199 | return 0; |
@@ -202,7 +220,9 @@ long_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, int utype, | |||
202 | ASN1error(ASN1_R_INTEGER_TOO_LARGE_FOR_LONG); | 220 | ASN1error(ASN1_R_INTEGER_TOO_LARGE_FOR_LONG); |
203 | return 0; | 221 | return 0; |
204 | } | 222 | } |
205 | memcpy(cp, <mp, sizeof(long)); | 223 | |
224 | long_set(pval, ltmp); | ||
225 | |||
206 | return 1; | 226 | return 1; |
207 | } | 227 | } |
208 | 228 | ||
@@ -210,7 +230,11 @@ static int | |||
210 | long_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it, int indent, | 230 | long_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it, int indent, |
211 | const ASN1_PCTX *pctx) | 231 | const ASN1_PCTX *pctx) |
212 | { | 232 | { |
213 | if (BIO_printf(out, "%ld\n", *(long *)pval) <= 0) | 233 | long val; |
234 | |||
235 | long_get(pval, &val); | ||
236 | |||
237 | if (BIO_printf(out, "%ld\n", val) <= 0) | ||
214 | return 0; | 238 | return 0; |
215 | 239 | ||
216 | return 1; | 240 | return 1; |