Skip to content

Commit 2c2d0fe

Browse files
authored
No bouncy castle (#68)
Remove all of the BouncyCastle code from the base Java classes. There is still some in the test libraries. This was the good work of linuxwolf to start and complete most of it.
1 parent ab0e545 commit 2c2d0fe

33 files changed

Lines changed: 1173 additions & 1294 deletions

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,5 @@ ehthumbs.db
9292
Thumbs.db
9393
Desktop.ini
9494

95-
/nbproject/
95+
/nbproject/
96+
/Examples

pom.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.augustcellars.cose</groupId>
88
<artifactId>cose-java</artifactId>
9-
<version>0.9.6</version>
9+
<version>0.9.7</version>
1010

1111
<name>com.augustcellars.cose:cose-java</name>
1212
<description>A Java implementation that supports the COSE secure message specification.</description>
@@ -98,11 +98,13 @@
9898
<groupId>org.bouncycastle</groupId>
9999
<artifactId>bcprov-jdk15on</artifactId>
100100
<version>1.54</version>
101-
</dependency>
101+
<scope>test</scope>
102+
</dependency>
102103
<dependency>
103104
<groupId>org.bouncycastle</groupId>
104105
<artifactId>bcpkix-jdk15on</artifactId>
105106
<version>1.54</version>
107+
<scope>test</scope>
106108
</dependency>
107109
<dependency>
108110
<groupId>com.upokecenter</groupId>

src/main/java/COSE/ASN1.java

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package COSE;
7+
8+
import java.util.ArrayList;
9+
10+
/**
11+
*
12+
* @author Jim
13+
*/
14+
public class ASN1 {
15+
// 1.2.840.10045.3.1.7
16+
public static final byte[] Oid_secp256r1 = new byte[]{0x06, 0x08, 0x2A, (byte) 0x86, 0x48, (byte) 0xCE, 0x3D, 0x03, 0x01, 0x07};
17+
// 1.3.132.0.34
18+
public static final byte[] Oid_secp384r1 = new byte[]{0x06, 0x05, 0x2B, (byte) 0x81, 0x04, 0x00, 0x22};
19+
// 1.3.132.0.35
20+
public static final byte[] Oid_secp521r1 = new byte[]{0x06, 0x05, 0x2B, (byte) 0x81, 0x04, 0x00, 0x23};
21+
22+
static final byte[] oid_ecPublicKey = new byte[]{0x06, 0x07, 0x2a, (byte) 0x86, 0x48, (byte) 0xce, 0x3d, 0x2, 0x1};
23+
24+
25+
private static final byte[] SequenceX = new byte[]{0x30};
26+
27+
/**
28+
* Encode a subject public key info structure from an oid and the data bytes
29+
* for the key
30+
*
31+
* @param oid - encoded Object Identifier
32+
* @param keyBytes - encoded key bytes
33+
* @return - encoded SPKI
34+
*/
35+
public static byte[] EncodeSubjectPublicKeyInfo(byte[] oid, byte[] keyBytes) throws CoseException
36+
{
37+
// SPKI ::= SEQUENCE {
38+
// algorithm SEQUENCE {
39+
// oid = id-ecPublicKey {1 2 840 10045 2}
40+
// namedCurve = oid for algorithm
41+
// }
42+
// subjectPublicKey BIT STRING CONTAINS key bytes
43+
// }
44+
// }
45+
try {
46+
47+
ArrayList<byte[]> xxx = new ArrayList<byte[]>();
48+
xxx.add(AlgorithmIdentifier(oid_ecPublicKey, oid));
49+
xxx.add(new byte[]{3});
50+
xxx.add(GetLength(keyBytes.length+1));
51+
xxx.add(new byte[]{0});
52+
xxx.add(keyBytes);
53+
54+
return Sequence(xxx);
55+
}
56+
catch (ArrayIndexOutOfBoundsException e) {
57+
System.out.print(e.toString());
58+
throw e;
59+
}
60+
}
61+
62+
public static byte[] EncodePKCS8(byte[] oid, byte[] keyBytes, byte[] spki) throws CoseException
63+
{
64+
// ECPrivateKey ::= SEQUENCE {
65+
// version INTEGER {1}
66+
// privateKey OCTET STRING
67+
// parameters [0] OBJECT IDENTIFIER = named curve
68+
// public key [1] BIT STRING OPTIONAL
69+
// }
70+
//
71+
// PKCS#8 ::= SEQUENCE {
72+
// version INTEGER {0}
73+
// privateKeyALgorithm SEQUENCE {
74+
// algorithm OID,
75+
// parameters ANY
76+
// }
77+
// privateKey ECPrivateKey,
78+
// attributes [0] IMPLICIT Attributes OPTIONAL
79+
// }
80+
81+
try {
82+
ArrayList<byte[]> xxx = new ArrayList<byte[]>();
83+
xxx.add(new byte[]{2, 1, 1});
84+
xxx.add(new byte[]{4});
85+
xxx.add(GetLength(keyBytes.length));
86+
xxx.add(keyBytes);
87+
xxx.add(new byte[]{(byte)0xa0});
88+
xxx.add(GetLength(oid.length));
89+
xxx.add(oid);
90+
if (spki != null) {
91+
xxx.add(new byte[]{(byte)0xa1});
92+
xxx.add(GetLength(spki.length));
93+
xxx.add(spki);
94+
}
95+
96+
byte[] ecPrivateKey = Sequence(xxx);
97+
98+
xxx = new ArrayList<byte[]>();
99+
xxx.add(new byte[]{2, 1, 0});
100+
xxx.add(AlgorithmIdentifier(oid_ecPublicKey, oid));
101+
xxx.add(new byte[]{4});
102+
xxx.add(GetLength(ecPrivateKey.length));
103+
xxx.add(ecPrivateKey);
104+
105+
return Sequence(xxx);
106+
}
107+
catch (ArrayIndexOutOfBoundsException e) {
108+
System.out.print(e.toString());
109+
throw e;
110+
}
111+
}
112+
public static byte[] EncodeSignature(byte[] r, byte[] s) throws CoseException {
113+
ArrayList<byte[]> x = new ArrayList<byte[]>();
114+
x.add(UnsignedInteger(r));
115+
x.add(UnsignedInteger(s));
116+
117+
return Sequence(x);
118+
}
119+
120+
private static byte[] AlgorithmIdentifier(byte[] oid, byte[] params) throws CoseException
121+
{
122+
ArrayList<byte[]> xxx = new ArrayList<byte[]>();
123+
xxx.add(oid);
124+
if (params != null) {
125+
xxx.add(params);
126+
}
127+
return Sequence(xxx);
128+
}
129+
private static byte[] Sequence(ArrayList<byte[]> members) throws CoseException
130+
{
131+
byte[] y = ToBytes(members);
132+
ArrayList<byte[]> x = new ArrayList<byte[]>();
133+
x.add(SequenceX);
134+
x.add(GetLength(y.length));
135+
x.add(y);
136+
137+
return ToBytes(x);
138+
}
139+
private static byte[] UnsignedInteger(byte[] i) throws CoseException {
140+
int pad = 0, offset = 0;
141+
142+
while (offset < i.length && i[offset] == 0) {
143+
offset++;
144+
}
145+
146+
if (offset == i.length) {
147+
return new byte[] {0x02, 0x01, 0x00};
148+
}
149+
if ((i[offset] & 0x80) != 0) {
150+
pad++;
151+
}
152+
int length = i.length - offset;
153+
byte[] der = new byte[2 + length + pad];
154+
der[0] = 0x02;
155+
der[1] = (byte)(length + pad);
156+
System.arraycopy(i, offset, der, 2 + pad, length);
157+
158+
return der;
159+
}
160+
161+
private static byte[] GetLength(int x) throws CoseException
162+
{
163+
if (x <= 127) {
164+
return new byte[]{(byte)x};
165+
}
166+
else if ( x < 256) {
167+
return new byte[]{(byte) 0x81, (byte) x};
168+
}
169+
throw new CoseException("Error in ASN1.GetLength");
170+
}
171+
172+
private static byte[] ToBytes(ArrayList<byte[]> x)
173+
{
174+
int l = 0;
175+
for (byte[] r : x) {
176+
l += r.length;
177+
}
178+
179+
byte[] b = new byte[l];
180+
l = 0;
181+
for (byte[] r : x) {
182+
System.arraycopy(r, 0, b, l, r.length);
183+
l += r.length;
184+
}
185+
186+
return b;
187+
}
188+
189+
190+
}

src/main/java/COSE/CoseException.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,7 @@ public class CoseException extends Exception {
1313
public CoseException(String message) {
1414
super(message);
1515
}
16+
public CoseException(String message, Exception ex) {
17+
super(message, ex);
18+
}
1619
}

src/main/java/COSE/ECPrivateKey.java

Lines changed: 0 additions & 153 deletions
This file was deleted.

0 commit comments

Comments
 (0)