scoop:scoop.crypto:0.1.0
src/x509/bundle.scoop
Selected production source · src/x509/bundle.scoop
1
package scoop.crypto.x5093
import scoop.crypto.native.*5
/**6
* Parses a PEM certificate bundle into independently owned certificates. Close every returned7
* certificate after use.8
*/9
public 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 = 019
while (index < count) {20
certificates.add(Certificate(cryptoValue(certificateBundleGet(bundle, index), "retain bundle certificate")))21
index += 122
}23
return certificates.toArray()24
} catch (error: Throwable) {25
for (certificate in certificates) certificate.close()26
throw error27
} finally { certificateBundleFree(bundle) }28
}29
}