@Test public void encryptDecodeWrongBasis() throws CoseException { CBORObject obj = CBORObject.NewMap(); thrown.expect(CoseException.class); thrown.expectMessage("Message is not a COSE security Message"); byte[] rgb = obj.EncodeToBytes(); Message msg = Message.DecodeFromBytes(rgb, MessageTag.Encrypt); }
@Before public void setUp() throws CoseException { recipient128 = new Recipient(); recipient128.addAttribute( HeaderKeys.Algorithm, AlgorithmID.Direct.AsCBOR(), Attribute.UNPROTECTED); CBORObject key128 = CBORObject.NewMap(); key128.Add(KeyKeys.KeyType.AsCBOR(), KeyKeys.KeyType_Octet); key128.Add(KeyKeys.Octet_K.AsCBOR(), CBORObject.FromObject(rgbKey128)); cnKey128 = new OneKey(key128); recipient128.SetKey(cnKey128); }
@Test public void encryptDecodeWrongCount() throws CoseException { CBORObject obj = CBORObject.NewArray(); obj.Add(CBORObject.False); thrown.expect(CoseException.class); thrown.expectMessage("Invalid Encrypt structure"); byte[] rgb = obj.EncodeToBytes(); Message msg = Message.DecodeFromBytes(rgb, MessageTag.Encrypt); }
protected CBORObject EncodeCBORObject() throws CoseException { if (rgbTag == null) throw new CoseException("Compute function not called"); CBORObject obj = CBORObject.NewArray(); if (objProtected.size() > 0) obj.Add(objProtected.EncodeToBytes()); else obj.Add(CBORObject.FromObject(new byte[0])); obj.Add(objUnprotected); obj.Add(rgbContent); obj.Add(rgbTag); return obj; }
@Test public void encryptBadIV() throws CoseException, InvalidCipherTextException, Exception { EncryptMessage msg = new EncryptMessage(); msg.addRecipient(recipient128); thrown.expect(CoseException.class); thrown.expectMessage("IV is incorrectly formed"); msg.addAttribute(HeaderKeys.Algorithm, AlgorithmID.AES_GCM_128.AsCBOR(), Attribute.PROTECTED); msg.addAttribute(HeaderKeys.IV, CBORObject.FromObject("IV"), Attribute.UNPROTECTED); msg.SetContent(rgbContent); msg.encrypt(); }
@Test public void encryptUnknownAlgorithm() throws CoseException, InvalidCipherTextException, Exception { EncryptMessage msg = new EncryptMessage(); msg.addRecipient(recipient128); thrown.expect(CoseException.class); thrown.expectMessage("Unknown Algorithm Specified"); msg.addAttribute(HeaderKeys.Algorithm, CBORObject.FromObject("Unknown"), Attribute.PROTECTED); msg.SetContent(rgbContent); msg.encrypt(); }
/** Test of Decrypt method, of class Encrypt0Message. */ @Test public void testRoundTrip() throws Exception { System.out.println("Round Trip"); EncryptMessage msg = new EncryptMessage(); msg.addAttribute(HeaderKeys.Algorithm, AlgorithmID.AES_GCM_128.AsCBOR(), Attribute.PROTECTED); msg.addAttribute(HeaderKeys.IV, CBORObject.FromObject(rgbIV96), Attribute.PROTECTED); msg.SetContent(rgbContent); msg.addRecipient(recipient128); msg.encrypt(); List<Recipient> rList = msg.getRecipientList(); assertEquals(rList.size(), 1); byte[] rgbMsg = msg.EncodeToBytes(); msg = (EncryptMessage) Message.DecodeFromBytes(rgbMsg, MessageTag.Encrypt); Recipient r = msg.getRecipient(0); r.SetKey(cnKey128); byte[] contentNew = msg.decrypt(r); assertArrayEquals(rgbContent, contentNew); }
public void DecodeFromCBORObject(CBORObject obj) throws CoseException { if (obj.size() != 5) throw new CoseException("Invalid MAC structure"); if (obj.get(0).getType() == CBORType.ByteString) { if (obj.get(0).GetByteString().length == 0) objProtected = CBORObject.NewMap(); else objProtected = CBORObject.DecodeFromBytes(obj.get(0).GetByteString()); } else throw new CoseException("Invalid MAC structure"); if (obj.get(1).getType() == CBORType.Map) { objUnprotected = obj.get(1); } else throw new CoseException("Invalid MAC structure"); if (obj.get(2).getType() == CBORType.ByteString) rgbContent = obj.get(2).GetByteString(); else if (!obj.get(2).isNull()) throw new CoseException("Invalid MAC struture"); if (obj.get(3).getType() == CBORType.ByteString) rgbTag = obj.get(3).GetByteString(); else throw new CoseException("Invalid MAC structure"); if (obj.get(4).getType() == CBORType.Array) { for (int i = 0; i < obj.get(4).size(); i++) { Recipient recipient = new Recipient(); recipient.DecodeFromCBORObject(obj.get(4).get(i)); recipientList.add(recipient); } } else throw new CoseException("Invalid MAC structure"); }