Tor  0.4.3.0-alpha-dev
authcert_parse.c
Go to the documentation of this file.
1 /* Copyright (c) 2001 Matej Pfajfar.
2  * Copyright (c) 2001-2004, Roger Dingledine.
3  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4  * Copyright (c) 2007-2020, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 
7 /**
8  * @file authcert_parse.c
9  * @brief Authority certificate parsing.
10  **/
11 
12 #include "core/or/or.h"
18 #include "lib/memarea/memarea.h"
19 
22 
23 /** List of tokens recognized in V3 authority certificates. */
25  AUTHCERT_MEMBERS,
26  T1("fingerprint", K_FINGERPRINT, CONCAT_ARGS, NO_OBJ ),
28 };
29 
30 /** Parse a key certificate from <b>s</b>; point <b>end-of-string</b> to
31  * the first character after the certificate. */
33 authority_cert_parse_from_string(const char *s, size_t maxlen,
34  const char **end_of_string)
35 {
36  /** Reject any certificate at least this big; it is probably an overflow, an
37  * attack, a bug, or some other nonsense. */
38 #define MAX_CERT_SIZE (128*1024)
39 
40  authority_cert_t *cert = NULL, *old_cert;
41  smartlist_t *tokens = NULL;
42  char digest[DIGEST_LEN];
43  directory_token_t *tok;
44  char fp_declared[DIGEST_LEN];
45  const char *eos;
46  size_t len;
47  int found;
48  memarea_t *area = NULL;
49  const char *end_of_s = s + maxlen;
50  const char *s_dup = s;
51 
52  s = eat_whitespace_eos(s, end_of_s);
53  eos = tor_memstr(s, end_of_s - s, "\ndir-key-certification");
54  if (! eos) {
55  log_warn(LD_DIR, "No signature found on key certificate");
56  return NULL;
57  }
58  eos = tor_memstr(eos, end_of_s - eos, "\n-----END SIGNATURE-----\n");
59  if (! eos) {
60  log_warn(LD_DIR, "No end-of-signature found on key certificate");
61  return NULL;
62  }
63  eos = memchr(eos+2, '\n', end_of_s - (eos+2));
64  tor_assert(eos);
65  ++eos;
66  len = eos - s;
67 
68  if (len > MAX_CERT_SIZE) {
69  log_warn(LD_DIR, "Certificate is far too big (at %lu bytes long); "
70  "rejecting", (unsigned long)len);
71  return NULL;
72  }
73 
74  tokens = smartlist_new();
75  area = memarea_new();
76  if (tokenize_string(area,s, eos, tokens, dir_key_certificate_table, 0) < 0) {
77  log_warn(LD_DIR, "Error tokenizing key certificate");
78  goto err;
79  }
80  if (router_get_hash_impl(s, eos - s, digest, "dir-key-certificate-version",
81  "\ndir-key-certification", '\n', DIGEST_SHA1) < 0)
82  goto err;
83  tok = smartlist_get(tokens, 0);
84  if (tok->tp != K_DIR_KEY_CERTIFICATE_VERSION || strcmp(tok->args[0], "3")) {
85  log_warn(LD_DIR,
86  "Key certificate does not begin with a recognized version (3).");
87  goto err;
88  }
89 
90  cert = tor_malloc_zero(sizeof(authority_cert_t));
91  memcpy(cert->cache_info.signed_descriptor_digest, digest, DIGEST_LEN);
92 
93  tok = find_by_keyword(tokens, K_DIR_SIGNING_KEY);
94  tor_assert(tok->key);
95  cert->signing_key = tok->key;
96  tok->key = NULL;
98  goto err;
99 
100  tok = find_by_keyword(tokens, K_DIR_IDENTITY_KEY);
101  tor_assert(tok->key);
102  cert->identity_key = tok->key;
103  tok->key = NULL;
104 
105  tok = find_by_keyword(tokens, K_FINGERPRINT);
106  tor_assert(tok->n_args);
107  if (base16_decode(fp_declared, DIGEST_LEN, tok->args[0],
108  strlen(tok->args[0])) != DIGEST_LEN) {
109  log_warn(LD_DIR, "Couldn't decode key certificate fingerprint %s",
110  escaped(tok->args[0]));
111  goto err;
112  }
113 
116  goto err;
117 
118  if (tor_memneq(cert->cache_info.identity_digest, fp_declared, DIGEST_LEN)) {
119  log_warn(LD_DIR, "Digest of certificate key didn't match declared "
120  "fingerprint");
121  goto err;
122  }
123 
124  tok = find_opt_by_keyword(tokens, K_DIR_ADDRESS);
125  if (tok) {
126  struct in_addr in;
127  char *address = NULL;
128  tor_assert(tok->n_args);
129  /* XXX++ use some tor_addr parse function below instead. -RD */
130  if (tor_addr_port_split(LOG_WARN, tok->args[0], &address,
131  &cert->dir_port) < 0 ||
132  tor_inet_aton(address, &in) == 0) {
133  log_warn(LD_DIR, "Couldn't parse dir-address in certificate");
134  tor_free(address);
135  goto err;
136  }
137  cert->addr = ntohl(in.s_addr);
138  tor_free(address);
139  }
140 
141  tok = find_by_keyword(tokens, K_DIR_KEY_PUBLISHED);
142  if (parse_iso_time(tok->args[0], &cert->cache_info.published_on) < 0) {
143  goto err;
144  }
145  tok = find_by_keyword(tokens, K_DIR_KEY_EXPIRES);
146  if (parse_iso_time(tok->args[0], &cert->expires) < 0) {
147  goto err;
148  }
149 
150  tok = smartlist_get(tokens, smartlist_len(tokens)-1);
151  if (tok->tp != K_DIR_KEY_CERTIFICATION) {
152  log_warn(LD_DIR, "Certificate didn't end with dir-key-certification.");
153  goto err;
154  }
155 
156  /* If we already have this cert, don't bother checking the signature. */
159  cert->signing_key_digest);
160  found = 0;
161  if (old_cert) {
162  /* XXXX We could just compare signed_descriptor_digest, but that wouldn't
163  * buy us much. */
164  if (old_cert->cache_info.signed_descriptor_len == len &&
165  old_cert->cache_info.signed_descriptor_body &&
166  tor_memeq(s, old_cert->cache_info.signed_descriptor_body, len)) {
167  log_debug(LD_DIR, "We already checked the signature on this "
168  "certificate; no need to do so again.");
169  found = 1;
170  }
171  }
172  if (!found) {
173  if (check_signature_token(digest, DIGEST_LEN, tok, cert->identity_key, 0,
174  "key certificate")) {
175  goto err;
176  }
177 
178  tok = find_by_keyword(tokens, K_DIR_KEY_CROSSCERT);
180  DIGEST_LEN,
181  tok,
182  cert->signing_key,
183  CST_NO_CHECK_OBJTYPE,
184  "key cross-certification")) {
185  goto err;
186  }
187  }
188 
189  cert->cache_info.signed_descriptor_len = len;
190  cert->cache_info.signed_descriptor_body = tor_malloc(len+1);
191  memcpy(cert->cache_info.signed_descriptor_body, s, len);
192  cert->cache_info.signed_descriptor_body[len] = 0;
194 
195  if (end_of_string) {
196  *end_of_string = eat_whitespace(eos);
197  }
199  smartlist_free(tokens);
200  if (area) {
201  DUMP_AREA(area, "authority cert");
202  memarea_drop_all(area);
203  }
204  return cert;
205  err:
206  dump_desc(s_dup, "authority cert");
207  authority_cert_free(cert);
209  smartlist_free(tokens);
210  if (area) {
211  DUMP_AREA(area, "authority cert");
212  memarea_drop_all(area);
213  }
214  return NULL;
215 }
authority_cert_t * authority_cert_parse_from_string(const char *s, size_t maxlen, const char **end_of_string)
#define END_OF_TABLE
Definition: parsecommon.h:244
void dump_desc(const char *desc, const char *type)
Definition: unparseable.c:496
#define memarea_drop_all(area)
Definition: memarea.h:22
Header file for authcert.c.
char identity_digest[DIGEST_LEN]
#define tor_assert(expr)
Definition: util_bug.h:102
int tor_inet_aton(const char *str, struct in_addr *addr)
Definition: inaddr.c:38
#define tor_free(p)
Definition: malloc.h:52
char signing_key_digest[DIGEST_LEN]
smartlist_t * smartlist_new(void)
memarea_t * memarea_new(void)
Definition: memarea.c:153
#define tor_memneq(a, b, sz)
Definition: di_ops.h:21
List of tokens common to V3 authority certificates and V3 consensuses.
char signed_descriptor_digest[DIGEST_LEN]
Header file for unparseable.c.
int tor_memeq(const void *a, const void *b, size_t sz)
Definition: di_ops.c:107
#define DIGEST_LEN
Definition: digest_sizes.h:20
signed_descriptor_t cache_info
Master header file for Tor-specific functionality.
const char * eat_whitespace(const char *s)
Definition: util_string.c:268
Header for memarea.c.
saved_location_t saved_location
#define LOG_WARN
Definition: log.h:53
Header file for authcert_parse.c.
#define CONCAT_ARGS
Definition: parsecommon.h:267
directory_token_t * find_opt_by_keyword(const smartlist_t *s, directory_keyword keyword)
Definition: parsecommon.c:450
crypto_pk_t * signing_key
#define LD_DIR
Definition: log.h:88
authority_cert_t * authority_cert_get_by_digests(const char *id_digest, const char *sk_digest)
Definition: authcert.c:650
Header file for parsecommon.c.
int crypto_pk_get_digest(const crypto_pk_t *pk, char *digest_out)
Definition: crypto_rsa.c:356
#define SMARTLIST_FOREACH(sl, type, var, cmd)
const char * escaped(const char *s)
Definition: escape.c:126
crypto_pk_t * identity_key
#define T1(s, t, a, o)
Definition: parsecommon.h:250
void token_clear(directory_token_t *tok)
Definition: parsecommon.c:41
struct crypto_pk_t * key
Definition: parsecommon.h:210
int base16_decode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:506
directory_keyword tp
Definition: parsecommon.h:202
int router_get_hash_impl(const char *s, size_t s_len, char *digest, const char *start_str, const char *end_str, char end_c, digest_algorithm_t alg)
Definition: sigcommon.c:74
int parse_iso_time(const char *cp, time_t *t)
Definition: time_fmt.c:392
int check_signature_token(const char *digest, ssize_t digest_len, directory_token_t *tok, crypto_pk_t *pkey, int flags, const char *doctype)
Definition: sigcommon.c:143
int tokenize_string(memarea_t *area, const char *start, const char *end, smartlist_t *out, const token_rule_t *table, int flags)
Definition: parsecommon.c:53
Header file for sigcommon.c.
Authority certificate structure.
static token_rule_t dir_key_certificate_table[]
const char * eat_whitespace_eos(const char *s, const char *eos)
Definition: util_string.c:295
int tor_addr_port_split(int severity, const char *addrport, char **address_out, uint16_t *port_out)
Definition: address.c:1818