Java Code Examples for co.nstant.in.cbor.model.ByteString

The following examples show how to use co.nstant.in.cbor.model.ByteString. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: webauthndemo   Source File: PackedAttestationStatement.java    License: Apache License 2.0 6 votes vote down vote up
@Override
DataItem encode() throws CborException {
  Map result = new Map();
  if (attestnCert != null) {
    Array x5c = new Array();
    x5c.add(new ByteString(attestnCert));
    for (byte[] cert : this.caCert) {
      x5c.add(new ByteString(cert));
    }
    result.put(new UnicodeString("x5c"), x5c);
  }
  if (ecdaaKeyId != null) {
    result.put(new UnicodeString("ecdaaKeyId"), new ByteString(ecdaaKeyId));
  }
  result.put(new UnicodeString("sig"), new ByteString(sig));
  result.put(new UnicodeString("alg"), new UnicodeString(alg.toString()));

  return result;
}
 
Example 2
/**
 * Decodes a cbor representation of an AndroidSafetyNetAttestationStatement into the object
 * representation
 *
 * @param attStmt Cbor DataItem representation of the attestation statement to decode
 * @return Decoded AndroidSafetyNetAttestationStatement
 * @throws ResponseException Input was not a valid AndroidSafetyNetAttestationStatement DataItem
 */
public static AndroidSafetyNetAttestationStatement decode(DataItem attStmt)
    throws ResponseException {
  AndroidSafetyNetAttestationStatement result = new AndroidSafetyNetAttestationStatement();
  Map given = (Map) attStmt;
  for (DataItem data : given.getKeys()) {
    if (data instanceof UnicodeString) {
      if (((UnicodeString) data).getString().equals("ver")) {
        UnicodeString version = (UnicodeString) given.get(data);
        result.ver = version.getString();
      } else if (((UnicodeString) data).getString().equals("response")) {
        result.response = ((ByteString) (given.get(data))).getBytes();
      }
    }
  }
  if (result.response == null || result.ver == null)
    throw new ResponseException("Invalid JWT Cbor");
  return result;
}
 
Example 3
Source Project: webauthndemo   Source File: CableRegistrationData.java    License: Apache License 2.0 6 votes vote down vote up
public static CableRegistrationData parseFromCbor(DataItem cborCableData) {
  CableRegistrationData cableData = new CableRegistrationData();

  Map cborMap = (Map) cborCableData;

  for (DataItem data : cborMap.getKeys()) {
    if (data instanceof UnicodeString) {
      switch (((UnicodeString) data).getString()) {
        case "version":
          cableData.versions = new ArrayList<>();
          cableData.versions.add(((UnsignedInteger) cborMap.get(data)).getValue().intValue());
          break;
        case "maxVersion":
          cableData.maxVersion = ((UnsignedInteger) cborMap.get(data)).getValue().intValue();
          break;
        case "authenticatorPublicKey":
          cableData.publicKey = ((ByteString) cborMap.get(data)).getBytes();
          break;
      }
    }
  }
  return cableData;
}
 
Example 4
Source Project: cbor-java   Source File: ByteStringDecoder.java    License: Apache License 2.0 6 votes vote down vote up
private ByteString decodeInfinitiveLength() throws CborException {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    for (;;) {
        DataItem dataItem = decoder.decodeNext();
        if (dataItem == null) {
            throw new CborException("Unexpected end of stream");
        }
        MajorType majorType = dataItem.getMajorType();
        if (Special.BREAK.equals(dataItem)) {
            break;
        } else if (majorType == MajorType.BYTE_STRING) {
            ByteString byteString = (ByteString) dataItem;
            byte[] byteArray = byteString.getBytes();
            if (byteArray != null) {
                bytes.write(byteArray, 0, byteArray.length);
            }
        } else {
            throw new CborException("Unexpected major type " + majorType);
        }
    }
    return new ByteString(bytes.toByteArray());
}
 
