[tor-commits] [metrics-lib/master] Handle unrecognized crypto blocks in server descriptors.

karsten at torproject.org karsten at torproject.org
Fri May 29 09:37:38 UTC 2015


commit a8d9e1beef3d0412684d4932354e6dd973bc7cc3
Author: Karsten Loesing <karsten.loesing at gmx.net>
Date:   Fri May 29 11:34:06 2015 +0200

    Handle unrecognized crypto blocks in server descriptors.
    
    This bug was triggered by #16227, but only because that was the first
    newly introduced crypto block that we don't recognize yet.
---
 .../descriptor/impl/ServerDescriptorImpl.java      |   35 ++++++++++------
 .../descriptor/impl/ServerDescriptorImplTest.java  |   42 ++++++++++++++++++++
 2 files changed, 64 insertions(+), 13 deletions(-)

diff --git a/src/org/torproject/descriptor/impl/ServerDescriptorImpl.java b/src/org/torproject/descriptor/impl/ServerDescriptorImpl.java
index f9a1e48..cf24904 100644
--- a/src/org/torproject/descriptor/impl/ServerDescriptorImpl.java
+++ b/src/org/torproject/descriptor/impl/ServerDescriptorImpl.java
@@ -66,7 +66,7 @@ public class ServerDescriptorImpl extends DescriptorImpl
     Scanner s = new Scanner(new String(this.rawDescriptorBytes)).
         useDelimiter("\n");
     String nextCrypto = null;
-    StringBuilder crypto = null;
+    List<String> cryptoLines = null;
     while (s.hasNext()) {
       String line = s.next();
       if (line.startsWith("@")) {
@@ -134,25 +134,34 @@ public class ServerDescriptorImpl extends DescriptorImpl
       } else if (keyword.equals("ntor-onion-key")) {
         this.parseNtorOnionKeyLine(line, lineNoOpt, partsNoOpt);
       } else if (line.startsWith("-----BEGIN")) {
-        crypto = new StringBuilder();
-        crypto.append(line + "\n");
+        cryptoLines = new ArrayList<String>();
+        cryptoLines.add(line);
       } else if (line.startsWith("-----END")) {
-        crypto.append(line + "\n");
-        String cryptoString = crypto.toString();
-        crypto = null;
-        if (nextCrypto.equals("onion-key")) {
+        cryptoLines.add(line);
+        StringBuilder sb = new StringBuilder();
+        for (String cryptoLine : cryptoLines) {
+          sb.append("\n" + cryptoLine);
+        }
+        String cryptoString = sb.toString().substring(1);
+        if ("onion-key".equals(nextCrypto)) {
           this.onionKey = cryptoString;
-        } else if (nextCrypto.equals("signing-key")) {
+        } else if ("signing-key".equals(nextCrypto)) {
           this.signingKey = cryptoString;
-        } else if (nextCrypto.equals("router-signature")) {
+        } else if ("router-signature".equals(nextCrypto)) {
           this.routerSignature = cryptoString;
-        } else {
+        } else if (this.failUnrecognizedDescriptorLines) {
           throw new DescriptorParseException("Unrecognized crypto "
-              + "block in server descriptor.");
+              + "block '" + cryptoString + "' in server descriptor.");
+        } else {
+          if (this.unrecognizedLines == null) {
+            this.unrecognizedLines = new ArrayList<String>();
+          }
+          this.unrecognizedLines.addAll(cryptoLines);
         }
+        cryptoLines = null;
         nextCrypto = null;
-      } else if (crypto != null) {
-        crypto.append(line + "\n");
+      } else if (cryptoLines != null) {
+        cryptoLines.add(line);
       } else {
         ParseHelper.parseKeyword(line, partsNoOpt[0]);
         if (this.failUnrecognizedDescriptorLines) {
diff --git a/test/org/torproject/descriptor/impl/ServerDescriptorImplTest.java b/test/org/torproject/descriptor/impl/ServerDescriptorImplTest.java
index a3329be..b593e21 100644
--- a/test/org/torproject/descriptor/impl/ServerDescriptorImplTest.java
+++ b/test/org/torproject/descriptor/impl/ServerDescriptorImplTest.java
@@ -1309,5 +1309,47 @@ public class ServerDescriptorImplTest {
     unrecognizedLines.add(unrecognizedLine);
     assertEquals(unrecognizedLines, descriptor.getUnrecognizedLines());
   }
+
+  @Test()
+  public void testSomeOtherKey() throws DescriptorParseException {
+    List<String> unrecognizedLines = new ArrayList<String>();
+    unrecognizedLines.add("some-other-key");
+    unrecognizedLines.add("-----BEGIN RSA PUBLIC KEY-----");
+    unrecognizedLines.add("MIGJAoGBAKM+iiHhO6eHsvd6Xjws9z9EQB1V/Bpuy5ciGJ"
+        + "1U4V9SeiKooSo5BpPL");
+    unrecognizedLines.add("o3XT+6PIgzl3R6uycjS3Ejk47vLEJdcVTm/VG6E0ppu3ol"
+        + "IynCI4QryfCEuC3cTF");
+    unrecognizedLines.add("9wE4WXY4nX7w0RTN18UVLxrt1A9PP0cobFNiPs9rzJCbKF"
+        + "facOkpAgMBAAE=");
+    unrecognizedLines.add("-----END RSA PUBLIC KEY-----");
+    StringBuilder sb = new StringBuilder();
+    for (String line : unrecognizedLines) {
+      sb.append("\n" + line);
+    }
+    ServerDescriptor descriptor = DescriptorBuilder.
+        createWithUnrecognizedLine(sb.toString().substring(1), false);
+    assertEquals(unrecognizedLines, descriptor.getUnrecognizedLines());
+  }
+
+  @Test()
+  public void testUnrecognizedCryptoBlockNoKeyword()
+      throws DescriptorParseException {
+    List<String> unrecognizedLines = new ArrayList<String>();
+    unrecognizedLines.add("-----BEGIN RSA PUBLIC KEY-----");
+    unrecognizedLines.add("MIGJAoGBAKM+iiHhO6eHsvd6Xjws9z9EQB1V/Bpuy5ciGJ"
+        + "1U4V9SeiKooSo5BpPL");
+    unrecognizedLines.add("o3XT+6PIgzl3R6uycjS3Ejk47vLEJdcVTm/VG6E0ppu3ol"
+        + "IynCI4QryfCEuC3cTF");
+    unrecognizedLines.add("9wE4WXY4nX7w0RTN18UVLxrt1A9PP0cobFNiPs9rzJCbKF"
+        + "facOkpAgMBAAE=");
+    unrecognizedLines.add("-----END RSA PUBLIC KEY-----");
+    StringBuilder sb = new StringBuilder();
+    for (String line : unrecognizedLines) {
+      sb.append("\n" + line);
+    }
+    ServerDescriptor descriptor = DescriptorBuilder.
+        createWithUnrecognizedLine(sb.toString().substring(1), false);
+    assertEquals(unrecognizedLines, descriptor.getUnrecognizedLines());
+  }
 }
 



More information about the tor-commits mailing list