| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include <openssl/asn1.h> |
| 5 |
#include <openssl/err.h> |
| 6 |
#include <openssl/obj.h> |
| 7 |
#include <openssl/x509.h> |
| 8 |
|
| 9 |
#include "../asn1/internal.h" |
| 10 |
#include "internal.h" |
| 11 |
|
| 12 |
|
| 13 |
X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_NID(X509_ATTRIBUTE **attr, int nid, |
| 14 |
int attrtype, const void *data, |
| 15 |
int len) { |
| 16 |
const ASN1_OBJECT *obj; |
| 17 |
|
| 18 |
obj = OBJ_nid2obj(nid); |
| 19 |
if (obj == NULL) { |
| 20 |
OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_NID); |
| 21 |
return NULL; |
| 22 |
} |
| 23 |
return X509_ATTRIBUTE_create_by_OBJ(attr, obj, attrtype, data, len); |
| 24 |
} |
| 25 |
|
| 26 |
X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_OBJ(X509_ATTRIBUTE **attr, |
| 27 |
const ASN1_OBJECT *obj, |
| 28 |
int attrtype, const void *data, |
| 29 |
int len) { |
| 30 |
X509_ATTRIBUTE *ret; |
| 31 |
|
| 32 |
if ((attr == NULL) || (*attr == NULL)) { |
| 33 |
if ((ret = X509_ATTRIBUTE_new()) == NULL) { |
| 34 |
return NULL; |
| 35 |
} |
| 36 |
} else { |
| 37 |
ret = *attr; |
| 38 |
} |
| 39 |
|
| 40 |
if (!X509_ATTRIBUTE_set1_object(ret, obj)) { |
| 41 |
goto err; |
| 42 |
} |
| 43 |
if (!X509_ATTRIBUTE_set1_data(ret, attrtype, data, len)) { |
| 44 |
goto err; |
| 45 |
} |
| 46 |
|
| 47 |
if ((attr != NULL) && (*attr == NULL)) { |
| 48 |
*attr = ret; |
| 49 |
} |
| 50 |
return ret; |
| 51 |
err: |
| 52 |
if ((attr == NULL) || (ret != *attr)) { |
| 53 |
X509_ATTRIBUTE_free(ret); |
| 54 |
} |
| 55 |
return NULL; |
| 56 |
} |
| 57 |
|
| 58 |
X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_txt(X509_ATTRIBUTE **attr, |
| 59 |
const char *attrname, int type, |
| 60 |
const unsigned char *bytes, |
| 61 |
int len) { |
| 62 |
ASN1_OBJECT *obj; |
| 63 |
X509_ATTRIBUTE *nattr; |
| 64 |
|
| 65 |
obj = OBJ_txt2obj(attrname, 0); |
| 66 |
if (obj == NULL) { |
| 67 |
OPENSSL_PUT_ERROR(X509, X509_R_INVALID_FIELD_NAME); |
| 68 |
ERR_add_error_data(2, "name=", attrname); |
| 69 |
return NULL; |
| 70 |
} |
| 71 |
nattr = X509_ATTRIBUTE_create_by_OBJ(attr, obj, type, bytes, len); |
| 72 |
ASN1_OBJECT_free(obj); |
| 73 |
return nattr; |
| 74 |
} |
| 75 |
|
| 76 |
int X509_ATTRIBUTE_set1_object(X509_ATTRIBUTE *attr, const ASN1_OBJECT *obj) { |
| 77 |
if ((attr == NULL) || (obj == NULL)) { |
| 78 |
return 0; |
| 79 |
} |
| 80 |
ASN1_OBJECT_free(attr->object); |
| 81 |
attr->object = OBJ_dup(obj); |
| 82 |
return attr->object != NULL; |
| 83 |
} |
| 84 |
|
| 85 |
int X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE *attr, int attrtype, |
| 86 |
const void *data, int len) { |
| 87 |
if (!attr) { |
| 88 |
return 0; |
| 89 |
} |
| 90 |
|
| 91 |
if (attrtype == 0) { |
| 92 |
// Do nothing. This is used to create an empty value set in |
| 93 |
// |X509_ATTRIBUTE_create_by_*|. This is invalid, but supported by OpenSSL. |
| 94 |
return 1; |
| 95 |
} |
| 96 |
|
| 97 |
ASN1_TYPE *typ = ASN1_TYPE_new(); |
| 98 |
if (typ == NULL) { |
| 99 |
return 0; |
| 100 |
} |
| 101 |
|
| 102 |
// This function is several functions in one. |
| 103 |
if (attrtype & MBSTRING_FLAG) { |
| 104 |
// |data| is an encoded string. We must decode and re-encode it to |attr|'s |
| 105 |
// preferred ASN.1 type. Note |len| may be -1, in which case |
| 106 |
// |ASN1_STRING_set_by_NID| calls |strlen| automatically. |
| 107 |
ASN1_STRING *str = ASN1_STRING_set_by_NID(NULL, data, len, attrtype, |
| 108 |
OBJ_obj2nid(attr->object)); |
| 109 |
if (str == NULL) { |
| 110 |
OPENSSL_PUT_ERROR(X509, ERR_R_ASN1_LIB); |
| 111 |
goto err; |
| 112 |
} |
| 113 |
asn1_type_set0_string(typ, str); |
| 114 |
} else if (len != -1) { |
| 115 |
// |attrtype| must be a valid |ASN1_STRING| type. |data| and |len| is a |
| 116 |
// value in the corresponding |ASN1_STRING| representation. |
| 117 |
ASN1_STRING *str = ASN1_STRING_type_new(attrtype); |
| 118 |
if (str == NULL || !ASN1_STRING_set(str, data, len)) { |
| 119 |
ASN1_STRING_free(str); |
| 120 |
goto err; |
| 121 |
} |
| 122 |
asn1_type_set0_string(typ, str); |
| 123 |
} else { |
| 124 |
// |attrtype| must be a valid |ASN1_TYPE| type. |data| is a pointer to an |
| 125 |
// object of the corresponding type. |
| 126 |
if (!ASN1_TYPE_set1(typ, attrtype, data)) { |
| 127 |
goto err; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
if (!sk_ASN1_TYPE_push(attr->set, typ)) { |
| 132 |
goto err; |
| 133 |
} |
| 134 |
return 1; |
| 135 |
|
| 136 |
err: |
| 137 |
ASN1_TYPE_free(typ); |
| 138 |
return 0; |
| 139 |
} |
| 140 |
|
| 141 |
int X509_ATTRIBUTE_count(const X509_ATTRIBUTE *attr) { |
| 142 |
if (attr == NULL) { |
| 143 |
return 0; |
| 144 |
} |
| 145 |
return (int)sk_ASN1_TYPE_num(attr->set); |
| 146 |
} |
| 147 |
|
| 148 |
ASN1_OBJECT *X509_ATTRIBUTE_get0_object(X509_ATTRIBUTE *attr) { |
| 149 |
if (attr == NULL) { |
| 150 |
return NULL; |
| 151 |
} |
| 152 |
return attr->object; |
| 153 |
} |
| 154 |
|
| 155 |
void *X509_ATTRIBUTE_get0_data(X509_ATTRIBUTE *attr, int idx, int attrtype, |
| 156 |
void *unused) { |
| 157 |
ASN1_TYPE *ttmp; |
| 158 |
ttmp = X509_ATTRIBUTE_get0_type(attr, idx); |
| 159 |
if (!ttmp) { |
| 160 |
return NULL; |
| 161 |
} |
| 162 |
if (attrtype != ASN1_TYPE_get(ttmp)) { |
| 163 |
OPENSSL_PUT_ERROR(X509, X509_R_WRONG_TYPE); |
| 164 |
return NULL; |
| 165 |
} |
| 166 |
return (void *)asn1_type_value_as_pointer(ttmp); |
| 167 |
} |
| 168 |
|
| 169 |
ASN1_TYPE *X509_ATTRIBUTE_get0_type(X509_ATTRIBUTE *attr, int idx) { |
| 170 |
if (attr == NULL) { |
| 171 |
return NULL; |
| 172 |
} |
| 173 |
if (idx >= X509_ATTRIBUTE_count(attr)) { |
| 174 |
return NULL; |
| 175 |
} |
| 176 |
return sk_ASN1_TYPE_value(attr->set, idx); |
| 177 |
} |