From f5dd7a60ff56f501ff7bf9be753e4b1875bfaf20 Mon Sep 17 00:00:00 2001 From: Kevin Sweeney Date: Mon, 12 Aug 2019 16:42:17 +0100 Subject: [PATCH] Don't pass read-only bytes as mutable to BoringSSL (#129) Motivation: The BoringSSL version of `BIO_new_mem_buf` takes a const buffer but we pass a mutable one. Modifications: Pass a const buffer. Result: Unsafe instead of Unsafe+Mutable. --- Sources/NIOSSL/SSLCertificate.swift | 4 ++-- Sources/NIOSSL/SSLPKCS12Bundle.swift | 2 +- Sources/NIOSSL/SSLPrivateKey.swift | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Sources/NIOSSL/SSLCertificate.swift b/Sources/NIOSSL/SSLCertificate.swift index a1cc9206..2d60f285 100644 --- a/Sources/NIOSSL/SSLCertificate.swift +++ b/Sources/NIOSSL/SSLCertificate.swift @@ -89,7 +89,7 @@ public class NIOSSLCertificate { /// DER format. public convenience init(bytes: [UInt8], format: NIOSSLSerializationFormats) throws { let ref = bytes.withUnsafeBytes { (ptr) -> UnsafeMutablePointer? in - let bio = CNIOBoringSSL_BIO_new_mem_buf(UnsafeMutableRawPointer(mutating: ptr.baseAddress!), CInt(ptr.count))! + let bio = CNIOBoringSSL_BIO_new_mem_buf(ptr.baseAddress, CInt(ptr.count))! defer { CNIOBoringSSL_BIO_free(bio) @@ -220,7 +220,7 @@ extension NIOSSLCertificate { } return try bytes.withUnsafeBytes { (ptr) -> [NIOSSLCertificate] in - let bio = CNIOBoringSSL_BIO_new_mem_buf(UnsafeMutableRawPointer(mutating: ptr.baseAddress!), CInt(ptr.count))! + let bio = CNIOBoringSSL_BIO_new_mem_buf(ptr.baseAddress, CInt(ptr.count))! defer { CNIOBoringSSL_BIO_free(bio) } diff --git a/Sources/NIOSSL/SSLPKCS12Bundle.swift b/Sources/NIOSSL/SSLPKCS12Bundle.swift index f65e2995..562cf669 100644 --- a/Sources/NIOSSL/SSLPKCS12Bundle.swift +++ b/Sources/NIOSSL/SSLPKCS12Bundle.swift @@ -94,7 +94,7 @@ public struct NIOSSLPKCS12Bundle { guard boringSSLIsInitialized else { fatalError("Failed to initialize BoringSSL") } let p12 = buffer.withUnsafeBytes { pointer -> OpaquePointer? in - let bio = CNIOBoringSSL_BIO_new_mem_buf(UnsafeMutableRawPointer(mutating: pointer.baseAddress), CInt(pointer.count))! + let bio = CNIOBoringSSL_BIO_new_mem_buf(pointer.baseAddress, CInt(pointer.count))! defer { CNIOBoringSSL_BIO_free(bio) } diff --git a/Sources/NIOSSL/SSLPrivateKey.swift b/Sources/NIOSSL/SSLPrivateKey.swift index 8f8593bb..79928bb0 100644 --- a/Sources/NIOSSL/SSLPrivateKey.swift +++ b/Sources/NIOSSL/SSLPrivateKey.swift @@ -166,7 +166,7 @@ public class NIOSSLPrivateKey { /// A delegating initializer for `init(buffer:format:passphraseCallback)` and `init(buffer:format:)`. private convenience init(bytes: [UInt8], format: NIOSSLSerializationFormats, callbackManager: CallbackManagerProtocol?) throws { let ref = bytes.withUnsafeBytes { (ptr) -> UnsafeMutablePointer? in - let bio = CNIOBoringSSL_BIO_new_mem_buf(UnsafeMutableRawPointer(mutating: ptr.baseAddress!), CInt(ptr.count))! + let bio = CNIOBoringSSL_BIO_new_mem_buf(ptr.baseAddress!, CInt(ptr.count))! defer { CNIOBoringSSL_BIO_free(bio) }