]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Make onion-key body optional in microdescs
authorNick Mathewson <nickm@torproject.org>
Mon, 24 Jun 2024 18:04:04 +0000 (14:04 -0400)
committerNick Mathewson <nickm@torproject.org>
Mon, 24 Jun 2024 21:48:55 +0000 (17:48 -0400)
Also, stop storing onion keys in microdesc_t.

(In prop350, for microdescs, we are making the body optional; the "onion-key"
entry is still mandatory, so that we can tell where microdescs begin.)

src/feature/dirparse/microdesc_parse.c
src/feature/dirparse/parsecommon.c
src/feature/dirparse/parsecommon.h
src/feature/nodelist/microdesc.c
src/feature/nodelist/microdesc_st.h
src/feature/nodelist/nodelist.c

index beb38bda309fc1bfb5d515decb1e8dd4747ec6c9..eef52f14f3649daf79c301b22423dd6c36c37bdb 100644 (file)
@@ -30,7 +30,7 @@
 /** List of tokens recognized in microdescriptors */
 // clang-format off
 static token_rule_t microdesc_token_table[] = {
-  T1_START("onion-key",        K_ONION_KEY,        NO_ARGS,     NEED_KEY_1024),
+  T1_START("onion-key",        K_ONION_KEY,        NO_ARGS,     OPT_KEY_1024),
   T1("ntor-onion-key",         K_ONION_KEY_NTOR,   GE(1),       NO_OBJ ),
   T0N("id",                    K_ID,               GE(2),       NO_OBJ ),
   T0N("a",                     K_A,                GE(1),       NO_OBJ ),
@@ -200,14 +200,11 @@ microdesc_parse_fields(microdesc_t *md,
   }
 
   tok = find_by_keyword(tokens, K_ONION_KEY);
-  if (!crypto_pk_public_exponent_ok(tok->key)) {
+  if (tok && tok->key && !crypto_pk_public_exponent_ok(tok->key)) {
     log_warn(LD_DIR,
              "Relay's onion key had invalid exponent.");
     goto err;
   }
-  md->onion_pkey = tor_memdup(tok->object_body, tok->object_size);
-  md->onion_pkey_len = tok->object_size;
-  crypto_pk_free(tok->key);
 
   if ((tok = find_opt_by_keyword(tokens, K_ONION_KEY_NTOR))) {
     curve25519_public_key_t k;
index d7a6d65346391e5c1edd1733f1b50bf9d0cc58ea..be1457b730db4ed32bbf3879cd39e0c6eaca678a 100644 (file)
@@ -215,6 +215,16 @@ token_check_object(memarea_t *area, const char *kwd,
         RET_ERR(ebuf);
       }
       break;
+    case OPT_KEY_1024:
+      /* If there is anything, it must be a 1024-bit RSA key. */
+      if (tok->object_body && !tok->key) {
+        tor_snprintf(ebuf, sizeof(ebuf), "Unexpected object for %s", kwd);
+        RET_ERR(ebuf);
+      }
+      if (!tok->key) {
+        break;
+      }
+      FALLTHROUGH;
     case NEED_KEY_1024: /* There must be a 1024-bit public key. */
       if (tok->key && crypto_pk_num_bits(tok->key) != PK_BYTES*8) {
         tor_snprintf(ebuf, sizeof(ebuf), "Wrong size on key for %s: %d bits",
@@ -395,7 +405,8 @@ get_next_token(memarea_t *area,
   }
 
   if (!strcmp(tok->object_type, "RSA PUBLIC KEY")) { /* If it's a public key */
-    if (o_syn != NEED_KEY && o_syn != NEED_KEY_1024 && o_syn != OBJ_OK) {
+    if (o_syn != OPT_KEY_1024 && o_syn != NEED_KEY &&
+        o_syn != NEED_KEY_1024 && o_syn != OBJ_OK) {
       RET_ERR("Unexpected public key.");
     }
     tok->key = crypto_pk_asn1_decode(tok->object_body, tok->object_size);
index 9333ec4b27c3260db06191b74a8614e3d69a0994..d48d27499f1d91127854bea5198de5c762551310 100644 (file)
@@ -220,6 +220,7 @@ typedef struct directory_token_t {
 typedef enum {
   NO_OBJ,        /**< No object, ever. */
   NEED_OBJ,      /**< Object is required. */
+  OPT_KEY_1024,  /**< If object is present, it must be a 1024 bit public key */
   NEED_KEY_1024, /**< Object is required, and must be a 1024 bit public key */
   NEED_KEY,      /**< Object is required, and must be a public key. */
   OBJ_OK,        /**< Object is optional. */
index 9e5f0bb9a4c0084f69fcfd0e439b4da85d514c77..3fd0f23fb501cc0da2d95c23a11ad9a290539df6 100644 (file)
@@ -909,8 +909,6 @@ microdesc_free_(microdesc_t *md, const char *fname, int lineno)
   //tor_assert(md->held_in_map == 0);
   //tor_assert(md->held_by_nodes == 0);
 
-  if (md->onion_pkey)
-    tor_free(md->onion_pkey);
   tor_free(md->onion_curve25519_pkey);
   tor_free(md->ed25519_identity_pkey);
   if (md->body && md->saved_location != SAVED_IN_CACHE)
index ad56b6d6c2b059e27e4cba51ceed40d3e6d2c7f4..c642e6e12b58beea96332a0a45d2b2d165280eeb 100644 (file)
@@ -63,14 +63,6 @@ struct microdesc_t {
 
   /* Fields in the microdescriptor. */
 
-  /**
-   * Public RSA TAP key for onions, ASN.1 encoded.  We store this
-   * in its encoded format since storing it as a crypto_pk_t uses
-   * significantly more memory. */
-  char *onion_pkey;
-  /** Length of onion_pkey, in bytes. */
-  size_t onion_pkey_len;
-
   /** As routerinfo_t.onion_curve25519_pkey */
   struct curve25519_public_key_t *onion_curve25519_pkey;
   /** Ed25519 identity key, if included. */
index bbaa51a40745dd8658ed9bcddbebe4391e838b39..09b10f10f6251663bcf3f2de6129baca5a74147b 100644 (file)
@@ -2052,11 +2052,8 @@ node_get_rsa_onion_key(const node_t *node)
   if (node->ri) {
     onion_pkey = node->ri->onion_pkey;
     onion_pkey_len = node->ri->onion_pkey_len;
-  } else if (node->rs && node->md) {
-    onion_pkey = node->md->onion_pkey;
-    onion_pkey_len = node->md->onion_pkey_len;
   } else {
-    /* No descriptor or microdescriptor. */
+    /* No descriptor; we don't take onion keys from microdescs. */
     goto end;
   }
   pk = router_get_rsa_onion_pkey(onion_pkey, onion_pkey_len);