xref: /aosp_15_r20/external/cronet/third_party/boringssl/src/crypto/x509/x509_att.c (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 /* Copyright (C) 1995-1998 Eric Young ([email protected])
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young ([email protected]).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to.  The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson ([email protected]).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  *    must display the following acknowledgement:
32  *    "This product includes cryptographic software written by
33  *     Eric Young ([email protected])"
34  *    The word 'cryptographic' can be left out if the rouines from the library
35  *    being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  *    the apps directory (application code) you must include an acknowledgement:
38  *    "This product includes software written by Tim Hudson ([email protected])"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.] */
56 
57 #include <openssl/asn1.h>
58 #include <openssl/err.h>
59 #include <openssl/obj.h>
60 #include <openssl/x509.h>
61 
62 #include "../asn1/internal.h"
63 #include "internal.h"
64 
65 
X509_ATTRIBUTE_create_by_NID(X509_ATTRIBUTE ** attr,int nid,int attrtype,const void * data,int len)66 X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_NID(X509_ATTRIBUTE **attr, int nid,
67                                              int attrtype, const void *data,
68                                              int len) {
69   const ASN1_OBJECT *obj;
70 
71   obj = OBJ_nid2obj(nid);
72   if (obj == NULL) {
73     OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_NID);
74     return NULL;
75   }
76   return X509_ATTRIBUTE_create_by_OBJ(attr, obj, attrtype, data, len);
77 }
78 
X509_ATTRIBUTE_create_by_OBJ(X509_ATTRIBUTE ** attr,const ASN1_OBJECT * obj,int attrtype,const void * data,int len)79 X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_OBJ(X509_ATTRIBUTE **attr,
80                                              const ASN1_OBJECT *obj,
81                                              int attrtype, const void *data,
82                                              int len) {
83   X509_ATTRIBUTE *ret;
84 
85   if ((attr == NULL) || (*attr == NULL)) {
86     if ((ret = X509_ATTRIBUTE_new()) == NULL) {
87       return NULL;
88     }
89   } else {
90     ret = *attr;
91   }
92 
93   if (!X509_ATTRIBUTE_set1_object(ret, obj)) {
94     goto err;
95   }
96   if (!X509_ATTRIBUTE_set1_data(ret, attrtype, data, len)) {
97     goto err;
98   }
99 
100   if ((attr != NULL) && (*attr == NULL)) {
101     *attr = ret;
102   }
103   return ret;
104 err:
105   if ((attr == NULL) || (ret != *attr)) {
106     X509_ATTRIBUTE_free(ret);
107   }
108   return NULL;
109 }
110 
X509_ATTRIBUTE_create_by_txt(X509_ATTRIBUTE ** attr,const char * attrname,int type,const unsigned char * bytes,int len)111 X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_txt(X509_ATTRIBUTE **attr,
112                                              const char *attrname, int type,
113                                              const unsigned char *bytes,
114                                              int len) {
115   ASN1_OBJECT *obj;
116   X509_ATTRIBUTE *nattr;
117 
118   obj = OBJ_txt2obj(attrname, 0);
119   if (obj == NULL) {
120     OPENSSL_PUT_ERROR(X509, X509_R_INVALID_FIELD_NAME);
121     ERR_add_error_data(2, "name=", attrname);
122     return NULL;
123   }
124   nattr = X509_ATTRIBUTE_create_by_OBJ(attr, obj, type, bytes, len);
125   ASN1_OBJECT_free(obj);
126   return nattr;
127 }
128 
X509_ATTRIBUTE_set1_object(X509_ATTRIBUTE * attr,const ASN1_OBJECT * obj)129 int X509_ATTRIBUTE_set1_object(X509_ATTRIBUTE *attr, const ASN1_OBJECT *obj) {
130   if ((attr == NULL) || (obj == NULL)) {
131     return 0;
132   }
133   ASN1_OBJECT_free(attr->object);
134   attr->object = OBJ_dup(obj);
135   return attr->object != NULL;
136 }
137 
X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE * attr,int attrtype,const void * data,int len)138 int X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE *attr, int attrtype,
139                              const void *data, int len) {
140   if (!attr) {
141     return 0;
142   }
143 
144   if (attrtype == 0) {
145     // Do nothing. This is used to create an empty value set in
146     // |X509_ATTRIBUTE_create_by_*|. This is invalid, but supported by OpenSSL.
147     return 1;
148   }
149 
150   ASN1_TYPE *typ = ASN1_TYPE_new();
151   if (typ == NULL) {
152     return 0;
153   }
154 
155   // This function is several functions in one.
156   if (attrtype & MBSTRING_FLAG) {
157     // |data| is an encoded string. We must decode and re-encode it to |attr|'s
158     // preferred ASN.1 type. Note |len| may be -1, in which case
159     // |ASN1_STRING_set_by_NID| calls |strlen| automatically.
160     ASN1_STRING *str = ASN1_STRING_set_by_NID(NULL, data, len, attrtype,
161                                               OBJ_obj2nid(attr->object));
162     if (str == NULL) {
163       OPENSSL_PUT_ERROR(X509, ERR_R_ASN1_LIB);
164       goto err;
165     }
166     asn1_type_set0_string(typ, str);
167   } else if (len != -1) {
168     // |attrtype| must be a valid |ASN1_STRING| type. |data| and |len| is a
169     // value in the corresponding |ASN1_STRING| representation.
170     ASN1_STRING *str = ASN1_STRING_type_new(attrtype);
171     if (str == NULL || !ASN1_STRING_set(str, data, len)) {
172       ASN1_STRING_free(str);
173       goto err;
174     }
175     asn1_type_set0_string(typ, str);
176   } else {
177     // |attrtype| must be a valid |ASN1_TYPE| type. |data| is a pointer to an
178     // object of the corresponding type.
179     if (!ASN1_TYPE_set1(typ, attrtype, data)) {
180       goto err;
181     }
182   }
183 
184   if (!sk_ASN1_TYPE_push(attr->set, typ)) {
185     goto err;
186   }
187   return 1;
188 
189 err:
190   ASN1_TYPE_free(typ);
191   return 0;
192 }
193 
X509_ATTRIBUTE_count(const X509_ATTRIBUTE * attr)194 int X509_ATTRIBUTE_count(const X509_ATTRIBUTE *attr) {
195   return (int)sk_ASN1_TYPE_num(attr->set);
196 }
197 
X509_ATTRIBUTE_get0_object(X509_ATTRIBUTE * attr)198 ASN1_OBJECT *X509_ATTRIBUTE_get0_object(X509_ATTRIBUTE *attr) {
199   if (attr == NULL) {
200     return NULL;
201   }
202   return attr->object;
203 }
204 
X509_ATTRIBUTE_get0_data(X509_ATTRIBUTE * attr,int idx,int attrtype,void * unused)205 void *X509_ATTRIBUTE_get0_data(X509_ATTRIBUTE *attr, int idx, int attrtype,
206                                void *unused) {
207   ASN1_TYPE *ttmp;
208   ttmp = X509_ATTRIBUTE_get0_type(attr, idx);
209   if (!ttmp) {
210     return NULL;
211   }
212   if (attrtype != ASN1_TYPE_get(ttmp)) {
213     OPENSSL_PUT_ERROR(X509, X509_R_WRONG_TYPE);
214     return NULL;
215   }
216   return (void *)asn1_type_value_as_pointer(ttmp);
217 }
218 
X509_ATTRIBUTE_get0_type(X509_ATTRIBUTE * attr,int idx)219 ASN1_TYPE *X509_ATTRIBUTE_get0_type(X509_ATTRIBUTE *attr, int idx) {
220   if (attr == NULL) {
221     return NULL;
222   }
223   if (idx >= X509_ATTRIBUTE_count(attr)) {
224     return NULL;
225   }
226   return sk_ASN1_TYPE_value(attr->set, idx);
227 }
228