Remove redundant copy of EVP_PKEY type

pkey->type and pkey->ameth->pkey_id are always synchronized with each
other. No sense in holding on to both.

Change-Id: Ibd447d31cd12946380e5c66ace1020048a8104e7
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/81308
Commit-Queue: David Benjamin <davidben@google.com>
Reviewed-by: Lily Chen <chlily@google.com>
Auto-Submit: David Benjamin <davidben@google.com>
diff --git a/crypto/evp/evp.cc b/crypto/evp/evp.cc
index a3b8e86..2275c62 100644
--- a/crypto/evp/evp.cc
+++ b/crypto/evp/evp.cc
@@ -41,7 +41,6 @@
     return NULL;
   }
 
-  ret->type = EVP_PKEY_NONE;
   ret->references = 1;
   return ret;
 }
@@ -51,7 +50,6 @@
     pkey->ameth->pkey_free(pkey);
   }
   pkey->pkey = nullptr;
-  pkey->type = EVP_PKEY_NONE;
   pkey->ameth = nullptr;
 }
 
@@ -81,7 +79,7 @@
 }
 
 int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
-  if (a->type != b->type) {
+  if (EVP_PKEY_id(a) != EVP_PKEY_id(b)) {
     return -1;
   }
 
@@ -104,9 +102,9 @@
 }
 
 int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) {
-  if (to->type == EVP_PKEY_NONE) {
+  if (EVP_PKEY_id(to) == EVP_PKEY_NONE) {
     evp_pkey_set_method(to, from->ameth);
-  } else if (to->type != from->type) {
+  } else if (EVP_PKEY_id(to) != EVP_PKEY_id(from)) {
     OPENSSL_PUT_ERROR(EVP, EVP_R_DIFFERENT_KEY_TYPES);
     return 0;
   }
@@ -155,7 +153,9 @@
   return 0;
 }
 
-int EVP_PKEY_id(const EVP_PKEY *pkey) { return pkey->type; }
+int EVP_PKEY_id(const EVP_PKEY *pkey) {
+  return pkey->ameth != nullptr ? pkey->ameth->pkey_id : EVP_PKEY_NONE;
+}
 
 // evp_pkey_asn1_find returns the ASN.1 method table for the given |nid|, which
 // should be one of the |EVP_PKEY_*| values. It returns NULL if |nid| is
@@ -180,7 +180,6 @@
 void evp_pkey_set_method(EVP_PKEY *pkey, const EVP_PKEY_ASN1_METHOD *method) {
   free_it(pkey);
   pkey->ameth = method;
-  pkey->type = pkey->ameth->pkey_id;
 }
 
 int EVP_PKEY_type(int nid) {
@@ -311,7 +310,7 @@
 }
 
 int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) {
-  if (a->type != b->type) {
+  if (EVP_PKEY_id(a) != EVP_PKEY_id(b)) {
     return -1;
   }
   if (a->ameth && a->ameth->param_cmp) {
diff --git a/crypto/evp/evp_asn1.cc b/crypto/evp/evp_asn1.cc
index d77a3d2..6229ce3 100644
--- a/crypto/evp/evp_asn1.cc
+++ b/crypto/evp/evp_asn1.cc
@@ -209,7 +209,7 @@
     if (ret == nullptr) {
       return nullptr;
     }
-    if (ret->type != type) {
+    if (EVP_PKEY_id(ret.get()) != type) {
       OPENSSL_PUT_ERROR(EVP, EVP_R_DIFFERENT_KEY_TYPES);
       return nullptr;
     }
@@ -279,7 +279,7 @@
 }
 
 int i2d_PublicKey(const EVP_PKEY *key, uint8_t **outp) {
-  switch (key->type) {
+  switch (EVP_PKEY_id(key)) {
     case EVP_PKEY_RSA:
       return i2d_RSAPublicKey(EVP_PKEY_get0_RSA(key), outp);
     case EVP_PKEY_DSA:
diff --git a/crypto/evp/evp_ctx.cc b/crypto/evp/evp_ctx.cc
index 5e307c1..bb3ee6e 100644
--- a/crypto/evp/evp_ctx.cc
+++ b/crypto/evp/evp_ctx.cc
@@ -297,7 +297,7 @@
     return 0;
   }
 
-  if (ctx->pkey->type != peer->type) {
+  if (EVP_PKEY_id(ctx->pkey.get()) != EVP_PKEY_id(peer)) {
     OPENSSL_PUT_ERROR(EVP, EVP_R_DIFFERENT_KEY_TYPES);
     return 0;
   }
diff --git a/crypto/evp/internal.h b/crypto/evp/internal.h
index e5256b5..48aa79a 100644
--- a/crypto/evp/internal.h
+++ b/crypto/evp/internal.h
@@ -30,6 +30,8 @@
 typedef struct evp_pkey_method_st EVP_PKEY_METHOD;
 
 struct evp_pkey_asn1_method_st {
+  // type contains one of the |EVP_PKEY_*| values and corresponds to the OID in
+  // the key type's AlgorithmIdentifier.
   int pkey_id;
   uint8_t oid[9];
   uint8_t oid_len;
@@ -94,15 +96,11 @@
 struct evp_pkey_st {
   CRYPTO_refcount_t references;
 
-  // type contains one of the EVP_PKEY_* values or NID_undef and determines
-  // the type of |pkey|.
-  int type;
-
-  // pkey contains a pointer to a structure dependent on |type|.
+  // pkey contains a pointer to a structure dependent on |ameth|.
   void *pkey;
 
-  // ameth contains a pointer to a method table that contains many ASN.1
-  // methods for the key type.
+  // ameth contains a pointer to a method table that determines the key type, or
+  // nullptr if the key is empty.
   const EVP_PKEY_ASN1_METHOD *ameth;
 } /* EVP_PKEY */;
 
diff --git a/crypto/evp/p_dh_asn1.cc b/crypto/evp/p_dh_asn1.cc
index 1c035e7..0f86ed4 100644
--- a/crypto/evp/p_dh_asn1.cc
+++ b/crypto/evp/p_dh_asn1.cc
@@ -126,7 +126,7 @@
 }
 
 DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey) {
-  if (pkey->type != EVP_PKEY_DH) {
+  if (EVP_PKEY_id(pkey) != EVP_PKEY_DH) {
     OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_A_DH_KEY);
     return NULL;
   }
diff --git a/crypto/evp/p_dsa_asn1.cc b/crypto/evp/p_dsa_asn1.cc
index 6c829c0..b07a982 100644
--- a/crypto/evp/p_dsa_asn1.cc
+++ b/crypto/evp/p_dsa_asn1.cc
@@ -257,7 +257,7 @@
 }
 
 DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey) {
-  if (pkey->type != EVP_PKEY_DSA) {
+  if (EVP_PKEY_id(pkey) != EVP_PKEY_DSA) {
     OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_A_DSA_KEY);
     return nullptr;
   }
diff --git a/crypto/evp/p_ec_asn1.cc b/crypto/evp/p_ec_asn1.cc
index d3e4b40..0bd7623 100644
--- a/crypto/evp/p_ec_asn1.cc
+++ b/crypto/evp/p_ec_asn1.cc
@@ -271,7 +271,7 @@
 }
 
 EC_KEY *EVP_PKEY_get0_EC_KEY(const EVP_PKEY *pkey) {
-  if (pkey->type != EVP_PKEY_EC) {
+  if (EVP_PKEY_id(pkey) != EVP_PKEY_EC) {
     OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_AN_EC_KEY_KEY);
     return NULL;
   }
diff --git a/crypto/evp/p_rsa_asn1.cc b/crypto/evp/p_rsa_asn1.cc
index 6c6bfc5..1c6f30b 100644
--- a/crypto/evp/p_rsa_asn1.cc
+++ b/crypto/evp/p_rsa_asn1.cc
@@ -182,7 +182,7 @@
 }
 
 RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey) {
-  if (pkey->type != EVP_PKEY_RSA) {
+  if (EVP_PKEY_id(pkey) != EVP_PKEY_RSA) {
     OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_AN_RSA_KEY);
     return NULL;
   }