1 /* $OpenBSD: x509attribute.c,v 1.3 2021/11/01 08:28:31 tb Exp $ */ 2 /* 3 * Copyright (c) 2020 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 <errno.h> 19 #include <stdio.h> 20 #include <string.h> 21 22 #include <openssl/err.h> 23 #include <openssl/x509.h> 24 25 void fail_head(const char *); 26 void fail_tail(void); 27 void fail_str(const char *, const char *); 28 void fail_int(const char *, int); 29 30 static const char *testname; 31 static int errcount; 32 33 void 34 fail_head(const char *stepname) 35 { 36 fprintf(stderr, "failure#%d testname=%s stepname=%s ", 37 ++errcount, testname, stepname); 38 } 39 40 void 41 fail_tail(void) 42 { 43 unsigned long errnum; 44 45 if ((errnum = ERR_get_error())) 46 fprintf(stderr, "OpenSSL says: %s\n", 47 ERR_error_string(errnum, NULL)); 48 if (errno) 49 fprintf(stderr, "libc says: %s\n", strerror(errno)); 50 } 51 52 void 53 fail_str(const char *stepname, const char *result) 54 { 55 fail_head(stepname); 56 fprintf(stderr, "wrong result=%s\n", result); 57 fail_tail(); 58 } 59 60 void 61 fail_int(const char *stepname, int result) 62 { 63 fail_head(stepname); 64 fprintf(stderr, "wrong result=%d\n", result); 65 fail_tail(); 66 } 67 68 int 69 main(void) 70 { 71 X509_ATTRIBUTE *attrib; 72 ASN1_TYPE *any; 73 ASN1_OBJECT *coid; 74 int num; 75 76 testname = "preparation"; 77 if ((coid = OBJ_nid2obj(NID_pkcs7_data)) == NULL) { 78 fail_str("OBJ_nid2obj", "NULL"); 79 return 1; 80 } 81 82 testname = "valid_args"; 83 if ((attrib = X509_ATTRIBUTE_create(NID_pkcs9_contentType, 84 V_ASN1_OBJECT, coid)) == NULL) 85 fail_str("X509_ATTRIBUTE_create", "NULL"); 86 else if (X509_ATTRIBUTE_get0_object(attrib) == NULL) 87 fail_str("X509_ATTRIBUTE_get0_object", "NULL"); 88 else if ((num = X509_ATTRIBUTE_count(attrib)) != 1) 89 fail_int("X509_ATTRIBUTE_count", num); 90 else if ((any = X509_ATTRIBUTE_get0_type(attrib, 0)) == NULL) 91 fail_str("X509_ATTRIBUTE_get0_type", "NULL"); 92 else if (any->type != V_ASN1_OBJECT) 93 fail_int("any->type", any->type); 94 else if (any->value.object != coid) 95 fail_str("value", "wrong pointer"); 96 X509_ATTRIBUTE_free(attrib); 97 98 testname = "bad_nid"; 99 if ((attrib = X509_ATTRIBUTE_create(-1, 100 V_ASN1_OBJECT, coid)) != NULL) 101 fail_str("X509_ATTRIBUTE_create", "not NULL"); 102 X509_ATTRIBUTE_free(attrib); 103 104 return errcount != 0; 105 } 106