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