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

[tor-commits] [metrics-lib/master] Parse crypto parts in network status votes.



commit 1413dfb124729a3c41dd1a9ac9c2823b2e608aa2
Author: Karsten Loesing <karsten.loesing@xxxxxxx>
Date:   Fri May 20 11:39:51 2016 +0200

    Parse crypto parts in network status votes.
---
 CHANGELOG.md                                       |  1 +
 .../impl/RelayNetworkStatusVoteImpl.java           | 92 +++++++++++++++++++++-
 .../impl/RelayNetworkStatusVoteImplTest.java       | 15 ++++
 3 files changed, 104 insertions(+), 4 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 66da859..a1b9a75 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,7 @@
    - Rename properties for overriding default descriptor source
      implementation classes.
    - Actually return the signing key digest in network status votes.
+   - Parse crypto parts in network status votes.
 
  * Minor changes
    - Include a Torperf results line with more than one unrecognized
diff --git a/src/org/torproject/descriptor/impl/RelayNetworkStatusVoteImpl.java b/src/org/torproject/descriptor/impl/RelayNetworkStatusVoteImpl.java
index 68f3b38..8d18919 100644
--- a/src/org/torproject/descriptor/impl/RelayNetworkStatusVoteImpl.java
+++ b/src/org/torproject/descriptor/impl/RelayNetworkStatusVoteImpl.java
@@ -3,6 +3,7 @@
 package org.torproject.descriptor.impl;
 
 import org.torproject.descriptor.DescriptorParseException;
+
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashSet;
@@ -75,7 +76,8 @@ public class RelayNetworkStatusVoteImpl extends NetworkStatusImpl
     this.ignoringAdvertisedBws = -1;
 
     Scanner s = new Scanner(new String(headerBytes)).useDelimiter("\n");
