From a831374b7209735a0142cf89f7468780784fc20f Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Mon, 6 Aug 2012 03:46:34 +0200 Subject: [PATCH] Fix handling of improperly-encoded DER signatures to match OpenSSL This always reads variables in DER signatures as positive, even when they are encoded as negative. --- core/src/main/java/com/google/bitcoin/core/ECKey.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/google/bitcoin/core/ECKey.java b/core/src/main/java/com/google/bitcoin/core/ECKey.java index e0a5a1ef..e50ec5d1 100644 --- a/core/src/main/java/com/google/bitcoin/core/ECKey.java +++ b/core/src/main/java/com/google/bitcoin/core/ECKey.java @@ -253,7 +253,10 @@ public class ECKey implements Serializable { DERInteger r = (DERInteger) seq.getObjectAt(0); DERInteger s = (DERInteger) seq.getObjectAt(1); decoder.close(); - return signer.verifySignature(data, r.getValue(), s.getValue()); + // OpenSSL deviates from the DER spec by interpreting these values as unsigned, though they should not be + // Thus, we always use the positive versions. + // See: http://r6.ca/blog/20111119T211504Z.html + return signer.verifySignature(data, r.getPositiveValue(), s.getPositiveValue()); } catch (IOException e) { throw new RuntimeException(e); }