Coverage Report

Created: 2020-08-14 21:15

/src/openssl/crypto/x509/x509name.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2020 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/safestack.h>
13
#include <openssl/asn1.h>
14
#include <openssl/objects.h>
15
#include <openssl/evp.h>
16
#include <openssl/x509.h>
17
#include "crypto/x509.h"
18
19
DEFINE_STACK_OF(X509_NAME_ENTRY)
20
0
21
0
int X509_NAME_get_text_by_NID(const X509_NAME *name, int nid,
22
0
                              char *buf, int len)
23
0
{
24
0
    ASN1_OBJECT *obj;
25
0
26
0
    obj = OBJ_nid2obj(nid);
27
0
    if (obj == NULL)
28
        return -1;
29
    return X509_NAME_get_text_by_OBJ(name, obj, buf, len);
30
}
31
0
32
0
int X509_NAME_get_text_by_OBJ(const X509_NAME *name, const ASN1_OBJECT *obj,
33
0
                              char *buf, int len)
34
0
{
35
0
    int i;
36
0
    const ASN1_STRING *data;
37
0
38
0
    i = X509_NAME_get_index_by_OBJ(name, obj, -1);
39
0
    if (i < 0)
40
0
        return -1;
41
0
    data = X509_NAME_ENTRY_get_data(X509_NAME_get_entry(name, i));
42
0
    if (buf == NULL)
43
0
        return data->length;
44
0
    if (len <= 0)
45
0
        return 0;
46
0
    i = (data->length > (len - 1)) ? (len - 1) : data->length;
47
0
    memcpy(buf, data->data, i);
48
    buf[i] = '\0';
49
    return i;
50
3.33k
}
51
3.33k
52
3.33k
int X509_NAME_entry_count(const X509_NAME *name)
53
3.33k
{
54
3.33k
    if (name == NULL)
55
        return 0;
56
    return sk_X509_NAME_ENTRY_num(name->entries);
57
0
}
58
0
59
0
int X509_NAME_get_index_by_NID(const X509_NAME *name, int nid, int lastpos)
60
0
{
61
0
    ASN1_OBJECT *obj;
62
0
63
0
    obj = OBJ_nid2obj(nid);
64
0
    if (obj == NULL)
65
        return -2;
66
    return X509_NAME_get_index_by_OBJ(name, obj, lastpos);
67
}
68
0
69
0
/* NOTE: you should be passing -1, not 0 as lastpos */
70
0
int X509_NAME_get_index_by_OBJ(const X509_NAME *name, const ASN1_OBJECT *obj,
71
0
                               int lastpos)
72
0
{
73
0
    int n;
74
0
    X509_NAME_ENTRY *ne;
75
0
    STACK_OF(X509_NAME_ENTRY) *sk;
76
0
77
0
    if (name == NULL)
78
0
        return -1;
79
0
    if (lastpos < 0)
80
0
        lastpos = -1;
81
0
    sk = name->entries;
82
0
    n = sk_X509_NAME_ENTRY_num(sk);
83
0
    for (lastpos++; lastpos < n; lastpos++) {
84
0
        ne = sk_X509_NAME_ENTRY_value(sk, lastpos);
85
0
        if (OBJ_cmp(ne->object, obj) == 0)
86
            return lastpos;
87
    }
88
469
    return -1;
89
469
}
90
469
91
0
X509_NAME_ENTRY *X509_NAME_get_entry(const X509_NAME *name, int loc)
92
469
{
93
469
    if (name == NULL || sk_X509_NAME_ENTRY_num(name->entries) <= loc
94
469
        || loc < 0)
95
        return NULL;
96
97
0
    return sk_X509_NAME_ENTRY_value(name->entries, loc);
98
0
}
99
0
100
0
X509_NAME_ENTRY *X509_NAME_delete_entry(X509_NAME *name, int loc)
101
0
{
102
0
    X509_NAME_ENTRY *ret;
103
0
    int i, n, set_prev, set_next;
104
0
    STACK_OF(X509_NAME_ENTRY) *sk;
105
0
106
0
    if (name == NULL || sk_X509_NAME_ENTRY_num(name->entries) <= loc
107
0
        || loc < 0)
108
0
        return NULL;
109
0
110
0
    sk = name->entries;
111
0
    ret = sk_X509_NAME_ENTRY_delete(sk, loc);
112
0
    n = sk_X509_NAME_ENTRY_num(sk);
113
0
    name->modified = 1;
114
0
    if (loc == n)
115
0
        return ret;
116
0
117
0
    /* else we need to fixup the set field */
118
0
    if (loc != 0)
119
0
        set_prev = (sk_X509_NAME_ENTRY_value(sk, loc - 1))->set;
120
0
    else
121
0
        set_prev = ret->set - 1;
122
0
    set_next = sk_X509_NAME_ENTRY_value(sk, loc)->set;
123
0
124
0
    /*-
125
0
     * set_prev is the previous set
126
0
     * set is the current set
127
0
     * set_next is the following
128
0
     * prev  1 1    1 1     1 1     1 1
129
0
     * set   1      1       2       2
130
0
     * next  1 1    2 2     2 2     3 2
131
0
     * so basically only if prev and next differ by 2, then
132
0
     * re-number down by 1
133
0
     */
134
0
    if (set_prev + 1 < set_next)
135
        for (i = loc; i < n; i++)
136
            sk_X509_NAME_ENTRY_value(sk, i)->set--;
137
    return ret;
138
}
139
0
140
0
int X509_NAME_add_entry_by_OBJ(X509_NAME *name, const ASN1_OBJECT *obj, int type,
141
0
                               const unsigned char *bytes, int len, int loc,
142
0
                               int set)
