Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/x509/x509_att.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (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 <openssl/x509v3.h>
18
#include "x509_lcl.h"
19
20
int X509at_get_attr_count(const STACK_OF(X509_ATTRIBUTE) *x)
21
0
{
22
0
    return sk_X509_ATTRIBUTE_num(x);
23
0
}
24
25
int X509at_get_attr_by_NID(const STACK_OF(X509_ATTRIBUTE) *x, int nid,
26
                           int lastpos)
27
0
{
28
0
    const ASN1_OBJECT *obj = OBJ_nid2obj(nid);
29
0
30
0
    if (obj == NULL)
31
0
        return -2;
32
0
    return X509at_get_attr_by_OBJ(x, obj, lastpos);
33
0
}
34
35
int X509at_get_attr_by_OBJ(const STACK_OF(X509_ATTRIBUTE) *sk,
36
                           const ASN1_OBJECT *obj, int lastpos)
37
0
{
38
0
    int n;
39
0
    X509_ATTRIBUTE *ex;
40
0
41
0
    if (sk == NULL)
42
0
        return -1;
43
0
    lastpos++;
44
0
    if (lastpos < 0)
45
0
        lastpos = 0;
46
0
    n = sk_X509_ATTRIBUTE_num(sk);
47
0
    for (; lastpos < n; lastpos++) {
48
0
        ex = sk_X509_ATTRIBUTE_value(sk, lastpos);
49
0
        if (OBJ_cmp(ex->object, obj) == 0)
50
0
            return lastpos;
51
0
    }
52
0
    return -1;
53
0
}
54
55
X509_ATTRIBUTE *X509at_get_attr(const STACK_OF(X509_ATTRIBUTE) *x, int loc)
56
0
{
57
0
    if (x == NULL || sk_X509_ATTRIBUTE_num(x) <= loc || loc < 0)
58
0
        return NULL;
59
0
60
0
    return sk_X509_ATTRIBUTE_value(x, loc);
61
0
}
62
63
X509_ATTRIBUTE *X509at_delete_attr(STACK_OF(X509_ATTRIBUTE) *x, int loc)
64
0
{
65
0
    X509_ATTRIBUTE *ret;
66
0
67
0
    if (x == NULL || sk_X509_ATTRIBUTE_num(x) <= loc || loc < 0)
68
0
        return NULL;
69
0
    ret = sk_X509_ATTRIBUTE_delete(x, loc);
70
0
    return ret;
71
0
}
72
73
STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr(STACK_OF(X509_ATTRIBUTE) **x,
74
                                           X509_ATTRIBUTE *attr)
75
0
{
76
0
    X509_ATTRIBUTE *new_attr = NULL;
77
0
    STACK_OF(X509_ATTRIBUTE) *sk = NULL;
78
0
79
0
    if (x == NULL) {
80
0
        X509err(X509_F_X509AT_ADD1_ATTR, ERR_R_PASSED_NULL_PARAMETER);
81
0
        goto err2;
82
0
    }
83
0
84
0
    if (*x == NULL) {
85
0
        if ((sk = sk_X509_ATTRIBUTE_new_null()) == NULL)
86
0
            goto err;
87
0
    } else
88
0
        sk = *x;
89
0
90
0
    if ((new_attr = X509_ATTRIBUTE_dup(attr)) == NULL)
91
0
        goto err2;
92
0
    if (!sk_X509_ATTRIBUTE_push(sk, new_attr))
93
0
        goto err;
94
0
    if (*x == NULL)
95
0
        *x = sk;
96
0
    return sk;
97
0
 err:
98
0
    X509err(X509_F_X509AT_ADD1_ATTR, ERR_R_MALLOC_FAILURE);
99
0
 err2:
100
0
    X509_ATTRIBUTE_free(new_attr);
101
0
    sk_X509_ATTRIBUTE_free(sk);
102
0
    return NULL;
103
0
}
104
105
STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_OBJ(STACK_OF(X509_ATTRIBUTE)
106
                                                  **x, const ASN1_OBJECT *obj,
107
                                                  int type,
108
                                                  const unsigned char *bytes,
109
                                                  int len)
110
0
{
111
0
    X509_ATTRIBUTE *attr;
112
0
    STACK_OF(X509_ATTRIBUTE) *ret;
113
0
    attr = X509_ATTRIBUTE_create_by_OBJ(NULL, obj, type, bytes, len);
114
0
    if (!attr)
115
0
        return 0;
116
0
    ret = X509at_add1_attr(x, attr);
117
0
    X509_ATTRIBUTE_free(attr);
118
0
    return ret;
119
0
}
120
121
STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_NID(STACK_OF(X509_ATTRIBUTE)
122
                                                  **x, int nid, int type,
123
                                                  const unsigned char *bytes,
124
                                                  int len)
125
0
{
126
0
    X509_ATTRIBUTE *attr;
127
0
    STACK_OF(X509_ATTRIBUTE) *ret;
128
0
    attr = X509_ATTRIBUTE_create_by_NID(NULL, nid, type, bytes, len);
129
0
    if (!attr)
130
0
        return 0;
131
0
    ret = X509at_add1_attr(x, attr);
132
0
    X509_ATTRIBUTE_free(attr);
133
0
    return ret;
134
0
}
135
136
STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_txt(STACK_OF(X509_ATTRIBUTE)
137
                                                  **x, const char *attrname,
138
                                                  int type,
139
                                                  const unsigned char *bytes,
140
                                                  int len)
141
0
{
142
0
    X509_ATTRIBUTE *attr;
143
0
    STACK_OF(X509_ATTRIBUTE) *ret;
144
0
    attr = X509_ATTRIBUTE_create_by_txt(NULL, attrname, type, bytes, len);
145
0
    if (!attr)
146
0
        return 0;
147
0
    ret = X509at_add1_attr(x, attr);
148
0
    X509_ATTRIBUTE_free(attr);
149
0
    return ret;
150
0
}
151
152
void *X509at_get0_data_by_OBJ(STACK_OF(X509_ATTRIBUTE) *x,
153
                              const ASN1_OBJECT *obj, int lastpos, int type)
154
0
{
155
0
    int i;
156
0
    X509_ATTRIBUTE *at;
157
0
    i = X509at_get_attr_by_OBJ(x, obj, lastpos);
158
0
    if (i == -1)
159
0
        return NULL;
160
0
    if ((lastpos <= -2) && (X509at_get_attr_by_OBJ(x, obj, i) != -1))
161
0
        return NULL;
162
0
    at = X509at_get_attr(x, i);
163
0
    if (lastpos <= -3 && (X509_ATTRIBUTE_count(at) != 1))
164
0
        return NULL;
165
0
    return X509_ATTRIBUTE_get0_data(at, 0, type, NULL);
166
0
}
167
168
X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_NID(X509_ATTRIBUTE **attr, int nid,
169
                                             int atrtype, const void *data,
170
                                             int len)
171
0
{
172
0
    ASN1_OBJECT *obj;
173
0
    X509_ATTRIBUTE *ret;
174
0
175
0
    obj = OBJ_nid2obj(nid);
176
0
    if (obj == NULL) {
177
0
        X509err(X509_F_X509_ATTRIBUTE_CREATE_BY_NID, X509_R_UNKNOWN_NID);
178
0
        return NULL;
179
0
    }
180
0
    ret = X509_ATTRIBUTE_create_by_OBJ(attr, obj, atrtype, data, len);
181
0
    if (ret == NULL)
182
0
        ASN1_OBJECT_free(obj);
183
0
    return ret;
184
0
}
185
186
X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_OBJ(X509_ATTRIBUTE **attr,
187
                                             const ASN1_OBJECT *obj,
188
                                             int atrtype, const void *data,
189
                                             int len)
190
0
{
191
0
    X509_ATTRIBUTE *ret;
192
0
193
0
    if ((attr == NULL) || (*attr == NULL)) {
194
0
        if ((ret = X509_ATTRIBUTE_new()) == NULL) {
195
0
            X509err(X509_F_X509_ATTRIBUTE_CREATE_BY_OBJ,
196
0
                    ERR_R_MALLOC_FAILURE);
197
0
            return NULL;
198
0
        }
199
0
    } else
200
0
        ret = *attr;
201
0
202
0
    if (!X509_ATTRIBUTE_set1_object(ret, obj))
203
0
        goto err;
204
0
    if (!X509_ATTRIBUTE_set1_data(ret, atrtype, data, len))
205
0
        goto err;
206
0
207
0
    if ((attr != NULL) && (*attr == NULL))
208
0
        *attr = ret;
209
0
    return ret;
210
0
 err:
211
0
    if ((attr == NULL) || (ret != *attr))
212
0
        X509_ATTRIBUTE_free(ret);
213
0
    return NULL;
214
0
}
215
216
X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_txt(X509_ATTRIBUTE **attr,
217
                                             const char *atrname, int type,
218
                                             const unsigned char *bytes,
219
                                             int len)
220
0
{
221
0
    ASN1_OBJECT *obj;
222
0
    X509_ATTRIBUTE *nattr;
223
0
224
0
    obj = OBJ_txt2obj(atrname, 0);
225
0
    if (obj == NULL) {
226
0
        X509err(X509_F_X509_ATTRIBUTE_CREATE_BY_TXT,
227
0
                X509_R_INVALID_FIELD_NAME);
228
0
        ERR_add_error_data(2, "name=", atrname);
229
0
        return NULL;
230
0
    }
231
0
    nattr = X509_ATTRIBUTE_create_by_OBJ(attr, obj, type, bytes, len);
232
0
    ASN1_OBJECT_free(obj);
233
0
    return nattr;
234
0
}
235
236
int X509_ATTRIBUTE_set1_object(X509_ATTRIBUTE *attr, const ASN1_OBJECT *obj)
237
0
{
238
0
    if ((attr == NULL) || (obj == NULL))
239
0
        return 0;
240
0
    ASN1_OBJECT_free(attr->object);
241
0
    attr->object = OBJ_dup(obj);
242
0
    return attr->object != NULL;
243
0
}
244
245
int X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE *attr, int attrtype,
246
                             const void *data, int len)
