David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 1 | /* Copyright (c) 2016, Google Inc. |
| 2 | * |
| 3 | * Permission to use, copy, modify, and/or distribute this software for any |
| 4 | * purpose with or without fee is hereby granted, provided that the above |
| 5 | * copyright notice and this permission notice appear in all copies. |
| 6 | * |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 | |
| 15 | #include <vector> |
| 16 | |
| 17 | #include <assert.h> |
| 18 | #include <string.h> |
| 19 | |
| 20 | #include <openssl/crypto.h> |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 21 | #include <openssl/digest.h> |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 22 | #include <openssl/err.h> |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 23 | #include <openssl/pem.h> |
| 24 | #include <openssl/x509.h> |
| 25 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 26 | namespace bssl { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 27 | |
| 28 | static const char kCrossSigningRootPEM[] = |
| 29 | "-----BEGIN CERTIFICATE-----\n" |
| 30 | "MIICcTCCAdqgAwIBAgIIagJHiPvE0MowDQYJKoZIhvcNAQELBQAwPDEaMBgGA1UE\n" |
| 31 | "ChMRQm9yaW5nU1NMIFRFU1RJTkcxHjAcBgNVBAMTFUNyb3NzLXNpZ25pbmcgUm9v\n" |
| 32 | "dCBDQTAgFw0xNTAxMDEwMDAwMDBaGA8yMTAwMDEwMTAwMDAwMFowPDEaMBgGA1UE\n" |
| 33 | "ChMRQm9yaW5nU1NMIFRFU1RJTkcxHjAcBgNVBAMTFUNyb3NzLXNpZ25pbmcgUm9v\n" |
| 34 | "dCBDQTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAwo3qFvSB9Zmlbpzn9wJp\n" |
| 35 | "ikI75Rxkatez8VkLqyxbOhPYl2Haz8F5p1gDG96dCI6jcLGgu3AKT9uhEQyyUko5\n" |
| 36 | "EKYasazSeA9CQrdyhPg0mkTYVETnPM1W/ebid1YtqQbq1CMWlq2aTDoSGAReGFKP\n" |
| 37 | "RTdXAbuAXzpCfi/d8LqV13UCAwEAAaN6MHgwDgYDVR0PAQH/BAQDAgIEMB0GA1Ud\n" |
| 38 | "JQQWMBQGCCsGAQUFBwMBBggrBgEFBQcDAjAPBgNVHRMBAf8EBTADAQH/MBkGA1Ud\n" |
| 39 | "DgQSBBBHKHC7V3Z/3oLvEZx0RZRwMBsGA1UdIwQUMBKAEEcocLtXdn/egu8RnHRF\n" |
| 40 | "lHAwDQYJKoZIhvcNAQELBQADgYEAnglibsy6mGtpIXivtlcz4zIEnHw/lNW+r/eC\n" |
| 41 | "CY7evZTmOoOuC/x9SS3MF9vawt1HFUummWM6ZgErqVBOXIB4//ykrcCgf5ZbF5Hr\n" |
| 42 | "+3EFprKhBqYiXdD8hpBkrBoXwn85LPYWNd2TceCrx0YtLIprE2R5MB2RIq8y4Jk3\n" |
| 43 | "YFXvkME=\n" |
| 44 | "-----END CERTIFICATE-----\n"; |
| 45 | |
| 46 | static const char kRootCAPEM[] = |
| 47 | "-----BEGIN CERTIFICATE-----\n" |
| 48 | "MIICVTCCAb6gAwIBAgIIAj5CwoHlWuYwDQYJKoZIhvcNAQELBQAwLjEaMBgGA1UE\n" |
| 49 | "ChMRQm9yaW5nU1NMIFRFU1RJTkcxEDAOBgNVBAMTB1Jvb3QgQ0EwIBcNMTUwMTAx\n" |
| 50 | "MDAwMDAwWhgPMjEwMDAxMDEwMDAwMDBaMC4xGjAYBgNVBAoTEUJvcmluZ1NTTCBU\n" |
| 51 | "RVNUSU5HMRAwDgYDVQQDEwdSb290IENBMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCB\n" |
| 52 | "iQKBgQDpDn8RDOZa5oaDcPZRBy4CeBH1siSSOO4mYgLHlPE+oXdqwI/VImi2XeJM\n" |
| 53 | "2uCFETXCknJJjYG0iJdrt/yyRFvZTQZw+QzGj+mz36NqhGxDWb6dstB2m8PX+plZ\n" |
| 54 | "w7jl81MDvUnWs8yiQ/6twgu5AbhWKZQDJKcNKCEpqa6UW0r5nwIDAQABo3oweDAO\n" |
| 55 | "BgNVHQ8BAf8EBAMCAgQwHQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsGAQUFBwMCMA8G\n" |
| 56 | "A1UdEwEB/wQFMAMBAf8wGQYDVR0OBBIEEEA31wH7QC+4HH5UBCeMWQEwGwYDVR0j\n" |
| 57 | "BBQwEoAQQDfXAftAL7gcflQEJ4xZATANBgkqhkiG9w0BAQsFAAOBgQDXylEK77Za\n" |
| 58 | "kKeY6ZerrScWyZhrjIGtHFu09qVpdJEzrk87k2G7iHHR9CAvSofCgEExKtWNS9dN\n" |
| 59 | "+9WiZp/U48iHLk7qaYXdEuO07No4BYtXn+lkOykE+FUxmA4wvOF1cTd2tdj3MzX2\n" |
| 60 | "kfGIBAYhzGZWhY3JbhIfTEfY1PNM1pWChQ==\n" |
| 61 | "-----END CERTIFICATE-----\n"; |
| 62 | |
| 63 | static const char kRootCrossSignedPEM[] = |
| 64 | "-----BEGIN CERTIFICATE-----\n" |
| 65 | "MIICYzCCAcygAwIBAgIIAj5CwoHlWuYwDQYJKoZIhvcNAQELBQAwPDEaMBgGA1UE\n" |
| 66 | "ChMRQm9yaW5nU1NMIFRFU1RJTkcxHjAcBgNVBAMTFUNyb3NzLXNpZ25pbmcgUm9v\n" |
| 67 | "dCBDQTAgFw0xNTAxMDEwMDAwMDBaGA8yMTAwMDEwMTAwMDAwMFowLjEaMBgGA1UE\n" |
| 68 | "ChMRQm9yaW5nU1NMIFRFU1RJTkcxEDAOBgNVBAMTB1Jvb3QgQ0EwgZ8wDQYJKoZI\n" |
| 69 | "hvcNAQEBBQADgY0AMIGJAoGBAOkOfxEM5lrmhoNw9lEHLgJ4EfWyJJI47iZiAseU\n" |
| 70 | "8T6hd2rAj9UiaLZd4kza4IURNcKSckmNgbSIl2u3/LJEW9lNBnD5DMaP6bPfo2qE\n" |
| 71 | "bENZvp2y0Habw9f6mVnDuOXzUwO9SdazzKJD/q3CC7kBuFYplAMkpw0oISmprpRb\n" |
| 72 | "SvmfAgMBAAGjejB4MA4GA1UdDwEB/wQEAwICBDAdBgNVHSUEFjAUBggrBgEFBQcD\n" |
| 73 | "AQYIKwYBBQUHAwIwDwYDVR0TAQH/BAUwAwEB/zAZBgNVHQ4EEgQQQDfXAftAL7gc\n" |
| 74 | "flQEJ4xZATAbBgNVHSMEFDASgBBHKHC7V3Z/3oLvEZx0RZRwMA0GCSqGSIb3DQEB\n" |
| 75 | "CwUAA4GBAErTxYJ0en9HVRHAAr5OO5wuk5Iq3VMc79TMyQLCXVL8YH8Uk7KEwv+q\n" |
| 76 | "9MEKZv2eR/Vfm4HlXlUuIqfgUXbwrAYC/YVVX86Wnbpy/jc73NYVCq8FEZeO+0XU\n" |
| 77 | "90SWAPDdp+iL7aZdimnMtG1qlM1edmz8AKbrhN/R3IbA2CL0nCWV\n" |
| 78 | "-----END CERTIFICATE-----\n"; |
| 79 | |
| 80 | static const char kIntermediatePEM[] = |
| 81 | "-----BEGIN CERTIFICATE-----\n" |
| 82 | "MIICXjCCAcegAwIBAgIJAKJMH+7rscPcMA0GCSqGSIb3DQEBCwUAMC4xGjAYBgNV\n" |
| 83 | "BAoTEUJvcmluZ1NTTCBURVNUSU5HMRAwDgYDVQQDEwdSb290IENBMCAXDTE1MDEw\n" |
| 84 | "MTAwMDAwMFoYDzIxMDAwMTAxMDAwMDAwWjA2MRowGAYDVQQKExFCb3JpbmdTU0wg\n" |
| 85 | "VEVTVElORzEYMBYGA1UEAxMPSW50ZXJtZWRpYXRlIENBMIGfMA0GCSqGSIb3DQEB\n" |
| 86 | "AQUAA4GNADCBiQKBgQC7YtI0l8ocTYJ0gKyXTtPL4iMJCNY4OcxXl48jkncVG1Hl\n" |
| 87 | "blicgNUa1r9m9YFtVkxvBinb8dXiUpEGhVg4awRPDcatlsBSEBuJkiZGYbRcAmSu\n" |
| 88 | "CmZYnf6u3aYQ18SU8WqVERPpE4cwVVs+6kwlzRw0+XDoZAczu8ZezVhCUc6NbQID\n" |
| 89 | "AQABo3oweDAOBgNVHQ8BAf8EBAMCAgQwHQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsG\n" |
| 90 | "AQUFBwMCMA8GA1UdEwEB/wQFMAMBAf8wGQYDVR0OBBIEEIwaaKi1dttdV3sfjRSy\n" |
| 91 | "BqMwGwYDVR0jBBQwEoAQQDfXAftAL7gcflQEJ4xZATANBgkqhkiG9w0BAQsFAAOB\n" |
| 92 | "gQCvnolNWEHuQS8PFVVyuLR+FKBeUUdrVbSfHSzTqNAqQGp0C9fk5oCzDq6ZgTfY\n" |
| 93 | "ESXM4cJhb3IAnW0UM0NFsYSKQJ50JZL2L3z5ZLQhHdbs4RmODGoC40BVdnJ4/qgB\n" |
| 94 | "aGSh09eQRvAVmbVCviDK2ipkWNegdyI19jFfNP5uIkGlYg==\n" |
| 95 | "-----END CERTIFICATE-----\n"; |
| 96 | |
| 97 | static const char kIntermediateSelfSignedPEM[] = |
| 98 | "-----BEGIN CERTIFICATE-----\n" |
| 99 | "MIICZjCCAc+gAwIBAgIJAKJMH+7rscPcMA0GCSqGSIb3DQEBCwUAMDYxGjAYBgNV\n" |
| 100 | "BAoTEUJvcmluZ1NTTCBURVNUSU5HMRgwFgYDVQQDEw9JbnRlcm1lZGlhdGUgQ0Ew\n" |
| 101 | "IBcNMTUwMTAxMDAwMDAwWhgPMjEwMDAxMDEwMDAwMDBaMDYxGjAYBgNVBAoTEUJv\n" |
| 102 | "cmluZ1NTTCBURVNUSU5HMRgwFgYDVQQDEw9JbnRlcm1lZGlhdGUgQ0EwgZ8wDQYJ\n" |
| 103 | "KoZIhvcNAQEBBQADgY0AMIGJAoGBALti0jSXyhxNgnSArJdO08viIwkI1jg5zFeX\n" |
| 104 | "jyOSdxUbUeVuWJyA1RrWv2b1gW1WTG8GKdvx1eJSkQaFWDhrBE8Nxq2WwFIQG4mS\n" |
| 105 | "JkZhtFwCZK4KZlid/q7dphDXxJTxapURE+kThzBVWz7qTCXNHDT5cOhkBzO7xl7N\n" |
| 106 | "WEJRzo1tAgMBAAGjejB4MA4GA1UdDwEB/wQEAwICBDAdBgNVHSUEFjAUBggrBgEF\n" |
| 107 | "BQcDAQYIKwYBBQUHAwIwDwYDVR0TAQH/BAUwAwEB/zAZBgNVHQ4EEgQQjBpoqLV2\n" |
| 108 | "211Xex+NFLIGozAbBgNVHSMEFDASgBCMGmiotXbbXVd7H40UsgajMA0GCSqGSIb3\n" |
| 109 | "DQEBCwUAA4GBALcccSrAQ0/EqQBsx0ZDTUydHXXNP2DrUkpUKmAXIe8McqIVSlkT\n" |
| 110 | "6H4xz7z8VRKBo9j+drjjtCw2i0CQc8aOLxRb5WJ8eVLnaW2XRlUqAzhF0CrulfVI\n" |
| 111 | "E4Vs6ZLU+fra1WAuIj6qFiigRja+3YkZArG8tMA9vtlhTX/g7YBZIkqH\n" |
| 112 | "-----END CERTIFICATE-----\n"; |
| 113 | |
| 114 | static const char kLeafPEM[] = |
| 115 | "-----BEGIN CERTIFICATE-----\n" |
| 116 | "MIICXjCCAcegAwIBAgIIWjO48ufpunYwDQYJKoZIhvcNAQELBQAwNjEaMBgGA1UE\n" |
| 117 | "ChMRQm9yaW5nU1NMIFRFU1RJTkcxGDAWBgNVBAMTD0ludGVybWVkaWF0ZSBDQTAg\n" |
| 118 | "Fw0xNTAxMDEwMDAwMDBaGA8yMTAwMDEwMTAwMDAwMFowMjEaMBgGA1UEChMRQm9y\n" |
| 119 | "aW5nU1NMIFRFU1RJTkcxFDASBgNVBAMTC2V4YW1wbGUuY29tMIGfMA0GCSqGSIb3\n" |
| 120 | "DQEBAQUAA4GNADCBiQKBgQDD0U0ZYgqShJ7oOjsyNKyVXEHqeafmk/bAoPqY/h1c\n" |
| 121 | "oPw2E8KmeqiUSoTPjG5IXSblOxcqpbAXgnjPzo8DI3GNMhAf8SYNYsoH7gc7Uy7j\n" |
| 122 | "5x8bUrisGnuTHqkqH6d4/e7ETJ7i3CpR8bvK16DggEvQTudLipz8FBHtYhFakfdh\n" |
| 123 | "TwIDAQABo3cwdTAOBgNVHQ8BAf8EBAMCBaAwHQYDVR0lBBYwFAYIKwYBBQUHAwEG\n" |
| 124 | "CCsGAQUFBwMCMAwGA1UdEwEB/wQCMAAwGQYDVR0OBBIEEKN5pvbur7mlXjeMEYA0\n" |
| 125 | "4nUwGwYDVR0jBBQwEoAQjBpoqLV2211Xex+NFLIGozANBgkqhkiG9w0BAQsFAAOB\n" |
| 126 | "gQBj/p+JChp//LnXWC1k121LM/ii7hFzQzMrt70bny406SGz9jAjaPOX4S3gt38y\n" |
| 127 | "rhjpPukBlSzgQXFg66y6q5qp1nQTD1Cw6NkKBe9WuBlY3iYfmsf7WT8nhlT1CttU\n" |
| 128 | "xNCwyMX9mtdXdQicOfNjIGUCD5OLV5PgHFPRKiHHioBAhg==\n" |
| 129 | "-----END CERTIFICATE-----\n"; |
| 130 | |
| 131 | static const char kLeafNoKeyUsagePEM[] = |
| 132 | "-----BEGIN CERTIFICATE-----\n" |
| 133 | "MIICNTCCAZ6gAwIBAgIJAIFQGaLQ0G2mMA0GCSqGSIb3DQEBCwUAMDYxGjAYBgNV\n" |
| 134 | "BAoTEUJvcmluZ1NTTCBURVNUSU5HMRgwFgYDVQQDEw9JbnRlcm1lZGlhdGUgQ0Ew\n" |
| 135 | "IBcNMTUwMTAxMDAwMDAwWhgPMjEwMDAxMDEwMDAwMDBaMDcxGjAYBgNVBAoTEUJv\n" |
| 136 | "cmluZ1NTTCBURVNUSU5HMRkwFwYDVQQDExBldmlsLmV4YW1wbGUuY29tMIGfMA0G\n" |
| 137 | "CSqGSIb3DQEBAQUAA4GNADCBiQKBgQDOKoZe75NPz77EOaMMl4/0s3PyQw++zJvp\n" |
| 138 | "ejHAxZiTPCJgMbEHLrSzNoHdopg+CLUH5bE4wTXM8w9Inv5P8OAFJt7gJuPUunmk\n" |
| 139 | "j+NoU3QfzOR6BroePcz1vXX9jyVHRs087M/sLqWRHu9IR+/A+UTcBaWaFiDVUxtJ\n" |
| 140 | "YOwFMwjNPQIDAQABo0gwRjAMBgNVHRMBAf8EAjAAMBkGA1UdDgQSBBBJfLEUWHq1\n" |
| 141 | "27rZ1AVx2J5GMBsGA1UdIwQUMBKAEIwaaKi1dttdV3sfjRSyBqMwDQYJKoZIhvcN\n" |
| 142 | "AQELBQADgYEALVKN2Y3LZJOtu6SxFIYKxbLaXhTGTdIjxipZhmbBRDFjbZjZZOTe\n" |
| 143 | "6Oo+VDNPYco4rBexK7umYXJyfTqoY0E8dbiImhTcGTEj7OAB3DbBomgU1AYe+t2D\n" |
| 144 | "uwBqh4Y3Eto+Zn4pMVsxGEfUpjzjZDel7bN1/oU/9KWPpDfywfUmjgk=\n" |
| 145 | "-----END CERTIFICATE-----\n"; |
| 146 | |
| 147 | static const char kForgeryPEM[] = |
| 148 | "-----BEGIN CERTIFICATE-----\n" |
| 149 | "MIICZzCCAdCgAwIBAgIIdTlMzQoKkeMwDQYJKoZIhvcNAQELBQAwNzEaMBgGA1UE\n" |
| 150 | "ChMRQm9yaW5nU1NMIFRFU1RJTkcxGTAXBgNVBAMTEGV2aWwuZXhhbXBsZS5jb20w\n" |
| 151 | "IBcNMTUwMTAxMDAwMDAwWhgPMjEwMDAxMDEwMDAwMDBaMDoxGjAYBgNVBAoTEUJv\n" |
| 152 | "cmluZ1NTTCBURVNUSU5HMRwwGgYDVQQDExNmb3JnZXJ5LmV4YW1wbGUuY29tMIGf\n" |
| 153 | "MA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDADTwruBQZGb7Ay6s9HiYv5d1lwtEy\n" |
| 154 | "xQdA2Sy8Rn8uA20Q4KgqwVY7wzIZ+z5Butrsmwb70gdG1XU+yRaDeE7XVoW6jSpm\n" |
| 155 | "0sw35/5vJbTcL4THEFbnX0OPZnvpuZDFUkvVtq5kxpDWsVyM24G8EEq7kPih3Sa3\n" |
| 156 | "OMhXVXF8kso6UQIDAQABo3cwdTAOBgNVHQ8BAf8EBAMCBaAwHQYDVR0lBBYwFAYI\n" |
| 157 | "KwYBBQUHAwEGCCsGAQUFBwMCMAwGA1UdEwEB/wQCMAAwGQYDVR0OBBIEEEYJ/WHM\n" |
| 158 | "8p64erPWIg4/liwwGwYDVR0jBBQwEoAQSXyxFFh6tdu62dQFcdieRjANBgkqhkiG\n" |
| 159 | "9w0BAQsFAAOBgQA+zH7bHPElWRWJvjxDqRexmYLn+D3Aivs8XgXQJsM94W0EzSUf\n" |
| 160 | "DSLfRgaQwcb2gg2xpDFoG+W0vc6O651uF23WGt5JaFFJJxqjII05IexfCNhuPmp4\n" |
| 161 | "4UZAXPttuJXpn74IY1tuouaM06B3vXKZR+/ityKmfJvSwxacmFcK+2ziAg==\n" |
| 162 | "-----END CERTIFICATE-----\n"; |
| 163 | |
| 164 | // kExamplePSSCert is an example RSA-PSS self-signed certificate, signed with |
| 165 | // the default hash functions. |
| 166 | static const char kExamplePSSCert[] = |
| 167 | "-----BEGIN CERTIFICATE-----\n" |
| 168 | "MIICYjCCAcagAwIBAgIJAI3qUyT6SIfzMBIGCSqGSIb3DQEBCjAFogMCAWowRTEL\n" |
| 169 | "MAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVy\n" |
| 170 | "bmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0xNDEwMDkxOTA5NTVaFw0xNTEwMDkxOTA5\n" |
| 171 | "NTVaMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQK\n" |
| 172 | "DBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwgZ8wDQYJKoZIhvcNAQEBBQADgY0A\n" |
| 173 | "MIGJAoGBAPi4bIO0vNmoV8CltFl2jFQdeesiUgR+0zfrQf2D+fCmhRU0dXFahKg8\n" |
| 174 | "0u9aTtPel4rd/7vPCqqGkr64UOTNb4AzMHYTj8p73OxaymPHAyXvqIqDWHYg+hZ3\n" |
| 175 | "13mSYwFIGth7Z/FSVUlO1m5KXNd6NzYM3t2PROjCpywrta9kS2EHAgMBAAGjUDBO\n" |
| 176 | "MB0GA1UdDgQWBBTQQfuJQR6nrVrsNF1JEflVgXgfEzAfBgNVHSMEGDAWgBTQQfuJ\n" |
| 177 | "QR6nrVrsNF1JEflVgXgfEzAMBgNVHRMEBTADAQH/MBIGCSqGSIb3DQEBCjAFogMC\n" |
| 178 | "AWoDgYEASUy2RZcgNbNQZA0/7F+V1YTLEXwD16bm+iSVnzGwtexmQVEYIZG74K/w\n" |
| 179 | "xbdZQdTbpNJkp1QPjPfh0zsatw6dmt5QoZ8K8No0DjR9dgf+Wvv5WJvJUIQBoAVN\n" |
| 180 | "Z0IL+OQFz6+LcTHxD27JJCebrATXZA0wThGTQDm7crL+a+SujBY=\n" |
| 181 | "-----END CERTIFICATE-----\n"; |
| 182 | |
| 183 | // kBadPSSCertPEM is a self-signed RSA-PSS certificate with bad parameters. |
| 184 | static const char kBadPSSCertPEM[] = |
| 185 | "-----BEGIN CERTIFICATE-----\n" |
| 186 | "MIIDdjCCAjqgAwIBAgIJANcwZLyfEv7DMD4GCSqGSIb3DQEBCjAxoA0wCwYJYIZI\n" |
| 187 | "AWUDBAIBoRowGAYJKoZIhvcNAQEIMAsGCWCGSAFlAwQCAaIEAgIA3jAnMSUwIwYD\n" |
| 188 | "VQQDDBxUZXN0IEludmFsaWQgUFNTIGNlcnRpZmljYXRlMB4XDTE1MTEwNDE2MDIz\n" |
| 189 | "NVoXDTE1MTIwNDE2MDIzNVowJzElMCMGA1UEAwwcVGVzdCBJbnZhbGlkIFBTUyBj\n" |
| 190 | "ZXJ0aWZpY2F0ZTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMTaM7WH\n" |
| 191 | "qVCAGAIA+zL1KWvvASTrhlq+1ePdO7wsrWX2KiYoTYrJYTnxhLnn0wrHqApt79nL\n" |
| 192 | "IBG7cfShyZqFHOY/IzlYPMVt+gPo293gw96Fds5JBsjhjkyGnOyr9OUntFqvxDbT\n" |
| 193 | "IIFU7o9IdxD4edaqjRv+fegVE+B79pDk4s0ujsk6dULtCg9Rst0ucGFo19mr+b7k\n" |
| 194 | "dbfn8pZ72ZNDJPueVdrUAWw9oll61UcYfk75XdrLk6JlL41GrYHc8KlfXf43gGQq\n" |
| 195 | "QfrpHkg4Ih2cI6Wt2nhFGAzrlcorzLliQIUJRIhM8h4IgDfpBpaPdVQLqS2pFbXa\n" |
| 196 | "5eQjqiyJwak2vJ8CAwEAAaNQME4wHQYDVR0OBBYEFCt180N4oGUt5LbzBwQ4Ia+2\n" |
| 197 | "4V97MB8GA1UdIwQYMBaAFCt180N4oGUt5LbzBwQ4Ia+24V97MAwGA1UdEwQFMAMB\n" |
| 198 | "Af8wMQYJKoZIhvcNAQEKMCSgDTALBglghkgBZQMEAgGhDTALBgkqhkiG9w0BAQii\n" |
| 199 | "BAICAN4DggEBAAjBtm90lGxgddjc4Xu/nbXXFHVs2zVcHv/mqOZoQkGB9r/BVgLb\n" |
| 200 | "xhHrFZ2pHGElbUYPfifdS9ztB73e1d4J+P29o0yBqfd4/wGAc/JA8qgn6AAEO/Xn\n" |
| 201 | "plhFeTRJQtLZVl75CkHXgUGUd3h+ADvKtcBuW9dSUncaUrgNKR8u/h/2sMG38RWY\n" |
| 202 | "DzBddC/66YTa3r7KkVUfW7yqRQfELiGKdcm+bjlTEMsvS+EhHup9CzbpoCx2Fx9p\n" |
| 203 | "NPtFY3yEObQhmL1JyoCRWqBE75GzFPbRaiux5UpEkns+i3trkGssZzsOuVqHNTNZ\n" |
| 204 | "lC9+9hPHIoc9UMmAQNo1vGIW3NWVoeGbaJ8=\n" |
| 205 | "-----END CERTIFICATE-----\n"; |
| 206 | |
| 207 | static const char kRSAKey[] = |
| 208 | "-----BEGIN RSA PRIVATE KEY-----\n" |
| 209 | "MIICXgIBAAKBgQDYK8imMuRi/03z0K1Zi0WnvfFHvwlYeyK9Na6XJYaUoIDAtB92\n" |
| 210 | "kWdGMdAQhLciHnAjkXLI6W15OoV3gA/ElRZ1xUpxTMhjP6PyY5wqT5r6y8FxbiiF\n" |
| 211 | "KKAnHmUcrgfVW28tQ+0rkLGMryRtrukXOgXBv7gcrmU7G1jC2a7WqmeI8QIDAQAB\n" |
| 212 | "AoGBAIBy09Fd4DOq/Ijp8HeKuCMKTHqTW1xGHshLQ6jwVV2vWZIn9aIgmDsvkjCe\n" |
| 213 | "i6ssZvnbjVcwzSoByhjN8ZCf/i15HECWDFFh6gt0P5z0MnChwzZmvatV/FXCT0j+\n" |
| 214 | "WmGNB/gkehKjGXLLcjTb6dRYVJSCZhVuOLLcbWIV10gggJQBAkEA8S8sGe4ezyyZ\n" |
| 215 | "m4e9r95g6s43kPqtj5rewTsUxt+2n4eVodD+ZUlCULWVNAFLkYRTBCASlSrm9Xhj\n" |
| 216 | "QpmWAHJUkQJBAOVzQdFUaewLtdOJoPCtpYoY1zd22eae8TQEmpGOR11L6kbxLQsk\n" |
| 217 | "aMly/DOnOaa82tqAGTdqDEZgSNmCeKKknmECQAvpnY8GUOVAubGR6c+W90iBuQLj\n" |
| 218 | "LtFp/9ihd2w/PoDwrHZaoUYVcT4VSfJQog/k7kjE4MYXYWL8eEKg3WTWQNECQQDk\n" |
| 219 | "104Wi91Umd1PzF0ijd2jXOERJU1wEKe6XLkYYNHWQAe5l4J4MWj9OdxFXAxIuuR/\n" |
| 220 | "tfDwbqkta4xcux67//khAkEAvvRXLHTaa6VFzTaiiO8SaFsHV3lQyXOtMrBpB5jd\n" |
| 221 | "moZWgjHvB2W9Ckn7sDqsPB+U2tyX0joDdQEyuiMECDY8oQ==\n" |
| 222 | "-----END RSA PRIVATE KEY-----\n"; |
| 223 | |
| 224 | |
| 225 | // CertFromPEM parses the given, NUL-terminated pem block and returns an |
| 226 | // |X509*|. |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 227 | static bssl::UniquePtr<X509> CertFromPEM(const char *pem) { |
| 228 | bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(pem, strlen(pem))); |
| 229 | return bssl::UniquePtr<X509>( |
| 230 | PEM_read_bio_X509(bio.get(), nullptr, nullptr, nullptr)); |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | // PrivateKeyFromPEM parses the given, NUL-terminated pem block and returns an |
| 234 | // |EVP_PKEY*|. |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 235 | static bssl::UniquePtr<EVP_PKEY> PrivateKeyFromPEM(const char *pem) { |
| 236 | bssl::UniquePtr<BIO> bio( |
| 237 | BIO_new_mem_buf(const_cast<char *>(pem), strlen(pem))); |
| 238 | return bssl::UniquePtr<EVP_PKEY>( |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 239 | PEM_read_bio_PrivateKey(bio.get(), nullptr, nullptr, nullptr)); |
| 240 | } |
| 241 | |
| 242 | // CertsToStack converts a vector of |X509*| to an OpenSSL STACK_OF(X509*), |
| 243 | // bumping the reference counts for each certificate in question. |
| 244 | static STACK_OF(X509)* CertsToStack(const std::vector<X509*> &certs) { |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 245 | bssl::UniquePtr<STACK_OF(X509)> stack(sk_X509_new_null()); |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 246 | if (!stack) { |
| 247 | return nullptr; |
| 248 | } |
| 249 | for (auto cert : certs) { |
| 250 | if (!sk_X509_push(stack.get(), cert)) { |
| 251 | return nullptr; |
| 252 | } |
| 253 | X509_up_ref(cert); |
| 254 | } |
| 255 | |
| 256 | return stack.release(); |
| 257 | } |
| 258 | |
| 259 | static bool Verify(X509 *leaf, const std::vector<X509 *> &roots, |
| 260 | const std::vector<X509 *> &intermediates, |
| 261 | unsigned long flags = 0) { |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 262 | bssl::UniquePtr<STACK_OF(X509)> roots_stack(CertsToStack(roots)); |
| 263 | bssl::UniquePtr<STACK_OF(X509)> intermediates_stack( |
| 264 | CertsToStack(intermediates)); |
| 265 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 266 | if (!roots_stack || |
| 267 | !intermediates_stack) { |
| 268 | return false; |
| 269 | } |
| 270 | |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 271 | bssl::UniquePtr<X509_STORE_CTX> ctx(X509_STORE_CTX_new()); |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 272 | if (!ctx) { |
| 273 | return false; |
| 274 | } |
| 275 | if (!X509_STORE_CTX_init(ctx.get(), nullptr /* no X509_STORE */, leaf, |
| 276 | intermediates_stack.get())) { |
| 277 | return false; |
| 278 | } |
| 279 | |
| 280 | X509_STORE_CTX_trusted_stack(ctx.get(), roots_stack.get()); |
| 281 | |
| 282 | X509_VERIFY_PARAM *param = X509_VERIFY_PARAM_new(); |
| 283 | if (param == nullptr) { |
| 284 | return false; |
| 285 | } |
| 286 | X509_VERIFY_PARAM_set_time(param, 1452807555 /* Jan 14th, 2016 */); |
| 287 | X509_VERIFY_PARAM_set_depth(param, 16); |
| 288 | if (flags) { |
| 289 | X509_VERIFY_PARAM_set_flags(param, flags); |
| 290 | } |
| 291 | X509_STORE_CTX_set0_param(ctx.get(), param); |
| 292 | |
| 293 | ERR_clear_error(); |
| 294 | return X509_verify_cert(ctx.get()) == 1; |
| 295 | } |
| 296 | |
| 297 | static bool TestVerify() { |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 298 | bssl::UniquePtr<X509> cross_signing_root(CertFromPEM(kCrossSigningRootPEM)); |
| 299 | bssl::UniquePtr<X509> root(CertFromPEM(kRootCAPEM)); |
| 300 | bssl::UniquePtr<X509> root_cross_signed(CertFromPEM(kRootCrossSignedPEM)); |
| 301 | bssl::UniquePtr<X509> intermediate(CertFromPEM(kIntermediatePEM)); |
| 302 | bssl::UniquePtr<X509> intermediate_self_signed( |
| 303 | CertFromPEM(kIntermediateSelfSignedPEM)); |
| 304 | bssl::UniquePtr<X509> leaf(CertFromPEM(kLeafPEM)); |
| 305 | bssl::UniquePtr<X509> leaf_no_key_usage(CertFromPEM(kLeafNoKeyUsagePEM)); |
| 306 | bssl::UniquePtr<X509> forgery(CertFromPEM(kForgeryPEM)); |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 307 | |
| 308 | if (!cross_signing_root || |
| 309 | !root || |
| 310 | !root_cross_signed || |
| 311 | !intermediate || |
| 312 | !intermediate_self_signed || |
| 313 | !leaf || |
| 314 | !leaf_no_key_usage || |
| 315 | !forgery) { |
| 316 | fprintf(stderr, "Failed to parse certificates\n"); |
| 317 | return false; |
| 318 | } |
| 319 | |
| 320 | std::vector<X509*> empty; |
| 321 | if (Verify(leaf.get(), empty, empty)) { |
| 322 | fprintf(stderr, "Leaf verified with no roots!\n"); |
| 323 | return false; |
| 324 | } |
| 325 | |
| 326 | if (Verify(leaf.get(), empty, {intermediate.get()})) { |
| 327 | fprintf(stderr, "Leaf verified with no roots!\n"); |
| 328 | return false; |
| 329 | } |
| 330 | |
| 331 | if (!Verify(leaf.get(), {root.get()}, {intermediate.get()})) { |
| 332 | ERR_print_errors_fp(stderr); |
| 333 | fprintf(stderr, "Basic chain didn't verify.\n"); |
| 334 | return false; |
| 335 | } |
| 336 | |
| 337 | if (!Verify(leaf.get(), {cross_signing_root.get()}, |
| 338 | {intermediate.get(), root_cross_signed.get()})) { |
| 339 | ERR_print_errors_fp(stderr); |
| 340 | fprintf(stderr, "Cross-signed chain didn't verify.\n"); |
| 341 | return false; |
| 342 | } |
| 343 | |
| 344 | if (!Verify(leaf.get(), {cross_signing_root.get(), root.get()}, |
| 345 | {intermediate.get(), root_cross_signed.get()})) { |
| 346 | ERR_print_errors_fp(stderr); |
| 347 | fprintf(stderr, "Cross-signed chain with root didn't verify.\n"); |
| 348 | return false; |
| 349 | } |
| 350 | |
| 351 | /* This is the “altchains” test – we remove the cross-signing CA but include |
| 352 | * the cross-sign in the intermediates. */ |
| 353 | if (!Verify(leaf.get(), {root.get()}, |
| 354 | {intermediate.get(), root_cross_signed.get()})) { |
| 355 | ERR_print_errors_fp(stderr); |
| 356 | fprintf(stderr, "Chain with cross-sign didn't backtrack to find root.\n"); |
| 357 | return false; |
| 358 | } |
| 359 | |
| 360 | if (Verify(leaf.get(), {root.get()}, |
| 361 | {intermediate.get(), root_cross_signed.get()}, |
| 362 | X509_V_FLAG_NO_ALT_CHAINS)) { |
| 363 | fprintf(stderr, "Altchains test still passed when disabled.\n"); |
| 364 | return false; |
| 365 | } |
| 366 | |
| 367 | if (Verify(forgery.get(), {intermediate_self_signed.get()}, |
| 368 | {leaf_no_key_usage.get()})) { |
| 369 | fprintf(stderr, "Basic constraints weren't checked.\n"); |
| 370 | return false; |
| 371 | } |
| 372 | |
| 373 | /* Test that one cannot skip Basic Constraints checking with a contorted set |
| 374 | * of roots and intermediates. This is a regression test for CVE-2015-1793. */ |
| 375 | if (Verify(forgery.get(), |
| 376 | {intermediate_self_signed.get(), root_cross_signed.get()}, |
| 377 | {leaf_no_key_usage.get(), intermediate.get()})) { |
| 378 | fprintf(stderr, "Basic constraints weren't checked.\n"); |
| 379 | return false; |
| 380 | } |
| 381 | |
| 382 | return true; |
| 383 | } |
| 384 | |
| 385 | static bool TestPSS() { |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 386 | bssl::UniquePtr<X509> cert(CertFromPEM(kExamplePSSCert)); |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 387 | if (!cert) { |
| 388 | return false; |
| 389 | } |
| 390 | |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 391 | bssl::UniquePtr<EVP_PKEY> pkey(X509_get_pubkey(cert.get())); |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 392 | if (!pkey) { |
| 393 | return false; |
| 394 | } |
| 395 | |
| 396 | if (!X509_verify(cert.get(), pkey.get())) { |
| 397 | fprintf(stderr, "Could not verify certificate.\n"); |
| 398 | return false; |
| 399 | } |
| 400 | return true; |
| 401 | } |
| 402 | |
| 403 | static bool TestBadPSSParameters() { |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 404 | bssl::UniquePtr<X509> cert(CertFromPEM(kBadPSSCertPEM)); |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 405 | if (!cert) { |
| 406 | return false; |
| 407 | } |
| 408 | |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 409 | bssl::UniquePtr<EVP_PKEY> pkey(X509_get_pubkey(cert.get())); |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 410 | if (!pkey) { |
| 411 | return false; |
| 412 | } |
| 413 | |
| 414 | if (X509_verify(cert.get(), pkey.get())) { |
| 415 | fprintf(stderr, "Unexpectedly verified bad certificate.\n"); |
| 416 | return false; |
| 417 | } |
| 418 | ERR_clear_error(); |
| 419 | return true; |
| 420 | } |
| 421 | |
| 422 | static bool SignatureRoundTrips(EVP_MD_CTX *md_ctx, EVP_PKEY *pkey) { |
| 423 | // Make a certificate like signed with |md_ctx|'s settings.' |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 424 | bssl::UniquePtr<X509> cert(CertFromPEM(kLeafPEM)); |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 425 | if (!cert || !X509_sign_ctx(cert.get(), md_ctx)) { |
| 426 | return false; |
| 427 | } |
| 428 | |
| 429 | // Ensure that |pkey| may still be used to verify the resulting signature. All |
| 430 | // settings in |md_ctx| must have been serialized appropriately. |
| 431 | return !!X509_verify(cert.get(), pkey); |
| 432 | } |
| 433 | |
| 434 | static bool TestSignCtx() { |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 435 | bssl::UniquePtr<EVP_PKEY> pkey(PrivateKeyFromPEM(kRSAKey)); |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 436 | if (!pkey) { |
| 437 | return false; |
| 438 | } |
| 439 | |
| 440 | // Test PKCS#1 v1.5. |
| 441 | ScopedEVP_MD_CTX md_ctx; |
| 442 | if (!EVP_DigestSignInit(md_ctx.get(), NULL, EVP_sha256(), NULL, pkey.get()) || |
| 443 | !SignatureRoundTrips(md_ctx.get(), pkey.get())) { |
| 444 | fprintf(stderr, "RSA PKCS#1 with SHA-256 failed\n"); |
| 445 | return false; |
| 446 | } |
| 447 | |
| 448 | // Test RSA-PSS with custom parameters. |
| 449 | md_ctx.Reset(); |
| 450 | EVP_PKEY_CTX *pkey_ctx; |
| 451 | if (!EVP_DigestSignInit(md_ctx.get(), &pkey_ctx, EVP_sha256(), NULL, |
| 452 | pkey.get()) || |
| 453 | !EVP_PKEY_CTX_set_rsa_padding(pkey_ctx, RSA_PKCS1_PSS_PADDING) || |
| 454 | !EVP_PKEY_CTX_set_rsa_mgf1_md(pkey_ctx, EVP_sha512()) || |
| 455 | !SignatureRoundTrips(md_ctx.get(), pkey.get())) { |
| 456 | fprintf(stderr, "RSA-PSS failed\n"); |
| 457 | return false; |
| 458 | } |
| 459 | |
| 460 | return true; |
| 461 | } |
| 462 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 463 | static int Main() { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 464 | CRYPTO_library_init(); |
| 465 | |
| 466 | if (!TestVerify() || |
| 467 | !TestPSS() || |
| 468 | !TestBadPSSParameters() || |
| 469 | !TestSignCtx()) { |
| 470 | return 1; |
| 471 | } |
| 472 | |
| 473 | printf("PASS\n"); |
| 474 | return 0; |
| 475 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 476 | |
| 477 | } // namespace bssl |
| 478 | |
| 479 | int main() { |
| 480 | return bssl::Main(); |
| 481 | } |