1*9b5d42b3Sschwarze /* $OpenBSD: asn1string_copy.c,v 1.1 2021/11/13 20:50:14 schwarze Exp $ */
2*9b5d42b3Sschwarze /*
3*9b5d42b3Sschwarze  * Copyright (c) 2021 Ingo Schwarze <schwarze@openbsd.org>
4*9b5d42b3Sschwarze  *
5*9b5d42b3Sschwarze  * Permission to use, copy, modify, and distribute this software for any
6*9b5d42b3Sschwarze  * purpose with or without fee is hereby granted, provided that the above
7*9b5d42b3Sschwarze  * copyright notice and this permission notice appear in all copies.
8*9b5d42b3Sschwarze  *
9*9b5d42b3Sschwarze  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10*9b5d42b3Sschwarze  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*9b5d42b3Sschwarze  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12*9b5d42b3Sschwarze  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*9b5d42b3Sschwarze  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*9b5d42b3Sschwarze  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15*9b5d42b3Sschwarze  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16*9b5d42b3Sschwarze  */
17*9b5d42b3Sschwarze 
18*9b5d42b3Sschwarze #include <err.h>
19*9b5d42b3Sschwarze #include <string.h>
20*9b5d42b3Sschwarze 
21*9b5d42b3Sschwarze #include <openssl/asn1.h>
22*9b5d42b3Sschwarze 
23*9b5d42b3Sschwarze int
main(void)24*9b5d42b3Sschwarze main(void)
25*9b5d42b3Sschwarze {
26*9b5d42b3Sschwarze 	const unsigned char	*data = "hello world";
27*9b5d42b3Sschwarze 	const unsigned char	*str;
28*9b5d42b3Sschwarze 	ASN1_STRING		*src, *dst;
29*9b5d42b3Sschwarze 	int			 irc;
30*9b5d42b3Sschwarze 
31*9b5d42b3Sschwarze 	/* Set up the source string. */
32*9b5d42b3Sschwarze 
33*9b5d42b3Sschwarze 	if ((src = ASN1_IA5STRING_new()) == NULL)
34*9b5d42b3Sschwarze 		err(1, "FAIL: ASN1_IA5STRING_new() returned NULL");
35*9b5d42b3Sschwarze 	if (ASN1_STRING_set(src, data, -1) == 0)
36*9b5d42b3Sschwarze 		err(1, "FAIL: ASN1_STRING_set(src) failed");
37*9b5d42b3Sschwarze 	if ((str = ASN1_STRING_get0_data(src)) == NULL)
38*9b5d42b3Sschwarze 		errx(1, "FAIL: 1st ASN1_STRING_get0_data(src) returned NULL");
39*9b5d42b3Sschwarze 	if (strcmp(str, data))
40*9b5d42b3Sschwarze 		errx(1, "FAIL: 1st ASN1_STRING_get0_data(src) "
41*9b5d42b3Sschwarze 		    "returned wrong data: \"%s\" (expected \"%s\")",
42*9b5d42b3Sschwarze 		    str, data);
43*9b5d42b3Sschwarze 	if ((irc = ASN1_STRING_length(src)) != (int)strlen(data))
44*9b5d42b3Sschwarze 		errx(1, "FAIL: 1st ASN1_STRING_length(src) "
45*9b5d42b3Sschwarze 		    "returned a wrong length: %d (expected %zu)",
46*9b5d42b3Sschwarze 		    irc, strlen(data));
47*9b5d42b3Sschwarze 	if ((irc = ASN1_STRING_type(src)) != V_ASN1_IA5STRING)
48*9b5d42b3Sschwarze 		errx(1, "FAIL: 1st ASN1_STRING_type(src) "
49*9b5d42b3Sschwarze 		    "returned a wrong type: %d (expected %d)",
50*9b5d42b3Sschwarze 		    irc, V_ASN1_IA5STRING);
51*9b5d42b3Sschwarze 
52*9b5d42b3Sschwarze 	/* Set up the destination string. */
53*9b5d42b3Sschwarze 
54*9b5d42b3Sschwarze 	if ((dst = ASN1_STRING_new()) == NULL)
55*9b5d42b3Sschwarze 		err(1, "FAIL: ASN1_STRING_new() returned NULL");
56*9b5d42b3Sschwarze 	if ((str = ASN1_STRING_get0_data(dst)) != NULL)
57*9b5d42b3Sschwarze 		errx(1, "FAIL: 1st ASN1_STRING_get0_data(dst) "
58*9b5d42b3Sschwarze 		    "returned \"%s\" (expected NULL)", str);
59*9b5d42b3Sschwarze 	if ((irc = ASN1_STRING_length(dst)) != 0)
60*9b5d42b3Sschwarze 		errx(1, "FAIL: 1st ASN1_STRING_length(dst) "
61*9b5d42b3Sschwarze 		    "returned a wrong length: %d (expected 0)", irc);
62*9b5d42b3Sschwarze 	if ((irc = ASN1_STRING_type(dst)) != V_ASN1_OCTET_STRING)
63*9b5d42b3Sschwarze 		errx(1, "FAIL: 1st ASN1_STRING_type(dst) "
64*9b5d42b3Sschwarze 		    "returned a wrong type: %d (expected %d)",
65*9b5d42b3Sschwarze 		    irc, V_ASN1_OCTET_STRING);
66*9b5d42b3Sschwarze 	ASN1_STRING_length_set(dst, -1);
67*9b5d42b3Sschwarze 	if ((str = ASN1_STRING_get0_data(dst)) != NULL)
68*9b5d42b3Sschwarze 		errx(1, "FAIL: 2nd ASN1_STRING_get0_data(dst) "
69*9b5d42b3Sschwarze 		    "returned \"%s\" (expected NULL)", str);
70*9b5d42b3Sschwarze 	if ((irc = ASN1_STRING_length(dst)) != -1)
71*9b5d42b3Sschwarze 		errx(1, "FAIL: 2nd ASN1_STRING_length(dst) "
72*9b5d42b3Sschwarze 		    "returned a wrong length: %d (expected -1)", irc);
73*9b5d42b3Sschwarze 	if ((irc = ASN1_STRING_type(dst)) != V_ASN1_OCTET_STRING)
74*9b5d42b3Sschwarze 		errx(1, "FAIL: 2nd ASN1_STRING_type(dst) "
75*9b5d42b3Sschwarze 		    "returned a wrong type: %d (expected %d)",
76*9b5d42b3Sschwarze 		    irc, V_ASN1_OCTET_STRING);
77*9b5d42b3Sschwarze 
78*9b5d42b3Sschwarze 	/* Attempt to copy in the wrong direction. */
79*9b5d42b3Sschwarze 
80*9b5d42b3Sschwarze 	if (ASN1_STRING_copy(src, dst) != 0)
81*9b5d42b3Sschwarze 		errx(1, "FAIL: ASN1_STRING_copy unexpectedly succeeded");
82*9b5d42b3Sschwarze 	if ((str = ASN1_STRING_get0_data(src)) == NULL)
83*9b5d42b3Sschwarze 		errx(1, "FAIL: 2nd ASN1_STRING_get0_data(src) returned NULL");
84*9b5d42b3Sschwarze 	if (strcmp(str, data))
85*9b5d42b3Sschwarze 		errx(1, "FAIL: 2nd ASN1_STRING_get0_data(src) "
86*9b5d42b3Sschwarze 		    "returned wrong data: \"%s\" (expected \"%s\")",
87*9b5d42b3Sschwarze 		    str, data);
88*9b5d42b3Sschwarze 	if ((irc = ASN1_STRING_length(src)) != (int)strlen(data))
89*9b5d42b3Sschwarze 		errx(1, "FAIL: 2nd ASN1_STRING_length(src) "
90*9b5d42b3Sschwarze 		    "returned a wrong length: %d (expected %zu)",
91*9b5d42b3Sschwarze 		    irc, strlen(data));
92*9b5d42b3Sschwarze 	if ((irc = ASN1_STRING_type(src)) != V_ASN1_IA5STRING)
93*9b5d42b3Sschwarze 		errx(1, "FAIL: 2nd ASN1_STRING_type(src) "
94*9b5d42b3Sschwarze 		    "returned a wrong type: %d (expected %d)",
95*9b5d42b3Sschwarze 		    irc, V_ASN1_IA5STRING);
96*9b5d42b3Sschwarze 
97*9b5d42b3Sschwarze 	/* Copy in the right direction. */
98*9b5d42b3Sschwarze 
99*9b5d42b3Sschwarze 	if (ASN1_STRING_copy(dst, src) != 1)
100*9b5d42b3Sschwarze 		err(1, "FAIL: ASN1_STRING_copy unexpectedly failed");
101*9b5d42b3Sschwarze 	if ((str = ASN1_STRING_get0_data(dst)) == NULL)
102*9b5d42b3Sschwarze 		errx(1, "FAIL: 3rd ASN1_STRING_get0_data(dst) returned NULL");
103*9b5d42b3Sschwarze 	if (strcmp(str, data))
104*9b5d42b3Sschwarze 		errx(1, "FAIL: 3rd ASN1_STRING_get0_data(dst) "
105*9b5d42b3Sschwarze 		    "returned wrong data: \"%s\" (expected \"%s\")",
106*9b5d42b3Sschwarze 		    str, data);
107*9b5d42b3Sschwarze 	if ((irc = ASN1_STRING_length(dst)) != (int)strlen(data))
108*9b5d42b3Sschwarze 		errx(1, "FAIL: 3rd ASN1_STRING_length(dst) "
109*9b5d42b3Sschwarze 		    "returned a wrong length: %d (expected %zu)",
110*9b5d42b3Sschwarze 		    irc, strlen(data));
111*9b5d42b3Sschwarze 	if ((irc = ASN1_STRING_type(dst)) != V_ASN1_IA5STRING)
112*9b5d42b3Sschwarze 		errx(1, "FAIL: 3rd ASN1_STRING_type(dst) "
113*9b5d42b3Sschwarze 		    "returned a wrong type: %d (expected %d)",
114*9b5d42b3Sschwarze 		    irc, V_ASN1_IA5STRING);
115*9b5d42b3Sschwarze 
116*9b5d42b3Sschwarze 	ASN1_STRING_free(src);
117*9b5d42b3Sschwarze 	ASN1_STRING_free(dst);
118*9b5d42b3Sschwarze 	return 0;
119*9b5d42b3Sschwarze }
120