247
0
{
248
0
    ASN1_TYPE *ttmp = NULL;
249
0
    ASN1_STRING *stmp = NULL;
250
0
    int atype = 0;
251
0
    if (!attr)
252
0
        return 0;
253
0
    if (attrtype & MBSTRING_FLAG) {
254
0
        stmp = ASN1_STRING_set_by_NID(NULL, data, len, attrtype,
255
0
                                      OBJ_obj2nid(attr->object));
256
0
        if (!stmp) {
257
0
            X509err(X509_F_X509_ATTRIBUTE_SET1_DATA, ERR_R_ASN1_LIB);
258
0
            return 0;
259
0
        }
260
0
        atype = stmp->type;
261
0
    } else if (len != -1) {
262
0
        if ((stmp = ASN1_STRING_type_new(attrtype)) == NULL)
263
0
            goto err;
264
0
        if (!ASN1_STRING_set(stmp, data, len))
265
0
            goto err;
266
0
        atype = attrtype;
267
0
    }
268
0
    /*
269
0
     * This is a bit naughty because the attribute should really have at
270
0
     * least one value but some types use and zero length SET and require
271
0
     * this.
272
0
     */
273
0
    if (attrtype == 0) {
274
0
        ASN1_STRING_free(stmp);
275
0
        return 1;
276
0
    }
277
0
    if ((ttmp = ASN1_TYPE_new()) == NULL)
278
0
        goto err;
279
0
    if ((len == -1) && !(attrtype & MBSTRING_FLAG)) {
280
0
        if (!ASN1_TYPE_set1(ttmp, attrtype, data))
281
0
            goto err;
282
0
    } else {
283
0
        ASN1_TYPE_set(ttmp, atype, stmp);
284
0
        stmp = NULL;
285
0
    }
286
0
    if (!sk_ASN1_TYPE_push(attr->set, ttmp))
287
0
        goto err;
288
0
    return 1;
289
0
 err:
290
0
    X509err(X509_F_X509_ATTRIBUTE_SET1_DATA, ERR_R_MALLOC_FAILURE);
291
0
    ASN1_TYPE_free(ttmp);
292
0
    ASN1_STRING_free(stmp);
293
0
    return 0;
294
0
}
295
296
int X509_ATTRIBUTE_count(const X509_ATTRIBUTE *attr)
297
0
{
298
0
    if (attr == NULL)
299
0
        return 0;
300
0
    return sk_ASN1_TYPE_num(attr->set);
301
0
}
302
303
ASN1_OBJECT *X509_ATTRIBUTE_get0_object(X509_ATTRIBUTE *attr)
304
0
{
305
0
    if (attr == NULL)
306
0
        return NULL;
307
0
    return attr->object;
308
0
}
309
310
void *X509_ATTRIBUTE_get0_data(X509_ATTRIBUTE *attr, int idx,
311
                               int atrtype, void *data)
312
0
{
313
0
    ASN1_TYPE *ttmp;
314
0
    ttmp = X509_ATTRIBUTE_get0_type(attr, idx);
315
0
    if (!ttmp)
316
0
        return NULL;
317
0
    if (atrtype != ASN1_TYPE_get(ttmp)) {
318
0
        X509err(X509_F_X509_ATTRIBUTE_GET0_DATA, X509_R_WRONG_TYPE);
319
0
        return NULL;
320
0
    }
321
0
    return ttmp->value.ptr;
322
0
}
323
324
ASN1_TYPE *X509_ATTRIBUTE_get0_type(X509_ATTRIBUTE *attr, int idx)
325
0
{
326
0
    if (attr == NULL)
327
0
        return NULL;
328
0
    return sk_ASN1_TYPE_value(attr->set, idx);
329
0
}