[BACK]Return to krl.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Diff for /src/usr.bin/ssh/krl.c between version 1.19 and 1.20

version 1.19, 2014/11/21 01:00:38 version 1.20, 2014/12/04 01:49:59
Line 28 
Line 28 
 #include <time.h>  #include <time.h>
 #include <unistd.h>  #include <unistd.h>
   
 #include "buffer.h"  #include "sshbuf.h"
 #include "key.h"  #include "sshkey.h"
 #include "authfile.h"  #include "authfile.h"
 #include "misc.h"  #include "misc.h"
 #include "log.h"  #include "log.h"
 #include "xmalloc.h"  #include "ssherr.h"
   
 #include "krl.h"  #include "krl.h"
   
Line 70 
Line 70 
 /* Tree of blobs (used for keys and fingerprints) */  /* Tree of blobs (used for keys and fingerprints) */
 struct revoked_blob {  struct revoked_blob {
         u_char *blob;          u_char *blob;
         u_int len;          size_t len;
         RB_ENTRY(revoked_blob) tree_entry;          RB_ENTRY(revoked_blob) tree_entry;
 };  };
 static int blob_cmp(struct revoked_blob *a, struct revoked_blob *b);  static int blob_cmp(struct revoked_blob *a, struct revoked_blob *b);
Line 79 
Line 79 
   
 /* Tracks revoked certs for a single CA */  /* Tracks revoked certs for a single CA */
 struct revoked_certs {  struct revoked_certs {
         Key *ca_key;          struct sshkey *ca_key;
         struct revoked_serial_tree revoked_serials;          struct revoked_serial_tree revoked_serials;
         struct revoked_key_id_tree revoked_key_ids;          struct revoked_key_id_tree revoked_key_ids;
         TAILQ_ENTRY(revoked_certs) entry;          TAILQ_ENTRY(revoked_certs) entry;
Line 153 
Line 153 
                 free(rki);                  free(rki);
         }          }
         if (rc->ca_key != NULL)          if (rc->ca_key != NULL)
                 key_free(rc->ca_key);                  sshkey_free(rc->ca_key);
 }  }
   
 void  void
