|
|
@ -22,6 +22,7 @@ import org.bouncycastle.util.io.pem.PemReader |
|
|
|
import org.bouncycastle.util.io.pem.PemWriter |
|
|
|
import org.bouncycastle.util.io.pem.PemWriter |
|
|
|
import org.openrs2.util.io.useAtomicBufferedWriter |
|
|
|
import org.openrs2.util.io.useAtomicBufferedWriter |
|
|
|
import java.io.IOException |
|
|
|
import java.io.IOException |
|
|
|
|
|
|
|
import java.io.Reader |
|
|
|
import java.math.BigInteger |
|
|
|
import java.math.BigInteger |
|
|
|
import java.nio.file.Files |
|
|
|
import java.nio.file.Files |
|
|
|
import java.nio.file.Path |
|
|
|
import java.nio.file.Path |
|
|
@ -133,7 +134,13 @@ public object Rsa { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public fun readPublicKey(path: Path): RSAKeyParameters { |
|
|
|
public fun readPublicKey(path: Path): RSAKeyParameters { |
|
|
|
val der = readSinglePemObject(path, PUBLIC_KEY) |
|
|
|
Files.newBufferedReader(path).use { reader -> |
|
|
|
|
|
|
|
return readPublicKey(reader) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public fun readPublicKey(reader: Reader): RSAKeyParameters { |
|
|
|
|
|
|
|
val der = readSinglePemObject(reader, PUBLIC_KEY) |
|
|
|
|
|
|
|
|
|
|
|
val spki = SubjectPublicKeyInfo.getInstance(der) |
|
|
|
val spki = SubjectPublicKeyInfo.getInstance(der) |
|
|
|
validateAlgorithm(spki.algorithm) |
|
|
|
validateAlgorithm(spki.algorithm) |
|
|
@ -148,7 +155,13 @@ public object Rsa { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public fun readPrivateKey(path: Path): RSAPrivateCrtKeyParameters { |
|
|
|
public fun readPrivateKey(path: Path): RSAPrivateCrtKeyParameters { |
|
|
|
val der = readSinglePemObject(path, PRIVATE_KEY) |
|
|
|
Files.newBufferedReader(path).use { reader -> |
|
|
|
|
|
|
|
return readPrivateKey(reader) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public fun readPrivateKey(reader: Reader): RSAPrivateCrtKeyParameters { |
|
|
|
|
|
|
|
val der = readSinglePemObject(reader, PRIVATE_KEY) |
|
|
|
|
|
|
|
|
|
|
|
val pki = PrivateKeyInfo.getInstance(der) |
|
|
|
val pki = PrivateKeyInfo.getInstance(der) |
|
|
|
validateAlgorithm(pki.privateKeyAlgorithm) |
|
|
|
validateAlgorithm(pki.privateKeyAlgorithm) |
|
|
@ -181,10 +194,11 @@ public object Rsa { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun readSinglePemObject(path: Path, type: String): ByteArray { |
|
|
|
private fun readSinglePemObject(reader: Reader, type: String): ByteArray { |
|
|
|
PemReader(Files.newBufferedReader(path)).use { |
|
|
|
val pemReader = PemReader(reader) |
|
|
|
val obj = it.readPemObject() |
|
|
|
|
|
|
|
if (obj == null || obj.type != type || it.readPemObject() != null) { |
|
|
|
val obj = pemReader.readPemObject() |
|
|
|
|
|
|
|
if (obj == null || obj.type != type || pemReader.readPemObject() != null) { |
|
|
|
throw IOException("Expecting single $type PEM object") |
|
|
|
throw IOException("Expecting single $type PEM object") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -194,7 +208,6 @@ public object Rsa { |
|
|
|
|
|
|
|
|
|
|
|
return obj.content |
|
|
|
return obj.content |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun writeSinglePemObject(path: Path, type: String, content: ByteArray) { |
|
|
|
private fun writeSinglePemObject(path: Path, type: String, content: ByteArray) { |
|
|
|
path.useAtomicBufferedWriter { writer -> |
|
|
|
path.useAtomicBufferedWriter { writer -> |
|
|
|