summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2018-10-22 11:45:50 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2018-10-22 11:45:50 +0700
commite8c09187bd814f71d502955e340d06a979619748 (patch)
treec5a52ab13e5473705f79a3b05502a38653edb8f1
parentEnv. prop.: Org.BouncyCastle.Asn1.AllowUnsafeInteger (diff)
downloadBouncyCastle.NET-ed25519-e8c09187bd814f71d502955e340d06a979619748.tar.xz
Include public key in EC encoding
-rw-r--r--crypto/src/asn1/sec/ECPrivateKeyStructure.cs11
-rw-r--r--crypto/src/pkcs/PrivateKeyInfoFactory.cs8
-rw-r--r--crypto/src/x509/SubjectPublicKeyInfoFactory.cs4
3 files changed, 8 insertions, 15 deletions
diff --git a/crypto/src/asn1/sec/ECPrivateKeyStructure.cs b/crypto/src/asn1/sec/ECPrivateKeyStructure.cs
index 32e020c0b..aec8e0af0 100644
--- a/crypto/src/asn1/sec/ECPrivateKeyStructure.cs
+++ b/crypto/src/asn1/sec/ECPrivateKeyStructure.cs
@@ -48,17 +48,8 @@ namespace Org.BouncyCastle.Asn1.Sec
         public ECPrivateKeyStructure(
             int         orderBitLength,
             BigInteger  key)
+            : this(orderBitLength, key, null)
         {
-            if (key == null)
-                throw new ArgumentNullException("key");
-            if (orderBitLength < key.BitLength)
-                throw new ArgumentException("must be >= key bitlength", "orderBitLength");
-
-            byte[] bytes = BigIntegers.AsUnsignedByteArray((orderBitLength + 7) / 8, key);
-
-            this.seq = new DerSequence(
-                new DerInteger(1),
-                new DerOctetString(bytes));
         }
 
         [Obsolete("Use constructor which takes 'orderBitLength' instead, to guarantee correct encoding")]
diff --git a/crypto/src/pkcs/PrivateKeyInfoFactory.cs b/crypto/src/pkcs/PrivateKeyInfoFactory.cs
index 69eb3fa67..3036dc8b6 100644
--- a/crypto/src/pkcs/PrivateKeyInfoFactory.cs
+++ b/crypto/src/pkcs/PrivateKeyInfoFactory.cs
@@ -9,6 +9,7 @@ using Org.BouncyCastle.Asn1.Sec;
 using Org.BouncyCastle.Asn1.X509;
 using Org.BouncyCastle.Asn1.X9;
 using Org.BouncyCastle.Crypto;
+using Org.BouncyCastle.Crypto.Generators;
 using Org.BouncyCastle.Crypto.Parameters;
 using Org.BouncyCastle.Math;
 using Org.BouncyCastle.Security;
@@ -117,6 +118,8 @@ namespace Org.BouncyCastle.Pkcs
             if (privateKey is ECPrivateKeyParameters)
             {
                 ECPrivateKeyParameters priv = (ECPrivateKeyParameters)privateKey;
+                DerBitString publicKey = new DerBitString(ECKeyPairGenerator.GetCorrespondingPublicKey(priv).Q.GetEncoded(false));
+
                 ECDomainParameters dp = priv.Parameters;
                 int orderBitLength = dp.N.BitLength;
 
@@ -134,7 +137,7 @@ namespace Org.BouncyCastle.Pkcs
                     algID = new AlgorithmIdentifier(CryptoProObjectIdentifiers.GostR3410x2001, gostParams);
 
                     // TODO Do we need to pass any parameters here?
-                    ec = new ECPrivateKeyStructure(orderBitLength, priv.D);
+                    ec = new ECPrivateKeyStructure(orderBitLength, priv.D, publicKey, null);
                 }
                 else
                 {
@@ -149,8 +152,7 @@ namespace Org.BouncyCastle.Pkcs
                         x962 = new X962Parameters(priv.PublicKeyParamSet);
                     }
 
-                    // TODO Possible to pass the publicKey bitstring here?
-                    ec = new ECPrivateKeyStructure(orderBitLength, priv.D, x962);
+                    ec = new ECPrivateKeyStructure(orderBitLength, priv.D, publicKey, x962);
 
                     algID = new AlgorithmIdentifier(X9ObjectIdentifiers.IdECPublicKey, x962);
                 }
diff --git a/crypto/src/x509/SubjectPublicKeyInfoFactory.cs b/crypto/src/x509/SubjectPublicKeyInfoFactory.cs
index fca5da3f5..2fa8b7a28 100644
--- a/crypto/src/x509/SubjectPublicKeyInfoFactory.cs
+++ b/crypto/src/x509/SubjectPublicKeyInfoFactory.cs
@@ -131,12 +131,12 @@ namespace Org.BouncyCastle.X509
                         x962 = new X962Parameters(_key.PublicKeyParamSet);
                     }
 
-                    Asn1OctetString p = (Asn1OctetString)(new X9ECPoint(_key.Q).ToAsn1Object());
+                    byte[] pubKey = _key.Q.GetEncoded(false);
 
                     AlgorithmIdentifier algID = new AlgorithmIdentifier(
                         X9ObjectIdentifiers.IdECPublicKey, x962.ToAsn1Object());
 
-                    return new SubjectPublicKeyInfo(algID, p.GetOctets());
+                    return new SubjectPublicKeyInfo(algID, pubKey);
                 }
             } // End of EC