@@ -329,11 +329,12 @@ import CZitiPrivate
329329 return
330330 }
331331
332- // Store certificate
333- let cert = dropFirst ( " pem: " , resp. id. cert)
332+ // Store certificates
333+ let certs = dropFirst ( " pem: " , resp. id. cert)
334334 _ = zkc. deleteCertificate ( silent: true )
335- let ( err, cns) = zkc. storeCertificates ( cert)
336- guard err == nil else {
335+ // storeCertificate only stores the first (leaf) certificate in the pem. that's ok - the full chain of certs is stored in the .zid
336+ // file. only the leaf/pubkey needs to be in the keychain.
337+ guard zkc. storeCertificate ( fromPem: certs) == nil else {
337338 let errStr = " Unable to store certificate \n "
338339 log. error ( errStr, function: " enroll() " )
339340 enrollCallback ( nil , ZitiError ( errStr) )
@@ -346,8 +347,8 @@ import CZitiPrivate
346347 ca = dropFirst ( " pem: " , idCa)
347348 }
348349
349- let zid = ZitiIdentity ( id: subj, ztAPIs: resp. ztAPIs, certCNs : cns , ca: ca)
350- log. info ( " Enrolled id: \( subj) with controller: \( zid. ztAPI) with cns: \( zid . getCertCNs ( ) ) " , function: " enroll() " )
350+ let zid = ZitiIdentity ( id: subj, ztAPIs: resp. ztAPIs, certs : certs , ca: ca)
351+ log. info ( " Enrolled id: \( subj) with controller: \( zid. ztAPI) " , function: " enroll() " )
351352
352353 enrollCallback ( zid, nil )
353354 }
@@ -384,14 +385,12 @@ import CZitiPrivate
384385 @objc public func run( _ postureChecks: ZitiPostureChecks ? , _ initCallback: @escaping InitCallback ) {
385386 // Get certificate
386387 let zkc = ZitiKeychain ( tag: id. id)
387- let ( maybeCerts, zErr) = zkc. getCertificates ( id. getCertCNs ( ) )
388- guard let certs = maybeCerts, zErr == nil else {
389- let errStr = zErr != nil ? zErr!. localizedDescription : " unable to retrieve certificates from keychain "
388+ guard let certPEM = id. getCertificates ( zkc) else {
389+ let errStr = " unable to retrieve certificates "
390390 log. error ( errStr)
391- initCallback ( zErr ?? ZitiError ( errStr) )
391+ initCallback ( ZitiError ( errStr) )
392392 return
393393 }
394- let certPEM = zkc. convertToPEM ( " CERTIFICATE " , ders: certs)
395394
396395 // Get private key
397396 guard let privKey = zkc. getPrivateKey ( ) else {
@@ -418,6 +417,7 @@ import CZitiPrivate
418417 model_list_append ( & ctrls, c. cstring)
419418 }
420419
420+ // ziti_context_init copies strings (strdup) for its own use, so it's ok to use references to swift strings here.
421421 var zitiCfg = ziti_config (
422422 controller_url: id. ztAPI. cstring,
423423 controllers: ctrls,
@@ -445,7 +445,7 @@ import CZitiPrivate
445445 pq_domain_cb: postureChecks? . domainQuery != nil ? Ziti . onDomainQuery : nil ,
446446 app_ctx: self . toVoidPtr ( ) ,
447447 events: ZitiContextEvent . rawValue | ZitiRouterEvent . rawValue | ZitiServiceEvent . rawValue | ZitiAuthEvent . rawValue | ZitiConfigEvent . rawValue,
448- event_cb: Ziti . onEvent, cert_extension_window: 0 )
448+ event_cb: Ziti . onEvent, cert_extension_window: 30 )
449449
450450 zitiStatus = ziti_context_set_options ( self . ztx, & zitiOpts)
451451 guard zitiStatus == Ziti . ZITI_OK else {
@@ -901,8 +901,20 @@ import CZitiPrivate
901901
902902 // update ourself
903903 if event. type == ZitiEvent . EventType. ConfigEvent {
904- mySelf. id. ztAPI = event. configEvent!. controllerUrl
905- mySelf. id. ca = event. configEvent!. caBundle
904+ let cfgEvent = event. configEvent!
905+ if !cfgEvent. controllerUrl. isEmpty { mySelf. id. ztAPI = cfgEvent. controllerUrl }
906+ if !cfgEvent. controllers. isEmpty { mySelf. id. ztAPIs = cfgEvent. controllers }
907+ if !cfgEvent. cert. isEmpty {
908+ mySelf. id. certs = cfgEvent. cert
909+ let zkc = ZitiKeychain ( tag: mySelf. id. id)
910+ _ = zkc. deleteCertificate ( )
911+ // store the first/leaf certificate in the keychain so it can be used in a key pair.
912+ let zErr = zkc. storeCertificate ( fromPem: event. configEvent!. cert)
913+ if zErr != nil {
914+ log. warn ( " failed to store certificate: \( zErr!. localizedDescription) " , function: " onEvent() " )
915+ }
916+ }
917+ if !cfgEvent. caBundle. isEmpty { mySelf. id. ca = cfgEvent. caBundle }
906918 }
907919
908920 mySelf. eventCallbacksLock. lock ( )
@@ -1125,7 +1137,14 @@ func scan<
11251137}
11261138
11271139extension String {
1140+ // use only when scope of c string matches scope of swift string.
11281141 var cstring : UnsafePointer < CChar > {
11291142 ( self as NSString ) . cString ( using: String . Encoding. utf8. rawValue) !
11301143 }
1144+ // use when c string needs to outlive swift string. caller must deallocate() the returned buffer when no longer needed.
1145+ var allocatedcString : UnsafeMutablePointer < CChar > {
1146+ let buf = UnsafeMutablePointer< CChar> . allocate( capacity: self . count + 1 )
1147+ buf. initialize ( from: self , count: self . count + 1 )
1148+ return buf
1149+ }
11311150}
0 commit comments