Files
RememBerBerandCursor 4d1d6ab33a feat(next-macos-native): localize migration, backup, crypto, and core errors
Extend zh/en/ja ToolError coverage for Electron import, workspace validation, favorites, translation words, RSA/SM2 crypto, and remaining Core engines; wire UI language through migration panel and tighten smoke find waits.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-17 06:25:14 +08:00

85 lines
4.5 KiB
Swift

import Foundation
/// RSA PKCS#1 type 1 private-encrypt / public-decrypt, matching Electron `node-forge` semantics via system OpenSSL.
enum RSAOpenSSLBridge {
static func privateEncrypt(
plaintext: String,
privateKeyBase64: String,
language: AppLanguage = AppLocalization.preferredLanguage()
) throws -> String {
let directory = try makeWorkspace()
defer { try? FileManager.default.removeItem(at: directory) }
let privateDER = try derData(from: privateKeyBase64, language: language)
let privateDERFile = directory.appendingPathComponent("private.der")
let privatePEM = directory.appendingPathComponent("private.pem")
let plainFile = directory.appendingPathComponent("plain.txt")
let cipherFile = directory.appendingPathComponent("cipher.bin")
try privateDER.write(to: privateDERFile)
try Data(plaintext.utf8).write(to: plainFile)
try runOpenSSL(["rsa", "-inform", "DER", "-in", privateDERFile.path, "-out", privatePEM.path])
try runOpenSSL(["rsautl", "-sign", "-inkey", privatePEM.path, "-in", plainFile.path, "-out", cipherFile.path])
guard FileManager.default.fileExists(atPath: cipherFile.path) else { throw err("crypto.error.rsaPrivateEncryptFailed", language) }
return try Data(contentsOf: cipherFile).base64EncodedString()
}
static func publicDecrypt(
ciphertextBase64: String,
publicKeyBase64: String,
language: AppLanguage = AppLocalization.preferredLanguage()
) throws -> String {
let directory = try makeWorkspace()
defer { try? FileManager.default.removeItem(at: directory) }
let publicDER = try derData(from: publicKeyBase64, language: language)
let publicDERFile = directory.appendingPathComponent("public.der")
let publicPEM = directory.appendingPathComponent("public.pem")
let cipherFile = directory.appendingPathComponent("cipher.bin")
let plainFile = directory.appendingPathComponent("plain.txt")
try publicDER.write(to: publicDERFile)
guard let cipher = Data(base64Encoded: ciphertextBase64.replacingOccurrences(of: "\\s", with: "", options: .regularExpression)) else {
throw err("crypto.error.cipherBase64", language)
}
try cipher.write(to: cipherFile)
try importPublicDER(publicDER, pemPath: publicPEM)
try runOpenSSL(["rsautl", "-verify", "-pubin", "-inkey", publicPEM.path, "-in", cipherFile.path, "-out", plainFile.path])
guard FileManager.default.fileExists(atPath: plainFile.path) else { throw err("crypto.error.rsaPublicDecryptFailed", language) }
guard let text = String(data: try Data(contentsOf: plainFile), encoding: .utf8) else {
throw err("crypto.error.decryptNotUtf8", language)
}
return text
}
private static func err(_ key: String, _ language: AppLanguage) -> ToolError {
ToolError(AppLocalization.string(key, language: language))
}
private static func derData(from base64: String, language: AppLanguage) throws -> Data {
let cleaned = base64.replacingOccurrences(of: "\\s", with: "", options: .regularExpression)
guard let data = Data(base64Encoded: cleaned), !data.isEmpty else { throw err("crypto.error.keyBase64Der", language) }
return data
}
private static func importPublicDER(_ der: Data, pemPath: URL) throws {
let derFile = pemPath.deletingLastPathComponent().appendingPathComponent("public-input.der")
try der.write(to: derFile)
do {
try runOpenSSL(["pkey", "-pubin", "-inform", "DER", "-in", derFile.path, "-out", pemPath.path])
return
} catch {}
try runOpenSSL(["rsa", "-RSAPublicKey_in", "-inform", "DER", "-in", derFile.path, "-out", pemPath.path])
}
private static func runOpenSSL(_ arguments: [String]) throws {
let output = try ProcessRunner.runSync(executable: "/usr/bin/openssl", arguments: arguments).lowercased()
if output.contains("unable to") || output.contains("error:") || output.contains("bad decrypt") {
throw ToolError(output.trimmingCharacters(in: .whitespacesAndNewlines))
}
}
private static func makeWorkspace() throws -> URL {
let directory = FileManager.default.temporaryDirectory
.appendingPathComponent(Product.id + "-rsa-" + UUID().uuidString)
try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true, attributes: [.posixPermissions: 0o700])
return directory
}
}