Skip to content

Commit 9852198

Browse files
authored
Add NIOSSLCertificate serial number var, and add to description (#247)
* Add serial number * Make a lazy var * PR comments * PR comments
1 parent 7c403e7 commit 9852198

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

Sources/NIOSSL/SSLCertificate.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ public class NIOSSLCertificate {
4646
case ipv4(in_addr)
4747
case ipv6(in6_addr)
4848
}
49+
50+
public var serialNumber: [UInt8] {
51+
let serialNumber = CNIOBoringSSL_X509_get_serialNumber(self.ref)!
52+
return Array(UnsafeBufferPointer(start: serialNumber.pointee.data, count: Int(serialNumber.pointee.length)))
53+
}
4954

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

440445
public var description: String {
441-
var desc = "<NIOSSLCertificate"
446+
let serialNumber = self.serialNumber.map { String($0, radix: 16) }.reduce("", +)
447+
var desc = "<NIOSSLCertificate;serial_number=\(serialNumber)"
442448
if let commonNameBytes = self.commonName() {
443449
let commonName = String(decoding: commonNameBytes, as: UTF8.self)
444450
desc += ";common_name=" + commonName

Tests/NIOSSLTests/SSLCertificateTest.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,15 +382,15 @@ class SSLCertificateTest: XCTestCase {
382382
}
383383

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

389389
XCTAssertEqual(debugString, expectedDebugDescription)
390390
}
391391

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

0 commit comments

Comments
 (0)