Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/x509/x509_v3.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2018 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 X509v3_get_ext_count(const STACK_OF(X509_EXTENSION) *x)
21
0
{
22
0
    if (x == NULL)
23
0
        return 0;
24
0
    return sk_X509_EXTENSION_num(x);
25
0
}
26
27
int X509v3_get_ext_by_NID(const STACK_OF(X509_EXTENSION) *x, int nid,
28
                          int lastpos)
29
0
{
30
0
    ASN1_OBJECT *obj;
31
0
32
0
    obj = OBJ_nid2obj(nid);
33
0
    if (obj == NULL)
34
0
        return -2;
35
0
    return X509v3_get_ext_by_OBJ(x, obj, lastpos);
36
0
}
37
38
int X509v3_get_ext_by_OBJ(const STACK_OF(X509_EXTENSION) *sk,
39
                          const ASN1_OBJECT *obj, int lastpos)
40
0
{
41
0
    int n;
42
0
    X509_EXTENSION *ex;
43
0
44
0
    if (sk == NULL)
45
0
        return -1;
46
0
    lastpos++;
47
0
    if (lastpos < 0)
48
0
        lastpos = 0;
49
0
    n = sk_X509_EXTENSION_num(sk);
50
0
    for (; lastpos < n; lastpos++) {
51
0
        ex = sk_X509_EXTENSION_value(sk, lastpos);
52
0
        if (OBJ_cmp(ex->object, obj) == 0)
53
0
            return lastpos;
54
0
    }
55
0
    return -1;
56
0
}
57
58
int X509v3_get_ext_by_critical(const STACK_OF(X509_EXTENSION) *sk, int crit,
59
                               int lastpos)
60
0
{
61
0
    int n;
62
0
    X509_EXTENSION *ex;
63
0
64
0
    if (sk == NULL)
65
0
        return -1;
66
0
    lastpos++;
67
0
    if (lastpos < 0)
68
0
        lastpos = 0;
69
0
    n = sk_X509_EXTENSION_num(sk);
70
0
    for (; lastpos < n; lastpos++) {
71
0
        ex = sk_X509_EXTENSION_value(sk, lastpos);
72
0
        if (((ex->critical > 0) && crit) || ((ex->critical <= 0) && !crit))
73
0
            return lastpos;
74
0
    }
75
0
    return -1;
76
0
}
77
78
X509_EXTENSION *X509v3_get_ext(const STACK_OF(X509_EXTENSION) *x, int loc)
79
0
{
80
0
    if (x == NULL || sk_X509_EXTENSION_num(x) <= loc || loc < 0)
81
0
        return NULL;
82
0
    else
83
0
        return sk_X509_EXTENSION_value(x, loc);
84
0
}
85
86
X509_EXTENSION *X509v3_delete_ext(STACK_OF(X509_EXTENSION) *x, int loc)
87
0
{
88
0
    X509_EXTENSION *ret;
89
0
90
0
    if (x == NULL || sk_X509_EXTENSION_num(x) <= loc || loc < 0)
91
0
        return NULL;
92
0
    ret = sk_X509_EXTENSION_delete(x, loc);
93
0
    return ret;
94
0
}
95
96
STACK_OF(X509_EXTENSION) *X509v3_add_ext(STACK_OF(X509_EXTENSION) **x,
97
                                         X509_EXTENSION *ex, int loc)
98
0
{
99
0
    X509_EXTENSION *new_ex = NULL;
100
0
    int n;
101
0
    STACK_OF(X509_EXTENSION) *sk = NULL;
102
0
103
0
    if (x == NULL) {
104
0
        X509err(X509_F_X509V3_ADD_EXT, ERR_R_PASSED_NULL_PARAMETER);
105
0
        goto err2;
106
0
    }
107
0
108
0
    if (*x == NULL) {
109
0
        if ((sk = sk_X509_EXTENSION_new_null()) == NULL)
110
0
            goto err;
111
0
    } else
112
0
        sk = *x;
113
0
114
0
    n = sk_X509_EXTENSION_num(sk);
115
0
    if (loc > n)
116
0
        loc = n;
117
0
    else if (loc < 0)
118
0
        loc = n;
119
0
120
0
    if ((new_ex = X509_EXTENSION_dup(ex)) == NULL)
121
0
        goto err2;
122
0
    if (!sk_X509_EXTENSION_insert(sk, new_ex, loc))
123
0
        goto err;
124
0
    if (*x == NULL)
125
0
        *x = sk;
126
0
    return sk;
127
0
 err:
128
0
    X509err(X509_F_X509V3_ADD_EXT, ERR_R_MALLOC_FAILURE);
129
0
 err2:
130
0
    X509_EXTENSION_free(new_ex);
131
0
    if (x != NULL && *x == NULL)
132
0
        sk_X509_EXTENSION_free(sk);
133
0
    return NULL;
134
0
}
135
136
X509_EXTENSION *X509_EXTENSION_create_by_NID(X509_EXTENSION **ex, int nid,
137
                                             int crit,
138
                                             ASN1_OCTET_STRING *data)