Example 5
Source Project: cbor-java   Source File: Example12Test.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
        new byte[] { (byte) 0xc2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 });
    CborDecoder decoder = new CborDecoder(byteArrayInputStream);
    DataItem b = decoder.decodeNext();

    Assert.assertTrue(b.hasTag());
    Tag tag = b.getTag();
    Assert.assertEquals(2, tag.getValue());

    Assert.assertTrue(b instanceof ByteString);
    ByteString byteString = (ByteString) b;
    Assert.assertArrayEquals(new byte[] { (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
        byteString.getBytes());
}
 
Example 6
Source Project: cbor-java   Source File: Example14Test.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
        new byte[] { (byte) 0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 });
    CborDecoder decoder = new CborDecoder(byteArrayInputStream);
    DataItem b = decoder.decodeNext();

    Assert.assertTrue(b.hasTag());
    Tag tag = b.getTag();
    Assert.assertEquals(3, tag.getValue());

    Assert.assertTrue(b instanceof ByteString);
    ByteString byteString = (ByteString) b;
    Assert.assertArrayEquals(new byte[] { (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
        byteString.getBytes());
}
 
Example 7
Source Project: cbor-java   Source File: ByteStringDecoderTest.java    License: Apache License 2.0 6 votes vote down vote up
public void decodingExample() throws CborException {
    byte bytes[] = { 0, 1, 2, 3 };
    // Encode
    ByteArrayOutputStream encodedStream = new ByteArrayOutputStream();
    new CborEncoder(encodedStream).encode(new ByteString(bytes));
    byte encodedBytes[] = encodedStream.toByteArray();
    // Decode
    ByteArrayInputStream inputStream = new ByteArrayInputStream(encodedBytes);
    CborDecoder decoder = new CborDecoder(inputStream);
    DataItem dataItem = decoder.decodeNext();
    assertEquals(MajorType.BYTE_STRING, dataItem.getMajorType());
    ByteString byteString = (ByteString) dataItem;
    byte[] decodedBytes = byteString.getBytes();
    // Verify
    assertEquals(bytes.length, decodedBytes.length);
    assertEquals(bytes[0], decodedBytes[0]);
    assertEquals(bytes[1], decodedBytes[1]);
    assertEquals(bytes[2], decodedBytes[2]);
    assertEquals(bytes[3], decodedBytes[3]);
}
 
Example 8
Source Project: cbor-java   Source File: MapBuilderTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void startMapInMap() {
    CborBuilder builder = new CborBuilder();
    List<DataItem> dataItems = builder.addMap().startMap(new ByteString(new byte[] { 0x01 })).put(1, 2).end()
        .startMap(1).end().startMap("asdf").end().end().build();
    Map rootMap = (Map) dataItems.get(0);
    DataItem keys[] = new DataItem[3];
    rootMap.getKeys().toArray(keys);
    assertEquals(keys[0], new ByteString(new byte[] { 0x01 }));
    assertEquals(keys[1], new UnsignedInteger(1));
    assertEquals(keys[2], new UnicodeString("asdf"));

    assertTrue(rootMap.get(keys[0]) instanceof Map);
    assertTrue(rootMap.get(keys[1]) instanceof Map);
    assertTrue(rootMap.get(keys[2]) instanceof Map);
}
 
Example 9
Source Project: webauthndemo   Source File: RsaKey.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public byte[] encode() throws CborException {
  ByteArrayOutputStream output = new ByteArrayOutputStream();
  List<DataItem> dataItems =
      new CborBuilder().addMap().put(new UnsignedInteger(KTY_LABEL), new UnsignedInteger(kty))
          .put(new UnsignedInteger(ALG_LABEL), new NegativeInteger(alg.encodeToInt()))
          .put(new NegativeInteger(N_LABEL), new ByteString(n))
          .put(new NegativeInteger(E_LABEL), new ByteString(e)).end().build();
  new CborEncoder(output).encode(dataItems);
  return output.toByteArray();
}
 
Example 10
Source Project: webauthndemo   Source File: EccKey.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public byte[] encode() throws CborException {
  ByteArrayOutputStream output = new ByteArrayOutputStream();
  List<DataItem> dataItems =
      new CborBuilder().addMap().put(new UnsignedInteger(KTY_LABEL), new UnsignedInteger(kty))
          .put(new UnsignedInteger(ALG_LABEL), new NegativeInteger(alg.encodeToInt()))
          .put(new NegativeInteger(CRV_LABEL), new UnsignedInteger(crv))
          .put(new NegativeInteger(X_LABEL), new ByteString(x))
          .put(new NegativeInteger(Y_LABEL), new ByteString(y)).end().build();
  new CborEncoder(output).encode(dataItems);
  return output.toByteArray();
}
 
Example 11
Source Project: webauthndemo   Source File: AttestationObject.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @param attestationObject
 * @return AttestationObject created from the provided byte array
 * @throws CborException
 * @throws ResponseException
 */
public static AttestationObject decode(byte[] attestationObject)
    throws CborException, ResponseException {
  AttestationObject result = new AttestationObject();
  List<DataItem> dataItems = CborDecoder.decode(attestationObject);

  if (dataItems.size() == 1 && dataItems.get(0) instanceof Map) {
    DataItem attStmt = null;
    Map attObjMap = (Map) dataItems.get(0);
    for (DataItem key : attObjMap.getKeys()) {
      if (key instanceof UnicodeString) {
        switch(((UnicodeString) key).getString()) {
          case "fmt":
            UnicodeString value = (UnicodeString) attObjMap.get(key);
            result.fmt = value.getString();
            break;
          case "authData":
            byte[] authData = ((ByteString) attObjMap.get(key)).getBytes();
            result.authData = AuthenticatorData.decode(authData);
            break;
          case "attStmt":
            attStmt = attObjMap.get(key);
            break;
            
        }
      }
    }

    if (attStmt != null) {
      result.attStmt = AttestationStatement.decode(result.fmt, attStmt);
    }

  }
  return result;
}
 
Example 12
@Override
DataItem encode() throws CborException {
  Map map = new Map();
  map.put(new UnicodeString("ver"), new UnicodeString(ver));
  map.put(new UnicodeString("response"), new ByteString(response));
  return map;
}
 
Example 13
Source Project: webauthndemo   Source File: FidoU2fAttestationStatement.java    License: Apache License 2.0 5 votes vote down vote up
@Override
DataItem encode() throws CborException {
  Map result = new Map();
  Array x5c = new Array();
  x5c.add(new ByteString(attestnCert));
  for (byte[] cert : this.caCert) {
    x5c.add(new ByteString(cert));
  }
  result.put(new UnicodeString("x5c"), x5c);
  result.put(new UnicodeString("sig"), new ByteString(sig));

  return result;
}
 
Example 14
Source Project: cbor-java   Source File: ByteStringEncoder.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void encode(ByteString byteString) throws CborException {
    byte[] bytes = byteString.getBytes();
    if (byteString.isChunked()) {
        encodeTypeChunked(MajorType.BYTE_STRING);
        if (bytes != null) {
            encode(new ByteString(bytes));
        }
    } else if (bytes == null) {
        encoder.encode(SimpleValue.NULL);
    } else {
        encodeTypeAndLength(MajorType.BYTE_STRING, bytes.length);
        write(bytes);
    }
}
 
Example 15
Source Project: cbor-java   Source File: AbstractEncoder.java    License: Apache License 2.0 5 votes vote down vote up
protected void encodeTypeAndLength(MajorType majorType, BigInteger length) throws CborException {
    boolean negative = majorType == MajorType.NEGATIVE_INTEGER;
    int symbol = majorType.getValue() << 5;
    if (length.compareTo(BigInteger.valueOf(24)) == -1) {
        write(symbol | length.intValue());
    } else if (length.compareTo(BigInteger.valueOf(256)) == -1) {
        symbol |= AdditionalInformation.ONE_BYTE.getValue();
        write((byte) symbol, (byte) length.intValue());
    } else if (length.compareTo(BigInteger.valueOf(65536L)) == -1) {
        symbol |= AdditionalInformation.TWO_BYTES.getValue();
        long twoByteValue = length.longValue();
        write((byte) symbol, (byte) (twoByteValue >> 8), (byte) (twoByteValue & 0xFF));
    } else if (length.compareTo(BigInteger.valueOf(4294967296L)) == -1) {
        symbol |= AdditionalInformation.FOUR_BYTES.getValue();
        long fourByteValue = length.longValue();
        write((byte) symbol, (byte) ((fourByteValue >> 24) & 0xFF), (byte) ((fourByteValue >> 16) & 0xFF),
            (byte) ((fourByteValue >> 8) & 0xFF), (byte) (fourByteValue & 0xFF));
    } else if (length.compareTo(new BigInteger("18446744073709551616")) == -1) {
        symbol |= AdditionalInformation.EIGHT_BYTES.getValue();
        BigInteger mask = BigInteger.valueOf(0xFF);
        write((byte) symbol, length.shiftRight(56).and(mask).byteValue(),
            length.shiftRight(48).and(mask).byteValue(), length.shiftRight(40).and(mask).byteValue(),
            length.shiftRight(32).and(mask).byteValue(), length.shiftRight(24).and(mask).byteValue(),
            length.shiftRight(16).and(mask).byteValue(), length.shiftRight(8).and(mask).byteValue(),
            length.and(mask).byteValue());
    } else {
        if (negative) {
            encoder.encode(new Tag(3));
        } else {
            encoder.encode(new Tag(2));
        }
        encoder.encode(new ByteString(length.toByteArray()));
    }
}
 
Example 16
Source Project: cbor-java   Source File: CborEncoder.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Encode a single {@link DataItem}.
 *
 * @param dataItem the {@link DataItem} to encode. If null, encoder encodes a
 *                 {@link SimpleValue} NULL value.
 * @throws CborException if {@link DataItem} could not be encoded or there was
 *                       an problem with the {@link OutputStream}.
 */
public void encode(DataItem dataItem) throws CborException {
    if (dataItem == null) {
        dataItem = SimpleValue.NULL;
    }

    if (dataItem.hasTag()) {
        Tag tagDi = dataItem.getTag();
        encode(tagDi);
    }

    switch (dataItem.getMajorType()) {
    case UNSIGNED_INTEGER:
        unsignedIntegerEncoder.encode((UnsignedInteger) dataItem);
        break;
    case NEGATIVE_INTEGER:
        negativeIntegerEncoder.encode((NegativeInteger) dataItem);
        break;
    case BYTE_STRING:
        byteStringEncoder.encode((ByteString) dataItem);
        break;
    case UNICODE_STRING:
        unicodeStringEncoder.encode((UnicodeString) dataItem);
        break;
    case ARRAY:
        arrayEncoder.encode((Array) dataItem);
        break;
    case MAP:
        mapEncoder.encode((Map) dataItem);
        break;
    case SPECIAL:
        specialEncoder.encode((Special) dataItem);
        break;
    case TAG:
        tagEncoder.encode((Tag) dataItem);
        break;
    default:
        throw new CborException("Unknown major type");
    }
}
 
Example 17
Source Project: cbor-java   Source File: ByteStringDecoder.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ByteString decode(int initialByte) throws CborException {
    long length = getLength(initialByte);
    if (length == INFINITY) {
        if (decoder.isAutoDecodeInfinitiveByteStrings()) {
            return decodeInfinitiveLength();
        } else {
            ByteString byteString = new ByteString(null);
            byteString.setChunked(true);
            return byteString;
        }
    } else {
        return decodeFixedLength(length);
    }
}
 
Example 18
Source Project: cbor-java   Source File: ByteStringEncoderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEncodeChunkedString() throws CborException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    List<DataItem> dataItems = new CborBuilder().add(new ByteString(null)).add(new ByteString("test".getBytes()))
        .startByteString("test".getBytes()).end().build();
    new CborEncoder(byteArrayOutputStream).encode(dataItems);
}
 
