]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/x509/v3_info.c
Stop raising ERR_R_MALLOC_FAILURE in most places
[thirdparty/openssl.git] / crypto / x509 / v3_info.c
CommitLineData
0f113f3e 1/*
3c2bdd7d 2 * Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved.
6d3724d3 3 *
4286ca47 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
d2e9e320
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
6d3724d3
DSH
8 */
9
10#include <stdio.h>
b39fc560 11#include "internal/cryptlib.h"
6d3724d3
DSH
12#include <openssl/conf.h>
13#include <openssl/asn1.h>
9d6b1ce6 14#include <openssl/asn1t.h>
6d3724d3 15#include <openssl/x509v3.h>
df2ee0e2 16#include "ext_dat.h"
6d3724d3 17
0f113f3e
MC
18static STACK_OF(CONF_VALUE) *i2v_AUTHORITY_INFO_ACCESS(X509V3_EXT_METHOD
19 *method, AUTHORITY_INFO_ACCESS
20 *ainfo, STACK_OF(CONF_VALUE)
21 *ret);
22static AUTHORITY_INFO_ACCESS *v2i_AUTHORITY_INFO_ACCESS(X509V3_EXT_METHOD
23 *method,
24 X509V3_CTX *ctx,
25 STACK_OF(CONF_VALUE)
26 *nval);
6d3724d3 27
47864aea 28const X509V3_EXT_METHOD ossl_v3_info = { NID_info_access, X509V3_EXT_MULTILINE,
0f113f3e
MC
29 ASN1_ITEM_ref(AUTHORITY_INFO_ACCESS),
30 0, 0, 0, 0,
31 0, 0,
32 (X509V3_EXT_I2V) i2v_AUTHORITY_INFO_ACCESS,
33 (X509V3_EXT_V2I)v2i_AUTHORITY_INFO_ACCESS,
34 0, 0,
35 NULL
36};
7d5b04db 37
47864aea 38const X509V3_EXT_METHOD ossl_v3_sinfo = { NID_sinfo_access, X509V3_EXT_MULTILINE,
0f113f3e
MC
39 ASN1_ITEM_ref(AUTHORITY_INFO_ACCESS),
40 0, 0, 0, 0,
41 0, 0,
42 (X509V3_EXT_I2V) i2v_AUTHORITY_INFO_ACCESS,
43 (X509V3_EXT_V2I)v2i_AUTHORITY_INFO_ACCESS,
44 0, 0,
45 NULL
46};
6d3724d3 47
9d6b1ce6 48ASN1_SEQUENCE(ACCESS_DESCRIPTION) = {
0f113f3e
MC
49 ASN1_SIMPLE(ACCESS_DESCRIPTION, method, ASN1_OBJECT),
50 ASN1_SIMPLE(ACCESS_DESCRIPTION, location, GENERAL_NAME)
d339187b 51} ASN1_SEQUENCE_END(ACCESS_DESCRIPTION)
9d6b1ce6
DSH
52
53IMPLEMENT_ASN1_FUNCTIONS(ACCESS_DESCRIPTION)
54
0f113f3e
MC
55ASN1_ITEM_TEMPLATE(AUTHORITY_INFO_ACCESS) =
56 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, GeneralNames, ACCESS_DESCRIPTION)
d339187b 57ASN1_ITEM_TEMPLATE_END(AUTHORITY_INFO_ACCESS)
9d6b1ce6
DSH
58
59IMPLEMENT_ASN1_FUNCTIONS(AUTHORITY_INFO_ACCESS)
60
75a3e392
MC
61static STACK_OF(CONF_VALUE) *i2v_AUTHORITY_INFO_ACCESS(
62 X509V3_EXT_METHOD *method, AUTHORITY_INFO_ACCESS *ainfo,
63 STACK_OF(CONF_VALUE) *ret)
6d3724d3 64{
0f113f3e
MC
65 ACCESS_DESCRIPTION *desc;
66 int i, nlen;
67 char objtmp[80], *ntmp;
68 CONF_VALUE *vtmp;
75a3e392
MC
69 STACK_OF(CONF_VALUE) *tret = ret;
70
0f113f3e 71 for (i = 0; i < sk_ACCESS_DESCRIPTION_num(ainfo); i++) {
75a3e392
MC
72 STACK_OF(CONF_VALUE) *tmp;
73
0f113f3e 74 desc = sk_ACCESS_DESCRIPTION_value(ainfo, i);
75a3e392 75 tmp = i2v_GENERAL_NAME(method, desc->location, tret);
e077455e
RL
76 if (tmp == NULL) {
77 ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
75a3e392 78 goto err;
e077455e 79 }
75a3e392
MC
80 tret = tmp;
81 vtmp = sk_CONF_VALUE_value(tret, i);
cbe29648 82 i2t_ASN1_OBJECT(objtmp, sizeof(objtmp), desc->method);
0904e79a 83 nlen = strlen(objtmp) + 3 + strlen(vtmp->name) + 1;
0f113f3e 84 ntmp = OPENSSL_malloc(nlen);
75a3e392
MC
85 if (ntmp == NULL)
86 goto err;
a2371fa9 87 BIO_snprintf(ntmp, nlen, "%s - %s", objtmp, vtmp->name);
0f113f3e
MC
88 OPENSSL_free(vtmp->name);
89 vtmp->name = ntmp;
0f113f3e 90 }
75a3e392 91 if (ret == NULL && tret == NULL)
0f113f3e 92 return sk_CONF_VALUE_new_null();
75a3e392
MC
93
94 return tret;
95 err:
75a3e392
MC
96 if (ret == NULL && tret != NULL)
97 sk_CONF_VALUE_pop_free(tret, X509V3_conf_free);
98 return NULL;
6d3724d3
DSH
99}
100
0f113f3e
MC
101static AUTHORITY_INFO_ACCESS *v2i_AUTHORITY_INFO_ACCESS(X509V3_EXT_METHOD
102 *method,
103 X509V3_CTX *ctx,
104 STACK_OF(CONF_VALUE)
105 *nval)
6d3724d3 106{
0f113f3e
MC
107 AUTHORITY_INFO_ACCESS *ainfo = NULL;
108 CONF_VALUE *cnf, ctmp;
109 ACCESS_DESCRIPTION *acc;
a150f8e1 110 int i;
e431363f 111 const int num = sk_CONF_VALUE_num(nval);
7a7ed5fc 112 char *objtmp, *ptmp;
75ebbd9a 113
7a908204 114 if ((ainfo = sk_ACCESS_DESCRIPTION_new_reserve(NULL, num)) == NULL) {
e077455e 115 ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
0f113f3e
MC
116 return NULL;
117 }
e431363f 118 for (i = 0; i < num; i++) {
0f113f3e 119 cnf = sk_CONF_VALUE_value(nval, i);
e431363f 120 if ((acc = ACCESS_DESCRIPTION_new()) == NULL) {
e077455e 121 ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
0f113f3e
MC
122 goto err;
123 }
e431363f 124 sk_ACCESS_DESCRIPTION_push(ainfo, acc); /* Cannot fail due to reserve */
0f113f3e 125 ptmp = strchr(cnf->name, ';');
12a765a5 126 if (ptmp == NULL) {
9311d0c4 127 ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_SYNTAX);
0f113f3e
MC
128 goto err;
129 }
0f113f3e
MC
130 ctmp.name = ptmp + 1;
131 ctmp.value = cnf->value;
132 if (!v2i_GENERAL_NAME_ex(acc->location, method, ctx, &ctmp, 0))
133 goto err;
e077455e 134 if ((objtmp = OPENSSL_strndup(cnf->name, ptmp - cnf->name)) == NULL)
7a7ed5fc 135 goto err;
7a7ed5fc 136 acc->method = OBJ_txt2obj(objtmp, 0);
0f113f3e 137 if (!acc->method) {
a150f8e1 138 ERR_raise_data(ERR_LIB_X509V3, X509V3_R_BAD_OBJECT,
7a7ed5fc 139 "value=%s", objtmp);
140 OPENSSL_free(objtmp);
0f113f3e
MC
141 goto err;
142 }
7a7ed5fc 143 OPENSSL_free(objtmp);
0f113f3e
MC
144 }
145 return ainfo;
146 err:
147 sk_ACCESS_DESCRIPTION_pop_free(ainfo, ACCESS_DESCRIPTION_free);
148 return NULL;
6d3724d3
DSH
149}
150
095d2f0f 151int i2a_ACCESS_DESCRIPTION(BIO *bp, const ACCESS_DESCRIPTION *a)
0f113f3e
MC
152{
153 i2a_ASN1_OBJECT(bp, a->method);
0f113f3e
MC
154 return 2;
155}