Skip to content

Commit b00f977

Browse files
committed
store certificates from config events
1 parent 0b8a623 commit b00f977

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

lib/Ziti.swift

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ import CZitiPrivate
334334
_ = zkc.deleteCertificate(silent: true)
335335
let (err, cns) = zkc.storeCertificates(cert)
336336
guard err == nil else {
337-
let errStr = "Unable to store certificate\n"
337+
let errStr = "Unable to store certificates\n"
338338
log.error(errStr, function:"enroll()")
339339
enrollCallback(nil, ZitiError(errStr))
340340
return
@@ -418,6 +418,7 @@ import CZitiPrivate
418418
model_list_append(&ctrls, c.cstring)
419419
}
420420

421+
// ziti_context_init copies strings (strdup) for its own use, so it's ok to use references to swift strings here.
421422
var zitiCfg = ziti_config(
422423
controller_url: id.ztAPI.cstring,
423424
controllers: ctrls,
@@ -445,7 +446,7 @@ import CZitiPrivate
445446
pq_domain_cb: postureChecks?.domainQuery != nil ? Ziti.onDomainQuery : nil,
446447
app_ctx: self.toVoidPtr(),
447448
events: ZitiContextEvent.rawValue | ZitiRouterEvent.rawValue | ZitiServiceEvent.rawValue | ZitiAuthEvent.rawValue | ZitiConfigEvent.rawValue,
448-
event_cb: Ziti.onEvent, cert_extension_window: 0)
449+
event_cb: Ziti.onEvent, cert_extension_window: 30)
449450

450451
zitiStatus = ziti_context_set_options(self.ztx, &zitiOpts)
451452
guard zitiStatus == Ziti.ZITI_OK else {
@@ -902,6 +903,15 @@ import CZitiPrivate
902903
// update ourself
903904
if event.type == ZitiEvent.EventType.ConfigEvent {
904905
mySelf.id.ztAPI = event.configEvent!.controllerUrl
906+
mySelf.id.ztAPIs = event.configEvent!.controllers
907+
let zkc = ZitiKeychain(tag: mySelf.id.id)
908+
_ = zkc.deleteCertificate()
909+
let (zErr, certCNs) = zkc.storeCertificates(event.configEvent!.cert)
910+
if zErr != nil {
911+
log.warn("failed to store certificates: \(zErr!.localizedDescription)", function:"onEvent()")
912+
} else {
913+
mySelf.id.certCNs = certCNs
914+
}
905915
mySelf.id.ca = event.configEvent!.caBundle
906916
}
907917

@@ -1125,7 +1135,14 @@ func scan<
11251135
}
11261136

11271137
extension String {
1138+
// use only when scope of c string matches scope of swift string.
11281139
var cstring: UnsafePointer<CChar> {
11291140
(self as NSString).cString(using: String.Encoding.utf8.rawValue)!
11301141
}
1142+
// use when c string needs to outlive swift string. caller must deallocate() the returned buffer when no longer needed.
1143+
var allocatedcString: UnsafeMutablePointer<CChar> {
1144+
let buf = UnsafeMutablePointer<CChar>.allocate(capacity: self.count + 1)
1145+
buf.initialize(from: self, count: self.count + 1)
1146+
return buf
1147+
}
11311148
}

lib/ZitiEvent.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,8 @@ import CZitiPrivate
271271
@objc public let controllerUrl:String
272272
@objc public let controllers:[String]
273273
@objc public let cfgSource:String
274-
275-
@objc public let caBundle:String // todo encapsulate ziti_id_cfg_s?
274+
@objc public let cert:String
275+
@objc public let caBundle:String
276276

277277
init( _ cEvent:ziti_config_event) {
278278
var str = ""
@@ -295,6 +295,12 @@ import CZitiPrivate
295295
caStr = String(cString: cStr)
296296
}
297297
caBundle = caStr
298+
299+
var certStr = ""
300+
if let cStr = cEvent.config.pointee.id.cert {
301+
certStr = String(cString: cStr)
302+
}
303+
cert = certStr
298304

299305
var ctrlsArray:[String] = []
300306
var ctrlList = cEvent.config.pointee.controllers
@@ -383,6 +389,7 @@ import CZitiPrivate
383389
str += " controllers: \(e.controllers))\n"
384390
str += " cfgSource: \(e.cfgSource)\n"
385391
str += " caBundle: \(e.caBundle)\n"
392+
str += " cert: \(e.cert)\n"
386393
}
387394
return str
388395
}

lib/ZitiTunnel.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,16 @@ public class ZitiTunnel : NSObject, ZitiUnretained {
349349
if !event.caBundle.isEmpty {
350350
ziti.id.ca = event.caBundle
351351
}
352+
if !event.certPEM.isEmpty {
353+
let zkc = ZitiKeychain(tag: ziti.id.id)
354+
_ = zkc.deleteCertificate()
355+
let (zErr, certCNs) = zkc.storeCertificates(event.certPEM)
356+
if zErr != nil {
357+
log.warn("failed to store certificates: \(zErr!.localizedDescription)", function:"onEventCallback()")
358+
} else {
359+
ziti.id.certCNs = certCNs
360+
}
361+
}
352362
// pass event to application
353363
mySelf.tunnelProvider?.tunnelEventCallback(event)
354364
default:

lib/ZitiTunnelEvent.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,9 @@ import CZitiPrivate
220220
/// CA bundle
221221
public var caBundle:String = ""
222222

223+
/// Certificte PEM (possibly multiple certificates)
224+
public var certPEM:String = ""
225+
223226
init(_ ziti:Ziti, _ evt:UnsafePointer<config_event>) {
224227
super.init(ziti)
225228
var ziti_cfg_ptr:UnsafeMutablePointer<ziti_config>?
@@ -239,6 +242,7 @@ import CZitiPrivate
239242
}
240243
}
241244
self.caBundle = toStr(ziti_cfg_ptr?.pointee.id.ca)
245+
self.certPEM = toStr(ziti_cfg_ptr?.pointee.id.cert)
242246
}
243247

244248
/// Debug description
@@ -247,6 +251,7 @@ import CZitiPrivate
247251
return super.debugDescription + "\n" +
248252
" controller_url: \(controllerUrl)\n" +
249253
" contrlollers: \(controllers)\n" +
250-
" caBundle: \(caBundle)"
254+
" caBundle: \(caBundle)\n" +
255+
" cert: \(certPEM)"
251256
}
252257
}

0 commit comments

Comments
 (0)