summaryrefslogtreecommitdiff
path: root/src/regress/lib
diff options
context:
space:
mode:
authorschwarze <>2021-12-11 22:58:48 +0000
committerschwarze <>2021-12-11 22:58:48 +0000
commit693b9af2c868379acbde502b0fdd91708ee6da6e (patch)
tree532c5bb73c4be67f02e0555a36f578b690062300 /src/regress/lib
parentba300c78ab7a7a24256b3e243db2e3f84ada4c14 (diff)
downloadopenbsd-693b9af2c868379acbde502b0fdd91708ee6da6e.tar.gz
openbsd-693b9af2c868379acbde502b0fdd91708ee6da6e.tar.bz2
openbsd-693b9af2c868379acbde502b0fdd91708ee6da6e.zip
Merge two bugfixes in ASN1_STRING_TABLE_add(3) and ASN1_STRING_TABLE_get(3)
from the OpenSSL 1.1.1 branch, which is still under a free license, mostly this commit: commit d35c0ff30b31be9fd5dcf3d552a16feb8de464bc Author: Dr. Stephen Henson <steve@openssl.org> Date: Fri Oct 19 15:06:31 2012 +0000 fix ASN1_STRING_TABLE_add so it can override existing string table values This fixes a segfault in ASN1_STRING_TABLE_add(3), which tried to change a static const entry when called with an nid already in the default table, and it switches the precedence of the two tables in ASN1_STRING_TABLE_get(3). In addition, it changes behaviour in the following minor ways: * Ignore negative minsize and maxsize arguments, not just -1. * Ignore a zero mask and zero flags. It's unclear whether these additional changes make the API absolutely better, but we want compatibility with OpenSSL in these functions. Tweaks & OK tb@.
Diffstat (limited to 'src/regress/lib')
-rw-r--r--src/regress/lib/libcrypto/asn1/Makefile5
-rw-r--r--src/regress/lib/libcrypto/asn1/string_table.c128
2 files changed, 131 insertions, 2 deletions
diff --git a/src/regress/lib/libcrypto/asn1/Makefile b/src/regress/lib/libcrypto/asn1/Makefile
index 1c4a42b80b..90eca92f8e 100644
--- a/src/regress/lib/libcrypto/asn1/Makefile
+++ b/src/regress/lib/libcrypto/asn1/Makefile
@@ -1,4 +1,4 @@
1# $OpenBSD: Makefile,v 1.9 2021/12/09 16:30:57 jsing Exp $ 1# $OpenBSD: Makefile,v 1.10 2021/12/11 22:58:48 schwarze Exp $
2 2
3TESTS = \ 3TESTS = \
4 asn1basic \ 4 asn1basic \
@@ -7,7 +7,8 @@ TESTS = \
7 asn1string_copy \ 7 asn1string_copy \
8 asn1time \ 8 asn1time \
9 asn1x509 \ 9 asn1x509 \
10 rfc5280time 10 rfc5280time \
11 string_table
11 12
12PROGS = ${TESTS} 13PROGS = ${TESTS}
13 14
diff --git a/src/regress/lib/libcrypto/asn1/string_table.c b/src/regress/lib/libcrypto/asn1/string_table.c
new file mode 100644
index 0000000000..e80cf0f206
--- /dev/null
+++ b/src/regress/lib/libcrypto/asn1/string_table.c
@@ -0,0 +1,128 @@
1/* $OpenBSD: string_table.c,v 1.1 2021/12/11 22:58:48 schwarze Exp $ */
2/*
3 * Copyright (c) 2021 Ingo Schwarze <schwarze@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <err.h>
19#include <stdarg.h>
20#include <openssl/asn1.h>
21#include <openssl/objects.h>
22
23static int errcount;
24
25static void
26report(const char *fmt, ...)
27{
28 va_list ap;
29
30 va_start(ap, fmt);
31 vwarnx(fmt, ap);
32 va_end(ap);
33
34 errcount++;
35}
36
37static void
38stable_check(const char *testname, ASN1_STRING_TABLE *have,
39 ASN1_STRING_TABLE *want, unsigned long want_flags)
40{
41 if (have == NULL) {
42 report("%s returned NULL", testname);
43 return;
44 }
45 if (have->nid != want->nid)
46 report("%s nid %d, expected %d", testname,
47 have->nid, want->nid);
48 if (have->minsize != want->minsize)
49 report("%s minsize %ld, expected %ld", testname,
50 have->minsize, want->minsize);
51 if (have->maxsize != want->maxsize)
52 report("%s maxsize %ld, expected %ld", testname,
53 have->maxsize, want->maxsize);
54 if (have->mask != want->mask)
55 report("%s mask %lu, expected %lu", testname,
56 have->mask, want->mask);
57 if (have->flags != want_flags)
58 report("%s flags %lu, expected %lu", testname,
59 have->flags, want_flags);
60}
61
62int
63main(void)
64{
65 ASN1_STRING_TABLE orig, mine, *have;
66 int irc;
67
68 orig.nid = NID_name;
69 orig.minsize = 1;
70 orig.maxsize = ub_name;
71 orig.mask = DIRSTRING_TYPE;
72 orig.flags = 0;
73
74 mine.nid = NID_name;
75 mine.minsize = 4;
76 mine.maxsize = 64;
77 mine.mask = B_ASN1_PRINTABLESTRING;
78 mine.flags = STABLE_NO_MASK;
79
80 /* Original entry. */
81
82 have = ASN1_STRING_TABLE_get(orig.nid);
83 stable_check("orig", have, &orig, 0);
84
85 /* Copy, but don't really change. */
86
87 irc = ASN1_STRING_TABLE_add(orig.nid, -1, -1, 0, 0);
88 if (irc != 1)
89 report("set noop returned %d, expected 1", irc);
90 have = ASN1_STRING_TABLE_get(orig.nid);
91 stable_check("noop", have, &orig, STABLE_FLAGS_MALLOC);
92
93 /* Change entry. */
94
95 irc = ASN1_STRING_TABLE_add(mine.nid, mine.minsize, mine.maxsize,
96 mine.mask, mine.flags);
97 if (irc != 1)
98 report("set returned %d, expected 1", irc);
99 have = ASN1_STRING_TABLE_get(mine.nid);
100 stable_check("set", have, &mine, STABLE_FLAGS_MALLOC | STABLE_NO_MASK);
101
102 /* New entry. */
103
104 mine.nid = NID_title;
105 irc = ASN1_STRING_TABLE_add(mine.nid, mine.minsize, mine.maxsize,
106 mine.mask, mine.flags);
107 if (irc != 1)
108 report("new returned %d, expected 1", irc);
109 have = ASN1_STRING_TABLE_get(mine.nid);
110 stable_check("new", have, &mine, STABLE_FLAGS_MALLOC | STABLE_NO_MASK);
111
112 /* Back to the initial state. */
113
114 ASN1_STRING_TABLE_cleanup();
115 have = ASN1_STRING_TABLE_get(orig.nid);
116 stable_check("back", have, &orig, 0);
117 if (ASN1_STRING_TABLE_get(mine.nid) != NULL)
118 report("deleted entry is not NULL");
119
120 switch (errcount) {
121 case 0:
122 return 0;
123 case 1:
124 errx(1, "one error");
125 default:
126 errx(1, "%d errors", errcount);
127 }
128}