Line 188 
Line 188 
         krl->krl_version = version;          krl->krl_version = version;
 }  }
   
 void  int
 ssh_krl_set_comment(struct ssh_krl *krl, const char *comment)  ssh_krl_set_comment(struct ssh_krl *krl, const char *comment)
 {  {
         free(krl->comment);          free(krl->comment);
         if ((krl->comment = strdup(comment)) == NULL)          if ((krl->comment = strdup(comment)) == NULL)
                 fatal("%s: strdup", __func__);                  return SSH_ERR_ALLOC_FAIL;
           return 0;
 }  }
   
 /*  /*
Line 201 
Line 202 
  * create a new one in the tree if one did not exist already.   * create a new one in the tree if one did not exist already.
  */   */
 static int  static int
 revoked_certs_for_ca_key(struct ssh_krl *krl, const Key *ca_key,  revoked_certs_for_ca_key(struct ssh_krl *krl, const struct sshkey *ca_key,
     struct revoked_certs **rcp, int allow_create)      struct revoked_certs **rcp, int allow_create)
 {  {
         struct revoked_certs *rc;          struct revoked_certs *rc;
           int r;
   
         *rcp = NULL;          *rcp = NULL;
         TAILQ_FOREACH(rc, &krl->revoked_certs, entry) {          TAILQ_FOREACH(rc, &krl->revoked_certs, entry) {
                 if (key_equal(rc->ca_key, ca_key)) {                  if (sshkey_equal(rc->ca_key, ca_key)) {
                         *rcp = rc;                          *rcp = rc;
                         return 0;                          return 0;
                 }                  }
Line 217 
Line 219 
                 return 0;                  return 0;
         /* If this CA doesn't exist in the list then add it now */          /* If this CA doesn't exist in the list then add it now */
         if ((rc = calloc(1, sizeof(*rc))) == NULL)          if ((rc = calloc(1, sizeof(*rc))) == NULL)
                 return -1;                  return SSH_ERR_ALLOC_FAIL;
         if ((rc->ca_key = key_from_private(ca_key)) == NULL) {          if ((r = sshkey_from_private(ca_key, &rc->ca_key)) != 0) {
                 free(rc);                  free(rc);
                 return -1;                  return r;
         }          }
         RB_INIT(&rc->revoked_serials);          RB_INIT(&rc->revoked_serials);
         RB_INIT(&rc->revoked_key_ids);          RB_INIT(&rc->revoked_key_ids);
         TAILQ_INSERT_TAIL(&krl->revoked_certs, rc, entry);          TAILQ_INSERT_TAIL(&krl->revoked_certs, rc, entry);
         debug3("%s: new CA %s", __func__, key_type(ca_key));          debug3("%s: new CA %s", __func__, sshkey_type(ca_key));
         *rcp = rc;          *rcp = rc;
         return 0;          return 0;
 }  }
Line 243 
Line 245 
         if (ers == NULL || serial_cmp(ers, &rs) != 0) {          if (ers == NULL || serial_cmp(ers, &rs) != 0) {
                 /* No entry matches. Just insert */                  /* No entry matches. Just insert */
                 if ((irs = malloc(sizeof(rs))) == NULL)                  if ((irs = malloc(sizeof(rs))) == NULL)
                         return -1;                          return SSH_ERR_ALLOC_FAIL;
                 memcpy(irs, &rs, sizeof(*irs));                  memcpy(irs, &rs, sizeof(*irs));
                 ers = RB_INSERT(revoked_serial_tree, rt, irs);                  ers = RB_INSERT(revoked_serial_tree, rt, irs);
                 if (ers != NULL) {                  if (ers != NULL) {
                         KRL_DBG(("%s: bad: ers != NULL", __func__));                          KRL_DBG(("%s: bad: ers != NULL", __func__));
                         /* Shouldn't happen */                          /* Shouldn't happen */
                         free(irs);                          free(irs);
                         return -1;                          return SSH_ERR_ALLOC_FAIL;
                 }                  }
                 ers = irs;                  ers = irs;
         } else {          } else {
Line 303 
Line 305 
 }  }
   
 int  int
 ssh_krl_revoke_cert_by_serial(struct ssh_krl *krl, const Key *ca_key,  ssh_krl_revoke_cert_by_serial(struct ssh_krl *krl, const struct sshkey *ca_key,
     u_int64_t serial)      u_int64_t serial)
 {  {
         return ssh_krl_revoke_cert_by_serial_range(krl, ca_key, serial, serial);          return ssh_krl_revoke_cert_by_serial_range(krl, ca_key, serial, serial);
 }  }
   
 int  int
 ssh_krl_revoke_cert_by_serial_range(struct ssh_krl *krl, const Key *ca_key,  ssh_krl_revoke_cert_by_serial_range(struct ssh_krl *krl, const struct sshkey *ca_key,
     u_int64_t lo, u_int64_t hi)      u_int64_t lo, u_int64_t hi)
 {  {
         struct revoked_certs *rc;          struct revoked_certs *rc;
           int r;
   
         if (lo > hi || lo == 0)          if (lo > hi || lo == 0)
                 return -1;                  return -1;
         if (revoked_certs_for_ca_key(krl, ca_key, &rc, 1) != 0)          if ((r = revoked_certs_for_ca_key(krl, ca_key, &rc, 1)) != 0)
                 return -1;                  return r;
         return insert_serial_range(&rc->revoked_serials, lo, hi);          return insert_serial_range(&rc->revoked_serials, lo, hi);
 }  }
   
 int  int
 ssh_krl_revoke_cert_by_key_id(struct ssh_krl *krl, const Key *ca_key,  ssh_krl_revoke_cert_by_key_id(struct ssh_krl *krl, const struct sshkey *ca_key,
     const char *key_id)      const char *key_id)
 {  {
         struct revoked_key_id *rki, *erki;          struct revoked_key_id *rki, *erki;
         struct revoked_certs *rc;          struct revoked_certs *rc;
           int r;
   
         if (revoked_certs_for_ca_key(krl, ca_key, &rc, 1) != 0)          if ((r = revoked_certs_for_ca_key(krl, ca_key, &rc, 1)) != 0)
                 return -1;                  return r;
   
         debug3("%s: revoke %s", __func__, key_id);          debug3("%s: revoke %s", __func__, key_id);
         if ((rki = calloc(1, sizeof(*rki))) == NULL ||          if ((rki = calloc(1, sizeof(*rki))) == NULL ||
             (rki->key_id = strdup(key_id)) == NULL) {              (rki->key_id = strdup(key_id)) == NULL) {
                 free(rki);                  free(rki);
                 fatal("%s: strdup", __func__);                  return SSH_ERR_ALLOC_FAIL;
         }          }
         erki = RB_INSERT(revoked_key_id_tree, &rc->revoked_key_ids, rki);          erki = RB_INSERT(revoked_key_id_tree, &rc->revoked_key_ids, rki);
         if (erki != NULL) {          if (erki != NULL) {
Line 348 
Line 352 
   
 /* Convert "key" to a public key blob without any certificate information */  /* Convert "key" to a public key blob without any certificate information */
 static int  static int
 plain_key_blob(const Key *key, u_char **blob, u_int *blen)  plain_key_blob(const struct sshkey *key, u_char **blob, size_t *blen)
 {  {
         Key *kcopy;          struct sshkey *kcopy;
         int r;          int r;
   
         if ((kcopy = key_from_private(key)) == NULL)          if ((r = sshkey_from_private(key, &kcopy)) != 0)
                 return -1;                  return r;
         if (key_is_cert(kcopy)) {          if (sshkey_is_cert(kcopy)) {
                 if (key_drop_cert(kcopy) != 0) {                  if ((r = sshkey_drop_cert(kcopy)) != 0) {
                         error("%s: key_drop_cert", __func__);                          sshkey_free(kcopy);
                         key_free(kcopy);                          return r;
                         return -1;  
                 }                  }
         }          }
         r = key_to_blob(kcopy, blob, blen);          r = sshkey_to_blob(kcopy, blob, blen);
         free(kcopy);          free(kcopy);
         return r;          return r;
 }  }
Line 374 
Line 377 
         struct revoked_blob *rb, *erb;          struct revoked_blob *rb, *erb;
   
         if ((rb = calloc(1, sizeof(*rb))) == NULL)          if ((rb = calloc(1, sizeof(*rb))) == NULL)
                 return -1;                  return SSH_ERR_ALLOC_FAIL;
         rb->blob = blob;          rb->blob = blob;
         rb->len = len;          rb->len = len;
         erb = RB_INSERT(revoked_blob_tree, rbt, rb);          erb = RB_INSERT(revoked_blob_tree, rbt, rb);
Line 386 
Line 389 
 }  }
   
 int  int
 ssh_krl_revoke_key_explicit(struct ssh_krl *krl, const Key *key)  ssh_krl_revoke_key_explicit(struct ssh_krl *krl, const struct sshkey *key)
 {  {
         u_char *blob;          u_char *blob;
         u_int len;          size_t len;
           int r;
   
         debug3("%s: revoke type %s", __func__, key_type(key));          debug3("%s: revoke type %s", __func__, sshkey_type(key));
         if (plain_key_blob(key, &blob, &len) < 0)          if ((r = plain_key_blob(key, &blob, &len)) != 0)
                 return -1;                  return r;
         return revoke_blob(&krl->revoked_keys, blob, len);          return revoke_blob(&krl->revoked_keys, blob, len);
 }  }
   
 int  int
 ssh_krl_revoke_key_sha1(struct ssh_krl *krl, const Key *key)  ssh_krl_revoke_key_sha1(struct ssh_krl *krl, const struct sshkey *key)
 {  {
         u_char *blob;          u_char *blob;
         u_int len;          size_t len;
           int r;
   
         debug3("%s: revoke type %s by sha1", __func__, key_type(key));          debug3("%s: revoke type %s by sha1", __func__, sshkey_type(key));
         if ((blob = key_fingerprint_raw(key, SSH_FP_SHA1, &len)) == NULL)          if ((r = sshkey_fingerprint_raw(key, SSH_FP_SHA1, &blob, &len)) != 0)
                 return -1;                  return r;
         return revoke_blob(&krl->revoked_sha1s, blob, len);          return revoke_blob(&krl->revoked_sha1s, blob, len);
 }  }
   
 int  int
 ssh_krl_revoke_key(struct ssh_krl *krl, const Key *key)  ssh_krl_revoke_key(struct ssh_krl *krl, const struct sshkey *key)
 {  {
         if (!key_is_cert(key))          if (!sshkey_is_cert(key))
                 return ssh_krl_revoke_key_sha1(krl, key);                  return ssh_krl_revoke_key_sha1(krl, key);
   
         if (key_cert_is_legacy(key) || key->cert->serial == 0) {          if (sshkey_cert_is_legacy(key) || key->cert->serial == 0) {
                 return ssh_krl_revoke_cert_by_key_id(krl,                  return ssh_krl_revoke_cert_by_key_id(krl,
                     key->cert->signature_key,                      key->cert->signature_key,
                     key->cert->key_id);                      key->cert->key_id);
Line 427 
Line 432 
 }  }
   
 /*  /*
  * Select a copact next section type to emit in a KRL based on the   * Select the most compact section type to emit next in a KRL based on
  * current section type, the run length of contiguous revoked serial   * the current section type, the run length of contiguous revoked serial
  * numbers and the gaps from the last and to the next revoked serial.   * numbers and the gaps from the last and to the next revoked serial.
  * Applies a mostly-accurate bit cost model to select the section type   * Applies a mostly-accurate bit cost model to select the section type
  * that will minimise the size of the resultant KRL.   * that will minimise the size of the resultant KRL.
Line 511 
Line 516 
   
 /* Generate a KRL_SECTION_CERTIFICATES KRL section */  /* Generate a KRL_SECTION_CERTIFICATES KRL section */
 static int  static int
 revoked_certs_generate(struct revoked_certs *rc, Buffer *buf)  revoked_certs_generate(struct revoked_certs *rc, struct sshbuf *buf)
 {  {
         int final, force_new_sect, r = -1;          int final, force_new_sect, r = -1;
         u_int64_t i, contig, gap, last = 0, bitmap_start = 0;          u_int64_t i, contig, gap, last = 0, bitmap_start = 0;
         struct revoked_serial *rs, *nrs;          struct revoked_serial *rs, *nrs;
         struct revoked_key_id *rki;          struct revoked_key_id *rki;
         int next_state, state = 0;          int next_state, state = 0;
         Buffer sect;          struct sshbuf *sect;
         u_char *kblob = NULL;  
         u_int klen;  
         BIGNUM *bitmap = NULL;          BIGNUM *bitmap = NULL;
   
         /* Prepare CA scope key blob if we have one supplied */          if ((sect = sshbuf_new()) == NULL)
         if (key_to_blob(rc->ca_key, &kblob, &klen) == 0)                  return SSH_ERR_ALLOC_FAIL;
                 return -1;  
   
         buffer_init(&sect);          /* Store the header: CA scope key, reserved */
           if ((r = sshkey_to_blob_buf(rc->ca_key, sect)) != 0 ||
               (r = sshbuf_put_stringb(buf, sect)) != 0 ||
               (r = sshbuf_put_string(buf, NULL, 0)) != 0)
                   goto out;
   
         /* Store the header */          sshbuf_reset(sect);
         buffer_put_string(buf, kblob, klen);  
         buffer_put_string(buf, NULL, 0); /* Reserved */  
   
         free(kblob);  
   
         /* Store the revoked serials.  */          /* Store the revoked serials.  */
         for (rs = RB_MIN(revoked_serial_tree, &rc->revoked_serials);          for (rs = RB_MIN(revoked_serial_tree, &rc->revoked_serials);
              rs != NULL;               rs != NULL;
Line 565 
Line 567 
                         case KRL_SECTION_CERT_SERIAL_RANGE:                          case KRL_SECTION_CERT_SERIAL_RANGE:
                                 break;                                  break;
                         case KRL_SECTION_CERT_SERIAL_BITMAP:                          case KRL_SECTION_CERT_SERIAL_BITMAP:
                                 buffer_put_bignum2(&sect, bitmap);                                  if ((r = sshbuf_put_bignum2(sect, bitmap)) != 0)
                                           goto out;
                                 BN_free(bitmap);                                  BN_free(bitmap);
                                 bitmap = NULL;                                  bitmap = NULL;
                                 break;                                  break;
                         }                          }
                         buffer_put_char(buf, state);                          if ((r = sshbuf_put_u8(buf, state)) != 0 ||
                         buffer_put_string(buf,                              (r = sshbuf_put_stringb(buf, sect)) != 0)
                             buffer_ptr(&sect), buffer_len(&sect));                                  goto out;
                         buffer_clear(&sect);                          sshbuf_reset(sect);
                 }                  }
   
                 /* If we are starting a new section then prepare it now */                  /* If we are starting a new section then prepare it now */
                 if (next_state != state || force_new_sect) {                  if (next_state != state || force_new_sect) {
                         debug3("%s: start state 0x%02x", __func__, next_state);                          debug3("%s: start state 0x%02x", __func__, next_state);
                         state = next_state;                          state = next_state;
                         buffer_clear(&sect);                          sshbuf_reset(sect);
                         switch (state) {                          switch (state) {
                         case KRL_SECTION_CERT_SERIAL_LIST:                          case KRL_SECTION_CERT_SERIAL_LIST:
                         case KRL_SECTION_CERT_SERIAL_RANGE:                          case KRL_SECTION_CERT_SERIAL_RANGE:
                                 break;                                  break;
                         case KRL_SECTION_CERT_SERIAL_BITMAP:                          case KRL_SECTION_CERT_SERIAL_BITMAP:
                                 if ((bitmap = BN_new()) == NULL)                                  if ((bitmap = BN_new()) == NULL) {
                                           r = SSH_ERR_ALLOC_FAIL;
                                         goto out;                                          goto out;
                                   }
                                 bitmap_start = rs->lo;                                  bitmap_start = rs->lo;
                                 buffer_put_int64(&sect, bitmap_start);                                  if ((r = sshbuf_put_u64(sect,
                                       bitmap_start)) != 0)
                                           goto out;
                                 break;                                  break;
                         }                          }
                 }                  }
Line 597 
Line 604 
                 /* Perform section-specific processing */                  /* Perform section-specific processing */
                 switch (state) {                  switch (state) {
                 case KRL_SECTION_CERT_SERIAL_LIST:                  case KRL_SECTION_CERT_SERIAL_LIST:
                         for (i = 0; i < contig; i++)                          for (i = 0; i < contig; i++) {
                                 buffer_put_int64(&sect, rs->lo + i);                                  if ((r = sshbuf_put_u64(sect, rs->lo + i)) != 0)
                                           goto out;
                           }
                         break;                          break;
                 case KRL_SECTION_CERT_SERIAL_RANGE:                  case KRL_SECTION_CERT_SERIAL_RANGE:
                         buffer_put_int64(&sect, rs->lo);                          if ((r = sshbuf_put_u64(sect, rs->lo)) != 0 ||
                         buffer_put_int64(&sect, rs->hi);                              (r = sshbuf_put_u64(sect, rs->hi)) != 0)
                                   goto out;
                         break;                          break;
                 case KRL_SECTION_CERT_SERIAL_BITMAP:                  case KRL_SECTION_CERT_SERIAL_BITMAP:
                         if (rs->lo - bitmap_start > INT_MAX) {                          if (rs->lo - bitmap_start > INT_MAX) {
Line 611 
Line 621 
                         }                          }
                         for (i = 0; i < contig; i++) {                          for (i = 0; i < contig; i++) {
                                 if (BN_set_bit(bitmap,                                  if (BN_set_bit(bitmap,
                                     rs->lo + i - bitmap_start) != 1)                                      rs->lo + i - bitmap_start) != 1) {
                                           r = SSH_ERR_ALLOC_FAIL;
                                         goto out;                                          goto out;
                                   }
                         }                          }
                         break;                          break;
                 }                  }
Line 627 
Line 639 
                 case KRL_SECTION_CERT_SERIAL_RANGE:                  case KRL_SECTION_CERT_SERIAL_RANGE:
                         break;                          break;
                 case KRL_SECTION_CERT_SERIAL_BITMAP:                  case KRL_SECTION_CERT_SERIAL_BITMAP:
                         buffer_put_bignum2(&sect, bitmap);                          if ((r = sshbuf_put_bignum2(sect, bitmap)) != 0)
                                   goto out;
                         BN_free(bitmap);                          BN_free(bitmap);
                         bitmap = NULL;                          bitmap = NULL;
                         break;                          break;
                 }                  }
                 buffer_put_char(buf, state);                  if ((r = sshbuf_put_u8(buf, state)) != 0 ||
                 buffer_put_string(buf,                      (r = sshbuf_put_stringb(buf, sect)) != 0)
                     buffer_ptr(&sect), buffer_len(&sect));                          goto out;
         }          }
         debug3("%s: serial done ", __func__);          debug3("%s: serial done ", __func__);
   
         /* Now output a section for any revocations by key ID */          /* Now output a section for any revocations by key ID */
         buffer_clear(&sect);          sshbuf_reset(sect);
         RB_FOREACH(rki, revoked_key_id_tree, &rc->revoked_key_ids) {          RB_FOREACH(rki, revoked_key_id_tree, &rc->revoked_key_ids) {
                 debug3("%s: key ID %s", __func__, rki->key_id);                  debug3("%s: key ID %s", __func__, rki->key_id);
                 buffer_put_cstring(&sect, rki->key_id);                  if ((r = sshbuf_put_cstring(sect, rki->key_id)) != 0)
                           goto out;
         }          }
         if (buffer_len(&sect) != 0) {          if (sshbuf_len(sect) != 0) {
                 buffer_put_char(buf, KRL_SECTION_CERT_KEY_ID);                  if ((r = sshbuf_put_u8(buf, KRL_SECTION_CERT_KEY_ID)) != 0 ||
                 buffer_put_string(buf, buffer_ptr(&sect),                      (r = sshbuf_put_stringb(buf, sect)) != 0)
                     buffer_len(&sect));                          goto out;
         }          }
         r = 0;          r = 0;
  out:   out:
         if (bitmap != NULL)          if (bitmap != NULL)
                 BN_free(bitmap);                  BN_free(bitmap);
         buffer_free(&sect);          sshbuf_free(sect);
         return r;          return r;
 }  }
   
 int  int
 ssh_krl_to_blob(struct ssh_krl *krl, Buffer *buf, const Key **sign_keys,  ssh_krl_to_blob(struct ssh_krl *krl, struct sshbuf *buf,
     u_int nsign_keys)      const struct sshkey **sign_keys, u_int nsign_keys)
 {  {
         int r = -1;          int r = -1;
         struct revoked_certs *rc;          struct revoked_certs *rc;
         struct revoked_blob *rb;          struct revoked_blob *rb;
         Buffer sect;          struct sshbuf *sect;
         u_char *kblob = NULL, *sblob = NULL;          u_char *sblob = NULL;
         u_int klen, slen, i;          size_t slen, i;
   
         if (krl->generated_date == 0)          if (krl->generated_date == 0)
                 krl->generated_date = time(NULL);                  krl->generated_date = time(NULL);
   
         buffer_init(&sect);          if ((sect = sshbuf_new()) == NULL)
                   return SSH_ERR_ALLOC_FAIL;
   
         /* Store the header */          /* Store the header */
         buffer_append(buf, KRL_MAGIC, sizeof(KRL_MAGIC) - 1);          if ((r = sshbuf_put(buf, KRL_MAGIC, sizeof(KRL_MAGIC) - 1)) != 0 ||
         buffer_put_int(buf, KRL_FORMAT_VERSION);              (r = sshbuf_put_u32(buf, KRL_FORMAT_VERSION)) != 0 ||
         buffer_put_int64(buf, krl->krl_version);              (r = sshbuf_put_u64(buf, krl->krl_version)) != 0 ||
         buffer_put_int64(buf, krl->generated_date);              (r = sshbuf_put_u64(buf, krl->generated_date) != 0) ||
         buffer_put_int64(buf, krl->flags);              (r = sshbuf_put_u64(buf, krl->flags)) != 0 ||
         buffer_put_string(buf, NULL, 0);              (r = sshbuf_put_string(buf, NULL, 0)) != 0 ||
         buffer_put_cstring(buf, krl->comment ? krl->comment : "");              (r = sshbuf_put_cstring(buf, krl->comment)) != 0)
                   goto out;
   
         /* Store sections for revoked certificates */          /* Store sections for revoked certificates */
         TAILQ_FOREACH(rc, &krl->revoked_certs, entry) {          TAILQ_FOREACH(rc, &krl->revoked_certs, entry) {
                 buffer_clear(&sect);                  sshbuf_reset(sect);
                 if (revoked_certs_generate(rc, &sect) != 0)                  if ((r = revoked_certs_generate(rc, sect)) != 0)
                         goto out;                          goto out;
                 buffer_put_char(buf, KRL_SECTION_CERTIFICATES);                  if ((r = sshbuf_put_u8(buf, KRL_SECTION_CERTIFICATES)) != 0 ||
                 buffer_put_string(buf, buffer_ptr(&sect),                      (r = sshbuf_put_stringb(buf, sect)) != 0)
                     buffer_len(&sect));                          goto out;
         }          }
   
         /* Finally, output sections for revocations by public key/hash */          /* Finally, output sections for revocations by public key/hash */
         buffer_clear(&sect);          sshbuf_reset(sect);
         RB_FOREACH(rb, revoked_blob_tree, &krl->revoked_keys) {          RB_FOREACH(rb, revoked_blob_tree, &krl->revoked_keys) {
                 debug3("%s: key len %u ", __func__, rb->len);                  debug3("%s: key len %zu ", __func__, rb->len);
                 buffer_put_string(&sect, rb->blob, rb->len);                  if ((sshbuf_put_string(sect, rb->blob, rb->len)) != 0)
                           goto out;
         }          }
         if (buffer_len(&sect) != 0) {          if (sshbuf_len(sect) != 0) {
                 buffer_put_char(buf, KRL_SECTION_EXPLICIT_KEY);                  if ((r = sshbuf_put_u8(buf, KRL_SECTION_EXPLICIT_KEY)) != 0 ||
                 buffer_put_string(buf, buffer_ptr(&sect),                      (r = sshbuf_put_stringb(buf, sect)) != 0)
                     buffer_len(&sect));                          goto out;
         }          }
         buffer_clear(&sect);          sshbuf_reset(sect);
         RB_FOREACH(rb, revoked_blob_tree, &krl->revoked_sha1s) {          RB_FOREACH(rb, revoked_blob_tree, &krl->revoked_sha1s) {
                 debug3("%s: hash len %u ", __func__, rb->len);                  debug3("%s: hash len %zu ", __func__, rb->len);
                 buffer_put_string(&sect, rb->blob, rb->len);                  if ((sshbuf_put_string(sect, rb->blob, rb->len)) != 0)
                           goto out;
         }          }
         if (buffer_len(&sect) != 0) {          if (sshbuf_len(sect) != 0) {
                 buffer_put_char(buf, KRL_SECTION_FINGERPRINT_SHA1);                  if ((r = sshbuf_put_u8(buf,
                 buffer_put_string(buf, buffer_ptr(&sect),                      KRL_SECTION_FINGERPRINT_SHA1)) != 0 ||
                     buffer_len(&sect));                      (r = sshbuf_put_stringb(buf, sect)) != 0)
                           goto out;
         }          }
   
         for (i = 0; i < nsign_keys; i++) {          for (i = 0; i < nsign_keys; i++) {
                 if (key_to_blob(sign_keys[i], &kblob, &klen) == 0)                  sshbuf_reset(sect);
                   if ((r = sshkey_to_blob_buf(sign_keys[i], sect)) != 0)
                         goto out;                          goto out;
   
                 debug3("%s: signature key len %u", __func__, klen);                  debug3("%s: signature key len %zu", __func__, sshbuf_len(sect));
                 buffer_put_char(buf, KRL_SECTION_SIGNATURE);                  if ((r = sshbuf_put_u8(buf, KRL_SECTION_SIGNATURE)) != 0 ||
                 buffer_put_string(buf, kblob, klen);                      (r = sshbuf_put_stringb(buf, sect)) != 0)
                           goto out;
   
                 if (key_sign(sign_keys[i], &sblob, &slen,                  if ((r = sshkey_sign(sign_keys[i], &sblob, &slen,
                     buffer_ptr(buf), buffer_len(buf)) == -1)                      sshbuf_ptr(buf), sshbuf_len(buf), 0)) == -1)
                         goto out;                          goto out;
                 debug3("%s: signature sig len %u", __func__, slen);                  debug3("%s: signature sig len %zu", __func__, slen);
                 buffer_put_string(buf, sblob, slen);                  if ((r = sshbuf_put_string(buf, sblob, slen)) != 0)
                           goto out;
         }          }
   
         r = 0;          r = 0;
  out:   out:
         free(kblob);  
         free(sblob);          free(sblob);
         buffer_free(&sect);          sshbuf_free(sect);
         return r;          return r;
 }  }
   