143
0
{
144
0
    X509_NAME_ENTRY *ne;
145
0
    int ret;
146
0
147
0
    ne = X509_NAME_ENTRY_create_by_OBJ(NULL, obj, type, bytes, len);
148
0
    if (!ne)
149
0
        return 0;
150
    ret = X509_NAME_add_entry(name, ne, loc, set);
151
    X509_NAME_ENTRY_free(ne);
152
    return ret;
153
}
154
0
155
0
int X509_NAME_add_entry_by_NID(X509_NAME *name, int nid, int type,
156
0
                               const unsigned char *bytes, int len, int loc,
157
0
                               int set)
158
0
{
159
0
    X509_NAME_ENTRY *ne;
160
0
    int ret;
161
0
    ne = X509_NAME_ENTRY_create_by_NID(NULL, nid, type, bytes, len);
162
0
    if (!ne)
163
0
        return 0;
164
    ret = X509_NAME_add_entry(name, ne, loc, set);
165
    X509_NAME_ENTRY_free(ne);
166
    return ret;
167
}
168
0
169
0
int X509_NAME_add_entry_by_txt(X509_NAME *name, const char *field, int type,
170
0
                               const unsigned char *bytes, int len, int loc,
171
0
                               int set)
172
0
{
173
0
    X509_NAME_ENTRY *ne;
174
0
    int ret;
175
0
    ne = X509_NAME_ENTRY_create_by_txt(NULL, field, type, bytes, len);
176
0
    if (!ne)
177
0
        return 0;
178
    ret = X509_NAME_add_entry(name, ne, loc, set);
179
    X509_NAME_ENTRY_free(ne);
180
    return ret;
181
}
182
183
/*
184
 * if set is -1, append to previous set, 0 'a new one', and 1, prepend to the
185
0
 * guy we are about to stomp on.
186
0
 */
187
0
int X509_NAME_add_entry(X509_NAME *name, const X509_NAME_ENTRY *ne, int loc,
188
0
                        int set)
189
0
{
190
0
    X509_NAME_ENTRY *new_name = NULL;
191
0
    int n, i, inc;
192
0
    STACK_OF(X509_NAME_ENTRY) *sk;
193
0
194
0
    if (name == NULL)
195
0
        return 0;
196
0
    sk = name->entries;
197
0
    n = sk_X509_NAME_ENTRY_num(sk);
198
0
    if (loc > n)
199
0
        loc = n;
200
0
    else if (loc < 0)
201
0
        loc = n;
202
0
    inc = (set == 0);
203
0
    name->modified = 1;
204
0
205
0
    if (set == -1) {
206
0
        if (loc == 0) {
207
0
            set = 0;
208
0
            inc = 1;
209
0
        } else {
210
0
            set = sk_X509_NAME_ENTRY_value(sk, loc - 1)->set;
211
0
        }
212
0
    } else {                    /* if (set >= 0) */
213
0
214
0
        if (loc >= n) {
215
0
            if (loc != 0)
216
0
                set = sk_X509_NAME_ENTRY_value(sk, loc - 1)->set + 1;
217
0
            else
218
0
                set = 0;
219
0
        } else
220
0
            set = sk_X509_NAME_ENTRY_value(sk, loc)->set;
221
0
    }
222
0
223
0
    if ((new_name = X509_NAME_ENTRY_dup(ne)) == NULL)
224
0
        goto err;
225
0
    new_name->set = set;
226
0
    if (!sk_X509_NAME_ENTRY_insert(sk, new_name, loc)) {
227
0
        X509err(X509_F_X509_NAME_ADD_ENTRY, ERR_R_MALLOC_FAILURE);
228
0
        goto err;
229
0
    }
230
0
    if (inc) {
231
0
        n = sk_X509_NAME_ENTRY_num(sk);
232
0
        for (i = loc + 1; i < n; i++)
233
0
            sk_X509_NAME_ENTRY_value(sk, i)->set += 1;
234
0
    }
235
0
    return 1;
236
0
 err:
237
0
    X509_NAME_ENTRY_free(new_name);
238
0
    return 0;
239
0
}
240
241
X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_txt(X509_NAME_ENTRY **ne,
242
                                               const char *field, int type,
243
                                               const unsigned char *bytes,
244
                                               int len)
