Add tests for AlgorithmIdentifier support in EVP.

Change-Id: I6691c93cfebfb06132e847d7c0bd3dd6bf076cab
Reviewed-on: https://boringssl-review.googlesource.com/1917
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/crypto/evp/example_sign.c b/crypto/evp/example_sign.c
index c25ef2a..42a19ec 100644
--- a/crypto/evp/example_sign.c
+++ b/crypto/evp/example_sign.c
@@ -17,10 +17,13 @@
 #include <stdlib.h>
 
 #include <openssl/bio.h>
+#include <openssl/bytestring.h>
 #include <openssl/crypto.h>
 #include <openssl/digest.h>
+#include <openssl/err.h>
 #include <openssl/evp.h>
 #include <openssl/rsa.h>
+#include <openssl/x509.h>
 
 
 /* kExampleRSAKeyDER is an RSA private key in ASN.1, DER format. Of course, you
@@ -95,26 +98,104 @@
     0x55, 0xa7, 0xab, 0x45, 0x02, 0x97, 0x60, 0x42,
 };
 
+/* kExamplePSSCert is an example self-signed certificate, signed with
+ * kExampleRSAKeyDER using RSA-PSS with default hash functions. */
+static const uint8_t kExamplePSSCert[] = {
+    0x30, 0x82, 0x02, 0x62, 0x30, 0x82, 0x01, 0xc6, 0xa0, 0x03, 0x02, 0x01,
+    0x02, 0x02, 0x09, 0x00, 0x8d, 0xea, 0x53, 0x24, 0xfa, 0x48, 0x87, 0xf3,
+    0x30, 0x12, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
+    0x0a, 0x30, 0x05, 0xa2, 0x03, 0x02, 0x01, 0x6a, 0x30, 0x45, 0x31, 0x0b,
+    0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x41, 0x55, 0x31,
+    0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f,
+    0x6d, 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, 0x21, 0x30, 0x1f,
+    0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72,
+    0x6e, 0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, 0x74, 0x73, 0x20,
+    0x50, 0x74, 0x79, 0x20, 0x4c, 0x74, 0x64, 0x30, 0x1e, 0x17, 0x0d, 0x31,
+    0x34, 0x31, 0x30, 0x30, 0x39, 0x31, 0x39, 0x30, 0x39, 0x35, 0x35, 0x5a,
+    0x17, 0x0d, 0x31, 0x35, 0x31, 0x30, 0x30, 0x39, 0x31, 0x39, 0x30, 0x39,
+    0x35, 0x35, 0x5a, 0x30, 0x45, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55,
+    0x04, 0x06, 0x13, 0x02, 0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03,
+    0x55, 0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65, 0x2d, 0x53, 0x74,
+    0x61, 0x74, 0x65, 0x31, 0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a,
+    0x0c, 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x57,
+    0x69, 0x64, 0x67, 0x69, 0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c,
+    0x74, 0x64, 0x30, 0x81, 0x9f, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48,
+    0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x81, 0x8d, 0x00,
+    0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xf8, 0xb8, 0x6c, 0x83, 0xb4,
+    0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5, 0xb4, 0x59, 0x76, 0x8c, 0x54, 0x1d,
+    0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e, 0xd3, 0x37, 0xeb, 0x41, 0xfd, 0x83,
+    0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34, 0x75, 0x71, 0x5a, 0x84, 0xa8, 0x3c,
+    0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde, 0x97, 0x8a, 0xdd, 0xff, 0xbb, 0xcf,
+    0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8, 0x50, 0xe4, 0xcd, 0x6f, 0x80, 0x33,
+    0x30, 0x76, 0x13, 0x8f, 0xca, 0x7b, 0xdc, 0xec, 0x5a, 0xca, 0x63, 0xc7,
+    0x03, 0x25, 0xef, 0xa8, 0x8a, 0x83, 0x58, 0x76, 0x20, 0xfa, 0x16, 0x77,
+    0xd7, 0x79, 0x92, 0x63, 0x01, 0x48, 0x1a, 0xd8, 0x7b, 0x67, 0xf1, 0x52,
+    0x55, 0x49, 0x4e, 0xd6, 0x6e, 0x4a, 0x5c, 0xd7, 0x7a, 0x37, 0x36, 0x0c,
+    0xde, 0xdd, 0x8f, 0x44, 0xe8, 0xc2, 0xa7, 0x2c, 0x2b, 0xb5, 0xaf, 0x64,
+    0x4b, 0x61, 0x07, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x50, 0x30, 0x4e,
+    0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0xd0,
+    0x41, 0xfb, 0x89, 0x41, 0x1e, 0xa7, 0xad, 0x5a, 0xec, 0x34, 0x5d, 0x49,
+    0x11, 0xf9, 0x55, 0x81, 0x78, 0x1f, 0x13, 0x30, 0x1f, 0x06, 0x03, 0x55,
+    0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0xd0, 0x41, 0xfb, 0x89,
+    0x41, 0x1e, 0xa7, 0xad, 0x5a, 0xec, 0x34, 0x5d, 0x49, 0x11, 0xf9, 0x55,
+    0x81, 0x78, 0x1f, 0x13, 0x30, 0x0c, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04,
+    0x05, 0x30, 0x03, 0x01, 0x01, 0xff, 0x30, 0x12, 0x06, 0x09, 0x2a, 0x86,
+    0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0a, 0x30, 0x05, 0xa2, 0x03, 0x02,
+    0x01, 0x6a, 0x03, 0x81, 0x81, 0x00, 0x49, 0x4c, 0xb6, 0x45, 0x97, 0x20,
+    0x35, 0xb3, 0x50, 0x64, 0x0d, 0x3f, 0xec, 0x5f, 0x95, 0xd5, 0x84, 0xcb,
+    0x11, 0x7c, 0x03, 0xd7, 0xa6, 0xe6, 0xfa, 0x24, 0x95, 0x9f, 0x31, 0xb0,
+    0xb5, 0xec, 0x66, 0x41, 0x51, 0x18, 0x21, 0x91, 0xbb, 0xe0, 0xaf, 0xf0,
+    0xc5, 0xb7, 0x59, 0x41, 0xd4, 0xdb, 0xa4, 0xd2, 0x64, 0xa7, 0x54, 0x0f,
+    0x8c, 0xf7, 0xe1, 0xd3, 0x3b, 0x1a, 0xb7, 0x0e, 0x9d, 0x9a, 0xde, 0x50,
+    0xa1, 0x9f, 0x0a, 0xf0, 0xda, 0x34, 0x0e, 0x34, 0x7d, 0x76, 0x07, 0xfe,
+    0x5a, 0xfb, 0xf9, 0x58, 0x9b, 0xc9, 0x50, 0x84, 0x01, 0xa0, 0x05, 0x4d,
+    0x67, 0x42, 0x0b, 0xf8, 0xe4, 0x05, 0xcf, 0xaf, 0x8b, 0x71, 0x31, 0xf1,
+    0x0f, 0x6e, 0xc9, 0x24, 0x27, 0x9b, 0xac, 0x04, 0xd7, 0x64, 0x0d, 0x30,
+    0x4e, 0x11, 0x93, 0x40, 0x39, 0xbb, 0x72, 0xb2, 0xfe, 0x6b, 0xe4, 0xae,
+    0x8c, 0x16,
+};
 
