1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.pkitesting;
17
18 import org.bouncycastle.asn1.ASN1Encodable;
19 import org.bouncycastle.asn1.ASN1Integer;
20 import org.bouncycastle.asn1.ASN1ObjectIdentifier;
21 import org.bouncycastle.asn1.DEROctetString;
22 import org.bouncycastle.asn1.DERSequence;
23
24 import java.io.IOException;
25 import java.io.UncheckedIOException;
26 import java.lang.reflect.InvocationTargetException;
27 import java.security.PrivateKey;
28 import java.security.spec.AlgorithmParameterSpec;
29 import java.util.Arrays;
30 import javax.security.auth.DestroyFailedException;
31
32
33
34
35
36
37
38
39
40 final class MLDSASeedPrivateKey implements PrivateKey {
41 private static final long serialVersionUID = 4206741400099880395L;
42 private final PrivateKey key;
43 private final byte[] seedFormat;
44
45 MLDSASeedPrivateKey(PrivateKey key, CertificateBuilder.Algorithm algorithm, byte[] seed) {
46 this.key = key;
47
48 try {
49 seedFormat = new DERSequence(new ASN1Encodable[]{
50 new ASN1Integer(0),
51 new DERSequence(new ASN1ObjectIdentifier(Algorithms.oidForAlgorithmName(algorithm.signatureType))),
52 new DEROctetString(seed)
53 }).getEncoded("DER");
54 } catch (IOException e) {
55 throw new UncheckedIOException("Unexpected problem encoding private key DER", e);
56 }
57 }
58
59 public AlgorithmParameterSpec getParams() {
60 try {
61 return (AlgorithmParameterSpec) key.getClass().getMethod("getParams").invoke(key);
62 } catch (Exception e) {
63 throw new UnsupportedOperationException(e);
64 }
65 }
66
67 @Override
68 public String getAlgorithm() {
69 return key.getAlgorithm();
70 }
71
72 @Override
73 public String getFormat() {
74 return key.getFormat();
75 }
76
77 @Override
78 public byte[] getEncoded() {
79 return seedFormat.clone();
80 }
81
82 @Override
83 public void destroy() throws DestroyFailedException {
84 key.destroy();
85 Arrays.fill(seedFormat, (byte) 0);
86 }
87
88 @Override
89 public boolean isDestroyed() {
90 return key.isDestroyed();
91 }
92
93 static PrivateKey unwrap(PrivateKey key) {
94 if (key instanceof MLDSASeedPrivateKey) {
95 return ((MLDSASeedPrivateKey) key).key;
96 }
97 return key;
98 }
99 }