Skip to content

Commit 85bcf2b

Browse files
committed
Add generic private key output to openssl
1 parent f030027 commit 85bcf2b

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

crypto/src/openssl/MiscPemGenerator.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,7 @@ private static PemObject CreatePemObject(object obj)
9797
AsymmetricKeyParameter akp = (AsymmetricKeyParameter) obj;
9898
if (akp.IsPrivate)
9999
{
100-
string keyType;
101-
encoding = EncodePrivateKey(akp, out keyType);
102-
103-
type = keyType + " PRIVATE KEY";
100+
encoding = EncodePrivateKey(akp, out type);
104101
}
105102
else
106103
{
@@ -174,10 +171,7 @@ private static PemObject CreatePemObject(
174171
AsymmetricKeyParameter akp = (AsymmetricKeyParameter) obj;
175172
if (akp.IsPrivate)
176173
{
177-
string keyType;
178-
keyData = EncodePrivateKey(akp, out keyType);
179-
180-
type = keyType + " PRIVATE KEY";
174+
keyData = EncodePrivateKey(akp, out type);
181175
}
182176
}
183177

@@ -221,7 +215,7 @@ private static byte[] EncodePrivateKey(
221215

222216
if (oid.Equals(X9ObjectIdentifiers.IdDsa))
223217
{
224-
keyType = "DSA";
218+
keyType = "DSA PRIVATE KEY";
225219

226220
DsaParameter p = DsaParameter.GetInstance(algID.Parameters);
227221

@@ -240,19 +234,23 @@ private static byte[] EncodePrivateKey(
240234

241235
if (oid.Equals(PkcsObjectIdentifiers.RsaEncryption))
242236
{
243-
keyType = "RSA";
237+
keyType = "RSA PRIVATE KEY";
238+
239+
return info.ParsePrivateKey().GetEncoded();
244240
}
245241
else if (oid.Equals(CryptoProObjectIdentifiers.GostR3410x2001)
246242
|| oid.Equals(X9ObjectIdentifiers.IdECPublicKey))
247243
{
248-
keyType = "EC";
244+
keyType = "EC PRIVATE KEY";
245+
246+
return info.ParsePrivateKey().GetEncoded();
249247
}
250248
else
251249
{
252-
throw new ArgumentException("Cannot handle private key of type: " + Platform.GetTypeName(akp), "akp");
253-
}
250+
keyType = "PRIVATE KEY";
254251

255-
return info.ParsePrivateKey().GetEncoded();
252+
return info.GetEncoded();
253+
}
256254
}
257255

258256
public PemObject Generate()

0 commit comments

Comments
 (0)