Skip to content

Commit

Permalink
Add NIOSSLCertificate serial number var, and add to description (#247)
Browse files Browse the repository at this point in the history
* Add serial number

* Make a lazy var

* PR comments

* PR comments
  • Loading branch information
Davidde94 authored Oct 14, 2020
1 parent 7c403e7 commit 9852198
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
8 changes: 7 additions & 1 deletion Sources/NIOSSL/SSLCertificate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public class NIOSSLCertificate {
case ipv4(in_addr)
case ipv6(in6_addr)
}

public var serialNumber: [UInt8] {
let serialNumber = CNIOBoringSSL_X509_get_serialNumber(self.ref)!
return Array(UnsafeBufferPointer(start: serialNumber.pointee.data, count: Int(serialNumber.pointee.length)))
}

private init(withOwnedReference ref: UnsafeMutablePointer<X509>) {
self._ref = UnsafeMutableRawPointer(ref) // erasing the type for @_implementationOnly import CNIOBoringSSL
Expand Down Expand Up @@ -438,7 +443,8 @@ internal class SubjectAltNameSequence: Sequence, IteratorProtocol {
extension NIOSSLCertificate: CustomStringConvertible {

public var description: String {
var desc = "<NIOSSLCertificate"
let serialNumber = self.serialNumber.map { String($0, radix: 16) }.reduce("", +)
var desc = "<NIOSSLCertificate;serial_number=\(serialNumber)"
if let commonNameBytes = self.commonName() {
let commonName = String(decoding: commonNameBytes, as: UTF8.self)
desc += ";common_name=" + commonName
Expand Down
4 changes: 2 additions & 2 deletions Tests/NIOSSLTests/SSLCertificateTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -382,15 +382,15 @@ class SSLCertificateTest: XCTestCase {
}

func testPrintingDebugDetailsNoAlternativeNames() throws {
let expectedDebugDescription = "<NIOSSLCertificate;common_name=robots.sanfransokyo.edu>"
let expectedDebugDescription = "<NIOSSLCertificate;serial_number=9fd7d05a34ca7984;common_name=robots.sanfransokyo.edu>"
let cert = try assertNoThrowWithValue(NIOSSLCertificate(bytes: .init(samplePemCert.utf8), format: .pem))
let debugString = String(describing: cert)

XCTAssertEqual(debugString, expectedDebugDescription)
}

func testPrintingDebugDetailsWithAlternativeNames() throws {
let expectedDebugDescription = "<NIOSSLCertificate;common_name=localhost;alternative_names=localhost,example.com,192.168.0.1,2001:db8::1>"
let expectedDebugDescription = "<NIOSSLCertificate;serial_number=46231a526848d57af4999e29f89988d178d94da2;common_name=localhost;alternative_names=localhost,example.com,192.168.0.1,2001:db8::1>"
let cert = try assertNoThrowWithValue(NIOSSLCertificate(bytes: .init(multiSanCert.utf8), format: .pem))
let debugString = String(describing: cert)

Expand Down

0 comments on commit 9852198

Please sign in to comment.