245
0
{
246
0
    ASN1_OBJECT *obj;
247
0
    X509_NAME_ENTRY *nentry;
248
0
249
0
    obj = OBJ_txt2obj(field, 0);
250
0
    if (obj == NULL) {
251
0
        X509err(X509_F_X509_NAME_ENTRY_CREATE_BY_TXT,
252
0
                X509_R_INVALID_FIELD_NAME);
253
0
        ERR_add_error_data(2, "name=", field);
254
0
        return NULL;
255
0
    }
256
0
    nentry = X509_NAME_ENTRY_create_by_OBJ(ne, obj, type, bytes, len);
257
0
    ASN1_OBJECT_free(obj);
258
0
    return nentry;
259
0
}
260
261
X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_NID(X509_NAME_ENTRY **ne, int nid,
262
                                               int type,
263
                                               const unsigned char *bytes,
264
                                               int len)
265
0
{
266
0
    ASN1_OBJECT *obj;
267
0
    X509_NAME_ENTRY *nentry;
268
0
269
0
    obj = OBJ_nid2obj(nid);
270
0
    if (obj == NULL) {
271
0
        X509err(X509_F_X509_NAME_ENTRY_CREATE_BY_NID, X509_R_UNKNOWN_NID);
272
0
        return NULL;
273
0
    }
274
0
    nentry = X509_NAME_ENTRY_create_by_OBJ(ne, obj, type, bytes, len);
275
0
    ASN1_OBJECT_free(obj);
276
0
    return nentry;
277
0
}
278
279
X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_OBJ(X509_NAME_ENTRY **ne,
280
                                               const ASN1_OBJECT *obj, int type,
281
                                               const unsigned char *bytes,
282
                                               int len)
283
0
{
284
0
    X509_NAME_ENTRY *ret;
285
0
286
0
    if ((ne == NULL) || (*ne == NULL)) {
287
0
        if ((ret = X509_NAME_ENTRY_new()) == NULL)
288
0
            return NULL;
289
0
    } else
290
0
        ret = *ne;
291
0
292
0
    if (!X509_NAME_ENTRY_set_object(ret, obj))
293
0
        goto err;
294
0
    if (!X509_NAME_ENTRY_set_data(ret, type, bytes, len))
295
0
        goto err;
296
0
297
0
    if ((ne != NULL) && (*ne == NULL))
298
0
        *ne = ret;
299
0
    return ret;
300
0
 err:
301
0
    if ((ne == NULL) || (ret != *ne))
302
0
        X509_NAME_ENTRY_free(ret);
303
0
    return NULL;
304
0
}
305
306
int X509_NAME_ENTRY_set_object(X509_NAME_ENTRY *ne, const ASN1_OBJECT *obj)
307
0
{
308
0
    if ((ne == NULL) || (obj == NULL)) {
309
0
        X509err(X509_F_X509_NAME_ENTRY_SET_OBJECT,
310
0
                ERR_R_PASSED_NULL_PARAMETER);
311
0
        return 0;
312
0
    }
313
0
    ASN1_OBJECT_free(ne->object);
314
0
    ne->object = OBJ_dup(obj);
315
0
    return ((ne->object == NULL) ? 0 : 1);
316
0
}
317
318
int X509_NAME_ENTRY_set_data(X509_NAME_ENTRY *ne, int type,
319
                             const unsigned char *bytes, int len)
320
0
{
321
0
    int i;
322
0
323
0
    if ((ne == NULL) || ((bytes == NULL) && (len != 0)))
324
0
        return 0;
325
0
    if ((type > 0) && (type & MBSTRING_FLAG))
326
0
        return ASN1_STRING_set_by_NID(&ne->value, bytes,
327
0
                                      len, type,
328
0
                                      OBJ_obj2nid(ne->object)) ? 1 : 0;
329
0
    if (len < 0)
330
0
        len = strlen((const char *)bytes);
331
0
    i = ASN1_STRING_set(ne->value, bytes, len);
332
0
    if (!i)
333
0
        return 0;
334
0
    if (type != V_ASN1_UNDEF) {
335
0
        if (type == V_ASN1_APP_CHOOSE)
336
0
            ne->value->type = ASN1_PRINTABLE_type(bytes, len);
337
0
        else
338
0
            ne->value->type = type;
339
0
    }
340
0
    return 1;
341
0
}
342
343
ASN1_OBJECT *X509_NAME_ENTRY_get_object(const X509_NAME_ENTRY *ne)
344
469
{
345
469
    if (ne == NULL)
346
469
        return NULL;
347
469
    return ne->object;
348
469
}
349
350
ASN1_STRING *X509_NAME_ENTRY_get_data(const X509_NAME_ENTRY *ne)
351
469
{
352
469
    if (ne == NULL)
353
469
        return NULL;
354
469
    return ne->value;
355
469
}
356
357
int X509_NAME_ENTRY_set(const X509_NAME_ENTRY *ne)
358
794
{
359
794
    return ne->set;
360
794
}