139
0
{
140
0
    ASN1_OBJECT *obj;
141
0
    X509_EXTENSION *ret;
142
0
143
0
    obj = OBJ_nid2obj(nid);
144
0
    if (obj == NULL) {
145
0
        X509err(X509_F_X509_EXTENSION_CREATE_BY_NID, X509_R_UNKNOWN_NID);
146
0
        return NULL;
147
0
    }
148
0
    ret = X509_EXTENSION_create_by_OBJ(ex, obj, crit, data);
149
0
    if (ret == NULL)
150
0
        ASN1_OBJECT_free(obj);
151
0
    return ret;
152
0
}
153
154
X509_EXTENSION *X509_EXTENSION_create_by_OBJ(X509_EXTENSION **ex,
155
                                             const ASN1_OBJECT *obj, int crit,
156
                                             ASN1_OCTET_STRING *data)
157
0
{
158
0
    X509_EXTENSION *ret;
159
0
160
0
    if ((ex == NULL) || (*ex == NULL)) {
161
0
        if ((ret = X509_EXTENSION_new()) == NULL) {
162
0
            X509err(X509_F_X509_EXTENSION_CREATE_BY_OBJ,
163
0
                    ERR_R_MALLOC_FAILURE);
164
0
            return NULL;
165
0
        }
166
0
    } else
167
0
        ret = *ex;
168
0
169
0
    if (!X509_EXTENSION_set_object(ret, obj))
170
0
        goto err;
171
0
    if (!X509_EXTENSION_set_critical(ret, crit))
172
0
        goto err;
173
0
    if (!X509_EXTENSION_set_data(ret, data))
174
0
        goto err;
175
0
176
0
    if ((ex != NULL) && (*ex == NULL))
177
0
        *ex = ret;
178
0
    return ret;
179
0
 err:
180
0
    if ((ex == NULL) || (ret != *ex))
181
0
        X509_EXTENSION_free(ret);
182
0
    return NULL;
183
0
}
184
185
int X509_EXTENSION_set_object(X509_EXTENSION *ex, const ASN1_OBJECT *obj)
186
0
{
187
0
    if ((ex == NULL) || (obj == NULL))
188
0
        return 0;
189
0
    ASN1_OBJECT_free(ex->object);
190
0
    ex->object = OBJ_dup(obj);
191
0
    return ex->object != NULL;
192
0
}
193
194
int X509_EXTENSION_set_critical(X509_EXTENSION *ex, int crit)
195
0
{
196
0
    if (ex == NULL)
197
0
        return 0;
198
0
    ex->critical = (crit) ? 0xFF : -1;
199
0
    return 1;
200
0
}
201
202
int X509_EXTENSION_set_data(X509_EXTENSION *ex, ASN1_OCTET_STRING *data)
203
0
{
204
0
    int i;
205
0
206
0
    if (ex == NULL)
207
0
        return 0;
208
0
    i = ASN1_OCTET_STRING_set(&ex->value, data->data, data->length);
209
0
    if (!i)
210
0
        return 0;
211
0
    return 1;
212
0
}
213
214
ASN1_OBJECT *X509_EXTENSION_get_object(X509_EXTENSION *ex)
215
0
{
216
0
    if (ex == NULL)
217
0
        return NULL;
218
0
    return ex->object;
219
0
}
220
221
ASN1_OCTET_STRING *X509_EXTENSION_get_data(X509_EXTENSION *ex)
222
0
{
223
0
    if (ex == NULL)
224
0
        return NULL;
225
0
    return &ex->value;
226
0
}
227
228
int X509_EXTENSION_get_critical(const X509_EXTENSION *ex)
229
0
{
230
0
    if (ex == NULL)
231
0
        return 0;
232
0
    if (ex->critical > 0)
233
0
        return 1;
234
0
    return 0;
235
0
}