2 * Copyright (c) 2002-2007,2011-2014 Apple 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@
27 #include <security_keychain/Certificate.h>
28 #include <security_cdsa_utilities/Schema.h>
29 #include <Security/oidscert.h>
30 #include <Security/oidsattr.h>
31 #include <Security/SecCertificate.h>
32 #include <Security/SecCertificatePriv.h>
33 #include <security_cdsa_client/cspclient.h>
34 #include <security_keychain/KeyItem.h>
35 #include <security_keychain/KCCursor.h>
37 #include <CommonCrypto/CommonDigestSPI.h>
40 using namespace KeychainCore
;
43 Certificate::clForType(CSSM_CERT_TYPE type
)
45 return CL(gGuidAppleX509CL
);
48 Certificate::Certificate(const CSSM_DATA
&data
, CSSM_CERT_TYPE type
, CSSM_CERT_ENCODING encoding
) :
49 ItemImpl(CSSM_DL_DB_RECORD_X509_CERTIFICATE
, reinterpret_cast<SecKeychainAttributeList
*>(NULL
), UInt32(data
.Length
), reinterpret_cast<const void *>(data
.Data
)),
50 mHaveTypeAndEncoding(true),
56 mV1SubjectPublicKeyCStructValue(NULL
),
57 mV1SubjectNameCStructValue(NULL
),
58 mV1IssuerNameCStructValue(NULL
),
61 if (data
.Length
== 0 || data
.Data
== NULL
)
62 MacOSError::throwMe(errSecParam
);
65 // db item constructor
66 Certificate::Certificate(const Keychain
&keychain
, const PrimaryKey
&primaryKey
, const CssmClient::DbUniqueRecord
&uniqueId
) :
67 ItemImpl(keychain
, primaryKey
, uniqueId
),
68 mHaveTypeAndEncoding(false),
72 mV1SubjectPublicKeyCStructValue(NULL
),
73 mV1SubjectNameCStructValue(NULL
),
74 mV1IssuerNameCStructValue(NULL
),
81 Certificate
* Certificate::make(const Keychain
&keychain
, const PrimaryKey
&primaryKey
, const CssmClient::DbUniqueRecord
&uniqueId
)
83 Certificate
* c
= new Certificate(keychain
, primaryKey
, uniqueId
);
84 keychain
->addItem(primaryKey
, c
);
90 Certificate
* Certificate::make(const Keychain
&keychain
, const PrimaryKey
&primaryKey
)
92 Certificate
* c
= new Certificate(keychain
, primaryKey
);
93 keychain
->addItem(primaryKey
, c
);
100 // PrimaryKey item constructor
101 Certificate::Certificate(const Keychain
&keychain
, const PrimaryKey
&primaryKey
) :
102 ItemImpl(keychain
, primaryKey
),
103 mHaveTypeAndEncoding(false),
107 mV1SubjectPublicKeyCStructValue(NULL
),
108 mV1SubjectNameCStructValue(NULL
),
109 mV1IssuerNameCStructValue(NULL
),
112 // @@@ In this case we don't know the type...
115 Certificate::Certificate(Certificate
&certificate
) :
116 ItemImpl(certificate
),
117 mHaveTypeAndEncoding(certificate
.mHaveTypeAndEncoding
),
118 mPopulated(false /* certificate.mPopulated */),
119 mType(certificate
.mType
),
120 mEncoding(certificate
.mEncoding
),
121 mCL(certificate
.mCL
),
123 mV1SubjectPublicKeyCStructValue(NULL
),
124 mV1SubjectNameCStructValue(NULL
),
125 mV1IssuerNameCStructValue(NULL
),
130 Certificate::~Certificate()
133 if (mV1SubjectPublicKeyCStructValue
)
134 releaseFieldValue(CSSMOID_X509V1SubjectPublicKeyCStruct
, mV1SubjectPublicKeyCStructValue
);
136 if (mCertHandle
&& mCL
)
137 CSSM_CL_CertAbortCache(mCL
->handle(), mCertHandle
);
139 if (mV1SubjectNameCStructValue
)
140 releaseFieldValue(CSSMOID_X509V1SubjectNameCStruct
, mV1SubjectNameCStructValue
);
142 if (mV1IssuerNameCStructValue
)
143 releaseFieldValue(CSSMOID_X509V1IssuerNameCStruct
, mV1IssuerNameCStructValue
);
146 CFRelease(mSha1Hash
);
153 Certificate::certHandle()
155 StLock
<Mutex
>_(mMutex
);
156 const CSSM_DATA
*cert
= &data();
159 if (CSSM_RETURN retval
= CSSM_CL_CertCache(clHandle(), cert
, &mCertHandle
))
160 CssmError::throwMe(retval
);
166 /* Return a zero terminated list of CSSM_DATA_PTR's with the values of the field specified by field. Caller must call releaseFieldValues to free the storage allocated by this call. */
168 Certificate::copyFieldValues(const CSSM_OID
&field
)
170 StLock
<Mutex
>_(mMutex
);
171 CSSM_CL_HANDLE clh
= clHandle();
172 CSSM_DATA_PTR fieldValue
, *fieldValues
;
173 CSSM_HANDLE resultsHandle
= 0;
174 uint32 numberOfFields
= 0;
177 result
= CSSM_CL_CertGetFirstCachedFieldValue(clh
, certHandle(), &field
, &resultsHandle
, &numberOfFields
, &fieldValue
);
180 if (result
== CSSMERR_CL_NO_FIELD_VALUES
)
183 CssmError::throwMe(result
);
186 fieldValues
= new CSSM_DATA_PTR
[numberOfFields
+ 1];
187 fieldValues
[0] = fieldValue
;
188 fieldValues
[numberOfFields
] = NULL
;
190 for (uint32 value
= 1; value
< numberOfFields
; ++value
)
192 CSSM_RETURN cresult
= CSSM_CL_CertGetNextCachedFieldValue(clh
, resultsHandle
, &fieldValues
[value
]);
195 fieldValues
[value
] = NULL
;
197 break; // No point in continuing really.
201 CSSM_CL_CertAbortQuery(clh
, resultsHandle
);
205 releaseFieldValues(field
, fieldValues
);
206 CssmError::throwMe(result
);
213 Certificate::releaseFieldValues(const CSSM_OID
&field
, CSSM_DATA_PTR
*fieldValues
)
215 StLock
<Mutex
>_(mMutex
);
218 CSSM_CL_HANDLE clh
= clHandle();
220 for (int ix
= 0; fieldValues
[ix
]; ++ix
)
221 CSSM_CL_FreeFieldValue(clh
, &field
, fieldValues
[ix
]);
223 delete[] fieldValues
;
228 Certificate::addParsedAttribute(const CSSM_DB_ATTRIBUTE_INFO
&info
, const CSSM_OID
&field
)
230 StLock
<Mutex
>_(mMutex
);
231 CSSM_DATA_PTR
*fieldValues
= copyFieldValues(field
);
234 CssmDbAttributeData
&anAttr
= mDbAttributes
->add(info
);
235 for (int ix
= 0; fieldValues
[ix
]; ++ix
)
236 anAttr
.add(*fieldValues
[ix
], *mDbAttributes
);
238 releaseFieldValues(field
, fieldValues
);
243 Certificate::addSubjectKeyIdentifier()
245 StLock
<Mutex
>_(mMutex
);
246 const CSSM_DB_ATTRIBUTE_INFO
&info
= Schema::attributeInfo(kSecSubjectKeyIdentifierItemAttr
);
247 const CSSM_OID
&field
= CSSMOID_SubjectKeyIdentifier
;
249 CSSM_DATA_PTR
*fieldValues
= copyFieldValues(field
);
252 CssmDbAttributeData
&anAttr
= mDbAttributes
->add(info
);
253 for (int ix
= 0; fieldValues
[ix
]; ++ix
)
255 const CSSM_X509_EXTENSION
*extension
= reinterpret_cast<const CSSM_X509_EXTENSION
*>(fieldValues
[ix
]->Data
);
256 if (extension
== NULL
|| fieldValues
[ix
]->Length
!= sizeof(CSSM_X509_EXTENSION
))
258 assert(extension
!= NULL
&& fieldValues
[ix
]->Length
== sizeof(CSSM_X509_EXTENSION
));
261 const CE_SubjectKeyID
*skid
= reinterpret_cast<CE_SubjectKeyID
*>(extension
->value
.parsedValue
);
264 assert(skid
!= NULL
);
267 anAttr
.add(*skid
, *mDbAttributes
);
270 releaseFieldValues(field
, fieldValues
);
274 /* Return a CSSM_DATA_PTR with the value of the first field specified by field. Caller must call releaseFieldValue to free the storage allocated by this call. */
276 Certificate::copyFirstFieldValue(const CSSM_OID
&field
)
278 StLock
<Mutex
>_(mMutex
);
279 CSSM_CL_HANDLE clh
= clHandle();
280 CSSM_DATA_PTR fieldValue
;
281 CSSM_HANDLE resultsHandle
= 0;
282 uint32 numberOfFields
= 0;
285 result
= CSSM_CL_CertGetFirstCachedFieldValue(clh
, certHandle(), &field
, &resultsHandle
, &numberOfFields
, &fieldValue
);
288 if (result
== CSSMERR_CL_NO_FIELD_VALUES
)
291 CssmError::throwMe(result
);
294 result
= CSSM_CL_CertAbortQuery(clh
, resultsHandle
);
298 releaseFieldValue(field
, fieldValue
);
299 CssmError::throwMe(result
);
306 Certificate::releaseFieldValue(const CSSM_OID
&field
, CSSM_DATA_PTR fieldValue
)
308 StLock
<Mutex
>_(mMutex
);
311 CSSM_CL_HANDLE clh
= clHandle();
312 CSSM_CL_FreeFieldValue(clh
, &field
, fieldValue
);
319 This method computes the keyIdentifier for the public key in the cert as
322 The keyIdentifier is composed of the 160-bit SHA-1 hash of the
323 value of the BIT STRING subjectPublicKey (excluding the tag,
324 length, and number of unused bits).
327 Certificate::publicKeyHash()
329 StLock
<Mutex
>_(mMutex
);
330 if (mPublicKeyHash
.Length
)
331 return mPublicKeyHash
;
333 CSSM_DATA_PTR keyPtr
= copyFirstFieldValue(CSSMOID_CSSMKeyStruct
);
334 if (keyPtr
&& keyPtr
->Data
)
336 CssmClient::CSP
csp(gGuidAppleCSP
);
337 CssmClient::PassThrough
passThrough(csp
);
338 CSSM_KEY
*key
= reinterpret_cast<CSSM_KEY
*>(keyPtr
->Data
);
342 /* Given a CSSM_KEY_PTR in any format, obtain the SHA-1 hash of the
343 * associated key blob.
344 * Key is specified in CSSM_CSP_CreatePassThroughContext.
345 * Hash is allocated by the CSP, in the App's memory, and returned
347 passThrough
.key(key
);
348 passThrough(CSSM_APPLECSP_KEYDIGEST
, NULL
, &outData
);
349 cssmData
= reinterpret_cast<CssmData
*>(outData
);
351 assert(cssmData
->Length
<= sizeof(mPublicKeyHashBytes
));
352 mPublicKeyHash
.Data
= mPublicKeyHashBytes
;
353 mPublicKeyHash
.Length
= cssmData
->Length
;
354 memcpy(mPublicKeyHash
.Data
, cssmData
->Data
, cssmData
->Length
);
355 csp
.allocator().free(cssmData
->Data
);
356 csp
.allocator().free(cssmData
);
359 releaseFieldValue(CSSMOID_CSSMKeyStruct
, keyPtr
);
361 return mPublicKeyHash
;
365 Certificate::subjectKeyIdentifier()
367 StLock
<Mutex
>_(mMutex
);
368 if (mSubjectKeyID
.Length
)
369 return mSubjectKeyID
;
371 CSSM_DATA_PTR fieldValue
= copyFirstFieldValue(CSSMOID_SubjectKeyIdentifier
);
372 if (fieldValue
&& fieldValue
->Data
&& fieldValue
->Length
== sizeof(CSSM_X509_EXTENSION
))
374 const CSSM_X509_EXTENSION
*extension
= reinterpret_cast<const CSSM_X509_EXTENSION
*>(fieldValue
->Data
);
375 const CE_SubjectKeyID
*skid
= reinterpret_cast<CE_SubjectKeyID
*>(extension
->value
.parsedValue
); // CSSM_DATA
377 if (skid
->Length
<= sizeof(mSubjectKeyIDBytes
))
379 mSubjectKeyID
.Data
= mSubjectKeyIDBytes
;
380 mSubjectKeyID
.Length
= skid
->Length
;
381 memcpy(mSubjectKeyID
.Data
, skid
->Data
, skid
->Length
);
384 mSubjectKeyID
.Length
= 0;
387 releaseFieldValue(CSSMOID_SubjectKeyIdentifier
, fieldValue
);
389 return mSubjectKeyID
;
394 * Given an CSSM_X509_NAME, Find the first (or last) name/value pair with
395 * a printable value which matches the specified OID (e.g., CSSMOID_CommonName).
396 * Returns the CFString-style encoding associated with name component's BER tag.
397 * Returns NULL if none found.
399 static const CSSM_DATA
*
401 const CSSM_X509_NAME
&x509Name
,
402 const CSSM_OID
*tvpType
, // NULL means "any printable field"
403 bool lastInstance
, // false means return first instance
404 CFStringBuiltInEncodings
*encoding
) // RETURNED
406 const CSSM_DATA
*result
= NULL
;
407 for(uint32 rdnDex
=0; rdnDex
<x509Name
.numberOfRDNs
; rdnDex
++) {
408 const CSSM_X509_RDN
*rdnPtr
=
409 &x509Name
.RelativeDistinguishedName
[rdnDex
];
410 for(uint32 tvpDex
=0; tvpDex
<rdnPtr
->numberOfPairs
; tvpDex
++) {
411 const CSSM_X509_TYPE_VALUE_PAIR
*tvpPtr
=
412 &rdnPtr
->AttributeTypeAndValue
[tvpDex
];
414 /* type/value pair: match caller's specified type? */
415 if(tvpType
!= NULL
&& tvpType
->Data
!= NULL
) {
416 if(tvpPtr
->type
.Length
!= tvpType
->Length
) {
419 if(memcmp(tvpPtr
->type
.Data
, tvpType
->Data
, tvpType
->Length
)) {
420 /* If we don't have a match but the requested OID is CSSMOID_UserID,
421 * look for a matching X.500 UserID OID: (0.9.2342.19200300.100.1.1) */
422 const char cssm_userid_oid
[] = { 0x09,0x49,0x86,0x49,0x1f,0x12,0x8c,0xe4,0x81,0x81 };
423 const char x500_userid_oid
[] = { 0x09,0x92,0x26,0x89,0x93,0xF2,0x2C,0x64,0x01,0x01 };
424 if(!(tvpType
->Length
== sizeof(cssm_userid_oid
) &&
425 !memcmp(tvpPtr
->type
.Data
, x500_userid_oid
, sizeof(x500_userid_oid
)) &&
426 !memcmp(tvpType
->Data
, cssm_userid_oid
, sizeof(cssm_userid_oid
)))) {
433 switch(tvpPtr
->valueType
) {
434 case BER_TAG_PRINTABLE_STRING
:
435 case BER_TAG_IA5_STRING
:
436 *encoding
= kCFStringEncodingASCII
;
437 result
= &tvpPtr
->value
;
439 case BER_TAG_PKIX_UTF8_STRING
:
440 case BER_TAG_GENERAL_STRING
:
441 case BER_TAG_PKIX_UNIVERSAL_STRING
:
442 *encoding
= kCFStringEncodingUTF8
;
443 result
= &tvpPtr
->value
;
445 case BER_TAG_T61_STRING
:
446 case BER_TAG_VIDEOTEX_STRING
:
447 case BER_TAG_ISO646_STRING
:
448 *encoding
= kCFStringEncodingISOLatin1
;
449 result
= &tvpPtr
->value
;
451 case BER_TAG_PKIX_BMP_STRING
:
452 *encoding
= kCFStringEncodingUnicode
;
453 result
= &tvpPtr
->value
;
459 /* if we found a result and we want the first instance, return it now. */
460 if(result
&& !lastInstance
) {
464 } /* for each pair */
467 /* result is NULL if no printable component was found */
472 * Infer printable label for a given CSSM_X509_NAME. Returns NULL
473 * if no appropriate printable name found. Returns the CFString-style
474 * encoding associated with name component's BER tag. Also optionally
475 * returns Description component and its encoding if present and the
476 * returned name component was one we explicitly requested.
478 static const CSSM_DATA
*inferLabelFromX509Name(
479 const CSSM_X509_NAME
*x509Name
,
480 CFStringBuiltInEncodings
*encoding
, // RETURNED
481 const CSSM_DATA
**description
, // optionally RETURNED
482 CFStringBuiltInEncodings
*descrEncoding
) // RETURNED if description != NULL
484 const CSSM_DATA
*printValue
;
485 if(description
!= NULL
) {
486 *description
= findPrintableField(*x509Name
, &CSSMOID_Description
, false, descrEncoding
);
489 * Search order (take the first one found with a printable
492 * -- Organizational Unit
495 * -- field of any kind
497 printValue
= findPrintableField(*x509Name
, &CSSMOID_CommonName
, true, encoding
);
498 if(printValue
!= NULL
) {
501 printValue
= findPrintableField(*x509Name
, &CSSMOID_OrganizationalUnitName
, false, encoding
);
502 if(printValue
!= NULL
) {
505 printValue
= findPrintableField(*x509Name
, &CSSMOID_OrganizationName
, false, encoding
);
506 if(printValue
!= NULL
) {
509 printValue
= findPrintableField(*x509Name
, &CSSMOID_EmailAddress
, false, encoding
);
510 if(printValue
!= NULL
) {
513 /* if we didn't get one of the above names, don't append description */
514 if(description
!= NULL
) {
518 return findPrintableField(*x509Name
, NULL
, false, encoding
);
522 * Infer printable label for a given an CSSM_X509_NAME. Returns NULL
523 * if no appropriate printable name found.
525 const CSSM_DATA
*SecInferLabelFromX509Name(
526 const CSSM_X509_NAME
*x509Name
)
528 /* callees of this routine don't care about the encoding */
529 CFStringBuiltInEncodings encoding
= kCFStringEncodingASCII
;
530 return inferLabelFromX509Name(x509Name
, &encoding
, NULL
, &encoding
);
535 Certificate::inferLabel(bool addLabel
, CFStringRef
*rtnString
)
537 StLock
<Mutex
>_(mMutex
);
538 // Set PrintName and optionally the Alias attribute for this certificate, based on the
539 // X509 SubjectAltName and SubjectName.
540 const CSSM_DATA
*printName
= NULL
;
541 const CSSM_DATA
*description
= NULL
;
542 std::vector
<CssmData
> emailAddresses
;
544 CssmAutoData
printPlusDescr(Allocator::standard());
545 CssmData printPlusDescData
;
546 CFStringBuiltInEncodings printEncoding
= kCFStringEncodingUTF8
;
547 CFStringBuiltInEncodings descrEncoding
= kCFStringEncodingUTF8
;
549 // Find the SubjectAltName fields, if any, and extract all the GNT_RFC822Name entries from all of them
550 const CSSM_OID
&sanOid
= CSSMOID_SubjectAltName
;
551 CSSM_DATA_PTR
*sanValues
= copyFieldValues(sanOid
);
552 const CSSM_OID
&snOid
= CSSMOID_X509V1SubjectNameCStruct
;
553 CSSM_DATA_PTR snValue
= copyFirstFieldValue(snOid
);
555 getNames(sanValues
, snValue
, GNT_RFC822Name
, emailAddresses
);
557 if (snValue
&& snValue
->Data
)
559 const CSSM_X509_NAME
&x509Name
= *(const CSSM_X509_NAME
*)snValue
->Data
;
560 printName
= inferLabelFromX509Name(&x509Name
, &printEncoding
,
561 &description
, &descrEncoding
);
564 /* Don't ever use "Thawte Freemail Member" as the label for a cert. Instead force
565 a fall back on the email address. */
566 const char tfm
[] = "Thawte Freemail Member";
567 if ( (printName
->Length
== sizeof(tfm
) - 1) &&
568 !memcmp(printName
->Data
, tfm
, sizeof(tfm
) - 1)) {
574 /* Do a check to see if a '\0' was at the end of printName and strip it. */
575 CssmData cleanedUpPrintName
;
576 if((printName
!= NULL
) &&
577 (printName
->Length
!= 0) &&
578 (printEncoding
!= kCFStringEncodingISOLatin1
) &&
579 (printEncoding
!= kCFStringEncodingUnicode
) &&
580 (printName
->Data
[printName
->Length
- 1] == '\0')) {
581 cleanedUpPrintName
.Data
= printName
->Data
;
582 cleanedUpPrintName
.Length
= printName
->Length
- 1;
583 printName
= &cleanedUpPrintName
;
586 if((printName
!= NULL
) && (description
!= NULL
) && (description
->Length
!= 0))
589 * Munge Print Name (which in this case is the CommonName) and Description
590 * together with the Description in parentheses. We convert from whatever
591 * format Print Name and Description are in to UTF8 here.
593 CFRef
<CFMutableStringRef
> combo(CFStringCreateMutable(NULL
, 0));
594 CFRef
<CFStringRef
> cfPrint(CFStringCreateWithBytes(NULL
, printName
->Data
,
595 (CFIndex
)printName
->Length
, printEncoding
, true));
596 CssmData
cleanedUpDescr(description
->Data
, description
->Length
);
597 if ((cleanedUpDescr
.Data
[cleanedUpDescr
.Length
- 1] == '\0') &&
598 (descrEncoding
!= kCFStringEncodingISOLatin1
) &&
599 (descrEncoding
!= kCFStringEncodingUnicode
)) {
600 cleanedUpDescr
.Length
--;
602 CFRef
<CFStringRef
> cfDesc(CFStringCreateWithBytes(NULL
, cleanedUpDescr
.Data
,
603 (CFIndex
)cleanedUpDescr
.Length
, descrEncoding
, true));
604 CFStringAppend(combo
, cfPrint
);
605 CFStringAppendCString(combo
, " (", kCFStringEncodingASCII
);
606 CFStringAppend(combo
, cfDesc
);
607 CFStringAppendCString(combo
, ")", kCFStringEncodingASCII
);
608 CFRef
<CFDataRef
> comboData(CFStringCreateExternalRepresentation(NULL
, combo
,
609 kCFStringEncodingUTF8
, 0));
610 printPlusDescr
.copy(CFDataGetBytePtr(comboData
), CFDataGetLength(comboData
));
611 printPlusDescData
= printPlusDescr
;
612 printName
= &printPlusDescData
;
613 printEncoding
= kCFStringEncodingUTF8
;
616 if (printName
== NULL
)
618 /* If the we couldn't find a label use the emailAddress instead. */
619 if (!emailAddresses
.empty())
620 printName
= &emailAddresses
[0];
624 puntData
.Data
= (uint8
*)"X509 Certificate";
625 puntData
.Length
= 16;
626 printName
= &puntData
;
628 printEncoding
= kCFStringEncodingUTF8
;
631 /* If we couldn't find an email address just use the printName which might be the url or something else useful. */
632 if (emailAddresses
.empty())
633 emailAddresses
.push_back(CssmData::overlay(*printName
));
635 /* What do we do with the inferred label - return it or add it mDbAttributes? */
638 mDbAttributes
->add(Schema::kX509CertificatePrintName
, *printName
);
639 CssmDbAttributeData
&attrData
= mDbAttributes
->add(Schema::kX509CertificateAlias
);
641 /* Add the email addresses to attrData and normalize them. */
643 for (std::vector
<CssmData
>::const_iterator it
= emailAddresses
.begin(); it
!= emailAddresses
.end(); ++it
, ++ix
)
645 /* Add the email address using the allocator from mDbAttributes. */
646 attrData
.add(*it
, *mDbAttributes
);
647 /* Normalize the emailAddresses in place since attrData already copied it. */
648 normalizeEmailAddress(attrData
.Value
[ix
]);
654 CFStringBuiltInEncodings testEncoding
= printEncoding
;
655 if(testEncoding
== kCFStringEncodingISOLatin1
) {
657 testEncoding
= kCFStringEncodingUTF8
;
659 *rtnString
= CFStringCreateWithBytes(NULL
, printName
->Data
,
660 (CFIndex
)printName
->Length
, testEncoding
, true);
661 if(*rtnString
== NULL
&& printEncoding
== kCFStringEncodingISOLatin1
) {
662 // string cannot be represented in UTF-8, fall back to ISO Latin 1
663 *rtnString
= CFStringCreateWithBytes(NULL
, printName
->Data
,
664 (CFIndex
)printName
->Length
, printEncoding
, true);
670 releaseFieldValue(snOid
, snValue
);
672 releaseFieldValues(sanOid
, sanValues
);
676 Certificate::populateAttributes()
678 StLock
<Mutex
>_(mMutex
);
682 addParsedAttribute(Schema::attributeInfo(kSecSubjectItemAttr
), CSSMOID_X509V1SubjectName
);
683 addParsedAttribute(Schema::attributeInfo(kSecIssuerItemAttr
), CSSMOID_X509V1IssuerName
);
684 addParsedAttribute(Schema::attributeInfo(kSecSerialNumberItemAttr
), CSSMOID_X509V1SerialNumber
);
686 addSubjectKeyIdentifier();
688 if(!mHaveTypeAndEncoding
)
689 MacOSError::throwMe(errSecDataNotAvailable
); // @@@ Or some other error.
691 // Adjust mType based on the actual version of the cert.
692 CSSM_DATA_PTR versionPtr
= copyFirstFieldValue(CSSMOID_X509V1Version
);
693 if (versionPtr
&& versionPtr
->Data
&& versionPtr
->Length
== sizeof(uint32
))
695 mType
= CSSM_CERT_X_509v1
+ (*reinterpret_cast<uint32
*>(versionPtr
->Data
));
698 mType
= CSSM_CERT_X_509v1
;
700 releaseFieldValue(CSSMOID_X509V1Version
, versionPtr
);
702 mDbAttributes
->add(Schema::attributeInfo(kSecCertTypeItemAttr
), mType
);
703 mDbAttributes
->add(Schema::attributeInfo(kSecCertEncodingItemAttr
), mEncoding
);
704 mDbAttributes
->add(Schema::attributeInfo(kSecPublicKeyHashItemAttr
), publicKeyHash());
713 StLock
<Mutex
>_(mMutex
);
714 CssmDataContainer
*data
= mData
.get();
715 if (!data
&& mKeychain
)
717 // Make sure mUniqueId is set.
719 CssmDataContainer _data
;
721 /* new data allocated by CSPDL, implicitly freed by CssmDataContainer */
722 mUniqueId
->get(NULL
, &_data
);
723 /* this saves a copy to be freed at destruction and to be passed to caller */
724 setData((UInt32
)_data
.length(), _data
.data());
728 // If the data hasn't been set we can't return it.
730 MacOSError::throwMe(errSecDataNotAvailable
);
735 CFHashCode
Certificate::hash()
737 (void)data(); // ensure that mData is set up
738 return ItemImpl::hash();
744 StLock
<Mutex
>_(mMutex
);
745 if (!mHaveTypeAndEncoding
)
747 SecKeychainAttribute attr
;
748 attr
.tag
= kSecCertTypeItemAttr
;
750 attr
.length
= sizeof(mType
);
751 getAttribute(attr
, NULL
);
758 Certificate::encoding()
760 StLock
<Mutex
>_(mMutex
);
761 if (!mHaveTypeAndEncoding
)
763 SecKeychainAttribute attr
;
764 attr
.tag
= kSecCertEncodingItemAttr
;
765 attr
.data
= &mEncoding
;
766 attr
.length
= sizeof(mEncoding
);
767 getAttribute(attr
, NULL
);
773 const CSSM_X509_ALGORITHM_IDENTIFIER_PTR
774 Certificate::algorithmID()
776 StLock
<Mutex
>_(mMutex
);
777 if (!mV1SubjectPublicKeyCStructValue
)
778 mV1SubjectPublicKeyCStructValue
= copyFirstFieldValue(CSSMOID_X509V1SubjectPublicKeyCStruct
);
780 CSSM_X509_SUBJECT_PUBLIC_KEY_INFO
*info
= (CSSM_X509_SUBJECT_PUBLIC_KEY_INFO
*)mV1SubjectPublicKeyCStructValue
->Data
;
781 CSSM_X509_ALGORITHM_IDENTIFIER
*algid
= &info
->algorithm
;
786 Certificate::sha1Hash()
788 StLock
<Mutex
>_(mMutex
);
790 SecCertificateRef certRef
= handle(false);
791 CFAllocatorRef allocRef
= (certRef
) ? CFGetAllocator(certRef
) : NULL
;
792 CSSM_DATA certData
= data();
793 if (certData
.Length
== 0 || !certData
.Data
) {
794 MacOSError::throwMe(errSecDataNotAvailable
);
796 const UInt8
*dataPtr
= (const UInt8
*)certData
.Data
;
797 CFIndex dataLen
= (CFIndex
)certData
.Length
;
798 CFMutableDataRef digest
= CFDataCreateMutable(allocRef
, CC_SHA1_DIGEST_LENGTH
);
799 CFDataSetLength(digest
, CC_SHA1_DIGEST_LENGTH
);
800 CCDigest(kCCDigestSHA1
, dataPtr
, dataLen
, CFDataGetMutableBytePtr(digest
));
803 return mSha1Hash
; /* object is owned by our instance; caller should NOT release it */
807 Certificate::commonName()
809 StLock
<Mutex
>_(mMutex
);
810 return distinguishedName(&CSSMOID_X509V1SubjectNameCStruct
, &CSSMOID_CommonName
);
814 Certificate::distinguishedName(const CSSM_OID
*sourceOid
, const CSSM_OID
*componentOid
)
816 StLock
<Mutex
>_(mMutex
);
817 CFStringRef rtnString
= NULL
;
818 CSSM_DATA_PTR fieldValue
= copyFirstFieldValue(*sourceOid
);
819 CSSM_X509_NAME_PTR x509Name
= (CSSM_X509_NAME_PTR
)fieldValue
->Data
;
820 const CSSM_DATA
*printValue
= NULL
;
821 CFStringBuiltInEncodings encoding
;
823 if (fieldValue
&& fieldValue
->Data
)
824 printValue
= findPrintableField(*x509Name
, componentOid
, true, &encoding
);
827 rtnString
= CFStringCreateWithBytes(NULL
, printValue
->Data
,
828 CFIndex(printValue
->Length
), encoding
, true);
830 releaseFieldValue(*sourceOid
, fieldValue
);
837 * Return a CFString containing the first email addresses for this certificate, based on the
838 * X509 SubjectAltName and SubjectName.
841 Certificate::copyFirstEmailAddress()
843 StLock
<Mutex
>_(mMutex
);
844 CFStringRef rtnString
;
846 const CSSM_OID
&sanOid
= CSSMOID_SubjectAltName
;
847 CSSM_DATA_PTR
*sanValues
= copyFieldValues(sanOid
);
848 const CSSM_OID
&snOid
= CSSMOID_X509V1SubjectNameCStruct
;
849 CSSM_DATA_PTR snValue
= copyFirstFieldValue(snOid
);
850 std::vector
<CssmData
> emailAddresses
;
852 getNames(sanValues
, snValue
, GNT_RFC822Name
, emailAddresses
);
853 if (emailAddresses
.empty())
857 /* Encoding is kCFStringEncodingUTF8 since the string is either
858 PRINTABLE_STRING, IA5_STRING, T61_STRING or PKIX_UTF8_STRING. */
859 rtnString
= CFStringCreateWithBytes(NULL
, emailAddresses
[0].Data
,
860 (CFIndex
)emailAddresses
[0].Length
, kCFStringEncodingUTF8
, true);
865 releaseFieldValue(snOid
, snValue
);
867 releaseFieldValues(sanOid
, sanValues
);
873 * Return a CFArray containing the DNS hostnames for this certificate, based on the
874 * X509 SubjectAltName and SubjectName.
877 Certificate::copyDNSNames()
879 StLock
<Mutex
>_(mMutex
);
880 CFMutableArrayRef array
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
881 std::vector
<CssmData
> dnsNames
;
883 // Find the SubjectAltName fields, if any, and extract the GNT_DNSName entries from all of them
884 const CSSM_OID
&sanOid
= CSSMOID_SubjectAltName
;
885 CSSM_DATA_PTR
*sanValues
= copyFieldValues(sanOid
);
887 const CSSM_OID
&snOid
= CSSMOID_X509V1SubjectNameCStruct
;
888 CSSM_DATA_PTR snValue
= copyFirstFieldValue(snOid
);
890 getNames(sanValues
, snValue
, GNT_DNSName
, dnsNames
);
892 for (std::vector
<CssmData
>::const_iterator it
= dnsNames
.begin(); it
!= dnsNames
.end(); ++it
)
894 /* Encoding is kCFStringEncodingUTF8 since the string is either
895 PRINTABLE_STRING, IA5_STRING, T61_STRING or PKIX_UTF8_STRING. */
896 CFStringRef string
= CFStringCreateWithBytes(NULL
, it
->Data
, static_cast<CFIndex
>(it
->Length
), kCFStringEncodingUTF8
, true);
897 /* Be prepared for improperly formatted (non-UTF8) strings! */
898 if (!string
) continue;
899 CFArrayAppendValue(array
, string
);
905 releaseFieldValue(snOid
, snValue
);
907 releaseFieldValues(sanOid
, sanValues
);
913 * Return a CFArray containing the email addresses for this certificate, based on the
914 * X509 SubjectAltName and SubjectName.
917 Certificate::copyEmailAddresses()
919 StLock
<Mutex
>_(mMutex
);
920 CFMutableArrayRef array
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
921 std::vector
<CssmData
> emailAddresses
;
923 // Find the SubjectAltName fields, if any, and extract all the GNT_RFC822Name entries from all of them
924 const CSSM_OID
&sanOid
= CSSMOID_SubjectAltName
;
925 CSSM_DATA_PTR
*sanValues
= copyFieldValues(sanOid
);
927 const CSSM_OID
&snOid
= CSSMOID_X509V1SubjectNameCStruct
;
928 CSSM_DATA_PTR snValue
= copyFirstFieldValue(snOid
);
930 getNames(sanValues
, snValue
, GNT_RFC822Name
, emailAddresses
);
932 for (std::vector
<CssmData
>::const_iterator it
= emailAddresses
.begin(); it
!= emailAddresses
.end(); ++it
)
934 /* Encoding is kCFStringEncodingUTF8 since the string is either
935 PRINTABLE_STRING, IA5_STRING, T61_STRING or PKIX_UTF8_STRING. */
936 CFStringRef string
= CFStringCreateWithBytes(NULL
, it
->Data
, static_cast<CFIndex
>(it
->Length
), kCFStringEncodingUTF8
, true);
937 /* Be prepared for improperly formatted (non-UTF8) strings! */
938 if (!string
) continue;
939 CFArrayAppendValue(array
, string
);
945 releaseFieldValue(snOid
, snValue
);
947 releaseFieldValues(sanOid
, sanValues
);
952 const CSSM_X509_NAME_PTR
953 Certificate::subjectName()
955 StLock
<Mutex
>_(mMutex
);
956 if (!mV1SubjectNameCStructValue
)
957 if ((mV1SubjectNameCStructValue
= copyFirstFieldValue(CSSMOID_X509V1SubjectNameCStruct
)) == NULL
)
960 return (const CSSM_X509_NAME_PTR
)mV1SubjectNameCStructValue
->Data
;
963 const CSSM_X509_NAME_PTR
964 Certificate::issuerName()
966 StLock
<Mutex
>_(mMutex
);
967 if (!mV1IssuerNameCStructValue
)
968 if ((mV1IssuerNameCStructValue
= copyFirstFieldValue(CSSMOID_X509V1IssuerNameCStruct
)) == NULL
)
971 return (const CSSM_X509_NAME_PTR
)mV1IssuerNameCStructValue
->Data
;
975 Certificate::clHandle()
977 StLock
<Mutex
>_(mMutex
);
979 mCL
= clForType(type());
981 return mCL
->handle();
985 Certificate::operator < (Certificate
&other
)
987 // Certificates in different keychains are considered equal if data is equal
988 // Note that the Identity '<' operator relies on this assumption.
989 return data() < other
.data();
993 Certificate::operator == (Certificate
&other
)
995 // Certificates in different keychains are considered equal if data is equal
996 // Note that the Identity '==' operator relies on this assumption.
997 return data() == other
.data();
1001 Certificate::update()
1007 Certificate::copyTo(const Keychain
&keychain
, Access
*newAccess
)
1009 StLock
<Mutex
>_(mMutex
);
1010 /* Certs can't have access controls. */
1012 MacOSError::throwMe(errSecNoAccessForItem
);
1014 Item
item(new Certificate(data(), type(), encoding()));
1015 keychain
->add(item
);
1020 Certificate::didModify()
1025 Certificate::add(Keychain
&keychain
)
1027 StLock
<Mutex
>_(mMutex
);
1028 // If we already have a Keychain we can't be added.
1030 MacOSError::throwMe(errSecDuplicateItem
);
1032 populateAttributes();
1034 CSSM_DB_RECORDTYPE recordType
= mDbAttributes
->recordType();
1036 Db
db(keychain
->database());
1037 // add the item to the (regular) db
1040 mUniqueId
= db
->insert(recordType
, mDbAttributes
.get(), mData
.get());
1042 catch (const CssmError
&e
)
1044 if (e
.osStatus() != CSSMERR_DL_INVALID_RECORDTYPE
)
1047 // Create the cert relation and try again.
1048 db
->createRelation(CSSM_DL_DB_RECORD_X509_CERTIFICATE
,
1049 "CSSM_DL_DB_RECORD_X509_CERTIFICATE",
1050 Schema::X509CertificateSchemaAttributeCount
,
1051 Schema::X509CertificateSchemaAttributeList
,
1052 Schema::X509CertificateSchemaIndexCount
,
1053 Schema::X509CertificateSchemaIndexList
);
1054 keychain
->keychainSchema()->didCreateRelation(
1055 CSSM_DL_DB_RECORD_X509_CERTIFICATE
,
1056 "CSSM_DL_DB_RECORD_X509_CERTIFICATE",
1057 Schema::X509CertificateSchemaAttributeCount
,
1058 Schema::X509CertificateSchemaAttributeList
,
1059 Schema::X509CertificateSchemaIndexCount
,
1060 Schema::X509CertificateSchemaIndexList
);
1062 mUniqueId
= db
->insert(recordType
, mDbAttributes
.get(), mData
.get());
1065 mPrimaryKey
= keychain
->makePrimaryKey(recordType
, mUniqueId
);
1066 mKeychain
= keychain
;
1072 Certificate::publicKey()
1074 StLock
<Mutex
>_(mMutex
);
1075 SecPointer
<KeyItem
> keyItem
;
1076 // Return a CSSM_DATA_PTR with the value of the first field specified by field.
1077 // Caller must call releaseFieldValue to free the storage allocated by this call.
1078 // call OSStatus SecKeyGetCSSMKey(SecKeyRef key, const CSSM_KEY **cssmKey); to retrieve
1080 CSSM_DATA_PTR keyPtr
= copyFirstFieldValue(CSSMOID_CSSMKeyStruct
);
1081 if (keyPtr
&& keyPtr
->Data
)
1083 CssmClient::CSP
csp(gGuidAppleCSP
);
1084 CssmKey
*cssmKey
= reinterpret_cast<CssmKey
*>(keyPtr
->Data
);
1085 CssmClient::Key
key(csp
, *cssmKey
);
1086 keyItem
= new KeyItem(key
);
1087 // Clear out KeyData since KeyItem() takes over ownership of the key, and we don't want it getting released.
1088 cssmKey
->KeyData
.Data
= NULL
;
1089 cssmKey
->KeyData
.Length
= 0;
1092 releaseFieldValue(CSSMOID_CSSMKeyStruct
, keyPtr
);
1097 // This function "borrowed" from the X509 CL, which is (currently) linked into
1098 // the Security.framework as a built-in plugin.
1099 extern "C" bool getField_normRDN_NSS (
1100 const CSSM_DATA
&derName
,
1101 uint32
&numFields
, // RETURNED (if successful, 0 or 1)
1102 CssmOwnedData
&fieldValue
); // RETURNED
1105 Certificate::cursorForIssuerAndSN(const StorageManager::KeychainList
&keychains
, const CssmData
&issuer
, const CssmData
&serialNumber
)
1107 CssmAutoData
fieldValue(Allocator::standard(Allocator::normal
));
1110 // We need to decode issuer, normalize it, then re-encode it
1111 if (!getField_normRDN_NSS(issuer
, numFields
, fieldValue
))
1112 MacOSError::throwMe(errSecDataNotAvailable
);
1114 // Code basically copied from SecKeychainSearchCreateFromAttributes and SecKeychainSearchCopyNext:
1115 KCCursor
cursor(keychains
, kSecCertificateItemClass
, NULL
);
1116 cursor
->conjunctive(CSSM_DB_AND
);
1117 cursor
->add(CSSM_DB_EQUAL
, Schema::kX509CertificateIssuer
, fieldValue
.get());
1118 cursor
->add(CSSM_DB_EQUAL
, Schema::kX509CertificateSerialNumber
, serialNumber
);
1124 Certificate::cursorForIssuerAndSN_CF(const StorageManager::KeychainList
&keychains
, CFDataRef issuer
, CFDataRef serialNumber
)
1126 // This assumes a normalized issuer
1127 CSSM_DATA issuerCSSM
, serialNumberCSSM
;
1129 issuerCSSM
.Length
= CFDataGetLength(issuer
);
1130 issuerCSSM
.Data
= const_cast<uint8
*>(CFDataGetBytePtr(issuer
));
1132 serialNumberCSSM
.Length
= CFDataGetLength(serialNumber
);
1133 serialNumberCSSM
.Data
= const_cast<uint8
*>(CFDataGetBytePtr(serialNumber
));
1135 // Code basically copied from SecKeychainSearchCreateFromAttributes and SecKeychainSearchCopyNext:
1136 KCCursor
cursor(keychains
, kSecCertificateItemClass
, NULL
);
1137 cursor
->conjunctive(CSSM_DB_AND
);
1138 cursor
->add(CSSM_DB_EQUAL
, Schema::kX509CertificateIssuer
, issuerCSSM
);
1139 cursor
->add(CSSM_DB_EQUAL
, Schema::kX509CertificateSerialNumber
, serialNumberCSSM
);
1145 Certificate::cursorForSubjectKeyID(const StorageManager::KeychainList
&keychains
, const CssmData
&subjectKeyID
)
1147 KCCursor
cursor(keychains
, kSecCertificateItemClass
, NULL
);
1148 cursor
->conjunctive(CSSM_DB_AND
);
1149 cursor
->add(CSSM_DB_EQUAL
, Schema::kX509CertificateSubjectKeyIdentifier
, subjectKeyID
);
1155 Certificate::cursorForEmail(const StorageManager::KeychainList
&keychains
, const char *emailAddress
)
1157 KCCursor
cursor(keychains
, kSecCertificateItemClass
, NULL
);
1160 cursor
->conjunctive(CSSM_DB_AND
);
1161 CssmSelectionPredicate
&pred
= cursor
->add(CSSM_DB_EQUAL
, Schema::kX509CertificateAlias
, emailAddress
);
1162 /* Normalize the emailAddresses in place since cursor already copied it. */
1163 normalizeEmailAddress(pred
.Attribute
.Value
[0]);
1169 SecPointer
<Certificate
>
1170 Certificate::findInKeychain(const StorageManager::KeychainList
&keychains
)
1172 StLock
<Mutex
>_(mMutex
);
1173 const CSSM_OID
&issuerOid
= CSSMOID_X509V1IssuerName
;
1174 CSSM_DATA_PTR issuerPtr
= copyFirstFieldValue(issuerOid
);
1175 CssmData
issuer(issuerPtr
->Data
, issuerPtr
->Length
);
1177 const CSSM_OID
&serialOid
= CSSMOID_X509V1SerialNumber
;
1178 CSSM_DATA_PTR serialPtr
= copyFirstFieldValue(serialOid
);
1179 CssmData
serial(serialPtr
->Data
, serialPtr
->Length
);
1181 SecPointer
<Certificate
> foundCert
= NULL
;
1183 foundCert
= findByIssuerAndSN(keychains
, issuer
, serial
);
1188 releaseFieldValue(issuerOid
, issuerPtr
);
1189 releaseFieldValue(serialOid
, serialPtr
);
1194 SecPointer
<Certificate
>
1195 Certificate::findByIssuerAndSN(const StorageManager::KeychainList
&keychains
, const CssmData
&issuer
, const CssmData
&serialNumber
)
1198 if (!cursorForIssuerAndSN(keychains
, issuer
, serialNumber
)->next(item
))
1199 CssmError::throwMe(errSecItemNotFound
);
1201 return static_cast<Certificate
*>(&*item
);
1204 SecPointer
<Certificate
>
1205 Certificate::findBySubjectKeyID(const StorageManager::KeychainList
&keychains
, const CssmData
&subjectKeyID
)
1208 if (!cursorForSubjectKeyID(keychains
, subjectKeyID
)->next(item
))
1209 CssmError::throwMe(errSecItemNotFound
);
1211 return static_cast<Certificate
*>(&*item
);
1214 SecPointer
<Certificate
>
1215 Certificate::findByEmail(const StorageManager::KeychainList
&keychains
, const char *emailAddress
)
1218 if (!cursorForEmail(keychains
, emailAddress
)->next(item
))
1219 CssmError::throwMe(errSecItemNotFound
);
1221 return static_cast<Certificate
*>(&*item
);
1224 /* Normalize emailAddresses in place. */
1226 Certificate::normalizeEmailAddress(CSSM_DATA
&emailAddress
)
1228 /* Do a check to see if a '\0' was at the end of emailAddress and strip it. */
1229 if (emailAddress
.Length
&& emailAddress
.Data
[emailAddress
.Length
- 1] == '\0')
1230 emailAddress
.Length
--;
1231 bool foundAt
= false;
1232 for (uint32 ix
= 0; ix
< emailAddress
.Length
; ++ix
)
1234 uint8 ch
= emailAddress
.Data
[ix
];
1237 if ('A' <= ch
&& ch
<= 'Z')
1238 emailAddress
.Data
[ix
] = ch
+ 'a' - 'A';
1246 Certificate::getNames(CSSM_DATA_PTR
*sanValues
, CSSM_DATA_PTR snValue
, CE_GeneralNameType generalNameType
, std::vector
<CssmData
> &names
)
1248 // Get the DNS host names or RFC822 email addresses for this certificate (depending on generalNameType),
1249 // within the X509 SubjectAltName and SubjectName.
1251 // Find the SubjectAltName fields, if any, and extract the nameType entries from all of them
1254 for (CSSM_DATA_PTR
*sanIx
= sanValues
; *sanIx
; ++sanIx
)
1256 CSSM_DATA_PTR sanValue
= *sanIx
;
1257 if (sanValue
&& sanValue
->Data
)
1259 CSSM_X509_EXTENSION
*cssmExt
= (CSSM_X509_EXTENSION
*)sanValue
->Data
;
1260 CE_GeneralNames
*parsedValue
= (CE_GeneralNames
*)cssmExt
->value
.parsedValue
;
1262 /* Grab all the values that are of the specified name type. */
1263 for (uint32 i
= 0; i
< parsedValue
->numNames
; ++i
)
1265 if (parsedValue
->generalName
[i
].nameType
== generalNameType
)
1267 if (parsedValue
->generalName
[i
].berEncoded
) // can't handle this
1270 names
.push_back(CssmData::overlay(parsedValue
->generalName
[i
].name
));
1277 if (names
.empty() && snValue
&& snValue
->Data
)
1279 const CSSM_X509_NAME
&x509Name
= *(const CSSM_X509_NAME
*)snValue
->Data
;
1280 for (uint32 rdnDex
= 0; rdnDex
< x509Name
.numberOfRDNs
; rdnDex
++)
1282 const CSSM_X509_RDN
*rdnPtr
=
1283 &x509Name
.RelativeDistinguishedName
[rdnDex
];
1284 for (uint32 tvpDex
= 0; tvpDex
< rdnPtr
->numberOfPairs
; tvpDex
++)
1286 const CSSM_X509_TYPE_VALUE_PAIR
*tvpPtr
=
1287 &rdnPtr
->AttributeTypeAndValue
[tvpDex
];
1289 /* type/value pair: match caller's specified type */
1290 if (GNT_RFC822Name
== generalNameType
) {
1291 if (((tvpPtr
->type
.Length
!= CSSMOID_EmailAddress
.Length
) ||
1292 memcmp(tvpPtr
->type
.Data
, CSSMOID_EmailAddress
.Data
, CSSMOID_EmailAddress
.Length
))) {
1296 if (GNT_DNSName
== generalNameType
) {
1297 if (((tvpPtr
->type
.Length
!= CSSMOID_CommonName
.Length
) ||
1298 memcmp(tvpPtr
->type
.Data
, CSSMOID_CommonName
.Data
, CSSMOID_CommonName
.Length
))) {
1304 switch (tvpPtr
->valueType
)
1306 case BER_TAG_PRINTABLE_STRING
:
1307 case BER_TAG_IA5_STRING
:
1308 case BER_TAG_T61_STRING
:
1309 case BER_TAG_PKIX_UTF8_STRING
:
1311 names
.push_back(CssmData::overlay(tvpPtr
->value
));
1316 } /* for each pair */
1317 } /* for each RDN */
1321 void Certificate::willRead()
1323 populateAttributes();
1326 Boolean
Certificate::isSelfSigned()
1328 StLock
<Mutex
>_(mMutex
);
1329 CSSM_DATA_PTR issuer
= NULL
;
1330 CSSM_DATA_PTR subject
= NULL
;
1331 OSStatus ortn
= errSecSuccess
;
1332 Boolean brtn
= false;
1334 issuer
= copyFirstFieldValue(CSSMOID_X509V1IssuerNameStd
);
1335 subject
= copyFirstFieldValue(CSSMOID_X509V1SubjectNameStd
);
1336 if((issuer
== NULL
) || (subject
== NULL
)) {
1339 else if((issuer
->Length
== subject
->Length
) &&
1340 !memcmp(issuer
->Data
, subject
->Data
, issuer
->Length
)) {
1344 /* names match: verify signature */
1346 CSSM_DATA certData
= data();
1347 crtn
= CSSM_CL_CertVerify(clHandle(), 0,
1348 &certData
, &certData
, NULL
, 0);
1354 releaseFieldValue(CSSMOID_X509V1IssuerNameStd
, issuer
);
1357 releaseFieldValue(CSSMOID_X509V1SubjectNameStd
, subject
);
1360 MacOSError::throwMe(ortn
);