Line 754 
Line 775 
 }  }
   
 static int  static int
 parse_revoked_certs(Buffer *buf, struct ssh_krl *krl)  parse_revoked_certs(struct sshbuf *buf, struct ssh_krl *krl)
 {  {
         int ret = -1, nbits;          int r = -1, nbits;
         u_char type;          u_char type;
         const u_char *blob;          const u_char *blob;
         u_int blen;          size_t blen;
         Buffer subsect;          struct sshbuf *subsect = NULL;
         u_int64_t serial, serial_lo, serial_hi;          u_int64_t serial, serial_lo, serial_hi;
         BIGNUM *bitmap = NULL;          BIGNUM *bitmap = NULL;
         char *key_id = NULL;          char *key_id = NULL;
         Key *ca_key = NULL;          struct sshkey *ca_key = NULL;
   
         buffer_init(&subsect);          if ((subsect = sshbuf_new()) == NULL)
                   return SSH_ERR_ALLOC_FAIL;
   
         if ((blob = buffer_get_string_ptr_ret(buf, &blen)) == NULL ||          /* Header: key, reserved */
             buffer_get_string_ptr_ret(buf, NULL) == NULL) { /* reserved */          if ((r = sshbuf_get_string_direct(buf, &blob, &blen)) != 0 ||
                 error("%s: buffer error", __func__);              (r = sshbuf_skip_string(buf)) != 0)
                 goto out;                  goto out;
         }          if ((r = sshkey_from_blob(blob, blen, &ca_key)) != 0)
         if ((ca_key = key_from_blob(blob, blen)) == NULL)  
                 goto out;                  goto out;
   
         while (buffer_len(buf) > 0) {          while (sshbuf_len(buf) > 0) {
                 if (buffer_get_char_ret(&type, buf) != 0 ||                  if (subsect != NULL) {
                     (blob = buffer_get_string_ptr_ret(buf, &blen)) == NULL) {                          sshbuf_free(subsect);
                         error("%s: buffer error", __func__);                          subsect = NULL;
                         goto out;  
                 }                  }
                 buffer_clear(&subsect);                  if ((r = sshbuf_get_u8(buf, &type)) != 0 ||
                 buffer_append(&subsect, blob, blen);                      (r = sshbuf_froms(buf, &subsect)) != 0)
                           goto out;
                 debug3("%s: subsection type 0x%02x", __func__, type);                  debug3("%s: subsection type 0x%02x", __func__, type);
                 /* buffer_dump(&subsect); */  
   
                 switch (type) {                  switch (type) {
                 case KRL_SECTION_CERT_SERIAL_LIST:                  case KRL_SECTION_CERT_SERIAL_LIST:
                         while (buffer_len(&subsect) > 0) {                          while (sshbuf_len(subsect) > 0) {
                                 if (buffer_get_int64_ret(&serial,                                  if ((r = sshbuf_get_u64(subsect, &serial)) != 0)
                                     &subsect) != 0) {  
                                         error("%s: buffer error", __func__);  
                                         goto out;                                          goto out;
                                 }                                  if ((r = ssh_krl_revoke_cert_by_serial(krl,
                                 if (ssh_krl_revoke_cert_by_serial(krl, ca_key,                                      ca_key, serial)) != 0)
                                     serial) != 0) {  
                                         error("%s: update failed", __func__);  
                                         goto out;                                          goto out;
                                 }  
                         }                          }
                         break;                          break;
                 case KRL_SECTION_CERT_SERIAL_RANGE:                  case KRL_SECTION_CERT_SERIAL_RANGE:
                         if (buffer_get_int64_ret(&serial_lo, &subsect) != 0 ||                          if ((r = sshbuf_get_u64(subsect, &serial_lo)) != 0 ||
                             buffer_get_int64_ret(&serial_hi, &subsect) != 0) {                              (r = sshbuf_get_u64(subsect, &serial_hi)) != 0)
                                 error("%s: buffer error", __func__);  
                                 goto out;                                  goto out;
                         }                          if ((r = ssh_krl_revoke_cert_by_serial_range(krl,
                         if (ssh_krl_revoke_cert_by_serial_range(krl, ca_key,                              ca_key, serial_lo, serial_hi)) != 0)
                             serial_lo, serial_hi) != 0) {  
                                 error("%s: update failed", __func__);  
                                 goto out;                                  goto out;
                         }  
                         break;                          break;
                 case KRL_SECTION_CERT_SERIAL_BITMAP:                  case KRL_SECTION_CERT_SERIAL_BITMAP:
                         if ((bitmap = BN_new()) == NULL) {                          if ((bitmap = BN_new()) == NULL) {
                                 error("%s: BN_new", __func__);                                  r = SSH_ERR_ALLOC_FAIL;
                                 goto out;                                  goto out;
                         }                          }
                         if (buffer_get_int64_ret(&serial_lo, &subsect) != 0 ||                          if ((r = sshbuf_get_u64(subsect, &serial_lo)) != 0 ||
                             buffer_get_bignum2_ret(&subsect, bitmap) != 0) {                              (r = sshbuf_get_bignum2(subsect, bitmap)) != 0)
                                 error("%s: buffer error", __func__);  
                                 goto out;                                  goto out;
                         }  
                         if ((nbits = BN_num_bits(bitmap)) < 0) {                          if ((nbits = BN_num_bits(bitmap)) < 0) {
                                 error("%s: bitmap bits < 0", __func__);                                  error("%s: bitmap bits < 0", __func__);
                                   r = SSH_ERR_INVALID_FORMAT;
                                 goto out;                                  goto out;
                         }                          }
                         for (serial = 0; serial < (u_int)nbits; serial++) {                          for (serial = 0; serial < (u_int)nbits; serial++) {
                                 if (serial > 0 && serial_lo + serial == 0) {                                  if (serial > 0 && serial_lo + serial == 0) {
                                         error("%s: bitmap wraps u64", __func__);                                          error("%s: bitmap wraps u64", __func__);
                                           r = SSH_ERR_INVALID_FORMAT;
                                         goto out;                                          goto out;
                                 }                                  }
                                 if (!BN_is_bit_set(bitmap, serial))                                  if (!BN_is_bit_set(bitmap, serial))
                                         continue;                                          continue;
                                 if (ssh_krl_revoke_cert_by_serial(krl, ca_key,                                  if ((r = ssh_krl_revoke_cert_by_serial(krl,
                                     serial_lo + serial) != 0) {                                      ca_key, serial_lo + serial)) != 0)
                                         error("%s: update failed", __func__);  
                                         goto out;                                          goto out;
                                 }  
                         }                          }
                         BN_free(bitmap);                          BN_free(bitmap);
                         bitmap = NULL;                          bitmap = NULL;
                         break;                          break;
                 case KRL_SECTION_CERT_KEY_ID:                  case KRL_SECTION_CERT_KEY_ID:
                         while (buffer_len(&subsect) > 0) {                          while (sshbuf_len(subsect) > 0) {
                                 if ((key_id = buffer_get_cstring_ret(&subsect,                                  if ((r = sshbuf_get_cstring(subsect,
                                     NULL)) == NULL) {                                      &key_id, NULL)) != 0)
                                         error("%s: buffer error", __func__);  
                                         goto out;                                          goto out;
                                 }                                  if ((r = ssh_krl_revoke_cert_by_key_id(krl,
                                 if (ssh_krl_revoke_cert_by_key_id(krl, ca_key,                                      ca_key, key_id)) != 0)
                                     key_id) != 0) {  
                                         error("%s: update failed", __func__);  
                                         goto out;                                          goto out;
                                 }  
                                 free(key_id);                                  free(key_id);
                                 key_id = NULL;                                  key_id = NULL;
                         }                          }
                         break;                          break;
                 default:                  default:
                         error("Unsupported KRL certificate section %u", type);                          error("Unsupported KRL certificate section %u", type);
                           r = SSH_ERR_INVALID_FORMAT;
                         goto out;                          goto out;
                 }                  }
                 if (buffer_len(&subsect) > 0) {                  if (sshbuf_len(subsect) > 0) {
                         error("KRL certificate section contains unparsed data");                          error("KRL certificate section contains unparsed data");
                           r = SSH_ERR_INVALID_FORMAT;
                         goto out;                          goto out;
                 }                  }
         }          }
   
         ret = 0;          r = 0;
  out:   out:
         if (ca_key != NULL)  
                 key_free(ca_key);  
         if (bitmap != NULL)          if (bitmap != NULL)
                 BN_free(bitmap);                  BN_free(bitmap);
         free(key_id);          free(key_id);
         buffer_free(&subsect);          sshkey_free(ca_key);
         return ret;          sshbuf_free(subsect);
           return r;
 }  }
   
   
 /* Attempt to parse a KRL, checking its signature (if any) with sign_ca_keys. */  /* Attempt to parse a KRL, checking its signature (if any) with sign_ca_keys. */
 int  int
 ssh_krl_from_blob(Buffer *buf, struct ssh_krl **krlp,  ssh_krl_from_blob(struct sshbuf *buf, struct ssh_krl **krlp,
     const Key **sign_ca_keys, u_int nsign_ca_keys)      const struct sshkey **sign_ca_keys, u_int nsign_ca_keys)
 {  {
         Buffer copy, sect;          struct sshbuf *copy = NULL, *sect = NULL;
         struct ssh_krl *krl;          struct ssh_krl *krl = NULL;
         char timestamp[64];          char timestamp[64];
         int ret = -1, r, sig_seen;          int r = -1, sig_seen;
         Key *key = NULL, **ca_used = NULL;          struct sshkey *key = NULL, **ca_used = NULL, **tmp_ca_used;
         u_char type, *rdata = NULL;          u_char type, *rdata = NULL;
         const u_char *blob;          const u_char *blob;
         u_int i, j, sig_off, sects_off, rlen, blen, format_version, nca_used;          size_t i, j, sig_off, sects_off, rlen, blen, nca_used;
           u_int format_version;
   
         nca_used = 0;          nca_used = 0;
         *krlp = NULL;          *krlp = NULL;
         if (buffer_len(buf) < sizeof(KRL_MAGIC) - 1 ||          if (sshbuf_len(buf) < sizeof(KRL_MAGIC) - 1 ||
             memcmp(buffer_ptr(buf), KRL_MAGIC, sizeof(KRL_MAGIC) - 1) != 0) {              memcmp(sshbuf_ptr(buf), KRL_MAGIC, sizeof(KRL_MAGIC) - 1) != 0) {
                 debug3("%s: not a KRL", __func__);                  debug3("%s: not a KRL", __func__);
                 /*                  return SSH_ERR_KRL_BAD_MAGIC;
                  * Return success but a NULL *krlp here to signal that the  
                  * file might be a simple list of keys.  
                  */  
                 return 0;  
         }          }
   
         /* Take a copy of the KRL buffer so we can verify its signature later */          /* Take a copy of the KRL buffer so we can verify its signature later */
         buffer_init(&copy);          if ((copy = sshbuf_fromb(buf)) == NULL) {
         buffer_append(&copy, buffer_ptr(buf), buffer_len(buf));                  r = SSH_ERR_ALLOC_FAIL;
                   goto out;
           }
           if ((r = sshbuf_consume(copy, sizeof(KRL_MAGIC) - 1)) != 0)
                   goto out;
   
         buffer_init(&sect);  
         buffer_consume(&copy, sizeof(KRL_MAGIC) - 1);  
   
         if ((krl = ssh_krl_init()) == NULL) {          if ((krl = ssh_krl_init()) == NULL) {
                 error("%s: alloc failed", __func__);                  error("%s: alloc failed", __func__);
                 goto out;                  goto out;
         }          }
   
         if (buffer_get_int_ret(&format_version, &copy) != 0) {          if ((r = sshbuf_get_u32(copy, &format_version)) != 0)
                 error("%s: KRL truncated", __func__);  
                 goto out;                  goto out;
         }  
         if (format_version != KRL_FORMAT_VERSION) {          if (format_version != KRL_FORMAT_VERSION) {
                 error("%s: KRL unsupported format version %u",                  r = SSH_ERR_INVALID_FORMAT;
                     __func__, format_version);  
                 goto out;                  goto out;
         }          }
         if (buffer_get_int64_ret(&krl->krl_version, &copy) != 0 ||          if ((r = sshbuf_get_u64(copy, &krl->krl_version)) != 0 ||
             buffer_get_int64_ret(&krl->generated_date, &copy) != 0 ||              (r = sshbuf_get_u64(copy, &krl->generated_date)) != 0 ||
             buffer_get_int64_ret(&krl->flags, &copy) != 0 ||              (r = sshbuf_get_u64(copy, &krl->flags)) != 0 ||
             buffer_get_string_ptr_ret(&copy, NULL) == NULL || /* reserved */              (r = sshbuf_skip_string(copy)) != 0 ||
             (krl->comment = buffer_get_cstring_ret(&copy, NULL)) == NULL) {              (r = sshbuf_get_cstring(copy, &krl->comment, NULL)) != 0)
                 error("%s: buffer error", __func__);  
                 goto out;                  goto out;
         }  
   
         format_timestamp(krl->generated_date, timestamp, sizeof(timestamp));          format_timestamp(krl->generated_date, timestamp, sizeof(timestamp));
         debug("KRL version %llu generated at %s%s%s",          debug("KRL version %llu generated at %s%s%s",
Line 948 
Line 947 
          * detailed parsing of data whose provenance is unverified.           * detailed parsing of data whose provenance is unverified.
          */           */
         sig_seen = 0;          sig_seen = 0;
         sects_off = buffer_len(buf) - buffer_len(&copy);          if (sshbuf_len(buf) < sshbuf_len(copy)) {
         while (buffer_len(&copy) > 0) {                  /* Shouldn't happen */
                 if (buffer_get_char_ret(&type, &copy) != 0 ||                  r = SSH_ERR_INTERNAL_ERROR;
                     (blob = buffer_get_string_ptr_ret(&copy, &blen)) == NULL) {                  goto out;
                         error("%s: buffer error", __func__);          }
           sects_off = sshbuf_len(buf) - sshbuf_len(copy);
           while (sshbuf_len(copy) > 0) {
                   if ((r = sshbuf_get_u8(copy, &type)) != 0 ||
                       (r = sshbuf_get_string_direct(copy, &blob, &blen)) != 0)
                         goto out;                          goto out;
                 }  
                 debug3("%s: first pass, section 0x%02x", __func__, type);                  debug3("%s: first pass, section 0x%02x", __func__, type);
                 if (type != KRL_SECTION_SIGNATURE) {                  if (type != KRL_SECTION_SIGNATURE) {
                         if (sig_seen) {                          if (sig_seen) {
                                   r = SSH_ERR_INVALID_FORMAT;
                                 error("KRL contains non-signature section "                                  error("KRL contains non-signature section "
                                     "after signature");                                      "after signature");
                                 goto out;                                  goto out;
Line 967 
Line 970 
                 }                  }
                 sig_seen = 1;                  sig_seen = 1;
                 /* First string component is the signing key */                  /* First string component is the signing key */
                 if ((key = key_from_blob(blob, blen)) == NULL) {                  if ((r = sshkey_from_blob(blob, blen, &key)) != 0) {
                           r = SSH_ERR_INVALID_FORMAT;
                         error("%s: invalid signature key", __func__);                          error("%s: invalid signature key", __func__);
                         goto out;                          goto out;
                 }                  }
                 sig_off = buffer_len(buf) - buffer_len(&copy);                  if (sshbuf_len(buf) < sshbuf_len(copy)) {
                           /* Shouldn't happen */
                           r = SSH_ERR_INTERNAL_ERROR;
                           goto out;
                   }
                   sig_off = sshbuf_len(buf) - sshbuf_len(copy);
                 /* Second string component is the signature itself */                  /* Second string component is the signature itself */
                 if ((blob = buffer_get_string_ptr_ret(&copy, &blen)) == NULL) {                  if ((r = sshbuf_get_string_direct(copy, &blob, &blen)) != 0) {
                         error("%s: buffer error", __func__);                          r = SSH_ERR_INVALID_FORMAT;
                         goto out;                          goto out;
                 }                  }
                 /* Check signature over entire KRL up to this point */                  /* Check signature over entire KRL up to this point */
                 if (key_verify(key, blob, blen,                  if ((r = sshkey_verify(key, blob, blen,
                     buffer_ptr(buf), buffer_len(buf) - sig_off) != 1) {                      sshbuf_ptr(buf), sshbuf_len(buf) - sig_off, 0)) != 0) {
                         error("bad signaure on KRL");                          error("bad signaure on KRL");
                         goto out;                          goto out;
                 }                  }
                 /* Check if this key has already signed this KRL */                  /* Check if this key has already signed this KRL */
                 for (i = 0; i < nca_used; i++) {                  for (i = 0; i < nca_used; i++) {
                         if (key_equal(ca_used[i], key)) {                          if (sshkey_equal(ca_used[i], key)) {
                                 error("KRL signed more than once with "                                  error("KRL signed more than once with "
                                     "the same key");                                      "the same key");
                                   r = SSH_ERR_SIGNATURE_INVALID;
                                 goto out;                                  goto out;
                         }                          }
                 }                  }
                 /* Record keys used to sign the KRL */                  /* Record keys used to sign the KRL */
                 ca_used = xrealloc(ca_used, nca_used + 1, sizeof(*ca_used));                  tmp_ca_used = reallocarray(ca_used, nca_used + 1,
                       sizeof(*ca_used));
                   if (tmp_ca_used == NULL) {
                           r = SSH_ERR_ALLOC_FAIL;
                           goto out;
                   }
                   ca_used = tmp_ca_used;
                 ca_used[nca_used++] = key;                  ca_used[nca_used++] = key;
                 key = NULL;                  key = NULL;
                 break;                  break;
         }          }
   
           if (sshbuf_len(copy) != 0) {
                   /* Shouldn't happen */
                   r = SSH_ERR_INTERNAL_ERROR;
                   goto out;
           }
   
         /*          /*
          * 2nd pass: parse and load the KRL, skipping the header to the point           * 2nd pass: parse and load the KRL, skipping the header to the point
          * where the section start.           * where the section start.
          */           */
         buffer_append(&copy, (u_char*)buffer_ptr(buf) + sects_off,          sshbuf_free(copy);
             buffer_len(buf) - sects_off);          if ((copy = sshbuf_fromb(buf)) == NULL) {
         while (buffer_len(&copy) > 0) {                  r = SSH_ERR_ALLOC_FAIL;
                 if (buffer_get_char_ret(&type, &copy) != 0 ||                  goto out;
                     (blob = buffer_get_string_ptr_ret(&copy, &blen)) == NULL) {          }
                         error("%s: buffer error", __func__);          if ((r = sshbuf_consume(copy, sects_off)) != 0)
                   goto out;
           while (sshbuf_len(copy) > 0) {
                   if (sect != NULL) {
                           sshbuf_free(sect);
                           sect = NULL;
                   }
                   if ((r = sshbuf_get_u8(copy, &type)) != 0 ||
                       (r = sshbuf_froms(copy, &sect)) != 0) {
                         goto out;                          goto out;
                 }                  }
                 debug3("%s: second pass, section 0x%02x", __func__, type);                  debug3("%s: second pass, section 0x%02x", __func__, type);
                 buffer_clear(&sect);  
                 buffer_append(&sect, blob, blen);  
   
                 switch (type) {                  switch (type) {
                 case KRL_SECTION_CERTIFICATES:                  case KRL_SECTION_CERTIFICATES:
                         if ((r = parse_revoked_certs(&sect, krl)) != 0)                          if ((r = parse_revoked_certs(sect, krl)) != 0)
                                 goto out;                                  goto out;
                         break;                          break;
                 case KRL_SECTION_EXPLICIT_KEY:                  case KRL_SECTION_EXPLICIT_KEY:
                 case KRL_SECTION_FINGERPRINT_SHA1:                  case KRL_SECTION_FINGERPRINT_SHA1:
                         while (buffer_len(&sect) > 0) {                          while (sshbuf_len(sect) > 0) {
                                 if ((rdata = buffer_get_string_ret(&sect,                                  if ((r = sshbuf_get_string(sect,
                                     &rlen)) == NULL) {                                      &rdata, &rlen)) != 0)
                                         error("%s: buffer error", __func__);  
                                         goto out;                                          goto out;
                                 }  
                                 if (type == KRL_SECTION_FINGERPRINT_SHA1 &&                                  if (type == KRL_SECTION_FINGERPRINT_SHA1 &&
                                     rlen != 20) {                                      rlen != 20) {
                                         error("%s: bad SHA1 length", __func__);                                          error("%s: bad SHA1 length", __func__);
                                           r = SSH_ERR_INVALID_FORMAT;
                                         goto out;                                          goto out;
                                 }                                  }
                                 if (revoke_blob(                                  if ((r = revoke_blob(
                                     type == KRL_SECTION_EXPLICIT_KEY ?                                      type == KRL_SECTION_EXPLICIT_KEY ?
                                     &krl->revoked_keys : &krl->revoked_sha1s,                                      &krl->revoked_keys : &krl->revoked_sha1s,
                                     rdata, rlen) != 0)                                      rdata, rlen)) != 0)
                                         goto out;                                          goto out;
                                 rdata = NULL; /* revoke_blob frees blob */                                  rdata = NULL; /* revoke_blob frees blob */
                         }                          }
                         break;                          break;
                 case KRL_SECTION_SIGNATURE:                  case KRL_SECTION_SIGNATURE:
                         /* Handled above, but still need to stay in synch */                          /* Handled above, but still need to stay in synch */
                         buffer_clear(&sect);                          sshbuf_reset(sect);
                         if ((blob = buffer_get_string_ptr_ret(&copy,                          sect = NULL;
                             &blen)) == NULL) {                          if ((r = sshbuf_skip_string(copy)) != 0)
                                 error("%s: buffer error", __func__);  
                                 goto out;                                  goto out;
                         }  
                         break;                          break;
                 default:                  default:
                         error("Unsupported KRL section %u", type);                          error("Unsupported KRL section %u", type);
                           r = SSH_ERR_INVALID_FORMAT;
                         goto out;                          goto out;
                 }                  }
                 if (buffer_len(&sect) > 0) {                  if (sshbuf_len(sect) > 0) {
                         error("KRL section contains unparsed data");                          error("KRL section contains unparsed data");
                           r = SSH_ERR_INVALID_FORMAT;
                         goto out;                          goto out;
                 }                  }
         }          }
Line 1065 
Line 1092 
                 if (ssh_krl_check_key(krl, ca_used[i]) == 0)                  if (ssh_krl_check_key(krl, ca_used[i]) == 0)
                         sig_seen = 1;                          sig_seen = 1;
                 else {                  else {
                         key_free(ca_used[i]);                          sshkey_free(ca_used[i]);
                         ca_used[i] = NULL;                          ca_used[i] = NULL;
                 }                  }
         }          }
         if (nca_used && !sig_seen) {          if (nca_used && !sig_seen) {
                   r = SSH_ERR_SIGNATURE_INVALID;
                 error("All keys used to sign KRL were revoked");                  error("All keys used to sign KRL were revoked");
                 goto out;                  goto out;
         }          }
Line 1081 
Line 1109 
                         for (j = 0; j < nca_used; j++) {                          for (j = 0; j < nca_used; j++) {
                                 if (ca_used[j] == NULL)                                  if (ca_used[j] == NULL)
                                         continue;                                          continue;
                                 if (key_equal(ca_used[j], sign_ca_keys[i])) {                                  if (sshkey_equal(ca_used[j], sign_ca_keys[i])) {
                                         sig_seen = 1;                                          sig_seen = 1;
                                         break;                                          break;
                                 }                                  }
                         }                          }
                 }                  }
                 if (!sig_seen) {                  if (!sig_seen) {
                           r = SSH_ERR_SIGNATURE_INVALID;
                         error("KRL not signed with any trusted key");                          error("KRL not signed with any trusted key");
                         goto out;                          goto out;
                 }                  }
         }          }
   
         *krlp = krl;          *krlp = krl;
         ret = 0;          r = 0;
  out:   out:
         if (ret != 0)          if (r != 0)
                 ssh_krl_free(krl);                  ssh_krl_free(krl);
         for (i = 0; i < nca_used; i++) {          for (i = 0; i < nca_used; i++)
                 if (ca_used[i] != NULL)                  sshkey_free(ca_used[i]);
                         key_free(ca_used[i]);  
         }  
         free(ca_used);          free(ca_used);
         free(rdata);          free(rdata);
         if (key != NULL)          sshkey_free(key);
                 key_free(key);          sshbuf_free(copy);
         buffer_free(&copy);          sshbuf_free(sect);
         buffer_free(&sect);          return r;
         return ret;  
 }  }
   
 /* Checks whether a given key/cert is revoked. Does not check its CA */  /* Checks whether a given key/cert is revoked. Does not check its CA */
 static int  static int
 is_key_revoked(struct ssh_krl *krl, const Key *key)  is_key_revoked(struct ssh_krl *krl, const struct sshkey *key)
 {  {
         struct revoked_blob rb, *erb;          struct revoked_blob rb, *erb;
         struct revoked_serial rs, *ers;          struct revoked_serial rs, *ers;
         struct revoked_key_id rki, *erki;          struct revoked_key_id rki, *erki;
         struct revoked_certs *rc;          struct revoked_certs *rc;
           int r;
   
         /* Check explicitly revoked hashes first */          /* Check explicitly revoked hashes first */
         memset(&rb, 0, sizeof(rb));          memset(&rb, 0, sizeof(rb));
         if ((rb.blob = key_fingerprint_raw(key, SSH_FP_SHA1, &rb.len)) == NULL)          if ((r = sshkey_fingerprint_raw(key, SSH_FP_SHA1,
                 return -1;              &rb.blob, &rb.len)) != 0)
                   return r;
         erb = RB_FIND(revoked_blob_tree, &krl->revoked_sha1s, &rb);          erb = RB_FIND(revoked_blob_tree, &krl->revoked_sha1s, &rb);
         free(rb.blob);          free(rb.blob);
         if (erb != NULL) {          if (erb != NULL) {
                 debug("%s: revoked by key SHA1", __func__);                  debug("%s: revoked by key SHA1", __func__);
                 return -1;                  return SSH_ERR_KEY_REVOKED;
         }          }
   
         /* Next, explicit keys */          /* Next, explicit keys */
         memset(&rb, 0, sizeof(rb));          memset(&rb, 0, sizeof(rb));
         if (plain_key_blob(key, &rb.blob, &rb.len) < 0)          if ((r = plain_key_blob(key, &rb.blob, &rb.len)) != 0)
                 return -1;                  return r;
         erb = RB_FIND(revoked_blob_tree, &krl->revoked_keys, &rb);          erb = RB_FIND(revoked_blob_tree, &krl->revoked_keys, &rb);
         free(rb.blob);          free(rb.blob);
         if (erb != NULL) {          if (erb != NULL) {
                 debug("%s: revoked by explicit key", __func__);                  debug("%s: revoked by explicit key", __func__);
                 return -1;                  return SSH_ERR_KEY_REVOKED;
         }          }
   
         if (!key_is_cert(key))          if (!sshkey_is_cert(key))
                 return 0;                  return 0;
   
         /* Check cert revocation */          /* Check cert revocation */
         if (revoked_certs_for_ca_key(krl, key->cert->signature_key,          if ((r = revoked_certs_for_ca_key(krl, key->cert->signature_key,
             &rc, 0) != 0)              &rc, 0)) != 0)
                 return -1;                  return r;
         if (rc == NULL)          if (rc == NULL)
                 return 0; /* No entry for this CA */                  return 0; /* No entry for this CA */
   
Line 1158 
Line 1186 
         erki = RB_FIND(revoked_key_id_tree, &rc->revoked_key_ids, &rki);          erki = RB_FIND(revoked_key_id_tree, &rc->revoked_key_ids, &rki);
         if (erki != NULL) {          if (erki != NULL) {
                 debug("%s: revoked by key ID", __func__);                  debug("%s: revoked by key ID", __func__);
                 return -1;                  return SSH_ERR_KEY_REVOKED;
         }          }
   
         /*          /*
          * Legacy cert formats lack serial numbers. Zero serials numbers           * Legacy cert formats lack serial numbers. Zero serials numbers
          * are ignored (it's the default when the CA doesn't specify one).           * are ignored (it's the default when the CA doesn't specify one).
          */           */
         if (key_cert_is_legacy(key) || key->cert->serial == 0)          if (sshkey_cert_is_legacy(key) || key->cert->serial == 0)
                 return 0;                  return 0;
   
         memset(&rs, 0, sizeof(rs));          memset(&rs, 0, sizeof(rs));
Line 1175 
Line 1203 
                 KRL_DBG(("%s: %llu matched %llu:%llu", __func__,                  KRL_DBG(("%s: %llu matched %llu:%llu", __func__,
                     key->cert->serial, ers->lo, ers->hi));                      key->cert->serial, ers->lo, ers->hi));
                 debug("%s: revoked by serial", __func__);                  debug("%s: revoked by serial", __func__);
                 return -1;                  return SSH_ERR_KEY_REVOKED;
         }          }
         KRL_DBG(("%s: %llu no match", __func__, key->cert->serial));          KRL_DBG(("%s: %llu no match", __func__, key->cert->serial));
   
