Scoop API

scoop:scoop.crypto:0.1.0

src/x509/bundle.scoop

Selected production source · src/x509/bundle.scoop

1package scoop.crypto.x509
3import scoop.crypto.native.*
5/**
6 * Parses a PEM certificate bundle into independently owned certificates. Close every returned
7 * certificate after use.
8 */
9public fun parseCertificatesPem(text: String): Array<Certificate> {
10 val bytes = text.toByteArray()
11 @Unsafe {
12 val bundle = bytes.withDataPointer { pointer, count ->
13 @Unsafe { return@withDataPointer cryptoValue(certificateBundleParse(pointer, count), "parse certificate bundle") }
14 }
15 val certificates = ArrayList<Certificate>()
16 try {
17 val count = certificateBundleCount(bundle)
18 var index = 0
19 while (index < count) {
20 certificates.add(Certificate(cryptoValue(certificateBundleGet(bundle, index), "retain bundle certificate")))
21 index += 1
22 }
23 return certificates.toArray()
24 } catch (error: Throwable) {
25 for (certificate in certificates) certificate.close()
26 throw error
27 } finally { certificateBundleFree(bundle) }
28 }