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/
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.
12 * The Original Code is the Netscape security libraries.
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
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
41 #include "SecAsn1Item.h"
43 #include <security_asn1/secasn1.h>
44 #include <security_asn1/secerr.h>
45 #include <security_asn1/secport.h>
47 #include <Security/SecAsn1Templates.h>
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!
61 * SecCmsAttributeCreate - create an attribute
63 * if value is NULL, the attribute won't have a value. It can be added later
64 * with SecCmsAttributeAddValue.
67 SecCmsAttributeCreate(PRArenaPool
*poolp
, SECOidTag oidtag
, SecAsn1Item
* value
, Boolean encoded
)
69 SecCmsAttribute
*attr
;
70 SecAsn1Item
* copiedvalue
;
73 PORT_Assert (poolp
!= NULL
);
75 mark
= PORT_ArenaMark (poolp
);
77 attr
= (SecCmsAttribute
*)PORT_ArenaZAlloc(poolp
, sizeof(SecCmsAttribute
));
81 attr
->typeTag
= SECOID_FindOIDByTag(oidtag
);
82 if (attr
->typeTag
== NULL
)
85 if (SECITEM_CopyItem(poolp
, &(attr
->type
), &(attr
->typeTag
->oid
)) != SECSuccess
)
89 if ((copiedvalue
= SECITEM_AllocItem(poolp
, NULL
, value
->Length
)) == NULL
)
92 if (SECITEM_CopyItem(poolp
, copiedvalue
, value
) != SECSuccess
)
95 if (SecCmsArrayAdd(poolp
, (void ***)&(attr
->values
), (void *)copiedvalue
) != SECSuccess
)
99 attr
->encoded
= encoded
;
101 PORT_ArenaUnmark (poolp
, mark
);
106 PORT_Assert (mark
!= NULL
);
107 PORT_ArenaRelease (poolp
, mark
);
112 * SecCmsAttributeAddValue - add another value to an attribute
115 SecCmsAttributeAddValue(PLArenaPool
*poolp
, SecCmsAttribute
*attr
, SecAsn1Item
* value
)
117 SecAsn1Item
*copiedvalue
;
120 PORT_Assert (poolp
!= NULL
);
122 mark
= PORT_ArenaMark(poolp
);
125 if ((copiedvalue
= SECITEM_AllocItem(poolp
, NULL
, value
->Length
)) == NULL
)
128 if (SECITEM_CopyItem(poolp
, copiedvalue
, value
) != SECSuccess
)
131 if (SecCmsArrayAdd(poolp
, (void ***)&(attr
->values
), (void *)copiedvalue
) != SECSuccess
)
135 PORT_ArenaUnmark(poolp
, mark
);
139 PORT_Assert (mark
!= NULL
);
140 PORT_ArenaRelease (poolp
, mark
);
145 * SecCmsAttributeGetType - return the OID tag
148 SecCmsAttributeGetType(SecCmsAttribute
*attr
)
152 typetag
= SECOID_FindOID(&(attr
->type
));
154 return SEC_OID_UNKNOWN
;
156 return typetag
->offset
;
160 * SecCmsAttributeGetValue - return the first attribute value
162 * We do some sanity checking first:
163 * - Multiple values are *not* expected.
164 * - Empty values are *not* expected.
167 SecCmsAttributeGetValue(SecCmsAttribute
*attr
)
174 value
= attr
->values
[0];
176 if (value
== NULL
|| value
->Data
== NULL
|| value
->Length
== 0)
179 if (attr
->values
[1] != NULL
)
186 * SecCmsAttributeCompareValue - compare the attribute's first value against data
189 SecCmsAttributeCompareValue(SecCmsAttribute
*attr
, SecAsn1Item
* av
)
196 value
= SecCmsAttributeGetValue(attr
);
198 return (value
!= NULL
&& value
->Length
== av
->Length
&&
199 PORT_Memcmp (value
->Data
, av
->Data
, value
->Length
) == 0);
203 * templates and functions for separate ASN.1 encoding of attributes
205 * used in SecCmsAttributeArrayReorder
209 * helper function for dynamic template determination of the attribute value
211 static const SecAsn1Template
*
212 cms_attr_choose_attr_value_template(void *src_or_dest
, Boolean encoding
, const char *buf
, size_t len
, void *dest
)
214 const SecAsn1Template
*theTemplate
;
215 SecCmsAttribute
*attribute
;
219 PORT_Assert (src_or_dest
!= NULL
);
220 if (src_or_dest
== NULL
)
223 attribute
= (SecCmsAttribute
*)src_or_dest
;
225 if (encoding
&& attribute
->encoded
)
226 /* we're encoding, and the attribute value is already encoded. */
227 return SEC_ASN1_GET(kSecAsn1AnyTemplate
);
229 /* get attribute's typeTag */
230 oiddata
= attribute
->typeTag
;
231 if (oiddata
== NULL
) {
232 oiddata
= SECOID_FindOID(&attribute
->type
);
233 attribute
->typeTag
= oiddata
;
236 if (oiddata
== NULL
) {
237 /* still no OID tag? OID is unknown then. en/decode value as ANY. */
239 theTemplate
= SEC_ASN1_GET(kSecAsn1AnyTemplate
);
241 switch (oiddata
->offset
) {
242 case SEC_OID_PKCS9_SMIME_CAPABILITIES
:
243 case SEC_OID_SMIME_ENCRYPTION_KEY_PREFERENCE
:
244 case SEC_OID_APPLE_HASH_AGILITY_V2
:
245 /* these guys need to stay DER-encoded */
247 /* same goes for OIDs that are not handled here */
249 theTemplate
= SEC_ASN1_GET(kSecAsn1AnyTemplate
);
251 /* otherwise choose proper template */
252 case SEC_OID_PKCS9_EMAIL_ADDRESS
:
253 case SEC_OID_RFC1274_MAIL
:
254 case SEC_OID_PKCS9_UNSTRUCTURED_NAME
:
256 theTemplate
= SEC_ASN1_GET(kSecAsn1IA5StringTemplate
);
258 case SEC_OID_PKCS9_CONTENT_TYPE
:
260 theTemplate
= SEC_ASN1_GET(kSecAsn1ObjectIDTemplate
);
262 case SEC_OID_PKCS9_MESSAGE_DIGEST
:
263 case SEC_OID_APPLE_HASH_AGILITY
:
265 theTemplate
= SEC_ASN1_GET(kSecAsn1OctetStringTemplate
);
267 case SEC_OID_PKCS9_SIGNING_TIME
:
268 case SEC_OID_APPLE_EXPIRATION_TIME
:
270 theTemplate
= SEC_ASN1_GET(kSecAsn1UTCTimeTemplate
); // @@@ This should be a choice between UTCTime and GeneralizedTime -- mb
272 /* XXX Want other types here, too */
278 * If we are encoding and we think we have an already-encoded value,
279 * then the code which initialized this attribute should have set
280 * the "encoded" property to true (and we would have returned early,
281 * up above). No devastating error, but that code should be fixed.
282 * (It could indicate that the resulting encoded bytes are wrong.)
284 PORT_Assert (!encoded
);
287 * We are decoding; record whether the resulting value is
288 * still encoded or not.
290 attribute
->encoded
= encoded
;
295 static const SecAsn1TemplateChooserPtr cms_attr_chooser
296 = cms_attr_choose_attr_value_template
;
298 const SecAsn1Template nss_cms_attribute_template
[] = {
300 0, NULL
, sizeof(SecCmsAttribute
) },
301 { SEC_ASN1_OBJECT_ID
,
302 offsetof(SecCmsAttribute
,type
) },
303 { SEC_ASN1_DYNAMIC
| SEC_ASN1_SET_OF
,
304 offsetof(SecCmsAttribute
,values
),
309 const SecAsn1Template nss_cms_set_of_attribute_template
[] = {
310 { SEC_ASN1_SET_OF
, 0, nss_cms_attribute_template
}
313 /* =============================================================================
314 * Attribute Array methods
318 * SecCmsAttributeArrayEncode - encode an Attribute array as SET OF Attributes
320 * If you are wondering why this routine does not reorder the attributes
321 * first, and might be tempted to make it do so, see the comment by the
322 * call to ReorderAttributes in cmsencode.c. (Or, see who else calls this
323 * and think long and hard about the implications of making it always
324 * do the reordering.)
327 SecCmsAttributeArrayEncode(PRArenaPool
*poolp
, SecCmsAttribute
***attrs
, SecAsn1Item
* dest
)
329 return SEC_ASN1EncodeItem (poolp
, dest
, (void *)attrs
, nss_cms_set_of_attribute_template
);
333 * SecCmsAttributeArrayReorder - sort attribute array by attribute's DER encoding
335 * make sure that the order of the attributes guarantees valid DER (which must be
336 * in lexigraphically ascending order for a SET OF); if reordering is necessary it
337 * will be done in place (in attrs).
340 SecCmsAttributeArrayReorder(SecCmsAttribute
**attrs
)
342 return SecCmsArraySortByDER((void **)attrs
, nss_cms_attribute_template
, NULL
);
346 * SecCmsAttributeArrayFindAttrByOidTag - look through a set of attributes and
347 * find one that matches the specified object ID.
349 * If "only" is true, then make sure that there is not more than one attribute
350 * of the same type. Otherwise, just return the first one found. (XXX Does
351 * anybody really want that first-found behavior? It was like that when I found it...)
354 SecCmsAttributeArrayFindAttrByOidTag(SecCmsAttribute
**attrs
, SECOidTag oidtag
, Boolean only
)
357 SecCmsAttribute
*attr1
, *attr2
;
362 oid
= SECOID_FindOIDByTag(oidtag
);
366 while ((attr1
= *attrs
++) != NULL
) {
367 if (attr1
->type
.Length
== oid
->oid
.Length
&& PORT_Memcmp (attr1
->type
.Data
,
369 oid
->oid
.Length
) == 0)
379 while ((attr2
= *attrs
++) != NULL
) {
380 if (attr2
->type
.Length
== oid
->oid
.Length
&& PORT_Memcmp (attr2
->type
.Data
,
382 oid
->oid
.Length
) == 0)
393 * SecCmsAttributeArrayAddAttr - add an attribute to an
394 * array of attributes.
397 SecCmsAttributeArrayAddAttr(PLArenaPool
*poolp
, SecCmsAttribute
***attrs
, SecCmsAttribute
*attr
)
399 SecCmsAttribute
*oattr
;
403 mark
= PORT_ArenaMark(poolp
);
405 /* find oidtag of attr */
406 type
= SecCmsAttributeGetType(attr
);
408 /* see if we have one already */
409 oattr
= SecCmsAttributeArrayFindAttrByOidTag(*attrs
, type
, PR_FALSE
);
410 PORT_Assert (oattr
== NULL
);
412 goto loser
; /* XXX or would it be better to replace it? */
414 /* no, shove it in */
415 if (SecCmsArrayAdd(poolp
, (void ***)attrs
, (void *)attr
) != SECSuccess
)
418 PORT_ArenaUnmark(poolp
, mark
);
422 PORT_ArenaRelease(poolp
, mark
);
427 * SecCmsAttributeArraySetAttr - set an attribute's value in a set of attributes
430 SecCmsAttributeArraySetAttr(PLArenaPool
*poolp
, SecCmsAttribute
***attrs
, SECOidTag type
, SecAsn1Item
* value
, Boolean encoded
)
432 SecCmsAttribute
*attr
;
435 mark
= PORT_ArenaMark(poolp
);
437 /* see if we have one already */
438 attr
= SecCmsAttributeArrayFindAttrByOidTag(*attrs
, type
, PR_FALSE
);
440 /* not found? create one! */
441 attr
= SecCmsAttributeCreate(poolp
, type
, value
, encoded
);
444 /* and add it to the list */
445 if (SecCmsArrayAdd(poolp
, (void ***)attrs
, (void *)attr
) != SECSuccess
)
448 /* found, shove it in */
449 /* XXX we need a decent memory model @#$#$!#!!! */
450 attr
->values
[0] = value
;
451 attr
->encoded
= encoded
;
454 PORT_ArenaUnmark (poolp
, mark
);
458 PORT_ArenaRelease (poolp
, mark
);