1 /* $OpenBSD: asn1string_copy.c,v 1.1 2021/11/13 20:50:14 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 <string.h> 20 21 #include <openssl/asn1.h> 22 23 int 24 main(void) 25 { 26 const unsigned char *data = "hello world"; 27 const unsigned char *str; 28 ASN1_STRING *src, *dst; 29 int irc; 30 31 /* Set up the source string. */ 32 33 if ((src = ASN1_IA5STRING_new()) == NULL) 34 err(1, "FAIL: ASN1_IA5STRING_new() returned NULL"); 35 if (ASN1_STRING_set(src, data, -1) == 0) 36 err(1, "FAIL: ASN1_STRING_set(src) failed"); 37 if ((str = ASN1_STRING_get0_data(src)) == NULL) 38 errx(1, "FAIL: 1st ASN1_STRING_get0_data(src) returned NULL"); 39 if (strcmp(str, data)) 40 errx(1, "FAIL: 1st ASN1_STRING_get0_data(src) " 41 "returned wrong data: \"%s\" (expected \"%s\")", 42 str, data); 43 if ((irc = ASN1_STRING_length(src)) != (int)strlen(data)) 44 errx(1, "FAIL: 1st ASN1_STRING_length(src) " 45 "returned a wrong length: %d (expected %zu)", 46 irc, strlen(data)); 47 if ((irc = ASN1_STRING_type(src)) != V_ASN1_IA5STRING) 48 errx(1, "FAIL: 1st ASN1_STRING_type(src) " 49 "returned a wrong type: %d (expected %d)", 50 irc, V_ASN1_IA5STRING); 51 52 /* Set up the destination string. */ 53 54 if ((dst = ASN1_STRING_new()) == NULL) 55 err(1, "FAIL: ASN1_STRING_new() returned NULL"); 56 if ((str = ASN1_STRING_get0_data(dst)) != NULL) 57 errx(1, "FAIL: 1st ASN1_STRING_get0_data(dst) " 58 "returned \"%s\" (expected NULL)", str); 59 if ((irc = ASN1_STRING_length(dst)) != 0) 60 errx(1, "FAIL: 1st ASN1_STRING_length(dst) " 61 "returned a wrong length: %d (expected 0)", irc); 62 if ((irc = ASN1_STRING_type(dst)) != V_ASN1_OCTET_STRING) 63 errx(1, "FAIL: 1st ASN1_STRING_type(dst) " 64 "returned a wrong type: %d (expected %d)", 65 irc, V_ASN1_OCTET_STRING); 66 ASN1_STRING_length_set(dst, -1); 67 if ((str = ASN1_STRING_get0_data(dst)) != NULL) 68 errx(1, "FAIL: 2nd ASN1_STRING_get0_data(dst) " 69 "returned \"%s\" (expected NULL)", str); 70 if ((irc = ASN1_STRING_length(dst)) != -1) 71 errx(1, "FAIL: 2nd ASN1_STRING_length(dst) " 72 "returned a wrong length: %d (expected -1)", irc); 73 if ((irc = ASN1_STRING_type(dst)) != V_ASN1_OCTET_STRING) 74 errx(1, "FAIL: 2nd ASN1_STRING_type(dst) " 75 "returned a wrong type: %d (expected %d)", 76 irc, V_ASN1_OCTET_STRING); 77 78 /* Attempt to copy in the wrong direction. */ 79 80 if (ASN1_STRING_copy(src, dst) != 0) 81 errx(1, "FAIL: ASN1_STRING_copy unexpectedly succeeded"); 82 if ((str = ASN1_STRING_get0_data(src)) == NULL) 83 errx(1, "FAIL: 2nd ASN1_STRING_get0_data(src) returned NULL"); 84 if (strcmp(str, data)) 85 errx(1, "FAIL: 2nd ASN1_STRING_get0_data(src) " 86 "returned wrong data: \"%s\" (expected \"%s\")", 87 str, data); 88 if ((irc = ASN1_STRING_length(src)) != (int)strlen(data)) 89 errx(1, "FAIL: 2nd ASN1_STRING_length(src) " 90 "returned a wrong length: %d (expected %zu)", 91 irc, strlen(data)); 92 if ((irc = ASN1_STRING_type(src)) != V_ASN1_IA5STRING) 93 errx(1, "FAIL: 2nd ASN1_STRING_type(src) " 94 "returned a wrong type: %d (expected %d)", 95 irc, V_ASN1_IA5STRING); 96 97 /* Copy in the right direction. */ 98 99 if (ASN1_STRING_copy(dst, src) != 1) 100 err(1, "FAIL: ASN1_STRING_copy unexpectedly failed"); 101 if ((str = ASN1_STRING_get0_data(dst)) == NULL) 102 errx(1, "FAIL: 3rd ASN1_STRING_get0_data(dst) returned NULL"); 103 if (strcmp(str, data)) 104 errx(1, "FAIL: 3rd ASN1_STRING_get0_data(dst) " 105 "returned wrong data: \"%s\" (expected \"%s\")", 106 str, data); 107 if ((irc = ASN1_STRING_length(dst)) != (int)strlen(data)) 108 errx(1, "FAIL: 3rd ASN1_STRING_length(dst) " 109 "returned a wrong length: %d (expected %zu)", 110 irc, strlen(data)); 111 if ((irc = ASN1_STRING_type(dst)) != V_ASN1_IA5STRING) 112 errx(1, "FAIL: 3rd ASN1_STRING_type(dst) " 113 "returned a wrong type: %d (expected %d)", 114 irc, V_ASN1_IA5STRING); 115 116 ASN1_STRING_free(src); 117 ASN1_STRING_free(dst); 118 return 0; 119 } 120