コード例 #1
0
ファイル: Asn1Utils.java プロジェクト: a-zuckut/gateway
  /**
   * Encode an ASN.1 BIT STRING.
   *
   * @param value the value to be encoded
   * @param nbits the number of bits in the bit string
   * @param buf the buffer with space to the left of current position where the value will be
   *     encoded
   * @return the length of the encoded data
   */
  public static int encodeBitString(BitSet value, int nbits, ByteBuffer buf) {
    if (value == null || nbits < value.length()) {
      throw new IllegalArgumentException();
    }

    int pos = buf.position();
    int contentLength = (int) Math.ceil(nbits / 8.0d);
    for (int i = contentLength; i > 0; i--) {
      byte octet = 0;
      for (int j = (i - 1) * 8; j < i * 8; j++) {
        if (value.get(j)) {
          octet |= BIT_STRING_MASK[j % 8];
        }
      }
      pos--;
      buf.put(pos, octet);
    }

    // Write out padding byte (primitive encoding)
    pos--;
    buf.put(pos, (byte) 0);
    contentLength++;

    buf.position(buf.position() - contentLength);
    int headerLength =
        DerUtils.encodeIdAndLength(
            DerId.TagClass.UNIVERSAL,
            DerId.EncodingType.PRIMITIVE,
            ASN1_BIT_STRING_TAG_NUM,
            contentLength,
            buf);
    return headerLength + contentLength;
  }
コード例 #2
0
ファイル: Asn1Utils.java プロジェクト: a-zuckut/gateway
 /**
  * Encode an ASN.1 SEQUENCE.
  *
  * @param contentLength the length of the SEQUENCE
  * @param buf the buffer with space to the left of current position where the value will be
  *     encoded
  * @return the length of the encoded data
  */
 public static int encodeSequence(int contentLength, ByteBuffer buf) {
   int headerLength =
       DerUtils.encodeIdAndLength(
           DerId.TagClass.UNIVERSAL,
           DerId.EncodingType.CONSTRUCTED,
           ASN1_SEQUENCE_TAG_NUM,
           contentLength,
           buf);
   return headerLength + contentLength;
 }
コード例 #3
0
ファイル: Asn1Utils.java プロジェクト: a-zuckut/gateway
 /**
  * Encode an ASN.1 IA5String.
  *
  * @param value the value to be encoded
  * @param buf the buffer with space to the left of current position where the value will be
  *     encoded
  * @return the length of the encoded data
  */
 public static int encodeIA5String(String value, ByteBuffer buf) {
   int pos = buf.position();
   byte[] data = (value == null) ? new byte[0] : value.getBytes();
   for (int i = data.length - 1; i >= 0; i--) {
     pos--;
     buf.put(pos, data[i]);
   }
   buf.position(buf.position() - data.length);
   int headerLength =
       DerUtils.encodeIdAndLength(
           DerId.TagClass.UNIVERSAL,
           DerId.EncodingType.PRIMITIVE,
           ASN1_IA5STRING_TAG_NUM,
           data.length,
           buf);
   return headerLength + data.length;
 }
コード例 #4
0
ファイル: Asn1Utils.java プロジェクト: a-zuckut/gateway
 /**
  * Encode an ASN.1 OCTET STRING.
  *
  * @param octets the octets
  * @param buf the buffer with space to the left of current position where the value will be
  *     encoded
  * @return the length of the encoded data
  */
 public static int encodeOctetString(short[] octets, ByteBuffer buf) {
   if (octets == null) {
     octets = new short[0];
   }
   int pos = buf.position();
   for (int i = octets.length - 1; i >= 0; i--) {
     pos--;
     buf.put(pos, (byte) octets[i]);
   }
   buf.position(buf.position() - octets.length);
   int headerLength =
       DerUtils.encodeIdAndLength(
           DerId.TagClass.UNIVERSAL,
           DerId.EncodingType.PRIMITIVE,
           ASN1_OCTET_STRING_TAG_NUM,
           octets.length,
           buf);
   return headerLength + octets.length;
 }
コード例 #5
0
ファイル: Asn1Utils.java プロジェクト: a-zuckut/gateway
 /**
  * Encode an ASN.1 INTEGER.
  *
  * @param value the value to be encoded
  * @param buf the buffer with space to the left of current position where the value will be
  *     encoded
  * @return the length of the encoded data
  */
 public static int encodeInteger(int value, ByteBuffer buf) {
   int pos = buf.position();
   int contentLength = 0;
   do {
     pos--;
     buf.put(pos, (byte) (value & 0xff));
     value >>>= 8;
     contentLength++;
   } while (value != 0);
   buf.position(buf.position() - contentLength);
   int headerLen =
       DerUtils.encodeIdAndLength(
           DerId.TagClass.UNIVERSAL,
           DerId.EncodingType.PRIMITIVE,
           ASN1_INTEGER_TAG_NUM,
           contentLength,
           buf);
   return headerLen + contentLength;
 }
コード例 #6
0
ファイル: Asn1Utils.java プロジェクト: a-zuckut/gateway
  /**
   * Encode an ASN.1 GeneralizedTime.
   *
   * @param date the date value
   * @param buf the buffer with space to the left of current position where the value will be
   *     encoded
   * @return the length of the encoded data
   */
  public static int encodeGeneralizedTime(Date date, ByteBuffer buf) {
    if (date == null) {
      throw new IllegalArgumentException();
    }

    int pos = buf.position();
    SimpleDateFormat format = new SimpleDateFormat(GENERALIZED_TIME_FORMAT);
    format.setTimeZone(TimeZone.getTimeZone("GMT")); // always use UTC
    String value = format.format(date);
    byte[] data = value.getBytes();
    for (int i = data.length - 1; i >= 0; i--) {
      pos--;
      buf.put(pos, data[i]);
    }
    buf.position(buf.position() - data.length);
    int headerLength =
        DerUtils.encodeIdAndLength(
            DerId.TagClass.UNIVERSAL,
            DerId.EncodingType.PRIMITIVE,
            ASN1_GENERALIZED_TIME_TAG_NUM,
            data.length,
            buf);
    return headerLength + data.length;
  }