Skeleton for install-ca command
This commit is contained in:
parent
1908892f0a
commit
173035f063
|
@ -4,7 +4,6 @@ A description of this package.
|
||||||
|
|
||||||
## TODO
|
## TODO
|
||||||
|
|
||||||
* PEM parser
|
|
||||||
* `install-ca` command
|
* `install-ca` command
|
||||||
* Filter for os types
|
* Filter for os types
|
||||||
* Find out why the argument parsing errors are not printed correctly
|
* Find out why the argument parsing errors are not printed correctly
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
disabled_rules:
|
disabled_rules:
|
||||||
- identifier_name
|
- identifier_name
|
||||||
- nesting
|
- nesting
|
||||||
|
|
||||||
|
line_length:
|
||||||
|
warning: 140
|
|
@ -10,13 +10,14 @@ struct InstallCACommand: Command {
|
||||||
let overview = "Install a Certificate Authority"
|
let overview = "Install a Certificate Authority"
|
||||||
|
|
||||||
private let binder = ArgumentBinder<InstallCACommand>()
|
private let binder = ArgumentBinder<InstallCACommand>()
|
||||||
|
private var path: String?
|
||||||
|
|
||||||
func addOptions(to parser: ArgumentParser) {
|
func addOptions(to parser: ArgumentParser) {
|
||||||
binder.bind(positional: parser.add(
|
binder.bind(positional: parser.add(
|
||||||
positional: "path",
|
positional: "path",
|
||||||
kind: String.self
|
kind: String.self
|
||||||
), to: { command, path in
|
), to: { command, path in
|
||||||
print("Chosen path: \(path)")
|
command.path = path
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,6 +26,8 @@ struct InstallCACommand: Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
func run() throws {
|
func run() throws {
|
||||||
print("Running!")
|
let url = URL(fileURLWithPath: path!)
|
||||||
|
let certificate = try Certificate.load(from: url)
|
||||||
|
print(certificate)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
//
|
//
|
||||||
// Copyright © 2019 Simon Kågedal Reimer. See LICENSE.
|
// Copyright © 2019 Simon Kågedal Reimer. See LICENSE.
|
||||||
//
|
//
|
||||||
// swiftlint:disable line_length
|
|
||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
|
|
|
@ -8,12 +8,18 @@ import Security
|
||||||
struct Certificate {
|
struct Certificate {
|
||||||
enum Error: LocalizedError {
|
enum Error: LocalizedError {
|
||||||
case invalidDERX509
|
case invalidDERX509
|
||||||
|
case importError(OSStatus)
|
||||||
|
case notACertficate
|
||||||
case unknown
|
case unknown
|
||||||
|
|
||||||
var errorDescription: String? {
|
var errorDescription: String? {
|
||||||
switch self {
|
switch self {
|
||||||
case .invalidDERX509:
|
case .invalidDERX509:
|
||||||
return "Given data was not a valid DER encoded X.509 certificate"
|
return "Given data was not a valid DER encoded X.509 certificate"
|
||||||
|
case .importError(let status):
|
||||||
|
return "Error from SecItemImport: \(status)"
|
||||||
|
case .notACertficate:
|
||||||
|
return "SecItemImport gave something else than a certificate"
|
||||||
case .unknown:
|
case .unknown:
|
||||||
return "Operation completed with an unknown error from the Security framework"
|
return "Operation completed with an unknown error from the Security framework"
|
||||||
}
|
}
|
||||||
|
@ -62,4 +68,22 @@ struct Certificate {
|
||||||
print("<unknown certificate>")
|
print("<unknown certificate>")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static func load(from url: URL) throws -> SecCertificate {
|
||||||
|
let data = try Data(contentsOf: url)
|
||||||
|
|
||||||
|
var cfitems: CFArray?
|
||||||
|
var format = SecExternalFormat.formatUnknown
|
||||||
|
var type = SecExternalItemType.itemTypeUnknown
|
||||||
|
|
||||||
|
let status = SecItemImport(data as CFData, url.lastPathComponent as CFString, &format, &type, [], nil, nil, &cfitems)
|
||||||
|
guard status == errSecSuccess else {
|
||||||
|
throw Error.importError(status)
|
||||||
|
}
|
||||||
|
guard type == .itemTypeCertificate, let items = cfitems as? [SecCertificate], let item = items.first else {
|
||||||
|
throw Error.notACertficate
|
||||||
|
}
|
||||||
|
return item
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue