From 85b04839f104c659b59e95c0dd1bad6ba94a9808 Mon Sep 17 00:00:00 2001 From: andy Date: Mon, 25 Feb 2019 13:27:07 +0000 Subject: [PATCH] rebased to master --- Sources/CryptorRSA/CryptorRSA.swift | 30 +++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/Sources/CryptorRSA/CryptorRSA.swift b/Sources/CryptorRSA/CryptorRSA.swift index ec52945..a8d2bfe 100644 --- a/Sources/CryptorRSA/CryptorRSA.swift +++ b/Sources/CryptorRSA/CryptorRSA.swift @@ -453,8 +453,20 @@ public class CryptorRSA { // Assign size of the corresponding cipher's IV let IVLength = EVP_CIPHER_iv_length(.make(optional: enc)) let iv = UnsafeMutablePointer.allocate(capacity: Int(IVLength)) - let encrypted = UnsafeMutablePointer.allocate(capacity: self.data.count + Int(IVLength)) + defer { + #if swift(>=4.1) + ek?.deallocate() + ekPtr.deallocate() + iv.deallocate() + encrypted.deallocate() + #else + ek?.deallocate(capacity: Int(EVP_PKEY_size(evp_key))) + ekPtr.deallocate(capacity: MemoryLayout.size) + iv.deallocate(capacity: Int(IVLength)) + encrypted.deallocate(capacity: self.data.count + Int(IVLength)) + #endif + } var encKeyLength: Int32 = 0 var processedLength: Int32 = 0 var encLength: Int32 = 0 @@ -651,7 +663,13 @@ public class CryptorRSA { var decMsgLen: Int32 = 0 let decrypted = UnsafeMutablePointer.allocate(capacity: Int(encryptedData.count + encryptedIV.count)) - + defer { + #if swift(>=4.1) + decrypted.deallocate() + #else + decrypted.deallocate(capacity: Int(encryptedData.count + encryptedIV.count)) + #endif + } // EVP_OpenInit returns 0 on error or the recovered secret key size if successful status = encryptedKey.withUnsafeBytes({ (ek: UnsafePointer) -> Int32 in return encryptedIV.withUnsafeBytes({ (iv: UnsafePointer) -> Int32 in @@ -756,6 +774,14 @@ public class CryptorRSA { EVP_DigestSignFinal(md_ctx, nil, &sig_len) let sig = UnsafeMutablePointer.allocate(capacity: sig_len) + defer { + #if swift(>=4.1) + sig.deallocate() + #else + sig.deallocate(capacity: sig_len) + #endif + } + rc = EVP_DigestSignFinal(md_ctx, sig, &sig_len) guard rc == 1, sig_len > 0 else { let source = "Signing failed."