]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/x509/x509_v3.c
Raise an error on syscall failure in tls_retry_write_records
[thirdparty/openssl.git] / crypto / x509 / x509_v3.c
CommitLineData
b1322259 1/*
454afd98 2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
3e4b43b9 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
b1322259
RS
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
d02b48c6
RE
8 */
9
10#include <stdio.h>
b39fc560 11#include "internal/cryptlib.h"
1f5e0f92 12#include <openssl/safestack.h>
ec577822
BM
13#include <openssl/asn1.h>
14#include <openssl/objects.h>
15#include <openssl/evp.h>
16#include <openssl/x509.h>
13938ace 17#include <openssl/x509v3.h>
706457b7 18#include "x509_local.h"
d02b48c6 19
0b3f827c 20int X509v3_get_ext_count(const STACK_OF(X509_EXTENSION) *x)
0f113f3e 21{
dd1f2842
PH
22 int ret;
23
0f113f3e 24 if (x == NULL)
26a7d938 25 return 0;
dd1f2842
PH
26 ret = sk_X509_EXTENSION_num(x);
27 return ret > 0 ? ret : 0;
0f113f3e 28}
d02b48c6 29
0b3f827c 30int X509v3_get_ext_by_NID(const STACK_OF(X509_EXTENSION) *x, int nid,
0f113f3e
MC
31 int lastpos)
32{
33 ASN1_OBJECT *obj;
d02b48c6 34
0f113f3e
MC
35 obj = OBJ_nid2obj(nid);
36 if (obj == NULL)
26a7d938
K
37 return -2;
38 return X509v3_get_ext_by_OBJ(x, obj, lastpos);
0f113f3e 39}
d02b48c6 40
0f113f3e 41int X509v3_get_ext_by_OBJ(const STACK_OF(X509_EXTENSION) *sk,
c47ba4e9 42 const ASN1_OBJECT *obj, int lastpos)
0f113f3e
MC
43{
44 int n;
45 X509_EXTENSION *ex;
d02b48c6 46
0f113f3e 47 if (sk == NULL)
26a7d938 48 return -1;
0f113f3e
MC
49 lastpos++;
50 if (lastpos < 0)
51 lastpos = 0;
52 n = sk_X509_EXTENSION_num(sk);
53 for (; lastpos < n; lastpos++) {
54 ex = sk_X509_EXTENSION_value(sk, lastpos);
55 if (OBJ_cmp(ex->object, obj) == 0)
26a7d938 56 return lastpos;
0f113f3e 57 }
26a7d938 58 return -1;
0f113f3e 59}
d02b48c6 60
0b3f827c 61int X509v3_get_ext_by_critical(const STACK_OF(X509_EXTENSION) *sk, int crit,
0f113f3e
MC
62 int lastpos)
63{
64 int n;
65 X509_EXTENSION *ex;
d02b48c6 66
0f113f3e 67 if (sk == NULL)
26a7d938 68 return -1;
0f113f3e
MC
69 lastpos++;
70 if (lastpos < 0)
71 lastpos = 0;
72 n = sk_X509_EXTENSION_num(sk);
73 for (; lastpos < n; lastpos++) {
74 ex = sk_X509_EXTENSION_value(sk, lastpos);
75 if (((ex->critical > 0) && crit) || ((ex->critical <= 0) && !crit))
26a7d938 76 return lastpos;
0f113f3e 77 }
26a7d938 78 return -1;
0f113f3e 79}
d02b48c6 80
0b3f827c 81X509_EXTENSION *X509v3_get_ext(const STACK_OF(X509_EXTENSION) *x, int loc)
0f113f3e
MC
82{
83 if (x == NULL || sk_X509_EXTENSION_num(x) <= loc || loc < 0)
84 return NULL;
85 else
86 return sk_X509_EXTENSION_value(x, loc);
87}
d02b48c6 88
0b3f827c 89X509_EXTENSION *X509v3_delete_ext(STACK_OF(X509_EXTENSION) *x, int loc)
0f113f3e
MC
90{
91 X509_EXTENSION *ret;
d02b48c6 92
0f113f3e 93 if (x == NULL || sk_X509_EXTENSION_num(x) <= loc || loc < 0)
26a7d938 94 return NULL;
0f113f3e 95 ret = sk_X509_EXTENSION_delete(x, loc);
26a7d938 96 return ret;
0f113f3e 97}
d02b48c6 98
0b3f827c 99STACK_OF(X509_EXTENSION) *X509v3_add_ext(STACK_OF(X509_EXTENSION) **x,
0f113f3e
MC
100 X509_EXTENSION *ex, int loc)
101{
102 X509_EXTENSION *new_ex = NULL;
103 int n;
104 STACK_OF(X509_EXTENSION) *sk = NULL;
d02b48c6 105
0f113f3e 106 if (x == NULL) {
9311d0c4 107 ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
e077455e 108 goto err;
0f113f3e 109 }
c755c5fd 110
0f113f3e 111 if (*x == NULL) {
e077455e
RL
112 if ((sk = sk_X509_EXTENSION_new_null()) == NULL) {
113 ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
0f113f3e 114 goto err;
e077455e 115 }
0f113f3e
MC
116 } else
117 sk = *x;
d02b48c6 118
0f113f3e
MC
119 n = sk_X509_EXTENSION_num(sk);
120 if (loc > n)
121 loc = n;
122 else if (loc < 0)
123 loc = n;
d02b48c6 124
e077455e
RL
125 if ((new_ex = X509_EXTENSION_dup(ex)) == NULL) {
126 ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
0f113f3e 127 goto err;
e077455e
RL
128 }
129 if (!sk_X509_EXTENSION_insert(sk, new_ex, loc)) {
130 ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
131 goto err;
132 }
0f113f3e
MC
133 if (*x == NULL)
134 *x = sk;
26a7d938 135 return sk;
0f113f3e 136 err:
222561fe 137 X509_EXTENSION_free(new_ex);
abcf2411
PK
138 if (x != NULL && *x == NULL)
139 sk_X509_EXTENSION_free(sk);
26a7d938 140 return NULL;
0f113f3e 141}
d02b48c6 142
6b691a5c 143X509_EXTENSION *X509_EXTENSION_create_by_NID(X509_EXTENSION **ex, int nid,
0f113f3e
MC
144 int crit,
145 ASN1_OCTET_STRING *data)
146{
147 ASN1_OBJECT *obj;
148 X509_EXTENSION *ret;
d02b48c6 149
0f113f3e
MC
150 obj = OBJ_nid2obj(nid);
151 if (obj == NULL) {
9311d0c4 152 ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_NID);
26a7d938 153 return NULL;
0f113f3e
MC
154 }
155 ret = X509_EXTENSION_create_by_OBJ(ex, obj, crit, data);
156 if (ret == NULL)
157 ASN1_OBJECT_free(obj);
26a7d938 158 return ret;
0f113f3e 159}
d02b48c6 160
6b691a5c 161X509_EXTENSION *X509_EXTENSION_create_by_OBJ(X509_EXTENSION **ex,
c47ba4e9 162 const ASN1_OBJECT *obj, int crit,
0f113f3e
MC
163 ASN1_OCTET_STRING *data)
164{
165 X509_EXTENSION *ret;
166
167 if ((ex == NULL) || (*ex == NULL)) {
168 if ((ret = X509_EXTENSION_new()) == NULL) {
e077455e 169 ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
26a7d938 170 return NULL;
0f113f3e
MC
171 }
172 } else
173 ret = *ex;
d02b48c6 174
0f113f3e
MC
175 if (!X509_EXTENSION_set_object(ret, obj))
176 goto err;
177 if (!X509_EXTENSION_set_critical(ret, crit))
178 goto err;
179 if (!X509_EXTENSION_set_data(ret, data))
180 goto err;
d02b48c6 181
0f113f3e
MC
182 if ((ex != NULL) && (*ex == NULL))
183 *ex = ret;
26a7d938 184 return ret;
0f113f3e
MC
185 err:
186 if ((ex == NULL) || (ret != *ex))
187 X509_EXTENSION_free(ret);
26a7d938 188 return NULL;
0f113f3e 189}
d02b48c6 190
c47ba4e9 191int X509_EXTENSION_set_object(X509_EXTENSION *ex, const ASN1_OBJECT *obj)
0f113f3e
MC
192{
193 if ((ex == NULL) || (obj == NULL))
26a7d938 194 return 0;
0f113f3e
MC
195 ASN1_OBJECT_free(ex->object);
196 ex->object = OBJ_dup(obj);
4e0e4d29 197 return ex->object != NULL;
0f113f3e 198}
d02b48c6 199
6b691a5c 200int X509_EXTENSION_set_critical(X509_EXTENSION *ex, int crit)
0f113f3e
MC
201{
202 if (ex == NULL)
26a7d938 203 return 0;
0f113f3e 204 ex->critical = (crit) ? 0xFF : -1;
208fb891 205 return 1;
0f113f3e 206}
d02b48c6 207
6b691a5c 208int X509_EXTENSION_set_data(X509_EXTENSION *ex, ASN1_OCTET_STRING *data)
0f113f3e
MC
209{
210 int i;
d02b48c6 211
0f113f3e 212 if (ex == NULL)
26a7d938 213 return 0;
4392479c 214 i = ASN1_OCTET_STRING_set(&ex->value, data->data, data->length);
0f113f3e 215 if (!i)
26a7d938 216 return 0;
208fb891 217 return 1;
0f113f3e 218}
d02b48c6 219
6b691a5c 220ASN1_OBJECT *X509_EXTENSION_get_object(X509_EXTENSION *ex)
0f113f3e
MC
221{
222 if (ex == NULL)
26a7d938
K
223 return NULL;
224 return ex->object;
0f113f3e 225}
d02b48c6 226
6b691a5c 227ASN1_OCTET_STRING *X509_EXTENSION_get_data(X509_EXTENSION *ex)
0f113f3e
MC
228{
229 if (ex == NULL)
26a7d938 230 return NULL;
4392479c 231 return &ex->value;
0f113f3e 232}
d02b48c6 233
7569362e 234int X509_EXTENSION_get_critical(const X509_EXTENSION *ex)
0f113f3e
MC
235{
236 if (ex == NULL)
26a7d938 237 return 0;
0f113f3e
MC
238 if (ex->critical > 0)
239 return 1;
240 return 0;
241}