Scoop API

scoop:scoop.crypto:0.1.0

src/x509/certificate.scoop

Selected production source · src/x509/certificate.scoop

1package scoop.crypto.x509
3import scoop.crypto.PublicKey
4import scoop.crypto.native.*
5import scoop.time.Instant
7/**
8 * A DNS or raw IP identity declared by an X.509 certificate.
9 */
10public enum SubjectAlternativeName {
11 /**
12 * A certificate DNS alternative name.
13 */
14 Dns(String),
15 /**
16 * A certificate IP alternative name represented by address bytes.
17 */
18 Ip(Array<Byte>) }
19/**
20 * The expected peer name or address used during certificate validation.
21 */
22public enum PeerIdentity {
23 /**
24 * The expected DNS peer name.
25 */
26 Dns(String),
27 /**
28 * The expected four- or sixteen-byte IP address.
29 */
30 Ip(Array<Byte>) }
32/**
33 * An owned parsed X.509 certificate. Reading or exporting it does not establish trust; use
34 * TrustStore.verify for chain, time, purpose, and optional peer identity checks.
35 */
36public class Certificate internal constructor(private var handle: ULong) : Closeable {
37 /**
38 * The backend's textual representation of the subject name.
39 */
40 public val subject: String get() = textField(0)
41 /**
42 * The backend's textual representation of the issuer name.
43 */
44 public val issuer: String get() = textField(1)
45 /**
46 * The certificate's serial number as text.
47 */
48 public val serialNumber: String get() = textField(2)
49 /**
50 * The beginning of the certificate's validity interval.
51 */
52 public val notBefore: Instant get() = timeField(0)
53 /**
54 * The end of the certificate's validity interval.
55 */
56 public val notAfter: Instant get() = timeField(1)
58 /**
59 * Parses certificate data into owned native handles.
60 */
61 public companion object {
62 /**
63 * Parses PEM input into a new owned Certificate; malformed input reports a cryptographic
64 * backend failure.
65 */
66 public fun fromPem(pem: String): Certificate = importCertificate(pem.toByteArray(), true)
67 /**
68 * Parses DER input into a new owned Certificate; malformed input reports a cryptographic
69 * backend failure.
70 */
71 public fun fromDer(der: List<Byte>): Certificate = importCertificate(der, false)
73 private fun importCertificate(input: List<Byte>, pem: Boolean): Certificate {
74 val bytes = input.toArray()
75 @Unsafe { return bytes.withDataPointer { pointer, count ->
76 @Unsafe { return@withDataPointer Certificate(cryptoValue(certificateImport(if (pem) 1 else 0, pointer, count), "parse certificate")) }
77 } }
78 }
79 }
81 internal fun nativeHandle(): ULong {
82 if (handle == 0UL) throw IllegalStateException(Some("certificate is closed"))
83 return handle
84 }
86 private fun textField(field: Int): String {
87 @Unsafe { return String.fromUtf8Unchecked(cryptoBytes(certificateText(nativeHandle(), field), "certificate field")) }
88 }
90 private fun timeField(field: Int): Instant {
91 @Unsafe { return Instant.fromEpochSeconds(cryptoValue(certificateTime(nativeHandle(), field), "certificate time").toLong()) }
92 }
94 /**
95 * Exports the live Certificate as DER data in an independent managed value.
96 */
97 public fun toDer(): Array<Byte> { @Unsafe { return cryptoBytes(certificateExport(nativeHandle(), 0), "export certificate") } }
98 /**
99 * Exports the live Certificate as PEM data in an independent managed value.
100 */
101 public fun toPem(): String { @Unsafe { return String.fromUtf8Unchecked(cryptoBytes(certificateExport(nativeHandle(), 1), "export certificate")) } }
102 /**
103 * Returns an independently owned public key extracted from this certificate.
104 */
105 public fun publicKey(): PublicKey { @Unsafe { return PublicKey(cryptoValue(certificateKey(nativeHandle()), "certificate public key")) } }
107 /**
108 * Checks only this certificate's signature against the supplied issuer key. It does not
109 * validate a trust chain, validity time, purpose, or peer identity.
110 */
111 public fun verifySignature(issuer: PublicKey): Boolean {
112 @Unsafe { return cryptoValue(certificateSignature(nativeHandle(), publicKeyHandle(issuer)), "certificate signature") == 1UL }
113 }
115 /**
116 * Returns the certificate's DNS and IP subject alternative names.
117 */
118 public fun subjectAlternativeNames(): Array<SubjectAlternativeName> {
119 @Unsafe {
120 val names = cryptoValue(certificateNames(nativeHandle()), "certificate names")
121 try {
122 val output = ArrayList<SubjectAlternativeName>()
123 var index = 0
124 val count = namesCount(names)
125 while (index < count) {
126 when (nameKind(names, index)) {
127 case 0 -> {
128 val bytes = cryptoBytes(nameBytes(names, index), "DNS alternative name")
129 output.add(SubjectAlternativeName.Dns(String.fromUtf8(bytes)))
130 }
131 case 1 -> output.add(SubjectAlternativeName.Ip(cryptoBytes(nameBytes(names, index), "IP alternative name")))
132 else -> { index += 1; continue }
133 }
134 index += 1
135 }
136 return output.toArray()
137 } finally { namesFree(names) }
138 }
139 }
141 /**
142 * Releases the native Certificate handle. Repeated closes are harmless; other operations after
143 * close fail.
144 */
145 public override fun close() {
146 val owned = handle
147 handle = 0UL
148 if (owned != 0UL) @Unsafe { certificateFree(owned) }
149 }