Skip to content

Commit

Permalink
Merge pull request #45 from jimsch/master
Browse files Browse the repository at this point in the history
Need to have the ASN.1 implementations for the keys as well
  • Loading branch information
jimsch authored Dec 17, 2016
2 parents 8ac0aa3 + f2f14ed commit e6173ab
Show file tree
Hide file tree
Showing 6 changed files with 318 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ To add this library to a Maven project, add the following to the `dependencies`
<dependency>
<groupId>com.augustcellars.cose</groupId>
<artifactId>cose-java</artifactId>
<version>0.9.3</version>
<version>0.9.4</version>
</dependency>
```

Expand Down
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.augustcellars.cose</groupId>
<artifactId>cose-java</artifactId>
<version>0.9.4-snapshot</version>
<version>0.9.4</version>

<name>com.augustcellars.cose:cose-java</name>
<description>A Java implementation that supports the COSE secure message specification.</description>
Expand Down Expand Up @@ -99,6 +99,11 @@
<artifactId>bcprov-jdk15on</artifactId>
<version>1.54</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<version>1.54</version>
</dependency>
<dependency>
<groupId>com.upokecenter</groupId>
<artifactId>cbor</artifactId>
Expand Down
40 changes: 33 additions & 7 deletions src/main/java/COSE/ECPrivateKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@
package COSE;

import com.upokecenter.cbor.CBORType;
import java.io.IOException;
import java.io.StringWriter;
import java.math.BigInteger;
import java.security.spec.ECField;
import java.security.spec.ECFieldFp;
import java.security.spec.ECParameterSpec;
import java.security.spec.ECPoint;
import java.security.spec.EllipticCurve;
import org.bouncycastle.asn1.nist.NISTObjectIdentifiers;
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.asn1.x9.X9ECParameters;
import org.bouncycastle.crypto.params.ECDomainParameters;
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;

/**
*
Expand All @@ -23,25 +32,31 @@ public class ECPrivateKey implements java.security.interfaces.ECPrivateKey {
String algorithm;
ECParameterSpec ecParameterSpec;
BigInteger privateKey;
byte[] encodedKey;

public ECPrivateKey(OneKey oneKey) throws CoseException
public ECPrivateKey(OneKey oneKey) throws CoseException, IOException
{
X9ECParameters p = oneKey.GetCurve();
org.bouncycastle.math.ec.ECPoint pubPoint;
ECDomainParameters parameters = new ECDomainParameters(p.getCurve(), p.getG(), p.getN(), p.getH());

/*
if (oneKey.get(KeyKeys.EC2_Y).getType()== CBORType.Boolean) {
byte[] X = oneKey.get(KeyKeys.EC2_X.AsCBOR()).GetByteString();
byte[] rgb = new byte[X.length + 1];
System.arraycopy(X, 0, rgb, 1, X.length);
rgb[0] = (byte) (2 + (oneKey.get(KeyKeys.EC2_Y).AsBoolean() ? 1 : 0));
org.bouncycastle.math.ec.ECPoint pubPoint;
pubPoint = p.getCurve().decodePoint(rgb);
point = new ECPoint(point.getAffineX(), point.getAffineY());
}
else {
point = new ECPoint(new BigInteger(1, oneKey.get(KeyKeys.EC2_X).GetByteString()), new BigInteger(1, oneKey.get(KeyKeys.EC2_Y).GetByteString()));
}
*/
pubPoint = p.getCurve().createPoint(new BigInteger(1, oneKey.get(KeyKeys.EC2_X).GetByteString()), new BigInteger(1, oneKey.get(KeyKeys.EC2_Y).GetByteString()));
}

ECPublicKeyParameters pub = new ECPublicKeyParameters(pubPoint, parameters);
ECPrivateKeyParameters priv = new ECPrivateKeyParameters(new BigInteger(1, oneKey.get(KeyKeys.EC2_D.AsCBOR()).GetByteString()), parameters);

/*
switch (AlgorithmID.FromCBOR(oneKey.get(KeyKeys.Algorithm))) {
case ECDH_ES_HKDF_256:
case ECDH_ES_HKDF_512:
Expand Down Expand Up @@ -71,13 +86,24 @@ public ECPrivateKey(OneKey oneKey) throws CoseException
default:
throw new CoseException("No algorithm specified");
}
*/
algorithm = "EC";

privateKey = new BigInteger(1, oneKey.get(KeyKeys.EC2_D).GetByteString());

ECField field = new ECFieldFp(p.getCurve().getField().getCharacteristic());
EllipticCurve crv = new EllipticCurve(field, p.getCurve().getA().toBigInteger(), p.getCurve().getB().toBigInteger());
ECPoint pt = new ECPoint(p.getG().getRawXCoord().toBigInteger(), p.getG().getRawYCoord().toBigInteger());
ecParameterSpec = new ECParameterSpec(crv, pt, p.getN(), p.getH().intValue());


AlgorithmIdentifier alg = new AlgorithmIdentifier(org.bouncycastle.asn1.x9.X9Curve.id_ecPublicKey, org.bouncycastle.asn1.nist.NISTNamedCurves.getOID("P-256"));

org.bouncycastle.asn1.sec.ECPrivateKey asnPrivate = new org.bouncycastle.asn1.sec.ECPrivateKey(256, privateKey);
byte[] x = asnPrivate.getEncoded();

PrivateKeyInfo asnPrivateX = new PrivateKeyInfo(alg, asnPrivate);
encodedKey = asnPrivateX.getEncoded();
}


Expand All @@ -93,12 +119,12 @@ public String getAlgorithm() {

@Override
public String getFormat() {
return null;
return "PKCS#8";
}

@Override
public byte[] getEncoded() {
return null;
return encodedKey;
}

@Override
Expand Down
37 changes: 28 additions & 9 deletions src/main/java/COSE/ECPublicKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@
package COSE;

import com.upokecenter.cbor.*;
import java.io.IOException;
import java.math.BigInteger;
import java.security.PublicKey;
import java.security.spec.ECParameterSpec;
import java.security.spec.ECPoint;
import java.security.spec.EllipticCurve;
import java.security.spec.ECField;
import java.security.spec.ECFieldFp;
import org.bouncycastle.asn1.ASN1OctetString;
import org.bouncycastle.asn1.DEROctetString;
import org.bouncycastle.asn1.eac.ECDSAPublicKey;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.asn1.x9.X9ECParameters;

/**
Expand All @@ -23,22 +29,29 @@ public class ECPublicKey implements java.security.interfaces.ECPublicKey {
ECPoint point;
String algorithm;
ECParameterSpec ecParameterSpec;
byte[] spkiEncoded;

public ECPublicKey(OneKey oneKey) throws CoseException
public ECPublicKey(OneKey oneKey) throws CoseException, IOException
{
X9ECParameters p = oneKey.GetCurve();
byte [] rgbKey;
byte[] X = oneKey.get(KeyKeys.EC2_X).GetByteString();

if (oneKey.get(KeyKeys.EC2_Y).getType()== CBORType.Boolean) {
byte[] X = oneKey.get(KeyKeys.EC2_X.AsCBOR()).GetByteString();
byte[] rgb = new byte[X.length + 1];
System.arraycopy(X, 0, rgb, 1, X.length);
rgb[0] = (byte) (2 + (oneKey.get(KeyKeys.EC2_Y).AsBoolean() ? 1 : 0));
rgbKey = new byte[X.length + 1];
System.arraycopy(X, 0, rgbKey, 1, X.length);
rgbKey[0] = (byte) (2 + (oneKey.get(KeyKeys.EC2_Y).AsBoolean() ? 1 : 0));
org.bouncycastle.math.ec.ECPoint pubPoint;
pubPoint = p.getCurve().decodePoint(rgb);
pubPoint = p.getCurve().decodePoint(rgbKey);
point = new ECPoint(point.getAffineX(), point.getAffineY());
}
else {
point = new ECPoint(new BigInteger(1, oneKey.get(KeyKeys.EC2_X).GetByteString()), new BigInteger(1, oneKey.get(KeyKeys.EC2_Y).GetByteString()));
rgbKey = new byte[X.length*2+1];
System.arraycopy(X, 0,rgbKey, 1, X.length);
byte[] Y = oneKey.get(KeyKeys.EC2_Y).GetByteString();
System.arraycopy(Y, 0, rgbKey, 1+X.length, X.length);
rgbKey[0] = 4;
point = new ECPoint(new BigInteger(1, X), new BigInteger(1, oneKey.get(KeyKeys.EC2_Y).GetByteString()));
}

/*
Expand Down Expand Up @@ -80,6 +93,12 @@ public ECPublicKey(OneKey oneKey) throws CoseException
EllipticCurve crv = new EllipticCurve(field, p.getCurve().getA().toBigInteger(), p.getCurve().getB().toBigInteger());
ECPoint pt = new ECPoint(p.getG().getRawXCoord().toBigInteger(), p.getG().getRawYCoord().toBigInteger());
ecParameterSpec = new ECParameterSpec(crv, pt, p.getN(), p.getH().intValue());


AlgorithmIdentifier alg = new AlgorithmIdentifier(org.bouncycastle.asn1.x9.X9Curve.id_ecPublicKey, org.bouncycastle.asn1.nist.NISTNamedCurves.getOID("P-256"));
SubjectPublicKeyInfo spki = new SubjectPublicKeyInfo(alg, rgbKey);
spkiEncoded = spki.getEncoded();

}

@Override
Expand All @@ -94,12 +113,12 @@ public String getAlgorithm() {

@Override
public String getFormat() {
return null;
return "X.509";
}

@Override
public byte[] getEncoded() {
return null;
return spkiEncoded;
}

@Override
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/COSE/OneKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package COSE;

import com.upokecenter.cbor.*;
import java.io.IOException;
import java.security.PrivateKey;
import java.security.PublicKey;
import org.bouncycastle.asn1.nist.NISTNamedCurves;
Expand Down Expand Up @@ -223,7 +224,12 @@ public PublicKey AsPublicKey() throws CoseException
{
if (get(KeyKeys.KeyType).equals(KeyKeys.KeyType_EC2))
{
return new ECPublicKey(this);
try {
return new ECPublicKey(this);
}
catch (IOException e) {
throw new CoseException("Internal Error encoding the key");
}
}
throw new CoseException("Cannot convert key as key type is not converted");
}
Expand All @@ -238,7 +244,11 @@ public PrivateKey AsPrivateKey() throws CoseException
{
if (get(KeyKeys.KeyType).equals(KeyKeys.KeyType_EC2))
{
return new ECPrivateKey(this);
try {
return new ECPrivateKey(this);
} catch (IOException ex) {
throw new CoseException("Internal error encoding the key");
}
}
throw new CoseException("Cannot convert key as key type is not converted");
}
Expand Down
Loading

0 comments on commit e6173ab

Please sign in to comment.