Skip to content

Commit 06df712

Browse files
authored
Merge pull request #41 from jimsch/master
Clean up all of the depreciated function calls
2 parents 6bd7a5d + 3e393f1 commit 06df712

13 files changed

+121
-111
lines changed

src/main/java/COSE/OneKey.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,16 @@ public byte[] EncodeToBytes()
203203
return keyMap.EncodeToBytes();
204204
}
205205

206+
/**
207+
* Return the key as a CBOR object
208+
*
209+
* @return
210+
*/
211+
public CBORObject AsCBOR()
212+
{
213+
return keyMap;
214+
}
215+
206216
/**
207217
* Return a java.security.PublicKey that is the same as the OneKey key
208218
*

src/main/java/COSE/Recipient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ public void encrypt() throws CoseException, Exception {
296296
}
297297

298298
public void addRecipient(Recipient recipient) {
299-
if (recipientList == null) recipientList = new ArrayList();
299+
if (recipientList == null) recipientList = new ArrayList<Recipient>();
300300
recipientList.add(recipient);
301301
}
302302

src/main/java/COSE/SignMessage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* @author jimsch
2727
*/
2828
public class SignMessage extends Message {
29-
protected List<Signer> signerList = new ArrayList();
29+
protected List<Signer> signerList = new ArrayList<Signer>();
3030

3131
/**
3232
* Create a signed message object for which the leading tag and the content will be included.

src/test/java/COSE/Encrypt0MessageTest.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ public void tearDown() {
5151
public void testRoundTrip() throws Exception {
5252
System.out.println("Round Trip");
5353
Encrypt0Message msg = new Encrypt0Message();
54-
msg.AddProtected(HeaderKeys.Algorithm, AlgorithmID.AES_GCM_128.AsCBOR());
55-
msg.AddProtected(HeaderKeys.IV, CBORObject.FromObject(rgbIV96));
54+
msg.addAttribute(HeaderKeys.Algorithm, AlgorithmID.AES_GCM_128.AsCBOR(), Attribute.PROTECTED);
55+
msg.addAttribute(HeaderKeys.IV, CBORObject.FromObject(rgbIV96), Attribute.PROTECTED);
5656
msg.SetContent(rgbContent);
5757
msg.encrypt(rgbKey128);
5858
byte[] rgbMsg = msg.EncodeToBytes();
@@ -79,7 +79,7 @@ public void encryptUnknownAlgorithm() throws CoseException, InvalidCipherTextExc
7979

8080
thrown.expect(CoseException.class);
8181
thrown.expectMessage("Unknown Algorithm Specified");
82-
msg.AddProtected(HeaderKeys.Algorithm, CBORObject.FromObject("Unknown"));
82+
msg.addAttribute(HeaderKeys.Algorithm, CBORObject.FromObject("Unknown"), Attribute.PROTECTED);
8383
msg.SetContent(rgbContent);
8484
msg.encrypt(rgbKey128);
8585
}
@@ -90,7 +90,7 @@ public void encryptUnsupportedAlgorithm() throws CoseException, InvalidCipherTex
9090

9191
thrown.expect(CoseException.class);
9292
thrown.expectMessage("Unsupported Algorithm Specified");
93-
msg.AddProtected(HeaderKeys.Algorithm, AlgorithmID.HMAC_SHA_256.AsCBOR());
93+
msg.addAttribute(HeaderKeys.Algorithm, AlgorithmID.HMAC_SHA_256.AsCBOR(), Attribute.PROTECTED);
9494
msg.SetContent(rgbContent);
9595
msg.encrypt(rgbKey128);
9696
}
@@ -101,7 +101,7 @@ public void encryptIncorrectKeySize() throws CoseException, InvalidCipherTextExc
101101

102102
thrown.expect(CoseException.class);
103103
thrown.expectMessage("Incorrect Key Size");
104-
msg.AddProtected(HeaderKeys.Algorithm, AlgorithmID.AES_GCM_128.AsCBOR());
104+
msg.addAttribute(HeaderKeys.Algorithm, AlgorithmID.AES_GCM_128.AsCBOR(), Attribute.PROTECTED);
105105
msg.SetContent(rgbContent);
106106
msg.encrypt(rgbKey256);
107107
}
@@ -111,7 +111,7 @@ public void encryptNullKey() throws CoseException, InvalidCipherTextException {
111111
Encrypt0Message msg = new Encrypt0Message();
112112

113113
thrown.expect(NullPointerException.class);
114-
msg.AddProtected(HeaderKeys.Algorithm, AlgorithmID.AES_GCM_128.AsCBOR());
114+
msg.addAttribute(HeaderKeys.Algorithm, AlgorithmID.AES_GCM_128.AsCBOR(), Attribute.PROTECTED);
115115
msg.SetContent(rgbContent);
116116
msg.encrypt(null);
117117
}
@@ -122,7 +122,7 @@ public void encryptNoContent() throws CoseException, InvalidCipherTextException
122122

123123
thrown.expect(CoseException.class);
124124
thrown.expectMessage("No Content Specified");
125-
msg.AddProtected(HeaderKeys.Algorithm, AlgorithmID.AES_GCM_128.AsCBOR());
125+
msg.addAttribute(HeaderKeys.Algorithm, AlgorithmID.AES_GCM_128.AsCBOR(), Attribute.PROTECTED);
126126
msg.encrypt(rgbKey128);
127127
}
128128

@@ -132,8 +132,8 @@ public void encryptBadIV() throws CoseException, InvalidCipherTextException {
132132

133133
thrown.expect(CoseException.class);
134134
thrown.expectMessage("IV is incorrectly formed");
135-
msg.AddProtected(HeaderKeys.Algorithm, AlgorithmID.AES_GCM_128.AsCBOR());
136-
msg.AddUnprotected(HeaderKeys.IV, CBORObject.FromObject("IV"));
135+
msg.addAttribute(HeaderKeys.Algorithm, AlgorithmID.AES_GCM_128.AsCBOR(), Attribute.PROTECTED);
136+
msg.addAttribute(HeaderKeys.IV, CBORObject.FromObject("IV"), Attribute.UNPROTECTED);
137137
msg.SetContent(rgbContent);
138138
msg.encrypt(rgbKey128);
139139
}
@@ -144,8 +144,8 @@ public void encryptIncorrectIV() throws CoseException, InvalidCipherTextExceptio
144144

145145
thrown.expect(CoseException.class);
146146
thrown.expectMessage("IV size is incorrect");
147-
msg.AddProtected(HeaderKeys.Algorithm, AlgorithmID.AES_GCM_128.AsCBOR());
148-
msg.AddUnprotected(HeaderKeys.IV, rgbIV128);
147+
msg.addAttribute(HeaderKeys.Algorithm, AlgorithmID.AES_GCM_128.AsCBOR(), Attribute.PROTECTED);
148+
msg.addAttribute(HeaderKeys.IV, rgbIV128, Attribute.UNPROTECTED);
149149
msg.SetContent(rgbContent);
150150
msg.encrypt(rgbKey128);
151151
}

src/test/java/COSE/EncryptMessageTest.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,19 @@ public class EncryptMessageTest {
2626
static byte[] rgbIV96 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
2727

2828
Recipient recipient128;
29-
CBORObject cnKey128;
29+
OneKey cnKey128;
3030

3131
@Rule
3232
public ExpectedException thrown = ExpectedException.none();
3333

3434
@Before
3535
public void setUp() throws CoseException {
3636
recipient128 = new Recipient();
37-
recipient128.addAttribute(HeaderKeys.Algorithm, AlgorithmID.Direct.AsCBOR(), Attribute.UnprotectedAttributes);
38-
cnKey128 = CBORObject.NewMap();
39-
cnKey128.Add(KeyKeys.KeyType.AsCBOR(), KeyKeys.KeyType_Octet);
40-
cnKey128.Add(KeyKeys.Octet_K.AsCBOR(), CBORObject.FromObject(rgbKey128));
37+
recipient128.addAttribute(HeaderKeys.Algorithm, AlgorithmID.Direct.AsCBOR(), Attribute.UNPROTECTED);
38+
CBORObject key128 = CBORObject.NewMap();
39+
key128.Add(KeyKeys.KeyType.AsCBOR(), KeyKeys.KeyType_Octet);
40+
key128.Add(KeyKeys.Octet_K.AsCBOR(), CBORObject.FromObject(rgbKey128));
41+
cnKey128 = new OneKey(key128);
4142
recipient128.SetKey(cnKey128);
4243
}
4344

@@ -48,8 +49,8 @@ public void setUp() throws CoseException {
4849
public void testRoundTrip() throws Exception {
4950
System.out.println("Round Trip");
5051
EncryptMessage msg = new EncryptMessage();
51-
msg.AddProtected(HeaderKeys.Algorithm, AlgorithmID.AES_GCM_128.AsCBOR());
52-
msg.AddProtected(HeaderKeys.IV, CBORObject.FromObject(rgbIV96));
52+
msg.addAttribute(HeaderKeys.Algorithm, AlgorithmID.AES_GCM_128.AsCBOR(), Attribute.PROTECTED);
53+
msg.addAttribute(HeaderKeys.IV, CBORObject.FromObject(rgbIV96), Attribute.PROTECTED);
5354
msg.SetContent(rgbContent);
5455
msg.addRecipient(recipient128);
5556
msg.encrypt();
@@ -85,7 +86,7 @@ public void encryptNoRecipients() throws CoseException, InvalidCipherTextExcepti
8586

8687
thrown.expect(CoseException.class);
8788
thrown.expectMessage("No recipients supplied");
88-
msg.AddProtected(HeaderKeys.Algorithm, AlgorithmID.AES_GCM_128.AsCBOR());
89+
msg.addAttribute(HeaderKeys.Algorithm, AlgorithmID.AES_GCM_128.AsCBOR(), Attribute.PROTECTED);
8990
msg.SetContent(rgbContent);
9091
msg.encrypt();
9192
}
@@ -108,7 +109,7 @@ public void encryptUnknownAlgorithm() throws CoseException, InvalidCipherTextExc
108109

109110
thrown.expect(CoseException.class);
110111
thrown.expectMessage("Unknown Algorithm Specified");
111-
msg.AddProtected(HeaderKeys.Algorithm, CBORObject.FromObject("Unknown"));
112+
msg.addAttribute(HeaderKeys.Algorithm, CBORObject.FromObject("Unknown"), Attribute.PROTECTED);
112113
msg.SetContent(rgbContent);
113114
msg.encrypt();
114115
}
@@ -120,7 +121,7 @@ public void encryptUnsupportedAlgorithm() throws CoseException, InvalidCipherTex
120121

121122
thrown.expect(CoseException.class);
122123
thrown.expectMessage("Unsupported Algorithm Specified");
123-
msg.AddProtected(HeaderKeys.Algorithm, AlgorithmID.HMAC_SHA_256.AsCBOR());
124+
msg.addAttribute(HeaderKeys.Algorithm, AlgorithmID.HMAC_SHA_256.AsCBOR(), Attribute.PROTECTED);
124125
msg.SetContent(rgbContent);
125126
msg.encrypt();
126127
}
@@ -132,7 +133,7 @@ public void encryptNoContent() throws CoseException, InvalidCipherTextException,
132133

133134
thrown.expect(CoseException.class);
134135
thrown.expectMessage("No Content Specified");
135-
msg.AddProtected(HeaderKeys.Algorithm, AlgorithmID.AES_GCM_128.AsCBOR());
136+
msg.addAttribute(HeaderKeys.Algorithm, AlgorithmID.AES_GCM_128.AsCBOR(), Attribute.PROTECTED);
136137
msg.encrypt();
137138
}
138139

@@ -143,8 +144,8 @@ public void encryptBadIV() throws CoseException, InvalidCipherTextException, Exc
143144

144145
thrown.expect(CoseException.class);
145146
thrown.expectMessage("IV is incorrectly formed");
146-
msg.AddProtected(HeaderKeys.Algorithm, AlgorithmID.AES_GCM_128.AsCBOR());
147-
msg.AddUnprotected(HeaderKeys.IV, CBORObject.FromObject("IV"));
147+
msg.addAttribute(HeaderKeys.Algorithm, AlgorithmID.AES_GCM_128.AsCBOR(), Attribute.PROTECTED);
148+
msg.addAttribute(HeaderKeys.IV, CBORObject.FromObject("IV"), Attribute.UNPROTECTED);
148149
msg.SetContent(rgbContent);
149150
msg.encrypt();
150151
}
@@ -156,8 +157,8 @@ public void encryptIncorrectIV() throws CoseException, InvalidCipherTextExceptio
156157

157158
thrown.expect(CoseException.class);
158159
thrown.expectMessage("IV size is incorrect");
159-
msg.AddProtected(HeaderKeys.Algorithm, AlgorithmID.AES_GCM_128.AsCBOR());
160-
msg.AddUnprotected(HeaderKeys.IV, rgbIV128);
160+
msg.addAttribute(HeaderKeys.Algorithm, AlgorithmID.AES_GCM_128.AsCBOR(), Attribute.PROTECTED);
161+
msg.addAttribute(HeaderKeys.IV, rgbIV128, Attribute.UNPROTECTED);
161162
msg.SetContent(rgbContent);
162163
msg.encrypt();
163164
}

src/test/java/COSE/MAC0MessageTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void tearDown() {
4848
public void testRoundTrip() throws Exception {
4949
System.out.println("Round Trip");
5050
MAC0Message msg = new MAC0Message();
51-
msg.AddProtected(HeaderKeys.Algorithm, AlgorithmID.HMAC_SHA_256.AsCBOR());
51+
msg.addAttribute(HeaderKeys.Algorithm, AlgorithmID.HMAC_SHA_256.AsCBOR(), Attribute.PROTECTED);
5252
msg.SetContent(rgbContent);
5353
msg.Create(rgbKey256);
5454

@@ -75,7 +75,7 @@ public void macUnknownAlgorithm() throws CoseException, InvalidCipherTextExcepti
7575

7676
thrown.expect(CoseException.class);
7777
thrown.expectMessage("Unknown Algorithm Specified");
78-
msg.AddProtected(HeaderKeys.Algorithm, CBORObject.FromObject("Unknown"));
78+
msg.addAttribute(HeaderKeys.Algorithm, CBORObject.FromObject("Unknown"), Attribute.PROTECTED);
7979
msg.SetContent(rgbContent);
8080
msg.Create(rgbKey256);
8181
}
@@ -86,7 +86,7 @@ public void macUnsupportedAlgorithm() throws CoseException, InvalidCipherTextExc
8686

8787
thrown.expect(CoseException.class);
8888
thrown.expectMessage("Unsupported MAC Algorithm");
89-
msg.AddProtected(HeaderKeys.Algorithm, AlgorithmID.AES_CCM_16_64_256.AsCBOR());
89+
msg.addAttribute(HeaderKeys.Algorithm, AlgorithmID.AES_CCM_16_64_256.AsCBOR(), Attribute.PROTECTED);
9090
msg.SetContent(rgbContent);
9191
msg.Create(rgbKey256);
9292
}
@@ -97,7 +97,7 @@ public void macNoContent() throws CoseException, InvalidCipherTextException, Exc
9797

9898
thrown.expect(CoseException.class);
9999
thrown.expectMessage("No Content Specified");
100-
msg.AddProtected(HeaderKeys.Algorithm, AlgorithmID.HMAC_SHA_256.AsCBOR());
100+
msg.addAttribute(HeaderKeys.Algorithm, AlgorithmID.HMAC_SHA_256.AsCBOR(), Attribute.PROTECTED);
101101
msg.Create(rgbKey256);
102102
}
103103

src/test/java/COSE/MACMessageTest.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class MACMessageTest {
2727
static byte[] rgbContent = {'T', 'h', 'i', 's', ' ', 'i', 's', ' ', 's', 'o', 'm', 'e', ' ', 'c', 'o', 'n', 't', 'e', 'n', 't'};
2828

2929
Recipient recipient256;
30-
CBORObject cnKey256;
30+
OneKey cnKey256;
3131

3232
@Rule
3333
public ExpectedException thrown = ExpectedException.none();
@@ -47,10 +47,11 @@ public static void tearDownClass() {
4747
@Before
4848
public void setUp() throws CoseException {
4949
recipient256 = new Recipient();
50-
recipient256.addAttribute(HeaderKeys.Algorithm, AlgorithmID.Direct.AsCBOR(), Attribute.UnprotectedAttributes);
51-
cnKey256 = CBORObject.NewMap();
52-
cnKey256.Add(KeyKeys.KeyType.AsCBOR(), KeyKeys.KeyType_Octet);
53-
cnKey256.Add(KeyKeys.Octet_K.AsCBOR(), CBORObject.FromObject(rgbKey256));
50+
recipient256.addAttribute(HeaderKeys.Algorithm, AlgorithmID.Direct.AsCBOR(), Attribute.UNPROTECTED);
51+
CBORObject key256 = CBORObject.NewMap();
52+
key256.Add(KeyKeys.KeyType.AsCBOR(), KeyKeys.KeyType_Octet);
53+
key256.Add(KeyKeys.Octet_K.AsCBOR(), CBORObject.FromObject(rgbKey256));
54+
cnKey256 = new OneKey(key256);
5455
recipient256.SetKey(cnKey256);
5556
}
5657

@@ -101,7 +102,7 @@ public void testGetRecipientCount() {
101102
public void testRoundTrip() throws Exception {
102103
System.out.println("Round Trip");
103104
MACMessage msg = new MACMessage();
104-
msg.AddProtected(HeaderKeys.Algorithm, AlgorithmID.HMAC_SHA_256.AsCBOR());
105+
msg.addAttribute(HeaderKeys.Algorithm, AlgorithmID.HMAC_SHA_256.AsCBOR(), Attribute.PROTECTED);
105106
msg.SetContent(rgbContent);
106107
msg.addRecipient(recipient256);
107108
msg.Create();
@@ -121,7 +122,7 @@ public void macNoRecipients() throws CoseException, InvalidCipherTextException,
121122

122123
thrown.expect(CoseException.class);
123124
thrown.expectMessage("No recipients supplied");
124-
msg.AddProtected(HeaderKeys.Algorithm, AlgorithmID.HMAC_SHA_256.AsCBOR());
125+
msg.addAttribute(HeaderKeys.Algorithm, AlgorithmID.HMAC_SHA_256.AsCBOR(), Attribute.PROTECTED);
125126
msg.SetContent(rgbContent);
126127
msg.Create();
127128
}
@@ -144,7 +145,7 @@ public void macUnknownAlgorithm() throws CoseException, InvalidCipherTextExcepti
144145

145146
thrown.expect(CoseException.class);
146147
thrown.expectMessage("Unknown Algorithm Specified");
147-
msg.AddProtected(HeaderKeys.Algorithm, CBORObject.FromObject("Unknown"));
148+
msg.addAttribute(HeaderKeys.Algorithm, CBORObject.FromObject("Unknown"), Attribute.PROTECTED);
148149
msg.SetContent(rgbContent);
149150
msg.Create();
150151
}
@@ -156,7 +157,7 @@ public void macUnsupportedAlgorithm() throws CoseException, InvalidCipherTextExc
156157

157158
thrown.expect(CoseException.class);
158159
thrown.expectMessage("Unsupported MAC Algorithm");
159-
msg.AddProtected(HeaderKeys.Algorithm, AlgorithmID.AES_CCM_16_64_256.AsCBOR());
160+
msg.addAttribute(HeaderKeys.Algorithm, AlgorithmID.AES_CCM_16_64_256.AsCBOR(), Attribute.PROTECTED);
160161
msg.SetContent(rgbContent);
161162
msg.Create();
162163
}
@@ -168,7 +169,7 @@ public void macNoContent() throws CoseException, InvalidCipherTextException, Exc
168169

169170
thrown.expect(CoseException.class);
170171
thrown.expectMessage("No Content Specified");
171-
msg.AddProtected(HeaderKeys.Algorithm, AlgorithmID.HMAC_SHA_256.AsCBOR());
172+
msg.addAttribute(HeaderKeys.Algorithm, AlgorithmID.HMAC_SHA_256.AsCBOR(), Attribute.PROTECTED);
172173
msg.Create();
173174
}
174175

src/test/java/COSE/MessageTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public void tearDown() {
5252
@Test
5353
public void testDecodeUnknown() throws Exception {
5454
Encrypt0Message msg = new Encrypt0Message(false, true);
55-
msg.AddProtected(HeaderKeys.Algorithm, AlgorithmID.AES_GCM_128.AsCBOR());
56-
msg.AddProtected(HeaderKeys.IV, CBORObject.FromObject(rgbIV96));
55+
msg.addAttribute(HeaderKeys.Algorithm, AlgorithmID.AES_GCM_128.AsCBOR(), Attribute.PROTECTED);
56+
msg.addAttribute(HeaderKeys.IV, CBORObject.FromObject(rgbIV96), Attribute.PROTECTED);
5757
msg.SetContent(rgbContent);
5858
msg.encrypt(rgbKey128);
5959
byte[] rgbMsg = msg.EncodeToBytes();
@@ -71,8 +71,8 @@ public void testDecodeUnknown() throws Exception {
7171
@Test
7272
public void testDecodeFromBytes_byteArr_MessageTag() throws Exception {
7373
Encrypt0Message msg = new Encrypt0Message(true, false);
74-
msg.AddProtected(HeaderKeys.Algorithm, AlgorithmID.AES_GCM_128.AsCBOR());
75-
msg.AddProtected(HeaderKeys.IV, CBORObject.FromObject(rgbIV96));
74+
msg.addAttribute(HeaderKeys.Algorithm, AlgorithmID.AES_GCM_128.AsCBOR(), Attribute.PROTECTED);
75+
msg.addAttribute(HeaderKeys.IV, CBORObject.FromObject(rgbIV96), Attribute.PROTECTED);
7676
msg.SetContent(rgbContent);
7777
msg.encrypt(rgbKey128);
7878
byte[] rgbMsg = msg.EncodeToBytes();

0 commit comments

Comments
 (0)