-int example_EVP_DigestSignInit(void) {
-  int ret = 0;
+static EVP_PKEY *load_example_rsa_key(void) {
+  EVP_PKEY *ret = NULL;
+  const uint8_t *derp = kExampleRSAKeyDER;
   EVP_PKEY *pkey = NULL;
   RSA *rsa = NULL;
-  const uint8_t *derp = kExampleRSAKeyDER;
-  uint8_t *sig = NULL;
-  size_t sig_len = 0;
-  EVP_MD_CTX md_ctx;
-
-  EVP_MD_CTX_init(&md_ctx);
 
   if (!d2i_RSAPrivateKey(&rsa, &derp, sizeof(kExampleRSAKeyDER))) {
-    goto out;
+    return NULL;
   }
 
   pkey = EVP_PKEY_new();
-  sig = malloc(sig_len);
+  if (pkey == NULL || !EVP_PKEY_set1_RSA(pkey, rsa)) {
+    goto out;
+  }
+
+  ret = pkey;
+  pkey = NULL;
+
+out:
+  if (pkey) {
+    EVP_PKEY_free(pkey);
+  }
+  if (rsa) {
+    RSA_free(rsa);
+  }
+
+  return ret;
+}
+
+static int example_EVP_DigestSignInit(void) {
+  int ret = 0;
+  EVP_PKEY *pkey = NULL;
+  uint8_t *sig = NULL;
+  size_t sig_len = 0;
+  EVP_MD_CTX md_ctx, md_ctx_verify;
+
+  EVP_MD_CTX_init(&md_ctx);
+  EVP_MD_CTX_init(&md_ctx_verify);
+
+  pkey = load_example_rsa_key();
   if (pkey == NULL ||
-      !EVP_PKEY_set1_RSA(pkey, rsa) ||
       EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL, pkey) != 1 ||
       EVP_DigestSignUpdate(&md_ctx, kMsg, sizeof(kMsg)) != 1) {
     goto out;
@@ -124,17 +205,20 @@
     goto out;
   }
   /* Sanity check for testing. */
-  if (sig_len != RSA_size(rsa)) {
+  if (sig_len != EVP_PKEY_size(pkey)) {
     fprintf(stderr, "sig_len mismatch\n");
     goto out;
   }
 
   sig = malloc(sig_len);
-  if (sig == NULL) {
+  if (sig == NULL || EVP_DigestSignFinal(&md_ctx, sig, &sig_len) != 1) {
     goto out;
   }
-  if (EVP_DigestSignFinal(&md_ctx, sig, &sig_len) != 1) {
-    free(sig);
+
+  /* Ensure that the signature round-trips. */
+  if (EVP_DigestVerifyInit(&md_ctx_verify, NULL, EVP_sha256(), NULL, pkey) != 1 ||
+      EVP_DigestVerifyUpdate(&md_ctx_verify, kMsg, sizeof(kMsg)) != 1 ||
+      EVP_DigestVerifyFinal(&md_ctx_verify, sig, sig_len) != 1) {
     goto out;
   }
 
@@ -146,12 +230,10 @@
   }
 
   EVP_MD_CTX_cleanup(&md_ctx);
+  EVP_MD_CTX_cleanup(&md_ctx_verify);
   if (pkey) {
     EVP_PKEY_free(pkey);
   }
-  if (rsa) {
-    RSA_free(rsa);
-  }
   if (sig) {
     free(sig);
   }
@@ -159,22 +241,15 @@
   return ret;
 }
 
-int example_EVP_DigestVerifyInit(void) {
+static int example_EVP_DigestVerifyInit(void) {
   int ret = 0;
   EVP_PKEY *pkey = NULL;
-  RSA *rsa = NULL;
-  const uint8_t *derp = kExampleRSAKeyDER;
   EVP_MD_CTX md_ctx;
 
   EVP_MD_CTX_init(&md_ctx);
 
-  if (!d2i_RSAPrivateKey(&rsa, &derp, sizeof(kExampleRSAKeyDER))) {
-    goto out;
-  }
-
-  pkey = EVP_PKEY_new();
+  pkey = load_example_rsa_key();
   if (pkey == NULL ||
-      !EVP_PKEY_set1_RSA(pkey, rsa) ||
       EVP_DigestVerifyInit(&md_ctx, NULL, EVP_sha256(), NULL, pkey) != 1 ||
       EVP_DigestVerifyUpdate(&md_ctx, kMsg, sizeof(kMsg)) != 1 ||
       EVP_DigestVerifyFinal(&md_ctx, kSignature, sizeof(kSignature)) != 1) {
@@ -191,8 +266,169 @@
   if (pkey) {
     EVP_PKEY_free(pkey);
   }
-  if (rsa) {
-    RSA_free(rsa);
+
+  return ret;
+}
+
+/* test_algorithm_roundtrip signs a message using an already-initialized
+ * |md_ctx|, sampling the AlgorithmIdentifier. It then uses |pkey| and the
+ * AlgorithmIdentifier to verify the signature. */
+static int test_algorithm_roundtrip(EVP_MD_CTX *md_ctx, EVP_PKEY *pkey) {
+  int ret = 0;
+  uint8_t *sig = NULL;
+  size_t sig_len = 0;
+  EVP_MD_CTX md_ctx_verify;
+  X509_ALGOR *algor = NULL;
+
+  EVP_MD_CTX_init(&md_ctx_verify);
+
+  if (EVP_DigestSignUpdate(md_ctx, kMsg, sizeof(kMsg)) != 1) {
+    goto out;
+  }
+
+  /* Save the algorithm. */
+  algor = X509_ALGOR_new();
+  if (algor == NULL || !EVP_DigestSignAlgorithm(md_ctx, algor)) {
+    goto out;
+  }
+
+  /* Determine the size of the signature. */
+  if (EVP_DigestSignFinal(md_ctx, NULL, &sig_len) != 1) {
+    goto out;
+  }
+  /* Sanity check for testing. */
+  if (sig_len != EVP_PKEY_size(pkey)) {
+    fprintf(stderr, "sig_len mismatch\n");
+    goto out;
+  }
+
+  sig = malloc(sig_len);
+  if (sig == NULL || EVP_DigestSignFinal(md_ctx, sig, &sig_len) != 1) {
+    goto out;
+  }
+
+  /* Ensure that the signature round-trips. */
+  if (EVP_DigestVerifyInitFromAlgorithm(&md_ctx_verify, algor, pkey) != 1 ||
+      EVP_DigestVerifyUpdate(&md_ctx_verify, kMsg, sizeof(kMsg)) != 1 ||
+      EVP_DigestVerifyFinal(&md_ctx_verify, sig, sig_len) != 1) {
+    goto out;
+  }
+
+  ret = 1;
+
+out:
+  EVP_MD_CTX_cleanup(&md_ctx_verify);
+  if (sig) {
+    free(sig);
+  }
+  if (algor) {
+    X509_ALGOR_free(algor);
+  }
+
+  return ret;
+}
+
+static int test_EVP_DigestSignAlgorithm(void) {
+  int ret = 0;
+  EVP_PKEY *pkey = NULL;
+  EVP_MD_CTX md_ctx;
+  EVP_PKEY_CTX *pkey_ctx;
+
+  EVP_MD_CTX_init(&md_ctx);
+
+  pkey = load_example_rsa_key();
+  if (pkey == NULL) {
+    goto out;
+  }
+
+  /* Test a simple AlgorithmIdentifier. */
+  if (EVP_DigestSignInit(&md_ctx, &pkey_ctx, EVP_sha256(), NULL, pkey) != 1 ||
+      !test_algorithm_roundtrip(&md_ctx, pkey)) {
+    fprintf(stderr, "RSA with SHA-256 failed\n");
+    goto out;
+  }
+
+  EVP_MD_CTX_cleanup(&md_ctx);
+  EVP_MD_CTX_init(&md_ctx);
+
+  /* Test RSA-PSS with custom parameters. */
+  if (EVP_DigestSignInit(&md_ctx, &pkey_ctx, EVP_sha256(), NULL, pkey) != 1 ||
+      EVP_PKEY_CTX_set_rsa_padding(pkey_ctx, RSA_PKCS1_PSS_PADDING) != 1 ||
+      EVP_PKEY_CTX_set_rsa_mgf1_md(pkey_ctx, EVP_sha512()) != 1 ||
+      !test_algorithm_roundtrip(&md_ctx, pkey)) {
+    fprintf(stderr, "RSA-PSS failed\n");
+    goto out;
+  }
+
+  ret = 1;
+
+out:
+  if (!ret) {
+    BIO_print_errors_fp(stderr);
+  }
+
+  EVP_MD_CTX_cleanup(&md_ctx);
+  if (pkey) {
+    EVP_PKEY_free(pkey);
+  }
+
+  return ret;
+}
+
+static int example_EVP_DigestVerifyInitFromAlgorithm(void) {
+  int ret = 0;
+  CBS cert, cert_body, tbs_cert, algorithm, signature;
+  uint8_t padding;
+  X509_ALGOR *algor = NULL;
+  const uint8_t *derp;
+  EVP_PKEY *pkey = NULL;
+  EVP_MD_CTX md_ctx;
+
+  EVP_MD_CTX_init(&md_ctx);
+
+  CBS_init(&cert, kExamplePSSCert, sizeof(kExamplePSSCert));
+  if (!CBS_get_asn1(&cert, &cert_body, CBS_ASN1_SEQUENCE) ||
+      CBS_len(&cert) != 0 ||
+      !CBS_get_any_asn1_element(&cert_body, &tbs_cert, NULL, NULL) ||
+      !CBS_get_asn1_element(&cert_body, &algorithm, CBS_ASN1_SEQUENCE) ||
+      !CBS_get_asn1(&cert_body, &signature, CBS_ASN1_BITSTRING) ||
+      CBS_len(&cert_body) != 0) {
+    fprintf(stderr, "Failed to parse certificate\n");
+    goto out;
+  }
+
+  /* Signatures are BIT STRINGs, but they have are multiple of 8 bytes, so the
+     leading phase byte is just a zero. */
+  if (!CBS_get_u8(&signature, &padding) || padding != 0) {
+    fprintf(stderr, "Invalid signature padding\n");
+    goto out;
+  }
+
+  derp = CBS_data(&algorithm);
+  if (!d2i_X509_ALGOR(&algor, &derp, CBS_len(&algorithm)) ||
+      derp != CBS_data(&algorithm) + CBS_len(&algorithm)) {
+    fprintf(stderr, "Failed to parse algorithm\n");
+  }
+
+  pkey = load_example_rsa_key();
+  if (pkey == NULL ||
+      EVP_DigestVerifyInitFromAlgorithm(&md_ctx, algor, pkey) != 1||
+      EVP_DigestVerifyUpdate(&md_ctx, CBS_data(&tbs_cert),
+                             CBS_len(&tbs_cert)) != 1 ||
+      EVP_DigestVerifyFinal(&md_ctx, CBS_data(&signature),
+                            CBS_len(&signature)) != 1) {
+    goto out;
+  }
+  ret = 1;
+
+out:
+  if (!ret) {
+    BIO_print_errors_fp(stderr);
+  }
+
+  EVP_MD_CTX_cleanup(&md_ctx);
+  if (pkey) {
+    EVP_PKEY_free(pkey);
   }
 
   return ret;
@@ -200,6 +436,7 @@
 
 int main(void) {
   CRYPTO_library_init();
+  ERR_load_crypto_strings();
 
   if (!example_EVP_DigestSignInit()) {
     fprintf(stderr, "EVP_DigestSignInit failed\n");
@@ -211,6 +448,16 @@
     return 1;
   }
 
+  if (!test_EVP_DigestSignAlgorithm()) {
+    fprintf(stderr, "EVP_DigestSignInit failed\n");
+    return 1;
+  }
+
+  if (!example_EVP_DigestVerifyInitFromAlgorithm()) {
+    fprintf(stderr, "EVP_DigestVerifyInitFromAlgorithm failed\n");
+    return 1;
+  }
+
   printf("PASS\n");
   return 0;
 }