aboutsummaryrefslogtreecommitdiff
path: root/test/x509_acert_test.c
blob: 0099d4629e15b16bcca1df5b814c7f8924da720e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * Copyright 2021 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the Apache License 2.0 (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

#include <openssl/pem.h>
#include <openssl/rsa.h>
#include <openssl/x509_acert.h>

#include "testutil.h"

static int test_print_acert(int idx)
{
    int ret = 0;
    const char *acert_file;
    X509_ACERT *acert = NULL;
    BIO *bp, *bout;

    if (!TEST_ptr(acert_file = test_get_argument(idx)))
        return 0;

    if (!TEST_ptr(bp = BIO_new_file(acert_file, "r")))
        return 0;

    if (!TEST_ptr(bout = BIO_new_fp(stderr, BIO_NOCLOSE)))
        goto err;

    if (!TEST_ptr(acert = PEM_read_bio_X509_ACERT(bp, NULL, NULL, NULL)))
        goto err;

    if (!TEST_int_eq(X509_ACERT_print(bout, acert), 1)) {
        goto err;
    }

    ret = 1;

err:
    BIO_free(bp);
    BIO_free(bout);
    X509_ACERT_free(acert);
    return ret;
}

static int test_acert_sign(void)
{
    int ret = 0;
    const char *acert_file;
    EVP_PKEY *pkey;
    BIO *bp = NULL;
    X509_ACERT *acert = NULL;

    if (!TEST_ptr(acert_file = test_get_argument(0)))
        return 0;

    if (!TEST_ptr(pkey = EVP_RSA_gen(2048)))
        return 0;

    if (!TEST_ptr(bp = BIO_new_file(acert_file, "r")))
        goto err;

    if (!TEST_ptr(acert = PEM_read_bio_X509_ACERT(bp, NULL, NULL, NULL)))
        goto err;

    if (!TEST_int_gt(X509_ACERT_sign(acert, pkey, EVP_sha256()), 0) ||
        !TEST_int_eq(X509_ACERT_verify(acert, pkey), 1))
        goto err;

    ret = 1;

err:
    BIO_free(bp);
    X509_ACERT_free(acert);
    EVP_PKEY_free(pkey);
    return ret;
}

/* IetfAttrSyntax structure with one value */
static const unsigned char attr_syntax_single[] = {
  0x30, 0x15, 0xa0, 0x09, 0x86, 0x07, 0x54, 0x65, 0x73, 0x74, 0x76, 0x61,
  0x6c, 0x30, 0x08, 0x0c, 0x06, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x31
};

/* IetfAttrSyntax structure with multiple values of the same type */
static const unsigned char attr_syntax_multiple[] = {
  0x30, 0x1d, 0x30, 0x1b, 0x0c, 0x07, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x20,
  0x31, 0x0c, 0x07, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x20, 0x32, 0x0c, 0x07,
  0x67, 0x72, 0x6f, 0x75, 0x70, 0x20, 0x33
};

/* IetfAttrSyntax structure with multiple values of different types */
static const unsigned char attr_syntax_diff_type[] = {
  0x30, 0x11, 0x30, 0x0f, 0x04, 0x08, 0x64, 0x65, 0x61, 0x64, 0x63, 0x6f,
  0x64, 0x65, 0x0c, 0x03, 0x61, 0x61, 0x61
};

/* IetfAttrSyntax structure with an invalid/unsupported value type */
static const unsigned char attr_syntax_invalid_type[] = {
  0x30, 0x05, 0x30, 0x03, 0x02, 0x01, 0x0a
};

#define ADD_TEST_DATA(x, valid) {x, sizeof(x), valid}

struct ietf_type_test_data {
    const unsigned char *data;
    size_t len;
    int valid;
};

static const struct ietf_type_test_data ietf_syntax_tests[] = {
    ADD_TEST_DATA(attr_syntax_single, 1),
    ADD_TEST_DATA(attr_syntax_multiple, 1),
    ADD_TEST_DATA(attr_syntax_diff_type, 0),
    ADD_TEST_DATA(attr_syntax_invalid_type, 0),
};

static int test_object_group_attr(int idx)
{
    int ret = 0;
    OSSL_IETF_ATTR_SYNTAX *ias = NULL;
    BIO *bout = NULL;
    const unsigned char *p;
    const struct ietf_type_test_data *test = &ietf_syntax_tests[idx];

    if (!TEST_ptr(bout = BIO_new_fp(stderr, BIO_NOCLOSE)))
        goto done;

    p = test->data;

    ias = d2i_OSSL_IETF_ATTR_SYNTAX(NULL, &p, test->len);

    if ((test->valid && !TEST_ptr(ias))
            || (!test->valid && !TEST_ptr_null(ias)))
        goto done;

    if (ias != NULL
            && !TEST_int_eq(OSSL_IETF_ATTR_SYNTAX_print(bout, ias, 4), 1)) {
        OSSL_IETF_ATTR_SYNTAX_free(ias);
        goto done;
    }

    ret = 1;

done:
    OSSL_IETF_ATTR_SYNTAX_free(ias);
    BIO_free(bout);
    return ret;
}

OPT_TEST_DECLARE_USAGE("[<attribute certs (PEM)>...]\n")
int setup_tests(void)
{
    int cnt;

    if (!test_skip_common_options()) {
        TEST_error("Error parsing test options\n");
        return 0;
    }

    cnt = test_get_argument_count();
    if (cnt < 1) {
        TEST_error("Must specify at least 1 attribute certificate file\n");
        return 0;
    }

    ADD_ALL_TESTS(test_print_acert, cnt);
    ADD_TEST(test_acert_sign);
    ADD_ALL_TESTS(test_object_group_attr, OSSL_NELEM(ietf_syntax_tests));

    return 1;
}