Make X509_certificate_type() less bad
authortb <tb@openbsd.org>
Mon, 13 Nov 2023 16:16:14 +0000 (16:16 +0000)
committertb <tb@openbsd.org>
Mon, 13 Nov 2023 16:16:14 +0000 (16:16 +0000)
This converts to proper single exit and undoes a number of unnecessarily
silly muppet antics.

ok beck

lib/libcrypto/x509/x509type.c

index 7d07d2b..1698d8a 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: x509type.c,v 1.23 2023/11/13 15:44:15 tb Exp $ */
+/* $OpenBSD: x509type.c,v 1.24 2023/11/13 16:16:14 tb Exp $ */
 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
@@ -73,12 +73,12 @@ X509_certificate_type(const X509 *x, const EVP_PKEY *pkey)
        int ret = 0;
 
        if (x == NULL)
-               return (0);
+               goto done;
 
-       if (pk == NULL) {
-               if ((pk = X509_get0_pubkey(x)) == NULL)
-                       return (0);
-       }
+       if (pk == NULL)
+               pk = X509_get0_pubkey(x);
+       if (pk == NULL)
+               goto done;
 
        switch (pk->type) {
        case EVP_PKEY_RSA:
@@ -107,25 +107,30 @@ X509_certificate_type(const X509 *x, const EVP_PKEY *pkey)
                break;
        }
 
-       nid = X509_get_signature_nid(x);
-       if (nid && OBJ_find_sigid_algs(nid, NULL, &nid)) {
-               switch (nid) {
-               case NID_rsaEncryption:
-               case NID_rsa:
-                       ret |= EVP_PKS_RSA;
-                       break;
-               case NID_dsa:
-               case NID_dsa_2:
-                       ret |= EVP_PKS_DSA;
-                       break;
-               case NID_X9_62_id_ecPublicKey:
-                       ret |= EVP_PKS_EC;
-                       break;
-               default:
-                       break;
-               }
+       if ((nid = X509_get_signature_nid(x)) == NID_undef)
+               goto done;
+
+       if (!OBJ_find_sigid_algs(nid, NULL, &nid))
+               goto done;
+
+       switch (nid) {
+       case NID_rsaEncryption:
+       case NID_rsa:
+               ret |= EVP_PKS_RSA;
+               break;
+       case NID_dsa:
+       case NID_dsa_2:
+               ret |= EVP_PKS_DSA;
+               break;
+       case NID_X9_62_id_ecPublicKey:
+               ret |= EVP_PKS_EC;
+               break;
+       default:
+               break;
        }
 
-       return (ret);
+ done:
+
+       return ret;
 }
 LCRYPTO_ALIAS(X509_certificate_type);