Example 19
Source Project: cbor-java   Source File: AbstractByteStringTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEncode() throws CborException {
    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(byteOutputStream);
    encoder.encode(new ByteString(value));
    Assert.assertArrayEquals(encodedValue, byteOutputStream.toByteArray());
}
 
Example 20
Source Project: cbor-java   Source File: AbstractByteStringTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    InputStream inputStream = new ByteArrayInputStream(encodedValue);
    CborDecoder decoder = new CborDecoder(inputStream);
    DataItem dataItem = decoder.decodeNext();
    Assert.assertTrue(dataItem instanceof ByteString);
    ByteString byteString = (ByteString) dataItem;
    Assert.assertArrayEquals(value, byteString.getBytes());
}
 
Example 21
Source Project: cbor-java   Source File: ArrayBuilderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldAddByteArray() {
    CborBuilder builder = new CborBuilder();
    List<DataItem> dataItems = builder.addArray().add(new byte[] { 0x0 }).end().build();
    assertEquals(1, dataItems.size());
    assertTrue(dataItems.get(0) instanceof Array);
    Array array = (Array) dataItems.get(0);
    assertEquals(1, array.getDataItems().size());
    assertTrue(array.getDataItems().get(0) instanceof ByteString);
}
 
Example 22
Source Project: cbor-java   Source File: CborBuilder.java    License: Apache License 2.0 4 votes vote down vote up
public ByteStringBuilder<CborBuilder> startByteString(byte[] bytes) {
    add(new ByteString(bytes).setChunked(true));
    return new ByteStringBuilder<CborBuilder>(this);
}
 
