2 * Copyright (c) 2002 Apple Computer, 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,
23 * Created 1/24/2002 by Doug Mitchell.
26 #include "AppleTPSession.h"
27 #include "certGroupUtils.h"
28 #include "tpdebugging.h"
30 #include <Security/oidsalg.h>
31 #include <Security/oidscert.h>
32 #include <Security/cssmapple.h>
33 #include <Security/cssmerrno.h>
34 #include <Security/debugging.h>
35 #include <Security/cssmapple.h>
38 #define tpCredDebug(args...) secdebug("tpCred", ## args)
41 * Build up a CSSM_X509_NAME from an arbitrary list of name/OID pairs.
42 * We do one a/v pair per RDN.
44 CSSM_X509_NAME
* AppleTPSession::buildX509Name(
45 const CSSM_APPLE_TP_NAME_OID
*nameArray
,
48 CSSM_X509_NAME
*top
= (CSSM_X509_NAME
*)malloc(sizeof(CSSM_X509_NAME
));
49 top
->numberOfRDNs
= numNames
;
52 top
->RelativeDistinguishedName
= NULL
;
55 top
->RelativeDistinguishedName
=
56 (CSSM_X509_RDN_PTR
)malloc(sizeof(CSSM_X509_RDN
) * numNames
);
57 CSSM_X509_RDN_PTR rdn
;
58 const CSSM_APPLE_TP_NAME_OID
*nameOid
;
60 for(nameDex
=0; nameDex
<numNames
; nameDex
++) {
61 rdn
= &top
->RelativeDistinguishedName
[nameDex
];
62 nameOid
= &nameArray
[nameDex
];
63 rdn
->numberOfPairs
= 1;
64 rdn
->AttributeTypeAndValue
= (CSSM_X509_TYPE_VALUE_PAIR_PTR
)
65 malloc(sizeof(CSSM_X509_TYPE_VALUE_PAIR
));
66 CSSM_X509_TYPE_VALUE_PAIR_PTR atvp
= rdn
->AttributeTypeAndValue
;
67 tpCopyCssmData(*this, nameOid
->oid
, &atvp
->type
);
68 atvp
->valueType
= BER_TAG_PRINTABLE_STRING
;
69 atvp
->value
.Length
= strlen(nameOid
->string
);
70 atvp
->value
.Data
= (uint8
*)malloc(atvp
->value
.Length
);
71 memmove(atvp
->value
.Data
, nameOid
->string
, atvp
->value
.Length
);
76 /* free the CSSM_X509_NAME obtained from buildX509Name */
77 void AppleTPSession::freeX509Name(
84 CSSM_X509_RDN_PTR rdn
;
85 for(nameDex
=0; nameDex
<top
->numberOfRDNs
; nameDex
++) {
86 rdn
= &top
->RelativeDistinguishedName
[nameDex
];
87 if(rdn
->AttributeTypeAndValue
) {
88 for(unsigned aDex
=0; aDex
<rdn
->numberOfPairs
; aDex
++) {
89 CSSM_X509_TYPE_VALUE_PAIR_PTR atvp
=
90 &rdn
->AttributeTypeAndValue
[aDex
];
91 free(atvp
->type
.Data
);
92 free(atvp
->value
.Data
);
94 free(rdn
->AttributeTypeAndValue
);
97 free(top
->RelativeDistinguishedName
);
101 /* Obtain a CSSM_X509_TIME representing "now" plus specified seconds */
104 * Although RFC 2459, *the* spec for X509 certs, allows for not before/after
105 * times to be expressed in ther generalized (4-digit year) or UTC (2-digit year
106 * with implied century rollover), IE 5 on Mac will not accept the generalized
109 #define TP_FOUR_DIGIT_YEAR 0
110 #if TP_FOUR_DIGIT_YEAR
111 #define TP_TIME_FORMAT TIME_GEN
112 #define TP_TIME_TAG BER_TAG_GENERALIZED_TIME
114 #define TP_TIME_FORMAT TIME_UTC
115 #define TP_TIME_TAG BER_TAG_UTC_TIME
116 #endif /* TP_FOUR_DIGIT_YEAR */
118 CSSM_X509_TIME
* AppleTPSession::buildX509Time(
119 unsigned secondsFromNow
)
121 CSSM_X509_TIME
*xtime
= (CSSM_X509_TIME
*)malloc(sizeof(CSSM_X509_TIME
));
122 xtime
->timeType
= TP_TIME_TAG
;
123 char *ts
= (char *)malloc(GENERALIZED_TIME_STRLEN
+ 1);
125 StLock
<Mutex
> _(tpTimeLock());
126 timeAtNowPlus(secondsFromNow
, TP_TIME_FORMAT
, ts
);
128 xtime
->time
.Data
= (uint8
*)ts
;
129 xtime
->time
.Length
= strlen(ts
);
133 /* Free CSSM_X509_TIME obtained in buildX509Time */
134 void AppleTPSession::freeX509Time(
135 CSSM_X509_TIME
*xtime
)
140 free((char *)xtime
->time
.Data
);
145 * Cook up a CSSM_DATA with specified integer, DER style (minimum number of
146 * bytes, big-endian).
148 static void intToDER(
151 CssmAllocator
&alloc
)
155 DER_Data
.Data
= (uint8
*)alloc
.malloc(1);
156 DER_Data
.Data
[0] = (unsigned char)(theInt
);
158 else if(theInt
< 0x10000) {
160 DER_Data
.Data
= (uint8
*)alloc
.malloc(2);
161 DER_Data
.Data
[0] = (unsigned char)(theInt
>> 8);
162 DER_Data
.Data
[1] = (unsigned char)(theInt
);
164 else if(theInt
< 0x1000000) {
166 DER_Data
.Data
= (uint8
*)alloc
.malloc(3);
167 DER_Data
.Data
[0] = (unsigned char)(theInt
>> 16);
168 DER_Data
.Data
[1] = (unsigned char)(theInt
>> 8);
169 DER_Data
.Data
[2] = (unsigned char)(theInt
);
173 DER_Data
.Data
= (uint8
*)alloc
.malloc(4);
174 DER_Data
.Data
[0] = (unsigned char)(theInt
>> 24);
175 DER_Data
.Data
[1] = (unsigned char)(theInt
>> 16);
176 DER_Data
.Data
[2] = (unsigned char)(theInt
>> 8);
177 DER_Data
.Data
[3] = (unsigned char)(theInt
);
181 /* The reverse of the above. */
182 static uint32
DERToInt(
183 const CSSM_DATA
&DER_Data
)
186 uint8
*bp
= DER_Data
.Data
;
187 for(unsigned dex
=0; dex
<DER_Data
.Length
; dex
++) {
194 /* Convert a reference key to a raw key. */
195 void AppleTPSession::refKeyToRaw(
196 CSSM_CSP_HANDLE cspHand
,
197 const CSSM_KEY
*refKey
,
198 CSSM_KEY_PTR rawKey
) // RETURNED
200 CSSM_CC_HANDLE ccHand
;
202 CSSM_ACCESS_CREDENTIALS creds
;
204 memset(rawKey
, 0, sizeof(CSSM_KEY
));
205 memset(&creds
, 0, sizeof(CSSM_ACCESS_CREDENTIALS
));
206 crtn
= CSSM_CSP_CreateSymmetricContext(cspHand
,
209 &creds
, // passPhrase
210 NULL
, // wrapping key
212 CSSM_PADDING_NONE
, // Padding
216 tpCredDebug("AppleTPSession::refKeyToRaw: context err");
217 CssmError::throwMe(crtn
);
220 crtn
= CSSM_WrapKey(ccHand
,
223 NULL
, // DescriptiveData
225 if(crtn
!= CSSM_OK
) {
226 tpCredDebug("AppleTPSession::refKeyToRaw: wrapKey err");
227 CssmError::throwMe(crtn
);
229 CSSM_DeleteContext(ccHand
);
234 * Cook up an unsigned cert.
235 * This is just a wrapper for CSSM_CL_CertCreateTemplate().
237 void AppleTPSession::makeCertTemplate(
239 CSSM_CL_HANDLE clHand
,
240 CSSM_CSP_HANDLE cspHand
, // for converting ref to raw key
242 const CSSM_X509_NAME
*issuerName
,
243 const CSSM_X509_NAME
*subjectName
,
244 const CSSM_X509_TIME
*notBefore
,
245 const CSSM_X509_TIME
*notAfter
,
246 const CSSM_KEY
*subjectPubKey
,
247 const CSSM_OID
&sigOid
, // e.g., CSSMOID_SHA1WithRSA
249 const CSSM_DATA
*subjectUniqueId
,
250 const CSSM_DATA
*issuerUniqueId
,
251 CSSM_X509_EXTENSION
*extensions
,
252 unsigned numExtensions
,
253 CSSM_DATA_PTR
&rawCert
)
255 CSSM_FIELD
*certTemp
;
256 unsigned fieldDex
= 0; // index into certTemp
257 CSSM_DATA serialDER
= {0, NULL
}; // serial number, DER format
258 CSSM_DATA versionDER
= {0, NULL
};
260 CSSM_X509_ALGORITHM_IDENTIFIER algId
;
261 const CSSM_KEY
*actPubKey
;
263 CSSM_BOOL freeRawKey
= CSSM_FALSE
;
266 algId
.algorithm
= sigOid
;
267 algId
.parameters
.Data
= NULL
;
268 algId
.parameters
.Length
= 0;
271 * Convert possible ref public key to raw format as required by CL.
273 switch(subjectPubKey
->KeyHeader
.BlobType
) {
274 case CSSM_KEYBLOB_RAW
:
275 actPubKey
= subjectPubKey
;
277 case CSSM_KEYBLOB_REFERENCE
:
278 refKeyToRaw(cspHand
, subjectPubKey
, &rawPubKey
);
279 actPubKey
= &rawPubKey
;
280 freeRawKey
= CSSM_TRUE
;
283 tpCredDebug("CSSM_CL_CertCreateTemplate: bad key blob type (%u)",
284 (unsigned)subjectPubKey
->KeyHeader
.BlobType
);
285 CssmError::throwMe(CSSMERR_TP_INVALID_REQUEST_INPUTS
);
290 * version, always 2 (X509v3)
291 * serialNumber thru subjectPubKey
293 unsigned numFields
= 8 + numExtensions
;
294 if(subjectUniqueId
) {
301 certTemp
= (CSSM_FIELD
*)malloc(sizeof(CSSM_FIELD
) * numFields
);
305 intToDER(2, versionDER
, *this);
306 certTemp
[fieldDex
].FieldOid
= CSSMOID_X509V1Version
;
307 certTemp
[fieldDex
++].FieldValue
= versionDER
;
310 intToDER(serialNumber
, serialDER
, *this);
311 certTemp
[fieldDex
].FieldOid
= CSSMOID_X509V1SerialNumber
;
312 certTemp
[fieldDex
++].FieldValue
= serialDER
;
314 /* subject and issuer name */
315 certTemp
[fieldDex
].FieldOid
= CSSMOID_X509V1IssuerNameCStruct
;
316 certTemp
[fieldDex
].FieldValue
.Data
= (uint8
*)issuerName
;
317 certTemp
[fieldDex
++].FieldValue
.Length
= sizeof(CSSM_X509_NAME
);
319 certTemp
[fieldDex
].FieldOid
= CSSMOID_X509V1SubjectNameCStruct
;
320 certTemp
[fieldDex
].FieldValue
.Data
= (uint8
*)subjectName
;
321 certTemp
[fieldDex
++].FieldValue
.Length
= sizeof(CSSM_X509_NAME
);
323 /* not before/after */
324 certTemp
[fieldDex
].FieldOid
= CSSMOID_X509V1ValidityNotBefore
;
325 certTemp
[fieldDex
].FieldValue
.Data
= (uint8
*)notBefore
;
326 certTemp
[fieldDex
++].FieldValue
.Length
= sizeof(CSSM_X509_TIME
);
328 certTemp
[fieldDex
].FieldOid
= CSSMOID_X509V1ValidityNotAfter
;
329 certTemp
[fieldDex
].FieldValue
.Data
= (uint8
*)notAfter
;
330 certTemp
[fieldDex
++].FieldValue
.Length
= sizeof(CSSM_X509_TIME
);
332 /* the subject key */
333 certTemp
[fieldDex
].FieldOid
= CSSMOID_CSSMKeyStruct
;
334 certTemp
[fieldDex
].FieldValue
.Data
= (uint8
*)actPubKey
;
335 certTemp
[fieldDex
++].FieldValue
.Length
= sizeof(CSSM_KEY
);
337 /* signature algorithm */
338 certTemp
[fieldDex
].FieldOid
= CSSMOID_X509V1SignatureAlgorithmTBS
;
339 certTemp
[fieldDex
].FieldValue
.Data
= (uint8
*)&algId
;
340 certTemp
[fieldDex
++].FieldValue
.Length
= sizeof(CSSM_X509_ALGORITHM_IDENTIFIER
);
342 /* subject/issuer unique IDs */
343 if(subjectUniqueId
!= 0) {
344 certTemp
[fieldDex
].FieldOid
= CSSMOID_X509V1CertificateSubjectUniqueId
;
345 certTemp
[fieldDex
++].FieldValue
= *subjectUniqueId
;
347 if(issuerUniqueId
!= 0) {
348 certTemp
[fieldDex
].FieldOid
= CSSMOID_X509V1CertificateIssuerUniqueId
;
349 certTemp
[fieldDex
++].FieldValue
= *issuerUniqueId
;
352 for(extNum
=0; extNum
<numExtensions
; extNum
++) {
353 CSSM_X509_EXTENSION_PTR ext
= &extensions
[extNum
];
354 if(ext
->format
== CSSM_X509_DATAFORMAT_PARSED
) {
355 certTemp
[fieldDex
].FieldOid
= ext
->extnId
;
358 certTemp
[fieldDex
].FieldOid
= CSSMOID_X509V3CertificateExtensionCStruct
;
360 certTemp
[fieldDex
].FieldValue
.Data
= (uint8
*)ext
;
361 certTemp
[fieldDex
++].FieldValue
.Length
= sizeof(CSSM_X509_EXTENSION
);
363 assert(fieldDex
== numFields
);
368 rawCert
= (CSSM_DATA_PTR
)malloc(sizeof(CSSM_DATA
));
369 rawCert
->Data
= NULL
;
371 CSSM_RETURN crtn
= CSSM_CL_CertCreateTemplate(clHand
,
376 tpCredDebug("CSSM_CL_CertCreateTemplate returned %s",
377 cssmErrorString(crtn
).c_str());
383 /* free the stuff we mallocd to get here */
384 free(serialDER
.Data
);
385 free(versionDER
.Data
);
388 tpFreeCssmData(*this, &rawPubKey
.KeyData
, CSSM_FALSE
);
391 CssmError::throwMe(crtn
);
395 /* given a cert and a ReferenceIdentifier, fill in ReferenceIdentifier and
396 * add it and the cert to tpCredMap. */
397 void AppleTPSession::addCertToMap(
398 const CSSM_DATA
*cert
,
401 StLock
<Mutex
> _(tpCredMapLock
);
403 TpCredHandle hand
= reinterpret_cast<TpCredHandle
>(cert
);
404 intToDER(hand
, *refId
, *this);
405 tpCredMap
[hand
] = cert
;
408 /* given a ReferenceIdentifier, obtain associated cert and remove from the map */
409 CSSM_DATA_PTR
AppleTPSession::getCertFromMap(
410 const CSSM_DATA
*refId
)
412 StLock
<Mutex
> _(tpCredMapLock
);
413 CSSM_DATA_PTR rtn
= NULL
;
415 if((refId
== NULL
) || (refId
->Data
== NULL
)) {
418 TpCredHandle hand
= DERToInt(*refId
);
419 credMap::iterator it
= tpCredMap
.find(hand
);
420 if(it
== tpCredMap
.end()) {
423 rtn
= const_cast<CSSM_DATA
*>(it
->second
);
424 tpCredMap
.erase(hand
);
429 * SubmitCredRequest, CSR form.
431 void AppleTPSession::SubmitCsrRequest(
432 const CSSM_TP_REQUEST_SET
&RequestInput
,
433 sint32
&EstimatedTime
, // RETURNED
434 CssmData
&ReferenceIdentifier
) // RETURNED
436 CSSM_DATA_PTR csrPtr
= NULL
;
437 CSSM_CC_HANDLE sigHand
= 0;
438 CSSM_APPLE_CL_CSR_REQUEST csrReq
;
440 memset(&csrReq
, 0, sizeof(csrReq
));
442 /* for now we're using the same struct for input as the the normal
443 * X509 cert request. */
444 CSSM_APPLE_TP_CERT_REQUEST
*certReq
=
445 (CSSM_APPLE_TP_CERT_REQUEST
*)RequestInput
.Requests
;
446 if((certReq
->cspHand
== 0) ||
447 (certReq
->clHand
== 0) ||
448 (certReq
->certPublicKey
== NULL
) ||
449 (certReq
->issuerPrivateKey
== NULL
) ||
450 (certReq
->signatureOid
.Data
== NULL
)) {
451 CssmError::throwMe(CSSMERR_TP_INVALID_REQUEST_INPUTS
);
454 /* convert ref public key to raw per CL requirements */
455 const CSSM_KEY
*subjectPubKey
= certReq
->certPublicKey
;
456 const CSSM_KEY
*actPubKey
= NULL
;
457 CSSM_BOOL freeRawKey
= CSSM_FALSE
;
460 switch(subjectPubKey
->KeyHeader
.BlobType
) {
461 case CSSM_KEYBLOB_RAW
:
462 actPubKey
= subjectPubKey
;
464 case CSSM_KEYBLOB_REFERENCE
:
465 refKeyToRaw(certReq
->cspHand
, subjectPubKey
, &rawPubKey
);
466 actPubKey
= &rawPubKey
;
467 freeRawKey
= CSSM_TRUE
;
470 tpCredDebug("SubmitCsrRequest: bad key blob type (%u)",
471 (unsigned)subjectPubKey
->KeyHeader
.BlobType
);
472 CssmError::throwMe(CSSMERR_TP_INVALID_REQUEST_INPUTS
);
475 /* cook up a CL-passthrough-specific request */
476 csrReq
.subjectNameX509
= buildX509Name(certReq
->subjectNames
,
477 certReq
->numSubjectNames
);
478 csrReq
.signatureAlg
= certReq
->signatureAlg
;
479 csrReq
.signatureOid
= certReq
->signatureOid
;
480 csrReq
.cspHand
= certReq
->cspHand
;
481 csrReq
.subjectPublicKey
= actPubKey
;
482 csrReq
.subjectPrivateKey
= certReq
->issuerPrivateKey
;
483 csrReq
.challengeString
= certReq
->challengeString
;
485 /* A crypto handle to pass to the CL */
487 crtn
= CSSM_CSP_CreateSignatureContext(certReq
->cspHand
,
488 certReq
->signatureAlg
,
490 certReq
->issuerPrivateKey
,
493 tpCredDebug("CSSM_CSP_CreateSignatureContext returned %s",
494 cssmErrorString(crtn
).c_str());
498 /* down to the CL to do the actual work */
499 crtn
= CSSM_CL_PassThrough(certReq
->clHand
,
501 CSSM_APPLEX509CL_OBTAIN_CSR
,
505 tpCredDebug("CSSM_CSP_CreateSignatureContext returned %s",
506 cssmErrorString(crtn
).c_str());
510 /* save it for retrieval by RetrieveCredResult */
511 addCertToMap(csrPtr
, &ReferenceIdentifier
);
515 /* free local resources */
516 if(csrReq
.subjectNameX509
) {
517 freeX509Name(csrReq
.subjectNameX509
);
520 CSSM_DeleteContext(sigHand
);
523 tpFreeCssmData(*this, &rawPubKey
.KeyData
, CSSM_FALSE
);
526 CssmError::throwMe(crtn
);
531 * Submit cred (cert) request. Currently the only form of request we
532 * handle is the basis "sign this cert with key right now", with policy OI
533 * CSSMOID_APPLE_TP_LOCAL_CERT_GEN.
535 void AppleTPSession::SubmitCredRequest(
536 const CSSM_TP_AUTHORITY_ID
*PreferredAuthority
,
537 CSSM_TP_AUTHORITY_REQUEST_TYPE RequestType
,
538 const CSSM_TP_REQUEST_SET
&RequestInput
,
539 const CSSM_TP_CALLERAUTH_CONTEXT
*CallerAuthContext
,
540 sint32
&EstimatedTime
,
541 CssmData
&ReferenceIdentifier
)
543 /* free all of these on return if non-NULL */
544 CSSM_DATA_PTR certTemplate
= NULL
;
545 CSSM_X509_TIME_PTR notBeforeX509
= NULL
;
546 CSSM_X509_TIME_PTR notAfterX509
= NULL
;
547 CSSM_X509_NAME_PTR subjectX509
= NULL
;
548 CSSM_X509_NAME_PTR issuerX509
= NULL
;
549 CSSM_X509_EXTENSION_PTR extens509
= NULL
;
550 CSSM_CC_HANDLE sigContext
= 0;
552 /* this gets saved on success */
553 CSSM_DATA_PTR signedCert
= NULL
;
555 /* validate rather limited set of input args */
556 if(PreferredAuthority
!= NULL
) {
557 CssmError::throwMe(CSSMERR_TP_INVALID_AUTHORITY
);
559 if(RequestType
!= CSSM_TP_AUTHORITY_REQUEST_CERTISSUE
) {
560 CssmError::throwMe(CSSMERR_TP_UNSUPPORTED_SERVICE
);
562 if(CallerAuthContext
== NULL
) {
563 CssmError::throwMe(CSSMERR_TP_INVALID_CALLERAUTH_CONTEXT_POINTER
);
565 if((RequestInput
.NumberOfRequests
!= 1) ||
566 (RequestInput
.Requests
== NULL
)) {
567 CssmError::throwMe(CSSMERR_TP_INVALID_REQUEST_INPUTS
);
570 /* Apple-specific args */
571 const CSSM_TP_POLICYINFO
*tpPolicy
= &CallerAuthContext
->Policy
;
572 if((tpPolicy
->NumberOfPolicyIds
!= 1) ||
573 (tpPolicy
->PolicyIds
== NULL
)) {
574 CssmError::throwMe(CSSMERR_TP_INVALID_CALLERAUTH_CONTEXT_POINTER
);
576 if(tpCompareCssmData(&tpPolicy
->PolicyIds
->FieldOid
,
577 &CSSMOID_APPLE_TP_CSR_GEN
)) {
578 /* break out to CSR-specific code */
579 SubmitCsrRequest(RequestInput
, EstimatedTime
, ReferenceIdentifier
);
582 else if(!tpCompareCssmData(&tpPolicy
->PolicyIds
->FieldOid
,
583 &CSSMOID_APPLE_TP_LOCAL_CERT_GEN
)) {
584 CssmError::throwMe(CSSMERR_TP_INVALID_POLICY_IDENTIFIERS
);
587 CSSM_APPLE_TP_CERT_REQUEST
*certReq
=
588 (CSSM_APPLE_TP_CERT_REQUEST
*)RequestInput
.Requests
;
589 if((certReq
->cspHand
== 0) ||
590 (certReq
->clHand
== 0) ||
591 (certReq
->certPublicKey
== NULL
) ||
592 (certReq
->issuerPrivateKey
== NULL
)) {
593 CssmError::throwMe(CSSMERR_TP_INVALID_REQUEST_INPUTS
);
595 if((certReq
->numExtensions
!= 0) & (certReq
->extensions
== NULL
)) {
596 CssmError::throwMe(CSSMERR_TP_INVALID_POINTER
);
599 CSSM_RETURN ourRtn
= CSSM_OK
;
602 /* convert caller's friendly names and times to CDSA style */
603 subjectX509
= buildX509Name(certReq
->subjectNames
, certReq
->numSubjectNames
);
604 if(certReq
->issuerNames
!= NULL
) {
605 issuerX509
= buildX509Name(certReq
->issuerNames
, certReq
->numIssuerNames
);
607 else if(certReq
->issuerNameX509
) {
608 /* caller obtained this from an existing signer's cert */
609 issuerX509
= certReq
->issuerNameX509
;
613 issuerX509
= subjectX509
;
615 notBeforeX509
= buildX509Time(certReq
->notBefore
);
616 notAfterX509
= buildX509Time(certReq
->notAfter
);
618 if(certReq
->numExtensions
!= 0) {
619 /* convert extensions array from CE_DataAndType to CSSM_X509_EXTENSION */
620 extens509
= (CSSM_X509_EXTENSION
*)malloc(sizeof(CSSM_X509_EXTENSION
) *
621 certReq
->numExtensions
);
622 memset(extens509
, 0, sizeof(CSSM_X509_EXTENSION
) *
623 certReq
->numExtensions
);
624 for(unsigned dex
=0; dex
<certReq
->numExtensions
; dex
++) {
625 CSSM_X509_EXTENSION
*extn
= &extens509
[dex
];
626 CE_DataAndType
*cdt
= &certReq
->extensions
[dex
];
631 case DT_AuthorityKeyID
:
632 parsedValue
= &cdt
->extension
.authorityKeyID
;
633 extnId
= CSSMOID_AuthorityKeyIdentifier
;
635 case DT_SubjectKeyID
:
636 parsedValue
= &cdt
->extension
.subjectKeyID
;
637 extnId
= CSSMOID_SubjectKeyIdentifier
;
640 parsedValue
= &cdt
->extension
.keyUsage
;
641 extnId
= CSSMOID_KeyUsage
;
643 case DT_SubjectAltName
:
644 parsedValue
= &cdt
->extension
.subjectAltName
;
645 extnId
= CSSMOID_SubjectAltName
;
647 case DT_ExtendedKeyUsage
:
648 parsedValue
= &cdt
->extension
.extendedKeyUsage
;
649 extnId
= CSSMOID_ExtendedKeyUsage
;
651 case DT_BasicConstraints
:
652 parsedValue
= &cdt
->extension
.basicConstraints
;
653 extnId
= CSSMOID_BasicConstraints
;
655 case DT_CertPolicies
:
656 parsedValue
= &cdt
->extension
.certPolicies
;
657 extnId
= CSSMOID_CertificatePolicies
;
659 case DT_NetscapeCertType
:
660 parsedValue
= &cdt
->extension
.netscapeCertType
;
661 extnId
= CSSMOID_NetscapeCertType
;
665 tpCredDebug("SubmitCredRequest: DT_Other not supported");
666 CssmError::throwMe(CSSMERR_TP_UNKNOWN_TAG
);
669 extn
->extnId
= extnId
;
670 extn
->critical
= cdt
->critical
;
671 extn
->format
= CSSM_X509_DATAFORMAT_PARSED
;
672 extn
->value
.parsedValue
= parsedValue
;
673 extn
->BERvalue
.Data
= NULL
;
674 extn
->BERvalue
.Length
= 0;
675 } /* for each extension */
676 } /* converting extensions */
678 /* cook up the unsigned template */
679 makeCertTemplate(certReq
->clHand
,
681 certReq
->serialNumber
,
686 certReq
->certPublicKey
,
687 certReq
->signatureOid
,
688 NULL
, // subjectUniqueID, not used here (yet)
689 NULL
, // issuerUniqueId
691 certReq
->numExtensions
,
694 /* create signature context */
695 ourRtn
= CSSM_CSP_CreateSignatureContext(certReq
->cspHand
,
696 certReq
->signatureAlg
,
697 (CallerAuthContext
? CallerAuthContext
->CallerCredentials
: NULL
),
698 certReq
->issuerPrivateKey
,
701 tpCredDebug("CSSM_CSP_CreateSignatureContext returned %s",
702 cssmErrorString(ourRtn
).c_str());
703 CssmError::throwMe(ourRtn
);
706 signedCert
= (CSSM_DATA_PTR
)malloc(sizeof(CSSM_DATA
));
707 signedCert
->Data
= NULL
;
708 signedCert
->Length
= 0;
709 ourRtn
= CSSM_CL_CertSign(certReq
->clHand
,
711 certTemplate
, // CertToBeSigned
716 tpCredDebug("CSSM_CL_CertSign returned %s",
717 cssmErrorString(ourRtn
).c_str());
718 CssmError::throwMe(ourRtn
);
721 /* save it for retrieval by RetrieveCredResult */
722 addCertToMap(signedCert
, &ReferenceIdentifier
);
725 catch (const CssmError
&cerr
) {
726 tpCredDebug("SubmitCredRequest: CSSM error %s",
727 cssmErrorString(cerr
).c_str());
728 ourRtn
= cerr
.cssmError();
731 tpCredDebug("SubmitCredRequest: unknown exception");
732 ourRtn
= CSSMERR_TP_INTERNAL_ERROR
; // ??
736 tpFreeCssmData(*this, certTemplate
, CSSM_TRUE
);
737 freeX509Name(subjectX509
);
738 if(certReq
->issuerNames
) {
739 freeX509Name(issuerX509
);
741 /* else same as subject */
742 freeX509Time(notBeforeX509
);
743 freeX509Time(notAfterX509
);
747 if(sigContext
!= 0) {
748 CSSM_DeleteContext(sigContext
);
751 CssmError::throwMe(ourRtn
);
755 void AppleTPSession::RetrieveCredResult(
756 const CssmData
&ReferenceIdentifier
,
757 const CSSM_TP_CALLERAUTH_CONTEXT
*CallerAuthCredentials
,
758 sint32
&EstimatedTime
,
759 CSSM_BOOL
&ConfirmationRequired
,
760 CSSM_TP_RESULT_SET_PTR
&RetrieveOutput
)
762 const CSSM_DATA
*cert
= getCertFromMap(&ReferenceIdentifier
);
765 tpCredDebug("RetrieveCredResult: refId not found");
766 CssmError::throwMe(CSSMERR_TP_INVALID_IDENTIFIER
);
769 /* CSSM_TP_RESULT_SET.Results points to a CSSM_ENCODED_CERT */
770 CSSM_ENCODED_CERT
*encCert
= (CSSM_ENCODED_CERT
*)malloc(sizeof(CSSM_ENCODED_CERT
));
771 encCert
->CertType
= CSSM_CERT_X_509v3
;
772 encCert
->CertEncoding
= CSSM_CERT_ENCODING_DER
;
775 * caller must free all three:
776 * CSSM_TP_RESULT_SET_PTR RetrieveOutput
777 * RetrieveOutput->Results (CSSM_ENCODED_CERT *encCert)
778 * encCert->CertBlob.Data (the actual cert)
780 encCert
->CertBlob
= *cert
;
781 RetrieveOutput
= (CSSM_TP_RESULT_SET_PTR
)malloc(
782 sizeof(CSSM_TP_RESULT_SET
));
783 RetrieveOutput
->Results
= encCert
;
784 RetrieveOutput
->NumberOfResults
= 1;
785 ConfirmationRequired
= CSSM_FALSE
;