]> git.saurik.com Git - apple/security.git/blob - libsecurity_smime/lib/cmsattr.c
Security-55179.11.tar.gz
[apple/security.git] / libsecurity_smime / lib / cmsattr.c
1 /*
2 * The contents of this file are subject to the Mozilla Public
3 * License Version 1.1 (the "License"); you may not use this file
4 * except in compliance with the License. You may obtain a copy of
5 * the License at http://www.mozilla.org/MPL/
6 *
7 * Software distributed under the License is distributed on an "AS
8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9 * implied. See the License for the specific language governing
10 * rights and limitations under the License.
11 *
12 * The Original Code is the Netscape security libraries.
13 *
14 * The Initial Developer of the Original Code is Netscape
15 * Communications Corporation. Portions created by Netscape are
16 * Copyright (C) 1994-2000 Netscape Communications Corporation. All
17 * Rights Reserved.
18 *
19 * Contributor(s):
20 *
21 * Alternatively, the contents of this file may be used under the
22 * terms of the GNU General Public License Version 2 or later (the
23 * "GPL"), in which case the provisions of the GPL are applicable
24 * instead of those above. If you wish to allow use of your
25 * version of this file only under the terms of the GPL and not to
26 * allow others to use your version of this file under the MPL,
27 * indicate your decision by deleting the provisions above and
28 * replace them with the notice and other provisions required by
29 * the GPL. If you do not delete the provisions above, a recipient
30 * may use your version of this file under either the MPL or the
31 * GPL.
32 */
33
34 /*
35 * CMS attributes.
36 */
37
38 #include "cmslocal.h"
39
40 #include "secoid.h"
41 #include "secitem.h"
42
43 #include <security_asn1/secasn1.h>
44 #include <security_asn1/secerr.h>
45
46 /*
47 * -------------------------------------------------------------------
48 * XXX The following Attribute stuff really belongs elsewhere.
49 * The Attribute type is *not* part of CMS but rather X.501.
50 * But for now, since CMS is the only customer of attributes,
51 * we define them here. Once there is a use outside of CMS,
52 * then change the attribute types and functions from internal
53 * to external naming convention, and move them elsewhere!
54 */
55
56
57 /*
58 * SecCmsAttributeCreate - create an attribute
59 *
60 * if value is NULL, the attribute won't have a value. It can be added later
61 * with SecCmsAttributeAddValue.
62 */
63 SecCmsAttribute *
64 SecCmsAttributeCreate(PRArenaPool *poolp, SECOidTag oidtag, CSSM_DATA_PTR value, Boolean encoded)
65 {
66 SecCmsAttribute *attr;
67 CSSM_DATA_PTR copiedvalue;
68 void *mark;
69
70 PORT_Assert (poolp != NULL);
71
72 mark = PORT_ArenaMark (poolp);
73
74 attr = (SecCmsAttribute *)PORT_ArenaZAlloc(poolp, sizeof(SecCmsAttribute));
75 if (attr == NULL)
76 goto loser;
77
78 attr->typeTag = SECOID_FindOIDByTag(oidtag);
79 if (attr->typeTag == NULL)
80 goto loser;
81
82 if (SECITEM_CopyItem(poolp, &(attr->type), &(attr->typeTag->oid)) != SECSuccess)
83 goto loser;
84
85 if (value != NULL) {
86 if ((copiedvalue = SECITEM_AllocItem(poolp, NULL, value->Length)) == NULL)
87 goto loser;
88
89 if (SECITEM_CopyItem(poolp, copiedvalue, value) != SECSuccess)
90 goto loser;
91
92 SecCmsArrayAdd(poolp, (void ***)&(attr->values), (void *)copiedvalue);
93 }
94
95 attr->encoded = encoded;
96
97 PORT_ArenaUnmark (poolp, mark);
98
99 return attr;
100
101 loser:
102 PORT_Assert (mark != NULL);
103 PORT_ArenaRelease (poolp, mark);
104 return NULL;
105 }
106
107 /*
108 * SecCmsAttributeAddValue - add another value to an attribute
109 */
110 OSStatus
111 SecCmsAttributeAddValue(PLArenaPool *poolp, SecCmsAttribute *attr, CSSM_DATA_PTR value)
112 {
113 CSSM_DATA copiedvalue;
114 void *mark;
115
116 PORT_Assert (poolp != NULL);
117
118 mark = PORT_ArenaMark(poolp);
119
120 /* XXX we need an object memory model #$%#$%! */
121 if (SECITEM_CopyItem(poolp, &copiedvalue, value) != SECSuccess)
122 goto loser;
123
124 if (SecCmsArrayAdd(poolp, (void ***)&(attr->values), (void *)&copiedvalue) != SECSuccess)
125 goto loser;
126
127 PORT_ArenaUnmark(poolp, mark);
128 return SECSuccess;
129
130 loser:
131 PORT_Assert (mark != NULL);
132 PORT_ArenaRelease (poolp, mark);
133 return SECFailure;
134 }
135
136 /*
137 * SecCmsAttributeGetType - return the OID tag
138 */
139 SECOidTag
140 SecCmsAttributeGetType(SecCmsAttribute *attr)
141 {
142 SECOidData *typetag;
143
144 typetag = SECOID_FindOID(&(attr->type));
145 if (typetag == NULL)
146 return SEC_OID_UNKNOWN;
147
148 return typetag->offset;
149 }
150
151 /*
152 * SecCmsAttributeGetValue - return the first attribute value
153 *
154 * We do some sanity checking first:
155 * - Multiple values are *not* expected.
156 * - Empty values are *not* expected.
157 */
158 CSSM_DATA_PTR
159 SecCmsAttributeGetValue(SecCmsAttribute *attr)
160 {
161 CSSM_DATA_PTR value;
162
163 if (attr == NULL)
164 return NULL;
165
166 value = attr->values[0];
167
168 if (value == NULL || value->Data == NULL || value->Length == 0)
169 return NULL;
170
171 if (attr->values[1] != NULL)
172 return NULL;
173
174 return value;
175 }
176
177 /*
178 * SecCmsAttributeCompareValue - compare the attribute's first value against data
179 */
180 Boolean
181 SecCmsAttributeCompareValue(SecCmsAttribute *attr, CSSM_DATA_PTR av)
182 {
183 CSSM_DATA_PTR value;
184
185 if (attr == NULL)
186 return PR_FALSE;
187
188 value = SecCmsAttributeGetValue(attr);
189
190 return (value != NULL && value->Length == av->Length &&
191 PORT_Memcmp (value->Data, av->Data, value->Length) == 0);
192 }
193
194 /*
195 * templates and functions for separate ASN.1 encoding of attributes
196 *
197 * used in SecCmsAttributeArrayReorder
198 */
199
200 /*
201 * helper function for dynamic template determination of the attribute value
202 */
203 static const SecAsn1Template *
204 cms_attr_choose_attr_value_template(void *src_or_dest, Boolean encoding, const char *buf, void *dest)
205 {
206 const SecAsn1Template *theTemplate;
207 SecCmsAttribute *attribute;
208 SECOidData *oiddata;
209 Boolean encoded;
210
211 PORT_Assert (src_or_dest != NULL);
212 if (src_or_dest == NULL)
213 return NULL;
214
215 attribute = (SecCmsAttribute *)src_or_dest;
216
217 if (encoding && attribute->encoded)
218 /* we're encoding, and the attribute value is already encoded. */
219 return SEC_ASN1_GET(kSecAsn1AnyTemplate);
220
221 /* get attribute's typeTag */
222 oiddata = attribute->typeTag;
223 if (oiddata == NULL) {
224 oiddata = SECOID_FindOID(&attribute->type);
225 attribute->typeTag = oiddata;
226 }
227
228 if (oiddata == NULL) {
229 /* still no OID tag? OID is unknown then. en/decode value as ANY. */
230 encoded = PR_TRUE;
231 theTemplate = SEC_ASN1_GET(kSecAsn1AnyTemplate);
232 } else {
233 switch (oiddata->offset) {
234 case SEC_OID_PKCS9_SMIME_CAPABILITIES:
235 case SEC_OID_SMIME_ENCRYPTION_KEY_PREFERENCE:
236 /* these guys need to stay DER-encoded */
237 default:
238 /* same goes for OIDs that are not handled here */
239 encoded = PR_TRUE;
240 theTemplate = SEC_ASN1_GET(kSecAsn1AnyTemplate);
241 break;
242 /* otherwise choose proper template */
243 case SEC_OID_PKCS9_EMAIL_ADDRESS:
244 case SEC_OID_RFC1274_MAIL:
245 case SEC_OID_PKCS9_UNSTRUCTURED_NAME:
246 encoded = PR_FALSE;
247 theTemplate = SEC_ASN1_GET(kSecAsn1IA5StringTemplate);
248 break;
249 case SEC_OID_PKCS9_CONTENT_TYPE:
250 encoded = PR_FALSE;
251 theTemplate = SEC_ASN1_GET(kSecAsn1ObjectIDTemplate);
252 break;
253 case SEC_OID_PKCS9_MESSAGE_DIGEST:
254 encoded = PR_FALSE;
255 theTemplate = SEC_ASN1_GET(kSecAsn1OctetStringTemplate);
256 break;
257 case SEC_OID_PKCS9_SIGNING_TIME:
258 encoded = PR_FALSE;
259 theTemplate = SEC_ASN1_GET(kSecAsn1UTCTimeTemplate); // @@@ This should be a choice between UTCTime and GeneralizedTime -- mb
260 break;
261 /* XXX Want other types here, too */
262 }
263 }
264
265 if (encoding) {
266 /*
267 * If we are encoding and we think we have an already-encoded value,
268 * then the code which initialized this attribute should have set
269 * the "encoded" property to true (and we would have returned early,
270 * up above). No devastating error, but that code should be fixed.
271 * (It could indicate that the resulting encoded bytes are wrong.)
272 */
273 PORT_Assert (!encoded);
274 } else {
275 /*
276 * We are decoding; record whether the resulting value is
277 * still encoded or not.
278 */
279 attribute->encoded = encoded;
280 }
281 return theTemplate;
282 }
283
284 static const SecAsn1TemplateChooserPtr cms_attr_chooser
285 = cms_attr_choose_attr_value_template;
286
287 const SecAsn1Template nss_cms_attribute_template[] = {
288 { SEC_ASN1_SEQUENCE,
289 0, NULL, sizeof(SecCmsAttribute) },
290 { SEC_ASN1_OBJECT_ID,
291 offsetof(SecCmsAttribute,type) },
292 { SEC_ASN1_DYNAMIC | SEC_ASN1_SET_OF,
293 offsetof(SecCmsAttribute,values),
294 &cms_attr_chooser },
295 { 0 }
296 };
297
298 const SecAsn1Template nss_cms_set_of_attribute_template[] = {
299 { SEC_ASN1_SET_OF, 0, nss_cms_attribute_template }
300 };
301
302 /* =============================================================================
303 * Attribute Array methods
304 */
305
306 /*
307 * SecCmsAttributeArrayEncode - encode an Attribute array as SET OF Attributes
308 *
309 * If you are wondering why this routine does not reorder the attributes
310 * first, and might be tempted to make it do so, see the comment by the
311 * call to ReorderAttributes in cmsencode.c. (Or, see who else calls this
312 * and think long and hard about the implications of making it always
313 * do the reordering.)
314 */
315 CSSM_DATA_PTR
316 SecCmsAttributeArrayEncode(PRArenaPool *poolp, SecCmsAttribute ***attrs, CSSM_DATA_PTR dest)
317 {
318 return SEC_ASN1EncodeItem (poolp, dest, (void *)attrs, nss_cms_set_of_attribute_template);
319 }
320
321 /*
322 * SecCmsAttributeArrayReorder - sort attribute array by attribute's DER encoding
323 *
324 * make sure that the order of the attributes guarantees valid DER (which must be
325 * in lexigraphically ascending order for a SET OF); if reordering is necessary it
326 * will be done in place (in attrs).
327 */
328 OSStatus
329 SecCmsAttributeArrayReorder(SecCmsAttribute **attrs)
330 {
331 return SecCmsArraySortByDER((void **)attrs, nss_cms_attribute_template, NULL);
332 }
333
334 /*
335 * SecCmsAttributeArrayFindAttrByOidTag - look through a set of attributes and
336 * find one that matches the specified object ID.
337 *
338 * If "only" is true, then make sure that there is not more than one attribute
339 * of the same type. Otherwise, just return the first one found. (XXX Does
340 * anybody really want that first-found behavior? It was like that when I found it...)
341 */
342 SecCmsAttribute *
343 SecCmsAttributeArrayFindAttrByOidTag(SecCmsAttribute **attrs, SECOidTag oidtag, Boolean only)
344 {
345 SECOidData *oid;
346 SecCmsAttribute *attr1, *attr2;
347
348 if (attrs == NULL)
349 return NULL;
350
351 oid = SECOID_FindOIDByTag(oidtag);
352 if (oid == NULL)
353 return NULL;
354
355 while ((attr1 = *attrs++) != NULL) {
356 if (attr1->type.Length == oid->oid.Length && PORT_Memcmp (attr1->type.Data,
357 oid->oid.Data,
358 oid->oid.Length) == 0)
359 break;
360 }
361
362 if (attr1 == NULL)
363 return NULL;
364
365 if (!only)
366 return attr1;
367
368 while ((attr2 = *attrs++) != NULL) {
369 if (attr2->type.Length == oid->oid.Length && PORT_Memcmp (attr2->type.Data,
370 oid->oid.Data,
371 oid->oid.Length) == 0)
372 break;
373 }
374
375 if (attr2 != NULL)
376 return NULL;
377
378 return attr1;
379 }
380
381 /*
382 * SecCmsAttributeArrayAddAttr - add an attribute to an
383 * array of attributes.
384 */
385 OSStatus
386 SecCmsAttributeArrayAddAttr(PLArenaPool *poolp, SecCmsAttribute ***attrs, SecCmsAttribute *attr)
387 {
388 SecCmsAttribute *oattr;
389 void *mark;
390 SECOidTag type;
391
392 mark = PORT_ArenaMark(poolp);
393
394 /* find oidtag of attr */
395 type = SecCmsAttributeGetType(attr);
396
397 /* see if we have one already */
398 oattr = SecCmsAttributeArrayFindAttrByOidTag(*attrs, type, PR_FALSE);
399 PORT_Assert (oattr == NULL);
400 if (oattr != NULL)
401 goto loser; /* XXX or would it be better to replace it? */
402
403 /* no, shove it in */
404 if (SecCmsArrayAdd(poolp, (void ***)attrs, (void *)attr) != SECSuccess)
405 goto loser;
406
407 PORT_ArenaUnmark(poolp, mark);
408 return SECSuccess;
409
410 loser:
411 PORT_ArenaRelease(poolp, mark);
412 return SECFailure;
413 }
414
415 /*
416 * SecCmsAttributeArraySetAttr - set an attribute's value in a set of attributes
417 */
418 OSStatus
419 SecCmsAttributeArraySetAttr(PLArenaPool *poolp, SecCmsAttribute ***attrs, SECOidTag type, CSSM_DATA_PTR value, Boolean encoded)
420 {
421 SecCmsAttribute *attr;
422 void *mark;
423
424 mark = PORT_ArenaMark(poolp);
425
426 /* see if we have one already */
427 attr = SecCmsAttributeArrayFindAttrByOidTag(*attrs, type, PR_FALSE);
428 if (attr == NULL) {
429 /* not found? create one! */
430 attr = SecCmsAttributeCreate(poolp, type, value, encoded);
431 if (attr == NULL)
432 goto loser;
433 /* and add it to the list */
434 if (SecCmsArrayAdd(poolp, (void ***)attrs, (void *)attr) != SECSuccess)
435 goto loser;
436 } else {
437 /* found, shove it in */
438 /* XXX we need a decent memory model @#$#$!#!!! */
439 attr->values[0] = value;
440 attr->encoded = encoded;
441 }
442
443 PORT_ArenaUnmark (poolp, mark);
444 return SECSuccess;
445
446 loser:
447 PORT_ArenaRelease (poolp, mark);
448 return SECFailure;
449 }
450