Skip to content

Commit

Permalink
[V4] Fix memory bug when loading keys (#208)
Browse files Browse the repository at this point in the history
Fix leaking pointer

Co-authored-by: Tim Condon <[email protected]>
  • Loading branch information
ptoffy and 0xTim authored Nov 4, 2024
1 parent 20ef179 commit 13e7513
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions Sources/JWTKit/Utilities/OpenSSLSigner.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Foundation
@_implementationOnly import CJWTKitBoringSSL
import Foundation

protocol OpenSSLSigner {
var algorithm: OpaquePointer { get }
Expand Down Expand Up @@ -33,23 +33,25 @@ extension OpenSSLSigner {
guard CJWTKitBoringSSL_EVP_DigestFinal_ex(context, &digest, &digestLength) == 1 else {
throw JWTError.signingAlgorithmFailure(OpenSSLError.digestFinalizationFailure)
}
return .init(digest[0..<Int(digestLength)])
return .init(digest[0 ..< Int(digestLength)])
}
}

protocol OpenSSLKey { }
protocol OpenSSLKey {}

extension OpenSSLKey {
static func load<Data, T>(pem data: Data, _ closure: (UnsafeMutablePointer<BIO>) -> (T?)) throws -> T
where Data: DataProtocol
{
let bytes = data.copyBytes()
let bio = CJWTKitBoringSSL_BIO_new_mem_buf(bytes, numericCast(bytes.count))
defer { CJWTKitBoringSSL_BIO_free(bio) }

guard let bioPtr = bio, let c = closure(bioPtr) else {
throw JWTError.signingAlgorithmFailure(OpenSSLError.bioConversionFailure)
try data.copyBytes().withUnsafeBytes { (bytes: UnsafeRawBufferPointer) in
let bio = CJWTKitBoringSSL_BIO_new_mem_buf(bytes.baseAddress, numericCast(bytes.count))

defer { CJWTKitBoringSSL_BIO_free(bio) }

guard let bioPtr = bio, let c = closure(bioPtr) else {
throw JWTError.signingAlgorithmFailure(OpenSSLError.bioConversionFailure)
}
return c
}
return c
}
}

0 comments on commit 13e7513

Please sign in to comment.