Skip to content

Commit

Permalink
Improve signature method code
Browse files Browse the repository at this point in the history
  • Loading branch information
fpseverino committed Jan 2, 2025
1 parent 3e488a0 commit 29ea986
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
26 changes: 16 additions & 10 deletions Sources/WalletOrders/OrderBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,28 +78,34 @@ public struct OrderBuilder: Sendable {
try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
defer { try? FileManager.default.removeItem(at: dir) }

try manifest.write(to: dir.appendingPathComponent(Self.manifestFileName))
try self.pemWWDRCertificate.write(to: dir.appendingPathComponent("wwdr.pem"), atomically: true, encoding: .utf8)
try self.pemCertificate.write(to: dir.appendingPathComponent("certificate.pem"), atomically: true, encoding: .utf8)
try self.pemPrivateKey.write(to: dir.appendingPathComponent("private.pem"), atomically: true, encoding: .utf8)
let manifestURL = dir.appendingPathComponent(Self.manifestFileName)
let wwdrURL = dir.appendingPathComponent("wwdr.pem")
let certificateURL = dir.appendingPathComponent("certificate.pem")
let privateKeyURL = dir.appendingPathComponent("private.pem")
let signatureURL = dir.appendingPathComponent(Self.signatureFileName)

try manifest.write(to: manifestURL)
try self.pemWWDRCertificate.write(to: wwdrURL, atomically: true, encoding: .utf8)
try self.pemCertificate.write(to: certificateURL, atomically: true, encoding: .utf8)
try self.pemPrivateKey.write(to: privateKeyURL, atomically: true, encoding: .utf8)

let process = Process()
process.currentDirectoryURL = dir
process.executableURL = self.openSSLURL
process.arguments = [
"smime", "-binary", "-sign",
"-certfile", dir.appendingPathComponent("wwdr.pem").path,
"-signer", dir.appendingPathComponent("certificate.pem").path,
"-inkey", dir.appendingPathComponent("private.pem").path,
"-in", dir.appendingPathComponent(Self.manifestFileName).path,
"-out", dir.appendingPathComponent(Self.signatureFileName).path,
"-certfile", wwdrURL.path,
"-signer", certificateURL.path,
"-inkey", privateKeyURL.path,
"-in", manifestURL.path,
"-out", signatureURL.path,
"-outform", "DER",
"-passin", "pass:\(pemPrivateKeyPassword)",
]
try process.run()
process.waitUntilExit()

return try Data(contentsOf: dir.appendingPathComponent(Self.signatureFileName))
return try Data(contentsOf: signatureURL)
} else {
let signature = try CMS.sign(
manifest,
Expand Down
26 changes: 16 additions & 10 deletions Sources/WalletPasses/PassBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,28 +103,34 @@ public struct PassBuilder: Sendable {
try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
defer { try? FileManager.default.removeItem(at: dir) }

try manifest.write(to: dir.appendingPathComponent(Self.manifestFileName))
try self.pemWWDRCertificate.write(to: dir.appendingPathComponent("wwdr.pem"), atomically: true, encoding: .utf8)
try self.pemCertificate.write(to: dir.appendingPathComponent("certificate.pem"), atomically: true, encoding: .utf8)
try self.pemPrivateKey.write(to: dir.appendingPathComponent("private.pem"), atomically: true, encoding: .utf8)
let manifestURL = dir.appendingPathComponent(Self.manifestFileName)
let wwdrURL = dir.appendingPathComponent("wwdr.pem")
let certificateURL = dir.appendingPathComponent("certificate.pem")
let privateKeyURL = dir.appendingPathComponent("private.pem")
let signatureURL = dir.appendingPathComponent(Self.signatureFileName)

try manifest.write(to: manifestURL)
try self.pemWWDRCertificate.write(to: wwdrURL, atomically: true, encoding: .utf8)
try self.pemCertificate.write(to: certificateURL, atomically: true, encoding: .utf8)
try self.pemPrivateKey.write(to: privateKeyURL, atomically: true, encoding: .utf8)

let process = Process()
process.currentDirectoryURL = dir
process.executableURL = self.openSSLURL
process.arguments = [
"smime", "-binary", "-sign",
"-certfile", dir.appendingPathComponent("wwdr.pem").path,
"-signer", dir.appendingPathComponent("certificate.pem").path,
"-inkey", dir.appendingPathComponent("private.pem").path,
"-in", dir.appendingPathComponent(Self.manifestFileName).path,
"-out", dir.appendingPathComponent(Self.signatureFileName).path,
"-certfile", wwdrURL.path,
"-signer", certificateURL.path,
"-inkey", privateKeyURL.path,
"-in", manifestURL.path,
"-out", signatureURL.path,
"-outform", "DER",
"-passin", "pass:\(pemPrivateKeyPassword)",
]
try process.run()
process.waitUntilExit()

return try Data(contentsOf: dir.appendingPathComponent(Self.signatureFileName))
return try Data(contentsOf: signatureURL)
} else {
let signature = try CMS.sign(
manifest,
Expand Down

0 comments on commit 29ea986

Please sign in to comment.