|
8 | 8 | import java.util.Collections; |
9 | 9 | import java.util.List; |
10 | 10 |
|
| 11 | +import org.bouncycastle.asn1.ASN1BitString; |
11 | 12 | import org.bouncycastle.asn1.ASN1EncodableVector; |
12 | 13 | import org.bouncycastle.asn1.ASN1Encoding; |
13 | 14 | import org.bouncycastle.asn1.ASN1ObjectIdentifier; |
|
16 | 17 | import org.bouncycastle.asn1.pkcs.PrivateKeyInfo; |
17 | 18 | import org.bouncycastle.asn1.sec.ECPrivateKey; |
18 | 19 | import org.bouncycastle.asn1.x509.AlgorithmIdentifier; |
19 | | -import org.bouncycastle.asn1.x9.ECNamedCurveTable; |
20 | 20 | import org.bouncycastle.internal.asn1.iana.IANAObjectIdentifiers; |
21 | 21 | import org.bouncycastle.internal.asn1.misc.MiscObjectIdentifiers; |
22 | 22 | import org.bouncycastle.jcajce.interfaces.MLDSAPrivateKey; |
@@ -271,20 +271,38 @@ public byte[] getEncoded() |
271 | 271 | { |
272 | 272 | try |
273 | 273 | { |
274 | | - byte[] mldsaKey = ((MLDSAPrivateKey)keys.get(0)).getSeed(); |
275 | | - PrivateKeyInfo pki = PrivateKeyInfo.getInstance(keys.get(1).getEncoded()); |
276 | | - byte[] tradKey = pki.getPrivateKey().getOctets(); |
277 | | - if (keys.get(1).getAlgorithm().contains("Ed")) |
| 274 | + PrivateKey key0 = keys.get(0); |
| 275 | + PrivateKey key1 = keys.get(1); |
| 276 | + |
| 277 | + byte[] mldsaKey = ((MLDSAPrivateKey)key0).getSeed(); |
| 278 | + |
| 279 | + byte[] key1Encoded = key1.getEncoded(); |
| 280 | + PrivateKeyInfo pki = PrivateKeyInfo.getInstance(key1Encoded); |
| 281 | + |
| 282 | + byte[] tradKey; |
| 283 | + String key1Algorithm = key1.getAlgorithm(); |
| 284 | + if (key1Algorithm.contains("Ed")) |
278 | 285 | { |
279 | | - tradKey = ASN1OctetString.getInstance(tradKey).getOctets(); |
| 286 | + tradKey = ASN1OctetString.getInstance(pki.parsePrivateKey()).getOctets(); |
280 | 287 | } |
281 | | - else if (keys.get(1).getAlgorithm().contains("EC")) |
| 288 | + else if (key1Algorithm.contains("EC")) |
282 | 289 | { |
283 | | - ECPrivateKey ecPrivateKey = ECPrivateKey.getInstance(tradKey); |
| 290 | + ECPrivateKey ecPrivateKey = ECPrivateKey.getInstance(pki.parsePrivateKey()); |
| 291 | + |
| 292 | + // TODO Do we also want to remove any parameters from the ECPrivateKey? |
| 293 | + ASN1BitString publicKey = ecPrivateKey.getPublicKey(); |
| 294 | + if (publicKey != null) |
| 295 | + { |
| 296 | + ecPrivateKey = new ECPrivateKey(ecPrivateKey.getPrivateKey(), ecPrivateKey.getParametersObject(), null); |
| 297 | + } |
284 | 298 |
|
285 | | - tradKey = new ECPrivateKey(ECNamedCurveTable.getByOID( |
286 | | - ASN1ObjectIdentifier.getInstance(ecPrivateKey.getParametersObject())).getCurve().getFieldSize(), ecPrivateKey.getKey(), ecPrivateKey.getParametersObject()).getEncoded(); |
| 299 | + tradKey = ecPrivateKey.getEncoded(); |
287 | 300 | } |
| 301 | + else |
| 302 | + { |
| 303 | + tradKey = pki.getPrivateKey().getOctets(); |
| 304 | + } |
| 305 | + |
288 | 306 | return new PrivateKeyInfo(algorithmIdentifier, Arrays.concatenate(mldsaKey, tradKey)).getEncoded(); |
289 | 307 | } |
290 | 308 | catch (IOException e) |
|
0 commit comments