Files
swift-nio-ssl/Sources/NIOSSL/SSLPublicKey.swift
T
Cory Benfield 45b1f82692 Rename everything. (#75)
Motivation:

Unfortunately, it turns out that the word "OpenSSL" just ended up everwhere
in our codebase, including in the names of our types. This was, in hindsight,
a bit of an unfortunate bit of naming.

We should take this breaking opportunity to give these types better names.
In particular, we should stop identifying them as belonging to any library
other than this one, "NIOSSL". That way we become somewhat independent of
the specific way that this library is implemented.

Modifications:

Renamed a bunch of stuff. Internal types mostly got their names changed
from OpenSSL->BoringSSL. External types generally got their names changed
to "NIOSSL", except where those types directly reflected a BoringSSL
implementation detail, specifically in the errors.

The main name changes are:

SSLContext -> NIOSSLContext
OpenSSLError -> BoringSSLError
NIOOpenSSLError -> NIOSSLError
OpenSSLInternalError -> BoringSSLInternalError
OpenSSLPKCS12Bundle -> NIOSSLPKCS12Bundle
OpenSSLPassphraseCallback -> NIOSSLPassphraseCallback
OpenSSLPassphraseSetter -> NIOSSLPassphraseSetter
OpenSSLPrivateKey -> NIOSSLPrivateKey
OpenSSLPublicKey -> NIOSSLPublicKey
OpenSSLCertificate -> NIOSSLCertificate
OpenSSLVerificationResult -> NIOSSLVerificationResult
OpenSSLVerificationCallback -> NIOSSLVerificationCallback
OpenSSLCertificateSource -> NIOSSLCertificateSource
OpenSSLPrivateKeySource -> NIOSSLPrivateKeySource
OpenSSLTrustRoots -> NIOSSLTrustRoots
OpenSSLSerializationFormats -> NIOSSLSerializationFormats
OpenSSLHandler -> NIOSSLHandler
OpenSSLClientHandler -> NIOSSLClientHandler
OpenSSLServerHandler -> NIOSSLServerHandler

Oh, and renamed the module too: it's now NIOSSL, instead of NIOOpenSSL.

Result:

Users have better names for things.
2019-03-01 17:25:33 +00:00

82 lines
3.0 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import CNIOBoringSSL
/// An `NIOSSLPublicKey` is an abstract handle to a public key owned by BoringSSL.
///
/// This object is of minimal utility, as it cannot be used for very many operations
/// in `NIOSSL`. Its primary purpose is to allow extracting public keys from
/// `NIOSSLCertificate` objects to be serialized, so that they can be passed to
/// general-purpose cryptography libraries.
public class NIOSSLPublicKey {
private let ref: UnsafeMutablePointer<EVP_PKEY>
fileprivate init(withOwnedReference ref: UnsafeMutablePointer<EVP_PKEY>) {
self.ref = ref
}
deinit {
CNIOBoringSSL_EVP_PKEY_free(self.ref)
}
}
// MARK:- Helpful initializers
extension NIOSSLPublicKey {
/// Create an `NIOSSLPublicKey` object from an internal `EVP_PKEY` pointer.
///
/// This method expects `pointer` to be passed at +1, and consumes that reference.
///
/// - parameters:
/// - pointer: A pointer to an `EVP_PKEY` structure containing the public key.
/// - returns: An `NIOSSLPublicKey` wrapping the pointer.
internal static func fromInternalPointer(takingOwnership pointer: UnsafeMutablePointer<EVP_PKEY>) -> NIOSSLPublicKey {
return NIOSSLPublicKey(withOwnedReference: pointer)
}
}
extension NIOSSLPublicKey {
/// Extracts the bytes of this public key in the SubjectPublicKeyInfo format.
///
/// The SubjectPublicKeyInfo format is defined in RFC 5280. In addition to the raw key bytes, it also
/// provides an identifier of the algorithm, ensuring that the key can be unambiguously decoded.
///
/// - returns: The DER-encoded SubjectPublicKeyInfo bytes for this public key.
/// - throws: If an error occurred while serializing the key.
public func toSPKIBytes() throws -> [UInt8] {
guard let bio = CNIOBoringSSL_BIO_new(CNIOBoringSSL_BIO_s_mem()) else {
throw NIOSSLError.unableToAllocateBoringSSLObject
}
defer {
CNIOBoringSSL_BIO_free(bio)
}
let rc = CNIOBoringSSL_i2d_PUBKEY_bio(bio, self.ref)
guard rc == 1 else {
let errorStack = BoringSSLError.buildErrorStack()
throw BoringSSLError.unknownError(errorStack)
}
var dataPtr: UnsafeMutablePointer<CChar>? = nil
let length = CNIOBoringSSL_BIO_get_mem_data(bio, &dataPtr)
guard let bytes = dataPtr.map({ UnsafeMutableRawBufferPointer(start: $0, count: length) }) else {
throw NIOSSLError.unableToAllocateBoringSSLObject
}
return Array(bytes)
}
}