|
93 | 93 | import org.bouncycastle.asn1.ASN1Integer; |
94 | 94 | import org.bouncycastle.asn1.DEROctetString; |
95 | 95 | import org.bouncycastle.asn1.DERUTF8String; |
| 96 | +import org.bouncycastle.asn1.DERBitString; |
96 | 97 | import org.bouncycastle.asn1.DLSequence; |
97 | 98 | import org.bouncycastle.asn1.DERTaggedObject; |
98 | 99 | import org.bouncycastle.asn1.ASN1ObjectIdentifier; |
@@ -1089,11 +1090,32 @@ public static void writeRSAPrivateKey(Writer _out, RSAPrivateCrtKey obj, CipherS |
1089 | 1090 | } |
1090 | 1091 |
|
1091 | 1092 | public static void writeECPrivateKey(Writer _out, ECPrivateKey obj, CipherSpec cipher, char[] passwd) throws IOException { |
| 1093 | + writeECPrivateKey(_out, obj, null, null, cipher, passwd); |
| 1094 | + } |
| 1095 | + |
| 1096 | + /** |
| 1097 | + * Writes an EC private key in SEC1 / "EC PRIVATE KEY" PEM format. |
| 1098 | + * When {@code curveOID} and {@code pubKeyBytes} are provided they are |
| 1099 | + * embedded as the optional {@code parameters[0]} and {@code publicKey[1]} |
| 1100 | + * fields so that the PEM can be decoded stand-alone (without external |
| 1101 | + * knowledge of the curve). |
| 1102 | + */ |
| 1103 | + public static void writeECPrivateKey(Writer _out, ECPrivateKey obj, |
| 1104 | + ASN1ObjectIdentifier curveOID, byte[] pubKeyBytes, |
| 1105 | + CipherSpec cipher, char[] passwd) throws IOException { |
1092 | 1106 | assert (obj != null); |
1093 | 1107 | final String PEM_STRING_EC = "EC PRIVATE KEY"; |
1094 | 1108 | BufferedWriter out = makeBuffered(_out); |
1095 | 1109 | final int bitLength = obj.getParams().getOrder().bitLength(); |
1096 | | - org.bouncycastle.asn1.sec.ECPrivateKey keyStruct = new org.bouncycastle.asn1.sec.ECPrivateKey(bitLength, obj.getS()); |
| 1110 | + final org.bouncycastle.asn1.sec.ECPrivateKey keyStruct; |
| 1111 | + if (curveOID != null && pubKeyBytes != null) { |
| 1112 | + keyStruct = new org.bouncycastle.asn1.sec.ECPrivateKey( |
| 1113 | + bitLength, obj.getS(), |
| 1114 | + new DERBitString(pubKeyBytes), |
| 1115 | + curveOID); |
| 1116 | + } else { |
| 1117 | + keyStruct = new org.bouncycastle.asn1.sec.ECPrivateKey(bitLength, obj.getS()); |
| 1118 | + } |
1097 | 1119 | if (cipher != null && passwd != null) { |
1098 | 1120 | writePemEncrypted(out, PEM_STRING_EC, keyStruct.getEncoded(), cipher, passwd); |
1099 | 1121 | } else { |
|
0 commit comments