]> git.ipfire.org Git - thirdparty/FORT-validator.git/commitdiff
Merge some duplicate code
authorAlberto Leiva Popper <ydahhrk@gmail.com>
Thu, 16 May 2024 18:42:31 +0000 (12:42 -0600)
committerAlberto Leiva Popper <ydahhrk@gmail.com>
Thu, 16 May 2024 18:42:31 +0000 (12:42 -0600)
New rule: BIOs no longer allowed in ASN1 JSON functions outside of
libcrypto_util.c.

src/asn1/asn1c/Certificate.c
src/libcrypto_util.c
src/libcrypto_util.h

index 76cb7e2fac36e289b7874e5da19a4e77d610f6d5..df6542c446803a6339b90070322255d789ecbe66 100644 (file)
@@ -1,7 +1,6 @@
 #include "asn1/asn1c/Certificate.h"
 
 #include <openssl/x509v3.h>
-#include <openssl/pem.h>
 
 #include "extension.h"
 #include "json_util.h"
@@ -34,39 +33,25 @@ static json_t *
 pk2json(X509 const *x)
 {
        json_t *root;
-       ASN1_OBJECT *xpoid;
-       EVP_PKEY *pkey;
-       BIO *bio;
+       json_t *child;
+       X509_PUBKEY *pubkey;
+       ASN1_OBJECT *oid;
 
        root = json_obj_new();
        if (root == NULL)
                return NULL;
 
-       /* algorithm */
-       if (!X509_PUBKEY_get0_param(&xpoid, NULL, NULL, NULL, X509_get_X509_PUBKEY(x)))
-               goto fail;
-       bio = BIO_new(BIO_s_mem());
-       if (bio == NULL)
-               goto fail;
-       if (i2a_ASN1_OBJECT(bio, xpoid) <= 0) {
-               BIO_free_all(bio);
+       pubkey = X509_get_X509_PUBKEY(x);
+       if (pubkey == NULL)
                goto fail;
-       }
-       if (json_object_add(root, "algorithm", bio2json(bio)))
+       if (!X509_PUBKEY_get0_param(&oid, NULL, NULL, NULL, pubkey))
                goto fail;
 
-       /* Actual pk */
-       pkey = X509_get0_pubkey(x);
-       if (pkey == NULL)
-               goto fail;
-       bio = BIO_new(BIO_s_mem());
-       if (bio == NULL)
-               goto fail;
-       if (PEM_write_bio_PUBKEY(bio, pkey) <= 0) {
-               BIO_free_all(bio);
+       child = oid2json(oid);
+       if (json_object_add(root, "algorithm", child))
                goto fail;
-       }
-       if (json_object_add(root, "subjectPublicKey", bio2json(bio)))
+       child = pubkey2json(X509_PUBKEY_get0(pubkey));
+       if (json_object_add(root, "subjectPublicKey", child))
                goto fail;
 
        return root;
index 3717ae11e23cf5df4856fd9f00ed70698f4fcec2..2608f5f7da5d5e6158224977584b87d8a4135667 100644 (file)
@@ -3,28 +3,15 @@
 #include <stdlib.h>
 #include <openssl/asn1.h>
 #include <openssl/opensslv.h>
+#include <openssl/pem.h>
 
 #include "alloc.h"
 #include "extension.h"
 #include "json_util.h"
+#include "asn1/asn1c/OBJECT_IDENTIFIER.h"
 
 /* Swallows @bio. */
-char *
-bio2str(BIO *bio)
-{
-       BUF_MEM *buffer;
-       char *str;
-
-       str = (BIO_get_mem_ptr(bio, &buffer) > 0)
-           ? pstrndup(buffer->data, buffer->length)
-           : NULL;
-
-       BIO_free_all(bio);
-       return str;
-}
-
-/* Swallows @bio. */
-json_t *
+static json_t *
 bio2json(BIO *bio)
 {
        BUF_MEM *buffer;
@@ -41,7 +28,10 @@ bio2json(BIO *bio)
 json_t *
 oid2json(ASN1_OBJECT const *oid)
 {
-       return oid ? json_str_new(OBJ_nid2sn(OBJ_obj2nid(oid))) : json_null();
+       char buf[OID_STR_MAXLEN];
+       return (oid != NULL)
+            ? json_strn_new(buf, OBJ_obj2txt(buf, OID_STR_MAXLEN, oid, 0))
+            : json_null();
 }
 
 json_t *
@@ -197,6 +187,25 @@ fail:      json_decref(parent);
        return NULL;
 }
 
+json_t *
+pubkey2json(EVP_PKEY *pubkey)
+{
+       BIO *bio;
+
+       if (pubkey == NULL)
+               return NULL;
+
+       bio = BIO_new(BIO_s_mem());
+       if (bio == NULL)
+               return NULL;
+       if (PEM_write_bio_PUBKEY(bio, pubkey) <= 0) {
+               BIO_free_all(bio);
+               return NULL;
+       }
+
+       return bio2json(bio);
+}
+
 static json_t *
 ext2json_known(struct extension_metadata const *meta, X509_EXTENSION *ext)
 {
@@ -247,8 +256,6 @@ exts2json(const STACK_OF(X509_EXTENSION) *exts)
        json_t *parent;
        json_t *child;
        X509_EXTENSION *ex;
-       BIO *bio;
-       char *name;
        int i;
 
        if (sk_X509_EXTENSION_num(exts) <= 0)
@@ -264,19 +271,7 @@ exts2json(const STACK_OF(X509_EXTENSION) *exts)
 
                ex = sk_X509_EXTENSION_value(exts, i);
 
-               /* Get the extension name */
-               bio = BIO_new(BIO_s_mem());
-               if (bio == NULL)
-                       goto fail;
-               if (i2a_ASN1_OBJECT(bio, X509_EXTENSION_get_object(ex)) <= 0) {
-                       BIO_free_all(bio);
-                       goto fail;
-               }
-
-               name = bio2str(bio);
-               child = json_str_new(name);
-               free(name);
-
+               child = oid2json(X509_EXTENSION_get_object(ex));
                if (json_object_add(parent, "extnID", child))
                        goto fail;
                child = json_boolean(X509_EXTENSION_get_critical(ex));
index 3e464f99001318830ea2f8c3dc8f36ecb492624b..4045021c3e62997eb45d2a4bbbf7b6800c338724 100644 (file)
@@ -6,8 +6,6 @@
 #include <openssl/bio.h>
 #include <openssl/x509v3.h>
 
-char *bio2str(BIO *);
-json_t *bio2json(BIO *);
 json_t *oid2json(ASN1_OBJECT const *);
 json_t *asn1int2json(ASN1_INTEGER const *);
 json_t *asn1str2json(ASN1_STRING const *); /* octet string, bit string, etc */
@@ -15,6 +13,7 @@ json_t *asn1time2json(ASN1_TIME const *);
 json_t *name2json(X509_NAME const *);
 json_t *gn2json(GENERAL_NAME const *);
 json_t *gns2json(GENERAL_NAMES const *);
+json_t *pubkey2json(EVP_PKEY *); /* LibreSSL needs not const */
 json_t *exts2json(const STACK_OF(X509_EXTENSION) *);
 
 #endif /* SRC_LIBCRYPTO_UTIL_H_ */