From cd65e1094fb0a9a6119aa7d7fa99baf929405470 Mon Sep 17 00:00:00 2001 From: Si Beaumont Date: Thu, 6 Jun 2024 12:19:36 +0100 Subject: [PATCH] extras: Add meaningful description to BoringSSL errors --- .../Util/CryptoKitErrors_boring.swift | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Sources/_CryptoExtras/Util/CryptoKitErrors_boring.swift b/Sources/_CryptoExtras/Util/CryptoKitErrors_boring.swift index 55b3cb37..fde5d02e 100644 --- a/Sources/_CryptoExtras/Util/CryptoKitErrors_boring.swift +++ b/Sources/_CryptoExtras/Util/CryptoKitErrors_boring.swift @@ -22,3 +22,28 @@ extension CryptoKitError { return .underlyingCoreCryptoError(error: Int32(bitPattern: CCryptoBoringSSL_ERR_get_error())) } } + +extension CryptoKitError { + public var description: String { + switch self { + case .underlyingCoreCryptoError(error: let error): + let errorCode = UInt32(bitPattern: error) + let lib = String(cString: CCryptoBoringSSL_ERR_lib_error_string(errorCode)) + let reason = String(cString: CCryptoBoringSSL_ERR_reason_error_string(errorCode)) + return "lib: \(lib), reason: \(reason), code: \(errorCode)" + case .incorrectKeySize: return "incorrectKeySize" + case .incorrectParameterSize: return "incorrectParameterSize" + case .authenticationFailure: return "authenticationFailure" + case .wrapFailure: return "wrapFailure" + case .unwrapFailure: return "unwrapFailure" + case .invalidParameter: return "invalidParameter" + @unknown default: return "unknown" + } + } +} + +#if hasAttribute(retroactive) +extension CryptoKitError: @retroactive CustomStringConvertible {} +#else +extension CryptoKitError: CustomStringConvertible {} +#endif