Example 23
Source Project: cbor-java   Source File: ByteStringDecoder.java    License: Apache License 2.0 4 votes vote down vote up
private ByteString decodeFixedLength(long length) throws CborException {
    return new ByteString(decodeBytes(length));
}
 
Example 24
Source Project: cbor-java   Source File: AbstractBuilder.java    License: Apache License 2.0 4 votes vote down vote up
protected DataItem convert(byte[] bytes) {
    return new ByteString(bytes);
}
 
Example 25
Source Project: cbor-java   Source File: ByteStringEncoderTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void shouldEncodeNullString() throws CborException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    List<DataItem> dataItems = new CborBuilder().add((ByteString) null).build();
    new CborEncoder(byteArrayOutputStream).encode(dataItems);
}
 
Example 26
Source Project: cbor-java   Source File: Example52Test.java    License: Apache License 2.0 4 votes vote down vote up
public Example52Test() {
    DataItem di = new ByteString(new byte[] { 0x64, 0x49, 0x45, 0x54, 0x46 });
    di.setTag(24);

    VALUE = new CborBuilder().add(di).build();
}
 
Example 27
Source Project: cbor-java   Source File: Example51Test.java    License: Apache License 2.0 4 votes vote down vote up
public Example51Test() {
    DataItem di = new ByteString(new byte[] { 0x01, 0x02, 0x03, 0x04 });
    di.setTag(23);

    VALUE = new CborBuilder().add(di).build();
}