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