-    boolean skipCrypto = false; /* TODO Parse crypto parts. */
+    String nextCrypto = "";
+    StringBuilder crypto = null;
     while (s.hasNext()) {
       String line = s.next();
       String[] parts = line.split("[ \t]+");
@@ -145,18 +147,52 @@ public class RelayNetworkStatusVoteImpl extends NetworkStatusImpl
         this.parseDirKeyExpiresLine(line, parts);
         break;
       case "dir-identity-key":
+        this.parseDirIdentityKeyLine(line, parts);
+        nextCrypto = "dir-identity-key";
+        break;
       case "dir-signing-key":
+        this.parseDirSigningKeyLine(line, parts);
+        nextCrypto = "dir-signing-key";
+        break;
       case "dir-key-crosscert":
+        this.parseDirKeyCrosscertLine(line, parts);
+        nextCrypto = "dir-key-crosscert";
+        break;
       case "dir-key-certification":
+        this.parseDirKeyCertificationLine(line, parts);
+        nextCrypto = "dir-key-certification";
         break;
       case "-----BEGIN":
-        skipCrypto = true;
+        crypto = new StringBuilder();
+        crypto.append(line).append("\n");
         break;
       case "-----END":
-        skipCrypto = false;
+        crypto.append(line).append("\n");
+        String cryptoString = crypto.toString();
+        crypto = null;
+        switch (nextCrypto) {
+        case "dir-identity-key":
+          this.dirIdentityKey = cryptoString;
+          break;
+        case "dir-signing-key":
+          this.dirSigningKey = cryptoString;
+          break;
+        case "dir-key-crosscert":
+          this.dirKeyCrosscert = cryptoString;
+          break;
+        case "dir-key-certification":
+          this.dirKeyCertification = cryptoString;
+          break;
+        default:
+          throw new DescriptorParseException("Unrecognized crypto "
+              + "block in vote.");
+        }
+        nextCrypto = "";
         break;
       default:
-        if (!skipCrypto) {
+        if (crypto != null) {
+          crypto.append(line).append("\n");
+        } else {
           if (this.failUnrecognizedDescriptorLines) {
             throw new DescriptorParseException("Unrecognized line '"
                 + line + "' in vote.");
@@ -414,6 +450,34 @@ public class RelayNetworkStatusVoteImpl extends NetworkStatusImpl
         parts, 1, 2);
   }
 
+  private void parseDirIdentityKeyLine(String line, String[] parts)
+      throws DescriptorParseException {
+    if (!line.equals("dir-identity-key")) {
+      throw new DescriptorParseException("Illegal line '" + line + "'.");
+    }
+  }
+
+  private void parseDirSigningKeyLine(String line, String[] parts)
+      throws DescriptorParseException {
+    if (!line.equals("dir-signing-key")) {
+      throw new DescriptorParseException("Illegal line '" + line + "'.");
+    }
+  }
+
+  private void parseDirKeyCrosscertLine(String line, String[] parts)
+      throws DescriptorParseException {
+    if (!line.equals("dir-key-crosscert")) {
+      throw new DescriptorParseException("Illegal line '" + line + "'.");
+    }
+  }
+
+  private void parseDirKeyCertificationLine(String line, String[] parts)
+      throws DescriptorParseException {
+    if (!line.equals("dir-key-certification")) {
+      throw new DescriptorParseException("Illegal line '" + line + "'.");
+    }
+  }
+
   protected void parseFooter(byte[] footerBytes)
       throws DescriptorParseException {
     Scanner s = new Scanner(new String(footerBytes)).useDelimiter("\n");
@@ -488,6 +552,26 @@ public class RelayNetworkStatusVoteImpl extends NetworkStatusImpl
     return this.dirKeyExpiresMillis;
   }
 
+  private String dirIdentityKey;
+  public String getDirIdentityKey() {
+    return this.dirIdentityKey;
+  }
+
+  private String dirSigningKey;
+  public String getDirSigningKey() {
+    return this.dirSigningKey;
+  }
+
+  private String dirKeyCrosscert;
+  public String getDirKeyCrosscert() {
+    return this.dirKeyCrosscert;
+  }
+
+  private String dirKeyCertification;
+  public String getDirKeyCertification() {
+    return this.dirKeyCertification;
+  }
+
   public String getSigningKeyDigest() {
     String signingKeyDigest = null;
     if (!this.directorySignatures.isEmpty()) {
diff --git a/test/org/torproject/descriptor/impl/RelayNetworkStatusVoteImplTest.java b/test/org/torproject/descriptor/impl/RelayNetworkStatusVoteImplTest.java
index 6111b59..261abd4 100644
--- a/test/org/torproject/descriptor/impl/RelayNetworkStatusVoteImplTest.java
+++ b/test/org/torproject/descriptor/impl/RelayNetworkStatusVoteImplTest.java
@@ -506,6 +506,21 @@ public class RelayNetworkStatusVoteImplTest {
     assertEquals("Tor 0.2.1.29 (r8e9b25e6c7a2e70c)",
         vote.getStatusEntry("00343A8024F70E214728F0C5AF7ACE0C1508F073").
         getVersion());
+    assertEquals(3, vote.getDirKeyCertificateVersion());
+    assertEquals("80550987E1D626E3EBA5E5E75A458DE0626D088C",
+        vote.getIdentity());
+    assertEquals(1303882477000L, /* 2011-04-27 05:34:37 */
+        vote.getDirKeyPublishedMillis());
+    assertEquals(1335504877000L, /* 2012-04-27 05:34:37 */
+        vote.getDirKeyExpiresMillis());
+    assertEquals("-----BEGIN RSA PUBLIC KEY-----",
+        vote.getDirIdentityKey().split("\n")[0]);
+    assertEquals("-----BEGIN RSA PUBLIC KEY-----",
+        vote.getDirSigningKey().split("\n")[0]);
+    assertEquals("-----BEGIN ID SIGNATURE-----",
+        vote.getDirKeyCrosscert().split("\n")[0]);
+    assertEquals("-----BEGIN SIGNATURE-----",
+        vote.getDirKeyCertification().split("\n")[0]);
     assertTrue(vote.getUnrecognizedLines().isEmpty());
   }
 



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