2 * Copyright (c) 2002,2011,2014 Apple Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 * tpCredRequest.cpp - credential request functions SubmitCredRequest,
25 #include "AppleTPSession.h"
26 #include "certGroupUtils.h"
27 #include "tpdebugging.h"
29 #include <Security/oidsalg.h>
30 #include <Security/oidsattr.h>
31 #include <Security/oidscert.h>
32 #include <Security/cssmapple.h>
33 #include <security_utilities/debugging.h>
34 #include <Security/cssmapple.h>
37 #define tpCredDebug(args...) secdebug("tpCred", ## args)
40 * Build up a CSSM_X509_NAME from an arbitrary list of name/OID pairs.
41 * We do one a/v pair per RDN.
43 CSSM_X509_NAME
* AppleTPSession::buildX509Name(
44 const CSSM_APPLE_TP_NAME_OID
*nameArray
,
47 CSSM_X509_NAME
*top
= (CSSM_X509_NAME
*)malloc(sizeof(CSSM_X509_NAME
));
48 top
->numberOfRDNs
= numNames
;
51 top
->RelativeDistinguishedName
= NULL
;
54 top
->RelativeDistinguishedName
=
55 (CSSM_X509_RDN_PTR
)malloc(sizeof(CSSM_X509_RDN
) * numNames
);
56 CSSM_X509_RDN_PTR rdn
;
57 const CSSM_APPLE_TP_NAME_OID
*nameOid
;
59 for(nameDex
=0; nameDex
<numNames
; nameDex
++) {
60 rdn
= &top
->RelativeDistinguishedName
[nameDex
];
61 nameOid
= &nameArray
[nameDex
];
62 rdn
->numberOfPairs
= 1;
63 rdn
->AttributeTypeAndValue
= (CSSM_X509_TYPE_VALUE_PAIR_PTR
)
64 malloc(sizeof(CSSM_X509_TYPE_VALUE_PAIR
));
65 CSSM_X509_TYPE_VALUE_PAIR_PTR atvp
= rdn
->AttributeTypeAndValue
;
66 tpCopyCssmData(*this, nameOid
->oid
, &atvp
->type
);
67 atvp
->value
.Length
= strlen(nameOid
->string
);
68 if(tpCompareOids(&CSSMOID_CountryName
, nameOid
->oid
)) {
70 * Country handled differently per RFC 3280 - must be printable,
71 * max of two characters in length
73 if(atvp
->value
.Length
> 2) {
74 CssmError::throwMe(CSSMERR_TP_INVALID_DATA
);
76 for(unsigned dex
=0; dex
<atvp
->value
.Length
; dex
++) {
77 int c
= nameOid
->string
[dex
];
78 if(!isprint(c
) || (c
== EOF
)) {
79 CssmError::throwMe(CSSMERR_TP_INVALID_DATA
);
82 atvp
->valueType
= BER_TAG_PRINTABLE_STRING
;
84 /* other special cases per RFC 3280 */
85 else if(tpCompareOids(&CSSMOID_DNQualifier
, nameOid
->oid
)) {
86 atvp
->valueType
= BER_TAG_PRINTABLE_STRING
;
88 else if(tpCompareOids(&CSSMOID_SerialNumber
, nameOid
->oid
)) {
89 atvp
->valueType
= BER_TAG_PRINTABLE_STRING
;
91 else if(tpCompareOids(&CSSMOID_EmailAddress
, nameOid
->oid
)) {
92 atvp
->valueType
= BER_TAG_IA5_STRING
;
96 atvp
->valueType
= BER_TAG_PKIX_UTF8_STRING
;
98 atvp
->value
.Data
= (uint8
*)malloc(atvp
->value
.Length
);
99 memmove(atvp
->value
.Data
, nameOid
->string
, atvp
->value
.Length
);
104 /* free the CSSM_X509_NAME obtained from buildX509Name */
105 void AppleTPSession::freeX509Name(
112 CSSM_X509_RDN_PTR rdn
;
113 for(nameDex
=0; nameDex
<top
->numberOfRDNs
; nameDex
++) {
114 rdn
= &top
->RelativeDistinguishedName
[nameDex
];
115 if(rdn
->AttributeTypeAndValue
) {
116 for(unsigned aDex
=0; aDex
<rdn
->numberOfPairs
; aDex
++) {
117 CSSM_X509_TYPE_VALUE_PAIR_PTR atvp
=
118 &rdn
->AttributeTypeAndValue
[aDex
];
119 free(atvp
->type
.Data
);
120 free(atvp
->value
.Data
);
122 free(rdn
->AttributeTypeAndValue
);
125 free(top
->RelativeDistinguishedName
);
129 /* Obtain a CSSM_X509_TIME representing "now" plus specified seconds */
132 * Although RFC 2459, *the* spec for X509 certs, allows for not before/after
133 * times to be expressed in ther generalized (4-digit year) or UTC (2-digit year
134 * with implied century rollover), IE 5 on Mac will not accept the generalized
137 #define TP_FOUR_DIGIT_YEAR 0
138 #if TP_FOUR_DIGIT_YEAR
139 #define TP_TIME_FORMAT TIME_GEN
140 #define TP_TIME_TAG BER_TAG_GENERALIZED_TIME
142 #define TP_TIME_FORMAT TIME_UTC
143 #define TP_TIME_TAG BER_TAG_UTC_TIME
144 #endif /* TP_FOUR_DIGIT_YEAR */
146 CSSM_X509_TIME
* AppleTPSession::buildX509Time(
147 unsigned secondsFromNow
)
149 CSSM_X509_TIME
*xtime
= (CSSM_X509_TIME
*)malloc(sizeof(CSSM_X509_TIME
));
150 xtime
->timeType
= TP_TIME_TAG
;
151 char *ts
= (char *)malloc(GENERALIZED_TIME_STRLEN
+ 1);
153 StLock
<Mutex
> _(tpTimeLock());
154 timeAtNowPlus(secondsFromNow
, TP_TIME_FORMAT
, ts
);
156 xtime
->time
.Data
= (uint8
*)ts
;
157 xtime
->time
.Length
= strlen(ts
);
161 /* Free CSSM_X509_TIME obtained in buildX509Time */
162 void AppleTPSession::freeX509Time(
163 CSSM_X509_TIME
*xtime
)
168 free((char *)xtime
->time
.Data
);
173 * Cook up a CSSM_DATA with specified integer, DER style (minimum number of
174 * bytes, big-endian).
176 static void intToDER(
182 * Calculate length in bytes of encoded integer, minimum length of 1.
185 uintptr_t unsignedInt
= (uintptr_t)theInt
;
186 while(unsignedInt
> 0xff) {
192 * DER encoding requires top bit to be zero, else it's a negative number.
193 * Even though we're passing around integers as CSSM_INTPTR, they really are
195 * unsignedInt contains the m.s. byte of theInt in its l.s. byte.
197 if(unsignedInt
& 0x80) {
201 DER_Data
.Data
= (uint8
*)alloc
.malloc(DER_Data
.Length
);
202 uint8
*dst
= DER_Data
.Data
+ DER_Data
.Length
- 1;
203 unsignedInt
= (uintptr_t)theInt
;
204 for(unsigned dex
=0; dex
<DER_Data
.Length
; dex
++) {
205 *dst
-- = unsignedInt
& 0xff;
206 /* this shifts off to zero if we're adding a zero at the top */
211 /* The reverse of the above. */
212 static CSSM_INTPTR
DERToInt(
213 const CSSM_DATA
&DER_Data
)
216 uint8
*bp
= DER_Data
.Data
;
217 for(unsigned dex
=0; dex
<DER_Data
.Length
; dex
++) {
224 /* Convert a reference key to a raw key. */
225 void AppleTPSession::refKeyToRaw(
226 CSSM_CSP_HANDLE cspHand
,
227 const CSSM_KEY
*refKey
,
228 CSSM_KEY_PTR rawKey
) // RETURNED
230 CSSM_CC_HANDLE ccHand
;
232 CSSM_ACCESS_CREDENTIALS creds
;
234 memset(rawKey
, 0, sizeof(CSSM_KEY
));
235 memset(&creds
, 0, sizeof(CSSM_ACCESS_CREDENTIALS
));
236 crtn
= CSSM_CSP_CreateSymmetricContext(cspHand
,
239 &creds
, // passPhrase
240 NULL
, // wrapping key
242 CSSM_PADDING_NONE
, // Padding
246 tpCredDebug("AppleTPSession::refKeyToRaw: context err");
247 CssmError::throwMe(crtn
);
250 crtn
= CSSM_WrapKey(ccHand
,
253 NULL
, // DescriptiveData
255 if(crtn
!= CSSM_OK
) {
256 tpCredDebug("AppleTPSession::refKeyToRaw: wrapKey err");
257 CssmError::throwMe(crtn
);
259 CSSM_DeleteContext(ccHand
);
264 * Cook up an unsigned cert.
265 * This is just a wrapper for CSSM_CL_CertCreateTemplate().
267 void AppleTPSession::makeCertTemplate(
269 CSSM_CL_HANDLE clHand
,
270 CSSM_CSP_HANDLE cspHand
, // for converting ref to raw key
272 const CSSM_X509_NAME
*issuerName
,
273 const CSSM_X509_NAME
*subjectName
,
274 const CSSM_X509_TIME
*notBefore
,
275 const CSSM_X509_TIME
*notAfter
,
276 const CSSM_KEY
*subjectPubKey
,
277 const CSSM_OID
&sigOid
, // e.g., CSSMOID_SHA1WithRSA
279 const CSSM_DATA
*subjectUniqueId
,
280 const CSSM_DATA
*issuerUniqueId
,
281 CSSM_X509_EXTENSION
*extensions
,
282 unsigned numExtensions
,
283 CSSM_DATA_PTR
&rawCert
)
285 CSSM_FIELD
*certTemp
;
286 unsigned fieldDex
= 0; // index into certTemp
287 CSSM_DATA serialDER
= {0, NULL
}; // serial number, DER format
288 CSSM_DATA versionDER
= {0, NULL
};
290 CSSM_X509_ALGORITHM_IDENTIFIER algId
;
291 const CSSM_KEY
*actPubKey
;
293 CSSM_BOOL freeRawKey
= CSSM_FALSE
;
296 algId
.algorithm
= sigOid
;
297 algId
.parameters
.Data
= NULL
;
298 algId
.parameters
.Length
= 0;
301 * Convert possible ref public key to raw format as required by CL.
303 switch(subjectPubKey
->KeyHeader
.BlobType
) {
304 case CSSM_KEYBLOB_RAW
:
305 actPubKey
= subjectPubKey
;
307 case CSSM_KEYBLOB_REFERENCE
:
308 refKeyToRaw(cspHand
, subjectPubKey
, &rawPubKey
);
309 actPubKey
= &rawPubKey
;
310 freeRawKey
= CSSM_TRUE
;
313 tpCredDebug("CSSM_CL_CertCreateTemplate: bad key blob type (%u)",
314 (unsigned)subjectPubKey
->KeyHeader
.BlobType
);
315 CssmError::throwMe(CSSMERR_TP_INVALID_REQUEST_INPUTS
);
320 * version, always 2 (X509v3)
321 * serialNumber thru subjectPubKey
323 unsigned numFields
= 8 + numExtensions
;
324 if(subjectUniqueId
) {
331 certTemp
= (CSSM_FIELD
*)malloc(sizeof(CSSM_FIELD
) * numFields
);
335 intToDER(2, versionDER
, *this);
336 certTemp
[fieldDex
].FieldOid
= CSSMOID_X509V1Version
;
337 certTemp
[fieldDex
++].FieldValue
= versionDER
;
340 intToDER(serialNumber
, serialDER
, *this);
341 certTemp
[fieldDex
].FieldOid
= CSSMOID_X509V1SerialNumber
;
342 certTemp
[fieldDex
++].FieldValue
= serialDER
;
344 /* subject and issuer name */
345 certTemp
[fieldDex
].FieldOid
= CSSMOID_X509V1IssuerNameCStruct
;
346 certTemp
[fieldDex
].FieldValue
.Data
= (uint8
*)issuerName
;
347 certTemp
[fieldDex
++].FieldValue
.Length
= sizeof(CSSM_X509_NAME
);
349 certTemp
[fieldDex
].FieldOid
= CSSMOID_X509V1SubjectNameCStruct
;
350 certTemp
[fieldDex
].FieldValue
.Data
= (uint8
*)subjectName
;
351 certTemp
[fieldDex
++].FieldValue
.Length
= sizeof(CSSM_X509_NAME
);
353 /* not before/after */
354 certTemp
[fieldDex
].FieldOid
= CSSMOID_X509V1ValidityNotBefore
;
355 certTemp
[fieldDex
].FieldValue
.Data
= (uint8
*)notBefore
;
356 certTemp
[fieldDex
++].FieldValue
.Length
= sizeof(CSSM_X509_TIME
);
358 certTemp
[fieldDex
].FieldOid
= CSSMOID_X509V1ValidityNotAfter
;
359 certTemp
[fieldDex
].FieldValue
.Data
= (uint8
*)notAfter
;
360 certTemp
[fieldDex
++].FieldValue
.Length
= sizeof(CSSM_X509_TIME
);
362 /* the subject key */
363 certTemp
[fieldDex
].FieldOid
= CSSMOID_CSSMKeyStruct
;
364 certTemp
[fieldDex
].FieldValue
.Data
= (uint8
*)actPubKey
;
365 certTemp
[fieldDex
++].FieldValue
.Length
= sizeof(CSSM_KEY
);
367 /* signature algorithm */
368 certTemp
[fieldDex
].FieldOid
= CSSMOID_X509V1SignatureAlgorithmTBS
;
369 certTemp
[fieldDex
].FieldValue
.Data
= (uint8
*)&algId
;
370 certTemp
[fieldDex
++].FieldValue
.Length
= sizeof(CSSM_X509_ALGORITHM_IDENTIFIER
);
372 /* subject/issuer unique IDs */
373 if(subjectUniqueId
!= 0) {
374 certTemp
[fieldDex
].FieldOid
= CSSMOID_X509V1CertificateSubjectUniqueId
;
375 certTemp
[fieldDex
++].FieldValue
= *subjectUniqueId
;
377 if(issuerUniqueId
!= 0) {
378 certTemp
[fieldDex
].FieldOid
= CSSMOID_X509V1CertificateIssuerUniqueId
;
379 certTemp
[fieldDex
++].FieldValue
= *issuerUniqueId
;
382 for(extNum
=0; extNum
<numExtensions
; extNum
++) {
383 CSSM_X509_EXTENSION_PTR ext
= &extensions
[extNum
];
384 if(ext
->format
== CSSM_X509_DATAFORMAT_PARSED
) {
385 certTemp
[fieldDex
].FieldOid
= ext
->extnId
;
388 certTemp
[fieldDex
].FieldOid
= CSSMOID_X509V3CertificateExtensionCStruct
;
390 certTemp
[fieldDex
].FieldValue
.Data
= (uint8
*)ext
;
391 certTemp
[fieldDex
++].FieldValue
.Length
= sizeof(CSSM_X509_EXTENSION
);
393 assert(fieldDex
== numFields
);
398 rawCert
= (CSSM_DATA_PTR
)malloc(sizeof(CSSM_DATA
));
399 rawCert
->Data
= NULL
;
401 CSSM_RETURN crtn
= CSSM_CL_CertCreateTemplate(clHand
,
406 tpCredDebug("CSSM_CL_CertCreateTemplate returned %ld", (long)crtn
);
412 /* free the stuff we mallocd to get here */
413 free(serialDER
.Data
);
414 free(versionDER
.Data
);
417 tpFreeCssmData(*this, &rawPubKey
.KeyData
, CSSM_FALSE
);
420 CssmError::throwMe(crtn
);
424 /* given a cert and a ReferenceIdentifier, fill in ReferenceIdentifier and
425 * add it and the cert to tpCredMap. */
426 void AppleTPSession::addCertToMap(
427 const CSSM_DATA
*cert
,
430 StLock
<Mutex
> _(tpCredMapLock
);
432 TpCredHandle hand
= reinterpret_cast<TpCredHandle
>(cert
);
433 intToDER(hand
, *refId
, *this);
434 tpCredMap
[hand
] = cert
;
437 /* given a ReferenceIdentifier, obtain associated cert and remove from the map */
438 CSSM_DATA_PTR
AppleTPSession::getCertFromMap(
439 const CSSM_DATA
*refId
)
441 StLock
<Mutex
> _(tpCredMapLock
);
442 CSSM_DATA_PTR rtn
= NULL
;
444 if((refId
== NULL
) || (refId
->Data
== NULL
)) {
447 TpCredHandle hand
= DERToInt(*refId
);
448 credMap::iterator it
= tpCredMap
.find(hand
);
449 if(it
== tpCredMap
.end()) {
452 rtn
= const_cast<CSSM_DATA
*>(it
->second
);
453 tpCredMap
.erase(hand
);
458 * SubmitCredRequest, CSR form.
460 void AppleTPSession::SubmitCsrRequest(
461 const CSSM_TP_REQUEST_SET
&RequestInput
,
462 const CSSM_TP_CALLERAUTH_CONTEXT
*CallerAuthContext
,
463 sint32
&EstimatedTime
, // RETURNED
464 CssmData
&ReferenceIdentifier
) // RETURNED
466 CSSM_DATA_PTR csrPtr
= NULL
;
467 CSSM_CC_HANDLE sigHand
= 0;
468 CSSM_APPLE_CL_CSR_REQUEST csrReq
;
470 memset(&csrReq
, 0, sizeof(csrReq
));
472 /* for now we're using the same struct for input as the the normal
473 * X509 cert request. */
474 CSSM_APPLE_TP_CERT_REQUEST
*certReq
=
475 (CSSM_APPLE_TP_CERT_REQUEST
*)RequestInput
.Requests
;
476 if((certReq
->cspHand
== 0) ||
477 (certReq
->clHand
== 0) ||
478 (certReq
->certPublicKey
== NULL
) ||
479 (certReq
->issuerPrivateKey
== NULL
) ||
480 (certReq
->signatureOid
.Data
== NULL
)) {
481 CssmError::throwMe(CSSMERR_TP_INVALID_REQUEST_INPUTS
);
484 /* convert ref public key to raw per CL requirements */
485 const CSSM_KEY
*subjectPubKey
= certReq
->certPublicKey
;
486 const CSSM_KEY
*actPubKey
= NULL
;
487 CSSM_BOOL freeRawKey
= CSSM_FALSE
;
490 switch(subjectPubKey
->KeyHeader
.BlobType
) {
491 case CSSM_KEYBLOB_RAW
:
492 actPubKey
= subjectPubKey
;
494 case CSSM_KEYBLOB_REFERENCE
:
495 refKeyToRaw(certReq
->cspHand
, subjectPubKey
, &rawPubKey
);
496 actPubKey
= &rawPubKey
;
497 freeRawKey
= CSSM_TRUE
;
500 tpCredDebug("SubmitCsrRequest: bad key blob type (%u)",
501 (unsigned)subjectPubKey
->KeyHeader
.BlobType
);
502 CssmError::throwMe(CSSMERR_TP_INVALID_REQUEST_INPUTS
);
505 /* cook up a CL-passthrough-specific request */
506 csrReq
.subjectNameX509
= buildX509Name(certReq
->subjectNames
,
507 certReq
->numSubjectNames
);
508 csrReq
.signatureAlg
= certReq
->signatureAlg
;
509 csrReq
.signatureOid
= certReq
->signatureOid
;
510 csrReq
.cspHand
= certReq
->cspHand
;
511 csrReq
.subjectPublicKey
= actPubKey
;
512 csrReq
.subjectPrivateKey
= certReq
->issuerPrivateKey
;
513 csrReq
.challengeString
= certReq
->challengeString
;
515 /* A crypto handle to pass to the CL */
517 crtn
= CSSM_CSP_CreateSignatureContext(certReq
->cspHand
,
518 certReq
->signatureAlg
,
519 (CallerAuthContext
? CallerAuthContext
->CallerCredentials
: NULL
),
520 certReq
->issuerPrivateKey
,
523 tpCredDebug("CSSM_CSP_CreateSignatureContext returned %ld", (long)crtn
);
527 /* down to the CL to do the actual work */
528 crtn
= CSSM_CL_PassThrough(certReq
->clHand
,
530 CSSM_APPLEX509CL_OBTAIN_CSR
,
534 tpCredDebug("CSSM_CL_PassThrough returned %ld", (long)crtn
);
538 /* save it for retrieval by RetrieveCredResult */
539 addCertToMap(csrPtr
, &ReferenceIdentifier
);
543 /* free local resources */
544 if(csrReq
.subjectNameX509
) {
545 freeX509Name(csrReq
.subjectNameX509
);
548 CSSM_DeleteContext(sigHand
);
551 tpFreeCssmData(*this, &rawPubKey
.KeyData
, CSSM_FALSE
);
554 CssmError::throwMe(crtn
);
559 * Submit cred (cert) request. Currently the only form of request we
560 * handle is the basis "sign this cert with key right now", with policy OI
561 * CSSMOID_APPLE_TP_LOCAL_CERT_GEN.
563 void AppleTPSession::SubmitCredRequest(
564 const CSSM_TP_AUTHORITY_ID
*PreferredAuthority
,
565 CSSM_TP_AUTHORITY_REQUEST_TYPE RequestType
,
566 const CSSM_TP_REQUEST_SET
&RequestInput
,
567 const CSSM_TP_CALLERAUTH_CONTEXT
*CallerAuthContext
,
568 sint32
&EstimatedTime
,
569 CssmData
&ReferenceIdentifier
)
571 /* free all of these on return if non-NULL */
572 CSSM_DATA_PTR certTemplate
= NULL
;
573 CSSM_X509_TIME_PTR notBeforeX509
= NULL
;
574 CSSM_X509_TIME_PTR notAfterX509
= NULL
;
575 CSSM_X509_NAME_PTR subjectX509
= NULL
;
576 CSSM_X509_NAME_PTR issuerX509
= NULL
;
577 CSSM_X509_EXTENSION_PTR extens509
= NULL
;
578 CSSM_CC_HANDLE sigContext
= 0;
580 /* this gets saved on success */
581 CSSM_DATA_PTR signedCert
= NULL
;
583 /* validate rather limited set of input args */
584 if(PreferredAuthority
!= NULL
) {
585 CssmError::throwMe(CSSMERR_TP_INVALID_AUTHORITY
);
587 if(RequestType
!= CSSM_TP_AUTHORITY_REQUEST_CERTISSUE
) {
588 CssmError::throwMe(CSSMERR_TP_UNSUPPORTED_SERVICE
);
590 if(CallerAuthContext
== NULL
) {
591 CssmError::throwMe(CSSMERR_TP_INVALID_CALLERAUTH_CONTEXT_POINTER
);
593 if((RequestInput
.NumberOfRequests
!= 1) ||
594 (RequestInput
.Requests
== NULL
)) {
595 CssmError::throwMe(CSSMERR_TP_INVALID_REQUEST_INPUTS
);
598 /* Apple-specific args */
599 const CSSM_TP_POLICYINFO
*tpPolicy
= &CallerAuthContext
->Policy
;
600 if((tpPolicy
->NumberOfPolicyIds
!= 1) ||
601 (tpPolicy
->PolicyIds
== NULL
)) {
602 CssmError::throwMe(CSSMERR_TP_INVALID_CALLERAUTH_CONTEXT_POINTER
);
604 if(tpCompareCssmData(&tpPolicy
->PolicyIds
->FieldOid
,
605 &CSSMOID_APPLE_TP_CSR_GEN
)) {
606 /* break out to CSR-specific code */
607 SubmitCsrRequest(RequestInput
, CallerAuthContext
, EstimatedTime
, ReferenceIdentifier
);
610 else if(!tpCompareCssmData(&tpPolicy
->PolicyIds
->FieldOid
,
611 &CSSMOID_APPLE_TP_LOCAL_CERT_GEN
)) {
612 CssmError::throwMe(CSSMERR_TP_INVALID_POLICY_IDENTIFIERS
);
615 CSSM_APPLE_TP_CERT_REQUEST
*certReq
=
616 (CSSM_APPLE_TP_CERT_REQUEST
*)RequestInput
.Requests
;
617 if((certReq
->cspHand
== 0) ||
618 (certReq
->clHand
== 0) ||
619 (certReq
->certPublicKey
== NULL
) ||
620 (certReq
->issuerPrivateKey
== NULL
)) {
621 CssmError::throwMe(CSSMERR_TP_INVALID_REQUEST_INPUTS
);
623 if((certReq
->numExtensions
!= 0) & (certReq
->extensions
== NULL
)) {
624 CssmError::throwMe(CSSMERR_TP_INVALID_POINTER
);
627 CSSM_RETURN ourRtn
= CSSM_OK
;
630 /* convert caller's friendly names and times to CDSA style */
631 subjectX509
= buildX509Name(certReq
->subjectNames
, certReq
->numSubjectNames
);
632 if(certReq
->issuerNames
!= NULL
) {
633 issuerX509
= buildX509Name(certReq
->issuerNames
, certReq
->numIssuerNames
);
635 else if(certReq
->issuerNameX509
) {
636 /* caller obtained this from an existing signer's cert */
637 issuerX509
= certReq
->issuerNameX509
;
641 issuerX509
= subjectX509
;
643 notBeforeX509
= buildX509Time(certReq
->notBefore
);
644 notAfterX509
= buildX509Time(certReq
->notAfter
);
646 if(certReq
->numExtensions
!= 0) {
647 /* convert extensions array from CE_DataAndType to CSSM_X509_EXTENSION */
648 extens509
= (CSSM_X509_EXTENSION
*)malloc(sizeof(CSSM_X509_EXTENSION
) *
649 certReq
->numExtensions
);
650 memset(extens509
, 0, sizeof(CSSM_X509_EXTENSION
) *
651 certReq
->numExtensions
);
652 for(unsigned dex
=0; dex
<certReq
->numExtensions
; dex
++) {
653 CSSM_X509_EXTENSION
*extn
= &extens509
[dex
];
654 CE_DataAndType
*cdt
= &certReq
->extensions
[dex
];
659 case DT_AuthorityKeyID
:
660 parsedValue
= &cdt
->extension
.authorityKeyID
;
661 extnId
= CSSMOID_AuthorityKeyIdentifier
;
663 case DT_SubjectKeyID
:
664 parsedValue
= &cdt
->extension
.subjectKeyID
;
665 extnId
= CSSMOID_SubjectKeyIdentifier
;
668 parsedValue
= &cdt
->extension
.keyUsage
;
669 extnId
= CSSMOID_KeyUsage
;
671 case DT_SubjectAltName
:
672 parsedValue
= &cdt
->extension
.subjectAltName
;
673 extnId
= CSSMOID_SubjectAltName
;
675 case DT_IssuerAltName
:
676 parsedValue
= &cdt
->extension
.issuerAltName
;
677 extnId
= CSSMOID_IssuerAltName
;
679 case DT_ExtendedKeyUsage
:
680 parsedValue
= &cdt
->extension
.extendedKeyUsage
;
681 extnId
= CSSMOID_ExtendedKeyUsage
;
683 case DT_BasicConstraints
:
684 parsedValue
= &cdt
->extension
.basicConstraints
;
685 extnId
= CSSMOID_BasicConstraints
;
687 case DT_CertPolicies
:
688 parsedValue
= &cdt
->extension
.certPolicies
;
689 extnId
= CSSMOID_CertificatePolicies
;
691 case DT_NetscapeCertType
:
692 parsedValue
= &cdt
->extension
.netscapeCertType
;
693 extnId
= CSSMOID_NetscapeCertType
;
695 case DT_CrlDistributionPoints
:
696 parsedValue
= &cdt
->extension
.crlDistPoints
;
697 extnId
= CSSMOID_CrlDistributionPoints
;
699 case DT_AuthorityInfoAccess
:
700 parsedValue
= &cdt
->extension
.authorityInfoAccess
;
701 extnId
= CSSMOID_AuthorityInfoAccess
;
705 tpCredDebug("SubmitCredRequest: DT_Other not supported");
706 CssmError::throwMe(CSSMERR_TP_UNKNOWN_TAG
);
709 extn
->extnId
= extnId
;
710 extn
->critical
= cdt
->critical
;
711 extn
->format
= CSSM_X509_DATAFORMAT_PARSED
;
712 extn
->value
.parsedValue
= parsedValue
;
713 extn
->BERvalue
.Data
= NULL
;
714 extn
->BERvalue
.Length
= 0;
715 } /* for each extension */
716 } /* converting extensions */
718 /* cook up the unsigned template */
719 makeCertTemplate(certReq
->clHand
,
721 certReq
->serialNumber
,
726 certReq
->certPublicKey
,
727 certReq
->signatureOid
,
728 NULL
, // subjectUniqueID, not used here (yet)
729 NULL
, // issuerUniqueId
731 certReq
->numExtensions
,
734 /* create signature context */
735 ourRtn
= CSSM_CSP_CreateSignatureContext(certReq
->cspHand
,
736 certReq
->signatureAlg
,
737 (CallerAuthContext
? CallerAuthContext
->CallerCredentials
: NULL
),
738 certReq
->issuerPrivateKey
,
741 tpCredDebug("CSSM_CSP_CreateSignatureContext returned %ld", (long)ourRtn
);
742 CssmError::throwMe(ourRtn
);
745 signedCert
= (CSSM_DATA_PTR
)malloc(sizeof(CSSM_DATA
));
746 signedCert
->Data
= NULL
;
747 signedCert
->Length
= 0;
748 ourRtn
= CSSM_CL_CertSign(certReq
->clHand
,
750 certTemplate
, // CertToBeSigned
755 tpCredDebug("CSSM_CL_CertSign returned %ld", (long)ourRtn
);
756 CssmError::throwMe(ourRtn
);
759 /* save it for retrieval by RetrieveCredResult */
760 addCertToMap(signedCert
, &ReferenceIdentifier
);
763 catch (const CssmError
&cerr
) {
764 tpCredDebug("SubmitCredRequest: CSSM error %ld", (long)cerr
.error
);
768 tpCredDebug("SubmitCredRequest: unknown exception");
769 ourRtn
= CSSMERR_TP_INTERNAL_ERROR
; // ??
773 tpFreeCssmData(*this, certTemplate
, CSSM_TRUE
);
774 freeX509Name(subjectX509
);
775 if(certReq
->issuerNames
) {
776 freeX509Name(issuerX509
);
778 /* else same as subject */
779 freeX509Time(notBeforeX509
);
780 freeX509Time(notAfterX509
);
784 if(sigContext
!= 0) {
785 CSSM_DeleteContext(sigContext
);
788 CssmError::throwMe(ourRtn
);
792 void AppleTPSession::RetrieveCredResult(
793 const CssmData
&ReferenceIdentifier
,
794 const CSSM_TP_CALLERAUTH_CONTEXT
*CallerAuthCredentials
,
795 sint32
&EstimatedTime
,
796 CSSM_BOOL
&ConfirmationRequired
,
797 CSSM_TP_RESULT_SET_PTR
&RetrieveOutput
)
799 CSSM_DATA
*cert
= getCertFromMap(&ReferenceIdentifier
);
802 tpCredDebug("RetrieveCredResult: refId not found");
803 CssmError::throwMe(CSSMERR_TP_INVALID_IDENTIFIER
);
806 /* CSSM_TP_RESULT_SET.Results points to a CSSM_ENCODED_CERT */
807 CSSM_ENCODED_CERT
*encCert
= (CSSM_ENCODED_CERT
*)malloc(sizeof(CSSM_ENCODED_CERT
));
808 encCert
->CertType
= CSSM_CERT_X_509v3
;
809 encCert
->CertEncoding
= CSSM_CERT_ENCODING_DER
;
812 * caller must free all three:
813 * CSSM_TP_RESULT_SET_PTR RetrieveOutput
814 * RetrieveOutput->Results (CSSM_ENCODED_CERT *encCert)
815 * encCert->CertBlob.Data (the actual cert)
817 * cert -- mallocd in SubmitCredRequest
819 encCert
->CertBlob
= *cert
;
820 RetrieveOutput
= (CSSM_TP_RESULT_SET_PTR
)malloc(
821 sizeof(CSSM_TP_RESULT_SET
));
822 RetrieveOutput
->Results
= encCert
;
823 RetrieveOutput
->NumberOfResults
= 1;
824 ConfirmationRequired
= CSSM_FALSE
;