Line 1183 
Line 1211 
 }  }
   
 int  int
 ssh_krl_check_key(struct ssh_krl *krl, const Key *key)  ssh_krl_check_key(struct ssh_krl *krl, const struct sshkey *key)
 {  {
         int r;          int r;
   
         debug2("%s: checking key", __func__);          debug2("%s: checking key", __func__);
         if ((r = is_key_revoked(krl, key)) != 0)          if ((r = is_key_revoked(krl, key)) != 0)
                 return r;                  return r;
         if (key_is_cert(key)) {          if (sshkey_is_cert(key)) {
                 debug2("%s: checking CA key", __func__);                  debug2("%s: checking CA key", __func__);
                 if ((r = is_key_revoked(krl, key->cert->signature_key)) != 0)                  if ((r = is_key_revoked(krl, key->cert->signature_key)) != 0)
                         return r;                          return r;
Line 1199 
Line 1227 
         return 0;          return 0;
 }  }
   
 /* Returns 0 on success, -1 on error or key revoked, -2 if path is not a KRL */  
 int  int
 ssh_krl_file_contains_key(const char *path, const Key *key)  ssh_krl_file_contains_key(const char *path, const struct sshkey *key)
 {  {
         Buffer krlbuf;          struct sshbuf *krlbuf = NULL;
         struct ssh_krl *krl;          struct ssh_krl *krl = NULL;
         int revoked, fd;          int oerrno = 0, r, fd;
   
         if (path == NULL)          if (path == NULL)
                 return 0;                  return 0;
   
           if ((krlbuf = sshbuf_new()) == NULL)
                   return SSH_ERR_ALLOC_FAIL;
         if ((fd = open(path, O_RDONLY)) == -1) {          if ((fd = open(path, O_RDONLY)) == -1) {
                 error("open %s: %s", path, strerror(errno));                  r = SSH_ERR_SYSTEM_ERROR;
                 error("Revoked keys file not accessible - refusing public key "                  oerrno = errno;
                     "authentication");                  goto out;
                 return -1;  
         }          }
         buffer_init(&krlbuf);          if ((r = sshkey_load_file(fd, path, krlbuf)) != 0) {
         if (!key_load_file(fd, path, &krlbuf)) {                  oerrno = errno;
                 close(fd);                  goto out;
                 buffer_free(&krlbuf);  
                 error("Revoked keys file not readable - refusing public key "  
                     "authentication");  
                 return -1;  
         }          }
         close(fd);          if ((r = ssh_krl_from_blob(krlbuf, &krl, NULL, 0)) != 0)
         if (ssh_krl_from_blob(&krlbuf, &krl, NULL, 0) != 0) {                  goto out;
                 buffer_free(&krlbuf);  
                 error("Invalid KRL, refusing public key "  
                     "authentication");  
                 return -1;  
         }  
         buffer_free(&krlbuf);  
         if (krl == NULL) {  
                 debug3("%s: %s is not a KRL file", __func__, path);  
                 return -2;  
         }  
         debug2("%s: checking KRL %s", __func__, path);          debug2("%s: checking KRL %s", __func__, path);
         revoked = ssh_krl_check_key(krl, key) != 0;          r = ssh_krl_check_key(krl, key);
    out:
           close(fd);
           sshbuf_free(krlbuf);
         ssh_krl_free(krl);          ssh_krl_free(krl);
         return revoked ? -1 : 0;          if (r != 0)
                   errno = oerrno;
           return r;
 }  }

Legend:
Removed from v.1.19  
changed lines
  Added in v.1.20