66package COSE ;
77
88import com .upokecenter .cbor .CBORType ;
9+ import java .io .IOException ;
10+ import java .io .StringWriter ;
911import java .math .BigInteger ;
1012import java .security .spec .ECField ;
1113import java .security .spec .ECFieldFp ;
1214import java .security .spec .ECParameterSpec ;
1315import java .security .spec .ECPoint ;
1416import java .security .spec .EllipticCurve ;
17+ import org .bouncycastle .asn1 .nist .NISTObjectIdentifiers ;
18+ import org .bouncycastle .asn1 .pkcs .PKCSObjectIdentifiers ;
19+ import org .bouncycastle .asn1 .pkcs .PrivateKeyInfo ;
1520import org .bouncycastle .asn1 .x9 .X9ECParameters ;
21+ import org .bouncycastle .crypto .params .ECDomainParameters ;
22+ import org .bouncycastle .crypto .params .ECPrivateKeyParameters ;
23+ import org .bouncycastle .crypto .params .ECPublicKeyParameters ;
24+ import org .bouncycastle .asn1 .x509 .AlgorithmIdentifier ;
1625
1726/**
1827 *
@@ -23,25 +32,31 @@ public class ECPrivateKey implements java.security.interfaces.ECPrivateKey {
2332 String algorithm ;
2433 ECParameterSpec ecParameterSpec ;
2534 BigInteger privateKey ;
35+ byte [] encodedKey ;
2636
27- public ECPrivateKey (OneKey oneKey ) throws CoseException
37+ public ECPrivateKey (OneKey oneKey ) throws CoseException , IOException
2838 {
2939 X9ECParameters p = oneKey .GetCurve ();
40+ org .bouncycastle .math .ec .ECPoint pubPoint ;
41+ ECDomainParameters parameters = new ECDomainParameters (p .getCurve (), p .getG (), p .getN (), p .getH ());
3042
31- /*
3243 if (oneKey .get (KeyKeys .EC2_Y ).getType ()== CBORType .Boolean ) {
3344 byte [] X = oneKey .get (KeyKeys .EC2_X .AsCBOR ()).GetByteString ();
3445 byte [] rgb = new byte [X .length + 1 ];
3546 System .arraycopy (X , 0 , rgb , 1 , X .length );
3647 rgb [0 ] = (byte ) (2 + (oneKey .get (KeyKeys .EC2_Y ).AsBoolean () ? 1 : 0 ));
37- org.bouncycastle.math.ec.ECPoint pubPoint;
3848 pubPoint = p .getCurve ().decodePoint (rgb );
3949 point = new ECPoint (point .getAffineX (), point .getAffineY ());
4050 }
4151 else {
4252 point = new ECPoint (new BigInteger (1 , oneKey .get (KeyKeys .EC2_X ).GetByteString ()), new BigInteger (1 , oneKey .get (KeyKeys .EC2_Y ).GetByteString ()));
43- }
44- */
53+ pubPoint = p .getCurve ().createPoint (new BigInteger (1 , oneKey .get (KeyKeys .EC2_X ).GetByteString ()), new BigInteger (1 , oneKey .get (KeyKeys .EC2_Y ).GetByteString ()));
54+ }
55+
56+ ECPublicKeyParameters pub = new ECPublicKeyParameters (pubPoint , parameters );
57+ ECPrivateKeyParameters priv = new ECPrivateKeyParameters (new BigInteger (1 , oneKey .get (KeyKeys .EC2_D .AsCBOR ()).GetByteString ()), parameters );
58+
59+ /*
4560 switch (AlgorithmID.FromCBOR(oneKey.get(KeyKeys.Algorithm))) {
4661 case ECDH_ES_HKDF_256:
4762 case ECDH_ES_HKDF_512:
@@ -71,13 +86,24 @@ public ECPrivateKey(OneKey oneKey) throws CoseException
7186 default:
7287 throw new CoseException("No algorithm specified");
7388 }
89+ */
90+ algorithm = "EC" ;
7491
7592 privateKey = new BigInteger (1 , oneKey .get (KeyKeys .EC2_D ).GetByteString ());
7693
7794 ECField field = new ECFieldFp (p .getCurve ().getField ().getCharacteristic ());
7895 EllipticCurve crv = new EllipticCurve (field , p .getCurve ().getA ().toBigInteger (), p .getCurve ().getB ().toBigInteger ());
7996 ECPoint pt = new ECPoint (p .getG ().getRawXCoord ().toBigInteger (), p .getG ().getRawYCoord ().toBigInteger ());
8097 ecParameterSpec = new ECParameterSpec (crv , pt , p .getN (), p .getH ().intValue ());
98+
99+
100+ AlgorithmIdentifier alg = new AlgorithmIdentifier (org .bouncycastle .asn1 .x9 .X9Curve .id_ecPublicKey , org .bouncycastle .asn1 .nist .NISTNamedCurves .getOID ("P-256" ));
101+
102+ org .bouncycastle .asn1 .sec .ECPrivateKey asnPrivate = new org .bouncycastle .asn1 .sec .ECPrivateKey (256 , privateKey );
103+ byte [] x = asnPrivate .getEncoded ();
104+
105+ PrivateKeyInfo asnPrivateX = new PrivateKeyInfo (alg , asnPrivate );
106+ encodedKey = asnPrivateX .getEncoded ();
81107 }
82108
83109
@@ -93,12 +119,12 @@ public String getAlgorithm() {
93119
94120 @ Override
95121 public String getFormat () {
96- return null ;
122+ return "PKCS#8" ;
97123 }
98124
99125 @ Override
100126 public byte [] getEncoded () {
101- return null ;
127+ return encodedKey ;
102128 }
103129
104130 @ Override
0 commit comments