summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjsing <>2022-06-26 13:10:15 +0000
committerjsing <>2022-06-26 13:10:15 +0000
commit40e8890ed2aa70fe008b1a19f9c95c2acf708db2 (patch)
tree8e4295e9d121593c408f9ea0ab62b671a26b7ff2 /src
parent04f7297a7faf857871e10ce5e829cddc1dbf3520 (diff)
downloadopenbsd-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.c46
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
71static int long_new(ASN1_VALUE **pval, const ASN1_ITEM *it); 71static int long_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
72static void long_free(ASN1_VALUE **pval, const ASN1_ITEM *it); 72static void long_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
73static void long_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
73 74
74static int long_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, 75static 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
113static void
114long_get(ASN1_VALUE **pval, long *out_val)
115{
116 memcpy(out_val, pval, sizeof(long));
117}
118
119static void
120long_set(ASN1_VALUE **pval, long val)
121{
122 memcpy(pval, &val, sizeof(long));
123}
124
112static int 125static int
113long_new(ASN1_VALUE **pval, const ASN1_ITEM *it) 126long_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
119static void 133static void
120long_free(ASN1_VALUE **pval, const ASN1_ITEM *it) 134long_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
121{ 135{
122 *(long *)pval = it->size; 136 long_clear(pval, it);
137}
138
139static void
140long_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
141{
142 /* Zero value. */
143 long_set(pval, it->size);
123} 144}
124 145
125static int 146static 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, &ltmp);
136 memcpy(&ltmp, 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, &ltmp, 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
210long_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it, int indent, 230long_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;