Coverage Report

Created: 2025-06-13 06:58

/src/openssl31/crypto/x509/v3_genn.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1999-2023 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <stdio.h>
11
#include "internal/cryptlib.h"
12
#include <openssl/asn1t.h>
13
#include <openssl/conf.h>
14
#include <openssl/x509v3.h>
15
16
ASN1_SEQUENCE(OTHERNAME) = {
17
        ASN1_SIMPLE(OTHERNAME, type_id, ASN1_OBJECT),
18
        /* Maybe have a true ANY DEFINED BY later */
19
        ASN1_EXP(OTHERNAME, value, ASN1_ANY, 0)
20
} ASN1_SEQUENCE_END(OTHERNAME)
21
22
IMPLEMENT_ASN1_FUNCTIONS(OTHERNAME)
23
24
ASN1_SEQUENCE(EDIPARTYNAME) = {
25
        /* DirectoryString is a CHOICE type so use explicit tagging */
26
        ASN1_EXP_OPT(EDIPARTYNAME, nameAssigner, DIRECTORYSTRING, 0),
27
        ASN1_EXP(EDIPARTYNAME, partyName, DIRECTORYSTRING, 1)
28
} ASN1_SEQUENCE_END(EDIPARTYNAME)
29
30
IMPLEMENT_ASN1_FUNCTIONS(EDIPARTYNAME)
31
32
ASN1_CHOICE(GENERAL_NAME) = {
33
        ASN1_IMP(GENERAL_NAME, d.otherName, OTHERNAME, GEN_OTHERNAME),
34
        ASN1_IMP(GENERAL_NAME, d.rfc822Name, ASN1_IA5STRING, GEN_EMAIL),
35
        ASN1_IMP(GENERAL_NAME, d.dNSName, ASN1_IA5STRING, GEN_DNS),
36
        /* Don't decode this */
37
        ASN1_IMP(GENERAL_NAME, d.x400Address, ASN1_SEQUENCE, GEN_X400),
38
        /* X509_NAME is a CHOICE type so use EXPLICIT */
39
        ASN1_EXP(GENERAL_NAME, d.directoryName, X509_NAME, GEN_DIRNAME),
40
        ASN1_IMP(GENERAL_NAME, d.ediPartyName, EDIPARTYNAME, GEN_EDIPARTY),
41
        ASN1_IMP(GENERAL_NAME, d.uniformResourceIdentifier, ASN1_IA5STRING, GEN_URI),
42
        ASN1_IMP(GENERAL_NAME, d.iPAddress, ASN1_OCTET_STRING, GEN_IPADD),
43
        ASN1_IMP(GENERAL_NAME, d.registeredID, ASN1_OBJECT, GEN_RID)
44
} ASN1_CHOICE_END(GENERAL_NAME)
45
46
IMPLEMENT_ASN1_FUNCTIONS(GENERAL_NAME)
47
48
ASN1_ITEM_TEMPLATE(GENERAL_NAMES) =
49
        ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, GeneralNames, GENERAL_NAME)
