Example #1
0
 @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);
 }
Example #2
0
  @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();
  }
Example #3
0
  @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();
  }
Example #4
0
  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;
  }
Example #5
0
  @Test
  public void encryptDecodeBadRecipients() throws CoseException {
    CBORObject obj = CBORObject.NewArray();
    obj.Add(CBORObject.FromObject(CBORObject.NewArray()).EncodeToBytes());
    obj.Add(CBORObject.NewMap());
    obj.Add(new byte[0]);
    obj.Add(CBORObject.False);

    thrown.expect(CoseException.class);
    thrown.expectMessage("Invalid Encrypt structure");

    byte[] rgb = obj.EncodeToBytes();
    Message msg = Message.DecodeFromBytes(rgb, MessageTag.Encrypt);
  }
Example #6
0
  /** 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);
  }