1e1051a39Sopenharmony_ci/* 2e1051a39Sopenharmony_ci * Copyright 2015-2022 The OpenSSL Project Authors. All Rights Reserved. 3e1051a39Sopenharmony_ci * 4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License"). You may not use 5e1051a39Sopenharmony_ci * this file except in compliance with the License. You can obtain a copy 6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at 7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html 8e1051a39Sopenharmony_ci */ 9e1051a39Sopenharmony_ci 10e1051a39Sopenharmony_ci#include "internal/nelem.h" 11e1051a39Sopenharmony_ci#include <string.h> 12e1051a39Sopenharmony_ci#include <openssl/bio.h> 13e1051a39Sopenharmony_ci#include <openssl/crypto.h> 14e1051a39Sopenharmony_ci#include <openssl/err.h> 15e1051a39Sopenharmony_ci#include <openssl/pem.h> 16e1051a39Sopenharmony_ci#include <openssl/x509.h> 17e1051a39Sopenharmony_ci 18e1051a39Sopenharmony_ci#include "testutil.h" 19e1051a39Sopenharmony_ci 20e1051a39Sopenharmony_ci#define PARAM_TIME 1474934400 /* Sep 27th, 2016 */ 21e1051a39Sopenharmony_ci 22e1051a39Sopenharmony_cistatic const char *kCRLTestRoot[] = { 23e1051a39Sopenharmony_ci "-----BEGIN CERTIFICATE-----\n", 24e1051a39Sopenharmony_ci "MIIDbzCCAlegAwIBAgIJAODri7v0dDUFMA0GCSqGSIb3DQEBCwUAME4xCzAJBgNV\n", 25e1051a39Sopenharmony_ci "BAYTAlVTMRMwEQYDVQQIDApDYWxpZm9ybmlhMRYwFAYDVQQHDA1Nb3VudGFpbiBW\n", 26e1051a39Sopenharmony_ci "aWV3MRIwEAYDVQQKDAlCb3JpbmdTU0wwHhcNMTYwOTI2MTUwNjI2WhcNMjYwOTI0\n", 27e1051a39Sopenharmony_ci "MTUwNjI2WjBOMQswCQYDVQQGEwJVUzETMBEGA1UECAwKQ2FsaWZvcm5pYTEWMBQG\n", 28e1051a39Sopenharmony_ci "A1UEBwwNTW91bnRhaW4gVmlldzESMBAGA1UECgwJQm9yaW5nU1NMMIIBIjANBgkq\n", 29e1051a39Sopenharmony_ci "hkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAo16WiLWZuaymsD8n5SKPmxV1y6jjgr3B\n", 30e1051a39Sopenharmony_ci "S/dUBpbrzd1aeFzNlI8l2jfAnzUyp+I21RQ+nh/MhqjGElkTtK9xMn1Y+S9GMRh+\n", 31e1051a39Sopenharmony_ci "5R/Du0iCb1tCZIPY07Tgrb0KMNWe0v2QKVVruuYSgxIWodBfxlKO64Z8AJ5IbnWp\n", 32e1051a39Sopenharmony_ci "uRqO6rctN9qUoMlTIAB6dL4G0tDJ/PGFWOJYwOMEIX54bly2wgyYJVBKiRRt4f7n\n", 33e1051a39Sopenharmony_ci "8H922qmvPNA9idmX9G1VAtgV6x97XXi7ULORIQvn9lVQF6nTYDBJhyuPB+mLThbL\n", 34e1051a39Sopenharmony_ci "P2o9orxGx7aCtnnBZUIxUvHNOI0FaSaZH7Fi0xsZ/GkG2HZe7ImPJwIDAQABo1Aw\n", 35e1051a39Sopenharmony_ci "TjAdBgNVHQ4EFgQUWPt3N5cZ/CRvubbrkqfBnAqhq94wHwYDVR0jBBgwFoAUWPt3\n", 36e1051a39Sopenharmony_ci "N5cZ/CRvubbrkqfBnAqhq94wDAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQsFAAOC\n", 37e1051a39Sopenharmony_ci "AQEAORu6M0MOwXy+3VEBwNilfTxyqDfruQsc1jA4PT8Oe8zora1WxE1JB4q2FJOz\n", 38e1051a39Sopenharmony_ci "EAuM3H/NXvEnBuN+ITvKZAJUfm4NKX97qmjMJwLKWe1gVv+VQTr63aR7mgWJReQN\n", 39e1051a39Sopenharmony_ci "XdMztlVeZs2dppV6uEg3ia1X0G7LARxGpA9ETbMyCpb39XxlYuTClcbA5ftDN99B\n", 40e1051a39Sopenharmony_ci "3Xg9KNdd++Ew22O3HWRDvdDpTO/JkzQfzi3sYwUtzMEonENhczJhGf7bQMmvL/w5\n", 41e1051a39Sopenharmony_ci "24Wxj4Z7KzzWIHsNqE/RIs6RV3fcW61j/mRgW2XyoWnMVeBzvcJr9NXp4VQYmFPw\n", 42e1051a39Sopenharmony_ci "amd8GKMZQvP0ufGnUn7D7uartA==\n", 43e1051a39Sopenharmony_ci "-----END CERTIFICATE-----\n", 44e1051a39Sopenharmony_ci NULL 45e1051a39Sopenharmony_ci}; 46e1051a39Sopenharmony_ci 47e1051a39Sopenharmony_cistatic const char *kCRLTestLeaf[] = { 48e1051a39Sopenharmony_ci "-----BEGIN CERTIFICATE-----\n", 49e1051a39Sopenharmony_ci "MIIDkDCCAnigAwIBAgICEAAwDQYJKoZIhvcNAQELBQAwTjELMAkGA1UEBhMCVVMx\n", 50e1051a39Sopenharmony_ci "EzARBgNVBAgMCkNhbGlmb3JuaWExFjAUBgNVBAcMDU1vdW50YWluIFZpZXcxEjAQ\n", 51e1051a39Sopenharmony_ci "BgNVBAoMCUJvcmluZ1NTTDAeFw0xNjA5MjYxNTA4MzFaFw0xNzA5MjYxNTA4MzFa\n", 52e1051a39Sopenharmony_ci "MEsxCzAJBgNVBAYTAlVTMRMwEQYDVQQIDApDYWxpZm9ybmlhMRIwEAYDVQQKDAlC\n", 53e1051a39Sopenharmony_ci "b3JpbmdTU0wxEzARBgNVBAMMCmJvcmluZy5zc2wwggEiMA0GCSqGSIb3DQEBAQUA\n", 54e1051a39Sopenharmony_ci "A4IBDwAwggEKAoIBAQDc5v1S1M0W+QWM+raWfO0LH8uvqEwuJQgODqMaGnSlWUx9\n", 55e1051a39Sopenharmony_ci "8iQcnWfjyPja3lWg9K62hSOFDuSyEkysKHDxijz5R93CfLcfnVXjWQDJe7EJTTDP\n", 56e1051a39Sopenharmony_ci "ozEvxN6RjAeYv7CF000euYr3QT5iyBjg76+bon1p0jHZBJeNPP1KqGYgyxp+hzpx\n", 57e1051a39Sopenharmony_ci "e0gZmTlGAXd8JQK4v8kpdYwD6PPifFL/jpmQpqOtQmH/6zcLjY4ojmqpEdBqIKIX\n", 58e1051a39Sopenharmony_ci "+saA29hMq0+NK3K+wgg31RU+cVWxu3tLOIiesETkeDgArjWRS1Vkzbi4v9SJxtNu\n", 59e1051a39Sopenharmony_ci "OZuAxWiynRJw3JwH/OFHYZIvQqz68ZBoj96cepjPAgMBAAGjezB5MAkGA1UdEwQC\n", 60e1051a39Sopenharmony_ci "MAAwLAYJYIZIAYb4QgENBB8WHU9wZW5TU0wgR2VuZXJhdGVkIENlcnRpZmljYXRl\n", 61e1051a39Sopenharmony_ci "MB0GA1UdDgQWBBTGn0OVVh/aoYt0bvEKG+PIERqnDzAfBgNVHSMEGDAWgBRY+3c3\n", 62e1051a39Sopenharmony_ci "lxn8JG+5tuuSp8GcCqGr3jANBgkqhkiG9w0BAQsFAAOCAQEAd2nM8gCQN2Dc8QJw\n", 63e1051a39Sopenharmony_ci "XSZXyuI3DBGGCHcay/3iXu0JvTC3EiQo8J6Djv7WLI0N5KH8mkm40u89fJAB2lLZ\n", 64e1051a39Sopenharmony_ci "ShuHVtcC182bOKnePgwp9CNwQ21p0rDEu/P3X46ZvFgdxx82E9xLa0tBB8PiPDWh\n", 65e1051a39Sopenharmony_ci "lV16jbaKTgX5AZqjnsyjR5o9/mbZVupZJXx5Syq+XA8qiJfstSYJs4KyKK9UOjql\n", 66e1051a39Sopenharmony_ci "ICkJVKpi2ahDBqX4MOH4SLfzVk8pqSpviS6yaA1RXqjpkxiN45WWaXDldVHMSkhC\n", 67e1051a39Sopenharmony_ci "5CNXsXi4b1nAntu89crwSLA3rEwzCWeYj+BX7e1T9rr3oJdwOU/2KQtW1js1yQUG\n", 68e1051a39Sopenharmony_ci "tjJMFw==\n", 69e1051a39Sopenharmony_ci "-----END CERTIFICATE-----\n", 70e1051a39Sopenharmony_ci NULL 71e1051a39Sopenharmony_ci}; 72e1051a39Sopenharmony_ci 73e1051a39Sopenharmony_cistatic const char *kBasicCRL[] = { 74e1051a39Sopenharmony_ci "-----BEGIN X509 CRL-----\n", 75e1051a39Sopenharmony_ci "MIIBpzCBkAIBATANBgkqhkiG9w0BAQsFADBOMQswCQYDVQQGEwJVUzETMBEGA1UE\n", 76e1051a39Sopenharmony_ci "CAwKQ2FsaWZvcm5pYTEWMBQGA1UEBwwNTW91bnRhaW4gVmlldzESMBAGA1UECgwJ\n", 77e1051a39Sopenharmony_ci "Qm9yaW5nU1NMFw0xNjA5MjYxNTEwNTVaFw0xNjEwMjYxNTEwNTVaoA4wDDAKBgNV\n", 78e1051a39Sopenharmony_ci "HRQEAwIBATANBgkqhkiG9w0BAQsFAAOCAQEAnrBKKgvd9x9zwK9rtUvVeFeJ7+LN\n", 79e1051a39Sopenharmony_ci "ZEAc+a5oxpPNEsJx6hXoApYEbzXMxuWBQoCs5iEBycSGudct21L+MVf27M38KrWo\n", 80e1051a39Sopenharmony_ci "eOkq0a2siqViQZO2Fb/SUFR0k9zb8xl86Zf65lgPplALun0bV/HT7MJcl04Tc4os\n", 81e1051a39Sopenharmony_ci "dsAReBs5nqTGNEd5AlC1iKHvQZkM//MD51DspKnDpsDiUVi54h9C1SpfZmX8H2Vv\n", 82e1051a39Sopenharmony_ci "diyu0fZ/bPAM3VAGawatf/SyWfBMyKpoPXEG39oAzmjjOj8en82psn7m474IGaho\n", 83e1051a39Sopenharmony_ci "/vBbhl1ms5qQiLYPjm4YELtnXQoFyC72tBjbdFd/ZE9k4CNKDbxFUXFbkw==\n", 84e1051a39Sopenharmony_ci "-----END X509 CRL-----\n", 85e1051a39Sopenharmony_ci NULL 86e1051a39Sopenharmony_ci}; 87e1051a39Sopenharmony_ci 88e1051a39Sopenharmony_cistatic const char *kRevokedCRL[] = { 89e1051a39Sopenharmony_ci "-----BEGIN X509 CRL-----\n", 90e1051a39Sopenharmony_ci "MIIBvjCBpwIBATANBgkqhkiG9w0BAQsFADBOMQswCQYDVQQGEwJVUzETMBEGA1UE\n", 91e1051a39Sopenharmony_ci "CAwKQ2FsaWZvcm5pYTEWMBQGA1UEBwwNTW91bnRhaW4gVmlldzESMBAGA1UECgwJ\n", 92e1051a39Sopenharmony_ci "Qm9yaW5nU1NMFw0xNjA5MjYxNTEyNDRaFw0xNjEwMjYxNTEyNDRaMBUwEwICEAAX\n", 93e1051a39Sopenharmony_ci "DTE2MDkyNjE1MTIyNlqgDjAMMAoGA1UdFAQDAgECMA0GCSqGSIb3DQEBCwUAA4IB\n", 94e1051a39Sopenharmony_ci "AQCUGaM4DcWzlQKrcZvI8TMeR8BpsvQeo5BoI/XZu2a8h//PyRyMwYeaOM+3zl0d\n", 95e1051a39Sopenharmony_ci "sjgCT8b3C1FPgT+P2Lkowv7rJ+FHJRNQkogr+RuqCSPTq65ha4WKlRGWkMFybzVH\n", 96e1051a39Sopenharmony_ci "NloxC+aU3lgp/NlX9yUtfqYmJek1CDrOOGPrAEAwj1l/BUeYKNGqfBWYJQtPJu+5\n", 97e1051a39Sopenharmony_ci "OaSvIYGpETCZJscUWODmLEb/O3DM438vLvxonwGqXqS0KX37+CHpUlyhnSovxXxp\n", 98e1051a39Sopenharmony_ci "Pz4aF+L7OtczxL0GYtD2fR9B7TDMqsNmHXgQrixvvOY7MUdLGbd4RfJL3yA53hyO\n", 99e1051a39Sopenharmony_ci "xzfKY2TzxLiOmctG0hXFkH5J\n", 100e1051a39Sopenharmony_ci "-----END X509 CRL-----\n", 101e1051a39Sopenharmony_ci NULL 102e1051a39Sopenharmony_ci}; 103e1051a39Sopenharmony_ci 104e1051a39Sopenharmony_cistatic const char *kBadIssuerCRL[] = { 105e1051a39Sopenharmony_ci "-----BEGIN X509 CRL-----\n", 106e1051a39Sopenharmony_ci "MIIBwjCBqwIBATANBgkqhkiG9w0BAQsFADBSMQswCQYDVQQGEwJVUzETMBEGA1UE\n", 107e1051a39Sopenharmony_ci "CAwKQ2FsaWZvcm5pYTEWMBQGA1UEBwwNTW91bnRhaW4gVmlldzEWMBQGA1UECgwN\n", 108e1051a39Sopenharmony_ci "Tm90IEJvcmluZ1NTTBcNMTYwOTI2MTUxMjQ0WhcNMTYxMDI2MTUxMjQ0WjAVMBMC\n", 109e1051a39Sopenharmony_ci "AhAAFw0xNjA5MjYxNTEyMjZaoA4wDDAKBgNVHRQEAwIBAjANBgkqhkiG9w0BAQsF\n", 110e1051a39Sopenharmony_ci "AAOCAQEAlBmjOA3Fs5UCq3GbyPEzHkfAabL0HqOQaCP12btmvIf/z8kcjMGHmjjP\n", 111e1051a39Sopenharmony_ci "t85dHbI4Ak/G9wtRT4E/j9i5KML+6yfhRyUTUJKIK/kbqgkj06uuYWuFipURlpDB\n", 112e1051a39Sopenharmony_ci "cm81RzZaMQvmlN5YKfzZV/clLX6mJiXpNQg6zjhj6wBAMI9ZfwVHmCjRqnwVmCUL\n", 113e1051a39Sopenharmony_ci "TybvuTmkryGBqREwmSbHFFjg5ixG/ztwzON/Ly78aJ8Bql6ktCl9+/gh6VJcoZ0q\n", 114e1051a39Sopenharmony_ci "L8V8aT8+Ghfi+zrXM8S9BmLQ9n0fQe0wzKrDZh14EK4sb7zmOzFHSxm3eEXyS98g\n", 115e1051a39Sopenharmony_ci "Od4cjsc3ymNk88S4jpnLRtIVxZB+SQ==\n", 116e1051a39Sopenharmony_ci "-----END X509 CRL-----\n", 117e1051a39Sopenharmony_ci NULL 118e1051a39Sopenharmony_ci}; 119e1051a39Sopenharmony_ci 120e1051a39Sopenharmony_ci/* 121e1051a39Sopenharmony_ci * This is kBasicCRL but with a critical issuing distribution point 122e1051a39Sopenharmony_ci * extension. 123e1051a39Sopenharmony_ci */ 124e1051a39Sopenharmony_cistatic const char *kKnownCriticalCRL[] = { 125e1051a39Sopenharmony_ci "-----BEGIN X509 CRL-----\n", 126e1051a39Sopenharmony_ci "MIIBujCBowIBATANBgkqhkiG9w0BAQsFADBOMQswCQYDVQQGEwJVUzETMBEGA1UE\n", 127e1051a39Sopenharmony_ci "CAwKQ2FsaWZvcm5pYTEWMBQGA1UEBwwNTW91bnRhaW4gVmlldzESMBAGA1UECgwJ\n", 128e1051a39Sopenharmony_ci "Qm9yaW5nU1NMFw0xNjA5MjYxNTEwNTVaFw0xNjEwMjYxNTEwNTVaoCEwHzAKBgNV\n", 129e1051a39Sopenharmony_ci "HRQEAwIBATARBgNVHRwBAf8EBzAFoQMBAf8wDQYJKoZIhvcNAQELBQADggEBAA+3\n", 130e1051a39Sopenharmony_ci "i+5e5Ub8sccfgOBs6WVJFI9c8gvJjrJ8/dYfFIAuCyeocs7DFXn1n13CRZ+URR/Q\n", 131e1051a39Sopenharmony_ci "mVWgU28+xeusuSPYFpd9cyYTcVyNUGNTI3lwgcE/yVjPaOmzSZKdPakApRxtpKKQ\n", 132e1051a39Sopenharmony_ci "NN/56aQz3bnT/ZSHQNciRB8U6jiD9V30t0w+FDTpGaG+7bzzUH3UVF9xf9Ctp60A\n", 133e1051a39Sopenharmony_ci "3mfLe0scas7owSt4AEFuj2SPvcE7yvdOXbu+IEv21cEJUVExJAbhvIweHXh6yRW+\n", 134e1051a39Sopenharmony_ci "7VVeiNzdIjkZjyTmAzoXGha4+wbxXyBRbfH+XWcO/H+8nwyG8Gktdu2QB9S9nnIp\n", 135e1051a39Sopenharmony_ci "o/1TpfOMSGhMyMoyPrk=\n", 136e1051a39Sopenharmony_ci "-----END X509 CRL-----\n", 137e1051a39Sopenharmony_ci NULL 138e1051a39Sopenharmony_ci}; 139e1051a39Sopenharmony_ci 140e1051a39Sopenharmony_ci/* 141e1051a39Sopenharmony_ci * kUnknownCriticalCRL is kBasicCRL but with an unknown critical extension. 142e1051a39Sopenharmony_ci */ 143e1051a39Sopenharmony_cistatic const char *kUnknownCriticalCRL[] = { 144e1051a39Sopenharmony_ci "-----BEGIN X509 CRL-----\n", 145e1051a39Sopenharmony_ci "MIIBvDCBpQIBATANBgkqhkiG9w0BAQsFADBOMQswCQYDVQQGEwJVUzETMBEGA1UE\n", 146e1051a39Sopenharmony_ci "CAwKQ2FsaWZvcm5pYTEWMBQGA1UEBwwNTW91bnRhaW4gVmlldzESMBAGA1UECgwJ\n", 147e1051a39Sopenharmony_ci "Qm9yaW5nU1NMFw0xNjA5MjYxNTEwNTVaFw0xNjEwMjYxNTEwNTVaoCMwITAKBgNV\n", 148e1051a39Sopenharmony_ci "HRQEAwIBATATBgwqhkiG9xIEAYS3CQABAf8EADANBgkqhkiG9w0BAQsFAAOCAQEA\n", 149e1051a39Sopenharmony_ci "GvBP0xqL509InMj/3493YVRV+ldTpBv5uTD6jewzf5XdaxEQ/VjTNe5zKnxbpAib\n", 150e1051a39Sopenharmony_ci "Kf7cwX0PMSkZjx7k7kKdDlEucwVvDoqC+O9aJcqVmM6GDyNb9xENxd0XCXja6MZC\n", 151e1051a39Sopenharmony_ci "yVgP4AwLauB2vSiEprYJyI1APph3iAEeDm60lTXX/wBM/tupQDDujKh2GPyvBRfJ\n", 152e1051a39Sopenharmony_ci "+wEDwGg3ICwvu4gO4zeC5qnFR+bpL9t5tOMAQnVZ0NWv+k7mkd2LbHdD44dxrfXC\n", 153e1051a39Sopenharmony_ci "nhtfERx99SDmC/jtUAJrGhtCO8acr7exCeYcduN7KKCm91OeCJKK6OzWst0Og1DB\n", 154e1051a39Sopenharmony_ci "kwzzU2rL3G65CrZ7H0SZsQ==\n", 155e1051a39Sopenharmony_ci "-----END X509 CRL-----\n", 156e1051a39Sopenharmony_ci NULL 157e1051a39Sopenharmony_ci}; 158e1051a39Sopenharmony_ci 159e1051a39Sopenharmony_ci/* 160e1051a39Sopenharmony_ci * kUnknownCriticalCRL2 is kBasicCRL but with a critical issuing distribution 161e1051a39Sopenharmony_ci * point extension followed by an unknown critical extension 162e1051a39Sopenharmony_ci */ 163e1051a39Sopenharmony_cistatic const char *kUnknownCriticalCRL2[] = { 164e1051a39Sopenharmony_ci "-----BEGIN X509 CRL-----\n", 165e1051a39Sopenharmony_ci "MIIBzzCBuAIBATANBgkqhkiG9w0BAQsFADBOMQswCQYDVQQGEwJVUzETMBEGA1UE\n", 166e1051a39Sopenharmony_ci "CAwKQ2FsaWZvcm5pYTEWMBQGA1UEBwwNTW91bnRhaW4gVmlldzESMBAGA1UECgwJ\n", 167e1051a39Sopenharmony_ci "Qm9yaW5nU1NMFw0xNjA5MjYxNTEwNTVaFw0xNjEwMjYxNTEwNTVaoDYwNDAKBgNV\n", 168e1051a39Sopenharmony_ci "HRQEAwIBATARBgNVHRwBAf8EBzAFoQMBAf8wEwYMKoZIhvcSBAGEtwkAAQH/BAAw\n", 169e1051a39Sopenharmony_ci "DQYJKoZIhvcNAQELBQADggEBACTcpQC8jXL12JN5YzOcQ64ubQIe0XxRAd30p7qB\n", 170e1051a39Sopenharmony_ci "BTXGpgqBjrjxRfLms7EBYodEXB2oXMsDq3km0vT1MfYdsDD05S+SQ9CDsq/pUfaC\n", 171e1051a39Sopenharmony_ci "E2WNI5p8WircRnroYvbN2vkjlRbMd1+yNITohXYXCJwjEOAWOx3XIM10bwPYBv4R\n", 172e1051a39Sopenharmony_ci "rDobuLHoMgL3yHgMHmAkP7YpkBucNqeBV8cCdeAZLuhXFWi6yfr3r/X18yWbC/r2\n", 173e1051a39Sopenharmony_ci "2xXdkrSqXLFo7ToyP8YKTgiXpya4x6m53biEYwa2ULlas0igL6DK7wjYZX95Uy7H\n", 174e1051a39Sopenharmony_ci "GKljn9weIYiMPV/BzGymwfv2EW0preLwtyJNJPaxbdin6Jc=\n", 175e1051a39Sopenharmony_ci "-----END X509 CRL-----\n", 176e1051a39Sopenharmony_ci NULL 177e1051a39Sopenharmony_ci}; 178e1051a39Sopenharmony_ci 179e1051a39Sopenharmony_cistatic const char **unknown_critical_crls[] = { 180e1051a39Sopenharmony_ci kUnknownCriticalCRL, kUnknownCriticalCRL2 181e1051a39Sopenharmony_ci}; 182e1051a39Sopenharmony_ci 183e1051a39Sopenharmony_cistatic X509 *test_root = NULL; 184e1051a39Sopenharmony_cistatic X509 *test_leaf = NULL; 185e1051a39Sopenharmony_ci 186e1051a39Sopenharmony_ci/* 187e1051a39Sopenharmony_ci * Glue an array of strings together. Return a BIO and put the string 188e1051a39Sopenharmony_ci * into |*out| so we can free it. 189e1051a39Sopenharmony_ci */ 190e1051a39Sopenharmony_cistatic BIO *glue2bio(const char **pem, char **out) 191e1051a39Sopenharmony_ci{ 192e1051a39Sopenharmony_ci size_t s = 0; 193e1051a39Sopenharmony_ci 194e1051a39Sopenharmony_ci *out = glue_strings(pem, &s); 195e1051a39Sopenharmony_ci return BIO_new_mem_buf(*out, s); 196e1051a39Sopenharmony_ci} 197e1051a39Sopenharmony_ci 198e1051a39Sopenharmony_ci/* 199e1051a39Sopenharmony_ci * Create a CRL from an array of strings. 200e1051a39Sopenharmony_ci */ 201e1051a39Sopenharmony_cistatic X509_CRL *CRL_from_strings(const char **pem) 202e1051a39Sopenharmony_ci{ 203e1051a39Sopenharmony_ci X509_CRL *crl; 204e1051a39Sopenharmony_ci char *p; 205e1051a39Sopenharmony_ci BIO *b = glue2bio(pem, &p); 206e1051a39Sopenharmony_ci 207e1051a39Sopenharmony_ci if (b == NULL) { 208e1051a39Sopenharmony_ci OPENSSL_free(p); 209e1051a39Sopenharmony_ci return NULL; 210e1051a39Sopenharmony_ci } 211e1051a39Sopenharmony_ci 212e1051a39Sopenharmony_ci crl = PEM_read_bio_X509_CRL(b, NULL, NULL, NULL); 213e1051a39Sopenharmony_ci 214e1051a39Sopenharmony_ci OPENSSL_free(p); 215e1051a39Sopenharmony_ci BIO_free(b); 216e1051a39Sopenharmony_ci return crl; 217e1051a39Sopenharmony_ci} 218e1051a39Sopenharmony_ci 219e1051a39Sopenharmony_ci/* 220e1051a39Sopenharmony_ci * Create an X509 from an array of strings. 221e1051a39Sopenharmony_ci */ 222e1051a39Sopenharmony_cistatic X509 *X509_from_strings(const char **pem) 223e1051a39Sopenharmony_ci{ 224e1051a39Sopenharmony_ci X509 *x; 225e1051a39Sopenharmony_ci char *p; 226e1051a39Sopenharmony_ci BIO *b = glue2bio(pem, &p); 227e1051a39Sopenharmony_ci 228e1051a39Sopenharmony_ci if (b == NULL) { 229e1051a39Sopenharmony_ci OPENSSL_free(p); 230e1051a39Sopenharmony_ci return NULL; 231e1051a39Sopenharmony_ci } 232e1051a39Sopenharmony_ci 233e1051a39Sopenharmony_ci x = PEM_read_bio_X509(b, NULL, NULL, NULL); 234e1051a39Sopenharmony_ci 235e1051a39Sopenharmony_ci OPENSSL_free(p); 236e1051a39Sopenharmony_ci BIO_free(b); 237e1051a39Sopenharmony_ci return x; 238e1051a39Sopenharmony_ci} 239e1051a39Sopenharmony_ci 240e1051a39Sopenharmony_ci/* 241e1051a39Sopenharmony_ci * Verify |leaf| certificate (chained up to |root|). |crls| if 242e1051a39Sopenharmony_ci * not NULL, is a list of CRLs to include in the verification. It is 243e1051a39Sopenharmony_ci * also free'd before returning, which is kinda yucky but convenient. 244e1051a39Sopenharmony_ci * Returns a value from X509_V_ERR_xxx or X509_V_OK. 245e1051a39Sopenharmony_ci */ 246e1051a39Sopenharmony_cistatic int verify(X509 *leaf, X509 *root, STACK_OF(X509_CRL) *crls, 247e1051a39Sopenharmony_ci unsigned long flags) 248e1051a39Sopenharmony_ci{ 249e1051a39Sopenharmony_ci X509_STORE_CTX *ctx = X509_STORE_CTX_new(); 250e1051a39Sopenharmony_ci X509_STORE *store = X509_STORE_new(); 251e1051a39Sopenharmony_ci X509_VERIFY_PARAM *param = X509_VERIFY_PARAM_new(); 252e1051a39Sopenharmony_ci STACK_OF(X509) *roots = sk_X509_new_null(); 253e1051a39Sopenharmony_ci int status = X509_V_ERR_UNSPECIFIED; 254e1051a39Sopenharmony_ci 255e1051a39Sopenharmony_ci if (!TEST_ptr(ctx) 256e1051a39Sopenharmony_ci || !TEST_ptr(store) 257e1051a39Sopenharmony_ci || !TEST_ptr(param) 258e1051a39Sopenharmony_ci || !TEST_ptr(roots)) 259e1051a39Sopenharmony_ci goto err; 260e1051a39Sopenharmony_ci 261e1051a39Sopenharmony_ci /* Create a stack; upref the cert because we free it below. */ 262e1051a39Sopenharmony_ci X509_up_ref(root); 263e1051a39Sopenharmony_ci if (!TEST_true(sk_X509_push(roots, root)) 264e1051a39Sopenharmony_ci || !TEST_true(X509_STORE_CTX_init(ctx, store, leaf, NULL))) 265e1051a39Sopenharmony_ci goto err; 266e1051a39Sopenharmony_ci X509_STORE_CTX_set0_trusted_stack(ctx, roots); 267e1051a39Sopenharmony_ci X509_STORE_CTX_set0_crls(ctx, crls); 268e1051a39Sopenharmony_ci X509_VERIFY_PARAM_set_time(param, PARAM_TIME); 269e1051a39Sopenharmony_ci if (!TEST_long_eq((long)X509_VERIFY_PARAM_get_time(param), PARAM_TIME)) 270e1051a39Sopenharmony_ci goto err; 271e1051a39Sopenharmony_ci X509_VERIFY_PARAM_set_depth(param, 16); 272e1051a39Sopenharmony_ci if (flags) 273e1051a39Sopenharmony_ci X509_VERIFY_PARAM_set_flags(param, flags); 274e1051a39Sopenharmony_ci X509_STORE_CTX_set0_param(ctx, param); 275e1051a39Sopenharmony_ci param = NULL; 276e1051a39Sopenharmony_ci 277e1051a39Sopenharmony_ci ERR_clear_error(); 278e1051a39Sopenharmony_ci status = X509_verify_cert(ctx) == 1 ? X509_V_OK 279e1051a39Sopenharmony_ci : X509_STORE_CTX_get_error(ctx); 280e1051a39Sopenharmony_cierr: 281e1051a39Sopenharmony_ci sk_X509_pop_free(roots, X509_free); 282e1051a39Sopenharmony_ci sk_X509_CRL_pop_free(crls, X509_CRL_free); 283e1051a39Sopenharmony_ci X509_VERIFY_PARAM_free(param); 284e1051a39Sopenharmony_ci X509_STORE_CTX_free(ctx); 285e1051a39Sopenharmony_ci X509_STORE_free(store); 286e1051a39Sopenharmony_ci return status; 287e1051a39Sopenharmony_ci} 288e1051a39Sopenharmony_ci 289e1051a39Sopenharmony_ci/* 290e1051a39Sopenharmony_ci * Create a stack of CRL's. Upref each one because we call pop_free on 291e1051a39Sopenharmony_ci * the stack and need to keep the CRL's around until the test exits. 292e1051a39Sopenharmony_ci * Yes this crashes on malloc failure; it forces us to debug. 293e1051a39Sopenharmony_ci */ 294e1051a39Sopenharmony_cistatic STACK_OF(X509_CRL) *make_CRL_stack(X509_CRL *x1, X509_CRL *x2) 295e1051a39Sopenharmony_ci{ 296e1051a39Sopenharmony_ci STACK_OF(X509_CRL) *sk = sk_X509_CRL_new_null(); 297e1051a39Sopenharmony_ci 298e1051a39Sopenharmony_ci sk_X509_CRL_push(sk, x1); 299e1051a39Sopenharmony_ci X509_CRL_up_ref(x1); 300e1051a39Sopenharmony_ci if (x2 != NULL) { 301e1051a39Sopenharmony_ci sk_X509_CRL_push(sk, x2); 302e1051a39Sopenharmony_ci X509_CRL_up_ref(x2); 303e1051a39Sopenharmony_ci } 304e1051a39Sopenharmony_ci return sk; 305e1051a39Sopenharmony_ci} 306e1051a39Sopenharmony_ci 307e1051a39Sopenharmony_cistatic int test_basic_crl(void) 308e1051a39Sopenharmony_ci{ 309e1051a39Sopenharmony_ci X509_CRL *basic_crl = CRL_from_strings(kBasicCRL); 310e1051a39Sopenharmony_ci X509_CRL *revoked_crl = CRL_from_strings(kRevokedCRL); 311e1051a39Sopenharmony_ci int r; 312e1051a39Sopenharmony_ci 313e1051a39Sopenharmony_ci r = TEST_ptr(basic_crl) 314e1051a39Sopenharmony_ci && TEST_ptr(revoked_crl) 315e1051a39Sopenharmony_ci && TEST_int_eq(verify(test_leaf, test_root, 316e1051a39Sopenharmony_ci make_CRL_stack(basic_crl, NULL), 317e1051a39Sopenharmony_ci X509_V_FLAG_CRL_CHECK), X509_V_OK) 318e1051a39Sopenharmony_ci && TEST_int_eq(verify(test_leaf, test_root, 319e1051a39Sopenharmony_ci make_CRL_stack(basic_crl, revoked_crl), 320e1051a39Sopenharmony_ci X509_V_FLAG_CRL_CHECK), X509_V_ERR_CERT_REVOKED); 321e1051a39Sopenharmony_ci X509_CRL_free(basic_crl); 322e1051a39Sopenharmony_ci X509_CRL_free(revoked_crl); 323e1051a39Sopenharmony_ci return r; 324e1051a39Sopenharmony_ci} 325e1051a39Sopenharmony_ci 326e1051a39Sopenharmony_cistatic int test_no_crl(void) 327e1051a39Sopenharmony_ci{ 328e1051a39Sopenharmony_ci return TEST_int_eq(verify(test_leaf, test_root, NULL, 329e1051a39Sopenharmony_ci X509_V_FLAG_CRL_CHECK), 330e1051a39Sopenharmony_ci X509_V_ERR_UNABLE_TO_GET_CRL); 331e1051a39Sopenharmony_ci} 332e1051a39Sopenharmony_ci 333e1051a39Sopenharmony_cistatic int test_bad_issuer_crl(void) 334e1051a39Sopenharmony_ci{ 335e1051a39Sopenharmony_ci X509_CRL *bad_issuer_crl = CRL_from_strings(kBadIssuerCRL); 336e1051a39Sopenharmony_ci int r; 337e1051a39Sopenharmony_ci 338e1051a39Sopenharmony_ci r = TEST_ptr(bad_issuer_crl) 339e1051a39Sopenharmony_ci && TEST_int_eq(verify(test_leaf, test_root, 340e1051a39Sopenharmony_ci make_CRL_stack(bad_issuer_crl, NULL), 341e1051a39Sopenharmony_ci X509_V_FLAG_CRL_CHECK), 342e1051a39Sopenharmony_ci X509_V_ERR_UNABLE_TO_GET_CRL); 343e1051a39Sopenharmony_ci X509_CRL_free(bad_issuer_crl); 344e1051a39Sopenharmony_ci return r; 345e1051a39Sopenharmony_ci} 346e1051a39Sopenharmony_ci 347e1051a39Sopenharmony_cistatic int test_known_critical_crl(void) 348e1051a39Sopenharmony_ci{ 349e1051a39Sopenharmony_ci X509_CRL *known_critical_crl = CRL_from_strings(kKnownCriticalCRL); 350e1051a39Sopenharmony_ci int r; 351e1051a39Sopenharmony_ci 352e1051a39Sopenharmony_ci r = TEST_ptr(known_critical_crl) 353e1051a39Sopenharmony_ci && TEST_int_eq(verify(test_leaf, test_root, 354e1051a39Sopenharmony_ci make_CRL_stack(known_critical_crl, NULL), 355e1051a39Sopenharmony_ci X509_V_FLAG_CRL_CHECK), X509_V_OK); 356e1051a39Sopenharmony_ci X509_CRL_free(known_critical_crl); 357e1051a39Sopenharmony_ci return r; 358e1051a39Sopenharmony_ci} 359e1051a39Sopenharmony_ci 360e1051a39Sopenharmony_cistatic int test_unknown_critical_crl(int n) 361e1051a39Sopenharmony_ci{ 362e1051a39Sopenharmony_ci X509_CRL *unknown_critical_crl = CRL_from_strings(unknown_critical_crls[n]); 363e1051a39Sopenharmony_ci int r; 364e1051a39Sopenharmony_ci 365e1051a39Sopenharmony_ci r = TEST_ptr(unknown_critical_crl) 366e1051a39Sopenharmony_ci && TEST_int_eq(verify(test_leaf, test_root, 367e1051a39Sopenharmony_ci make_CRL_stack(unknown_critical_crl, NULL), 368e1051a39Sopenharmony_ci X509_V_FLAG_CRL_CHECK), 369e1051a39Sopenharmony_ci X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION); 370e1051a39Sopenharmony_ci X509_CRL_free(unknown_critical_crl); 371e1051a39Sopenharmony_ci return r; 372e1051a39Sopenharmony_ci} 373e1051a39Sopenharmony_ci 374e1051a39Sopenharmony_cistatic int test_reuse_crl(void) 375e1051a39Sopenharmony_ci{ 376e1051a39Sopenharmony_ci X509_CRL *reused_crl = CRL_from_strings(kBasicCRL); 377e1051a39Sopenharmony_ci char *p; 378e1051a39Sopenharmony_ci BIO *b = glue2bio(kRevokedCRL, &p); 379e1051a39Sopenharmony_ci 380e1051a39Sopenharmony_ci if (b == NULL) { 381e1051a39Sopenharmony_ci OPENSSL_free(p); 382e1051a39Sopenharmony_ci X509_CRL_free(reused_crl); 383e1051a39Sopenharmony_ci return 0; 384e1051a39Sopenharmony_ci } 385e1051a39Sopenharmony_ci 386e1051a39Sopenharmony_ci reused_crl = PEM_read_bio_X509_CRL(b, &reused_crl, NULL, NULL); 387e1051a39Sopenharmony_ci 388e1051a39Sopenharmony_ci OPENSSL_free(p); 389e1051a39Sopenharmony_ci BIO_free(b); 390e1051a39Sopenharmony_ci X509_CRL_free(reused_crl); 391e1051a39Sopenharmony_ci return 1; 392e1051a39Sopenharmony_ci} 393e1051a39Sopenharmony_ci 394e1051a39Sopenharmony_ciint setup_tests(void) 395e1051a39Sopenharmony_ci{ 396e1051a39Sopenharmony_ci if (!TEST_ptr(test_root = X509_from_strings(kCRLTestRoot)) 397e1051a39Sopenharmony_ci || !TEST_ptr(test_leaf = X509_from_strings(kCRLTestLeaf))) 398e1051a39Sopenharmony_ci return 0; 399e1051a39Sopenharmony_ci 400e1051a39Sopenharmony_ci ADD_TEST(test_no_crl); 401e1051a39Sopenharmony_ci ADD_TEST(test_basic_crl); 402e1051a39Sopenharmony_ci ADD_TEST(test_bad_issuer_crl); 403e1051a39Sopenharmony_ci ADD_TEST(test_known_critical_crl); 404e1051a39Sopenharmony_ci ADD_ALL_TESTS(test_unknown_critical_crl, OSSL_NELEM(unknown_critical_crls)); 405e1051a39Sopenharmony_ci ADD_TEST(test_reuse_crl); 406e1051a39Sopenharmony_ci return 1; 407e1051a39Sopenharmony_ci} 408e1051a39Sopenharmony_ci 409e1051a39Sopenharmony_civoid cleanup_tests(void) 410e1051a39Sopenharmony_ci{ 411e1051a39Sopenharmony_ci X509_free(test_root); 412e1051a39Sopenharmony_ci X509_free(test_leaf); 413e1051a39Sopenharmony_ci} 414