Convert ASN1_INTEGER_get() to ASN1_INTEGER_get_uint64()
authortb <tb@openbsd.org>
Tue, 23 May 2023 06:42:08 +0000 (06:42 +0000)
committertb <tb@openbsd.org>
Tue, 23 May 2023 06:42:08 +0000 (06:42 +0000)
The former is broken by design and should not be used. The latter allows
for unambiguous error checking. Add a few casts to print uint64_t without
the PRIu64 monstrosity.

ok claudio

usr.sbin/rpki-client/roa.c
usr.sbin/rpki-client/validate.c

index 705208c..206cd01 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: roa.c,v 1.66 2023/04/26 16:32:41 claudio Exp $ */
+/*     $OpenBSD: roa.c,v 1.67 2023/05/23 06:42:08 tb Exp $ */
 /*
  * Copyright (c) 2022 Theo Buehler <tb@openbsd.org>
  * Copyright (c) 2019 Kristaps Dzonsons <kristaps@bsd.lv>
@@ -107,7 +107,7 @@ roa_parse_econtent(const unsigned char *d, size_t dsz, struct parse *p)
        int                              addrsz;
        enum afi                         afi;
        const ROAIPAddress              *addr;
-       long                             maxlen;
+       uint64_t                         maxlen;
        struct ip_addr                   ipaddr;
        struct roa_ip                   *res;
        int                              ipaddrblocksz;
@@ -168,21 +168,23 @@ roa_parse_econtent(const unsigned char *d, size_t dsz, struct parse *p)
                        maxlen = ipaddr.prefixlen;
 
                        if (addr->maxLength != NULL) {
-                               maxlen = ASN1_INTEGER_get(addr->maxLength);
-                               if (maxlen < 0) {
+                               if (!ASN1_INTEGER_get_uint64(&maxlen,
+                                   addr->maxLength)) {
                                        warnx("%s: RFC 6482 section 3.2: "
-                                           "ASN1_INTEGER_get failed", p->fn);
+                                           "ASN1_INTEGER_get_uint64 failed",
+                                           p->fn);
                                        goto out;
                                }
                                if (ipaddr.prefixlen > maxlen) {
                                        warnx("%s: prefixlen (%d) larger than "
-                                           "maxLength (%ld)", p->fn,
-                                           ipaddr.prefixlen, maxlen);
+                                           "maxLength (%llu)", p->fn,
+                                           ipaddr.prefixlen,
+                                           (unsigned long long)maxlen);
                                        goto out;
                                }
                                if (maxlen > ((afi == AFI_IPV4) ? 32 : 128)) {
-                                       warnx("%s: maxLength (%ld) too large",
-                                           p->fn, maxlen);
+                                       warnx("%s: maxLength (%llu) too large",
+                                           p->fn, (unsigned long long)maxlen);
                                        goto out;
                                }
                        }
index ef0bc7f..0aa2015 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: validate.c,v 1.61 2023/05/11 14:05:31 claudio Exp $ */
+/*     $OpenBSD: validate.c,v 1.62 2023/05/23 06:42:08 tb Exp $ */
 /*
  * Copyright (c) 2019 Kristaps Dzonsons <kristaps@bsd.lv>
  *
@@ -516,13 +516,13 @@ valid_rsc(const char *fn, struct cert *cert, struct rsc *rsc)
 int
 valid_econtent_version(const char *fn, const ASN1_INTEGER *aint)
 {
-       long version;
+       uint64_t version;
 
        if (aint == NULL)
                return 1;
 
-       if ((version = ASN1_INTEGER_get(aint)) < 0) {
-               warnx("%s: ASN1_INTEGER_get failed", fn);
+       if (!ASN1_INTEGER_get_uint64(&version, aint)) {
+               warnx("%s: ASN1_INTEGER_get_uint64 failed", fn);
                return 0;
        }
 
@@ -531,7 +531,8 @@ valid_econtent_version(const char *fn, const ASN1_INTEGER *aint)
                warnx("%s: incorrect encoding for version 0", fn);
                return 0;
        default:
-               warnx("%s: version %ld not supported (yet)", fn, version);
+               warnx("%s: version %llu not supported (yet)", fn,
+                   (unsigned long long)version);
                return 0;
        }
 }