Skip to content

Commit ed442a5

Browse files
magnuskkarlssonaaschmid
authored andcommitted
add PKCS1 and PKCS8 support for pem files (#2)
Code of both `createPrivateKeyFromPemPkcs*` methods is copied from http://magnus-k-karlsson.blogspot.com/2018/05/how-to-read-pem-pkcs1-or-pkcs8-encoded.html. Note: It was permitted to use the code by Magnus via email on 4th February 2020 11:29am CET. Additinally he asked for adding him as co-writer. Therefore I committed this code using Magnus as author. Thanks for allowing this! Signed-off-by: Magnus Karlsson <[email protected]>
1 parent 8d4a9a4 commit ed442a5

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ dependencies {
5353
tasks {
5454
withType<JavaCompile> {
5555
options.encoding = "UTF-8"
56-
options.compilerArgs.addAll(listOf("-Xlint:all", "-Werror", "-Xlint:-processing"))
56+
options.compilerArgs.addAll(listOf("-Xlint:all", "-Werror", "-Xlint:-processing", "-XDenableSunApiLintControl"))
5757
}
5858

5959
withType<Jar> {

src/main/java/de/aaschmid/taskwarrior/client/KeyStoreBuilder.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.io.File;
55
import java.io.FileInputStream;
66
import java.io.IOException;
7+
import java.math.BigInteger;
78
import java.nio.file.Files;
89
import java.security.KeyFactory;
910
import java.security.KeyStore;
@@ -19,7 +20,9 @@
1920
import java.security.spec.InvalidKeySpecException;
2021
import java.security.spec.KeySpec;
2122
import java.security.spec.PKCS8EncodedKeySpec;
23+
import java.security.spec.RSAPrivateCrtKeySpec;
2224
import java.util.ArrayList;
25+
import java.util.Base64;
2326
import java.util.List;
2427
import java.util.concurrent.atomic.AtomicInteger;
2528

@@ -135,6 +138,35 @@ private PrivateKey createPrivateKeyFor(File privateKeyFile) {
135138
}
136139
}
137140

141+
@SuppressWarnings("sunapi")
142+
private PrivateKey createPrivateKeyFromPemPkcs1(String privateKeyContent) throws IOException {
143+
try {
144+
byte[] bytes = Base64.getDecoder().decode(privateKeyContent);
145+
146+
sun.security.util.DerInputStream derReader = new sun.security.util.DerInputStream(bytes);
147+
sun.security.util.DerValue[] seq = derReader.getSequence(0);
148+
// skip version seq[0];
149+
BigInteger modulus = seq[1].getBigInteger();
150+
BigInteger publicExp = seq[2].getBigInteger();
151+
BigInteger privateExp = seq[3].getBigInteger();
152+
BigInteger prime1 = seq[4].getBigInteger();
153+
BigInteger prime2 = seq[5].getBigInteger();
154+
BigInteger exp1 = seq[6].getBigInteger();
155+
BigInteger exp2 = seq[7].getBigInteger();
156+
BigInteger crtCoef = seq[8].getBigInteger();
157+
158+
RSAPrivateCrtKeySpec keySpec = new RSAPrivateCrtKeySpec(modulus, publicExp, privateExp, prime1, prime2, exp1, exp2, crtCoef);
159+
return createPrivateKey(privateKeyFile, keySpec);
160+
} catch (Error | Exception e) {
161+
throw new TaskwarriorKeyStoreException("Could not use required but proprietary 'sun.security.util' package on this platform.", e);
162+
}
163+
}
164+
165+
private PrivateKey createPrivateKeyFromPemPkcs8(String privateKeyContent) {
166+
byte[] bytes = Base64.getDecoder().decode(privateKeyContent);
167+
return createPrivateKey(privateKeyFile, new PKCS8EncodedKeySpec(bytes));
168+
}
169+
138170
private PrivateKey createPrivateKeyFromPkcs8Der(byte[] privateKeyBytes) {
139171
return createPrivateKey(privateKeyFile, new PKCS8EncodedKeySpec(privateKeyBytes));
140172
}

0 commit comments

Comments
 (0)