Program: 171900
Project: 171900
Buggy source:
/** 
 * When encoded in DER, the signature - holding the  x-coordinate of the elliptic curve point in the value "r" and the  y-coordinate of the elliptic curve point in the value "s"  - becomes the following sequence of bytes (in total 70 bytes instead of 64 bytes): 0x30 b1 0x02 b2 (vr) 0x02 b3 (vs) where: - 0x30 is always the first byte of the DER encoded signature format (ASN.1 tag for sequence) - b1 is a single byte value, encoding the length in bytes of the remaining list of bytes  (from the first 0x02 to the end of the encoding); is a value between 0x44 and 0x46 - 0x02 is a fixed value indicating that an integer value will follow (ASN.1 tag for int) - b2 is a single byte value, encoding the length in bytes of (vr); (either 0x20 (32 bytes) or 0x21 (33 bytes), depending on whether an optional fill byte 0x00 is used as most significant byte) - (vr) is the signed big-endian encoding of the value "r", of minimal length; - 0x02 is a fixed value indicating that an integer value will follow (ASN.1 tag for int) - b3 is a single byte value, encoding the length in bytes of (vs); (either 0x20 (32 bytes) or 0x21 (33 bytes), depending on whether an optional fill byte 0x00 is used as most significant byte) - (vs) is the signed big-endian encoding of the value "s", of minimal length.
 */
private static byte[] getDEREncodedSignature(byte[] signatureValue){
  ByteArrayOutputStream baos=new ByteArrayOutputStream();
  byte[] r=new byte[32];
  byte[] s=new byte[32];
  try {
    System.arraycopy(signatureValue,0,r,0,32);
    System.arraycopy(signatureValue,32,s,0,32);
  }
 catch (  ArrayIndexOutOfBoundsException e) {
    getLogger().error("String_Node_Str",e);
    return new byte[0];
  }
  int neededByteLength=signatureValue.length + 6;
  boolean isFillByteForR=false;
  boolean isFillByteForS=false;
  if (r[0] < 0) {
    isFillByteForR=true;
    neededByteLength+=1;
  }
  if (s[0] < 0) {
    isFillByteForS=true;
    neededByteLength+=1;
  }
  baos.write(0x30);
  baos.write(neededByteLength - 2);
  baos.write(0x02);
  try {
    if (isFillByteForR) {
      baos.write(0x21);
      baos.write(0x00);
      baos.write(r);
    }
 else {
      baos.write(0x20);
      baos.write(r);
    }
  }
 catch (  IOException e) {
    e.printStackTrace();
  }
  baos.write(0x02);
  try {
    if (isFillByteForS) {
      baos.write(0x21);
      baos.write(0x00);
      baos.write(s);
    }
 else {
      baos.write(0x20);
      baos.write(s);
    }
  }
 catch (  IOException e) {
    e.printStackTrace();
  }
  byte[] derEncodedSignature=baos.toByteArray();
  try {
    baos.close();
  }
 catch (  IOException e) {
    e.printStackTrace();
  }
  return derEncodedSignature;
}

Fixed version (ground truth):
/** 
 * When encoded in DER, the signature - holding the  x-coordinate of the elliptic curve point in the value "r" and the  y-coordinate of the elliptic curve point in the value "s"  - becomes the following sequence of bytes (in total somewhere between 68 and 72 bytes instead of 64 bytes): 0x30 len(z) 0x02 len(r) r 0x02 len(s) s where: - 0x30: is always the first byte of the DER encoded signature format (ASN.1 tag for sequence) - len(z): is a single byte value, encoding the length in bytes of the sequence z (remaining list of bytes) (from the first 0x02 to the end of the encoding); is a value between 0x43 and 0x46 - 0x02: is a fixed value indicating that an integer value will follow (ASN.1 tag for int) - len(r): is a single byte value, encoding the length in bytes of r;  Distinguished Encoding Rules (DER)-encoded integers are defined so that they can encode both positive and negative values   (aka signed values). This means that the leftmost bit (aka most-significant bit in big-endian) indicates whether the value  is positive (0) or negative (1). For ECDSA, however, the r and s values are positive integers. So the leftmost bit must be a 0. If it's not, a 0x00   padding byte must be added. Furthermore, DER require that integer values are represented in the shortest byte representation possible. This  effectively prohibits the use of leading zeroes (0x00) if the leftmost bit was not set to 1. So len(r) will either be 0x21 (33 bytes), 0x20 (32 bytes) or less (mostly not less than 0x1F (31 bytes)). Case 31 bytes or less: The leftmost bytes of the raw (non-DER-encoded) r are 0x00 and, according to DER, need to be  removed so that r is DER-encoded in the shortest possible way. Also, the leftmost bit of the  remaining byte array is 0 (-> a positive x-value). Case 32 bytes: What we would normally expect, as the x- and y-coordinates are positive values of 32 bytes length.  The leftmost bit is set to 0 and the leftmost byte is not 0x00. Case 33 bytes: A padding 0x00 byte was added as the most significant (leftmost) byte because the raw (non-DER-encoded) r value had the leftmost bit set to 1, which would result in a negative value.  - r: is the signed big-endian encoding of the value "r", of minimal length; - 0x02: is a fixed value indicating that an integer value will follow (ASN.1 tag for int) - len(s): is a single byte value, encoding the length in bytes of s; (See further explanation of len(r) that applies as well for len(s)) - s: is the signed big-endian encoding of the value "s", of minimal length.
 * @param rawSignatureValue The r and s values (each 32 bytes) of an ECDSA signature, given as a byte array of 64 bytes
 * @return A byte array representing the DER-encoded version of the raw r and s values
 */
private static byte[] getDEREncodedSignature(byte[] rawSignatureValue){
  ByteArrayOutputStream baos=new ByteArrayOutputStream();
  byte[] r=new byte[32];
  byte[] s=new byte[32];
  try {
    System.arraycopy(rawSignatureValue,0,r,0,32);
    System.arraycopy(rawSignatureValue,32,s,0,32);
  }
 catch (  ArrayIndexOutOfBoundsException e) {
    getLogger().error("String_Node_Str",e);
    return new byte[0];
  }
  byte[] rDerEncoded=getDerEncodedSignatureValue(r);
  byte[] sDerEncoded=getDerEncodedSignatureValue(s);
  baos.write(0x30);
  baos.write(rDerEncoded.length + sDerEncoded.length);
  try {
    baos.write(rDerEncoded);
    baos.write(sDerEncoded);
  }
 catch (  IOException e) {
    getLogger().error("String_Node_Str",e);
  }
  byte[] derEncodedSignature=baos.toByteArray();
  try {
    baos.close();
  }
 catch (  IOException e) {
    getLogger().error("String_Node_Str",e);
  }
  if (showSignatureVerificationLog) {
    StringBuilder sb=new StringBuilder();
    sb.append("String_Node_Str").append(System.lineSeparator());
    sb.append("String_Node_Str").append(ByteUtils.toHexString(rawSignatureValue)).append(System.lineSeparator());
    sb.append("String_Node_Str").append(ByteUtils.toHexString(r)).append(System.lineSeparator());
    sb.append("String_Node_Str").append(ByteUtils.toHexString(rDerEncoded)).append(System.lineSeparator());
    sb.append("String_Node_Str").append(ByteUtils.toHexString(s)).append(System.lineSeparator());
    sb.append("String_Node_Str").append(ByteUtils.toHexString(sDerEncoded)).append(System.lineSeparator());
    sb.append("String_Node_Str").append(ByteUtils.toHexString(derEncodedSignature));
    getLogger().debug(sb.toString());
  }
  return derEncodedSignature;
}