2 * Copyright (c) 2004-2005 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 * identSearch.cpp - search for identity whose cert has specified email address
28 #include "identSearch.h"
29 #include <Security/SecKeychainItemPriv.h> /* for kSecAlias */
32 * Does the specified identity's cert have the specified email address? Returns
37 const void *emailAddress
, // UTF8 encoded email address
38 unsigned emailAddressLen
)
40 SecCertificateRef certRef
;
44 ortn
= SecIdentityCopyCertificate(idRef
, &certRef
);
46 /* should never happen */
47 cssmPerror("SecIdentityCopyCertificate", ortn
);
52 * Fetch one attribute - the alias (which is always the "best attempt" at
53 * finding an email address within a cert).
55 UInt32 oneTag
= kSecAlias
;
56 SecKeychainAttributeInfo attrInfo
;
58 attrInfo
.tag
= &oneTag
;
59 attrInfo
.format
= NULL
;
60 SecKeychainAttributeList
*attrList
= NULL
;
61 SecKeychainAttribute
*attr
= NULL
;
63 ortn
= SecKeychainItemCopyAttributesAndData((SecKeychainItemRef
)certRef
,
67 NULL
, // length - don't need the data
69 if(ortn
|| (attrList
== NULL
) || (attrList
->count
!= 1)) {
70 /* I don't *think* this should ever happen... */
71 cssmPerror("SecKeychainItemCopyAttributesAndData", ortn
);
74 attr
= attrList
->attr
;
75 if(attr
->length
== emailAddressLen
) {
76 if(!memcmp(attr
->data
, emailAddress
, emailAddressLen
)) {
81 SecKeychainItemFreeAttributesAndData(attrList
, NULL
);
87 OSStatus
findIdentity(
88 const void *emailAddress
, // UTF8 encoded email address
89 unsigned emailAddressLen
,
90 SecKeychainRef kcRef
, // keychain to search, or NULL to search all
91 SecIdentityRef
*idRef
) // RETURNED
95 /* Search for all identities */
96 SecIdentitySearchRef srchRef
= nil
;
97 ortn
= SecIdentitySearchCreate(kcRef
,
101 /* should never happen */
102 cssmPerror("SecIdentitySearchCreate", ortn
);
106 SecIdentityRef foundId
= NULL
;
108 SecIdentityRef thisId
;
109 ortn
= SecIdentitySearchCopyNext(srchRef
, &thisId
);
113 /* email addres match? */
114 if(idHasEmail(thisId
, emailAddress
, emailAddressLen
)) {
119 /* we're done with thie identity */
122 } while(ortn
== noErr
);
129 return errSecItemNotFound
;