From: Claudio Jeker Subject: Re: rpki-client: kill the BN_CTX in the parser To: Theo Buehler Cc: tech@openbsd.org Date: Wed, 11 Nov 2026 13:12:50 +0100 On Wed, Nov 11, 2026 at 11:21:32AM +0100, Theo Buehler wrote: > Before threads were added to the parser, having a global BN_CTX was easy > enough. While it saves many small allocations, I don't think it's worth > the complexity. > > A bignum is a struct containing a pointer to an array of words and then > four ints. Sequence numbers are at most 20 octets (fitting into 3 8-bit > words on 64 bits), so each seqnum requires at most 48 bytes. We check > with two bignums instead of three without loss of clarity, so we have > all this complexity to avoid allocations of at most 96 bytes. Fine with me. OK claudio@ > Index: extern.h > =================================================================== > RCS file: /cvs/src/usr.sbin/rpki-client/extern.h,v > diff -u -p -r1.322 extern.h > --- extern.h 10 Nov 2026 06:53:27 -0000 1.322 > +++ extern.h 11 Nov 2026 09:40:29 -0000 > @@ -802,8 +802,7 @@ const struct signed_obj *mft_obj(void); > struct mft *mft_read(struct ibuf *); > int mft_compare_issued(const struct mft *, const struct mft *); > int mft_compare_seqnum(const struct mft *, const struct mft *); > -int mft_seqnum_gap_present(const struct mft *, const struct mft *, > - BN_CTX *); > +int mft_seqnum_gap_present(const struct mft *, const struct mft *); > > void roa_buffer(struct ibuf *, const struct roa *); > void roa_free(struct roa *); > Index: mft.c > =================================================================== > RCS file: /cvs/src/usr.sbin/rpki-client/mft.c,v > diff -u -p -r1.149 mft.c > --- mft.c 3 Nov 2026 14:29:03 -0000 1.149 > +++ mft.c 11 Nov 2026 09:43:09 -0000 > @@ -588,29 +588,25 @@ mft_compare_seqnum(const struct mft *a, > * Return 1 if a gap is detected. > */ > int > -mft_seqnum_gap_present(const struct mft *a, const struct mft *b, BN_CTX *bn_ctx) > +mft_seqnum_gap_present(const struct mft *a, const struct mft *b) > { > - BIGNUM *diff, *seqnum_a, *seqnum_b; > + BIGNUM *seqnum_a = NULL, *seqnum_b = NULL; > int ret = 0; > > - BN_CTX_start(bn_ctx); > - if ((diff = BN_CTX_get(bn_ctx)) == NULL || > - (seqnum_a = BN_CTX_get(bn_ctx)) == NULL || > - (seqnum_b = BN_CTX_get(bn_ctx)) == NULL) > - errx(1, "BN_CTX_get"); > - > if (!BN_hex2bn(&seqnum_a, a->seqnum)) > errx(1, "BN_hex2bn"); > > if (!BN_hex2bn(&seqnum_b, b->seqnum)) > errx(1, "BN_hex2bn"); > > - if (!BN_sub(diff, seqnum_a, seqnum_b)) > - errx(1, "BN_sub"); > + if (!BN_add_word(seqnum_b, 1)) > + errx(1, "BN_add_word"); > > - ret = !BN_is_one(diff); > + if (BN_cmp(seqnum_a, seqnum_b) != 0) > + ret = 1; > > - BN_CTX_end(bn_ctx); > + BN_free(seqnum_a); > + BN_free(seqnum_b); > > return ret; > } > Index: parser.c > =================================================================== > RCS file: /cvs/src/usr.sbin/rpki-client/parser.c,v > diff -u -p -r1.202 parser.c > --- parser.c 10 Nov 2026 11:04:01 -0000 1.202 > +++ parser.c 11 Nov 2026 09:45:00 -0000 > @@ -395,7 +395,7 @@ parse_load_crl_from_mft(struct entity *e > static struct mft * > proc_parser_mft_pre(struct entity *ent, char *file, struct crl **crl, > char **crlfile, struct mft *cached_mft, const char **errstr, > - X509_STORE_CTX *ctx, BN_CTX *bn_ctx) > + X509_STORE_CTX *ctx) > { > struct mft *mft; > struct cert *cert = NULL; > @@ -503,7 +503,7 @@ proc_parser_mft_pre(struct entity *ent, > } > > if (seqnum_cmp > 0) { > - if (mft_seqnum_gap_present(mft, cached_mft, bn_ctx)) { > + if (mft_seqnum_gap_present(mft, cached_mft)) { > mft->seqnum_gap = 1; > if (verbose) > warnx("%s: seqnum gap detected #%s -> #%s", > @@ -528,7 +528,7 @@ proc_parser_mft_pre(struct entity *ent, > */ > static char * > proc_parser_mft(struct entity *ent, struct mft **mp, char **crlfile, > - time_t *crlmtime, X509_STORE_CTX *ctx, BN_CTX *bn_ctx) > + time_t *crlmtime, X509_STORE_CTX *ctx) > { > struct mft *mft1 = NULL, *mft2 = NULL; > struct crl *crl, *crl1 = NULL, *crl2 = NULL; > @@ -541,13 +541,13 @@ proc_parser_mft(struct entity *ent, stru > > file2 = parse_filepath(ent->repoid, ent->path, ent->file, DIR_VALID); > mft2 = proc_parser_mft_pre(ent, file2, &crl2, &crl2file, NULL, > - &err2, ctx, bn_ctx); > + &err2, ctx); > > if (!noop) { > file1 = parse_filepath(ent->repoid, ent->path, ent->file, > DIR_TEMP); > mft1 = proc_parser_mft_pre(ent, file1, &crl1, &crl1file, mft2, > - &err1, ctx, bn_ctx); > + &err1, ctx); > } > > if (proc_parser_mft_check(file1, mft1)) { > @@ -917,7 +917,7 @@ parse_load_file(struct entity *ent, unsi > */ > static void > parse_entity(struct entityq *q, struct ibufqueue *msgq, struct entityq *mftq, > - X509_STORE_CTX *ctx, BN_CTX *bn_ctx) > + X509_STORE_CTX *ctx) > { > struct entity *ent; > struct tal *tal; > @@ -992,7 +992,7 @@ parse_entity(struct entityq *q, struct i > break; > case RTYPE_MFT: > file = proc_parser_mft(ent, &mft, &crlfile, &crlmtime, > - ctx, bn_ctx); > + ctx); > io_str_buffer(b, file); > if (mft != NULL) > mtime = mft->signtime; > @@ -1105,13 +1105,10 @@ parse_worker(void *arg) > struct entity *ent; > struct ibufqueue *myq; > X509_STORE_CTX *ctx; > - BN_CTX *bn_ctx; > int n; > > if ((ctx = X509_STORE_CTX_new()) == NULL) > err(1, "X509_STORE_CTX_new"); > - if ((bn_ctx = BN_CTX_new()) == NULL) > - err(1, "BN_CTX_new"); > if ((myq = ibufq_new()) == NULL) > err(1, "ibufqueue_new"); > > @@ -1135,7 +1132,7 @@ parse_worker(void *arg) > cond_signal(&globalq_cond); > mtx_leave(&globalq_mtx); > > - parse_entity(&q, myq, &mftq, ctx, bn_ctx); > + parse_entity(&q, myq, &mftq, ctx); > > if (ibufq_queuelen(myq) > 0) { > mtx_enter(&globalmsgq_mtx); > @@ -1153,7 +1150,6 @@ parse_worker(void *arg) > } > > X509_STORE_CTX_free(ctx); > - BN_CTX_free(bn_ctx); > ibufq_free(myq); > return NULL; > } > -- :wq Claudio