[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [metrics-lib/master] Parse crypto parts in server descriptors.



commit 0d880af304a7e265598470ba86323993e71fefa9
Author: Karsten Loesing <karsten.loesing@xxxxxxx>
Date:   Fri Mar 30 13:16:55 2012 +0200

    Parse crypto parts in server descriptors.
---
 .../torproject/descriptor/ServerDescriptor.java    |   15 ++++++
 .../descriptor/impl/ServerDescriptorImpl.java      |   53 +++++++++++++++++---
 2 files changed, 60 insertions(+), 8 deletions(-)

diff --git a/src/org/torproject/descriptor/ServerDescriptor.java b/src/org/torproject/descriptor/ServerDescriptor.java
index d641d49..b6bc9de 100644
--- a/src/org/torproject/descriptor/ServerDescriptor.java
+++ b/src/org/torproject/descriptor/ServerDescriptor.java
@@ -64,10 +64,25 @@ public interface ServerDescriptor extends Descriptor {
    * if the descriptor does not contain an uptime line. */
   public int getUptime();
 
+  /* Return the onion key in PEM format, or null if the descriptor
+   * doesn't contain a signing key (which is the case in sanitized bridge
+   * descriptors). */
+  public String getOnionKey();
+
+  /* Return the signing key in PEM format, or null if the descriptor
+   * doesn't contain a signing key (which is the case in sanitized bridge
+   * descriptors). */
+  public String getSigningKey();
+
   /* Return the relay's exit policy consisting of one or more accept or
    * reject lines. */
   public List<String> getExitPolicyLines();
 
+  /* Return the signature of the PKCS1-padded server descriptor digest, or
+   * null if the descriptor doesn't contain a signature (which is the case
+   * in sanitized bridge descriptors). */
+  public String getRouterSignature();
+
   /* Return the contact information for this relay, or null if no contact
    * information is included in the descriptor. */
   public String getContact();
diff --git a/src/org/torproject/descriptor/impl/ServerDescriptorImpl.java b/src/org/torproject/descriptor/impl/ServerDescriptorImpl.java
index cd41e8c..5fc6ca1 100644
--- a/src/org/torproject/descriptor/impl/ServerDescriptorImpl.java
+++ b/src/org/torproject/descriptor/impl/ServerDescriptorImpl.java
@@ -71,8 +71,8 @@ public class ServerDescriptorImpl extends DescriptorImpl
     try {
       BufferedReader br = new BufferedReader(new StringReader(
           new String(this.rawDescriptorBytes)));
-      String line;
-      boolean skipCrypto = false;
+      String line, nextCrypto = null;
+      StringBuilder crypto = null;
       while ((line = br.readLine()) != null) {
         if (line.startsWith("@")) {
           continue;
@@ -99,14 +99,17 @@ public class ServerDescriptorImpl extends DescriptorImpl
           this.parseUptimeLine(line, lineNoOpt, partsNoOpt);
         } else if (keyword.equals("onion-key")) {
           this.parseOnionKeyLine(line, lineNoOpt, partsNoOpt);
+          nextCrypto = "onion-key";
         } else if (keyword.equals("signing-key")) {
           this.parseSigningKeyLine(line, lineNoOpt, partsNoOpt);
+          nextCrypto = "signing-key";
         } else if (keyword.equals("accept")) {
           this.parseAcceptLine(line, lineNoOpt, partsNoOpt);
         } else if (keyword.equals("reject")) {
           this.parseRejectLine(line, lineNoOpt, partsNoOpt);
         } else if (keyword.equals("router-signature")) {
           this.parseRouterSignatureLine(line, lineNoOpt, partsNoOpt);
+          nextCrypto = "router-signature";
         } else if (keyword.equals("contact")) {
           this.parseContactLine(line, lineNoOpt, partsNoOpt);
         } else if (keyword.equals("family")) {
@@ -128,10 +131,26 @@ public class ServerDescriptorImpl extends DescriptorImpl
         } else if (keyword.equals("allow-single-hop-exits")) {
           this.parseAllowSingleHopExitsLine(line, lineNoOpt, partsNoOpt);
         } else if (line.startsWith("-----BEGIN")) {
-          skipCrypto = true;
+          crypto = new StringBuilder();
+          crypto.append(line + "\n");
         } else if (line.startsWith("-----END")) {
-          skipCrypto = false;
-        } else if (!skipCrypto) {
+          crypto.append(line + "\n");
+          String cryptoString = crypto.toString();
+          crypto = null;
+          if (nextCrypto.equals("onion-key")) {
+            this.onionKey = cryptoString;
+          } else if (nextCrypto.equals("signing-key")) {
+            this.signingKey = cryptoString;
+          } else if (nextCrypto.equals("router-signature")) {
+            this.routerSignature = cryptoString;
+          } else {
+            throw new DescriptorParseException("Unrecognized crypto "
+                + "block in server descriptor.");
+          }
+          nextCrypto = null;
+        } else if (crypto != null) {
+          crypto.append(line + "\n");
+        } else {
           if (this.failUnrecognizedDescriptorLines) {
             throw new DescriptorParseException("Unrecognized line '"
                 + line + "' in server descriptor.");
@@ -259,12 +278,16 @@ public class ServerDescriptorImpl extends DescriptorImpl
 
   private void parseOnionKeyLine(String line, String lineNoOpt,
       String[] partsNoOpt) throws DescriptorParseException {
-    /* Not parsing crypto parts (yet). */
+    if (!lineNoOpt.equals("onion-key")) {
+      throw new DescriptorParseException("Illegal line '" + line + "'.");
+    }
   }
 
   private void parseSigningKeyLine(String line, String lineNoOpt,
       String[] partsNoOpt) throws DescriptorParseException {
-    /* Not parsing crypto parts (yet). */
+    if (!lineNoOpt.equals("signing-key")) {
+      throw new DescriptorParseException("Illegal line '" + line + "'.");
+    }
   }
 
   private void parseAcceptLine(String line, String lineNoOpt,
@@ -291,7 +314,6 @@ public class ServerDescriptorImpl extends DescriptorImpl
     if (!lineNoOpt.equals("router-signature")) {
       throw new DescriptorParseException("Illegal line '" + line + "'.");
     }
-    /* Not parsing crypto parts (yet). */
   }
 
   private void parseContactLine(String line, String lineNoOpt,
@@ -524,11 +546,26 @@ public class ServerDescriptorImpl extends DescriptorImpl
     return this.uptime;
   }
 
+  private String onionKey;
+  public String getOnionKey() {
+    return this.onionKey;
+  }
+
+  private String signingKey;
+  public String getSigningKey() {
+    return this.signingKey;
+  }
+
   private List<String> exitPolicyLines = new ArrayList<String>();
   public List<String> getExitPolicyLines() {
     return new ArrayList<String>(this.exitPolicyLines);
   }
 
+  private String routerSignature;
+  public String getRouterSignature() {
+    return this.routerSignature;
+  }
+
   private String contact;
   public String getContact() {
     return this.contact;

_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits