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
43 #include <security_asn1/secasn1.h>
44 #include <security_asn1/secerr.h>
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!
58 * SecCmsAttributeCreate - create an attribute
60 * if value is NULL, the attribute won't have a value. It can be added later
61 * with SecCmsAttributeAddValue.
64 SecCmsAttributeCreate(PRArenaPool
*poolp
, SECOidTag oidtag
, CSSM_DATA_PTR value
, Boolean encoded
)
66 SecCmsAttribute
*attr
;
67 CSSM_DATA_PTR copiedvalue
;
70 PORT_Assert (poolp
!= NULL
);
72 mark
= PORT_ArenaMark (poolp
);
74 attr
= (SecCmsAttribute
*)PORT_ArenaZAlloc(poolp
, sizeof(SecCmsAttribute
));
78 attr
->typeTag
= SECOID_FindOIDByTag(oidtag
);
79 if (attr
->typeTag
== NULL
)
82 if (SECITEM_CopyItem(poolp
, &(attr
->type
), &(attr
->typeTag
->oid
)) != SECSuccess
)
86 if ((copiedvalue
= SECITEM_AllocItem(poolp
, NULL
, value
->Length
)) == NULL
)
89 if (SECITEM_CopyItem(poolp
, copiedvalue
, value
) != SECSuccess
)
92 if (SecCmsArrayAdd(poolp
, (void ***)&(attr
->values
), (void *)copiedvalue
) != SECSuccess
)
96 attr
->encoded
= encoded
;
98 PORT_ArenaUnmark (poolp
, mark
);
103 PORT_Assert (mark
!= NULL
);
104 PORT_ArenaRelease (poolp
, mark
);
109 * SecCmsAttributeAddValue - add another value to an attribute
112 SecCmsAttributeAddValue(PLArenaPool
*poolp
, SecCmsAttribute
*attr
, CSSM_DATA_PTR value
)
114 CSSM_DATA copiedvalue
;
117 PORT_Assert (poolp
!= NULL
);
119 mark
= PORT_ArenaMark(poolp
);
121 /* XXX we need an object memory model #$%#$%! */
122 if (SECITEM_CopyItem(poolp
, &copiedvalue
, value
) != SECSuccess
)
125 if (SecCmsArrayAdd(poolp
, (void ***)&(attr
->values
), (void *)&copiedvalue
) != SECSuccess
)
128 PORT_ArenaUnmark(poolp
, mark
);
132 PORT_Assert (mark
!= NULL
);
133 PORT_ArenaRelease (poolp
, mark
);
138 * SecCmsAttributeGetType - return the OID tag
141 SecCmsAttributeGetType(SecCmsAttribute
*attr
)
145 typetag
= SECOID_FindOID(&(attr
->type
));
147 return SEC_OID_UNKNOWN
;
149 return typetag
->offset
;
153 * SecCmsAttributeGetValue - return the first attribute value
155 * We do some sanity checking first:
156 * - Multiple values are *not* expected.
157 * - Empty values are *not* expected.
160 SecCmsAttributeGetValue(SecCmsAttribute
*attr
)
167 value
= attr
->values
[0];
169 if (value
== NULL
|| value
->Data
== NULL
|| value
->Length
== 0)
172 if (attr
->values
[1] != NULL
)
179 * SecCmsAttributeCompareValue - compare the attribute's first value against data
182 SecCmsAttributeCompareValue(SecCmsAttribute
*attr
, CSSM_DATA_PTR av
)
189 value
= SecCmsAttributeGetValue(attr
);
191 return (value
!= NULL
&& value
->Length
== av
->Length
&&
192 PORT_Memcmp (value
->Data
, av
->Data
, value
->Length
) == 0);
196 * templates and functions for separate ASN.1 encoding of attributes
198 * used in SecCmsAttributeArrayReorder
202 * helper function for dynamic template determination of the attribute value
204 static const SecAsn1Template
*
205 cms_attr_choose_attr_value_template(void *src_or_dest
, Boolean encoding
, const char *buf
, void *dest
)
207 const SecAsn1Template
*theTemplate
;
208 SecCmsAttribute
*attribute
;
212 PORT_Assert (src_or_dest
!= NULL
);
213 if (src_or_dest
== NULL
)
216 attribute
= (SecCmsAttribute
*)src_or_dest
;
218 if (encoding
&& attribute
->encoded
)
219 /* we're encoding, and the attribute value is already encoded. */
220 return SEC_ASN1_GET(kSecAsn1AnyTemplate
);
222 /* get attribute's typeTag */
223 oiddata
= attribute
->typeTag
;
224 if (oiddata
== NULL
) {
225 oiddata
= SECOID_FindOID(&attribute
->type
);
226 attribute
->typeTag
= oiddata
;
229 if (oiddata
== NULL
) {
230 /* still no OID tag? OID is unknown then. en/decode value as ANY. */
232 theTemplate
= SEC_ASN1_GET(kSecAsn1AnyTemplate
);
234 switch (oiddata
->offset
) {
235 case SEC_OID_PKCS9_SMIME_CAPABILITIES
:
236 case SEC_OID_SMIME_ENCRYPTION_KEY_PREFERENCE
:
237 /* these guys need to stay DER-encoded */
239 /* same goes for OIDs that are not handled here */
241 theTemplate
= SEC_ASN1_GET(kSecAsn1AnyTemplate
);
243 /* otherwise choose proper template */
244 case SEC_OID_PKCS9_EMAIL_ADDRESS
:
245 case SEC_OID_RFC1274_MAIL
:
246 case SEC_OID_PKCS9_UNSTRUCTURED_NAME
:
248 theTemplate
= SEC_ASN1_GET(kSecAsn1IA5StringTemplate
);
250 case SEC_OID_PKCS9_CONTENT_TYPE
:
252 theTemplate
= SEC_ASN1_GET(kSecAsn1ObjectIDTemplate
);
254 case SEC_OID_PKCS9_MESSAGE_DIGEST
:
256 theTemplate
= SEC_ASN1_GET(kSecAsn1OctetStringTemplate
);
258 case SEC_OID_PKCS9_SIGNING_TIME
:
260 theTemplate
= SEC_ASN1_GET(kSecAsn1UTCTimeTemplate
); // @@@ This should be a choice between UTCTime and GeneralizedTime -- mb
262 /* XXX Want other types here, too */
268 * If we are encoding and we think we have an already-encoded value,
269 * then the code which initialized this attribute should have set
270 * the "encoded" property to true (and we would have returned early,
271 * up above). No devastating error, but that code should be fixed.
272 * (It could indicate that the resulting encoded bytes are wrong.)
274 PORT_Assert (!encoded
);
277 * We are decoding; record whether the resulting value is
278 * still encoded or not.
280 attribute
->encoded
= encoded
;
285 static const SecAsn1TemplateChooserPtr cms_attr_chooser
286 = cms_attr_choose_attr_value_template
;
288 const SecAsn1Template nss_cms_attribute_template
[] = {
290 0, NULL
, sizeof(SecCmsAttribute
) },
291 { SEC_ASN1_OBJECT_ID
,
292 offsetof(SecCmsAttribute
,type
) },
293 { SEC_ASN1_DYNAMIC
| SEC_ASN1_SET_OF
,
294 offsetof(SecCmsAttribute
,values
),
299 const SecAsn1Template nss_cms_set_of_attribute_template
[] = {
300 { SEC_ASN1_SET_OF
, 0, nss_cms_attribute_template
}
303 /* =============================================================================
304 * Attribute Array methods
308 * SecCmsAttributeArrayEncode - encode an Attribute array as SET OF Attributes
310 * If you are wondering why this routine does not reorder the attributes
311 * first, and might be tempted to make it do so, see the comment by the
312 * call to ReorderAttributes in cmsencode.c. (Or, see who else calls this
313 * and think long and hard about the implications of making it always
314 * do the reordering.)
317 SecCmsAttributeArrayEncode(PRArenaPool
*poolp
, SecCmsAttribute
***attrs
, CSSM_DATA_PTR dest
)
319 return SEC_ASN1EncodeItem (poolp
, dest
, (void *)attrs
, nss_cms_set_of_attribute_template
);
323 * SecCmsAttributeArrayReorder - sort attribute array by attribute's DER encoding
325 * make sure that the order of the attributes guarantees valid DER (which must be
326 * in lexigraphically ascending order for a SET OF); if reordering is necessary it
327 * will be done in place (in attrs).
330 SecCmsAttributeArrayReorder(SecCmsAttribute
**attrs
)
332 return SecCmsArraySortByDER((void **)attrs
, nss_cms_attribute_template
, NULL
);
336 * SecCmsAttributeArrayFindAttrByOidTag - look through a set of attributes and
337 * find one that matches the specified object ID.
339 * If "only" is true, then make sure that there is not more than one attribute
340 * of the same type. Otherwise, just return the first one found. (XXX Does
341 * anybody really want that first-found behavior? It was like that when I found it...)
344 SecCmsAttributeArrayFindAttrByOidTag(SecCmsAttribute
**attrs
, SECOidTag oidtag
, Boolean only
)
347 SecCmsAttribute
*attr1
, *attr2
;
352 oid
= SECOID_FindOIDByTag(oidtag
);
356 while ((attr1
= *attrs
++) != NULL
) {
357 if (attr1
->type
.Length
== oid
->oid
.Length
&& PORT_Memcmp (attr1
->type
.Data
,
359 oid
->oid
.Length
) == 0)
369 while ((attr2
= *attrs
++) != NULL
) {
370 if (attr2
->type
.Length
== oid
->oid
.Length
&& PORT_Memcmp (attr2
->type
.Data
,
372 oid
->oid
.Length
) == 0)
383 * SecCmsAttributeArrayAddAttr - add an attribute to an
384 * array of attributes.
387 SecCmsAttributeArrayAddAttr(PLArenaPool
*poolp
, SecCmsAttribute
***attrs
, SecCmsAttribute
*attr
)
389 SecCmsAttribute
*oattr
;
393 mark
= PORT_ArenaMark(poolp
);
395 /* find oidtag of attr */
396 type
= SecCmsAttributeGetType(attr
);
398 /* see if we have one already */
399 oattr
= SecCmsAttributeArrayFindAttrByOidTag(*attrs
, type
, PR_FALSE
);
400 PORT_Assert (oattr
== NULL
);
402 goto loser
; /* XXX or would it be better to replace it? */
404 /* no, shove it in */
405 if (SecCmsArrayAdd(poolp
, (void ***)attrs
, (void *)attr
) != SECSuccess
)
408 PORT_ArenaUnmark(poolp
, mark
);
412 PORT_ArenaRelease(poolp
, mark
);
417 * SecCmsAttributeArraySetAttr - set an attribute's value in a set of attributes
420 SecCmsAttributeArraySetAttr(PLArenaPool
*poolp
, SecCmsAttribute
***attrs
, SECOidTag type
, CSSM_DATA_PTR value
, Boolean encoded
)
422 SecCmsAttribute
*attr
;
425 mark
= PORT_ArenaMark(poolp
);
427 /* see if we have one already */
428 attr
= SecCmsAttributeArrayFindAttrByOidTag(*attrs
, type
, PR_FALSE
);
430 /* not found? create one! */
431 attr
= SecCmsAttributeCreate(poolp
, type
, value
, encoded
);
434 /* and add it to the list */
435 if (SecCmsArrayAdd(poolp
, (void ***)attrs
, (void *)attr
) != SECSuccess
)
438 /* found, shove it in */
439 /* XXX we need a decent memory model @#$#$!#!!! */
440 attr
->values
[0] = value
;
441 attr
->encoded
= encoded
;
444 PORT_ArenaUnmark (poolp
, mark
);
448 PORT_ArenaRelease (poolp
, mark
);