1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
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 (tjh@cryptsoft.com).
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 (eay@cryptsoft.com)"
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 (tjh@cryptsoft.com)"
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 ASN1_TYPE *ttmp = NULL;
141 ASN1_STRING *stmp = NULL;
142 int atype = 0;
143 if (!attr) {
144 return 0;
145 }
146 if (attrtype & MBSTRING_FLAG) {
147 stmp = ASN1_STRING_set_by_NID(NULL, data, len, attrtype,
148 OBJ_obj2nid(attr->object));
149 if (!stmp) {
150 OPENSSL_PUT_ERROR(X509, ERR_R_ASN1_LIB);
151 return 0;
152 }
153 atype = stmp->type;
154 } else if (len != -1) {
155 if (!(stmp = ASN1_STRING_type_new(attrtype))) {
156 goto err;
157 }
158 if (!ASN1_STRING_set(stmp, data, len)) {
159 goto err;
160 }
161 atype = attrtype;
162 }
163 // This is a bit naughty because the attribute should really have at
164 // least one value but some types use and zero length SET and require
165 // this.
166 if (attrtype == 0) {
167 ASN1_STRING_free(stmp);
168 return 1;
169 }
170 if (!(ttmp = ASN1_TYPE_new())) {
171 goto err;
172 }
173 if ((len == -1) && !(attrtype & MBSTRING_FLAG)) {
174 if (!ASN1_TYPE_set1(ttmp, attrtype, data)) {
175 goto err;
176 }
177 } else {
178 ASN1_TYPE_set(ttmp, atype, stmp);
179 stmp = NULL;
180 }
181 if (!sk_ASN1_TYPE_push(attr->set, ttmp)) {
182 goto err;
183 }
184 return 1;
185 err:
186 ASN1_TYPE_free(ttmp);
187 ASN1_STRING_free(stmp);
188 return 0;
189 }
190
X509_ATTRIBUTE_count(const X509_ATTRIBUTE * attr)191 int X509_ATTRIBUTE_count(const X509_ATTRIBUTE *attr) {
192 return (int)sk_ASN1_TYPE_num(attr->set);
193 }
194
X509_ATTRIBUTE_get0_object(X509_ATTRIBUTE * attr)195 ASN1_OBJECT *X509_ATTRIBUTE_get0_object(X509_ATTRIBUTE *attr) {
196 if (attr == NULL) {
197 return NULL;
198 }
199 return attr->object;
200 }
201
X509_ATTRIBUTE_get0_data(X509_ATTRIBUTE * attr,int idx,int attrtype,void * unused)202 void *X509_ATTRIBUTE_get0_data(X509_ATTRIBUTE *attr, int idx, int attrtype,
203 void *unused) {
204 ASN1_TYPE *ttmp;
205 ttmp = X509_ATTRIBUTE_get0_type(attr, idx);
206 if (!ttmp) {
207 return NULL;
208 }
209 if (attrtype != ASN1_TYPE_get(ttmp)) {
210 OPENSSL_PUT_ERROR(X509, X509_R_WRONG_TYPE);
211 return NULL;
212 }
213 return (void *)asn1_type_value_as_pointer(ttmp);
214 }
215
X509_ATTRIBUTE_get0_type(X509_ATTRIBUTE * attr,int idx)216 ASN1_TYPE *X509_ATTRIBUTE_get0_type(X509_ATTRIBUTE *attr, int idx) {
217 if (attr == NULL) {
218 return NULL;
219 }
220 if (idx >= X509_ATTRIBUTE_count(attr)) {
221 return NULL;
222 }
223 return sk_ASN1_TYPE_value(attr->set, idx);
224 }
225