50
ASN1_ITEM_TEMPLATE_END(GENERAL_NAMES)
51
52
IMPLEMENT_ASN1_FUNCTIONS(GENERAL_NAMES)
53
54
GENERAL_NAME *GENERAL_NAME_dup(const GENERAL_NAME *a)
55
0
{
56
0
    return (GENERAL_NAME *)ASN1_dup((i2d_of_void *)i2d_GENERAL_NAME,
57
0
                                    (d2i_of_void *)d2i_GENERAL_NAME,
58
0
                                    (char *)a);
59
0
}
60
61
static int edipartyname_cmp(const EDIPARTYNAME *a, const EDIPARTYNAME *b)
62
20
{
63
20
    int res;
64
65
20
    if (a == NULL || b == NULL) {
66
        /*
67
         * Shouldn't be possible in a valid GENERAL_NAME, but we handle it
68
         * anyway. OTHERNAME_cmp treats NULL != NULL so we do the same here
69
         */
70
0
        return -1;
71
0
    }
72
20
    if (a->nameAssigner == NULL && b->nameAssigner != NULL)
73
0
        return -1;
74
20
    if (a->nameAssigner != NULL && b->nameAssigner == NULL)
75
0
        return 1;
76
    /* If we get here then both have nameAssigner set, or both unset */
77
20
    if (a->nameAssigner != NULL) {
78
4
        res = ASN1_STRING_cmp(a->nameAssigner, b->nameAssigner);
79
4
        if (res != 0)
80
0
            return res;
81
4
    }
82
    /*
83
     * partyName is required, so these should never be NULL. We treat it in
84
     * the same way as the a == NULL || b == NULL case above
85
     */
86
20
    if (a->partyName == NULL || b->partyName == NULL)
87
0
        return -1;
88
89
20
    return ASN1_STRING_cmp(a->partyName, b->partyName);
90
20
}
91
92
/* Returns 0 if they are equal, != 0 otherwise. */
93
int GENERAL_NAME_cmp(GENERAL_NAME *a, GENERAL_NAME *b)
94
5.33k
{
95
5.33k
    int result = -1;
96
97
5.33k
    if (!a || !b || a->type != b->type)
98
4.36k
        return -1;
99
973
    switch (a->type) {
100
132
    case GEN_X400:
101
132
        result = ASN1_STRING_cmp(a->d.x400Address, b->d.x400Address);
102
132
        break;
103
104
20
    case GEN_EDIPARTY:
105
20
        result = edipartyname_cmp(a->d.ediPartyName, b->d.ediPartyName);
106
20
        break;
107
108
32
    case GEN_OTHERNAME:
109
32
        result = OTHERNAME_cmp(a->d.otherName, b->d.otherName);
110
32
        break;
111
112
25
    case GEN_EMAIL:
113
54
    case GEN_DNS:
114
101
    case GEN_URI:
115
101
        result = ASN1_STRING_cmp(a->d.ia5, b->d.ia5);
116
101
        break;
117
118
337
    case GEN_DIRNAME:
119
337
        result = X509_NAME_cmp(a->d.dirn, b->d.dirn);
120
337
        break;
121
122
39
    case GEN_IPADD:
123
39
        result = ASN1_OCTET_STRING_cmp(a->d.ip, b->d.ip);
124
39
        break;
125
126
312
    case GEN_RID:
127
312
        result = OBJ_cmp(a->d.rid, b->d.rid);
128
312
        break;
129
973
    }
130
973
    return result;
131
973
}
132
133
/* Returns 0 if they are equal, != 0 otherwise. */
134
int OTHERNAME_cmp(OTHERNAME *a, OTHERNAME *b)
135
32
{
136
32
    int result = -1;
137
138
32
    if (!a || !b)
139
0
        return -1;
140
    /* Check their type first. */
141
32
    if ((result = OBJ_cmp(a->type_id, b->type_id)) != 0)
142
0
        return result;
143
    /* Check the value. */
144
32
    result = ASN1_TYPE_cmp(a->value, b->value);
145
32
    return result;
146
32
}
147
148
void GENERAL_NAME_set0_value(GENERAL_NAME *a, int type, void *value)
149
0
{
150
0
    switch (type) {
151
0
    case GEN_X400:
152
0
        a->d.x400Address = value;
153
0
        break;
154
155
0
    case GEN_EDIPARTY:
156
0
        a->d.ediPartyName = value;
157
0
        break;
158
159
0
    case GEN_OTHERNAME:
160
0
        a->d.otherName = value;
161
0
        break;
162
163
0
    case GEN_EMAIL:
164
0
    case GEN_DNS:
165
0
    case GEN_URI:
166
0
        a->d.ia5 = value;
167
0
        break;
168
169
0
    case GEN_DIRNAME:
170
0
        a->d.dirn = value;
171
0
        break;
172
173
0
    case GEN_IPADD:
174
0
        a->d.ip = value;
175
0
        break;
176
177
0
    case GEN_RID:
178
0
        a->d.rid = value;
179
0
        break;
180
0
    }
181
0
    a->type = type;
182
0
}
183
184
void *GENERAL_NAME_get0_value(const GENERAL_NAME *a, int *ptype)
185
0
{
186
0
    if (ptype)
187
0
        *ptype = a->type;
188
0
    switch (a->type) {
189
0
    case GEN_X400:
190
0
        return a->d.x400Address;
191
192
0
    case GEN_EDIPARTY:
193
0
        return a->d.ediPartyName;
194
195
0
    case GEN_OTHERNAME:
196
0
        return a->d.otherName;
197
198
0
    case GEN_EMAIL:
199
0
    case GEN_DNS:
200
0
    case GEN_URI:
201
0
        return a->d.ia5;
202
203
0
    case GEN_DIRNAME:
204
0
        return a->d.dirn;
205
206
0
    case GEN_IPADD:
207
0
        return a->d.ip;
208
209
0
    case GEN_RID:
210
0
        return a->d.rid;
211
212
0
    default:
213
0
        return NULL;
214
0
    }
215
0
}
216
217
int GENERAL_NAME_set0_othername(GENERAL_NAME *gen,
218
                                ASN1_OBJECT *oid, ASN1_TYPE *value)
219
0
{
220
0
    OTHERNAME *oth;
221
0
    oth = OTHERNAME_new();
222
0
    if (oth == NULL)
223
0
        return 0;
224
0
    ASN1_TYPE_free(oth->value);
225
0
    oth->type_id = oid;
226
0
    oth->value = value;
227
0
    GENERAL_NAME_set0_value(gen, GEN_OTHERNAME, oth);
228
0
    return 1;
229
0
}
230
231
int GENERAL_NAME_get0_otherName(const GENERAL_NAME *gen,
232
                                ASN1_OBJECT **poid, ASN1_TYPE **pvalue)
233
0
{
234
0
    if (gen->type != GEN_OTHERNAME)
235
0
        return 0;
236
0
    if (poid)
237
0
        *poid = gen->d.otherName->type_id;
238
0
    if (pvalue)
239
0
        *pvalue = gen->d.otherName->value;
240
0
    return 1;
241
0
}