Jean-Paul Calderone | 8671c85 | 2011-03-02 19:26:20 -0500 | [diff] [blame] | 1 | # Copyright (c) Jean-Paul Calderone |
| 2 | # See LICENSE file for details. |
Jean-Paul Calderone | 8b63d45 | 2008-03-21 18:31:12 -0400 | [diff] [blame] | 3 | |
Jean-Paul Calderone | d8782ad | 2008-03-04 23:39:59 -0500 | [diff] [blame] | 4 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 5 | Unit tests for :py:mod:`OpenSSL.crypto`. |
Jean-Paul Calderone | d8782ad | 2008-03-04 23:39:59 -0500 | [diff] [blame] | 6 | """ |
| 7 | |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 8 | from warnings import simplefilter |
Jean-Paul Calderone | 0b88b6a | 2009-07-05 12:44:41 -0400 | [diff] [blame] | 9 | |
Alex Gaynor | 4b9c96a | 2014-08-14 09:51:48 -0700 | [diff] [blame] | 10 | import base64 |
Jean-Paul Calderone | 62ca8da | 2010-08-11 19:58:08 -0400 | [diff] [blame] | 11 | from subprocess import PIPE, Popen |
Rick Dean | 47262da | 2009-07-08 16:17:17 -0500 | [diff] [blame] | 12 | from datetime import datetime, timedelta |
Jean-Paul Calderone | d8782ad | 2008-03-04 23:39:59 -0500 | [diff] [blame] | 13 | |
Alex Gaynor | 791212d | 2015-09-05 15:46:08 -0400 | [diff] [blame] | 14 | import pytest |
| 15 | |
Alex Gaynor | 9939ba1 | 2017-06-25 16:28:24 -0400 | [diff] [blame] | 16 | from cryptography import x509 |
Paul Kehrer | 72d968b | 2016-07-29 15:31:04 +0800 | [diff] [blame] | 17 | from cryptography.hazmat.backends.openssl.backend import backend |
| 18 | from cryptography.hazmat.primitives import serialization |
| 19 | from cryptography.hazmat.primitives.asymmetric import rsa |
| 20 | |
Alex Gaynor | e466bc9 | 2017-07-06 23:43:47 -0400 | [diff] [blame] | 21 | import flaky |
| 22 | |
Alex Gaynor | 01f90a1 | 2019-02-07 09:14:48 -0500 | [diff] [blame] | 23 | from OpenSSL.crypto import TYPE_RSA, TYPE_DSA, Error, PKey |
| 24 | from OpenSSL.crypto import X509, X509Name |
Alex Gaynor | 3128750 | 2015-09-05 16:11:27 -0400 | [diff] [blame] | 25 | from OpenSSL.crypto import ( |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 26 | X509Store, |
| 27 | X509StoreFlags, |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 28 | X509StoreContext, |
| 29 | X509StoreContextError |
Alex Gaynor | 3128750 | 2015-09-05 16:11:27 -0400 | [diff] [blame] | 30 | ) |
Alex Gaynor | 01f90a1 | 2019-02-07 09:14:48 -0500 | [diff] [blame] | 31 | from OpenSSL.crypto import X509Req |
| 32 | from OpenSSL.crypto import X509Extension |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 33 | from OpenSSL.crypto import load_certificate, load_privatekey |
Cory Benfield | 6492f7c | 2015-10-27 16:57:58 +0900 | [diff] [blame] | 34 | from OpenSSL.crypto import load_publickey, dump_publickey |
Jean-Paul Calderone | f17e421 | 2009-04-01 14:21:40 -0400 | [diff] [blame] | 35 | from OpenSSL.crypto import FILETYPE_PEM, FILETYPE_ASN1, FILETYPE_TEXT |
Jean-Paul Calderone | 7191986 | 2009-04-01 13:01:19 -0400 | [diff] [blame] | 36 | from OpenSSL.crypto import dump_certificate, load_certificate_request |
| 37 | from OpenSSL.crypto import dump_certificate_request, dump_privatekey |
Alex Gaynor | 01f90a1 | 2019-02-07 09:14:48 -0500 | [diff] [blame] | 38 | from OpenSSL.crypto import PKCS7, load_pkcs7_data |
| 39 | from OpenSSL.crypto import PKCS12, load_pkcs12 |
Dominic Chen | f05b212 | 2015-10-13 16:32:35 +0000 | [diff] [blame] | 40 | from OpenSSL.crypto import CRL, Revoked, dump_crl, load_crl |
Alex Gaynor | 01f90a1 | 2019-02-07 09:14:48 -0500 | [diff] [blame] | 41 | from OpenSSL.crypto import NetscapeSPKI |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 42 | from OpenSSL.crypto import ( |
| 43 | sign, verify, get_elliptic_curve, get_elliptic_curves) |
Hynek Schlawack | f0e6685 | 2015-10-16 20:18:38 +0200 | [diff] [blame] | 44 | |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 45 | from .util import EqualityTestsMixin, is_consistent_type, WARNING_TYPE_EXPECTED |
Jean-Paul Calderone | add7bf0 | 2010-08-22 17:38:30 -0400 | [diff] [blame] | 46 | |
Alex Gaynor | aceb3e2 | 2015-09-05 12:00:22 -0400 | [diff] [blame] | 47 | |
Jean-Paul Calderone | 9da338d | 2011-05-04 11:40:54 -0400 | [diff] [blame] | 48 | def normalize_privatekey_pem(pem): |
| 49 | return dump_privatekey(FILETYPE_PEM, load_privatekey(FILETYPE_PEM, pem)) |
| 50 | |
Jean-Paul Calderone | add7bf0 | 2010-08-22 17:38:30 -0400 | [diff] [blame] | 51 | |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 52 | GOOD_CIPHER = "blowfish" |
| 53 | BAD_CIPHER = "zippers" |
| 54 | |
Anthony Alba | 2ce737f | 2015-12-04 11:04:56 +0800 | [diff] [blame] | 55 | GOOD_DIGEST = "SHA1" |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 56 | BAD_DIGEST = "monkeys" |
| 57 | |
Paul Kehrer | a40898b | 2017-06-11 16:30:58 -1000 | [diff] [blame] | 58 | old_root_cert_pem = b"""-----BEGIN CERTIFICATE----- |
Rick Dean | 94e46fd | 2009-07-18 14:51:24 -0500 | [diff] [blame] | 59 | MIIC7TCCAlagAwIBAgIIPQzE4MbeufQwDQYJKoZIhvcNAQEFBQAwWDELMAkGA1UE |
| 60 | BhMCVVMxCzAJBgNVBAgTAklMMRAwDgYDVQQHEwdDaGljYWdvMRAwDgYDVQQKEwdU |
| 61 | ZXN0aW5nMRgwFgYDVQQDEw9UZXN0aW5nIFJvb3QgQ0EwIhgPMjAwOTAzMjUxMjM2 |
| 62 | NThaGA8yMDE3MDYxMTEyMzY1OFowWDELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAklM |
| 63 | MRAwDgYDVQQHEwdDaGljYWdvMRAwDgYDVQQKEwdUZXN0aW5nMRgwFgYDVQQDEw9U |
| 64 | ZXN0aW5nIFJvb3QgQ0EwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAPmaQumL |
| 65 | urpE527uSEHdL1pqcDRmWzu+98Y6YHzT/J7KWEamyMCNZ6fRW1JCR782UQ8a07fy |
| 66 | 2xXsKy4WdKaxyG8CcatwmXvpvRQ44dSANMihHELpANTdyVp6DCysED6wkQFurHlF |
| 67 | 1dshEaJw8b/ypDhmbVIo6Ci1xvCJqivbLFnbAgMBAAGjgbswgbgwHQYDVR0OBBYE |
| 68 | FINVdy1eIfFJDAkk51QJEo3IfgSuMIGIBgNVHSMEgYAwfoAUg1V3LV4h8UkMCSTn |
| 69 | VAkSjch+BK6hXKRaMFgxCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJJTDEQMA4GA1UE |
| 70 | BxMHQ2hpY2FnbzEQMA4GA1UEChMHVGVzdGluZzEYMBYGA1UEAxMPVGVzdGluZyBS |
| 71 | b290IENBggg9DMTgxt659DAMBgNVHRMEBTADAQH/MA0GCSqGSIb3DQEBBQUAA4GB |
| 72 | AGGCDazMJGoWNBpc03u6+smc95dEead2KlZXBATOdFT1VesY3+nUOqZhEhTGlDMi |
| 73 | hkgaZnzoIq/Uamidegk4hirsCT/R+6vsKAAxNTcBjUeZjlykCJWy5ojShGftXIKY |
| 74 | w/njVbKMXrvc83qmTdGl3TAM0fxQIpqgcglFLveEBgzn |
| 75 | -----END CERTIFICATE----- |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 76 | """ |
Rick Dean | 94e46fd | 2009-07-18 14:51:24 -0500 | [diff] [blame] | 77 | |
Paul Kehrer | a40898b | 2017-06-11 16:30:58 -1000 | [diff] [blame] | 78 | root_cert_pem = b"""-----BEGIN CERTIFICATE----- |
| 79 | MIIC6TCCAlKgAwIBAgIIPQzE4MbeufQwDQYJKoZIhvcNAQEFBQAwWDELMAkGA1UE |
| 80 | BhMCVVMxCzAJBgNVBAgTAklMMRAwDgYDVQQHEwdDaGljYWdvMRAwDgYDVQQKEwdU |
| 81 | ZXN0aW5nMRgwFgYDVQQDEw9UZXN0aW5nIFJvb3QgQ0EwHhcNMTcwNjExMjIzMjU5 |
| 82 | WhcNMzcwNjA2MjIzMjU5WjBYMQswCQYDVQQGEwJVUzELMAkGA1UECBMCSUwxEDAO |
| 83 | BgNVBAcTB0NoaWNhZ28xEDAOBgNVBAoTB1Rlc3RpbmcxGDAWBgNVBAMTD1Rlc3Rp |
| 84 | bmcgUm9vdCBDQTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA+ZpC6Yu6ukTn |
| 85 | bu5IQd0vWmpwNGZbO773xjpgfNP8nspYRqbIwI1np9FbUkJHvzZRDxrTt/LbFewr |
| 86 | LhZ0prHIbwJxq3CZe+m9FDjh1IA0yKEcQukA1N3JWnoMLKwQPrCRAW6seUXV2yER |
| 87 | onDxv/KkOGZtUijoKLXG8ImqK9ssWdsCAwEAAaOBuzCBuDAdBgNVHQ4EFgQUg1V3 |
| 88 | LV4h8UkMCSTnVAkSjch+BK4wgYgGA1UdIwSBgDB+gBSDVXctXiHxSQwJJOdUCRKN |
| 89 | yH4ErqFcpFowWDELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAklMMRAwDgYDVQQHEwdD |
| 90 | aGljYWdvMRAwDgYDVQQKEwdUZXN0aW5nMRgwFgYDVQQDEw9UZXN0aW5nIFJvb3Qg |
| 91 | Q0GCCD0MxODG3rn0MAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADgYEANFYQ |
| 92 | R+T70VcZ+SnvURnwviFgCXeedBzCr21meo+DNHbkp2gudB9W8Xrned/wtUBVymy9 |
| 93 | gjB5jINfU7Lci0H57Evsw96UJJVfhXdUMHpqt1RGCoEd9FWnrDyrSy0NysnBT2bH |
| 94 | lEqxh3aFEUx9IOQ4sgnx1/NOFXBpkRtivl6O0Ec= |
| 95 | -----END CERTIFICATE----- |
| 96 | """ |
| 97 | |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 98 | root_key_pem = b"""-----BEGIN RSA PRIVATE KEY----- |
Rick Dean | 94e46fd | 2009-07-18 14:51:24 -0500 | [diff] [blame] | 99 | MIICXQIBAAKBgQD5mkLpi7q6ROdu7khB3S9aanA0Zls7vvfGOmB80/yeylhGpsjA |
| 100 | jWen0VtSQke/NlEPGtO38tsV7CsuFnSmschvAnGrcJl76b0UOOHUgDTIoRxC6QDU |
| 101 | 3claegwsrBA+sJEBbqx5RdXbIRGicPG/8qQ4Zm1SKOgotcbwiaor2yxZ2wIDAQAB |
| 102 | AoGBAPCgMpmLxzwDaUmcFbTJUvlLW1hoxNNYSu2jIZm1k/hRAcE60JYwvBkgz3UB |
| 103 | yMEh0AtLxYe0bFk6EHah11tMUPgscbCq73snJ++8koUw+csk22G65hOs51bVb7Aa |
| 104 | 6JBe67oLzdtvgCUFAA2qfrKzWRZzAdhUirQUZgySZk+Xq1pBAkEA/kZG0A6roTSM |
| 105 | BVnx7LnPfsycKUsTumorpXiylZJjTi9XtmzxhrYN6wgZlDOOwOLgSQhszGpxVoMD |
| 106 | u3gByT1b2QJBAPtL3mSKdvwRu/+40zaZLwvSJRxaj0mcE4BJOS6Oqs/hS1xRlrNk |
| 107 | PpQ7WJ4yM6ZOLnXzm2mKyxm50Mv64109FtMCQQDOqS2KkjHaLowTGVxwC0DijMfr |
| 108 | I9Lf8sSQk32J5VWCySWf5gGTfEnpmUa41gKTMJIbqZZLucNuDcOtzUaeWZlZAkA8 |
| 109 | ttXigLnCqR486JDPTi9ZscoZkZ+w7y6e/hH8t6d5Vjt48JVyfjPIaJY+km58LcN3 |
| 110 | 6AWSeGAdtRFHVzR7oHjVAkB4hutvxiOeiIVQNBhM6RSI9aBPMI21DoX2JRoxvNW2 |
| 111 | cbvAhow217X9V0dVerEOKxnNYspXRrh36h7k4mQA+sDq |
| 112 | -----END RSA PRIVATE KEY----- |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 113 | """ |
Rick Dean | 94e46fd | 2009-07-18 14:51:24 -0500 | [diff] [blame] | 114 | |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 115 | intermediate_cert_pem = b"""-----BEGIN CERTIFICATE----- |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 116 | MIICVzCCAcCgAwIBAgIRAMPzhm6//0Y/g2pmnHR2C4cwDQYJKoZIhvcNAQENBQAw |
| 117 | WDELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAklMMRAwDgYDVQQHEwdDaGljYWdvMRAw |
| 118 | DgYDVQQKEwdUZXN0aW5nMRgwFgYDVQQDEw9UZXN0aW5nIFJvb3QgQ0EwHhcNMTQw |
| 119 | ODI4MDIwNDA4WhcNMjQwODI1MDIwNDA4WjBmMRUwEwYDVQQDEwxpbnRlcm1lZGlh |
| 120 | dGUxDDAKBgNVBAoTA29yZzERMA8GA1UECxMIb3JnLXVuaXQxCzAJBgNVBAYTAlVT |
| 121 | MQswCQYDVQQIEwJDQTESMBAGA1UEBxMJU2FuIERpZWdvMIGfMA0GCSqGSIb3DQEB |
| 122 | AQUAA4GNADCBiQKBgQDYcEQw5lfbEQRjr5Yy4yxAHGV0b9Al+Lmu7wLHMkZ/ZMmK |
| 123 | FGIbljbviiD1Nz97Oh2cpB91YwOXOTN2vXHq26S+A5xe8z/QJbBsyghMur88CjdT |
| 124 | 21H2qwMa+r5dCQwEhuGIiZ3KbzB/n4DTMYI5zy4IYPv0pjxShZn4aZTCCK2IUwID |
| 125 | AQABoxMwETAPBgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3DQEBDQUAA4GBAPIWSkLX |
| 126 | QRMApOjjyC+tMxumT5e2pMqChHmxobQK4NMdrf2VCx+cRT6EmY8sK3/Xl/X8UBQ+ |
| 127 | 9n5zXb1ZwhW/sTWgUvmOceJ4/XVs9FkdWOOn1J0XBch9ZIiFe/s5ASIgG7fUdcUF |
| 128 | 9mAWS6FK2ca3xIh5kIupCXOFa0dPvlw/YUFT |
| 129 | -----END CERTIFICATE----- |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 130 | """ |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 131 | |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 132 | intermediate_key_pem = b"""-----BEGIN RSA PRIVATE KEY----- |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 133 | MIICWwIBAAKBgQDYcEQw5lfbEQRjr5Yy4yxAHGV0b9Al+Lmu7wLHMkZ/ZMmKFGIb |
| 134 | ljbviiD1Nz97Oh2cpB91YwOXOTN2vXHq26S+A5xe8z/QJbBsyghMur88CjdT21H2 |
| 135 | qwMa+r5dCQwEhuGIiZ3KbzB/n4DTMYI5zy4IYPv0pjxShZn4aZTCCK2IUwIDAQAB |
| 136 | AoGAfSZVV80pSeOKHTYfbGdNY/jHdU9eFUa/33YWriXU+77EhpIItJjkRRgivIfo |
| 137 | rhFJpBSGmDLblaqepm8emsXMeH4+2QzOYIf0QGGP6E6scjTt1PLqdqKfVJ1a2REN |
| 138 | 147cujNcmFJb/5VQHHMpaPTgttEjlzuww4+BCDPsVRABWrkCQQD3loH36nLoQTtf |
| 139 | +kQq0T6Bs9/UWkTAGo0ND81ALj0F8Ie1oeZg6RNT96RxZ3aVuFTESTv6/TbjWywO |
| 140 | wdzlmV1vAkEA38rTJ6PTwaJlw5OttdDzAXGPB9tDmzh9oSi7cHwQQXizYd8MBYx4 |
| 141 | sjHUKD3dCQnb1dxJFhd3BT5HsnkRMbVZXQJAbXduH17ZTzcIOXc9jHDXYiFVZV5D |
| 142 | 52vV0WCbLzVCZc3jMrtSUKa8lPN5EWrdU3UchWybyG0MR5mX8S5lrF4SoQJAIyUD |
| 143 | DBKaSqpqONCUUx1BTFS9FYrFjzbL4+c1qHCTTPTblt8kUCrDOZjBrKAqeiTmNSum |
| 144 | /qUot9YUBF8m6BuGsQJATHHmdFy/fG1VLkyBp49CAa8tN3Z5r/CgTznI4DfMTf4C |
| 145 | NbRHn2UmYlwQBa+L5lg9phewNe8aEwpPyPLoV85U8Q== |
| 146 | -----END RSA PRIVATE KEY----- |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 147 | """ |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 148 | |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 149 | server_cert_pem = b"""-----BEGIN CERTIFICATE----- |
Paul Kehrer | a40898b | 2017-06-11 16:30:58 -1000 | [diff] [blame] | 150 | MIICJDCCAY2gAwIBAgIJAJn/HpR21r/8MA0GCSqGSIb3DQEBBQUAMFgxCzAJBgNV |
| 151 | BAYTAlVTMQswCQYDVQQIDAJJTDEQMA4GA1UEBwwHQ2hpY2FnbzEQMA4GA1UECgwH |
| 152 | VGVzdGluZzEYMBYGA1UEAwwPVGVzdGluZyBSb290IENBMB4XDTE3MDYxMjAwMTA1 |
| 153 | N1oXDTM3MDYwNzAwMTA1N1owGDEWMBQGA1UEAwwNbG92ZWx5IHNlcnZlcjCBnzAN |
| 154 | BgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAvqb4brndXS2kEL84qXbZXE6LYK+UrhNi |
| 155 | 70sdIM/24NVN7tXkPlOXqrMWhFHHml+aeSpPkH5b1vKnY1TcULmEubnNICtvjmZ5 |
| 156 | SGMQn+J+RmBs1SMd0EgY/0wBBQdlrlYp2QYgm8YC+zxTNSqWvhMFZAgHbj6Un5SS |
| 157 | T8JGBqytjB0CAwEAAaM2MDQwHQYDVR0OBBYEFINVdy1eIfFJDAkk51QJEo3IfgSu |
| 158 | MBMGA1UdJQQMMAoGCCsGAQUFBwMBMA0GCSqGSIb3DQEBBQUAA4GBAGki1K6WgHHJ |
| 159 | qC6aY2EowjaWOXLO6jUZIhGk7BA7vMRfNug429AOZ4m5F6OQhzmJmlw67Jyu2FeI |
| 160 | h0VtBuQoHPtjqZXF59oX6hMMmGLMs9pV0UA3fJs5MYA4/V5ZcQy0Ie0QoJNejLzE |
| 161 | 6V1Qz1rRTYLUyEcpI7ZCmBg2KQQI8YZI |
Rick Dean | 94e46fd | 2009-07-18 14:51:24 -0500 | [diff] [blame] | 162 | -----END CERTIFICATE----- |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 163 | """ |
Rick Dean | 94e46fd | 2009-07-18 14:51:24 -0500 | [diff] [blame] | 164 | |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 165 | server_key_pem = normalize_privatekey_pem(b"""-----BEGIN RSA PRIVATE KEY----- |
Rick Dean | 94e46fd | 2009-07-18 14:51:24 -0500 | [diff] [blame] | 166 | MIICWwIBAAKBgQC+pvhuud1dLaQQvzipdtlcTotgr5SuE2LvSx0gz/bg1U3u1eQ+ |
| 167 | U5eqsxaEUceaX5p5Kk+QflvW8qdjVNxQuYS5uc0gK2+OZnlIYxCf4n5GYGzVIx3Q |
| 168 | SBj/TAEFB2WuVinZBiCbxgL7PFM1Kpa+EwVkCAduPpSflJJPwkYGrK2MHQIDAQAB |
| 169 | AoGAbwuZ0AR6JveahBaczjfnSpiFHf+mve2UxoQdpyr6ROJ4zg/PLW5K/KXrC48G |
| 170 | j6f3tXMrfKHcpEoZrQWUfYBRCUsGD5DCazEhD8zlxEHahIsqpwA0WWssJA2VOLEN |
| 171 | j6DuV2pCFbw67rfTBkTSo32ahfXxEKev5KswZk0JIzH3ooECQQDgzS9AI89h0gs8 |
| 172 | Dt+1m11Rzqo3vZML7ZIyGApUzVan+a7hbc33nbGRkAXjHaUBJO31it/H6dTO+uwX |
| 173 | msWwNG5ZAkEA2RyFKs5xR5USTFaKLWCgpH/ydV96KPOpBND7TKQx62snDenFNNbn |
| 174 | FwwOhpahld+vqhYk+pfuWWUpQciE+Bu7ZQJASjfT4sQv4qbbKK/scePicnDdx9th |
| 175 | 4e1EeB9xwb+tXXXUo/6Bor/AcUNwfiQ6Zt9PZOK9sR3lMZSsP7rMi7kzuQJABie6 |
| 176 | 1sXXjFH7nNJvRG4S39cIxq8YRYTy68II/dlB2QzGpKxV/POCxbJ/zu0CU79tuYK7 |
| 177 | NaeNCFfH3aeTrX0LyQJAMBWjWmeKM2G2sCExheeQK0ROnaBC8itCECD4Jsve4nqf |
| 178 | r50+LF74iLXFwqysVCebPKMOpDWp/qQ1BbJQIPs7/A== |
| 179 | -----END RSA PRIVATE KEY----- |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 180 | """) |
Rick Dean | 94e46fd | 2009-07-18 14:51:24 -0500 | [diff] [blame] | 181 | |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 182 | intermediate_server_cert_pem = b"""-----BEGIN CERTIFICATE----- |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 183 | MIICWDCCAcGgAwIBAgIRAPQFY9jfskSihdiNSNdt6GswDQYJKoZIhvcNAQENBQAw |
| 184 | ZjEVMBMGA1UEAxMMaW50ZXJtZWRpYXRlMQwwCgYDVQQKEwNvcmcxETAPBgNVBAsT |
| 185 | CG9yZy11bml0MQswCQYDVQQGEwJVUzELMAkGA1UECBMCQ0ExEjAQBgNVBAcTCVNh |
| 186 | biBEaWVnbzAeFw0xNDA4MjgwMjEwNDhaFw0yNDA4MjUwMjEwNDhaMG4xHTAbBgNV |
| 187 | BAMTFGludGVybWVkaWF0ZS1zZXJ2aWNlMQwwCgYDVQQKEwNvcmcxETAPBgNVBAsT |
| 188 | CG9yZy11bml0MQswCQYDVQQGEwJVUzELMAkGA1UECBMCQ0ExEjAQBgNVBAcTCVNh |
| 189 | biBEaWVnbzCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAqpJZygd+w1faLOr1 |
| 190 | iOAmbBhx5SZWcTCZ/ZjHQTJM7GuPT624QkqsixFghRKdDROwpwnAP7gMRukLqiy4 |
| 191 | +kRuGT5OfyGggL95i2xqA+zehjj08lSTlvGHpePJgCyTavIy5+Ljsj4DKnKyuhxm |
| 192 | biXTRrH83NDgixVkObTEmh/OVK0CAwEAATANBgkqhkiG9w0BAQ0FAAOBgQBa0Npw |
| 193 | UkzjaYEo1OUE1sTI6Mm4riTIHMak4/nswKh9hYup//WVOlr/RBSBtZ7Q/BwbjobN |
| 194 | 3bfAtV7eSAqBsfxYXyof7G1ALANQERkq3+oyLP1iVt08W1WOUlIMPhdCF/QuCwy6 |
| 195 | x9MJLhUCGLJPM+O2rAPWVD9wCmvq10ALsiH3yA== |
| 196 | -----END CERTIFICATE----- |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 197 | """ |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 198 | |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 199 | intermediate_server_key_pem = b"""-----BEGIN RSA PRIVATE KEY----- |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 200 | MIICXAIBAAKBgQCqklnKB37DV9os6vWI4CZsGHHlJlZxMJn9mMdBMkzsa49PrbhC |
| 201 | SqyLEWCFEp0NE7CnCcA/uAxG6QuqLLj6RG4ZPk5/IaCAv3mLbGoD7N6GOPTyVJOW |
| 202 | 8Yel48mALJNq8jLn4uOyPgMqcrK6HGZuJdNGsfzc0OCLFWQ5tMSaH85UrQIDAQAB |
| 203 | AoGAIQ594j5zna3/9WaPsTgnmhlesVctt4AAx/n827DA4ayyuHFlXUuVhtoWR5Pk |
| 204 | 5ezj9mtYW8DyeCegABnsu2vZni/CdvU6uiS1Hv6qM1GyYDm9KWgovIP9rQCDSGaz |
| 205 | d57IWVGxx7ODFkm3gN5nxnSBOFVHytuW1J7FBRnEsehRroECQQDXHFOv82JuXDcz |
| 206 | z3+4c74IEURdOHcbycxlppmK9kFqm5lsUdydnnGW+mvwDk0APOB7Wg7vyFyr393e |
| 207 | dpmBDCzNAkEAyv6tVbTKUYhSjW+QhabJo896/EqQEYUmtMXxk4cQnKeR/Ao84Rkf |
| 208 | EqD5IykMUfUI0jJU4DGX+gWZ10a7kNbHYQJAVFCuHNFxS4Cpwo0aqtnzKoZaHY/8 |
| 209 | X9ABZfafSHCtw3Op92M+7ikkrOELXdS9KdKyyqbKJAKNEHF3LbOfB44WIQJAA2N4 |
| 210 | 9UNNVUsXRbElEnYUS529CdUczo4QdVgQjkvk5RiPAUwSdBd9Q0xYnFOlFwEmIowg |
| 211 | ipWJWe0aAlP18ZcEQQJBAL+5lekZ/GUdQoZ4HAsN5a9syrzavJ9VvU1KOOPorPZK |
| 212 | nMRZbbQgP+aSB7yl6K0gaLaZ8XaK0pjxNBh6ASqg9f4= |
| 213 | -----END RSA PRIVATE KEY----- |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 214 | """ |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 215 | |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 216 | client_cert_pem = b"""-----BEGIN CERTIFICATE----- |
Paul Kehrer | a40898b | 2017-06-11 16:30:58 -1000 | [diff] [blame] | 217 | MIICIjCCAYugAwIBAgIJAKxpFI5lODkjMA0GCSqGSIb3DQEBBQUAMFgxCzAJBgNV |
| 218 | BAYTAlVTMQswCQYDVQQIDAJJTDEQMA4GA1UEBwwHQ2hpY2FnbzEQMA4GA1UECgwH |
| 219 | VGVzdGluZzEYMBYGA1UEAwwPVGVzdGluZyBSb290IENBMB4XDTE3MDYxMjAwMDQx |
| 220 | M1oXDTM3MDYwNzAwMDQxM1owFjEUMBIGA1UEAwwLdWdseSBjbGllbnQwgZ8wDQYJ |
| 221 | KoZIhvcNAQEBBQADgY0AMIGJAoGBAMBmH9JG02bme0xPipvpjMSlOugyWrauf4at |
| 222 | EdGJn7GQLD8IY2Fu0+Kvv9DFpSPboFKZCsfDVsYoRs+xaJbtt1dJ6ymX7EqKS7gb |
| 223 | 8q+eeZZ14keqyJd5Rm2q6swQtw9ADD3E8cS6GqpQm+8SgxOycISoYz7sO1ugJFqN |
| 224 | jId+W4BFAgMBAAGjNjA0MB0GA1UdDgQWBBSDVXctXiHxSQwJJOdUCRKNyH4ErjAT |
| 225 | BgNVHSUEDDAKBggrBgEFBQcDAjANBgkqhkiG9w0BAQUFAAOBgQAMqcHyweaCOZNN |
| 226 | dWQQOsBKQlL5wqVVZwucHPWqobjxpULKy9gS2ha2zbgkXcB/BnBOSwe0Fm+MJV0T |
| 227 | NbnTghcGJNpEH7VKn4xSLvIGZmnZZWgxeIB16z4GhpkK2fShBJ+6GKZjsgjT0lSH |
| 228 | JRgjHbWutZfZvbSHXr9n7PIphG1Ojg== |
Rick Dean | 94e46fd | 2009-07-18 14:51:24 -0500 | [diff] [blame] | 229 | -----END CERTIFICATE----- |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 230 | """ |
Rick Dean | 94e46fd | 2009-07-18 14:51:24 -0500 | [diff] [blame] | 231 | |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 232 | client_key_pem = normalize_privatekey_pem(b"""-----BEGIN RSA PRIVATE KEY----- |
Rick Dean | 94e46fd | 2009-07-18 14:51:24 -0500 | [diff] [blame] | 233 | MIICXgIBAAKBgQDAZh/SRtNm5ntMT4qb6YzEpTroMlq2rn+GrRHRiZ+xkCw/CGNh |
| 234 | btPir7/QxaUj26BSmQrHw1bGKEbPsWiW7bdXSespl+xKiku4G/KvnnmWdeJHqsiX |
| 235 | eUZtqurMELcPQAw9xPHEuhqqUJvvEoMTsnCEqGM+7DtboCRajYyHfluARQIDAQAB |
| 236 | AoGATkZ+NceY5Glqyl4mD06SdcKfV65814vg2EL7V9t8+/mi9rYL8KztSXGlQWPX |
| 237 | zuHgtRoMl78yQ4ZJYOBVo+nsx8KZNRCEBlE19bamSbQLCeQMenWnpeYyQUZ908gF |
| 238 | h6L9qsFVJepgA9RDgAjyDoS5CaWCdCCPCH2lDkdcqC54SVUCQQDseuduc4wi8h4t |
| 239 | V8AahUn9fn9gYfhoNuM0gdguTA0nPLVWz4hy1yJiWYQe0H7NLNNTmCKiLQaJpAbb |
| 240 | TC6vE8C7AkEA0Ee8CMJUc20BnGEmxwgWcVuqFWaKCo8jTH1X38FlATUsyR3krjW2 |
| 241 | dL3yDD9NwHxsYP7nTKp/U8MV7U9IBn4y/wJBAJl7H0/BcLeRmuJk7IqJ7b635iYB |
| 242 | D/9beFUw3MUXmQXZUfyYz39xf6CDZsu1GEdEC5haykeln3Of4M9d/4Kj+FcCQQCY |
| 243 | si6xwT7GzMDkk/ko684AV3KPc/h6G0yGtFIrMg7J3uExpR/VdH2KgwMkZXisSMvw |
| 244 | JJEQjOMCVsEJlRk54WWjAkEAzoZNH6UhDdBK5F38rVt/y4SEHgbSfJHIAmPS32Kq |
| 245 | f6GGcfNpip0Uk7q7udTKuX7Q/buZi/C4YW7u3VKAquv9NA== |
| 246 | -----END RSA PRIVATE KEY----- |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 247 | """) |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 248 | |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 249 | cleartextCertificatePEM = b"""-----BEGIN CERTIFICATE----- |
Paul Kehrer | a40898b | 2017-06-11 16:30:58 -1000 | [diff] [blame] | 250 | MIIC6TCCAlKgAwIBAgIIPQzE4MbeufQwDQYJKoZIhvcNAQEFBQAwWDELMAkGA1UE |
Jean-Paul Calderone | 20131f5 | 2009-04-01 12:05:45 -0400 | [diff] [blame] | 251 | BhMCVVMxCzAJBgNVBAgTAklMMRAwDgYDVQQHEwdDaGljYWdvMRAwDgYDVQQKEwdU |
Paul Kehrer | a40898b | 2017-06-11 16:30:58 -1000 | [diff] [blame] | 252 | ZXN0aW5nMRgwFgYDVQQDEw9UZXN0aW5nIFJvb3QgQ0EwHhcNMTcwNjExMjIzMjU5 |
| 253 | WhcNMzcwNjA2MjIzMjU5WjBYMQswCQYDVQQGEwJVUzELMAkGA1UECBMCSUwxEDAO |
| 254 | BgNVBAcTB0NoaWNhZ28xEDAOBgNVBAoTB1Rlc3RpbmcxGDAWBgNVBAMTD1Rlc3Rp |
| 255 | bmcgUm9vdCBDQTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA+ZpC6Yu6ukTn |
| 256 | bu5IQd0vWmpwNGZbO773xjpgfNP8nspYRqbIwI1np9FbUkJHvzZRDxrTt/LbFewr |
| 257 | LhZ0prHIbwJxq3CZe+m9FDjh1IA0yKEcQukA1N3JWnoMLKwQPrCRAW6seUXV2yER |
| 258 | onDxv/KkOGZtUijoKLXG8ImqK9ssWdsCAwEAAaOBuzCBuDAdBgNVHQ4EFgQUg1V3 |
| 259 | LV4h8UkMCSTnVAkSjch+BK4wgYgGA1UdIwSBgDB+gBSDVXctXiHxSQwJJOdUCRKN |
| 260 | yH4ErqFcpFowWDELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAklMMRAwDgYDVQQHEwdD |
| 261 | aGljYWdvMRAwDgYDVQQKEwdUZXN0aW5nMRgwFgYDVQQDEw9UZXN0aW5nIFJvb3Qg |
| 262 | Q0GCCD0MxODG3rn0MAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADgYEANFYQ |
| 263 | R+T70VcZ+SnvURnwviFgCXeedBzCr21meo+DNHbkp2gudB9W8Xrned/wtUBVymy9 |
| 264 | gjB5jINfU7Lci0H57Evsw96UJJVfhXdUMHpqt1RGCoEd9FWnrDyrSy0NysnBT2bH |
| 265 | lEqxh3aFEUx9IOQ4sgnx1/NOFXBpkRtivl6O0Ec= |
Jean-Paul Calderone | 20131f5 | 2009-04-01 12:05:45 -0400 | [diff] [blame] | 266 | -----END CERTIFICATE----- |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 267 | """ |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 268 | |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 269 | cleartextPrivateKeyPEM = normalize_privatekey_pem(b"""\ |
Jean-Paul Calderone | d50d204 | 2011-05-04 17:00:49 -0400 | [diff] [blame] | 270 | -----BEGIN RSA PRIVATE KEY----- |
Jean-Paul Calderone | 20131f5 | 2009-04-01 12:05:45 -0400 | [diff] [blame] | 271 | MIICXQIBAAKBgQD5mkLpi7q6ROdu7khB3S9aanA0Zls7vvfGOmB80/yeylhGpsjA |
| 272 | jWen0VtSQke/NlEPGtO38tsV7CsuFnSmschvAnGrcJl76b0UOOHUgDTIoRxC6QDU |
| 273 | 3claegwsrBA+sJEBbqx5RdXbIRGicPG/8qQ4Zm1SKOgotcbwiaor2yxZ2wIDAQAB |
| 274 | AoGBAPCgMpmLxzwDaUmcFbTJUvlLW1hoxNNYSu2jIZm1k/hRAcE60JYwvBkgz3UB |
| 275 | yMEh0AtLxYe0bFk6EHah11tMUPgscbCq73snJ++8koUw+csk22G65hOs51bVb7Aa |
| 276 | 6JBe67oLzdtvgCUFAA2qfrKzWRZzAdhUirQUZgySZk+Xq1pBAkEA/kZG0A6roTSM |
| 277 | BVnx7LnPfsycKUsTumorpXiylZJjTi9XtmzxhrYN6wgZlDOOwOLgSQhszGpxVoMD |
| 278 | u3gByT1b2QJBAPtL3mSKdvwRu/+40zaZLwvSJRxaj0mcE4BJOS6Oqs/hS1xRlrNk |
| 279 | PpQ7WJ4yM6ZOLnXzm2mKyxm50Mv64109FtMCQQDOqS2KkjHaLowTGVxwC0DijMfr |
| 280 | I9Lf8sSQk32J5VWCySWf5gGTfEnpmUa41gKTMJIbqZZLucNuDcOtzUaeWZlZAkA8 |
| 281 | ttXigLnCqR486JDPTi9ZscoZkZ+w7y6e/hH8t6d5Vjt48JVyfjPIaJY+km58LcN3 |
| 282 | 6AWSeGAdtRFHVzR7oHjVAkB4hutvxiOeiIVQNBhM6RSI9aBPMI21DoX2JRoxvNW2 |
| 283 | cbvAhow217X9V0dVerEOKxnNYspXRrh36h7k4mQA+sDq |
| 284 | -----END RSA PRIVATE KEY----- |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 285 | """) |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 286 | |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 287 | cleartextCertificateRequestPEM = b"""-----BEGIN CERTIFICATE REQUEST----- |
Jean-Paul Calderone | add7bf0 | 2010-08-22 17:38:30 -0400 | [diff] [blame] | 288 | MIIBnjCCAQcCAQAwXjELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAklMMRAwDgYDVQQH |
| 289 | EwdDaGljYWdvMRcwFQYDVQQKEw5NeSBDb21wYW55IEx0ZDEXMBUGA1UEAxMORnJl |
| 290 | ZGVyaWNrIERlYW4wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBANp6Y17WzKSw |
| 291 | BsUWkXdqg6tnXy8H8hA1msCMWpc+/2KJ4mbv5NyD6UD+/SqagQqulPbF/DFea9nA |
| 292 | E0zhmHJELcM8gUTIlXv/cgDWnmK4xj8YkjVUiCdqKRAKeuzLG1pGmwwF5lGeJpXN |
| 293 | xQn5ecR0UYSOWj6TTGXB9VyUMQzCClcBAgMBAAGgADANBgkqhkiG9w0BAQUFAAOB |
| 294 | gQAAJGuF/R/GGbeC7FbFW+aJgr9ee0Xbl6nlhu7pTe67k+iiKT2dsl2ti68MVTnu |
| 295 | Vrb3HUNqOkiwsJf6kCtq5oPn3QVYzTa76Dt2y3Rtzv6boRSlmlfrgS92GNma8JfR |
| 296 | oICQk3nAudi6zl1Dix3BCv1pUp5KMtGn3MeDEi6QFGy2rA== |
| 297 | -----END CERTIFICATE REQUEST----- |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 298 | """ |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 299 | |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 300 | encryptedPrivateKeyPEM = b"""-----BEGIN RSA PRIVATE KEY----- |
Jean-Paul Calderone | 20131f5 | 2009-04-01 12:05:45 -0400 | [diff] [blame] | 301 | Proc-Type: 4,ENCRYPTED |
| 302 | DEK-Info: DES-EDE3-CBC,9573604A18579E9E |
Jean-Paul Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 303 | |
Jean-Paul Calderone | 20131f5 | 2009-04-01 12:05:45 -0400 | [diff] [blame] | 304 | SHOho56WxDkT0ht10UTeKc0F5u8cqIa01kzFAmETw0MAs8ezYtK15NPdCXUm3X/2 |
| 305 | a17G7LSF5bkxOgZ7vpXyMzun/owrj7CzvLxyncyEFZWvtvzaAhPhvTJtTIB3kf8B |
| 306 | 8+qRcpTGK7NgXEgYBW5bj1y4qZkD4zCL9o9NQzsKI3Ie8i0239jsDOWR38AxjXBH |
| 307 | mGwAQ4Z6ZN5dnmM4fhMIWsmFf19sNyAML4gHenQCHhmXbjXeVq47aC2ProInJbrm |
| 308 | +00TcisbAQ40V9aehVbcDKtS4ZbMVDwncAjpXpcncC54G76N6j7F7wL7L/FuXa3A |
| 309 | fvSVy9n2VfF/pJ3kYSflLHH2G/DFxjF7dl0GxhKPxJjp3IJi9VtuvmN9R2jZWLQF |
| 310 | tfC8dXgy/P9CfFQhlinqBTEwgH0oZ/d4k4NVFDSdEMaSdmBAjlHpc+Vfdty3HVnV |
| 311 | rKXj//wslsFNm9kIwJGIgKUa/n2jsOiydrsk1mgH7SmNCb3YHgZhbbnq0qLat/HC |
| 312 | gHDt3FHpNQ31QzzL3yrenFB2L9osIsnRsDTPFNi4RX4SpDgNroxOQmyzCCV6H+d4 |
| 313 | o1mcnNiZSdxLZxVKccq0AfRpHqpPAFnJcQHP6xyT9MZp6fBa0XkxDnt9kNU8H3Qw |
| 314 | 7SJWZ69VXjBUzMlQViLuaWMgTnL+ZVyFZf9hTF7U/ef4HMLMAVNdiaGG+G+AjCV/ |
| 315 | MbzjS007Oe4qqBnCWaFPSnJX6uLApeTbqAxAeyCql56ULW5x6vDMNC3dwjvS/CEh |
| 316 | 11n8RkgFIQA0AhuKSIg3CbuartRsJnWOLwgLTzsrKYL4yRog1RJrtw== |
| 317 | -----END RSA PRIVATE KEY----- |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 318 | """ |
Jean-Paul Calderone | 55ec171 | 2009-05-13 14:14:30 -0400 | [diff] [blame] | 319 | |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 320 | encryptedPrivateKeyPEMPassphrase = b"foobar" |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 321 | |
Cory Benfield | 6492f7c | 2015-10-27 16:57:58 +0900 | [diff] [blame] | 322 | |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 323 | cleartextPublicKeyPEM = b"""-----BEGIN PUBLIC KEY----- |
Cory Benfield | 6492f7c | 2015-10-27 16:57:58 +0900 | [diff] [blame] | 324 | MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxszlc+b71LvlLS0ypt/l |
| 325 | gT/JzSVJtnEqw9WUNGeiChywX2mmQLHEt7KP0JikqUFZOtPclNY823Q4pErMTSWC |
| 326 | 90qlUxI47vNJbXGRfmO2q6Zfw6SE+E9iUb74xezbOJLjBuUIkQzEKEFV+8taiRV+ |
| 327 | ceg1v01yCT2+OjhQW3cxG42zxyRFmqesbQAUWgS3uhPrUQqYQUEiTmVhh4FBUKZ5 |
| 328 | XIneGUpX1S7mXRxTLH6YzRoGFqRoc9A0BBNcoXHTWnxV215k4TeHMFYE5RG0KYAS |
| 329 | 8Xk5iKICEXwnZreIt3jyygqoOKsKZMK/Zl2VhMGhJR6HXRpQCyASzEG7bgtROLhL |
| 330 | ywIDAQAB |
| 331 | -----END PUBLIC KEY----- |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 332 | """ |
Cory Benfield | 6492f7c | 2015-10-27 16:57:58 +0900 | [diff] [blame] | 333 | |
Jean-Paul Calderone | dc138fa | 2009-06-27 14:32:07 -0400 | [diff] [blame] | 334 | # Some PKCS#7 stuff. Generated with the openssl command line: |
| 335 | # |
| 336 | # openssl crl2pkcs7 -inform pem -outform pem -certfile s.pem -nocrl |
| 337 | # |
| 338 | # with a certificate and key (but the key should be irrelevant) in s.pem |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 339 | pkcs7Data = b"""\ |
Jean-Paul Calderone | dc138fa | 2009-06-27 14:32:07 -0400 | [diff] [blame] | 340 | -----BEGIN PKCS7----- |
| 341 | MIIDNwYJKoZIhvcNAQcCoIIDKDCCAyQCAQExADALBgkqhkiG9w0BBwGgggMKMIID |
| 342 | BjCCAm+gAwIBAgIBATANBgkqhkiG9w0BAQQFADB7MQswCQYDVQQGEwJTRzERMA8G |
| 343 | A1UEChMITTJDcnlwdG8xFDASBgNVBAsTC00yQ3J5cHRvIENBMSQwIgYDVQQDExtN |
| 344 | MkNyeXB0byBDZXJ0aWZpY2F0ZSBNYXN0ZXIxHTAbBgkqhkiG9w0BCQEWDm5ncHNA |
| 345 | cG9zdDEuY29tMB4XDTAwMDkxMDA5NTEzMFoXDTAyMDkxMDA5NTEzMFowUzELMAkG |
| 346 | A1UEBhMCU0cxETAPBgNVBAoTCE0yQ3J5cHRvMRIwEAYDVQQDEwlsb2NhbGhvc3Qx |
| 347 | HTAbBgkqhkiG9w0BCQEWDm5ncHNAcG9zdDEuY29tMFwwDQYJKoZIhvcNAQEBBQAD |
| 348 | SwAwSAJBAKy+e3dulvXzV7zoTZWc5TzgApr8DmeQHTYC8ydfzH7EECe4R1Xh5kwI |
| 349 | zOuuFfn178FBiS84gngaNcrFi0Z5fAkCAwEAAaOCAQQwggEAMAkGA1UdEwQCMAAw |
| 350 | LAYJYIZIAYb4QgENBB8WHU9wZW5TU0wgR2VuZXJhdGVkIENlcnRpZmljYXRlMB0G |
| 351 | A1UdDgQWBBTPhIKSvnsmYsBVNWjj0m3M2z0qVTCBpQYDVR0jBIGdMIGagBT7hyNp |
| 352 | 65w6kxXlxb8pUU/+7Sg4AaF/pH0wezELMAkGA1UEBhMCU0cxETAPBgNVBAoTCE0y |
| 353 | Q3J5cHRvMRQwEgYDVQQLEwtNMkNyeXB0byBDQTEkMCIGA1UEAxMbTTJDcnlwdG8g |
| 354 | Q2VydGlmaWNhdGUgTWFzdGVyMR0wGwYJKoZIhvcNAQkBFg5uZ3BzQHBvc3QxLmNv |
| 355 | bYIBADANBgkqhkiG9w0BAQQFAAOBgQA7/CqT6PoHycTdhEStWNZde7M/2Yc6BoJu |
| 356 | VwnW8YxGO8Sn6UJ4FeffZNcYZddSDKosw8LtPOeWoK3JINjAk5jiPQ2cww++7QGG |
| 357 | /g5NDjxFZNDJP1dGiLAxPW6JXwov4v0FmdzfLOZ01jDcgQQZqEpYlgpuI5JEWUQ9 |
| 358 | Ho4EzbYCOaEAMQA= |
| 359 | -----END PKCS7----- |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 360 | """ |
Jean-Paul Calderone | dc138fa | 2009-06-27 14:32:07 -0400 | [diff] [blame] | 361 | |
Alex Gaynor | 8fa1dd6 | 2014-08-14 09:57:51 -0700 | [diff] [blame] | 362 | pkcs7DataASN1 = base64.b64decode(b""" |
Alex Gaynor | 4b9c96a | 2014-08-14 09:51:48 -0700 | [diff] [blame] | 363 | MIIDNwYJKoZIhvcNAQcCoIIDKDCCAyQCAQExADALBgkqhkiG9w0BBwGgggMKMIID |
| 364 | BjCCAm+gAwIBAgIBATANBgkqhkiG9w0BAQQFADB7MQswCQYDVQQGEwJTRzERMA8G |
| 365 | A1UEChMITTJDcnlwdG8xFDASBgNVBAsTC00yQ3J5cHRvIENBMSQwIgYDVQQDExtN |
| 366 | MkNyeXB0byBDZXJ0aWZpY2F0ZSBNYXN0ZXIxHTAbBgkqhkiG9w0BCQEWDm5ncHNA |
| 367 | cG9zdDEuY29tMB4XDTAwMDkxMDA5NTEzMFoXDTAyMDkxMDA5NTEzMFowUzELMAkG |
| 368 | A1UEBhMCU0cxETAPBgNVBAoTCE0yQ3J5cHRvMRIwEAYDVQQDEwlsb2NhbGhvc3Qx |
| 369 | HTAbBgkqhkiG9w0BCQEWDm5ncHNAcG9zdDEuY29tMFwwDQYJKoZIhvcNAQEBBQAD |
| 370 | SwAwSAJBAKy+e3dulvXzV7zoTZWc5TzgApr8DmeQHTYC8ydfzH7EECe4R1Xh5kwI |
| 371 | zOuuFfn178FBiS84gngaNcrFi0Z5fAkCAwEAAaOCAQQwggEAMAkGA1UdEwQCMAAw |
| 372 | LAYJYIZIAYb4QgENBB8WHU9wZW5TU0wgR2VuZXJhdGVkIENlcnRpZmljYXRlMB0G |
| 373 | A1UdDgQWBBTPhIKSvnsmYsBVNWjj0m3M2z0qVTCBpQYDVR0jBIGdMIGagBT7hyNp |
| 374 | 65w6kxXlxb8pUU/+7Sg4AaF/pH0wezELMAkGA1UEBhMCU0cxETAPBgNVBAoTCE0y |
| 375 | Q3J5cHRvMRQwEgYDVQQLEwtNMkNyeXB0byBDQTEkMCIGA1UEAxMbTTJDcnlwdG8g |
| 376 | Q2VydGlmaWNhdGUgTWFzdGVyMR0wGwYJKoZIhvcNAQkBFg5uZ3BzQHBvc3QxLmNv |
| 377 | bYIBADANBgkqhkiG9w0BAQQFAAOBgQA7/CqT6PoHycTdhEStWNZde7M/2Yc6BoJu |
| 378 | VwnW8YxGO8Sn6UJ4FeffZNcYZddSDKosw8LtPOeWoK3JINjAk5jiPQ2cww++7QGG |
| 379 | /g5NDjxFZNDJP1dGiLAxPW6JXwov4v0FmdzfLOZ01jDcgQQZqEpYlgpuI5JEWUQ9 |
| 380 | Ho4EzbYCOaEAMQA= |
Alex Gaynor | 8fa1dd6 | 2014-08-14 09:57:51 -0700 | [diff] [blame] | 381 | """) |
Alex Gaynor | 4b9c96a | 2014-08-14 09:51:48 -0700 | [diff] [blame] | 382 | |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 383 | crlData = b"""\ |
Jean-Paul Calderone | 3eb5cc7 | 2010-01-30 15:24:40 -0500 | [diff] [blame] | 384 | -----BEGIN X509 CRL----- |
| 385 | MIIBWzCBxTANBgkqhkiG9w0BAQQFADBYMQswCQYDVQQGEwJVUzELMAkGA1UECBMC |
| 386 | SUwxEDAOBgNVBAcTB0NoaWNhZ28xEDAOBgNVBAoTB1Rlc3RpbmcxGDAWBgNVBAMT |
| 387 | D1Rlc3RpbmcgUm9vdCBDQRcNMDkwNzI2MDQzNDU2WhcNMTIwOTI3MDI0MTUyWjA8 |
| 388 | MBUCAgOrGA8yMDA5MDcyNTIzMzQ1NlowIwICAQAYDzIwMDkwNzI1MjMzNDU2WjAM |
| 389 | MAoGA1UdFQQDCgEEMA0GCSqGSIb3DQEBBAUAA4GBAEBt7xTs2htdD3d4ErrcGAw1 |
| 390 | 4dKcVnIWTutoI7xxen26Wwvh8VCsT7i/UeP+rBl9rC/kfjWjzQk3/zleaarGTpBT |
| 391 | 0yp4HXRFFoRhhSE/hP+eteaPXRgrsNRLHe9ZDd69wmh7J1wMDb0m81RG7kqcbsid |
| 392 | vrzEeLDRiiPl92dyyWmu |
| 393 | -----END X509 CRL----- |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 394 | """ |
Jean-Paul Calderone | e890db3 | 2010-08-22 16:55:15 -0400 | [diff] [blame] | 395 | |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 396 | crlDataUnsupportedExtension = b"""\ |
Paul Kehrer | 5e3dd4c | 2016-03-11 09:58:28 -0400 | [diff] [blame] | 397 | -----BEGIN X509 CRL----- |
| 398 | MIIGRzCCBS8CAQIwDQYJKoZIhvcNAQELBQAwJzELMAkGA1UEBhMCVVMxGDAWBgNV |
| 399 | BAMMD2NyeXB0b2dyYXBoeS5pbxgPMjAxNTAxMDEwMDAwMDBaGA8yMDE2MDEwMTAw |
| 400 | MDAwMFowggTOMBQCAQAYDzIwMTUwMTAxMDAwMDAwWjByAgEBGA8yMDE1MDEwMTAw |
| 401 | MDAwMFowXDAYBgNVHRgEERgPMjAxNTAxMDEwMDAwMDBaMDQGA1UdHQQtMCukKTAn |
| 402 | MQswCQYDVQQGEwJVUzEYMBYGA1UEAwwPY3J5cHRvZ3JhcGh5LmlvMAoGA1UdFQQD |
| 403 | CgEAMHICAQIYDzIwMTUwMTAxMDAwMDAwWjBcMBgGA1UdGAQRGA8yMDE1MDEwMTAw |
| 404 | MDAwMFowNAYDVR0dBC0wK6QpMCcxCzAJBgNVBAYTAlVTMRgwFgYDVQQDDA9jcnlw |
| 405 | dG9ncmFwaHkuaW8wCgYDVR0VBAMKAQEwcgIBAxgPMjAxNTAxMDEwMDAwMDBaMFww |
| 406 | GAYDVR0YBBEYDzIwMTUwMTAxMDAwMDAwWjA0BgNVHR0ELTArpCkwJzELMAkGA1UE |
| 407 | BhMCVVMxGDAWBgNVBAMMD2NyeXB0b2dyYXBoeS5pbzAKBgNVHRUEAwoBAjByAgEE |
| 408 | GA8yMDE1MDEwMTAwMDAwMFowXDAYBgNVHRgEERgPMjAxNTAxMDEwMDAwMDBaMDQG |
| 409 | A1UdHQQtMCukKTAnMQswCQYDVQQGEwJVUzEYMBYGA1UEAwwPY3J5cHRvZ3JhcGh5 |
| 410 | LmlvMAoGA1UdFQQDCgEDMHICAQUYDzIwMTUwMTAxMDAwMDAwWjBcMBgGA1UdGAQR |
| 411 | GA8yMDE1MDEwMTAwMDAwMFowNAYDVR0dBC0wK6QpMCcxCzAJBgNVBAYTAlVTMRgw |
| 412 | FgYDVQQDDA9jcnlwdG9ncmFwaHkuaW8wCgYDVR0VBAMKAQQwcgIBBhgPMjAxNTAx |
| 413 | MDEwMDAwMDBaMFwwGAYDVR0YBBEYDzIwMTUwMTAxMDAwMDAwWjA0BgNVHR0ELTAr |
| 414 | pCkwJzELMAkGA1UEBhMCVVMxGDAWBgNVBAMMD2NyeXB0b2dyYXBoeS5pbzAKBgNV |
| 415 | HRUEAwoBBTByAgEHGA8yMDE1MDEwMTAwMDAwMFowXDAYBgNVHRgEERgPMjAxNTAx |
| 416 | MDEwMDAwMDBaMDQGA1UdHQQtMCukKTAnMQswCQYDVQQGEwJVUzEYMBYGA1UEAwwP |
| 417 | Y3J5cHRvZ3JhcGh5LmlvMAoGA1UdFQQDCgEGMHICAQgYDzIwMTUwMTAxMDAwMDAw |
| 418 | WjBcMBgGA1UdGAQRGA8yMDE1MDEwMTAwMDAwMFowNAYDVR0dBC0wK6QpMCcxCzAJ |
| 419 | BgNVBAYTAlVTMRgwFgYDVQQDDA9jcnlwdG9ncmFwaHkuaW8wCgYDVR0VBAMKAQgw |
| 420 | cgIBCRgPMjAxNTAxMDEwMDAwMDBaMFwwGAYDVR0YBBEYDzIwMTUwMTAxMDAwMDAw |
| 421 | WjA0BgNVHR0ELTArpCkwJzELMAkGA1UEBhMCVVMxGDAWBgNVBAMMD2NyeXB0b2dy |
| 422 | YXBoeS5pbzAKBgNVHRUEAwoBCTByAgEKGA8yMDE1MDEwMTAwMDAwMFowXDAYBgNV |
| 423 | HRgEERgPMjAxNTAxMDEwMDAwMDBaMDQGA1UdHQQtMCukKTAnMQswCQYDVQQGEwJV |
| 424 | UzEYMBYGA1UEAwwPY3J5cHRvZ3JhcGh5LmlvMAoGA1UdFQQDCgEKMC4CAQsYDzIw |
| 425 | MTUwMTAxMDAwMDAwWjAYMAoGA1UdFQQDCgEBMAoGAyoDBAQDCgEAMA0GCSqGSIb3 |
| 426 | DQEBCwUAA4IBAQBTaloHlPaCZzYee8LxkWej5meiqxQVNWFoVdjesroa+f1FRrH+ |
| 427 | drRU60Nq97KCKf7f9GNN/J3ZIlQmYhmuDqh12f+XLpotoj1ZRfBz2hjFCkJlv+2c |
| 428 | oWWGNHgA70ndFoVtcmX088SYpX8E3ARATivS4q2h9WlwV6rO93mhg3HGIe3JpcK4 |
| 429 | 7BcW6Poi/ut/zsDOkVbI00SqaujRpdmdCTht82MH3ztjyDkI9KYaD/YEweKSrWOz |
| 430 | SdEILd164bfBeLuplVI+xpmTEMVNpXBlSXl7+xIw9Vk7p7Q1Pa3k/SvhOldYCm6y |
| 431 | C1xAg/AAq6w78yzYt18j5Mj0s6eeHi1YpHKw |
| 432 | -----END X509 CRL----- |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 433 | """ |
Paul Kehrer | 5e3dd4c | 2016-03-11 09:58:28 -0400 | [diff] [blame] | 434 | |
Jean-Paul Calderone | 55ec171 | 2009-05-13 14:14:30 -0400 | [diff] [blame] | 435 | |
| 436 | # A broken RSA private key which can be used to test the error path through |
| 437 | # PKey.check. |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 438 | inconsistentPrivateKeyPEM = b"""-----BEGIN RSA PRIVATE KEY----- |
Jean-Paul Calderone | 55ec171 | 2009-05-13 14:14:30 -0400 | [diff] [blame] | 439 | MIIBPAIBAAJBAKy+e3dulvXzV7zoTZWc5TzgApr8DmeQHTYC8ydfzH7EECe4R1Xh |
| 440 | 5kwIzOuuFfn178FBiS84gngaNcrFi0Z5fAkCAwEaAQJBAIqm/bz4NA1H++Vx5Ewx |
| 441 | OcKp3w19QSaZAwlGRtsUxrP7436QjnREM3Bm8ygU11BjkPVmtrKm6AayQfCHqJoT |
| 442 | zIECIQDW0BoMoL0HOYM/mrTLhaykYAVqgIeJsPjvkEhTFXWBuQIhAM3deFAvWNu4 |
| 443 | nklUQ37XsCT2c9tmNt1LAT+slG2JOTTRAiAuXDtC/m3NYVwyHfFm+zKHRzHkClk2 |
| 444 | HjubeEgjpj32AQIhAJqMGTaZVOwevTXvvHwNeH+vRWsAYU/gbx+OQB+7VOcBAiEA |
| 445 | oolb6NMg/R3enNPvS1O4UU1H8wpaF77L4yiSWlE0p4w= |
| 446 | -----END RSA PRIVATE KEY----- |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 447 | """ |
Jean-Paul Calderone | 55ec171 | 2009-05-13 14:14:30 -0400 | [diff] [blame] | 448 | |
Jean-Paul Calderone | ff83cdd | 2013-08-12 18:05:51 -0400 | [diff] [blame] | 449 | # certificate with NULL bytes in subjectAltName and common name |
| 450 | |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 451 | nulbyteSubjectAltNamePEM = b"""-----BEGIN CERTIFICATE----- |
Jean-Paul Calderone | ff83cdd | 2013-08-12 18:05:51 -0400 | [diff] [blame] | 452 | MIIE2DCCA8CgAwIBAgIBADANBgkqhkiG9w0BAQUFADCBxTELMAkGA1UEBhMCVVMx |
| 453 | DzANBgNVBAgMBk9yZWdvbjESMBAGA1UEBwwJQmVhdmVydG9uMSMwIQYDVQQKDBpQ |
| 454 | eXRob24gU29mdHdhcmUgRm91bmRhdGlvbjEgMB4GA1UECwwXUHl0aG9uIENvcmUg |
| 455 | RGV2ZWxvcG1lbnQxJDAiBgNVBAMMG251bGwucHl0aG9uLm9yZwBleGFtcGxlLm9y |
| 456 | ZzEkMCIGCSqGSIb3DQEJARYVcHl0aG9uLWRldkBweXRob24ub3JnMB4XDTEzMDgw |
| 457 | NzEzMTE1MloXDTEzMDgwNzEzMTI1MlowgcUxCzAJBgNVBAYTAlVTMQ8wDQYDVQQI |
| 458 | DAZPcmVnb24xEjAQBgNVBAcMCUJlYXZlcnRvbjEjMCEGA1UECgwaUHl0aG9uIFNv |
| 459 | ZnR3YXJlIEZvdW5kYXRpb24xIDAeBgNVBAsMF1B5dGhvbiBDb3JlIERldmVsb3Bt |
| 460 | ZW50MSQwIgYDVQQDDBtudWxsLnB5dGhvbi5vcmcAZXhhbXBsZS5vcmcxJDAiBgkq |
| 461 | hkiG9w0BCQEWFXB5dGhvbi1kZXZAcHl0aG9uLm9yZzCCASIwDQYJKoZIhvcNAQEB |
| 462 | BQADggEPADCCAQoCggEBALXq7cn7Rn1vO3aA3TrzA5QLp6bb7B3f/yN0CJ2XFj+j |
| 463 | pHs+Gw6WWSUDpybiiKnPec33BFawq3kyblnBMjBU61ioy5HwQqVkJ8vUVjGIUq3P |
| 464 | vX/wBmQfzCe4o4uM89gpHyUL9UYGG8oCRa17dgqcv7u5rg0Wq2B1rgY+nHwx3JIv |
| 465 | KRrgSwyRkGzpN8WQ1yrXlxWjgI9de0mPVDDUlywcWze1q2kwaEPTM3hLAmD1PESA |
| 466 | oY/n8A/RXoeeRs9i/Pm/DGUS8ZPINXk/yOzsR/XvvkTVroIeLZqfmFpnZeF0cHzL |
| 467 | 08LODkVJJ9zjLdT7SA4vnne4FEbAxDbKAq5qkYzaL4UCAwEAAaOB0DCBzTAMBgNV |
| 468 | HRMBAf8EAjAAMB0GA1UdDgQWBBSIWlXAUv9hzVKjNQ/qWpwkOCL3XDALBgNVHQ8E |
| 469 | BAMCBeAwgZAGA1UdEQSBiDCBhYIeYWx0bnVsbC5weXRob24ub3JnAGV4YW1wbGUu |
| 470 | Y29tgSBudWxsQHB5dGhvbi5vcmcAdXNlckBleGFtcGxlLm9yZ4YpaHR0cDovL251 |
| 471 | bGwucHl0aG9uLm9yZwBodHRwOi8vZXhhbXBsZS5vcmeHBMAAAgGHECABDbgAAAAA |
| 472 | AAAAAAAAAAEwDQYJKoZIhvcNAQEFBQADggEBAKxPRe99SaghcI6IWT7UNkJw9aO9 |
| 473 | i9eo0Fj2MUqxpKbdb9noRDy2CnHWf7EIYZ1gznXPdwzSN4YCjV5d+Q9xtBaowT0j |
| 474 | HPERs1ZuytCNNJTmhyqZ8q6uzMLoht4IqH/FBfpvgaeC5tBTnTT0rD5A/olXeimk |
| 475 | kX4LxlEx5RAvpGB2zZVRGr6LobD9rVK91xuHYNIxxxfEGE8tCCWjp0+3ksri9SXx |
| 476 | VHWBnbM9YaL32u3hxm8sYB/Yb8WSBavJCWJJqRStVRHM1koZlJmXNx2BX4vPo6iW |
| 477 | RFEIPQsFZRLrtnCAiEhyT8bC2s/Njlu6ly9gtJZWSV46Q3ZjBL4q9sHKqZQ= |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 478 | -----END CERTIFICATE-----""" |
Jean-Paul Calderone | ff83cdd | 2013-08-12 18:05:51 -0400 | [diff] [blame] | 479 | |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 480 | large_key_pem = b"""-----BEGIN RSA PRIVATE KEY----- |
Colleen Murphy | e09399b | 2016-03-01 17:40:49 -0800 | [diff] [blame] | 481 | MIIJYgIBAAKCAg4AtRua8eIeevRfsj+fkcHr1vmse7Kgb+oX1ssJAvCb1R7JQMnH |
| 482 | hNDjDP6b3vEkZuPUzlDHymP+cNkXvvi4wJ4miVbO3+SeU4Sh+jmsHeHzGIXat9xW |
| 483 | 9PFtuPM5FQq8zvkY8aDeRYmYwN9JKu4/neMBCBqostYlTEWg+bSytO/qWnyHTHKh |
| 484 | g0GfaDdqUQPsGQw+J0MgaYIjQOCVASHAPlzbDQLCtuOb587rwTLkZA2GwoHB/LyJ |
| 485 | BwT0HHgBaiObE12Vs6wi2en0Uu11CiwEuK1KIBcZ2XbE6eApaZa6VH9ysEmUxPt7 |
| 486 | TqyZ4E2oMIYaLPNRxuvozdwTlj1svI1k1FrkaXGc5MTjbgigPMKjIb0T7b/4GNzt |
| 487 | DhP1LvAeUMnrEi3hJJrcJPXHPqS8/RiytR9xQQW6Sdh4LaA3f9MQm3WSevWage3G |
| 488 | P8YcCLssOVKsArDjuA52NF5LmYuAeUzXprm4ITDi2oO+0iFBpFW6VPEK4A9vO0Yk |
| 489 | M/6Wt6tG8zyWhaSH1zFUTwfQ9Yvjyt5w1lrUaAJuoTpwbMVZaDJaEhjOaXU0dyPQ |
| 490 | jOsePDOQcU6dkeTWsQ3LsHPEEug/X6819TLG5mb3V7bvV9nPFBfTJSCEG794kr90 |
| 491 | XgZfIN71FrdByxLerlbuJI21pPs/nZi9SXi9jAWeiS45/azUxMsyYgJArui+gjq7 |
| 492 | sV1pWiBm6/orAgMBAAECggINQp5L6Yu+oIXBqcSjgq8tfF9M5hd30pLuf/EheHZf |
| 493 | LA7uAqn2fVGFI2OInIJhXIOT5OxsAXO0xXfltzawZxIFpOFMqajj4F7aYjvSpw9V |
| 494 | J4EdSiJ/zgv8y1qUdbwEZbHVThRZjoSlrtSzilonBoHZAE0mHtqMz7iRFSk1zz6t |
| 495 | GunRrvo/lROPentf3TsvHquVNUYI5yaapyO1S7xJhecMIIYSb8nbsHI54FBDGNas |
| 496 | 6mFmpPwI/47/6HTwOEWupnn3NicsjrHzUInOUpaMig4cRR+aP5bjqg/ty8xI8AoN |
| 497 | evEmCytiWTc+Rvbp1ieN+1jpjN18PjUk80/W7qioHUDt4ieLic8uxWH2VD9SCEnX |
| 498 | Mpi9tA/FqoZ+2A/3m1OfrY6jiZVE2g+asi9lCK7QVWL39eK82H4rPvtp0/dyo1/i |
| 499 | ZZz68TXg+m8IgEZcp88hngbkuoTTzpGE73QuPKhGA1uMIimDdqPPB5WP76q+03Oi |
| 500 | IRR5DfZnqPERed49by0enJ7tKa/gFPZizOV8ALKr0Dp+vfAkxGDLPLBLd2A3//tw |
| 501 | xg0Q/wltihHSBujv4nYlDXdc5oYyMYZ+Lhc/VuOghHfBq3tgEQ1ECM/ofqXEIdy7 |
| 502 | nVcpZn3Eeq8Jl5CrqxE1ee3NxlzsJHn99yGQpr7mOhW/psJF3XNz80Meg3L4m1T8 |
| 503 | sMBK0GbaassuJhdzb5whAoIBBw48sx1b1WR4XxQc5O/HjHva+l16i2pjUnOUTcDF |
| 504 | RWmSbIhBm2QQ2rVhO8+fak0tkl6ZnMWW4i0U/X5LOEBbC7+IS8bO3j3Revi+Vw5x |
| 505 | j96LMlIe9XEub5i/saEWgiz7maCvfzLFU08e1OpT4qPDpP293V400ubA6R7WQTCv |
| 506 | pBkskGwHeu0l/TuKkVqBFFUTu7KEbps8Gjg7MkJaFriAOv1zis/umK8pVS3ZAM6e |
| 507 | 8w5jfpRccn8Xzta2fRwTB5kCmfxdDsY0oYGxPLRAbW72bORoLGuyyPp/ojeGwoik |
| 508 | JX9RttErc6FjyZtks370Pa8UL5QskyhMbDhrZW2jFD+RXYM1BrvmZRjbAoIBBwy4 |
| 509 | iFJpuDfytJfz1MWtaL5DqEL/kmiZYAXl6hifNhGu5GAipVIIGsDqEYW4i+VC15aa |
| 510 | 7kOCwz/I5zsB3vSDW96IRs4wXtqEZSibc2W/bqfVi+xcvPPl1ZhQ2EAwa4D/x035 |
| 511 | kyf20ffWOU+1yf2cnijzqs3IzlveUm+meLw5s3Rc+iG7DPWWeCoe1hVwANI1euNc |
| 512 | pqKwKY905yFyjOje2OgiEU2kS4YME4zGeBys8yo7E42hNnN2EPK6xkkUqzdudLLQ |
| 513 | 8OUlKRTc8AbIf3XG1rpA4VUpTv3hhxGGwCRy6If8zgZQsNYchgNztRGk72Gcb8Dm |
| 514 | vFSEN3ZtwxU64G3YZzntdcr2WPzxAoIBBw30g6Fgdb/gmVnOpL0//T0ePNDKIMPs |
| 515 | jVJLaRduhoZgB1Bb9qPUPX0SzRzLZtg1tkZSDjBDoHmOHJfhxUaXt+FLCPPbrE4t |
| 516 | +nq9n/nBaMM779w9ClqhqLOyGrwKoxjSmhi+TVEHyIxCbXMvPHVHfX9WzxjbcGrN |
| 517 | ZvRaEVZWo+QlIX8yqdSwqxLk1WtAIRzvlcj7NKum8xBxPed6BNFep/PtgIAmoLT5 |
| 518 | L8wb7EWb2iUdc2KbZ4OaY51lDScqpATgXu3WjXfM+Q52G0mX6Wyd0cjlL711Zrjb |
| 519 | yLbiueZT94lgIHHRRKtKc8CEqcjkQV5OzABS3P/gQSfgZXBdLKjOpTnKDUq7IBeH |
| 520 | AoIBBweAOEIAPLQg1QRUrr3xRrYKRwlakgZDii9wJt1l5AgBTICzbTA1vzDJ1JM5 |
| 521 | AqSpCV6w9JWyYVcXK+HLdKBRZLaPPNEQDJ5lOxD6uMziWGl2rg8tj+1xNMWfxiPz |
| 522 | aTCjoe4EoBUMoTq2gwzRcM2usEQNikXVhnj9Wzaivsaeb4bJ3GRPW5DkrO6JSEtT |
| 523 | w+gvyMqQM2Hy5k7E7BT46sXVwaj/jZxuqGnebRixXtnp0WixdRIqYWUr1UqLf6hQ |
| 524 | G7WP2BgoxCMaCmNW8+HMD/xuxucEotoIhZ+GgJKBFoNnjl3BX+qxYdSe9RbL/5Tr |
| 525 | 4It6Jxtj8uETJXEbv9Cg6v1agWPS9YY8RLTBAoIBBwrU2AsAUts6h1LgGLKK3UWZ |
| 526 | oLH5E+4o+7HqSGRcRodVeN9NBXIYdHHOLeEG6YNGJiJ3bFP5ZQEu9iDsyoFVKJ9O |
| 527 | Mw/y6dKZuxOCZ+X8FopSROg3yWfdOpAm6cnQZp3WqLNX4n/Q6WvKojfyEiPphjwT |
| 528 | 0ymrUJELXLWJmjUyPoAk6HgC0Gs28ZnEXbyhx7CSbZNFyCU/PNUDZwto3GisIPD3 |
| 529 | le7YjqHugezmjMGlA0sDw5aCXjfbl74vowRFYMO6e3ItApfSRgNV86CDoX74WI/5 |
| 530 | AYU/QVM4wGt8XGT2KwDFJaxYGKsGDMWmXY04dS+WPuetCbouWUusyFwRb9SzFave |
| 531 | vYeU7Ab/ |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 532 | -----END RSA PRIVATE KEY-----""" |
Colleen Murphy | e09399b | 2016-03-01 17:40:49 -0800 | [diff] [blame] | 533 | |
Paul Kehrer | 72d968b | 2016-07-29 15:31:04 +0800 | [diff] [blame] | 534 | ec_private_key_pem = b"""-----BEGIN PRIVATE KEY----- |
| 535 | MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgYirTZSx+5O8Y6tlG |
| 536 | cka6W6btJiocdrdolfcukSoTEk+hRANCAAQkvPNu7Pa1GcsWU4v7ptNfqCJVq8Cx |
| 537 | zo0MUVPQgwJ3aJtNM1QMOQUayCrRwfklg+D/rFSUwEUqtZh7fJDiFqz3 |
| 538 | -----END PRIVATE KEY----- |
| 539 | """ |
| 540 | |
Paul Kehrer | 59d2625 | 2017-07-20 10:45:54 +0200 | [diff] [blame] | 541 | ec_root_key_pem = b"""-----BEGIN EC PRIVATE KEY----- |
| 542 | MIGlAgEBBDEAz/HOBFPYLB0jLWeTpJn4Yc4m/C4mdWymVHBjOmnwiPHKT326iYN/ |
| 543 | ZhmSs+RM94RsoAcGBSuBBAAioWQDYgAEwE5vDdla/nLpWAPAQ0yFGqwLuw4BcN2r |
| 544 | U+sKab5EAEHzLeceRa8ffncYdCXNoVsBcdob1y66CFZMEWLetPTmGapyWkBAs6/L |
| 545 | 8kUlkU9OsE+7IVo4QQJkgV5gM+Dim1XE |
| 546 | -----END EC PRIVATE KEY----- |
| 547 | """ |
| 548 | |
| 549 | ec_root_cert_pem = b"""-----BEGIN CERTIFICATE----- |
| 550 | MIICLTCCAbKgAwIBAgIMWW/hwTl6ufz6/WkCMAoGCCqGSM49BAMDMFgxGDAWBgNV |
| 551 | BAMTD1Rlc3RpbmcgUm9vdCBDQTEQMA4GA1UEChMHVGVzdGluZzEQMA4GA1UEBxMH |
| 552 | Q2hpY2FnbzELMAkGA1UECBMCSUwxCzAJBgNVBAYTAlVTMCAXDTE3MDcxOTIyNDgz |
| 553 | M1oYDzk5OTkxMjMxMjM1OTU5WjBYMRgwFgYDVQQDEw9UZXN0aW5nIFJvb3QgQ0Ex |
| 554 | EDAOBgNVBAoTB1Rlc3RpbmcxEDAOBgNVBAcTB0NoaWNhZ28xCzAJBgNVBAgTAklM |
| 555 | MQswCQYDVQQGEwJVUzB2MBAGByqGSM49AgEGBSuBBAAiA2IABMBObw3ZWv5y6VgD |
| 556 | wENMhRqsC7sOAXDdq1PrCmm+RABB8y3nHkWvH353GHQlzaFbAXHaG9cuughWTBFi |
| 557 | 3rT05hmqclpAQLOvy/JFJZFPTrBPuyFaOEECZIFeYDPg4ptVxKNDMEEwDwYDVR0T |
| 558 | AQH/BAUwAwEB/zAPBgNVHQ8BAf8EBQMDBwQAMB0GA1UdDgQWBBSoTrF0H2m8RDzB |
| 559 | MnY2KReEPfz7ZjAKBggqhkjOPQQDAwNpADBmAjEA3+G1oVCxGjYX4iUN93QYcNHe |
| 560 | e3fJQJwX9+KsHRut6qNZDUbvRbtO1YIAwB4UJZjwAjEAtXCPURS5A4McZHnSwgTi |
| 561 | Td8GMrwKz0557OxxtKN6uVVy4ACFMqEw0zN/KJI1vxc9 |
| 562 | -----END CERTIFICATE-----""" |
| 563 | |
Mrmaxmeier | 8cd3b17 | 2020-03-11 22:03:59 +0100 | [diff] [blame] | 564 | rsa_p_not_prime_pem = """ |
| 565 | -----BEGIN RSA PRIVATE KEY----- |
| 566 | MBsCAQACAS0CAQcCAQACAQ8CAQMCAQACAQACAQA= |
| 567 | -----END RSA PRIVATE KEY----- |
| 568 | """ |
| 569 | |
Jean-Paul Calderone | 55ec171 | 2009-05-13 14:14:30 -0400 | [diff] [blame] | 570 | |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 571 | @pytest.fixture |
| 572 | def x509_data(): |
Jean-Paul Calderone | f0179c7 | 2009-07-17 15:48:22 -0400 | [diff] [blame] | 573 | """ |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 574 | Create a new private key and start a certificate request (for a test |
| 575 | to finish in one way or another). |
Jean-Paul Calderone | f0179c7 | 2009-07-17 15:48:22 -0400 | [diff] [blame] | 576 | """ |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 577 | # Basic setup stuff to generate a certificate |
| 578 | pkey = PKey() |
Alex Gaynor | 6e9f976 | 2018-05-12 07:44:37 -0400 | [diff] [blame] | 579 | pkey.generate_key(TYPE_RSA, 512) |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 580 | req = X509Req() |
| 581 | req.set_pubkey(pkey) |
| 582 | # Authority good you have. |
| 583 | req.get_subject().commonName = "Yoda root CA" |
| 584 | x509 = X509() |
| 585 | subject = x509.get_subject() |
| 586 | subject.commonName = req.get_subject().commonName |
| 587 | x509.set_issuer(subject) |
| 588 | x509.set_pubkey(pkey) |
| 589 | now = datetime.now() |
| 590 | expire = datetime.now() + timedelta(days=100) |
| 591 | x509.set_notBefore(now.strftime("%Y%m%d%H%M%SZ").encode()) |
| 592 | x509.set_notAfter(expire.strftime("%Y%m%d%H%M%SZ").encode()) |
| 593 | yield pkey, x509 |
Jean-Paul Calderone | f0179c7 | 2009-07-17 15:48:22 -0400 | [diff] [blame] | 594 | |
Jean-Paul Calderone | f0179c7 | 2009-07-17 15:48:22 -0400 | [diff] [blame] | 595 | |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 596 | class TestX509Ext(object): |
| 597 | """ |
| 598 | Tests for `OpenSSL.crypto.X509Extension`. |
| 599 | """ |
Jean-Paul Calderone | ef9a3dc | 2013-03-02 16:33:32 -0800 | [diff] [blame] | 600 | |
Jean-Paul Calderone | fe1b9bd | 2010-08-03 18:00:21 -0400 | [diff] [blame] | 601 | def test_str(self): |
| 602 | """ |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 603 | The string representation of `X509Extension` instances as |
| 604 | returned by `str` includes stuff. |
Jean-Paul Calderone | fe1b9bd | 2010-08-03 18:00:21 -0400 | [diff] [blame] | 605 | """ |
| 606 | # This isn't necessarily the best string representation. Perhaps it |
| 607 | # will be changed/improved in the future. |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 608 | assert ( |
| 609 | str(X509Extension(b'basicConstraints', True, b'CA:false')) == |
| 610 | 'CA:FALSE' |
| 611 | ) |
Jean-Paul Calderone | fe1b9bd | 2010-08-03 18:00:21 -0400 | [diff] [blame] | 612 | |
Jean-Paul Calderone | 6864905 | 2009-07-17 21:14:27 -0400 | [diff] [blame] | 613 | def test_type(self): |
| 614 | """ |
Alex Gaynor | 01f90a1 | 2019-02-07 09:14:48 -0500 | [diff] [blame] | 615 | `X509Extension` can be used to create instances of that type. |
Jean-Paul Calderone | 6864905 | 2009-07-17 21:14:27 -0400 | [diff] [blame] | 616 | """ |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 617 | assert is_consistent_type( |
Jean-Paul Calderone | 40dd099 | 2010-08-22 17:52:07 -0400 | [diff] [blame] | 618 | X509Extension, |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 619 | 'X509Extension', b'basicConstraints', True, b'CA:true') |
Jean-Paul Calderone | 6864905 | 2009-07-17 21:14:27 -0400 | [diff] [blame] | 620 | |
Jean-Paul Calderone | e7db4b4 | 2008-12-31 13:39:24 -0500 | [diff] [blame] | 621 | def test_construction(self): |
| 622 | """ |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 623 | `X509Extension` accepts an extension type name, a critical flag, |
Alex Gaynor | 01f90a1 | 2019-02-07 09:14:48 -0500 | [diff] [blame] | 624 | and an extension value and returns an `X509Extension` instance. |
Jean-Paul Calderone | e7db4b4 | 2008-12-31 13:39:24 -0500 | [diff] [blame] | 625 | """ |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 626 | basic = X509Extension(b'basicConstraints', True, b'CA:true') |
Alex Gaynor | 01f90a1 | 2019-02-07 09:14:48 -0500 | [diff] [blame] | 627 | assert isinstance(basic, X509Extension) |
Jean-Paul Calderone | e7db4b4 | 2008-12-31 13:39:24 -0500 | [diff] [blame] | 628 | |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 629 | comment = X509Extension(b'nsComment', False, b'pyOpenSSL unit test') |
Alex Gaynor | 01f90a1 | 2019-02-07 09:14:48 -0500 | [diff] [blame] | 630 | assert isinstance(comment, X509Extension) |
Jean-Paul Calderone | e7db4b4 | 2008-12-31 13:39:24 -0500 | [diff] [blame] | 631 | |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 632 | @pytest.mark.parametrize('type_name, critical, value', [ |
| 633 | (b'thisIsMadeUp', False, b'hi'), |
| 634 | (b'basicConstraints', False, b'blah blah'), |
Jean-Paul Calderone | 391585f | 2008-12-31 14:36:31 -0500 | [diff] [blame] | 635 | |
Jean-Paul Calderone | 2ee1e7c | 2008-12-31 14:58:38 -0500 | [diff] [blame] | 636 | # Exercise a weird one (an extension which uses the r2i method). This |
| 637 | # exercises the codepath that requires a non-NULL ctx to be passed to |
| 638 | # X509V3_EXT_nconf. It can't work now because we provide no |
| 639 | # configuration database. It might be made to work in the future. |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 640 | (b'proxyCertInfo', True, |
| 641 | b'language:id-ppl-anyLanguage,pathlen:1,policy:text:AB') |
| 642 | ]) |
| 643 | def test_invalid_extension(self, type_name, critical, value): |
Jean-Paul Calderone | e7db4b4 | 2008-12-31 13:39:24 -0500 | [diff] [blame] | 644 | """ |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 645 | `X509Extension` raises something if it is passed a bad |
| 646 | extension name or value. |
| 647 | """ |
| 648 | with pytest.raises(Error): |
| 649 | X509Extension(type_name, critical, value) |
| 650 | |
| 651 | @pytest.mark.parametrize('critical_flag', [True, False]) |
| 652 | def test_get_critical(self, critical_flag): |
| 653 | """ |
| 654 | `X509ExtensionType.get_critical` returns the value of the |
Jean-Paul Calderone | e7db4b4 | 2008-12-31 13:39:24 -0500 | [diff] [blame] | 655 | extension's critical flag. |
| 656 | """ |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 657 | ext = X509Extension(b'basicConstraints', critical_flag, b'CA:true') |
| 658 | assert ext.get_critical() == critical_flag |
Jean-Paul Calderone | e7db4b4 | 2008-12-31 13:39:24 -0500 | [diff] [blame] | 659 | |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 660 | @pytest.mark.parametrize('short_name, value', [ |
| 661 | (b'basicConstraints', b'CA:true'), |
| 662 | (b'nsComment', b'foo bar'), |
| 663 | ]) |
| 664 | def test_get_short_name(self, short_name, value): |
Jean-Paul Calderone | f8c5fab | 2008-12-31 15:53:48 -0500 | [diff] [blame] | 665 | """ |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 666 | `X509ExtensionType.get_short_name` returns a string giving the |
Alex Gaynor | 3128750 | 2015-09-05 16:11:27 -0400 | [diff] [blame] | 667 | short type name of the extension. |
Jean-Paul Calderone | f8c5fab | 2008-12-31 15:53:48 -0500 | [diff] [blame] | 668 | """ |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 669 | ext = X509Extension(short_name, True, value) |
| 670 | assert ext.get_short_name() == short_name |
Jean-Paul Calderone | f8c5fab | 2008-12-31 15:53:48 -0500 | [diff] [blame] | 671 | |
Jean-Paul Calderone | 5a9e461 | 2011-04-01 18:27:45 -0400 | [diff] [blame] | 672 | def test_get_data(self): |
| 673 | """ |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 674 | `X509Extension.get_data` returns a string giving the data of |
Alex Gaynor | 3128750 | 2015-09-05 16:11:27 -0400 | [diff] [blame] | 675 | the extension. |
Jean-Paul Calderone | 5a9e461 | 2011-04-01 18:27:45 -0400 | [diff] [blame] | 676 | """ |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 677 | ext = X509Extension(b'basicConstraints', True, b'CA:true') |
Jean-Paul Calderone | 5a9e461 | 2011-04-01 18:27:45 -0400 | [diff] [blame] | 678 | # Expect to get back the DER encoded form of CA:true. |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 679 | assert ext.get_data() == b'0\x03\x01\x01\xff' |
Jean-Paul Calderone | 5a9e461 | 2011-04-01 18:27:45 -0400 | [diff] [blame] | 680 | |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 681 | def test_unused_subject(self, x509_data): |
Jean-Paul Calderone | 5a9e461 | 2011-04-01 18:27:45 -0400 | [diff] [blame] | 682 | """ |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 683 | The `subject` parameter to `X509Extension` may be provided for an |
| 684 | extension which does not use it and is ignored in this case. |
Jean-Paul Calderone | 5a9e461 | 2011-04-01 18:27:45 -0400 | [diff] [blame] | 685 | """ |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 686 | pkey, x509 = x509_data |
Jean-Paul Calderone | 40dd099 | 2010-08-22 17:52:07 -0400 | [diff] [blame] | 687 | ext1 = X509Extension( |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 688 | b'basicConstraints', False, b'CA:TRUE', subject=x509) |
| 689 | x509.add_extensions([ext1]) |
| 690 | x509.sign(pkey, 'sha1') |
Jean-Paul Calderone | f0179c7 | 2009-07-17 15:48:22 -0400 | [diff] [blame] | 691 | # This is a little lame. Can we think of a better way? |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 692 | text = dump_certificate(FILETYPE_TEXT, x509) |
| 693 | assert b'X509v3 Basic Constraints:' in text |
| 694 | assert b'CA:TRUE' in text |
Jean-Paul Calderone | f0179c7 | 2009-07-17 15:48:22 -0400 | [diff] [blame] | 695 | |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 696 | def test_subject(self, x509_data): |
Jean-Paul Calderone | f0179c7 | 2009-07-17 15:48:22 -0400 | [diff] [blame] | 697 | """ |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 698 | If an extension requires a subject, the `subject` parameter to |
| 699 | `X509Extension` provides its value. |
Jean-Paul Calderone | f0179c7 | 2009-07-17 15:48:22 -0400 | [diff] [blame] | 700 | """ |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 701 | pkey, x509 = x509_data |
Jean-Paul Calderone | 40dd099 | 2010-08-22 17:52:07 -0400 | [diff] [blame] | 702 | ext3 = X509Extension( |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 703 | b'subjectKeyIdentifier', False, b'hash', subject=x509) |
| 704 | x509.add_extensions([ext3]) |
| 705 | x509.sign(pkey, 'sha1') |
| 706 | text = dump_certificate(FILETYPE_TEXT, x509) |
| 707 | assert b'X509v3 Subject Key Identifier:' in text |
Jean-Paul Calderone | f0179c7 | 2009-07-17 15:48:22 -0400 | [diff] [blame] | 708 | |
Jean-Paul Calderone | f0179c7 | 2009-07-17 15:48:22 -0400 | [diff] [blame] | 709 | def test_missing_subject(self): |
| 710 | """ |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 711 | If an extension requires a subject and the `subject` parameter |
Alex Gaynor | 3128750 | 2015-09-05 16:11:27 -0400 | [diff] [blame] | 712 | is given no value, something happens. |
Jean-Paul Calderone | f0179c7 | 2009-07-17 15:48:22 -0400 | [diff] [blame] | 713 | """ |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 714 | with pytest.raises(Error): |
| 715 | X509Extension(b'subjectKeyIdentifier', False, b'hash') |
Jean-Paul Calderone | f0179c7 | 2009-07-17 15:48:22 -0400 | [diff] [blame] | 716 | |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 717 | @pytest.mark.parametrize('bad_obj', [ |
| 718 | True, |
| 719 | object(), |
| 720 | "hello", |
| 721 | [], |
| 722 | ]) |
| 723 | def test_invalid_subject(self, bad_obj): |
Jean-Paul Calderone | f0179c7 | 2009-07-17 15:48:22 -0400 | [diff] [blame] | 724 | """ |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 725 | If the `subject` parameter is given a value which is not an |
| 726 | `X509` instance, `TypeError` is raised. |
Jean-Paul Calderone | f0179c7 | 2009-07-17 15:48:22 -0400 | [diff] [blame] | 727 | """ |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 728 | with pytest.raises(TypeError): |
| 729 | X509Extension( |
| 730 | 'basicConstraints', False, 'CA:TRUE', subject=bad_obj) |
Jean-Paul Calderone | f0179c7 | 2009-07-17 15:48:22 -0400 | [diff] [blame] | 731 | |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 732 | def test_unused_issuer(self, x509_data): |
Jean-Paul Calderone | f0179c7 | 2009-07-17 15:48:22 -0400 | [diff] [blame] | 733 | """ |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 734 | The `issuer` parameter to `X509Extension` may be provided for an |
| 735 | extension which does not use it and is ignored in this case. |
Jean-Paul Calderone | f0179c7 | 2009-07-17 15:48:22 -0400 | [diff] [blame] | 736 | """ |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 737 | pkey, x509 = x509_data |
Jean-Paul Calderone | 40dd099 | 2010-08-22 17:52:07 -0400 | [diff] [blame] | 738 | ext1 = X509Extension( |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 739 | b'basicConstraints', False, b'CA:TRUE', issuer=x509) |
| 740 | x509.add_extensions([ext1]) |
| 741 | x509.sign(pkey, 'sha1') |
| 742 | text = dump_certificate(FILETYPE_TEXT, x509) |
| 743 | assert b'X509v3 Basic Constraints:' in text |
| 744 | assert b'CA:TRUE' in text |
Jean-Paul Calderone | f0179c7 | 2009-07-17 15:48:22 -0400 | [diff] [blame] | 745 | |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 746 | def test_issuer(self, x509_data): |
Jean-Paul Calderone | f0179c7 | 2009-07-17 15:48:22 -0400 | [diff] [blame] | 747 | """ |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 748 | If an extension requires an issuer, the `issuer` parameter to |
| 749 | `X509Extension` provides its value. |
Jean-Paul Calderone | f0179c7 | 2009-07-17 15:48:22 -0400 | [diff] [blame] | 750 | """ |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 751 | pkey, x509 = x509_data |
Jean-Paul Calderone | f0179c7 | 2009-07-17 15:48:22 -0400 | [diff] [blame] | 752 | ext2 = X509Extension( |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 753 | b'authorityKeyIdentifier', False, b'issuer:always', |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 754 | issuer=x509) |
| 755 | x509.add_extensions([ext2]) |
| 756 | x509.sign(pkey, 'sha1') |
| 757 | text = dump_certificate(FILETYPE_TEXT, x509) |
| 758 | assert b'X509v3 Authority Key Identifier:' in text |
| 759 | assert b'DirName:/CN=Yoda root CA' in text |
Jean-Paul Calderone | f0179c7 | 2009-07-17 15:48:22 -0400 | [diff] [blame] | 760 | |
Jean-Paul Calderone | f0179c7 | 2009-07-17 15:48:22 -0400 | [diff] [blame] | 761 | def test_missing_issuer(self): |
| 762 | """ |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 763 | If an extension requires an issue and the `issuer` parameter is |
| 764 | given no value, something happens. |
Jean-Paul Calderone | f0179c7 | 2009-07-17 15:48:22 -0400 | [diff] [blame] | 765 | """ |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 766 | with pytest.raises(Error): |
| 767 | X509Extension( |
| 768 | b'authorityKeyIdentifier', |
| 769 | False, b'keyid:always,issuer:always') |
Jean-Paul Calderone | f0179c7 | 2009-07-17 15:48:22 -0400 | [diff] [blame] | 770 | |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 771 | @pytest.mark.parametrize('bad_obj', [ |
| 772 | True, |
| 773 | object(), |
| 774 | "hello", |
| 775 | [], |
| 776 | ]) |
| 777 | def test_invalid_issuer(self, bad_obj): |
Jean-Paul Calderone | f0179c7 | 2009-07-17 15:48:22 -0400 | [diff] [blame] | 778 | """ |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 779 | If the `issuer` parameter is given a value which is not an |
| 780 | `X509` instance, `TypeError` is raised. |
Jean-Paul Calderone | f0179c7 | 2009-07-17 15:48:22 -0400 | [diff] [blame] | 781 | """ |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 782 | with pytest.raises(TypeError): |
| 783 | X509Extension( |
| 784 | 'basicConstraints', False, 'keyid:always,issuer:always', |
| 785 | issuer=bad_obj) |
Rick Dean | 47262da | 2009-07-08 16:17:17 -0500 | [diff] [blame] | 786 | |
| 787 | |
Paul Kehrer | 72d968b | 2016-07-29 15:31:04 +0800 | [diff] [blame] | 788 | class TestPKey(object): |
| 789 | """ |
Hynek Schlawack | 3bcf315 | 2017-02-18 08:25:34 +0100 | [diff] [blame] | 790 | Tests for `OpenSSL.crypto.PKey`. |
Paul Kehrer | 72d968b | 2016-07-29 15:31:04 +0800 | [diff] [blame] | 791 | """ |
| 792 | |
| 793 | def test_convert_from_cryptography_private_key(self): |
| 794 | """ |
| 795 | PKey.from_cryptography_key creates a proper private PKey. |
| 796 | """ |
| 797 | key = serialization.load_pem_private_key( |
| 798 | intermediate_key_pem, None, backend |
| 799 | ) |
| 800 | pkey = PKey.from_cryptography_key(key) |
| 801 | |
| 802 | assert isinstance(pkey, PKey) |
| 803 | assert pkey.bits() == key.key_size |
| 804 | assert pkey._only_public is False |
| 805 | assert pkey._initialized is True |
| 806 | |
| 807 | def test_convert_from_cryptography_public_key(self): |
| 808 | """ |
| 809 | PKey.from_cryptography_key creates a proper public PKey. |
| 810 | """ |
| 811 | key = serialization.load_pem_public_key(cleartextPublicKeyPEM, backend) |
| 812 | pkey = PKey.from_cryptography_key(key) |
| 813 | |
| 814 | assert isinstance(pkey, PKey) |
| 815 | assert pkey.bits() == key.key_size |
| 816 | assert pkey._only_public is True |
| 817 | assert pkey._initialized is True |
| 818 | |
| 819 | def test_convert_from_cryptography_unsupported_type(self): |
| 820 | """ |
| 821 | PKey.from_cryptography_key raises TypeError with an unsupported type. |
| 822 | """ |
| 823 | key = serialization.load_pem_private_key( |
| 824 | ec_private_key_pem, None, backend |
| 825 | ) |
| 826 | with pytest.raises(TypeError): |
| 827 | PKey.from_cryptography_key(key) |
| 828 | |
| 829 | def test_convert_public_pkey_to_cryptography_key(self): |
| 830 | """ |
| 831 | PKey.to_cryptography_key creates a proper cryptography public key. |
| 832 | """ |
| 833 | pkey = load_publickey(FILETYPE_PEM, cleartextPublicKeyPEM) |
| 834 | key = pkey.to_cryptography_key() |
| 835 | |
| 836 | assert isinstance(key, rsa.RSAPublicKey) |
| 837 | assert pkey.bits() == key.key_size |
| 838 | |
| 839 | def test_convert_private_pkey_to_cryptography_key(self): |
| 840 | """ |
| 841 | PKey.to_cryptography_key creates a proper cryptography private key. |
| 842 | """ |
| 843 | pkey = load_privatekey(FILETYPE_PEM, cleartextPrivateKeyPEM) |
| 844 | key = pkey.to_cryptography_key() |
| 845 | |
| 846 | assert isinstance(key, rsa.RSAPrivateKey) |
| 847 | assert pkey.bits() == key.key_size |
| 848 | |
Jean-Paul Calderone | 6864905 | 2009-07-17 21:14:27 -0400 | [diff] [blame] | 849 | def test_type(self): |
| 850 | """ |
Alex Gaynor | 01f90a1 | 2019-02-07 09:14:48 -0500 | [diff] [blame] | 851 | `PKey` can be used to create instances of that type. |
Jean-Paul Calderone | 6864905 | 2009-07-17 21:14:27 -0400 | [diff] [blame] | 852 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 853 | assert is_consistent_type(PKey, 'PKey') |
Jean-Paul Calderone | 6864905 | 2009-07-17 21:14:27 -0400 | [diff] [blame] | 854 | |
Jean-Paul Calderone | d8782ad | 2008-03-04 23:39:59 -0500 | [diff] [blame] | 855 | def test_construction(self): |
| 856 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 857 | `PKey` takes no arguments and returns a new `PKey` instance. |
Jean-Paul Calderone | d8782ad | 2008-03-04 23:39:59 -0500 | [diff] [blame] | 858 | """ |
Jean-Paul Calderone | d8782ad | 2008-03-04 23:39:59 -0500 | [diff] [blame] | 859 | key = PKey() |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 860 | assert isinstance(key, PKey) |
Jean-Paul Calderone | d8782ad | 2008-03-04 23:39:59 -0500 | [diff] [blame] | 861 | |
Jean-Paul Calderone | d8782ad | 2008-03-04 23:39:59 -0500 | [diff] [blame] | 862 | def test_pregeneration(self): |
| 863 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 864 | `PKey.bits` and `PKey.type` return `0` before the key is generated. |
| 865 | `PKey.check` raises `TypeError` before the key is generated. |
Jean-Paul Calderone | d8782ad | 2008-03-04 23:39:59 -0500 | [diff] [blame] | 866 | """ |
| 867 | key = PKey() |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 868 | assert key.type() == 0 |
| 869 | assert key.bits() == 0 |
| 870 | with pytest.raises(TypeError): |
| 871 | key.check() |
Jean-Paul Calderone | d8782ad | 2008-03-04 23:39:59 -0500 | [diff] [blame] | 872 | |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 873 | def test_failed_generation(self): |
Jean-Paul Calderone | d8782ad | 2008-03-04 23:39:59 -0500 | [diff] [blame] | 874 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 875 | `PKey.generate_key` takes two arguments, the first giving the key type |
| 876 | as one of `TYPE_RSA` or `TYPE_DSA` and the second giving the number of |
| 877 | bits to generate. If an invalid type is specified or generation fails, |
| 878 | `Error` is raised. If an invalid number of bits is specified, |
| 879 | `ValueError` or `Error` is raised. |
Jean-Paul Calderone | d8782ad | 2008-03-04 23:39:59 -0500 | [diff] [blame] | 880 | """ |
| 881 | key = PKey() |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 882 | with pytest.raises(TypeError): |
| 883 | key.generate_key("foo", "bar") |
| 884 | with pytest.raises(Error): |
| 885 | key.generate_key(-1, 0) |
Jean-Paul Calderone | ab82db7 | 2008-03-06 00:09:31 -0500 | [diff] [blame] | 886 | |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 887 | with pytest.raises(ValueError): |
| 888 | key.generate_key(TYPE_RSA, -1) |
| 889 | with pytest.raises(ValueError): |
| 890 | key.generate_key(TYPE_RSA, 0) |
Jean-Paul Calderone | d71fe98 | 2008-03-06 00:31:50 -0500 | [diff] [blame] | 891 | |
Alex Gaynor | 5bb2bd1 | 2016-07-03 10:48:32 -0400 | [diff] [blame] | 892 | with pytest.raises(TypeError): |
| 893 | key.generate_key(TYPE_RSA, object()) |
| 894 | |
Jean-Paul Calderone | d71fe98 | 2008-03-06 00:31:50 -0500 | [diff] [blame] | 895 | # XXX RSA generation for small values of bits is fairly buggy in a wide |
| 896 | # range of OpenSSL versions. I need to figure out what the safe lower |
| 897 | # bound for a reasonable number of OpenSSL versions is and explicitly |
| 898 | # check for that in the wrapper. The failure behavior is typically an |
| 899 | # infinite loop inside OpenSSL. |
| 900 | |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 901 | # with pytest.raises(Error): |
| 902 | # key.generate_key(TYPE_RSA, 2) |
Jean-Paul Calderone | d8782ad | 2008-03-04 23:39:59 -0500 | [diff] [blame] | 903 | |
| 904 | # XXX DSA generation seems happy with any number of bits. The DSS |
| 905 | # says bits must be between 512 and 1024 inclusive. OpenSSL's DSA |
| 906 | # generator doesn't seem to care about the upper limit at all. For |
Jean-Paul Calderone | eff3cd9 | 2008-03-05 22:35:26 -0500 | [diff] [blame] | 907 | # the lower limit, it uses 512 if anything smaller is specified. |
Jean-Paul Calderone | d8782ad | 2008-03-04 23:39:59 -0500 | [diff] [blame] | 908 | # So, it doesn't seem possible to make generate_key fail for |
| 909 | # TYPE_DSA with a bits argument which is at least an int. |
| 910 | |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 911 | # with pytest.raises(Error): |
| 912 | # key.generate_key(TYPE_DSA, -7) |
Jean-Paul Calderone | d8782ad | 2008-03-04 23:39:59 -0500 | [diff] [blame] | 913 | |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 914 | def test_rsa_generation(self): |
Jean-Paul Calderone | d8782ad | 2008-03-04 23:39:59 -0500 | [diff] [blame] | 915 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 916 | `PKey.generate_key` generates an RSA key when passed `TYPE_RSA` as a |
| 917 | type and a reasonable number of bits. |
Jean-Paul Calderone | d8782ad | 2008-03-04 23:39:59 -0500 | [diff] [blame] | 918 | """ |
Alex Gaynor | 6e9f976 | 2018-05-12 07:44:37 -0400 | [diff] [blame] | 919 | bits = 512 |
Jean-Paul Calderone | d8782ad | 2008-03-04 23:39:59 -0500 | [diff] [blame] | 920 | key = PKey() |
| 921 | key.generate_key(TYPE_RSA, bits) |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 922 | assert key.type() == TYPE_RSA |
| 923 | assert key.bits() == bits |
| 924 | assert key.check() |
Jean-Paul Calderone | d8782ad | 2008-03-04 23:39:59 -0500 | [diff] [blame] | 925 | |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 926 | def test_dsa_generation(self): |
Jean-Paul Calderone | d8782ad | 2008-03-04 23:39:59 -0500 | [diff] [blame] | 927 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 928 | `PKey.generate_key` generates a DSA key when passed `TYPE_DSA` as a |
| 929 | type and a reasonable number of bits. |
Jean-Paul Calderone | d8782ad | 2008-03-04 23:39:59 -0500 | [diff] [blame] | 930 | """ |
| 931 | # 512 is a magic number. The DSS (Digital Signature Standard) |
| 932 | # allows a minimum of 512 bits for DSA. DSA_generate_parameters |
| 933 | # will silently promote any value below 512 to 512. |
| 934 | bits = 512 |
| 935 | key = PKey() |
| 936 | key.generate_key(TYPE_DSA, bits) |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 937 | assert key.type() == TYPE_DSA |
| 938 | assert key.bits() == bits |
| 939 | with pytest.raises(TypeError): |
| 940 | key.check() |
Jean-Paul Calderone | d8782ad | 2008-03-04 23:39:59 -0500 | [diff] [blame] | 941 | |
Jean-Paul Calderone | d8782ad | 2008-03-04 23:39:59 -0500 | [diff] [blame] | 942 | def test_regeneration(self): |
| 943 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 944 | `PKey.generate_key` can be called multiple times on the same key to |
| 945 | generate new keys. |
Jean-Paul Calderone | d8782ad | 2008-03-04 23:39:59 -0500 | [diff] [blame] | 946 | """ |
| 947 | key = PKey() |
| 948 | for type, bits in [(TYPE_RSA, 512), (TYPE_DSA, 576)]: |
Alex Gaynor | 7f63649 | 2015-09-04 13:26:52 -0400 | [diff] [blame] | 949 | key.generate_key(type, bits) |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 950 | assert key.type() == type |
| 951 | assert key.bits() == bits |
Jean-Paul Calderone | eff3cd9 | 2008-03-05 22:35:26 -0500 | [diff] [blame] | 952 | |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 953 | def test_inconsistent_key(self): |
Jean-Paul Calderone | 55ec171 | 2009-05-13 14:14:30 -0400 | [diff] [blame] | 954 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 955 | `PKey.check` returns `Error` if the key is not consistent. |
Jean-Paul Calderone | 55ec171 | 2009-05-13 14:14:30 -0400 | [diff] [blame] | 956 | """ |
| 957 | key = load_privatekey(FILETYPE_PEM, inconsistentPrivateKeyPEM) |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 958 | with pytest.raises(Error): |
| 959 | key.check() |
Jean-Paul Calderone | e81020e | 2011-06-12 21:48:57 -0400 | [diff] [blame] | 960 | |
Jean-Paul Calderone | 02d0197 | 2011-10-31 10:39:29 -0400 | [diff] [blame] | 961 | def test_check_public_key(self): |
| 962 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 963 | `PKey.check` raises `TypeError` if only the public part of the key |
| 964 | is available. |
Jean-Paul Calderone | 02d0197 | 2011-10-31 10:39:29 -0400 | [diff] [blame] | 965 | """ |
| 966 | # A trick to get a public-only key |
| 967 | key = PKey() |
| 968 | key.generate_key(TYPE_RSA, 512) |
| 969 | cert = X509() |
| 970 | cert.set_pubkey(key) |
| 971 | pub = cert.get_pubkey() |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 972 | with pytest.raises(TypeError): |
| 973 | pub.check() |
Jean-Paul Calderone | 02d0197 | 2011-10-31 10:39:29 -0400 | [diff] [blame] | 974 | |
Mrmaxmeier | 8cd3b17 | 2020-03-11 22:03:59 +0100 | [diff] [blame] | 975 | def test_check_pr_897(self): |
| 976 | """ |
| 977 | `PKey.check` raises `OpenSSL.crypto.Error` if provided with broken key |
| 978 | """ |
| 979 | pkey = load_privatekey(FILETYPE_PEM, rsa_p_not_prime_pem) |
| 980 | with pytest.raises(Error): |
| 981 | pkey.check() |
| 982 | |
Jean-Paul Calderone | 02d0197 | 2011-10-31 10:39:29 -0400 | [diff] [blame] | 983 | |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 984 | def x509_name(**attrs): |
Jean-Paul Calderone | eff3cd9 | 2008-03-05 22:35:26 -0500 | [diff] [blame] | 985 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 986 | Return a new X509Name with the given attributes. |
Jean-Paul Calderone | eff3cd9 | 2008-03-05 22:35:26 -0500 | [diff] [blame] | 987 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 988 | # XXX There's no other way to get a new X509Name yet. |
| 989 | name = X509().get_subject() |
| 990 | attrs = list(attrs.items()) |
Alex Gaynor | aceb3e2 | 2015-09-05 12:00:22 -0400 | [diff] [blame] | 991 | |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 992 | # Make the order stable - order matters! |
| 993 | def key(attr): |
| 994 | return attr[1] |
| 995 | attrs.sort(key=key) |
| 996 | for k, v in attrs: |
| 997 | setattr(name, k, v) |
| 998 | return name |
Alex Gaynor | 85b4970 | 2015-09-05 16:30:59 -0400 | [diff] [blame] | 999 | |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1000 | |
| 1001 | class TestX509Name(object): |
| 1002 | """ |
| 1003 | Unit tests for `OpenSSL.crypto.X509Name`. |
| 1004 | """ |
Jean-Paul Calderone | e098dc7 | 2008-03-06 18:36:19 -0500 | [diff] [blame] | 1005 | |
Rick Dean | e15b147 | 2009-07-09 15:53:42 -0500 | [diff] [blame] | 1006 | def test_type(self): |
| 1007 | """ |
Alex Gaynor | 01f90a1 | 2019-02-07 09:14:48 -0500 | [diff] [blame] | 1008 | The type of X509Name objects is `X509Name`. |
Rick Dean | e15b147 | 2009-07-09 15:53:42 -0500 | [diff] [blame] | 1009 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1010 | name = x509_name() |
Alex Gaynor | 01f90a1 | 2019-02-07 09:14:48 -0500 | [diff] [blame] | 1011 | assert isinstance(name, X509Name) |
Rick Dean | e15b147 | 2009-07-09 15:53:42 -0500 | [diff] [blame] | 1012 | |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1013 | def test_only_string_attributes(self): |
Jean-Paul Calderone | 9ce9afb | 2011-04-22 18:16:22 -0400 | [diff] [blame] | 1014 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1015 | Attempting to set a non-`str` attribute name on an `X509Name` instance |
| 1016 | causes `TypeError` to be raised. |
Jean-Paul Calderone | 9ce9afb | 2011-04-22 18:16:22 -0400 | [diff] [blame] | 1017 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1018 | name = x509_name() |
Jean-Paul Calderone | 9ce9afb | 2011-04-22 18:16:22 -0400 | [diff] [blame] | 1019 | # Beyond these cases, you may also think that unicode should be |
Alex Gaynor | 3128750 | 2015-09-05 16:11:27 -0400 | [diff] [blame] | 1020 | # rejected. Sorry, you're wrong. unicode is automatically converted |
| 1021 | # to str outside of the control of X509Name, so there's no way to |
| 1022 | # reject it. |
Jean-Paul Calderone | ff363be | 2013-03-03 10:21:23 -0800 | [diff] [blame] | 1023 | |
Alex Gaynor | 3128750 | 2015-09-05 16:11:27 -0400 | [diff] [blame] | 1024 | # Also, this used to test str subclasses, but that test is less |
| 1025 | # relevant now that the implementation is in Python instead of C. Also |
| 1026 | # PyPy automatically converts str subclasses to str when they are |
| 1027 | # passed to setattr, so we can't test it on PyPy. Apparently CPython |
| 1028 | # does this sometimes as well. |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1029 | with pytest.raises(TypeError): |
| 1030 | setattr(name, None, "hello") |
| 1031 | with pytest.raises(TypeError): |
| 1032 | setattr(name, 30, "hello") |
Jean-Paul Calderone | 9ce9afb | 2011-04-22 18:16:22 -0400 | [diff] [blame] | 1033 | |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1034 | def test_set_invalid_attribute(self): |
Jean-Paul Calderone | 9ce9afb | 2011-04-22 18:16:22 -0400 | [diff] [blame] | 1035 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1036 | Attempting to set any attribute name on an `X509Name` instance for |
| 1037 | which no corresponding NID is defined causes `AttributeError` to be |
| 1038 | raised. |
Jean-Paul Calderone | 9ce9afb | 2011-04-22 18:16:22 -0400 | [diff] [blame] | 1039 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1040 | name = x509_name() |
| 1041 | with pytest.raises(AttributeError): |
| 1042 | setattr(name, "no such thing", None) |
Jean-Paul Calderone | 9ce9afb | 2011-04-22 18:16:22 -0400 | [diff] [blame] | 1043 | |
Jean-Paul Calderone | eff3cd9 | 2008-03-05 22:35:26 -0500 | [diff] [blame] | 1044 | def test_attributes(self): |
| 1045 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1046 | `X509Name` instances have attributes for each standard (?) |
| 1047 | X509Name field. |
Jean-Paul Calderone | eff3cd9 | 2008-03-05 22:35:26 -0500 | [diff] [blame] | 1048 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1049 | name = x509_name() |
Jean-Paul Calderone | eff3cd9 | 2008-03-05 22:35:26 -0500 | [diff] [blame] | 1050 | name.commonName = "foo" |
Alex Gaynor | 3772611 | 2016-07-04 09:51:32 -0400 | [diff] [blame] | 1051 | assert name.commonName == "foo" |
| 1052 | assert name.CN == "foo" |
| 1053 | |
Jean-Paul Calderone | eff3cd9 | 2008-03-05 22:35:26 -0500 | [diff] [blame] | 1054 | name.CN = "baz" |
Alex Gaynor | 3772611 | 2016-07-04 09:51:32 -0400 | [diff] [blame] | 1055 | assert name.commonName == "baz" |
| 1056 | assert name.CN == "baz" |
| 1057 | |
Jean-Paul Calderone | eff3cd9 | 2008-03-05 22:35:26 -0500 | [diff] [blame] | 1058 | name.commonName = "bar" |
Alex Gaynor | 3772611 | 2016-07-04 09:51:32 -0400 | [diff] [blame] | 1059 | assert name.commonName == "bar" |
| 1060 | assert name.CN == "bar" |
| 1061 | |
Jean-Paul Calderone | eff3cd9 | 2008-03-05 22:35:26 -0500 | [diff] [blame] | 1062 | name.CN = "quux" |
Alex Gaynor | 3772611 | 2016-07-04 09:51:32 -0400 | [diff] [blame] | 1063 | assert name.commonName == "quux" |
| 1064 | assert name.CN == "quux" |
| 1065 | |
| 1066 | assert name.OU is None |
Jean-Paul Calderone | eff3cd9 | 2008-03-05 22:35:26 -0500 | [diff] [blame] | 1067 | |
Alex Gaynor | 7778e79 | 2016-07-03 23:38:48 -0400 | [diff] [blame] | 1068 | with pytest.raises(AttributeError): |
| 1069 | name.foobar |
| 1070 | |
Jean-Paul Calderone | eff3cd9 | 2008-03-05 22:35:26 -0500 | [diff] [blame] | 1071 | def test_copy(self): |
| 1072 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1073 | `X509Name` creates a new `X509Name` instance with all the same |
| 1074 | attributes as an existing `X509Name` instance when called with one. |
Jean-Paul Calderone | eff3cd9 | 2008-03-05 22:35:26 -0500 | [diff] [blame] | 1075 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1076 | name = x509_name(commonName="foo", emailAddress="bar@example.com") |
Jean-Paul Calderone | eff3cd9 | 2008-03-05 22:35:26 -0500 | [diff] [blame] | 1077 | |
| 1078 | copy = X509Name(name) |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1079 | assert copy.commonName == "foo" |
| 1080 | assert copy.emailAddress == "bar@example.com" |
Jean-Paul Calderone | e098dc7 | 2008-03-06 18:36:19 -0500 | [diff] [blame] | 1081 | |
| 1082 | # Mutate the copy and ensure the original is unmodified. |
Jean-Paul Calderone | eff3cd9 | 2008-03-05 22:35:26 -0500 | [diff] [blame] | 1083 | copy.commonName = "baz" |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1084 | assert name.commonName == "foo" |
Jean-Paul Calderone | e098dc7 | 2008-03-06 18:36:19 -0500 | [diff] [blame] | 1085 | |
| 1086 | # Mutate the original and ensure the copy is unmodified. |
Jean-Paul Calderone | eff3cd9 | 2008-03-05 22:35:26 -0500 | [diff] [blame] | 1087 | name.emailAddress = "quux@example.com" |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1088 | assert copy.emailAddress == "bar@example.com" |
Jean-Paul Calderone | eff3cd9 | 2008-03-05 22:35:26 -0500 | [diff] [blame] | 1089 | |
Jean-Paul Calderone | e098dc7 | 2008-03-06 18:36:19 -0500 | [diff] [blame] | 1090 | def test_repr(self): |
| 1091 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1092 | `repr` passed an `X509Name` instance should return a string containing |
| 1093 | a description of the type and the NIDs which have been set on it. |
Jean-Paul Calderone | e098dc7 | 2008-03-06 18:36:19 -0500 | [diff] [blame] | 1094 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1095 | name = x509_name(commonName="foo", emailAddress="bar") |
| 1096 | assert repr(name) == "<X509Name object '/emailAddress=bar/CN=foo'>" |
Jean-Paul Calderone | e098dc7 | 2008-03-06 18:36:19 -0500 | [diff] [blame] | 1097 | |
Jean-Paul Calderone | e098dc7 | 2008-03-06 18:36:19 -0500 | [diff] [blame] | 1098 | def test_comparison(self): |
| 1099 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1100 | `X509Name` instances should compare based on their NIDs. |
Jean-Paul Calderone | e098dc7 | 2008-03-06 18:36:19 -0500 | [diff] [blame] | 1101 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1102 | def _equality(a, b, assert_true, assert_false): |
| 1103 | assert_true(a == b) |
| 1104 | assert_false(a != b) |
| 1105 | assert_true(b == a) |
| 1106 | assert_false(b != a) |
Jean-Paul Calderone | e098dc7 | 2008-03-06 18:36:19 -0500 | [diff] [blame] | 1107 | |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1108 | def assert_true(x): |
| 1109 | assert x |
| 1110 | |
| 1111 | def assert_false(x): |
| 1112 | assert not x |
| 1113 | |
| 1114 | def assert_equal(a, b): |
| 1115 | _equality(a, b, assert_true, assert_false) |
Jean-Paul Calderone | e098dc7 | 2008-03-06 18:36:19 -0500 | [diff] [blame] | 1116 | |
| 1117 | # Instances compare equal to themselves. |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1118 | name = x509_name() |
| 1119 | assert_equal(name, name) |
Jean-Paul Calderone | e098dc7 | 2008-03-06 18:36:19 -0500 | [diff] [blame] | 1120 | |
| 1121 | # Empty instances should compare equal to each other. |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1122 | assert_equal(x509_name(), x509_name()) |
Jean-Paul Calderone | e098dc7 | 2008-03-06 18:36:19 -0500 | [diff] [blame] | 1123 | |
| 1124 | # Instances with equal NIDs should compare equal to each other. |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1125 | assert_equal(x509_name(commonName="foo"), |
| 1126 | x509_name(commonName="foo")) |
Jean-Paul Calderone | e098dc7 | 2008-03-06 18:36:19 -0500 | [diff] [blame] | 1127 | |
| 1128 | # Instance with equal NIDs set using different aliases should compare |
| 1129 | # equal to each other. |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1130 | assert_equal(x509_name(commonName="foo"), |
| 1131 | x509_name(CN="foo")) |
Jean-Paul Calderone | e098dc7 | 2008-03-06 18:36:19 -0500 | [diff] [blame] | 1132 | |
| 1133 | # Instances with more than one NID with the same values should compare |
| 1134 | # equal to each other. |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1135 | assert_equal(x509_name(CN="foo", organizationalUnitName="bar"), |
| 1136 | x509_name(commonName="foo", OU="bar")) |
Jean-Paul Calderone | e098dc7 | 2008-03-06 18:36:19 -0500 | [diff] [blame] | 1137 | |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1138 | def assert_not_equal(a, b): |
| 1139 | _equality(a, b, assert_false, assert_true) |
Jean-Paul Calderone | e098dc7 | 2008-03-06 18:36:19 -0500 | [diff] [blame] | 1140 | |
| 1141 | # Instances with different values for the same NID should not compare |
| 1142 | # equal to each other. |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1143 | assert_not_equal(x509_name(CN="foo"), |
| 1144 | x509_name(CN="bar")) |
Jean-Paul Calderone | e098dc7 | 2008-03-06 18:36:19 -0500 | [diff] [blame] | 1145 | |
| 1146 | # Instances with different NIDs should not compare equal to each other. |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1147 | assert_not_equal(x509_name(CN="foo"), |
| 1148 | x509_name(OU="foo")) |
Jean-Paul Calderone | e098dc7 | 2008-03-06 18:36:19 -0500 | [diff] [blame] | 1149 | |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1150 | assert_not_equal(x509_name(), object()) |
Alex Gaynor | 7778e79 | 2016-07-03 23:38:48 -0400 | [diff] [blame] | 1151 | |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1152 | def _inequality(a, b, assert_true, assert_false): |
| 1153 | assert_true(a < b) |
| 1154 | assert_true(a <= b) |
| 1155 | assert_true(b > a) |
| 1156 | assert_true(b >= a) |
| 1157 | assert_false(a > b) |
| 1158 | assert_false(a >= b) |
| 1159 | assert_false(b < a) |
| 1160 | assert_false(b <= a) |
Jean-Paul Calderone | e098dc7 | 2008-03-06 18:36:19 -0500 | [diff] [blame] | 1161 | |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1162 | def assert_less_than(a, b): |
| 1163 | _inequality(a, b, assert_true, assert_false) |
Jean-Paul Calderone | e098dc7 | 2008-03-06 18:36:19 -0500 | [diff] [blame] | 1164 | |
| 1165 | # An X509Name with a NID with a value which sorts less than the value |
| 1166 | # of the same NID on another X509Name compares less than the other |
| 1167 | # X509Name. |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1168 | assert_less_than(x509_name(CN="abc"), |
| 1169 | x509_name(CN="def")) |
Jean-Paul Calderone | e098dc7 | 2008-03-06 18:36:19 -0500 | [diff] [blame] | 1170 | |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1171 | def assert_greater_than(a, b): |
| 1172 | _inequality(a, b, assert_false, assert_true) |
Jean-Paul Calderone | e098dc7 | 2008-03-06 18:36:19 -0500 | [diff] [blame] | 1173 | |
| 1174 | # An X509Name with a NID with a value which sorts greater than the |
| 1175 | # value of the same NID on another X509Name compares greater than the |
| 1176 | # other X509Name. |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1177 | assert_greater_than(x509_name(CN="def"), |
| 1178 | x509_name(CN="abc")) |
Jean-Paul Calderone | 2aa2b33 | 2008-03-06 21:43:14 -0500 | [diff] [blame] | 1179 | |
Jean-Paul Calderone | 110cd09 | 2008-03-24 17:27:42 -0400 | [diff] [blame] | 1180 | def test_hash(self): |
| 1181 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1182 | `X509Name.hash` returns an integer hash based on the value of the name. |
Jean-Paul Calderone | 110cd09 | 2008-03-24 17:27:42 -0400 | [diff] [blame] | 1183 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1184 | a = x509_name(CN="foo") |
| 1185 | b = x509_name(CN="foo") |
| 1186 | assert a.hash() == b.hash() |
Jean-Paul Calderone | 110cd09 | 2008-03-24 17:27:42 -0400 | [diff] [blame] | 1187 | a.CN = "bar" |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1188 | assert a.hash() != b.hash() |
Jean-Paul Calderone | 110cd09 | 2008-03-24 17:27:42 -0400 | [diff] [blame] | 1189 | |
Jean-Paul Calderone | e957a00 | 2008-03-25 15:16:51 -0400 | [diff] [blame] | 1190 | def test_der(self): |
| 1191 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1192 | `X509Name.der` returns the DER encoded form of the name. |
Jean-Paul Calderone | e957a00 | 2008-03-25 15:16:51 -0400 | [diff] [blame] | 1193 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1194 | a = x509_name(CN="foo", C="US") |
| 1195 | assert (a.der() == |
| 1196 | b'0\x1b1\x0b0\t\x06\x03U\x04\x06\x13\x02US' |
| 1197 | b'1\x0c0\n\x06\x03U\x04\x03\x0c\x03foo') |
Jean-Paul Calderone | e957a00 | 2008-03-25 15:16:51 -0400 | [diff] [blame] | 1198 | |
Jean-Paul Calderone | c54cc18 | 2008-03-26 21:11:07 -0400 | [diff] [blame] | 1199 | def test_get_components(self): |
| 1200 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1201 | `X509Name.get_components` returns a `list` of two-tuples of `str` |
Jean-Paul Calderone | c54cc18 | 2008-03-26 21:11:07 -0400 | [diff] [blame] | 1202 | giving the NIDs and associated values which make up the name. |
| 1203 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1204 | a = x509_name() |
| 1205 | assert a.get_components() == [] |
Jean-Paul Calderone | c54cc18 | 2008-03-26 21:11:07 -0400 | [diff] [blame] | 1206 | a.CN = "foo" |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1207 | assert a.get_components() == [(b"CN", b"foo")] |
Jean-Paul Calderone | c54cc18 | 2008-03-26 21:11:07 -0400 | [diff] [blame] | 1208 | a.organizationalUnitName = "bar" |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1209 | assert a.get_components() == [(b"CN", b"foo"), (b"OU", b"bar")] |
Jean-Paul Calderone | c54cc18 | 2008-03-26 21:11:07 -0400 | [diff] [blame] | 1210 | |
Jean-Paul Calderone | 4bf75c6 | 2013-08-23 15:39:53 -0400 | [diff] [blame] | 1211 | def test_load_nul_byte_attribute(self): |
| 1212 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1213 | An `X509Name` from an `X509` instance loaded from a file can have a |
Jean-Paul Calderone | 4bf75c6 | 2013-08-23 15:39:53 -0400 | [diff] [blame] | 1214 | NUL byte in the value of one of its attributes. |
| 1215 | """ |
| 1216 | cert = load_certificate(FILETYPE_PEM, nulbyteSubjectAltNamePEM) |
| 1217 | subject = cert.get_subject() |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1218 | assert "null.python.org\x00example.org" == subject.commonName |
Jean-Paul Calderone | 4bf75c6 | 2013-08-23 15:39:53 -0400 | [diff] [blame] | 1219 | |
Romuald Brunet | 4183beb | 2019-01-21 19:38:33 +0100 | [diff] [blame] | 1220 | def test_load_nul_byte_components(self): |
| 1221 | """ |
| 1222 | An `X509Name` from an `X509` instance loaded from a file can have a |
| 1223 | NUL byte in the value of its components |
| 1224 | """ |
| 1225 | cert = load_certificate(FILETYPE_PEM, nulbyteSubjectAltNamePEM) |
| 1226 | subject = cert.get_subject() |
| 1227 | components = subject.get_components() |
| 1228 | ccn = [value for name, value in components if name == b'CN'] |
| 1229 | assert ccn[0] == b'null.python.org\x00example.org' |
| 1230 | |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1231 | def test_set_attribute_failure(self): |
Jean-Paul Calderone | 5300d6a | 2013-12-29 16:36:50 -0500 | [diff] [blame] | 1232 | """ |
| 1233 | If the value of an attribute cannot be set for some reason then |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1234 | `Error` is raised. |
Jean-Paul Calderone | 5300d6a | 2013-12-29 16:36:50 -0500 | [diff] [blame] | 1235 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1236 | name = x509_name() |
Jean-Paul Calderone | 5300d6a | 2013-12-29 16:36:50 -0500 | [diff] [blame] | 1237 | # This value is too long |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1238 | with pytest.raises(Error): |
| 1239 | setattr(name, "O", b"x" * 512) |
Jean-Paul Calderone | 5300d6a | 2013-12-29 16:36:50 -0500 | [diff] [blame] | 1240 | |
| 1241 | |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 1242 | class _PKeyInteractionTestsMixin: |
| 1243 | """ |
| 1244 | Tests which involve another thing and a PKey. |
| 1245 | """ |
Alex Gaynor | aceb3e2 | 2015-09-05 12:00:22 -0400 | [diff] [blame] | 1246 | |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 1247 | def signable(self): |
| 1248 | """ |
Alex Chan | fb078d8 | 2017-04-20 11:16:15 +0100 | [diff] [blame] | 1249 | Return something with a `set_pubkey`, `set_pubkey`, and `sign` method. |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 1250 | """ |
| 1251 | raise NotImplementedError() |
| 1252 | |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1253 | def test_sign_with_ungenerated(self): |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 1254 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1255 | `X509Req.sign` raises `ValueError` when passed a `PKey` with no parts. |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 1256 | """ |
| 1257 | request = self.signable() |
| 1258 | key = PKey() |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1259 | with pytest.raises(ValueError): |
| 1260 | request.sign(key, GOOD_DIGEST) |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 1261 | |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1262 | def test_sign_with_public_key(self): |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 1263 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1264 | `X509Req.sign` raises `ValueError` when passed a `PKey` with no private |
| 1265 | part as the signing key. |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 1266 | """ |
| 1267 | request = self.signable() |
| 1268 | key = PKey() |
| 1269 | key.generate_key(TYPE_RSA, 512) |
| 1270 | request.set_pubkey(key) |
| 1271 | pub = request.get_pubkey() |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1272 | with pytest.raises(ValueError): |
| 1273 | request.sign(pub, GOOD_DIGEST) |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 1274 | |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1275 | def test_sign_with_unknown_digest(self): |
Jean-Paul Calderone | cc05a91 | 2010-08-03 18:24:19 -0400 | [diff] [blame] | 1276 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1277 | `X509Req.sign` raises `ValueError` when passed a digest name which is |
| 1278 | not known. |
Jean-Paul Calderone | cc05a91 | 2010-08-03 18:24:19 -0400 | [diff] [blame] | 1279 | """ |
| 1280 | request = self.signable() |
| 1281 | key = PKey() |
| 1282 | key.generate_key(TYPE_RSA, 512) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1283 | with pytest.raises(ValueError): |
| 1284 | request.sign(key, BAD_DIGEST) |
Jean-Paul Calderone | cc05a91 | 2010-08-03 18:24:19 -0400 | [diff] [blame] | 1285 | |
Jean-Paul Calderone | b972559 | 2010-08-03 18:17:22 -0400 | [diff] [blame] | 1286 | def test_sign(self): |
| 1287 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1288 | `X509Req.sign` succeeds when passed a private key object and a |
| 1289 | valid digest function. `X509Req.verify` can be used to check |
Alex Gaynor | 3128750 | 2015-09-05 16:11:27 -0400 | [diff] [blame] | 1290 | the signature. |
Jean-Paul Calderone | b972559 | 2010-08-03 18:17:22 -0400 | [diff] [blame] | 1291 | """ |
| 1292 | request = self.signable() |
| 1293 | key = PKey() |
| 1294 | key.generate_key(TYPE_RSA, 512) |
| 1295 | request.set_pubkey(key) |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 1296 | request.sign(key, GOOD_DIGEST) |
Jean-Paul Calderone | b972559 | 2010-08-03 18:17:22 -0400 | [diff] [blame] | 1297 | # If the type has a verify method, cover that too. |
| 1298 | if getattr(request, 'verify', None) is not None: |
| 1299 | pub = request.get_pubkey() |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1300 | assert request.verify(pub) |
Jean-Paul Calderone | b972559 | 2010-08-03 18:17:22 -0400 | [diff] [blame] | 1301 | # Make another key that won't verify. |
| 1302 | key = PKey() |
| 1303 | key.generate_key(TYPE_RSA, 512) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1304 | with pytest.raises(Error): |
| 1305 | request.verify(key) |
Jean-Paul Calderone | b972559 | 2010-08-03 18:17:22 -0400 | [diff] [blame] | 1306 | |
| 1307 | |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1308 | class TestX509Req(_PKeyInteractionTestsMixin): |
Jean-Paul Calderone | 2aa2b33 | 2008-03-06 21:43:14 -0500 | [diff] [blame] | 1309 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1310 | Tests for `OpenSSL.crypto.X509Req`. |
Jean-Paul Calderone | 2aa2b33 | 2008-03-06 21:43:14 -0500 | [diff] [blame] | 1311 | """ |
Alex Gaynor | aceb3e2 | 2015-09-05 12:00:22 -0400 | [diff] [blame] | 1312 | |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 1313 | def signable(self): |
| 1314 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1315 | Create and return a new `X509Req`. |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 1316 | """ |
| 1317 | return X509Req() |
| 1318 | |
Jean-Paul Calderone | 6864905 | 2009-07-17 21:14:27 -0400 | [diff] [blame] | 1319 | def test_type(self): |
| 1320 | """ |
Alex Gaynor | 01f90a1 | 2019-02-07 09:14:48 -0500 | [diff] [blame] | 1321 | `X509Req` can be used to create instances of that type. |
Jean-Paul Calderone | 6864905 | 2009-07-17 21:14:27 -0400 | [diff] [blame] | 1322 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1323 | assert is_consistent_type(X509Req, 'X509Req') |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 1324 | |
Jean-Paul Calderone | 2aa2b33 | 2008-03-06 21:43:14 -0500 | [diff] [blame] | 1325 | def test_construction(self): |
| 1326 | """ |
Alex Gaynor | 01f90a1 | 2019-02-07 09:14:48 -0500 | [diff] [blame] | 1327 | `X509Req` takes no arguments and returns an `X509Req` instance. |
Jean-Paul Calderone | 2aa2b33 | 2008-03-06 21:43:14 -0500 | [diff] [blame] | 1328 | """ |
| 1329 | request = X509Req() |
Alex Gaynor | 01f90a1 | 2019-02-07 09:14:48 -0500 | [diff] [blame] | 1330 | assert isinstance(request, X509Req) |
Jean-Paul Calderone | 2aa2b33 | 2008-03-06 21:43:14 -0500 | [diff] [blame] | 1331 | |
Jean-Paul Calderone | 8dd19b8 | 2008-12-28 20:41:16 -0500 | [diff] [blame] | 1332 | def test_version(self): |
| 1333 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1334 | `X509Req.set_version` sets the X.509 version of the certificate |
| 1335 | request. `X509Req.get_version` returns the X.509 version of the |
| 1336 | certificate request. The initial value of the version is 0. |
Jean-Paul Calderone | 8dd19b8 | 2008-12-28 20:41:16 -0500 | [diff] [blame] | 1337 | """ |
| 1338 | request = X509Req() |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1339 | assert request.get_version() == 0 |
Jean-Paul Calderone | 8dd19b8 | 2008-12-28 20:41:16 -0500 | [diff] [blame] | 1340 | request.set_version(1) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1341 | assert request.get_version() == 1 |
Jean-Paul Calderone | 8dd19b8 | 2008-12-28 20:41:16 -0500 | [diff] [blame] | 1342 | request.set_version(3) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1343 | assert request.get_version() == 3 |
Jean-Paul Calderone | 8dd19b8 | 2008-12-28 20:41:16 -0500 | [diff] [blame] | 1344 | |
Jean-Paul Calderone | 54e49e9 | 2010-07-30 11:04:46 -0400 | [diff] [blame] | 1345 | def test_version_wrong_args(self): |
Jean-Paul Calderone | aa6c069 | 2010-09-08 22:43:09 -0400 | [diff] [blame] | 1346 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1347 | `X509Req.set_version` raises `TypeError` if called with a non-`int` |
| 1348 | argument. |
Jean-Paul Calderone | aa6c069 | 2010-09-08 22:43:09 -0400 | [diff] [blame] | 1349 | """ |
Jean-Paul Calderone | 54e49e9 | 2010-07-30 11:04:46 -0400 | [diff] [blame] | 1350 | request = X509Req() |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1351 | with pytest.raises(TypeError): |
| 1352 | request.set_version("foo") |
Jean-Paul Calderone | 54e49e9 | 2010-07-30 11:04:46 -0400 | [diff] [blame] | 1353 | |
Jean-Paul Calderone | 2aa2b33 | 2008-03-06 21:43:14 -0500 | [diff] [blame] | 1354 | def test_get_subject(self): |
| 1355 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1356 | `X509Req.get_subject` returns an `X509Name` for the subject of the |
| 1357 | request and which is valid even after the request object is |
| 1358 | otherwise dead. |
Jean-Paul Calderone | 2aa2b33 | 2008-03-06 21:43:14 -0500 | [diff] [blame] | 1359 | """ |
| 1360 | request = X509Req() |
| 1361 | subject = request.get_subject() |
Alex Gaynor | 01f90a1 | 2019-02-07 09:14:48 -0500 | [diff] [blame] | 1362 | assert isinstance(subject, X509Name) |
Jean-Paul Calderone | 2aa2b33 | 2008-03-06 21:43:14 -0500 | [diff] [blame] | 1363 | subject.commonName = "foo" |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1364 | assert request.get_subject().commonName == "foo" |
Jean-Paul Calderone | 2aa2b33 | 2008-03-06 21:43:14 -0500 | [diff] [blame] | 1365 | del request |
| 1366 | subject.commonName = "bar" |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1367 | assert subject.commonName == "bar" |
Jean-Paul Calderone | 54e49e9 | 2010-07-30 11:04:46 -0400 | [diff] [blame] | 1368 | |
Jean-Paul Calderone | fe1b9bd | 2010-08-03 18:00:21 -0400 | [diff] [blame] | 1369 | def test_add_extensions(self): |
| 1370 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1371 | `X509Req.add_extensions` accepts a `list` of `X509Extension` instances |
| 1372 | and adds them to the X509 request. |
Jean-Paul Calderone | fe1b9bd | 2010-08-03 18:00:21 -0400 | [diff] [blame] | 1373 | """ |
| 1374 | request = X509Req() |
| 1375 | request.add_extensions([ |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 1376 | X509Extension(b'basicConstraints', True, b'CA:false')]) |
Stephen Holsapple | ca545b7 | 2014-01-28 21:43:25 -0800 | [diff] [blame] | 1377 | exts = request.get_extensions() |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1378 | assert len(exts) == 1 |
| 1379 | assert exts[0].get_short_name() == b'basicConstraints' |
| 1380 | assert exts[0].get_critical() == 1 |
| 1381 | assert exts[0].get_data() == b'0\x00' |
Stephen Holsapple | 7fbdf64 | 2014-03-01 20:05:47 -0800 | [diff] [blame] | 1382 | |
Stephen Holsapple | 7fbdf64 | 2014-03-01 20:05:47 -0800 | [diff] [blame] | 1383 | def test_get_extensions(self): |
| 1384 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1385 | `X509Req.get_extensions` returns a `list` of extensions added to this |
| 1386 | X509 request. |
Stephen Holsapple | 7fbdf64 | 2014-03-01 20:05:47 -0800 | [diff] [blame] | 1387 | """ |
| 1388 | request = X509Req() |
| 1389 | exts = request.get_extensions() |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1390 | assert exts == [] |
Stephen Holsapple | 7fbdf64 | 2014-03-01 20:05:47 -0800 | [diff] [blame] | 1391 | request.add_extensions([ |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 1392 | X509Extension(b'basicConstraints', True, b'CA:true'), |
| 1393 | X509Extension(b'keyUsage', False, b'digitalSignature')]) |
Stephen Holsapple | 7fbdf64 | 2014-03-01 20:05:47 -0800 | [diff] [blame] | 1394 | exts = request.get_extensions() |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1395 | assert len(exts) == 2 |
| 1396 | assert exts[0].get_short_name() == b'basicConstraints' |
| 1397 | assert exts[0].get_critical() == 1 |
| 1398 | assert exts[0].get_data() == b'0\x03\x01\x01\xff' |
| 1399 | assert exts[1].get_short_name() == b'keyUsage' |
| 1400 | assert exts[1].get_critical() == 0 |
| 1401 | assert exts[1].get_data() == b'\x03\x02\x07\x80' |
Jean-Paul Calderone | fe1b9bd | 2010-08-03 18:00:21 -0400 | [diff] [blame] | 1402 | |
Jean-Paul Calderone | fe1b9bd | 2010-08-03 18:00:21 -0400 | [diff] [blame] | 1403 | def test_add_extensions_wrong_args(self): |
| 1404 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1405 | `X509Req.add_extensions` raises `TypeError` if called with a |
| 1406 | non-`list`. Or it raises `ValueError` if called with a `list` |
| 1407 | containing objects other than `X509Extension` instances. |
Jean-Paul Calderone | fe1b9bd | 2010-08-03 18:00:21 -0400 | [diff] [blame] | 1408 | """ |
| 1409 | request = X509Req() |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1410 | with pytest.raises(TypeError): |
| 1411 | request.add_extensions(object()) |
| 1412 | with pytest.raises(ValueError): |
| 1413 | request.add_extensions([object()]) |
Jean-Paul Calderone | fe1b9bd | 2010-08-03 18:00:21 -0400 | [diff] [blame] | 1414 | |
Jean-Paul Calderone | 5565f0f | 2013-03-06 11:10:20 -0800 | [diff] [blame] | 1415 | def test_verify_wrong_args(self): |
| 1416 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1417 | `X509Req.verify` raises `TypeError` if passed anything other than a |
| 1418 | `PKey` instance as its single argument. |
Jean-Paul Calderone | 5565f0f | 2013-03-06 11:10:20 -0800 | [diff] [blame] | 1419 | """ |
| 1420 | request = X509Req() |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1421 | with pytest.raises(TypeError): |
| 1422 | request.verify(object()) |
Jean-Paul Calderone | 5565f0f | 2013-03-06 11:10:20 -0800 | [diff] [blame] | 1423 | |
Jean-Paul Calderone | 5565f0f | 2013-03-06 11:10:20 -0800 | [diff] [blame] | 1424 | def test_verify_uninitialized_key(self): |
| 1425 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1426 | `X509Req.verify` raises `OpenSSL.crypto.Error` if called with a |
| 1427 | `OpenSSL.crypto.PKey` which contains no key data. |
Jean-Paul Calderone | 5565f0f | 2013-03-06 11:10:20 -0800 | [diff] [blame] | 1428 | """ |
| 1429 | request = X509Req() |
| 1430 | pkey = PKey() |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1431 | with pytest.raises(Error): |
| 1432 | request.verify(pkey) |
Jean-Paul Calderone | 5565f0f | 2013-03-06 11:10:20 -0800 | [diff] [blame] | 1433 | |
Jean-Paul Calderone | 5565f0f | 2013-03-06 11:10:20 -0800 | [diff] [blame] | 1434 | def test_verify_wrong_key(self): |
| 1435 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1436 | `X509Req.verify` raises `OpenSSL.crypto.Error` if called with a |
| 1437 | `OpenSSL.crypto.PKey` which does not represent the public part of the |
Alex Gaynor | 3128750 | 2015-09-05 16:11:27 -0400 | [diff] [blame] | 1438 | key which signed the request. |
Jean-Paul Calderone | 5565f0f | 2013-03-06 11:10:20 -0800 | [diff] [blame] | 1439 | """ |
| 1440 | request = X509Req() |
| 1441 | pkey = load_privatekey(FILETYPE_PEM, cleartextPrivateKeyPEM) |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 1442 | request.sign(pkey, GOOD_DIGEST) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1443 | another_pkey = load_privatekey(FILETYPE_PEM, client_key_pem) |
| 1444 | with pytest.raises(Error): |
| 1445 | request.verify(another_pkey) |
| 1446 | |
| 1447 | def test_verify_success(self): |
| 1448 | """ |
| 1449 | `X509Req.verify` returns `True` if called with a `OpenSSL.crypto.PKey` |
| 1450 | which represents the public part of the key which signed the request. |
| 1451 | """ |
| 1452 | request = X509Req() |
| 1453 | pkey = load_privatekey(FILETYPE_PEM, cleartextPrivateKeyPEM) |
| 1454 | request.sign(pkey, GOOD_DIGEST) |
| 1455 | assert request.verify(pkey) |
Jean-Paul Calderone | 5565f0f | 2013-03-06 11:10:20 -0800 | [diff] [blame] | 1456 | |
Paul Kehrer | 41c1024 | 2017-06-29 18:24:17 -0500 | [diff] [blame] | 1457 | def test_convert_from_cryptography(self): |
| 1458 | crypto_req = x509.load_pem_x509_csr( |
| 1459 | cleartextCertificateRequestPEM, backend |
| 1460 | ) |
| 1461 | req = X509Req.from_cryptography(crypto_req) |
| 1462 | assert isinstance(req, X509Req) |
| 1463 | |
| 1464 | def test_convert_from_cryptography_unsupported_type(self): |
| 1465 | with pytest.raises(TypeError): |
| 1466 | X509Req.from_cryptography(object()) |
| 1467 | |
| 1468 | def test_convert_to_cryptography_key(self): |
| 1469 | req = load_certificate_request( |
| 1470 | FILETYPE_PEM, cleartextCertificateRequestPEM |
| 1471 | ) |
| 1472 | crypto_req = req.to_cryptography() |
| 1473 | assert isinstance(crypto_req, x509.CertificateSigningRequest) |
| 1474 | |
Jean-Paul Calderone | 5565f0f | 2013-03-06 11:10:20 -0800 | [diff] [blame] | 1475 | |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1476 | class TestX509(_PKeyInteractionTestsMixin): |
Jean-Paul Calderone | 78381d2 | 2008-03-06 23:35:22 -0500 | [diff] [blame] | 1477 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1478 | Tests for `OpenSSL.crypto.X509`. |
Jean-Paul Calderone | 78381d2 | 2008-03-06 23:35:22 -0500 | [diff] [blame] | 1479 | """ |
Jean-Paul Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 1480 | pemData = cleartextCertificatePEM + cleartextPrivateKeyPEM |
Jean-Paul Calderone | 8114b45 | 2008-03-25 15:27:59 -0400 | [diff] [blame] | 1481 | |
Roland Hedberg | 7e4930e | 2008-04-22 22:58:50 +0200 | [diff] [blame] | 1482 | extpem = """ |
| 1483 | -----BEGIN CERTIFICATE----- |
| 1484 | MIIC3jCCAkegAwIBAgIJAJHFjlcCgnQzMA0GCSqGSIb3DQEBBQUAMEcxCzAJBgNV |
| 1485 | BAYTAlNFMRUwEwYDVQQIEwxXZXN0ZXJib3R0b20xEjAQBgNVBAoTCUNhdGFsb2dp |
| 1486 | eDENMAsGA1UEAxMEUm9vdDAeFw0wODA0MjIxNDQ1MzhaFw0wOTA0MjIxNDQ1Mzha |
| 1487 | MFQxCzAJBgNVBAYTAlNFMQswCQYDVQQIEwJXQjEUMBIGA1UEChMLT3Blbk1ldGFk |
| 1488 | aXIxIjAgBgNVBAMTGW5vZGUxLm9tMi5vcGVubWV0YWRpci5vcmcwgZ8wDQYJKoZI |
| 1489 | hvcNAQEBBQADgY0AMIGJAoGBAPIcQMrwbk2nESF/0JKibj9i1x95XYAOwP+LarwT |
| 1490 | Op4EQbdlI9SY+uqYqlERhF19w7CS+S6oyqx0DRZSk4Y9dZ9j9/xgm2u/f136YS1u |
| 1491 | zgYFPvfUs6PqYLPSM8Bw+SjJ+7+2+TN+Tkiof9WP1cMjodQwOmdsiRbR0/J7+b1B |
| 1492 | hec1AgMBAAGjgcQwgcEwCQYDVR0TBAIwADAsBglghkgBhvhCAQ0EHxYdT3BlblNT |
| 1493 | TCBHZW5lcmF0ZWQgQ2VydGlmaWNhdGUwHQYDVR0OBBYEFIdHsBcMVVMbAO7j6NCj |
| 1494 | 03HgLnHaMB8GA1UdIwQYMBaAFL2h9Bf9Mre4vTdOiHTGAt7BRY/8MEYGA1UdEQQ/ |
| 1495 | MD2CDSouZXhhbXBsZS5vcmeCESoub20yLmV4bWFwbGUuY29thwSC7wgKgRNvbTJA |
| 1496 | b3Blbm1ldGFkaXIub3JnMA0GCSqGSIb3DQEBBQUAA4GBALd7WdXkp2KvZ7/PuWZA |
| 1497 | MPlIxyjS+Ly11+BNE0xGQRp9Wz+2lABtpgNqssvU156+HkKd02rGheb2tj7MX9hG |
| 1498 | uZzbwDAZzJPjzDQDD7d3cWsrVcfIdqVU7epHqIadnOF+X0ghJ39pAm6VVadnSXCt |
| 1499 | WpOdIpB8KksUTCzV591Nr1wd |
| 1500 | -----END CERTIFICATE----- |
| 1501 | """ |
Alex Gaynor | aceb3e2 | 2015-09-05 12:00:22 -0400 | [diff] [blame] | 1502 | |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 1503 | def signable(self): |
| 1504 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1505 | Create and return a new `X509`. |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 1506 | """ |
| 1507 | return X509() |
| 1508 | |
Jean-Paul Calderone | 6864905 | 2009-07-17 21:14:27 -0400 | [diff] [blame] | 1509 | def test_type(self): |
| 1510 | """ |
Alex Gaynor | 01f90a1 | 2019-02-07 09:14:48 -0500 | [diff] [blame] | 1511 | `X509` can be used to create instances of that type. |
Jean-Paul Calderone | 6864905 | 2009-07-17 21:14:27 -0400 | [diff] [blame] | 1512 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1513 | assert is_consistent_type(X509, 'X509') |
Jean-Paul Calderone | 6864905 | 2009-07-17 21:14:27 -0400 | [diff] [blame] | 1514 | |
Jean-Paul Calderone | 78381d2 | 2008-03-06 23:35:22 -0500 | [diff] [blame] | 1515 | def test_construction(self): |
| 1516 | """ |
Alex Gaynor | 01f90a1 | 2019-02-07 09:14:48 -0500 | [diff] [blame] | 1517 | `X509` takes no arguments and returns an instance of `X509`. |
Jean-Paul Calderone | 78381d2 | 2008-03-06 23:35:22 -0500 | [diff] [blame] | 1518 | """ |
| 1519 | certificate = X509() |
Alex Gaynor | 01f90a1 | 2019-02-07 09:14:48 -0500 | [diff] [blame] | 1520 | assert isinstance(certificate, X509) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1521 | assert type(certificate).__name__ == 'X509' |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1522 | assert type(certificate) == X509 |
Jean-Paul Calderone | 3544eb4 | 2010-07-30 22:09:47 -0400 | [diff] [blame] | 1523 | |
Jean-Paul Calderone | 3544eb4 | 2010-07-30 22:09:47 -0400 | [diff] [blame] | 1524 | def test_set_version_wrong_args(self): |
| 1525 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1526 | `X509.set_version` raises `TypeError` if invoked with an argument |
| 1527 | not of type `int`. |
Jean-Paul Calderone | 3544eb4 | 2010-07-30 22:09:47 -0400 | [diff] [blame] | 1528 | """ |
| 1529 | cert = X509() |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1530 | with pytest.raises(TypeError): |
| 1531 | cert.set_version(None) |
Jean-Paul Calderone | 3544eb4 | 2010-07-30 22:09:47 -0400 | [diff] [blame] | 1532 | |
Jean-Paul Calderone | 3544eb4 | 2010-07-30 22:09:47 -0400 | [diff] [blame] | 1533 | def test_version(self): |
| 1534 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1535 | `X509.set_version` sets the certificate version number. |
| 1536 | `X509.get_version` retrieves it. |
Jean-Paul Calderone | 3544eb4 | 2010-07-30 22:09:47 -0400 | [diff] [blame] | 1537 | """ |
| 1538 | cert = X509() |
| 1539 | cert.set_version(1234) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1540 | assert cert.get_version() == 1234 |
Jean-Paul Calderone | 3544eb4 | 2010-07-30 22:09:47 -0400 | [diff] [blame] | 1541 | |
Jean-Paul Calderone | 78381d2 | 2008-03-06 23:35:22 -0500 | [diff] [blame] | 1542 | def test_serial_number(self): |
| 1543 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1544 | The serial number of an `X509` can be retrieved and |
| 1545 | modified with `X509.get_serial_number` and |
| 1546 | `X509.set_serial_number`. |
Jean-Paul Calderone | 78381d2 | 2008-03-06 23:35:22 -0500 | [diff] [blame] | 1547 | """ |
| 1548 | certificate = X509() |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1549 | with pytest.raises(TypeError): |
| 1550 | certificate.set_serial_number("1") |
| 1551 | assert certificate.get_serial_number() == 0 |
Jean-Paul Calderone | 78381d2 | 2008-03-06 23:35:22 -0500 | [diff] [blame] | 1552 | certificate.set_serial_number(1) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1553 | assert certificate.get_serial_number() == 1 |
Jean-Paul Calderone | 78381d2 | 2008-03-06 23:35:22 -0500 | [diff] [blame] | 1554 | certificate.set_serial_number(2 ** 32 + 1) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1555 | assert certificate.get_serial_number() == 2 ** 32 + 1 |
Jean-Paul Calderone | 78381d2 | 2008-03-06 23:35:22 -0500 | [diff] [blame] | 1556 | certificate.set_serial_number(2 ** 64 + 1) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1557 | assert certificate.get_serial_number() == 2 ** 64 + 1 |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 1558 | certificate.set_serial_number(2 ** 128 + 1) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1559 | assert certificate.get_serial_number() == 2 ** 128 + 1 |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 1560 | |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 1561 | def _setBoundTest(self, which): |
| 1562 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1563 | `X509.set_notBefore` takes a string in the format of an |
Alex Gaynor | 3128750 | 2015-09-05 16:11:27 -0400 | [diff] [blame] | 1564 | ASN1 GENERALIZEDTIME and sets the beginning of the certificate's |
| 1565 | validity period to it. |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 1566 | """ |
| 1567 | certificate = X509() |
| 1568 | set = getattr(certificate, 'set_not' + which) |
| 1569 | get = getattr(certificate, 'get_not' + which) |
| 1570 | |
Jean-Paul Calderone | e0615b5 | 2008-03-09 21:44:46 -0400 | [diff] [blame] | 1571 | # Starts with no value. |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1572 | assert get() is None |
Jean-Paul Calderone | e0615b5 | 2008-03-09 21:44:46 -0400 | [diff] [blame] | 1573 | |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 1574 | # GMT (Or is it UTC?) -exarkun |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 1575 | when = b"20040203040506Z" |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 1576 | set(when) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1577 | assert get() == when |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 1578 | |
| 1579 | # A plus two hours and thirty minutes offset |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 1580 | when = b"20040203040506+0530" |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 1581 | set(when) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1582 | assert get() == when |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 1583 | |
| 1584 | # A minus one hour fifteen minutes offset |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 1585 | when = b"20040203040506-0115" |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 1586 | set(when) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1587 | assert get() == when |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 1588 | |
| 1589 | # An invalid string results in a ValueError |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1590 | with pytest.raises(ValueError): |
| 1591 | set(b"foo bar") |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 1592 | |
Jean-Paul Calderone | 31ca200 | 2010-01-30 15:14:43 -0500 | [diff] [blame] | 1593 | # The wrong number of arguments results in a TypeError. |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1594 | with pytest.raises(TypeError): |
| 1595 | set() |
Alex Gaynor | 85b4970 | 2015-09-05 16:30:59 -0400 | [diff] [blame] | 1596 | with pytest.raises(TypeError): |
| 1597 | set(b"20040203040506Z", b"20040203040506Z") |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1598 | with pytest.raises(TypeError): |
| 1599 | get(b"foo bar") |
Jean-Paul Calderone | e890db3 | 2010-08-22 16:55:15 -0400 | [diff] [blame] | 1600 | |
Jean-Paul Calderone | fe1b9bd | 2010-08-03 18:00:21 -0400 | [diff] [blame] | 1601 | # XXX ASN1_TIME (not GENERALIZEDTIME) |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 1602 | |
| 1603 | def test_set_notBefore(self): |
| 1604 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1605 | `X509.set_notBefore` takes a string in the format of an |
Alex Gaynor | 3128750 | 2015-09-05 16:11:27 -0400 | [diff] [blame] | 1606 | ASN1 GENERALIZEDTIME and sets the beginning of the certificate's |
| 1607 | validity period to it. |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 1608 | """ |
| 1609 | self._setBoundTest("Before") |
| 1610 | |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 1611 | def test_set_notAfter(self): |
| 1612 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1613 | `X509.set_notAfter` takes a string in the format of an ASN1 |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 1614 | GENERALIZEDTIME and sets the end of the certificate's validity period |
| 1615 | to it. |
| 1616 | """ |
| 1617 | self._setBoundTest("After") |
Jean-Paul Calderone | 76576d5 | 2008-03-24 16:04:46 -0400 | [diff] [blame] | 1618 | |
Jean-Paul Calderone | 38a646d | 2008-03-25 15:16:18 -0400 | [diff] [blame] | 1619 | def test_get_notBefore(self): |
| 1620 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1621 | `X509.get_notBefore` returns a string in the format of an |
Alex Gaynor | 3128750 | 2015-09-05 16:11:27 -0400 | [diff] [blame] | 1622 | ASN1 GENERALIZEDTIME even for certificates which store it as UTCTIME |
Jean-Paul Calderone | 38a646d | 2008-03-25 15:16:18 -0400 | [diff] [blame] | 1623 | internally. |
| 1624 | """ |
Paul Kehrer | a40898b | 2017-06-11 16:30:58 -1000 | [diff] [blame] | 1625 | cert = load_certificate(FILETYPE_PEM, old_root_cert_pem) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1626 | assert cert.get_notBefore() == b"20090325123658Z" |
Jean-Paul Calderone | 38a646d | 2008-03-25 15:16:18 -0400 | [diff] [blame] | 1627 | |
Rick Dean | 38a05c8 | 2009-07-18 01:41:30 -0500 | [diff] [blame] | 1628 | def test_get_notAfter(self): |
| 1629 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1630 | `X509.get_notAfter` returns a string in the format of an |
Alex Gaynor | 3128750 | 2015-09-05 16:11:27 -0400 | [diff] [blame] | 1631 | ASN1 GENERALIZEDTIME even for certificates which store it as UTCTIME |
Rick Dean | 38a05c8 | 2009-07-18 01:41:30 -0500 | [diff] [blame] | 1632 | internally. |
| 1633 | """ |
Paul Kehrer | a40898b | 2017-06-11 16:30:58 -1000 | [diff] [blame] | 1634 | cert = load_certificate(FILETYPE_PEM, old_root_cert_pem) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1635 | assert cert.get_notAfter() == b"20170611123658Z" |
Rick Dean | 38a05c8 | 2009-07-18 01:41:30 -0500 | [diff] [blame] | 1636 | |
Jean-Paul Calderone | ccf9d48 | 2010-07-30 22:36:42 -0400 | [diff] [blame] | 1637 | def test_gmtime_adj_notBefore_wrong_args(self): |
| 1638 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1639 | `X509.gmtime_adj_notBefore` raises `TypeError` if called with a |
| 1640 | non-`int` argument. |
Jean-Paul Calderone | ccf9d48 | 2010-07-30 22:36:42 -0400 | [diff] [blame] | 1641 | """ |
| 1642 | cert = X509() |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1643 | with pytest.raises(TypeError): |
| 1644 | cert.gmtime_adj_notBefore(None) |
Jean-Paul Calderone | ccf9d48 | 2010-07-30 22:36:42 -0400 | [diff] [blame] | 1645 | |
Alex Gaynor | 7f5610c | 2017-07-07 00:09:34 -0400 | [diff] [blame] | 1646 | @flaky.flaky |
Jean-Paul Calderone | 4f237b2 | 2010-07-30 22:29:39 -0400 | [diff] [blame] | 1647 | def test_gmtime_adj_notBefore(self): |
| 1648 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1649 | `X509.gmtime_adj_notBefore` changes the not-before timestamp to be the |
| 1650 | current time plus the number of seconds passed in. |
Jean-Paul Calderone | 4f237b2 | 2010-07-30 22:29:39 -0400 | [diff] [blame] | 1651 | """ |
| 1652 | cert = load_certificate(FILETYPE_PEM, self.pemData) |
Alex Gaynor | 85b4970 | 2015-09-05 16:30:59 -0400 | [diff] [blame] | 1653 | not_before_min = ( |
| 1654 | datetime.utcnow().replace(microsecond=0) + timedelta(seconds=100) |
| 1655 | ) |
Jean-Paul Calderone | 4f237b2 | 2010-07-30 22:29:39 -0400 | [diff] [blame] | 1656 | cert.gmtime_adj_notBefore(100) |
Alex Gaynor | 85b4970 | 2015-09-05 16:30:59 -0400 | [diff] [blame] | 1657 | not_before = datetime.strptime( |
| 1658 | cert.get_notBefore().decode(), "%Y%m%d%H%M%SZ" |
| 1659 | ) |
Alex Gaynor | 7f5610c | 2017-07-07 00:09:34 -0400 | [diff] [blame] | 1660 | not_before_max = datetime.utcnow() + timedelta(seconds=100) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1661 | assert not_before_min <= not_before <= not_before_max |
Jean-Paul Calderone | 4f237b2 | 2010-07-30 22:29:39 -0400 | [diff] [blame] | 1662 | |
Jean-Paul Calderone | ccf9d48 | 2010-07-30 22:36:42 -0400 | [diff] [blame] | 1663 | def test_gmtime_adj_notAfter_wrong_args(self): |
| 1664 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1665 | `X509.gmtime_adj_notAfter` raises `TypeError` if called with a |
| 1666 | non-`int` argument. |
Jean-Paul Calderone | ccf9d48 | 2010-07-30 22:36:42 -0400 | [diff] [blame] | 1667 | """ |
| 1668 | cert = X509() |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1669 | with pytest.raises(TypeError): |
| 1670 | cert.gmtime_adj_notAfter(None) |
Jean-Paul Calderone | ccf9d48 | 2010-07-30 22:36:42 -0400 | [diff] [blame] | 1671 | |
Alex Gaynor | 642de6f | 2017-07-24 00:57:38 -0400 | [diff] [blame] | 1672 | @flaky.flaky |
Jean-Paul Calderone | 4f237b2 | 2010-07-30 22:29:39 -0400 | [diff] [blame] | 1673 | def test_gmtime_adj_notAfter(self): |
| 1674 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1675 | `X509.gmtime_adj_notAfter` changes the not-after timestamp |
Alex Gaynor | 3128750 | 2015-09-05 16:11:27 -0400 | [diff] [blame] | 1676 | to be the current time plus the number of seconds passed in. |
Jean-Paul Calderone | 4f237b2 | 2010-07-30 22:29:39 -0400 | [diff] [blame] | 1677 | """ |
| 1678 | cert = load_certificate(FILETYPE_PEM, self.pemData) |
Alex Gaynor | 85b4970 | 2015-09-05 16:30:59 -0400 | [diff] [blame] | 1679 | not_after_min = ( |
| 1680 | datetime.utcnow().replace(microsecond=0) + timedelta(seconds=100) |
| 1681 | ) |
Jean-Paul Calderone | 4f237b2 | 2010-07-30 22:29:39 -0400 | [diff] [blame] | 1682 | cert.gmtime_adj_notAfter(100) |
Alex Gaynor | 85b4970 | 2015-09-05 16:30:59 -0400 | [diff] [blame] | 1683 | not_after = datetime.strptime( |
| 1684 | cert.get_notAfter().decode(), "%Y%m%d%H%M%SZ" |
| 1685 | ) |
Maximilian Hils | bed25c9 | 2015-07-25 12:58:07 +0200 | [diff] [blame] | 1686 | not_after_max = datetime.utcnow() + timedelta(seconds=100) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1687 | assert not_after_min <= not_after <= not_after_max |
Jean-Paul Calderone | ccf9d48 | 2010-07-30 22:36:42 -0400 | [diff] [blame] | 1688 | |
Jean-Paul Calderone | ccf9d48 | 2010-07-30 22:36:42 -0400 | [diff] [blame] | 1689 | def test_has_expired(self): |
| 1690 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1691 | `X509.has_expired` returns `True` if the certificate's not-after time |
| 1692 | is in the past. |
Jean-Paul Calderone | ccf9d48 | 2010-07-30 22:36:42 -0400 | [diff] [blame] | 1693 | """ |
| 1694 | cert = X509() |
| 1695 | cert.gmtime_adj_notAfter(-1) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1696 | assert cert.has_expired() |
Jean-Paul Calderone | ccf9d48 | 2010-07-30 22:36:42 -0400 | [diff] [blame] | 1697 | |
Jean-Paul Calderone | ccf9d48 | 2010-07-30 22:36:42 -0400 | [diff] [blame] | 1698 | def test_has_not_expired(self): |
| 1699 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1700 | `X509.has_expired` returns `False` if the certificate's not-after time |
| 1701 | is in the future. |
Jean-Paul Calderone | ccf9d48 | 2010-07-30 22:36:42 -0400 | [diff] [blame] | 1702 | """ |
| 1703 | cert = X509() |
| 1704 | cert.gmtime_adj_notAfter(2) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1705 | assert not cert.has_expired() |
Jean-Paul Calderone | ccf9d48 | 2010-07-30 22:36:42 -0400 | [diff] [blame] | 1706 | |
Jeff Tang | fc18f7b | 2015-04-15 17:42:33 -0400 | [diff] [blame] | 1707 | def test_root_has_not_expired(self): |
| 1708 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1709 | `X509.has_expired` returns `False` if the certificate's not-after time |
| 1710 | is in the future. |
Jeff Tang | fc18f7b | 2015-04-15 17:42:33 -0400 | [diff] [blame] | 1711 | """ |
| 1712 | cert = load_certificate(FILETYPE_PEM, root_cert_pem) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1713 | assert not cert.has_expired() |
Jeff Tang | fc18f7b | 2015-04-15 17:42:33 -0400 | [diff] [blame] | 1714 | |
Rick Dean | 38a05c8 | 2009-07-18 01:41:30 -0500 | [diff] [blame] | 1715 | def test_digest(self): |
| 1716 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1717 | `X509.digest` returns a string giving ":"-separated hex-encoded |
Alex Gaynor | 3128750 | 2015-09-05 16:11:27 -0400 | [diff] [blame] | 1718 | words of the digest of the certificate. |
Rick Dean | 38a05c8 | 2009-07-18 01:41:30 -0500 | [diff] [blame] | 1719 | """ |
Paul Kehrer | a40898b | 2017-06-11 16:30:58 -1000 | [diff] [blame] | 1720 | cert = load_certificate(FILETYPE_PEM, old_root_cert_pem) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1721 | assert ( |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 1722 | # This is MD5 instead of GOOD_DIGEST because the digest algorithm |
| 1723 | # actually matters to the assertion (ie, another arbitrary, good |
| 1724 | # digest will not product the same digest). |
Jeff Tang | fc18f7b | 2015-04-15 17:42:33 -0400 | [diff] [blame] | 1725 | # Digest verified with the command: |
| 1726 | # openssl x509 -in root_cert.pem -noout -fingerprint -md5 |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1727 | cert.digest("MD5") == |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 1728 | b"19:B3:05:26:2B:F8:F2:FF:0B:8F:21:07:A8:28:B8:75") |
Rick Dean | 38a05c8 | 2009-07-18 01:41:30 -0500 | [diff] [blame] | 1729 | |
Jean-Paul Calderone | 83a593d | 2011-04-01 17:45:07 -0400 | [diff] [blame] | 1730 | def _extcert(self, pkey, extensions): |
| 1731 | cert = X509() |
David Benjamin | 6b79947 | 2020-06-24 17:14:16 -0400 | [diff] [blame^] | 1732 | # Certificates with extensions must be X.509v3, which is encoded with a |
| 1733 | # version of two. |
| 1734 | cert.set_version(2) |
Jean-Paul Calderone | 83a593d | 2011-04-01 17:45:07 -0400 | [diff] [blame] | 1735 | cert.set_pubkey(pkey) |
| 1736 | cert.get_subject().commonName = "Unit Tests" |
| 1737 | cert.get_issuer().commonName = "Unit Tests" |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 1738 | when = datetime.now().strftime("%Y%m%d%H%M%SZ").encode("ascii") |
Jean-Paul Calderone | 83a593d | 2011-04-01 17:45:07 -0400 | [diff] [blame] | 1739 | cert.set_notBefore(when) |
| 1740 | cert.set_notAfter(when) |
| 1741 | |
| 1742 | cert.add_extensions(extensions) |
Jeff Tang | fc18f7b | 2015-04-15 17:42:33 -0400 | [diff] [blame] | 1743 | cert.sign(pkey, 'sha1') |
Jean-Paul Calderone | 83a593d | 2011-04-01 17:45:07 -0400 | [diff] [blame] | 1744 | return load_certificate( |
| 1745 | FILETYPE_PEM, dump_certificate(FILETYPE_PEM, cert)) |
| 1746 | |
Roland Hedberg | 7e4930e | 2008-04-22 22:58:50 +0200 | [diff] [blame] | 1747 | def test_extension_count(self): |
| 1748 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1749 | `X509.get_extension_count` returns the number of extensions |
Alex Gaynor | 3128750 | 2015-09-05 16:11:27 -0400 | [diff] [blame] | 1750 | that are present in the certificate. |
Roland Hedberg | 7e4930e | 2008-04-22 22:58:50 +0200 | [diff] [blame] | 1751 | """ |
Jean-Paul Calderone | f7b3ee6 | 2011-04-01 17:36:24 -0400 | [diff] [blame] | 1752 | pkey = load_privatekey(FILETYPE_PEM, client_key_pem) |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 1753 | ca = X509Extension(b'basicConstraints', True, b'CA:FALSE') |
| 1754 | key = X509Extension(b'keyUsage', True, b'digitalSignature') |
Jean-Paul Calderone | f7b3ee6 | 2011-04-01 17:36:24 -0400 | [diff] [blame] | 1755 | subjectAltName = X509Extension( |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 1756 | b'subjectAltName', True, b'DNS:example.com') |
Jean-Paul Calderone | f7b3ee6 | 2011-04-01 17:36:24 -0400 | [diff] [blame] | 1757 | |
| 1758 | # Try a certificate with no extensions at all. |
Jean-Paul Calderone | 83a593d | 2011-04-01 17:45:07 -0400 | [diff] [blame] | 1759 | c = self._extcert(pkey, []) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1760 | assert c.get_extension_count() == 0 |
Jean-Paul Calderone | f7b3ee6 | 2011-04-01 17:36:24 -0400 | [diff] [blame] | 1761 | |
| 1762 | # And a certificate with one |
Jean-Paul Calderone | 83a593d | 2011-04-01 17:45:07 -0400 | [diff] [blame] | 1763 | c = self._extcert(pkey, [ca]) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1764 | assert c.get_extension_count() == 1 |
Jean-Paul Calderone | f7b3ee6 | 2011-04-01 17:36:24 -0400 | [diff] [blame] | 1765 | |
| 1766 | # And a certificate with several |
Jean-Paul Calderone | 83a593d | 2011-04-01 17:45:07 -0400 | [diff] [blame] | 1767 | c = self._extcert(pkey, [ca, key, subjectAltName]) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1768 | assert c.get_extension_count() == 3 |
Roland Hedberg | 7e4930e | 2008-04-22 22:58:50 +0200 | [diff] [blame] | 1769 | |
Jean-Paul Calderone | 83a593d | 2011-04-01 17:45:07 -0400 | [diff] [blame] | 1770 | def test_get_extension(self): |
| 1771 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1772 | `X509.get_extension` takes an integer and returns an |
| 1773 | `X509Extension` corresponding to the extension at that index. |
Jean-Paul Calderone | 83a593d | 2011-04-01 17:45:07 -0400 | [diff] [blame] | 1774 | """ |
| 1775 | pkey = load_privatekey(FILETYPE_PEM, client_key_pem) |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 1776 | ca = X509Extension(b'basicConstraints', True, b'CA:FALSE') |
| 1777 | key = X509Extension(b'keyUsage', True, b'digitalSignature') |
Jean-Paul Calderone | 83a593d | 2011-04-01 17:45:07 -0400 | [diff] [blame] | 1778 | subjectAltName = X509Extension( |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 1779 | b'subjectAltName', False, b'DNS:example.com') |
Jean-Paul Calderone | 83a593d | 2011-04-01 17:45:07 -0400 | [diff] [blame] | 1780 | |
| 1781 | cert = self._extcert(pkey, [ca, key, subjectAltName]) |
| 1782 | |
| 1783 | ext = cert.get_extension(0) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1784 | assert isinstance(ext, X509Extension) |
| 1785 | assert ext.get_critical() |
| 1786 | assert ext.get_short_name() == b'basicConstraints' |
Jean-Paul Calderone | 83a593d | 2011-04-01 17:45:07 -0400 | [diff] [blame] | 1787 | |
| 1788 | ext = cert.get_extension(1) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1789 | assert isinstance(ext, X509Extension) |
| 1790 | assert ext.get_critical() |
| 1791 | assert ext.get_short_name() == b'keyUsage' |
Jean-Paul Calderone | 83a593d | 2011-04-01 17:45:07 -0400 | [diff] [blame] | 1792 | |
| 1793 | ext = cert.get_extension(2) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1794 | assert isinstance(ext, X509Extension) |
| 1795 | assert not ext.get_critical() |
| 1796 | assert ext.get_short_name() == b'subjectAltName' |
Jean-Paul Calderone | 83a593d | 2011-04-01 17:45:07 -0400 | [diff] [blame] | 1797 | |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1798 | with pytest.raises(IndexError): |
| 1799 | cert.get_extension(-1) |
| 1800 | with pytest.raises(IndexError): |
| 1801 | cert.get_extension(4) |
| 1802 | with pytest.raises(TypeError): |
| 1803 | cert.get_extension("hello") |
Jean-Paul Calderone | 83a593d | 2011-04-01 17:45:07 -0400 | [diff] [blame] | 1804 | |
Jean-Paul Calderone | 4bf75c6 | 2013-08-23 15:39:53 -0400 | [diff] [blame] | 1805 | def test_nullbyte_subjectAltName(self): |
Jean-Paul Calderone | ff83cdd | 2013-08-12 18:05:51 -0400 | [diff] [blame] | 1806 | """ |
Jean-Paul Calderone | 9af07b0 | 2013-08-23 16:07:31 -0400 | [diff] [blame] | 1807 | The fields of a `subjectAltName` extension on an X509 may contain NUL |
Jean-Paul Calderone | 4bf75c6 | 2013-08-23 15:39:53 -0400 | [diff] [blame] | 1808 | bytes and this value is reflected in the string representation of the |
| 1809 | extension object. |
Jean-Paul Calderone | ff83cdd | 2013-08-12 18:05:51 -0400 | [diff] [blame] | 1810 | """ |
Jean-Paul Calderone | 4bf75c6 | 2013-08-23 15:39:53 -0400 | [diff] [blame] | 1811 | cert = load_certificate(FILETYPE_PEM, nulbyteSubjectAltNamePEM) |
Jean-Paul Calderone | ff83cdd | 2013-08-12 18:05:51 -0400 | [diff] [blame] | 1812 | |
| 1813 | ext = cert.get_extension(3) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1814 | assert ext.get_short_name() == b'subjectAltName' |
| 1815 | assert ( |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 1816 | b"DNS:altnull.python.org\x00example.com, " |
| 1817 | b"email:null@python.org\x00user@example.org, " |
| 1818 | b"URI:http://null.python.org\x00http://example.org, " |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1819 | b"IP Address:192.0.2.1, IP Address:2001:DB8:0:0:0:0:0:1\n" == |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 1820 | str(ext).encode("ascii")) |
Jean-Paul Calderone | 4bf75c6 | 2013-08-23 15:39:53 -0400 | [diff] [blame] | 1821 | |
Jean-Paul Calderone | fe1b9bd | 2010-08-03 18:00:21 -0400 | [diff] [blame] | 1822 | def test_invalid_digest_algorithm(self): |
| 1823 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1824 | `X509.digest` raises `ValueError` if called with an unrecognized hash |
| 1825 | algorithm. |
Jean-Paul Calderone | fe1b9bd | 2010-08-03 18:00:21 -0400 | [diff] [blame] | 1826 | """ |
| 1827 | cert = X509() |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1828 | with pytest.raises(ValueError): |
| 1829 | cert.digest(BAD_DIGEST) |
Jean-Paul Calderone | 4f237b2 | 2010-07-30 22:29:39 -0400 | [diff] [blame] | 1830 | |
Jean-Paul Calderone | 4f237b2 | 2010-07-30 22:29:39 -0400 | [diff] [blame] | 1831 | def test_get_subject(self): |
| 1832 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1833 | `X509.get_subject` returns an `X509Name` instance. |
Jean-Paul Calderone | 4f237b2 | 2010-07-30 22:29:39 -0400 | [diff] [blame] | 1834 | """ |
| 1835 | cert = load_certificate(FILETYPE_PEM, self.pemData) |
| 1836 | subj = cert.get_subject() |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1837 | assert isinstance(subj, X509Name) |
| 1838 | assert ( |
| 1839 | subj.get_components() == |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 1840 | [(b'C', b'US'), (b'ST', b'IL'), (b'L', b'Chicago'), |
| 1841 | (b'O', b'Testing'), (b'CN', b'Testing Root CA')]) |
Jean-Paul Calderone | 4f237b2 | 2010-07-30 22:29:39 -0400 | [diff] [blame] | 1842 | |
Jean-Paul Calderone | 4f237b2 | 2010-07-30 22:29:39 -0400 | [diff] [blame] | 1843 | def test_set_subject_wrong_args(self): |
| 1844 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1845 | `X509.set_subject` raises a `TypeError` if called with an argument not |
| 1846 | of type `X509Name`. |
Jean-Paul Calderone | 4f237b2 | 2010-07-30 22:29:39 -0400 | [diff] [blame] | 1847 | """ |
| 1848 | cert = X509() |
Alex Gaynor | 85b4970 | 2015-09-05 16:30:59 -0400 | [diff] [blame] | 1849 | with pytest.raises(TypeError): |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1850 | cert.set_subject(None) |
Jean-Paul Calderone | 4f237b2 | 2010-07-30 22:29:39 -0400 | [diff] [blame] | 1851 | |
| 1852 | def test_set_subject(self): |
| 1853 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1854 | `X509.set_subject` changes the subject of the certificate to the one |
| 1855 | passed in. |
Jean-Paul Calderone | 4f237b2 | 2010-07-30 22:29:39 -0400 | [diff] [blame] | 1856 | """ |
| 1857 | cert = X509() |
| 1858 | name = cert.get_subject() |
| 1859 | name.C = 'AU' |
Alex Gaynor | c3697ad | 2017-11-20 08:19:32 -0500 | [diff] [blame] | 1860 | name.OU = 'Unit Tests' |
Jean-Paul Calderone | 4f237b2 | 2010-07-30 22:29:39 -0400 | [diff] [blame] | 1861 | cert.set_subject(name) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1862 | assert ( |
| 1863 | cert.get_subject().get_components() == |
Alex Gaynor | c3697ad | 2017-11-20 08:19:32 -0500 | [diff] [blame] | 1864 | [(b'C', b'AU'), (b'OU', b'Unit Tests')]) |
Jean-Paul Calderone | 4f237b2 | 2010-07-30 22:29:39 -0400 | [diff] [blame] | 1865 | |
Jean-Paul Calderone | 4f237b2 | 2010-07-30 22:29:39 -0400 | [diff] [blame] | 1866 | def test_get_issuer(self): |
| 1867 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1868 | `X509.get_issuer` returns an `X509Name` instance. |
Jean-Paul Calderone | 4f237b2 | 2010-07-30 22:29:39 -0400 | [diff] [blame] | 1869 | """ |
| 1870 | cert = load_certificate(FILETYPE_PEM, self.pemData) |
| 1871 | subj = cert.get_issuer() |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1872 | assert isinstance(subj, X509Name) |
Jean-Paul Calderone | 30a4cb3 | 2010-08-11 23:54:12 -0400 | [diff] [blame] | 1873 | comp = subj.get_components() |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1874 | assert ( |
| 1875 | comp == |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 1876 | [(b'C', b'US'), (b'ST', b'IL'), (b'L', b'Chicago'), |
| 1877 | (b'O', b'Testing'), (b'CN', b'Testing Root CA')]) |
Jean-Paul Calderone | 4f237b2 | 2010-07-30 22:29:39 -0400 | [diff] [blame] | 1878 | |
Jean-Paul Calderone | 4f237b2 | 2010-07-30 22:29:39 -0400 | [diff] [blame] | 1879 | def test_set_issuer_wrong_args(self): |
| 1880 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1881 | `X509.set_issuer` raises a `TypeError` if called with an argument not |
| 1882 | of type `X509Name`. |
Jean-Paul Calderone | 4f237b2 | 2010-07-30 22:29:39 -0400 | [diff] [blame] | 1883 | """ |
| 1884 | cert = X509() |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1885 | with pytest.raises(TypeError): |
| 1886 | cert.set_issuer(None) |
Jean-Paul Calderone | 4f237b2 | 2010-07-30 22:29:39 -0400 | [diff] [blame] | 1887 | |
Jean-Paul Calderone | 4f237b2 | 2010-07-30 22:29:39 -0400 | [diff] [blame] | 1888 | def test_set_issuer(self): |
| 1889 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1890 | `X509.set_issuer` changes the issuer of the certificate to the |
Alex Gaynor | 3128750 | 2015-09-05 16:11:27 -0400 | [diff] [blame] | 1891 | one passed in. |
Jean-Paul Calderone | 4f237b2 | 2010-07-30 22:29:39 -0400 | [diff] [blame] | 1892 | """ |
| 1893 | cert = X509() |
| 1894 | name = cert.get_issuer() |
| 1895 | name.C = 'AU' |
Alex Gaynor | c3697ad | 2017-11-20 08:19:32 -0500 | [diff] [blame] | 1896 | name.OU = 'Unit Tests' |
Jean-Paul Calderone | 4f237b2 | 2010-07-30 22:29:39 -0400 | [diff] [blame] | 1897 | cert.set_issuer(name) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1898 | assert ( |
| 1899 | cert.get_issuer().get_components() == |
Alex Gaynor | c3697ad | 2017-11-20 08:19:32 -0500 | [diff] [blame] | 1900 | [(b'C', b'AU'), (b'OU', b'Unit Tests')]) |
Jean-Paul Calderone | 4f237b2 | 2010-07-30 22:29:39 -0400 | [diff] [blame] | 1901 | |
Jean-Paul Calderone | 4f237b2 | 2010-07-30 22:29:39 -0400 | [diff] [blame] | 1902 | def test_get_pubkey_uninitialized(self): |
| 1903 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1904 | When called on a certificate with no public key, `X509.get_pubkey` |
| 1905 | raises `OpenSSL.crypto.Error`. |
Jean-Paul Calderone | 4f237b2 | 2010-07-30 22:29:39 -0400 | [diff] [blame] | 1906 | """ |
| 1907 | cert = X509() |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1908 | with pytest.raises(Error): |
| 1909 | cert.get_pubkey() |
Jean-Paul Calderone | 4f237b2 | 2010-07-30 22:29:39 -0400 | [diff] [blame] | 1910 | |
Alex Gaynor | 7778e79 | 2016-07-03 23:38:48 -0400 | [diff] [blame] | 1911 | def test_set_pubkey_wrong_type(self): |
| 1912 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1913 | `X509.set_pubkey` raises `TypeError` when given an object of the |
| 1914 | wrong type. |
Alex Gaynor | 7778e79 | 2016-07-03 23:38:48 -0400 | [diff] [blame] | 1915 | """ |
| 1916 | cert = X509() |
| 1917 | with pytest.raises(TypeError): |
| 1918 | cert.set_pubkey(object()) |
| 1919 | |
Jean-Paul Calderone | ccf9d48 | 2010-07-30 22:36:42 -0400 | [diff] [blame] | 1920 | def test_subject_name_hash(self): |
| 1921 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1922 | `X509.subject_name_hash` returns the hash of the certificate's |
Alex Gaynor | 3128750 | 2015-09-05 16:11:27 -0400 | [diff] [blame] | 1923 | subject name. |
Jean-Paul Calderone | ccf9d48 | 2010-07-30 22:36:42 -0400 | [diff] [blame] | 1924 | """ |
| 1925 | cert = load_certificate(FILETYPE_PEM, self.pemData) |
Alex Gaynor | 4cb0520 | 2019-02-02 11:06:41 -0500 | [diff] [blame] | 1926 | # SHA1 |
| 1927 | assert cert.subject_name_hash() == 3278919224 |
Jean-Paul Calderone | ccf9d48 | 2010-07-30 22:36:42 -0400 | [diff] [blame] | 1928 | |
Jean-Paul Calderone | 2755fa5 | 2011-05-18 19:42:10 -0400 | [diff] [blame] | 1929 | def test_get_signature_algorithm(self): |
| 1930 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1931 | `X509.get_signature_algorithm` returns a string which means |
Jean-Paul Calderone | 2755fa5 | 2011-05-18 19:42:10 -0400 | [diff] [blame] | 1932 | the algorithm used to sign the certificate. |
| 1933 | """ |
| 1934 | cert = load_certificate(FILETYPE_PEM, self.pemData) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1935 | assert b"sha1WithRSAEncryption" == cert.get_signature_algorithm() |
Jean-Paul Calderone | 2755fa5 | 2011-05-18 19:42:10 -0400 | [diff] [blame] | 1936 | |
Jean-Paul Calderone | 2755fa5 | 2011-05-18 19:42:10 -0400 | [diff] [blame] | 1937 | def test_get_undefined_signature_algorithm(self): |
Jean-Paul Calderone | 5d8e405 | 2011-05-19 17:51:43 -0400 | [diff] [blame] | 1938 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1939 | `X509.get_signature_algorithm` raises `ValueError` if the signature |
| 1940 | algorithm is undefined or unknown. |
Jean-Paul Calderone | 5d8e405 | 2011-05-19 17:51:43 -0400 | [diff] [blame] | 1941 | """ |
| 1942 | # This certificate has been modified to indicate a bogus OID in the |
| 1943 | # signature algorithm field so that OpenSSL does not recognize it. |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 1944 | certPEM = b"""\ |
Jean-Paul Calderone | 2755fa5 | 2011-05-18 19:42:10 -0400 | [diff] [blame] | 1945 | -----BEGIN CERTIFICATE----- |
| 1946 | MIIC/zCCAmigAwIBAgIBATAGBgJ8BQUAMHsxCzAJBgNVBAYTAlNHMREwDwYDVQQK |
| 1947 | EwhNMkNyeXB0bzEUMBIGA1UECxMLTTJDcnlwdG8gQ0ExJDAiBgNVBAMTG00yQ3J5 |
| 1948 | cHRvIENlcnRpZmljYXRlIE1hc3RlcjEdMBsGCSqGSIb3DQEJARYObmdwc0Bwb3N0 |
| 1949 | MS5jb20wHhcNMDAwOTEwMDk1MTMwWhcNMDIwOTEwMDk1MTMwWjBTMQswCQYDVQQG |
| 1950 | EwJTRzERMA8GA1UEChMITTJDcnlwdG8xEjAQBgNVBAMTCWxvY2FsaG9zdDEdMBsG |
| 1951 | CSqGSIb3DQEJARYObmdwc0Bwb3N0MS5jb20wXDANBgkqhkiG9w0BAQEFAANLADBI |
| 1952 | AkEArL57d26W9fNXvOhNlZzlPOACmvwOZ5AdNgLzJ1/MfsQQJ7hHVeHmTAjM664V |
| 1953 | +fXvwUGJLziCeBo1ysWLRnl8CQIDAQABo4IBBDCCAQAwCQYDVR0TBAIwADAsBglg |
| 1954 | hkgBhvhCAQ0EHxYdT3BlblNTTCBHZW5lcmF0ZWQgQ2VydGlmaWNhdGUwHQYDVR0O |
| 1955 | BBYEFM+EgpK+eyZiwFU1aOPSbczbPSpVMIGlBgNVHSMEgZ0wgZqAFPuHI2nrnDqT |
| 1956 | FeXFvylRT/7tKDgBoX+kfTB7MQswCQYDVQQGEwJTRzERMA8GA1UEChMITTJDcnlw |
| 1957 | dG8xFDASBgNVBAsTC00yQ3J5cHRvIENBMSQwIgYDVQQDExtNMkNyeXB0byBDZXJ0 |
| 1958 | aWZpY2F0ZSBNYXN0ZXIxHTAbBgkqhkiG9w0BCQEWDm5ncHNAcG9zdDEuY29tggEA |
| 1959 | MA0GCSqGSIb3DQEBBAUAA4GBADv8KpPo+gfJxN2ERK1Y1l17sz/ZhzoGgm5XCdbx |
| 1960 | jEY7xKfpQngV599k1xhl11IMqizDwu0855agrckg2MCTmOI9DZzDD77tAYb+Dk0O |
| 1961 | PEVk0Mk/V0aIsDE9bolfCi/i/QWZ3N8s5nTWMNyBBBmoSliWCm4jkkRZRD0ejgTN |
| 1962 | tgI5 |
| 1963 | -----END CERTIFICATE----- |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 1964 | """ |
Jean-Paul Calderone | 2755fa5 | 2011-05-18 19:42:10 -0400 | [diff] [blame] | 1965 | cert = load_certificate(FILETYPE_PEM, certPEM) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1966 | with pytest.raises(ValueError): |
| 1967 | cert.get_signature_algorithm() |
Jean-Paul Calderone | ccf9d48 | 2010-07-30 22:36:42 -0400 | [diff] [blame] | 1968 | |
Alex Gaynor | 3772611 | 2016-07-04 09:51:32 -0400 | [diff] [blame] | 1969 | def test_sign_bad_pubkey_type(self): |
| 1970 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 1971 | `X509.sign` raises `TypeError` when called with the wrong type. |
Alex Gaynor | 3772611 | 2016-07-04 09:51:32 -0400 | [diff] [blame] | 1972 | """ |
| 1973 | cert = X509() |
| 1974 | with pytest.raises(TypeError): |
| 1975 | cert.sign(object(), b"sha256") |
| 1976 | |
Alex Gaynor | 9939ba1 | 2017-06-25 16:28:24 -0400 | [diff] [blame] | 1977 | def test_convert_from_cryptography(self): |
| 1978 | crypto_cert = x509.load_pem_x509_certificate( |
| 1979 | intermediate_cert_pem, backend |
| 1980 | ) |
| 1981 | cert = X509.from_cryptography(crypto_cert) |
| 1982 | |
| 1983 | assert isinstance(cert, X509) |
| 1984 | assert cert.get_version() == crypto_cert.version.value |
| 1985 | |
| 1986 | def test_convert_from_cryptography_unsupported_type(self): |
| 1987 | with pytest.raises(TypeError): |
| 1988 | X509.from_cryptography(object()) |
| 1989 | |
| 1990 | def test_convert_to_cryptography_key(self): |
| 1991 | cert = load_certificate(FILETYPE_PEM, intermediate_cert_pem) |
| 1992 | crypto_cert = cert.to_cryptography() |
| 1993 | |
| 1994 | assert isinstance(crypto_cert, x509.Certificate) |
| 1995 | assert crypto_cert.version.value == cert.get_version() |
| 1996 | |
Jean-Paul Calderone | ccf9d48 | 2010-07-30 22:36:42 -0400 | [diff] [blame] | 1997 | |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 1998 | class TestX509Store(object): |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1999 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 2000 | Test for `OpenSSL.crypto.X509Store`. |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2001 | """ |
Alex Gaynor | aceb3e2 | 2015-09-05 12:00:22 -0400 | [diff] [blame] | 2002 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2003 | def test_type(self): |
| 2004 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 2005 | `X509Store` is a type object. |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2006 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 2007 | assert is_consistent_type(X509Store, 'X509Store') |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2008 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2009 | def test_add_cert(self): |
Jean-Paul Calderone | e6f32b8 | 2013-03-06 10:27:57 -0800 | [diff] [blame] | 2010 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 2011 | `X509Store.add_cert` adds a `X509` instance to the certificate store. |
Jean-Paul Calderone | e6f32b8 | 2013-03-06 10:27:57 -0800 | [diff] [blame] | 2012 | """ |
| 2013 | cert = load_certificate(FILETYPE_PEM, cleartextCertificatePEM) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2014 | store = X509Store() |
Jean-Paul Calderone | e6f32b8 | 2013-03-06 10:27:57 -0800 | [diff] [blame] | 2015 | store.add_cert(cert) |
| 2016 | |
Alex Chan | fb078d8 | 2017-04-20 11:16:15 +0100 | [diff] [blame] | 2017 | @pytest.mark.parametrize('cert', [None, 1.0, 'cert', object()]) |
| 2018 | def test_add_cert_wrong_args(self, cert): |
| 2019 | """ |
| 2020 | `X509Store.add_cert` raises `TypeError` if passed a non-X509 object |
| 2021 | as its first argument. |
| 2022 | """ |
| 2023 | store = X509Store() |
| 2024 | with pytest.raises(TypeError): |
| 2025 | store.add_cert(cert) |
| 2026 | |
Paul Kehrer | 0e6c553 | 2018-08-23 10:52:15 -0500 | [diff] [blame] | 2027 | def test_add_cert_accepts_duplicate(self): |
Jean-Paul Calderone | e6f32b8 | 2013-03-06 10:27:57 -0800 | [diff] [blame] | 2028 | """ |
Paul Kehrer | 0e6c553 | 2018-08-23 10:52:15 -0500 | [diff] [blame] | 2029 | `X509Store.add_cert` doesn't raise `OpenSSL.crypto.Error` if an attempt |
| 2030 | is made to add the same certificate to the store more than once. |
Jean-Paul Calderone | e6f32b8 | 2013-03-06 10:27:57 -0800 | [diff] [blame] | 2031 | """ |
| 2032 | cert = load_certificate(FILETYPE_PEM, cleartextCertificatePEM) |
| 2033 | store = X509Store() |
| 2034 | store.add_cert(cert) |
Paul Kehrer | 0e6c553 | 2018-08-23 10:52:15 -0500 | [diff] [blame] | 2035 | store.add_cert(cert) |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2036 | |
| 2037 | |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2038 | class TestPKCS12(object): |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 2039 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2040 | Test for `OpenSSL.crypto.PKCS12` and `OpenSSL.crypto.load_pkcs12`. |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 2041 | """ |
| 2042 | pemData = cleartextCertificatePEM + cleartextPrivateKeyPEM |
| 2043 | |
Jean-Paul Calderone | c3a41f7 | 2009-07-25 12:36:02 -0400 | [diff] [blame] | 2044 | def test_type(self): |
| 2045 | """ |
Alex Gaynor | 01f90a1 | 2019-02-07 09:14:48 -0500 | [diff] [blame] | 2046 | `PKCS12` is a type object. |
Jean-Paul Calderone | c3a41f7 | 2009-07-25 12:36:02 -0400 | [diff] [blame] | 2047 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2048 | assert is_consistent_type(PKCS12, 'PKCS12') |
Jean-Paul Calderone | c3a41f7 | 2009-07-25 12:36:02 -0400 | [diff] [blame] | 2049 | |
Rick Dean | f94096c | 2009-07-18 14:23:06 -0500 | [diff] [blame] | 2050 | def test_empty_construction(self): |
Rick Dean | 38a05c8 | 2009-07-18 01:41:30 -0500 | [diff] [blame] | 2051 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2052 | `PKCS12` returns a new instance of `PKCS12` with no certificate, |
| 2053 | private key, CA certificates, or friendly name. |
Rick Dean | 38a05c8 | 2009-07-18 01:41:30 -0500 | [diff] [blame] | 2054 | """ |
Jean-Paul Calderone | a202edb | 2009-07-25 12:22:12 -0400 | [diff] [blame] | 2055 | p12 = PKCS12() |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2056 | assert None is p12.get_certificate() |
| 2057 | assert None is p12.get_privatekey() |
| 2058 | assert None is p12.get_ca_certificates() |
| 2059 | assert None is p12.get_friendlyname() |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 2060 | |
| 2061 | def test_type_errors(self): |
Rick Dean | 38a05c8 | 2009-07-18 01:41:30 -0500 | [diff] [blame] | 2062 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2063 | The `PKCS12` setter functions (`set_certificate`, `set_privatekey`, |
| 2064 | `set_ca_certificates`, and `set_friendlyname`) raise `TypeError` |
| 2065 | when passed objects of types other than those expected. |
Rick Dean | 38a05c8 | 2009-07-18 01:41:30 -0500 | [diff] [blame] | 2066 | """ |
Jean-Paul Calderone | a202edb | 2009-07-25 12:22:12 -0400 | [diff] [blame] | 2067 | p12 = PKCS12() |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2068 | for bad_arg in [3, PKey(), X509]: |
| 2069 | with pytest.raises(TypeError): |
| 2070 | p12.set_certificate(bad_arg) |
| 2071 | for bad_arg in [3, 'legbone', X509()]: |
| 2072 | with pytest.raises(TypeError): |
| 2073 | p12.set_privatekey(bad_arg) |
| 2074 | for bad_arg in [3, X509(), (3, 4), (PKey(),)]: |
| 2075 | with pytest.raises(TypeError): |
| 2076 | p12.set_ca_certificates(bad_arg) |
| 2077 | for bad_arg in [6, ('foo', 'bar')]: |
| 2078 | with pytest.raises(TypeError): |
| 2079 | p12.set_friendlyname(bad_arg) |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 2080 | |
| 2081 | def test_key_only(self): |
| 2082 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2083 | A `PKCS12` with only a private key can be exported using |
| 2084 | `PKCS12.export` and loaded again using `load_pkcs12`. |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 2085 | """ |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 2086 | passwd = b"blah" |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 2087 | p12 = PKCS12() |
Jean-Paul Calderone | a202edb | 2009-07-25 12:22:12 -0400 | [diff] [blame] | 2088 | pkey = load_privatekey(FILETYPE_PEM, cleartextPrivateKeyPEM) |
Jean-Paul Calderone | 7426ed8 | 2009-07-25 21:19:23 -0400 | [diff] [blame] | 2089 | p12.set_privatekey(pkey) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2090 | assert None is p12.get_certificate() |
| 2091 | assert pkey == p12.get_privatekey() |
Rick Dean | 321a051 | 2009-08-13 17:21:29 -0500 | [diff] [blame] | 2092 | try: |
| 2093 | dumped_p12 = p12.export(passphrase=passwd, iter=2, maciter=3) |
| 2094 | except Error: |
| 2095 | # Some versions of OpenSSL will throw an exception |
| 2096 | # for this nearly useless PKCS12 we tried to generate: |
| 2097 | # [('PKCS12 routines', 'PKCS12_create', 'invalid null argument')] |
| 2098 | return |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 2099 | p12 = load_pkcs12(dumped_p12, passwd) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2100 | assert None is p12.get_ca_certificates() |
| 2101 | assert None is p12.get_certificate() |
Jean-Paul Calderone | 7426ed8 | 2009-07-25 21:19:23 -0400 | [diff] [blame] | 2102 | |
| 2103 | # OpenSSL fails to bring the key back to us. So sad. Perhaps in the |
| 2104 | # future this will be improved. |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2105 | assert isinstance(p12.get_privatekey(), (PKey, type(None))) |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 2106 | |
| 2107 | def test_cert_only(self): |
| 2108 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2109 | A `PKCS12` with only a certificate can be exported using |
| 2110 | `PKCS12.export` and loaded again using `load_pkcs12`. |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 2111 | """ |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 2112 | passwd = b"blah" |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 2113 | p12 = PKCS12() |
Jean-Paul Calderone | a202edb | 2009-07-25 12:22:12 -0400 | [diff] [blame] | 2114 | cert = load_certificate(FILETYPE_PEM, cleartextCertificatePEM) |
Jean-Paul Calderone | 7426ed8 | 2009-07-25 21:19:23 -0400 | [diff] [blame] | 2115 | p12.set_certificate(cert) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2116 | assert cert == p12.get_certificate() |
| 2117 | assert None is p12.get_privatekey() |
Rick Dean | 321a051 | 2009-08-13 17:21:29 -0500 | [diff] [blame] | 2118 | try: |
| 2119 | dumped_p12 = p12.export(passphrase=passwd, iter=2, maciter=3) |
| 2120 | except Error: |
| 2121 | # Some versions of OpenSSL will throw an exception |
| 2122 | # for this nearly useless PKCS12 we tried to generate: |
| 2123 | # [('PKCS12 routines', 'PKCS12_create', 'invalid null argument')] |
| 2124 | return |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 2125 | p12 = load_pkcs12(dumped_p12, passwd) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2126 | assert None is p12.get_privatekey() |
Jean-Paul Calderone | 7426ed8 | 2009-07-25 21:19:23 -0400 | [diff] [blame] | 2127 | |
| 2128 | # OpenSSL fails to bring the cert back to us. Groany mcgroan. |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2129 | assert isinstance(p12.get_certificate(), (X509, type(None))) |
Jean-Paul Calderone | 7426ed8 | 2009-07-25 21:19:23 -0400 | [diff] [blame] | 2130 | |
| 2131 | # Oh ho. It puts the certificate into the ca certificates list, in |
| 2132 | # fact. Totally bogus, I would think. Nevertheless, let's exploit |
| 2133 | # that to check to see if it reconstructed the certificate we expected |
| 2134 | # it to. At some point, hopefully this will change so that |
| 2135 | # p12.get_certificate() is actually what returns the loaded |
| 2136 | # certificate. |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2137 | assert ( |
| 2138 | cleartextCertificatePEM == |
Jean-Paul Calderone | 7426ed8 | 2009-07-25 21:19:23 -0400 | [diff] [blame] | 2139 | dump_certificate(FILETYPE_PEM, p12.get_ca_certificates()[0])) |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 2140 | |
Alex Gaynor | 3128750 | 2015-09-05 16:11:27 -0400 | [diff] [blame] | 2141 | def gen_pkcs12(self, cert_pem=None, key_pem=None, ca_pem=None, |
| 2142 | friendly_name=None): |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 2143 | """ |
Jean-Paul Calderone | 7426ed8 | 2009-07-25 21:19:23 -0400 | [diff] [blame] | 2144 | Generate a PKCS12 object with components from PEM. Verify that the set |
| 2145 | functions return None. |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 2146 | """ |
Rick Dean | f94096c | 2009-07-18 14:23:06 -0500 | [diff] [blame] | 2147 | p12 = PKCS12() |
| 2148 | if cert_pem: |
| 2149 | ret = p12.set_certificate(load_certificate(FILETYPE_PEM, cert_pem)) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2150 | assert ret is None |
Rick Dean | f94096c | 2009-07-18 14:23:06 -0500 | [diff] [blame] | 2151 | if key_pem: |
Jean-Paul Calderone | 7426ed8 | 2009-07-25 21:19:23 -0400 | [diff] [blame] | 2152 | ret = p12.set_privatekey(load_privatekey(FILETYPE_PEM, key_pem)) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2153 | assert ret is None |
Rick Dean | f94096c | 2009-07-18 14:23:06 -0500 | [diff] [blame] | 2154 | if ca_pem: |
Alex Gaynor | 85b4970 | 2015-09-05 16:30:59 -0400 | [diff] [blame] | 2155 | ret = p12.set_ca_certificates( |
| 2156 | (load_certificate(FILETYPE_PEM, ca_pem),) |
| 2157 | ) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2158 | assert ret is None |
Jean-Paul Calderone | 7426ed8 | 2009-07-25 21:19:23 -0400 | [diff] [blame] | 2159 | if friendly_name: |
| 2160 | ret = p12.set_friendlyname(friendly_name) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2161 | assert ret is None |
Rick Dean | f94096c | 2009-07-18 14:23:06 -0500 | [diff] [blame] | 2162 | return p12 |
| 2163 | |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 2164 | def check_recovery(self, p12_str, key=None, cert=None, ca=None, passwd=b"", |
Jean-Paul Calderone | 7426ed8 | 2009-07-25 21:19:23 -0400 | [diff] [blame] | 2165 | extra=()): |
Rick Dean | f94096c | 2009-07-18 14:23:06 -0500 | [diff] [blame] | 2166 | """ |
Jean-Paul Calderone | 7426ed8 | 2009-07-25 21:19:23 -0400 | [diff] [blame] | 2167 | Use openssl program to confirm three components are recoverable from a |
| 2168 | PKCS12 string. |
Rick Dean | f94096c | 2009-07-18 14:23:06 -0500 | [diff] [blame] | 2169 | """ |
| 2170 | if key: |
Jean-Paul Calderone | 7426ed8 | 2009-07-25 21:19:23 -0400 | [diff] [blame] | 2171 | recovered_key = _runopenssl( |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 2172 | p12_str, b"pkcs12", b"-nocerts", b"-nodes", b"-passin", |
| 2173 | b"pass:" + passwd, *extra) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2174 | assert recovered_key[-len(key):] == key |
Rick Dean | f94096c | 2009-07-18 14:23:06 -0500 | [diff] [blame] | 2175 | if cert: |
Jean-Paul Calderone | 7426ed8 | 2009-07-25 21:19:23 -0400 | [diff] [blame] | 2176 | recovered_cert = _runopenssl( |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 2177 | p12_str, b"pkcs12", b"-clcerts", b"-nodes", b"-passin", |
| 2178 | b"pass:" + passwd, b"-nokeys", *extra) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2179 | assert recovered_cert[-len(cert):] == cert |
Rick Dean | f94096c | 2009-07-18 14:23:06 -0500 | [diff] [blame] | 2180 | if ca: |
Jean-Paul Calderone | 7426ed8 | 2009-07-25 21:19:23 -0400 | [diff] [blame] | 2181 | recovered_cert = _runopenssl( |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 2182 | p12_str, b"pkcs12", b"-cacerts", b"-nodes", b"-passin", |
| 2183 | b"pass:" + passwd, b"-nokeys", *extra) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2184 | assert recovered_cert[-len(ca):] == ca |
Rick Dean | f94096c | 2009-07-18 14:23:06 -0500 | [diff] [blame] | 2185 | |
Stephen Holsapple | 3848262 | 2014-04-05 20:29:34 -0700 | [diff] [blame] | 2186 | def verify_pkcs12_container(self, p12): |
| 2187 | """ |
| 2188 | Verify that the PKCS#12 container contains the correct client |
| 2189 | certificate and private key. |
Jean-Paul Calderone | f0ff13b | 2014-05-05 12:48:33 -0400 | [diff] [blame] | 2190 | |
| 2191 | :param p12: The PKCS12 instance to verify. |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2192 | :type p12: `PKCS12` |
Stephen Holsapple | 3848262 | 2014-04-05 20:29:34 -0700 | [diff] [blame] | 2193 | """ |
| 2194 | cert_pem = dump_certificate(FILETYPE_PEM, p12.get_certificate()) |
| 2195 | key_pem = dump_privatekey(FILETYPE_PEM, p12.get_privatekey()) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2196 | assert ( |
| 2197 | (client_cert_pem, client_key_pem, None) == |
Jean-Paul Calderone | f0ff13b | 2014-05-05 12:48:33 -0400 | [diff] [blame] | 2198 | (cert_pem, key_pem, p12.get_ca_certificates())) |
Stephen Holsapple | 3848262 | 2014-04-05 20:29:34 -0700 | [diff] [blame] | 2199 | |
Rick Dean | f94096c | 2009-07-18 14:23:06 -0500 | [diff] [blame] | 2200 | def test_load_pkcs12(self): |
| 2201 | """ |
Jean-Paul Calderone | 7426ed8 | 2009-07-25 21:19:23 -0400 | [diff] [blame] | 2202 | A PKCS12 string generated using the openssl command line can be loaded |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2203 | with `load_pkcs12` and its components extracted and examined. |
Rick Dean | f94096c | 2009-07-18 14:23:06 -0500 | [diff] [blame] | 2204 | """ |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 2205 | passwd = b"whatever" |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 2206 | pem = client_key_pem + client_cert_pem |
Jean-Paul Calderone | 7426ed8 | 2009-07-25 21:19:23 -0400 | [diff] [blame] | 2207 | p12_str = _runopenssl( |
Alex Gaynor | 85b4970 | 2015-09-05 16:30:59 -0400 | [diff] [blame] | 2208 | pem, |
| 2209 | b"pkcs12", |
| 2210 | b"-export", |
| 2211 | b"-clcerts", |
| 2212 | b"-passout", |
| 2213 | b"pass:" + passwd |
| 2214 | ) |
Stephen Holsapple | 3848262 | 2014-04-05 20:29:34 -0700 | [diff] [blame] | 2215 | p12 = load_pkcs12(p12_str, passphrase=passwd) |
| 2216 | self.verify_pkcs12_container(p12) |
| 2217 | |
Abraham Martin | c5484ba | 2015-03-25 15:33:05 +0000 | [diff] [blame] | 2218 | def test_load_pkcs12_text_passphrase(self): |
| 2219 | """ |
| 2220 | A PKCS12 string generated using the openssl command line can be loaded |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2221 | with `load_pkcs12` and its components extracted and examined. |
Abraham Martin | c5484ba | 2015-03-25 15:33:05 +0000 | [diff] [blame] | 2222 | Using text as passphrase instead of bytes. DeprecationWarning expected. |
| 2223 | """ |
| 2224 | pem = client_key_pem + client_cert_pem |
| 2225 | passwd = b"whatever" |
| 2226 | p12_str = _runopenssl(pem, b"pkcs12", b"-export", b"-clcerts", |
| 2227 | b"-passout", b"pass:" + passwd) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2228 | with pytest.warns(DeprecationWarning) as w: |
Abraham Martin | c5484ba | 2015-03-25 15:33:05 +0000 | [diff] [blame] | 2229 | simplefilter("always") |
Jean-Paul Calderone | 13a0e65 | 2015-03-29 07:58:51 -0400 | [diff] [blame] | 2230 | p12 = load_pkcs12(p12_str, passphrase=b"whatever".decode("ascii")) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2231 | assert ( |
Jean-Paul Calderone | 13a0e65 | 2015-03-29 07:58:51 -0400 | [diff] [blame] | 2232 | "{0} for passphrase is no longer accepted, use bytes".format( |
Jean-Paul Calderone | 6462b07 | 2015-03-29 07:03:11 -0400 | [diff] [blame] | 2233 | WARNING_TYPE_EXPECTED |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2234 | ) == str(w[-1].message)) |
Jean-Paul Calderone | 6462b07 | 2015-03-29 07:03:11 -0400 | [diff] [blame] | 2235 | |
Abraham Martin | c5484ba | 2015-03-25 15:33:05 +0000 | [diff] [blame] | 2236 | self.verify_pkcs12_container(p12) |
| 2237 | |
Stephen Holsapple | 3848262 | 2014-04-05 20:29:34 -0700 | [diff] [blame] | 2238 | def test_load_pkcs12_no_passphrase(self): |
| 2239 | """ |
Jean-Paul Calderone | f0ff13b | 2014-05-05 12:48:33 -0400 | [diff] [blame] | 2240 | A PKCS12 string generated using openssl command line can be loaded with |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2241 | `load_pkcs12` without a passphrase and its components extracted |
Jean-Paul Calderone | f0ff13b | 2014-05-05 12:48:33 -0400 | [diff] [blame] | 2242 | and examined. |
Stephen Holsapple | 3848262 | 2014-04-05 20:29:34 -0700 | [diff] [blame] | 2243 | """ |
| 2244 | pem = client_key_pem + client_cert_pem |
| 2245 | p12_str = _runopenssl( |
| 2246 | pem, b"pkcs12", b"-export", b"-clcerts", b"-passout", b"pass:") |
| 2247 | p12 = load_pkcs12(p12_str) |
| 2248 | self.verify_pkcs12_container(p12) |
| 2249 | |
Stephen Holsapple | 3848262 | 2014-04-05 20:29:34 -0700 | [diff] [blame] | 2250 | def _dump_and_load(self, dump_passphrase, load_passphrase): |
| 2251 | """ |
| 2252 | A helper method to dump and load a PKCS12 object. |
| 2253 | """ |
| 2254 | p12 = self.gen_pkcs12(client_cert_pem, client_key_pem) |
| 2255 | dumped_p12 = p12.export(passphrase=dump_passphrase, iter=2, maciter=3) |
| 2256 | return load_pkcs12(dumped_p12, passphrase=load_passphrase) |
| 2257 | |
Stephen Holsapple | 3848262 | 2014-04-05 20:29:34 -0700 | [diff] [blame] | 2258 | def test_load_pkcs12_null_passphrase_load_empty(self): |
| 2259 | """ |
| 2260 | A PKCS12 string can be dumped with a null passphrase, loaded with an |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2261 | empty passphrase with `load_pkcs12`, and its components |
Stephen Holsapple | 3848262 | 2014-04-05 20:29:34 -0700 | [diff] [blame] | 2262 | extracted and examined. |
| 2263 | """ |
Jean-Paul Calderone | f0ff13b | 2014-05-05 12:48:33 -0400 | [diff] [blame] | 2264 | self.verify_pkcs12_container( |
| 2265 | self._dump_and_load(dump_passphrase=None, load_passphrase=b'')) |
Stephen Holsapple | 3848262 | 2014-04-05 20:29:34 -0700 | [diff] [blame] | 2266 | |
Stephen Holsapple | 3848262 | 2014-04-05 20:29:34 -0700 | [diff] [blame] | 2267 | def test_load_pkcs12_null_passphrase_load_null(self): |
| 2268 | """ |
| 2269 | A PKCS12 string can be dumped with a null passphrase, loaded with a |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2270 | null passphrase with `load_pkcs12`, and its components |
Stephen Holsapple | 3848262 | 2014-04-05 20:29:34 -0700 | [diff] [blame] | 2271 | extracted and examined. |
| 2272 | """ |
Jean-Paul Calderone | f0ff13b | 2014-05-05 12:48:33 -0400 | [diff] [blame] | 2273 | self.verify_pkcs12_container( |
| 2274 | self._dump_and_load(dump_passphrase=None, load_passphrase=None)) |
Stephen Holsapple | 3848262 | 2014-04-05 20:29:34 -0700 | [diff] [blame] | 2275 | |
Stephen Holsapple | 3848262 | 2014-04-05 20:29:34 -0700 | [diff] [blame] | 2276 | def test_load_pkcs12_empty_passphrase_load_empty(self): |
| 2277 | """ |
| 2278 | A PKCS12 string can be dumped with an empty passphrase, loaded with an |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2279 | empty passphrase with `load_pkcs12`, and its components |
Stephen Holsapple | 3848262 | 2014-04-05 20:29:34 -0700 | [diff] [blame] | 2280 | extracted and examined. |
| 2281 | """ |
Jean-Paul Calderone | f0ff13b | 2014-05-05 12:48:33 -0400 | [diff] [blame] | 2282 | self.verify_pkcs12_container( |
| 2283 | self._dump_and_load(dump_passphrase=b'', load_passphrase=b'')) |
Stephen Holsapple | 3848262 | 2014-04-05 20:29:34 -0700 | [diff] [blame] | 2284 | |
Stephen Holsapple | 3848262 | 2014-04-05 20:29:34 -0700 | [diff] [blame] | 2285 | def test_load_pkcs12_empty_passphrase_load_null(self): |
| 2286 | """ |
| 2287 | A PKCS12 string can be dumped with an empty passphrase, loaded with a |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2288 | null passphrase with `load_pkcs12`, and its components |
Stephen Holsapple | 3848262 | 2014-04-05 20:29:34 -0700 | [diff] [blame] | 2289 | extracted and examined. |
| 2290 | """ |
Jean-Paul Calderone | f0ff13b | 2014-05-05 12:48:33 -0400 | [diff] [blame] | 2291 | self.verify_pkcs12_container( |
| 2292 | self._dump_and_load(dump_passphrase=b'', load_passphrase=None)) |
Rick Dean | f94096c | 2009-07-18 14:23:06 -0500 | [diff] [blame] | 2293 | |
Rick Dean | ee56830 | 2009-07-24 09:56:29 -0500 | [diff] [blame] | 2294 | def test_load_pkcs12_garbage(self): |
| 2295 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2296 | `load_pkcs12` raises `OpenSSL.crypto.Error` when passed |
Alex Gaynor | 85b4970 | 2015-09-05 16:30:59 -0400 | [diff] [blame] | 2297 | a string which is not a PKCS12 dump. |
Rick Dean | ee56830 | 2009-07-24 09:56:29 -0500 | [diff] [blame] | 2298 | """ |
| 2299 | passwd = 'whatever' |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2300 | with pytest.raises(Error) as err: |
| 2301 | load_pkcs12(b'fruit loops', passwd) |
| 2302 | assert err.value.args[0][0][0] == 'asn1 encoding routines' |
| 2303 | assert len(err.value.args[0][0]) == 3 |
Rick Dean | ee56830 | 2009-07-24 09:56:29 -0500 | [diff] [blame] | 2304 | |
Rick Dean | f94096c | 2009-07-18 14:23:06 -0500 | [diff] [blame] | 2305 | def test_replace(self): |
| 2306 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2307 | `PKCS12.set_certificate` replaces the certificate in a PKCS12 |
| 2308 | cluster. `PKCS12.set_privatekey` replaces the private key. |
| 2309 | `PKCS12.set_ca_certificates` replaces the CA certificates. |
Rick Dean | f94096c | 2009-07-18 14:23:06 -0500 | [diff] [blame] | 2310 | """ |
Jean-Paul Calderone | 7426ed8 | 2009-07-25 21:19:23 -0400 | [diff] [blame] | 2311 | p12 = self.gen_pkcs12(client_cert_pem, client_key_pem, root_cert_pem) |
| 2312 | p12.set_certificate(load_certificate(FILETYPE_PEM, server_cert_pem)) |
| 2313 | p12.set_privatekey(load_privatekey(FILETYPE_PEM, server_key_pem)) |
Jean-Paul Calderone | a202edb | 2009-07-25 12:22:12 -0400 | [diff] [blame] | 2314 | root_cert = load_certificate(FILETYPE_PEM, root_cert_pem) |
Rick Dean | f94096c | 2009-07-18 14:23:06 -0500 | [diff] [blame] | 2315 | client_cert = load_certificate(FILETYPE_PEM, client_cert_pem) |
Alex Gaynor | aceb3e2 | 2015-09-05 12:00:22 -0400 | [diff] [blame] | 2316 | p12.set_ca_certificates([root_cert]) # not a tuple |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2317 | assert 1 == len(p12.get_ca_certificates()) |
| 2318 | assert root_cert == p12.get_ca_certificates()[0] |
Jean-Paul Calderone | 7426ed8 | 2009-07-25 21:19:23 -0400 | [diff] [blame] | 2319 | p12.set_ca_certificates([client_cert, root_cert]) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2320 | assert 2 == len(p12.get_ca_certificates()) |
| 2321 | assert client_cert == p12.get_ca_certificates()[0] |
| 2322 | assert root_cert == p12.get_ca_certificates()[1] |
Rick Dean | f94096c | 2009-07-18 14:23:06 -0500 | [diff] [blame] | 2323 | |
Rick Dean | f94096c | 2009-07-18 14:23:06 -0500 | [diff] [blame] | 2324 | def test_friendly_name(self): |
| 2325 | """ |
Jean-Paul Calderone | 64efa2c | 2011-09-11 10:00:09 -0400 | [diff] [blame] | 2326 | The *friendlyName* of a PKCS12 can be set and retrieved via |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2327 | `PKCS12.get_friendlyname` and `PKCS12_set_friendlyname`, and a |
| 2328 | `PKCS12` with a friendly name set can be dumped with `PKCS12.export`. |
Rick Dean | f94096c | 2009-07-18 14:23:06 -0500 | [diff] [blame] | 2329 | """ |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 2330 | passwd = b'Dogmeat[]{}!@#$%^&*()~`?/.,<>-_+=";:' |
Jean-Paul Calderone | 7426ed8 | 2009-07-25 21:19:23 -0400 | [diff] [blame] | 2331 | p12 = self.gen_pkcs12(server_cert_pem, server_key_pem, root_cert_pem) |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 2332 | for friendly_name in [b'Serverlicious', None, b'###']: |
Rick Dean | 42d69e1 | 2009-07-20 11:36:08 -0500 | [diff] [blame] | 2333 | p12.set_friendlyname(friendly_name) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2334 | assert p12.get_friendlyname() == friendly_name |
Jean-Paul Calderone | a202edb | 2009-07-25 12:22:12 -0400 | [diff] [blame] | 2335 | dumped_p12 = p12.export(passphrase=passwd, iter=2, maciter=3) |
Rick Dean | 42d69e1 | 2009-07-20 11:36:08 -0500 | [diff] [blame] | 2336 | reloaded_p12 = load_pkcs12(dumped_p12, passwd) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2337 | assert p12.get_friendlyname() == reloaded_p12.get_friendlyname() |
Jean-Paul Calderone | a202edb | 2009-07-25 12:22:12 -0400 | [diff] [blame] | 2338 | # We would use the openssl program to confirm the friendly |
| 2339 | # name, but it is not possible. The pkcs12 command |
| 2340 | # does not store the friendly name in the cert's |
Rick Dean | 42d69e1 | 2009-07-20 11:36:08 -0500 | [diff] [blame] | 2341 | # alias, which we could then extract. |
Jean-Paul Calderone | 7426ed8 | 2009-07-25 21:19:23 -0400 | [diff] [blame] | 2342 | self.check_recovery( |
| 2343 | dumped_p12, key=server_key_pem, cert=server_cert_pem, |
| 2344 | ca=root_cert_pem, passwd=passwd) |
Rick Dean | f94096c | 2009-07-18 14:23:06 -0500 | [diff] [blame] | 2345 | |
Rick Dean | f94096c | 2009-07-18 14:23:06 -0500 | [diff] [blame] | 2346 | def test_various_empty_passphrases(self): |
| 2347 | """ |
Jean-Paul Calderone | 7426ed8 | 2009-07-25 21:19:23 -0400 | [diff] [blame] | 2348 | Test that missing, None, and '' passphrases are identical for PKCS12 |
| 2349 | export. |
Rick Dean | f94096c | 2009-07-18 14:23:06 -0500 | [diff] [blame] | 2350 | """ |
Jean-Paul Calderone | 7426ed8 | 2009-07-25 21:19:23 -0400 | [diff] [blame] | 2351 | p12 = self.gen_pkcs12(client_cert_pem, client_key_pem, root_cert_pem) |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 2352 | passwd = b"" |
Jean-Paul Calderone | 7426ed8 | 2009-07-25 21:19:23 -0400 | [diff] [blame] | 2353 | dumped_p12_empty = p12.export(iter=2, maciter=0, passphrase=passwd) |
| 2354 | dumped_p12_none = p12.export(iter=3, maciter=2, passphrase=None) |
| 2355 | dumped_p12_nopw = p12.export(iter=9, maciter=4) |
| 2356 | for dumped_p12 in [dumped_p12_empty, dumped_p12_none, dumped_p12_nopw]: |
| 2357 | self.check_recovery( |
| 2358 | dumped_p12, key=client_key_pem, cert=client_cert_pem, |
| 2359 | ca=root_cert_pem, passwd=passwd) |
Rick Dean | f94096c | 2009-07-18 14:23:06 -0500 | [diff] [blame] | 2360 | |
Rick Dean | f94096c | 2009-07-18 14:23:06 -0500 | [diff] [blame] | 2361 | def test_removing_ca_cert(self): |
| 2362 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2363 | Passing `None` to `PKCS12.set_ca_certificates` removes all CA |
| 2364 | certificates. |
Rick Dean | f94096c | 2009-07-18 14:23:06 -0500 | [diff] [blame] | 2365 | """ |
Jean-Paul Calderone | 7426ed8 | 2009-07-25 21:19:23 -0400 | [diff] [blame] | 2366 | p12 = self.gen_pkcs12(server_cert_pem, server_key_pem, root_cert_pem) |
| 2367 | p12.set_ca_certificates(None) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2368 | assert None is p12.get_ca_certificates() |
Rick Dean | f94096c | 2009-07-18 14:23:06 -0500 | [diff] [blame] | 2369 | |
Rick Dean | f94096c | 2009-07-18 14:23:06 -0500 | [diff] [blame] | 2370 | def test_export_without_mac(self): |
| 2371 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2372 | Exporting a PKCS12 with a `maciter` of `-1` excludes the MAC entirely. |
Rick Dean | f94096c | 2009-07-18 14:23:06 -0500 | [diff] [blame] | 2373 | """ |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 2374 | passwd = b"Lake Michigan" |
Jean-Paul Calderone | 7426ed8 | 2009-07-25 21:19:23 -0400 | [diff] [blame] | 2375 | p12 = self.gen_pkcs12(server_cert_pem, server_key_pem, root_cert_pem) |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 2376 | dumped_p12 = p12.export(maciter=-1, passphrase=passwd, iter=2) |
Jean-Paul Calderone | 7426ed8 | 2009-07-25 21:19:23 -0400 | [diff] [blame] | 2377 | self.check_recovery( |
| 2378 | dumped_p12, key=server_key_pem, cert=server_cert_pem, |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 2379 | passwd=passwd, extra=(b"-nomacver",)) |
Jean-Paul Calderone | 7426ed8 | 2009-07-25 21:19:23 -0400 | [diff] [blame] | 2380 | |
Jean-Paul Calderone | 7426ed8 | 2009-07-25 21:19:23 -0400 | [diff] [blame] | 2381 | def test_load_without_mac(self): |
| 2382 | """ |
| 2383 | Loading a PKCS12 without a MAC does something other than crash. |
| 2384 | """ |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 2385 | passwd = b"Lake Michigan" |
Jean-Paul Calderone | 7426ed8 | 2009-07-25 21:19:23 -0400 | [diff] [blame] | 2386 | p12 = self.gen_pkcs12(server_cert_pem, server_key_pem, root_cert_pem) |
| 2387 | dumped_p12 = p12.export(maciter=-1, passphrase=passwd, iter=2) |
Rick Dean | 321a051 | 2009-08-13 17:21:29 -0500 | [diff] [blame] | 2388 | try: |
| 2389 | recovered_p12 = load_pkcs12(dumped_p12, passwd) |
| 2390 | # The person who generated this PCKS12 should be flogged, |
| 2391 | # or better yet we should have a means to determine |
| 2392 | # whether a PCKS12 had a MAC that was verified. |
| 2393 | # Anyway, libopenssl chooses to allow it, so the |
| 2394 | # pyopenssl binding does as well. |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2395 | assert isinstance(recovered_p12, PKCS12) |
Rick Dean | 321a051 | 2009-08-13 17:21:29 -0500 | [diff] [blame] | 2396 | except Error: |
| 2397 | # Failing here with an exception is preferred as some openssl |
| 2398 | # versions do. |
| 2399 | pass |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 2400 | |
Rick Dean | 25bcc1f | 2009-07-20 11:53:13 -0500 | [diff] [blame] | 2401 | def test_zero_len_list_for_ca(self): |
| 2402 | """ |
Jean-Paul Calderone | da1ffa7 | 2009-07-25 21:24:34 -0400 | [diff] [blame] | 2403 | A PKCS12 with an empty CA certificates list can be exported. |
Rick Dean | 25bcc1f | 2009-07-20 11:53:13 -0500 | [diff] [blame] | 2404 | """ |
Alex Gaynor | 6575bd1 | 2015-09-05 16:44:36 -0400 | [diff] [blame] | 2405 | passwd = b'Hobie 18' |
Jean-Paul Calderone | da1ffa7 | 2009-07-25 21:24:34 -0400 | [diff] [blame] | 2406 | p12 = self.gen_pkcs12(server_cert_pem, server_key_pem) |
Alex Gaynor | 85b4970 | 2015-09-05 16:30:59 -0400 | [diff] [blame] | 2407 | p12.set_ca_certificates([]) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2408 | assert () == p12.get_ca_certificates() |
Alex Gaynor | 85b4970 | 2015-09-05 16:30:59 -0400 | [diff] [blame] | 2409 | dumped_p12 = p12.export(passphrase=passwd, iter=3) |
| 2410 | self.check_recovery( |
| 2411 | dumped_p12, key=server_key_pem, cert=server_cert_pem, |
| 2412 | passwd=passwd) |
Rick Dean | 25bcc1f | 2009-07-20 11:53:13 -0500 | [diff] [blame] | 2413 | |
Rick Dean | f94096c | 2009-07-18 14:23:06 -0500 | [diff] [blame] | 2414 | def test_export_without_args(self): |
Jean-Paul Calderone | 38a646d | 2008-03-25 15:16:18 -0400 | [diff] [blame] | 2415 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2416 | All the arguments to `PKCS12.export` are optional. |
Jean-Paul Calderone | 38a646d | 2008-03-25 15:16:18 -0400 | [diff] [blame] | 2417 | """ |
Jean-Paul Calderone | da1ffa7 | 2009-07-25 21:24:34 -0400 | [diff] [blame] | 2418 | p12 = self.gen_pkcs12(server_cert_pem, server_key_pem, root_cert_pem) |
Rick Dean | f94096c | 2009-07-18 14:23:06 -0500 | [diff] [blame] | 2419 | dumped_p12 = p12.export() # no args |
Jean-Paul Calderone | da1ffa7 | 2009-07-25 21:24:34 -0400 | [diff] [blame] | 2420 | self.check_recovery( |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 2421 | dumped_p12, key=server_key_pem, cert=server_cert_pem, passwd=b"") |
Rick Dean | f94096c | 2009-07-18 14:23:06 -0500 | [diff] [blame] | 2422 | |
Abraham Martin | c5484ba | 2015-03-25 15:33:05 +0000 | [diff] [blame] | 2423 | def test_export_without_bytes(self): |
| 2424 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2425 | Test `PKCS12.export` with text not bytes as passphrase |
Abraham Martin | c5484ba | 2015-03-25 15:33:05 +0000 | [diff] [blame] | 2426 | """ |
| 2427 | p12 = self.gen_pkcs12(server_cert_pem, server_key_pem, root_cert_pem) |
| 2428 | |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2429 | with pytest.warns(DeprecationWarning) as w: |
Abraham Martin | c5484ba | 2015-03-25 15:33:05 +0000 | [diff] [blame] | 2430 | simplefilter("always") |
Jean-Paul Calderone | 13a0e65 | 2015-03-29 07:58:51 -0400 | [diff] [blame] | 2431 | dumped_p12 = p12.export(passphrase=b"randomtext".decode("ascii")) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2432 | assert ( |
Jean-Paul Calderone | 13a0e65 | 2015-03-29 07:58:51 -0400 | [diff] [blame] | 2433 | "{0} for passphrase is no longer accepted, use bytes".format( |
Jean-Paul Calderone | 6462b07 | 2015-03-29 07:03:11 -0400 | [diff] [blame] | 2434 | WARNING_TYPE_EXPECTED |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2435 | ) == str(w[-1].message)) |
Abraham Martin | c5484ba | 2015-03-25 15:33:05 +0000 | [diff] [blame] | 2436 | self.check_recovery( |
Alex Gaynor | 791212d | 2015-09-05 15:46:08 -0400 | [diff] [blame] | 2437 | dumped_p12, |
| 2438 | key=server_key_pem, |
| 2439 | cert=server_cert_pem, |
| 2440 | passwd=b"randomtext" |
| 2441 | ) |
Abraham Martin | c5484ba | 2015-03-25 15:33:05 +0000 | [diff] [blame] | 2442 | |
Rick Dean | f94096c | 2009-07-18 14:23:06 -0500 | [diff] [blame] | 2443 | def test_key_cert_mismatch(self): |
| 2444 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2445 | `PKCS12.export` raises an exception when a key and certificate |
Jean-Paul Calderone | da1ffa7 | 2009-07-25 21:24:34 -0400 | [diff] [blame] | 2446 | mismatch. |
Rick Dean | f94096c | 2009-07-18 14:23:06 -0500 | [diff] [blame] | 2447 | """ |
Jean-Paul Calderone | da1ffa7 | 2009-07-25 21:24:34 -0400 | [diff] [blame] | 2448 | p12 = self.gen_pkcs12(server_cert_pem, client_key_pem, root_cert_pem) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2449 | with pytest.raises(Error): |
| 2450 | p12.export() |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 2451 | |
| 2452 | |
Rick Dean | 4c9ad61 | 2009-07-17 15:05:22 -0500 | [diff] [blame] | 2453 | def _runopenssl(pem, *args): |
| 2454 | """ |
| 2455 | Run the command line openssl tool with the given arguments and write |
Rick Dean | 55d1ce6 | 2009-08-13 17:40:24 -0500 | [diff] [blame] | 2456 | the given PEM to its stdin. Not safe for quotes. |
Rick Dean | 4c9ad61 | 2009-07-17 15:05:22 -0500 | [diff] [blame] | 2457 | """ |
Alex Gaynor | 6cbc69a | 2017-07-25 09:07:04 -0400 | [diff] [blame] | 2458 | proc = Popen([b"openssl"] + list(args), stdin=PIPE, stdout=PIPE) |
Jean-Paul Calderone | 62ca8da | 2010-08-11 19:58:08 -0400 | [diff] [blame] | 2459 | proc.stdin.write(pem) |
| 2460 | proc.stdin.close() |
Jean-Paul Calderone | e82e325 | 2013-03-03 10:14:10 -0800 | [diff] [blame] | 2461 | output = proc.stdout.read() |
| 2462 | proc.stdout.close() |
| 2463 | proc.wait() |
| 2464 | return output |
Rick Dean | 4c9ad61 | 2009-07-17 15:05:22 -0500 | [diff] [blame] | 2465 | |
| 2466 | |
Hynek Schlawack | 40d448f | 2016-06-03 16:15:14 -0700 | [diff] [blame] | 2467 | class TestLoadPublicKey(object): |
Paul Kehrer | 32fc4e6 | 2016-06-03 15:21:44 -0700 | [diff] [blame] | 2468 | """ |
Hynek Schlawack | 40d448f | 2016-06-03 16:15:14 -0700 | [diff] [blame] | 2469 | Tests for :func:`load_publickey`. |
Paul Kehrer | 32fc4e6 | 2016-06-03 15:21:44 -0700 | [diff] [blame] | 2470 | """ |
Hynek Schlawack | 40d448f | 2016-06-03 16:15:14 -0700 | [diff] [blame] | 2471 | def test_loading_works(self): |
Paul Kehrer | 32fc4e6 | 2016-06-03 15:21:44 -0700 | [diff] [blame] | 2472 | """ |
Hynek Schlawack | 40d448f | 2016-06-03 16:15:14 -0700 | [diff] [blame] | 2473 | load_publickey loads public keys and sets correct attributes. |
Paul Kehrer | 32fc4e6 | 2016-06-03 15:21:44 -0700 | [diff] [blame] | 2474 | """ |
| 2475 | key = load_publickey(FILETYPE_PEM, cleartextPublicKeyPEM) |
Hynek Schlawack | 40d448f | 2016-06-03 16:15:14 -0700 | [diff] [blame] | 2476 | |
| 2477 | assert True is key._only_public |
| 2478 | assert 2048 == key.bits() |
| 2479 | assert TYPE_RSA == key.type() |
| 2480 | |
| 2481 | def test_invalid_type(self): |
| 2482 | """ |
| 2483 | load_publickey doesn't support FILETYPE_TEXT. |
| 2484 | """ |
| 2485 | with pytest.raises(ValueError): |
| 2486 | load_publickey(FILETYPE_TEXT, cleartextPublicKeyPEM) |
| 2487 | |
| 2488 | def test_invalid_key_format(self): |
| 2489 | """ |
| 2490 | load_publickey explodes on incorrect keys. |
| 2491 | """ |
| 2492 | with pytest.raises(Error): |
| 2493 | load_publickey(FILETYPE_ASN1, cleartextPublicKeyPEM) |
| 2494 | |
| 2495 | def test_tolerates_unicode_strings(self): |
| 2496 | """ |
| 2497 | load_publickey works with text strings, not just bytes. |
| 2498 | """ |
| 2499 | serialized = cleartextPublicKeyPEM.decode('ascii') |
| 2500 | key = load_publickey(FILETYPE_PEM, serialized) |
| 2501 | dumped_pem = dump_publickey(FILETYPE_PEM, key) |
| 2502 | |
| 2503 | assert dumped_pem == cleartextPublicKeyPEM |
Paul Kehrer | 32fc4e6 | 2016-06-03 15:21:44 -0700 | [diff] [blame] | 2504 | |
| 2505 | |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2506 | class TestFunction(object): |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 2507 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2508 | Tests for free-functions in the `OpenSSL.crypto` module. |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 2509 | """ |
Jean-Paul Calderone | fe1b9bd | 2010-08-03 18:00:21 -0400 | [diff] [blame] | 2510 | |
| 2511 | def test_load_privatekey_invalid_format(self): |
| 2512 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2513 | `load_privatekey` raises `ValueError` if passed an unknown filetype. |
Jean-Paul Calderone | fe1b9bd | 2010-08-03 18:00:21 -0400 | [diff] [blame] | 2514 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2515 | with pytest.raises(ValueError): |
| 2516 | load_privatekey(100, root_key_pem) |
Jean-Paul Calderone | fe1b9bd | 2010-08-03 18:00:21 -0400 | [diff] [blame] | 2517 | |
Jean-Paul Calderone | fe1b9bd | 2010-08-03 18:00:21 -0400 | [diff] [blame] | 2518 | def test_load_privatekey_invalid_passphrase_type(self): |
| 2519 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2520 | `load_privatekey` raises `TypeError` if passed a passphrase that is |
| 2521 | neither a `str` nor a callable. |
Jean-Paul Calderone | fe1b9bd | 2010-08-03 18:00:21 -0400 | [diff] [blame] | 2522 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2523 | with pytest.raises(TypeError): |
| 2524 | load_privatekey( |
| 2525 | FILETYPE_PEM, encryptedPrivateKeyPEMPassphrase, object()) |
Jean-Paul Calderone | fe1b9bd | 2010-08-03 18:00:21 -0400 | [diff] [blame] | 2526 | |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 2527 | def test_load_privatekey_wrongPassphrase(self): |
| 2528 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2529 | `load_privatekey` raises `OpenSSL.crypto.Error` when it is passed an |
| 2530 | encrypted PEM and an incorrect passphrase. |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 2531 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2532 | with pytest.raises(Error) as err: |
| 2533 | load_privatekey(FILETYPE_PEM, encryptedPrivateKeyPEM, b"quack") |
| 2534 | assert err.value.args[0] != [] |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 2535 | |
Ziga Seilnacht | 376cf97 | 2009-12-22 16:04:10 +0100 | [diff] [blame] | 2536 | def test_load_privatekey_passphraseWrongType(self): |
| 2537 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2538 | `load_privatekey` raises `ValueError` when it is passeda passphrase |
| 2539 | with a private key encoded in a format, that doesn't support |
| 2540 | encryption. |
Ziga Seilnacht | 376cf97 | 2009-12-22 16:04:10 +0100 | [diff] [blame] | 2541 | """ |
| 2542 | key = load_privatekey(FILETYPE_PEM, cleartextPrivateKeyPEM) |
| 2543 | blob = dump_privatekey(FILETYPE_ASN1, key) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2544 | with pytest.raises(ValueError): |
| 2545 | load_privatekey(FILETYPE_ASN1, blob, "secret") |
Ziga Seilnacht | 376cf97 | 2009-12-22 16:04:10 +0100 | [diff] [blame] | 2546 | |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 2547 | def test_load_privatekey_passphrase(self): |
| 2548 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2549 | `load_privatekey` can create a `PKey` object from an encrypted PEM |
| 2550 | string if given the passphrase. |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 2551 | """ |
| 2552 | key = load_privatekey( |
| 2553 | FILETYPE_PEM, encryptedPrivateKeyPEM, |
| 2554 | encryptedPrivateKeyPEMPassphrase) |
Alex Gaynor | 01f90a1 | 2019-02-07 09:14:48 -0500 | [diff] [blame] | 2555 | assert isinstance(key, PKey) |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 2556 | |
Ziga Seilnacht | 6b90a40 | 2009-12-22 14:33:47 +0100 | [diff] [blame] | 2557 | def test_load_privatekey_passphrase_exception(self): |
| 2558 | """ |
Alex Gaynor | 791212d | 2015-09-05 15:46:08 -0400 | [diff] [blame] | 2559 | If the passphrase callback raises an exception, that exception is |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2560 | raised by `load_privatekey`. |
Ziga Seilnacht | 6b90a40 | 2009-12-22 14:33:47 +0100 | [diff] [blame] | 2561 | """ |
| 2562 | def cb(ignored): |
| 2563 | raise ArithmeticError |
| 2564 | |
Alex Gaynor | 791212d | 2015-09-05 15:46:08 -0400 | [diff] [blame] | 2565 | with pytest.raises(ArithmeticError): |
| 2566 | load_privatekey(FILETYPE_PEM, encryptedPrivateKeyPEM, cb) |
Ziga Seilnacht | 6b90a40 | 2009-12-22 14:33:47 +0100 | [diff] [blame] | 2567 | |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 2568 | def test_load_privatekey_wrongPassphraseCallback(self): |
| 2569 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2570 | `load_privatekey` raises `OpenSSL.crypto.Error` when it |
Jean-Paul Calderone | d440a08 | 2011-09-14 11:02:05 -0400 | [diff] [blame] | 2571 | is passed an encrypted PEM and a passphrase callback which returns an |
| 2572 | incorrect passphrase. |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 2573 | """ |
| 2574 | called = [] |
Alex Gaynor | aceb3e2 | 2015-09-05 12:00:22 -0400 | [diff] [blame] | 2575 | |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 2576 | def cb(*a): |
| 2577 | called.append(None) |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 2578 | return b"quack" |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2579 | with pytest.raises(Error) as err: |
| 2580 | load_privatekey(FILETYPE_PEM, encryptedPrivateKeyPEM, cb) |
| 2581 | assert called |
| 2582 | assert err.value.args[0] != [] |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 2583 | |
| 2584 | def test_load_privatekey_passphraseCallback(self): |
| 2585 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2586 | `load_privatekey` can create a `PKey` object from an encrypted PEM |
| 2587 | string if given a passphrase callback which returns the correct |
| 2588 | password. |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 2589 | """ |
| 2590 | called = [] |
Alex Gaynor | aceb3e2 | 2015-09-05 12:00:22 -0400 | [diff] [blame] | 2591 | |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 2592 | def cb(writing): |
| 2593 | called.append(writing) |
| 2594 | return encryptedPrivateKeyPEMPassphrase |
| 2595 | key = load_privatekey(FILETYPE_PEM, encryptedPrivateKeyPEM, cb) |
Alex Gaynor | 01f90a1 | 2019-02-07 09:14:48 -0500 | [diff] [blame] | 2596 | assert isinstance(key, PKey) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2597 | assert called == [False] |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 2598 | |
Jean-Paul Calderone | 105cb95 | 2011-09-14 10:16:46 -0400 | [diff] [blame] | 2599 | def test_load_privatekey_passphrase_wrong_return_type(self): |
| 2600 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2601 | `load_privatekey` raises `ValueError` if the passphrase callback |
| 2602 | returns something other than a byte string. |
Jean-Paul Calderone | 105cb95 | 2011-09-14 10:16:46 -0400 | [diff] [blame] | 2603 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2604 | with pytest.raises(ValueError): |
| 2605 | load_privatekey( |
| 2606 | FILETYPE_PEM, encryptedPrivateKeyPEM, lambda *args: 3) |
Jean-Paul Calderone | fe1b9bd | 2010-08-03 18:00:21 -0400 | [diff] [blame] | 2607 | |
Alex Chan | fb078d8 | 2017-04-20 11:16:15 +0100 | [diff] [blame] | 2608 | def test_dump_privatekey_wrong_args(self): |
| 2609 | """ |
| 2610 | `dump_privatekey` raises `TypeError` if called with a `cipher` |
| 2611 | argument but no `passphrase` argument. |
| 2612 | """ |
| 2613 | key = PKey() |
| 2614 | key.generate_key(TYPE_RSA, 512) |
| 2615 | with pytest.raises(TypeError): |
| 2616 | dump_privatekey(FILETYPE_PEM, key, cipher=GOOD_CIPHER) |
| 2617 | |
Paul Kehrer | cded993 | 2017-06-29 18:43:42 -0500 | [diff] [blame] | 2618 | def test_dump_privatekey_not_rsa_key(self): |
| 2619 | """ |
| 2620 | `dump_privatekey` raises `TypeError` if called with a key that is |
| 2621 | not RSA. |
| 2622 | """ |
| 2623 | key = PKey() |
| 2624 | key.generate_key(TYPE_DSA, 512) |
| 2625 | with pytest.raises(TypeError): |
| 2626 | dump_privatekey(FILETYPE_TEXT, key) |
| 2627 | |
| 2628 | def test_dump_privatekey_invalid_pkey(self): |
| 2629 | with pytest.raises(TypeError): |
| 2630 | dump_privatekey(FILETYPE_TEXT, object()) |
| 2631 | |
Jean-Paul Calderone | fe1b9bd | 2010-08-03 18:00:21 -0400 | [diff] [blame] | 2632 | def test_dump_privatekey_unknown_cipher(self): |
| 2633 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2634 | `dump_privatekey` raises `ValueError` if called with an unrecognized |
| 2635 | cipher name. |
Jean-Paul Calderone | fe1b9bd | 2010-08-03 18:00:21 -0400 | [diff] [blame] | 2636 | """ |
| 2637 | key = PKey() |
| 2638 | key.generate_key(TYPE_RSA, 512) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2639 | with pytest.raises(ValueError): |
| 2640 | dump_privatekey(FILETYPE_PEM, key, BAD_CIPHER, "passphrase") |
Jean-Paul Calderone | fe1b9bd | 2010-08-03 18:00:21 -0400 | [diff] [blame] | 2641 | |
Jean-Paul Calderone | fe1b9bd | 2010-08-03 18:00:21 -0400 | [diff] [blame] | 2642 | def test_dump_privatekey_invalid_passphrase_type(self): |
| 2643 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2644 | `dump_privatekey` raises `TypeError` if called with a passphrase which |
| 2645 | is neither a `str` nor a callable. |
Jean-Paul Calderone | fe1b9bd | 2010-08-03 18:00:21 -0400 | [diff] [blame] | 2646 | """ |
| 2647 | key = PKey() |
| 2648 | key.generate_key(TYPE_RSA, 512) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2649 | with pytest.raises(TypeError): |
| 2650 | dump_privatekey(FILETYPE_PEM, key, GOOD_CIPHER, object()) |
Jean-Paul Calderone | fe1b9bd | 2010-08-03 18:00:21 -0400 | [diff] [blame] | 2651 | |
Jean-Paul Calderone | fe1b9bd | 2010-08-03 18:00:21 -0400 | [diff] [blame] | 2652 | def test_dump_privatekey_invalid_filetype(self): |
| 2653 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2654 | `dump_privatekey` raises `ValueError` if called with an unrecognized |
| 2655 | filetype. |
Jean-Paul Calderone | fe1b9bd | 2010-08-03 18:00:21 -0400 | [diff] [blame] | 2656 | """ |
| 2657 | key = PKey() |
| 2658 | key.generate_key(TYPE_RSA, 512) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2659 | with pytest.raises(ValueError): |
| 2660 | dump_privatekey(100, key) |
Jean-Paul Calderone | fe1b9bd | 2010-08-03 18:00:21 -0400 | [diff] [blame] | 2661 | |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2662 | def test_load_privatekey_passphrase_callback_length(self): |
Ziga Seilnacht | 781295a | 2009-12-22 14:58:01 +0100 | [diff] [blame] | 2663 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2664 | `crypto.load_privatekey` should raise an error when the passphrase |
| 2665 | provided by the callback is too long, not silently truncate it. |
Ziga Seilnacht | 781295a | 2009-12-22 14:58:01 +0100 | [diff] [blame] | 2666 | """ |
| 2667 | def cb(ignored): |
| 2668 | return "a" * 1025 |
| 2669 | |
Alex Gaynor | 791212d | 2015-09-05 15:46:08 -0400 | [diff] [blame] | 2670 | with pytest.raises(ValueError): |
| 2671 | load_privatekey(FILETYPE_PEM, encryptedPrivateKeyPEM, cb) |
Ziga Seilnacht | 781295a | 2009-12-22 14:58:01 +0100 | [diff] [blame] | 2672 | |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 2673 | def test_dump_privatekey_passphrase(self): |
| 2674 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2675 | `dump_privatekey` writes an encrypted PEM when given a passphrase. |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 2676 | """ |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 2677 | passphrase = b"foo" |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 2678 | key = load_privatekey(FILETYPE_PEM, cleartextPrivateKeyPEM) |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 2679 | pem = dump_privatekey(FILETYPE_PEM, key, GOOD_CIPHER, passphrase) |
Alex Gaynor | 1257600 | 2019-11-18 00:18:50 -0500 | [diff] [blame] | 2680 | assert isinstance(pem, bytes) |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 2681 | loadedKey = load_privatekey(FILETYPE_PEM, pem, passphrase) |
Alex Gaynor | 01f90a1 | 2019-02-07 09:14:48 -0500 | [diff] [blame] | 2682 | assert isinstance(loadedKey, PKey) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2683 | assert loadedKey.type() == key.type() |
| 2684 | assert loadedKey.bits() == key.bits() |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 2685 | |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2686 | def test_dump_privatekey_passphrase_wrong_type(self): |
Ziga Seilnacht | 376cf97 | 2009-12-22 16:04:10 +0100 | [diff] [blame] | 2687 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2688 | `dump_privatekey` raises `ValueError` when it is passed a passphrase |
| 2689 | with a private key encoded in a format, that doesn't support |
| 2690 | encryption. |
Ziga Seilnacht | 376cf97 | 2009-12-22 16:04:10 +0100 | [diff] [blame] | 2691 | """ |
| 2692 | key = load_privatekey(FILETYPE_PEM, cleartextPrivateKeyPEM) |
Alex Gaynor | 791212d | 2015-09-05 15:46:08 -0400 | [diff] [blame] | 2693 | with pytest.raises(ValueError): |
| 2694 | dump_privatekey(FILETYPE_ASN1, key, GOOD_CIPHER, "secret") |
Ziga Seilnacht | 376cf97 | 2009-12-22 16:04:10 +0100 | [diff] [blame] | 2695 | |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 2696 | def test_dump_certificate(self): |
| 2697 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2698 | `dump_certificate` writes PEM, DER, and text. |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 2699 | """ |
Jean-Paul Calderone | f17e421 | 2009-04-01 14:21:40 -0400 | [diff] [blame] | 2700 | pemData = cleartextCertificatePEM + cleartextPrivateKeyPEM |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 2701 | cert = load_certificate(FILETYPE_PEM, pemData) |
| 2702 | dumped_pem = dump_certificate(FILETYPE_PEM, cert) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2703 | assert dumped_pem == cleartextCertificatePEM |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 2704 | dumped_der = dump_certificate(FILETYPE_ASN1, cert) |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 2705 | good_der = _runopenssl(dumped_pem, b"x509", b"-outform", b"DER") |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2706 | assert dumped_der == good_der |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 2707 | cert2 = load_certificate(FILETYPE_ASN1, dumped_der) |
| 2708 | dumped_pem2 = dump_certificate(FILETYPE_PEM, cert2) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2709 | assert dumped_pem2 == cleartextCertificatePEM |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 2710 | dumped_text = dump_certificate(FILETYPE_TEXT, cert) |
Alex Gaynor | 316aa2c | 2016-09-10 14:42:53 -0400 | [diff] [blame] | 2711 | good_text = _runopenssl( |
| 2712 | dumped_pem, b"x509", b"-noout", b"-text", b"-nameopt", b"") |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2713 | assert dumped_text == good_text |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 2714 | |
Alex Gaynor | 3772611 | 2016-07-04 09:51:32 -0400 | [diff] [blame] | 2715 | def test_dump_certificate_bad_type(self): |
| 2716 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2717 | `dump_certificate` raises a `ValueError` if it's called with |
Alex Gaynor | 3772611 | 2016-07-04 09:51:32 -0400 | [diff] [blame] | 2718 | a bad type. |
| 2719 | """ |
| 2720 | cert = load_certificate(FILETYPE_PEM, cleartextCertificatePEM) |
| 2721 | with pytest.raises(ValueError): |
| 2722 | dump_certificate(object(), cert) |
| 2723 | |
Jean-Paul Calderone | e82e325 | 2013-03-03 10:14:10 -0800 | [diff] [blame] | 2724 | def test_dump_privatekey_pem(self): |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 2725 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2726 | `dump_privatekey` writes a PEM |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 2727 | """ |
| 2728 | key = load_privatekey(FILETYPE_PEM, cleartextPrivateKeyPEM) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2729 | assert key.check() |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 2730 | dumped_pem = dump_privatekey(FILETYPE_PEM, key) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2731 | assert dumped_pem == cleartextPrivateKeyPEM |
Jean-Paul Calderone | e82e325 | 2013-03-03 10:14:10 -0800 | [diff] [blame] | 2732 | |
Jean-Paul Calderone | e82e325 | 2013-03-03 10:14:10 -0800 | [diff] [blame] | 2733 | def test_dump_privatekey_asn1(self): |
| 2734 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2735 | `dump_privatekey` writes a DER |
Jean-Paul Calderone | e82e325 | 2013-03-03 10:14:10 -0800 | [diff] [blame] | 2736 | """ |
| 2737 | key = load_privatekey(FILETYPE_PEM, cleartextPrivateKeyPEM) |
| 2738 | dumped_pem = dump_privatekey(FILETYPE_PEM, key) |
| 2739 | |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 2740 | dumped_der = dump_privatekey(FILETYPE_ASN1, key) |
Jean-Paul Calderone | f17e421 | 2009-04-01 14:21:40 -0400 | [diff] [blame] | 2741 | # XXX This OpenSSL call writes "writing RSA key" to standard out. Sad. |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 2742 | good_der = _runopenssl(dumped_pem, b"rsa", b"-outform", b"DER") |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2743 | assert dumped_der == good_der |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 2744 | key2 = load_privatekey(FILETYPE_ASN1, dumped_der) |
| 2745 | dumped_pem2 = dump_privatekey(FILETYPE_PEM, key2) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2746 | assert dumped_pem2 == cleartextPrivateKeyPEM |
Jean-Paul Calderone | e82e325 | 2013-03-03 10:14:10 -0800 | [diff] [blame] | 2747 | |
Jean-Paul Calderone | e82e325 | 2013-03-03 10:14:10 -0800 | [diff] [blame] | 2748 | def test_dump_privatekey_text(self): |
| 2749 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2750 | `dump_privatekey` writes a text |
Jean-Paul Calderone | e82e325 | 2013-03-03 10:14:10 -0800 | [diff] [blame] | 2751 | """ |
| 2752 | key = load_privatekey(FILETYPE_PEM, cleartextPrivateKeyPEM) |
| 2753 | dumped_pem = dump_privatekey(FILETYPE_PEM, key) |
| 2754 | |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 2755 | dumped_text = dump_privatekey(FILETYPE_TEXT, key) |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 2756 | good_text = _runopenssl(dumped_pem, b"rsa", b"-noout", b"-text") |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2757 | assert dumped_text == good_text |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 2758 | |
Cory Benfield | 6492f7c | 2015-10-27 16:57:58 +0900 | [diff] [blame] | 2759 | def test_dump_publickey_pem(self): |
| 2760 | """ |
Cory Benfield | 11c1019 | 2015-10-27 17:23:03 +0900 | [diff] [blame] | 2761 | dump_publickey writes a PEM. |
Cory Benfield | 6492f7c | 2015-10-27 16:57:58 +0900 | [diff] [blame] | 2762 | """ |
| 2763 | key = load_publickey(FILETYPE_PEM, cleartextPublicKeyPEM) |
| 2764 | dumped_pem = dump_publickey(FILETYPE_PEM, key) |
Cory Benfield | d86f1d8 | 2015-10-27 17:25:17 +0900 | [diff] [blame] | 2765 | assert dumped_pem == cleartextPublicKeyPEM |
Cory Benfield | 6492f7c | 2015-10-27 16:57:58 +0900 | [diff] [blame] | 2766 | |
| 2767 | def test_dump_publickey_asn1(self): |
| 2768 | """ |
Cory Benfield | 11c1019 | 2015-10-27 17:23:03 +0900 | [diff] [blame] | 2769 | dump_publickey writes a DER. |
Cory Benfield | 6492f7c | 2015-10-27 16:57:58 +0900 | [diff] [blame] | 2770 | """ |
| 2771 | key = load_publickey(FILETYPE_PEM, cleartextPublicKeyPEM) |
| 2772 | dumped_der = dump_publickey(FILETYPE_ASN1, key) |
| 2773 | key2 = load_publickey(FILETYPE_ASN1, dumped_der) |
| 2774 | dumped_pem2 = dump_publickey(FILETYPE_PEM, key2) |
Cory Benfield | d86f1d8 | 2015-10-27 17:25:17 +0900 | [diff] [blame] | 2775 | assert dumped_pem2 == cleartextPublicKeyPEM |
Cory Benfield | 6492f7c | 2015-10-27 16:57:58 +0900 | [diff] [blame] | 2776 | |
Cory Benfield | e02c7d8 | 2015-10-27 17:34:49 +0900 | [diff] [blame] | 2777 | def test_dump_publickey_invalid_type(self): |
| 2778 | """ |
| 2779 | dump_publickey doesn't support FILETYPE_TEXT. |
| 2780 | """ |
| 2781 | key = load_publickey(FILETYPE_PEM, cleartextPublicKeyPEM) |
| 2782 | |
| 2783 | with pytest.raises(ValueError): |
| 2784 | dump_publickey(FILETYPE_TEXT, key) |
| 2785 | |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 2786 | def test_dump_certificate_request(self): |
| 2787 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2788 | `dump_certificate_request` writes a PEM, DER, and text. |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 2789 | """ |
Alex Gaynor | 3128750 | 2015-09-05 16:11:27 -0400 | [diff] [blame] | 2790 | req = load_certificate_request( |
| 2791 | FILETYPE_PEM, cleartextCertificateRequestPEM) |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 2792 | dumped_pem = dump_certificate_request(FILETYPE_PEM, req) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2793 | assert dumped_pem == cleartextCertificateRequestPEM |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 2794 | dumped_der = dump_certificate_request(FILETYPE_ASN1, req) |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 2795 | good_der = _runopenssl(dumped_pem, b"req", b"-outform", b"DER") |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2796 | assert dumped_der == good_der |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 2797 | req2 = load_certificate_request(FILETYPE_ASN1, dumped_der) |
| 2798 | dumped_pem2 = dump_certificate_request(FILETYPE_PEM, req2) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2799 | assert dumped_pem2 == cleartextCertificateRequestPEM |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 2800 | dumped_text = dump_certificate_request(FILETYPE_TEXT, req) |
Alex Gaynor | 316aa2c | 2016-09-10 14:42:53 -0400 | [diff] [blame] | 2801 | good_text = _runopenssl( |
| 2802 | dumped_pem, b"req", b"-noout", b"-text", b"-nameopt", b"") |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2803 | assert dumped_text == good_text |
| 2804 | with pytest.raises(ValueError): |
| 2805 | dump_certificate_request(100, req) |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 2806 | |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2807 | def test_dump_privatekey_passphrase_callback(self): |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 2808 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2809 | `dump_privatekey` writes an encrypted PEM when given a callback |
Alex Gaynor | 791212d | 2015-09-05 15:46:08 -0400 | [diff] [blame] | 2810 | which returns the correct passphrase. |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 2811 | """ |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 2812 | passphrase = b"foo" |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 2813 | called = [] |
Alex Gaynor | aceb3e2 | 2015-09-05 12:00:22 -0400 | [diff] [blame] | 2814 | |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 2815 | def cb(writing): |
| 2816 | called.append(writing) |
| 2817 | return passphrase |
| 2818 | key = load_privatekey(FILETYPE_PEM, cleartextPrivateKeyPEM) |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 2819 | pem = dump_privatekey(FILETYPE_PEM, key, GOOD_CIPHER, cb) |
Alex Gaynor | 1257600 | 2019-11-18 00:18:50 -0500 | [diff] [blame] | 2820 | assert isinstance(pem, bytes) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2821 | assert called == [True] |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 2822 | loadedKey = load_privatekey(FILETYPE_PEM, pem, passphrase) |
Alex Gaynor | 01f90a1 | 2019-02-07 09:14:48 -0500 | [diff] [blame] | 2823 | assert isinstance(loadedKey, PKey) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2824 | assert loadedKey.type() == key.type() |
| 2825 | assert loadedKey.bits() == key.bits() |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 2826 | |
Ziga Seilnacht | 6b90a40 | 2009-12-22 14:33:47 +0100 | [diff] [blame] | 2827 | def test_dump_privatekey_passphrase_exception(self): |
| 2828 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2829 | `dump_privatekey` should not overwrite the exception raised |
Ziga Seilnacht | 6b90a40 | 2009-12-22 14:33:47 +0100 | [diff] [blame] | 2830 | by the passphrase callback. |
| 2831 | """ |
| 2832 | def cb(ignored): |
| 2833 | raise ArithmeticError |
| 2834 | |
| 2835 | key = load_privatekey(FILETYPE_PEM, cleartextPrivateKeyPEM) |
Alex Gaynor | 85b4970 | 2015-09-05 16:30:59 -0400 | [diff] [blame] | 2836 | with pytest.raises(ArithmeticError): |
| 2837 | dump_privatekey(FILETYPE_PEM, key, GOOD_CIPHER, cb) |
Ziga Seilnacht | 6b90a40 | 2009-12-22 14:33:47 +0100 | [diff] [blame] | 2838 | |
Ziga Seilnacht | 781295a | 2009-12-22 14:58:01 +0100 | [diff] [blame] | 2839 | def test_dump_privatekey_passphraseCallbackLength(self): |
| 2840 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2841 | `crypto.dump_privatekey` should raise an error when the passphrase |
| 2842 | provided by the callback is too long, not silently truncate it. |
Ziga Seilnacht | 781295a | 2009-12-22 14:58:01 +0100 | [diff] [blame] | 2843 | """ |
| 2844 | def cb(ignored): |
| 2845 | return "a" * 1025 |
| 2846 | |
| 2847 | key = load_privatekey(FILETYPE_PEM, cleartextPrivateKeyPEM) |
Alex Gaynor | 85b4970 | 2015-09-05 16:30:59 -0400 | [diff] [blame] | 2848 | with pytest.raises(ValueError): |
| 2849 | dump_privatekey(FILETYPE_PEM, key, GOOD_CIPHER, cb) |
Ziga Seilnacht | 781295a | 2009-12-22 14:58:01 +0100 | [diff] [blame] | 2850 | |
Alex Gaynor | 4b9c96a | 2014-08-14 09:51:48 -0700 | [diff] [blame] | 2851 | def test_load_pkcs7_data_pem(self): |
Jean-Paul Calderone | dc138fa | 2009-06-27 14:32:07 -0400 | [diff] [blame] | 2852 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2853 | `load_pkcs7_data` accepts a PKCS#7 string and returns an instance of |
| 2854 | `PKCS`. |
Jean-Paul Calderone | dc138fa | 2009-06-27 14:32:07 -0400 | [diff] [blame] | 2855 | """ |
| 2856 | pkcs7 = load_pkcs7_data(FILETYPE_PEM, pkcs7Data) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2857 | assert isinstance(pkcs7, PKCS7) |
Jean-Paul Calderone | dc138fa | 2009-06-27 14:32:07 -0400 | [diff] [blame] | 2858 | |
Alex Gaynor | 4b9c96a | 2014-08-14 09:51:48 -0700 | [diff] [blame] | 2859 | def test_load_pkcs7_data_asn1(self): |
Alex Gaynor | 9875a91 | 2014-08-14 13:35:05 -0700 | [diff] [blame] | 2860 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2861 | `load_pkcs7_data` accepts a bytes containing ASN1 data representing |
| 2862 | PKCS#7 and returns an instance of `PKCS7`. |
Alex Gaynor | 9875a91 | 2014-08-14 13:35:05 -0700 | [diff] [blame] | 2863 | """ |
Alex Gaynor | 4b9c96a | 2014-08-14 09:51:48 -0700 | [diff] [blame] | 2864 | pkcs7 = load_pkcs7_data(FILETYPE_ASN1, pkcs7DataASN1) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2865 | assert isinstance(pkcs7, PKCS7) |
Alex Gaynor | 4b9c96a | 2014-08-14 09:51:48 -0700 | [diff] [blame] | 2866 | |
Jean-Paul Calderone | e82e325 | 2013-03-03 10:14:10 -0800 | [diff] [blame] | 2867 | def test_load_pkcs7_data_invalid(self): |
| 2868 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2869 | If the data passed to `load_pkcs7_data` is invalid, `Error` is raised. |
Jean-Paul Calderone | e82e325 | 2013-03-03 10:14:10 -0800 | [diff] [blame] | 2870 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2871 | with pytest.raises(Error): |
| 2872 | load_pkcs7_data(FILETYPE_PEM, b"foo") |
Jean-Paul Calderone | e82e325 | 2013-03-03 10:14:10 -0800 | [diff] [blame] | 2873 | |
Alex Gaynor | 09a386e | 2016-07-03 09:32:44 -0400 | [diff] [blame] | 2874 | def test_load_pkcs7_type_invalid(self): |
| 2875 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2876 | If the type passed to `load_pkcs7_data`, `ValueError` is raised. |
Alex Gaynor | 09a386e | 2016-07-03 09:32:44 -0400 | [diff] [blame] | 2877 | """ |
| 2878 | with pytest.raises(ValueError): |
| 2879 | load_pkcs7_data(object(), b"foo") |
| 2880 | |
Jean-Paul Calderone | e82e325 | 2013-03-03 10:14:10 -0800 | [diff] [blame] | 2881 | |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 2882 | class TestLoadCertificate(object): |
Jean-Paul Calderone | 4a68b40 | 2013-12-29 16:54:58 -0500 | [diff] [blame] | 2883 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 2884 | Tests for `load_certificate_request`. |
Jean-Paul Calderone | 4a68b40 | 2013-12-29 16:54:58 -0500 | [diff] [blame] | 2885 | """ |
Alex Gaynor | aceb3e2 | 2015-09-05 12:00:22 -0400 | [diff] [blame] | 2886 | |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 2887 | def test_bad_file_type(self): |
Jean-Paul Calderone | 4a68b40 | 2013-12-29 16:54:58 -0500 | [diff] [blame] | 2888 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 2889 | If the file type passed to `load_certificate_request` is neither |
| 2890 | `FILETYPE_PEM` nor `FILETYPE_ASN1` then `ValueError` is raised. |
Jean-Paul Calderone | 4a68b40 | 2013-12-29 16:54:58 -0500 | [diff] [blame] | 2891 | """ |
Alex Gaynor | 7778e79 | 2016-07-03 23:38:48 -0400 | [diff] [blame] | 2892 | with pytest.raises(ValueError): |
| 2893 | load_certificate_request(object(), b"") |
| 2894 | with pytest.raises(ValueError): |
| 2895 | load_certificate(object(), b"") |
Jean-Paul Calderone | 4a68b40 | 2013-12-29 16:54:58 -0500 | [diff] [blame] | 2896 | |
Alex Gaynor | 3772611 | 2016-07-04 09:51:32 -0400 | [diff] [blame] | 2897 | def test_bad_certificate(self): |
| 2898 | """ |
Alex Chan | 9e2a993 | 2017-01-25 14:29:19 +0000 | [diff] [blame] | 2899 | If the bytes passed to `load_certificate` are not a valid certificate, |
| 2900 | an exception is raised. |
Alex Gaynor | 3772611 | 2016-07-04 09:51:32 -0400 | [diff] [blame] | 2901 | """ |
| 2902 | with pytest.raises(Error): |
| 2903 | load_certificate(FILETYPE_ASN1, b"lol") |
| 2904 | |
Jean-Paul Calderone | 4a68b40 | 2013-12-29 16:54:58 -0500 | [diff] [blame] | 2905 | |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2906 | class TestPKCS7(object): |
Jean-Paul Calderone | 6864905 | 2009-07-17 21:14:27 -0400 | [diff] [blame] | 2907 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2908 | Tests for `PKCS7`. |
Jean-Paul Calderone | 6864905 | 2009-07-17 21:14:27 -0400 | [diff] [blame] | 2909 | """ |
Alex Gaynor | aceb3e2 | 2015-09-05 12:00:22 -0400 | [diff] [blame] | 2910 | |
Jean-Paul Calderone | 07c9374 | 2010-07-30 10:53:41 -0400 | [diff] [blame] | 2911 | def test_type_is_signed(self): |
Jean-Paul Calderone | aa6c069 | 2010-09-08 22:43:09 -0400 | [diff] [blame] | 2912 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2913 | `PKCS7.type_is_signed` returns `True` if the PKCS7 object is of |
| 2914 | the type *signed*. |
Jean-Paul Calderone | aa6c069 | 2010-09-08 22:43:09 -0400 | [diff] [blame] | 2915 | """ |
Jean-Paul Calderone | 07c9374 | 2010-07-30 10:53:41 -0400 | [diff] [blame] | 2916 | pkcs7 = load_pkcs7_data(FILETYPE_PEM, pkcs7Data) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2917 | assert pkcs7.type_is_signed() |
Jean-Paul Calderone | 07c9374 | 2010-07-30 10:53:41 -0400 | [diff] [blame] | 2918 | |
Jean-Paul Calderone | 07c9374 | 2010-07-30 10:53:41 -0400 | [diff] [blame] | 2919 | def test_type_is_enveloped(self): |
Jean-Paul Calderone | aa6c069 | 2010-09-08 22:43:09 -0400 | [diff] [blame] | 2920 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2921 | `PKCS7.type_is_enveloped` returns `False` if the PKCS7 object is not |
| 2922 | of the type *enveloped*. |
Jean-Paul Calderone | aa6c069 | 2010-09-08 22:43:09 -0400 | [diff] [blame] | 2923 | """ |
Jean-Paul Calderone | 07c9374 | 2010-07-30 10:53:41 -0400 | [diff] [blame] | 2924 | pkcs7 = load_pkcs7_data(FILETYPE_PEM, pkcs7Data) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2925 | assert not pkcs7.type_is_enveloped() |
Jean-Paul Calderone | 07c9374 | 2010-07-30 10:53:41 -0400 | [diff] [blame] | 2926 | |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2927 | def test_type_is_signed_and_enveloped(self): |
Jean-Paul Calderone | aa6c069 | 2010-09-08 22:43:09 -0400 | [diff] [blame] | 2928 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2929 | `PKCS7.type_is_signedAndEnveloped` returns `False` |
Alex Gaynor | 791212d | 2015-09-05 15:46:08 -0400 | [diff] [blame] | 2930 | if the PKCS7 object is not of the type *signed and enveloped*. |
Jean-Paul Calderone | aa6c069 | 2010-09-08 22:43:09 -0400 | [diff] [blame] | 2931 | """ |
Jean-Paul Calderone | 07c9374 | 2010-07-30 10:53:41 -0400 | [diff] [blame] | 2932 | pkcs7 = load_pkcs7_data(FILETYPE_PEM, pkcs7Data) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2933 | assert not pkcs7.type_is_signedAndEnveloped() |
Jean-Paul Calderone | 07c9374 | 2010-07-30 10:53:41 -0400 | [diff] [blame] | 2934 | |
Jean-Paul Calderone | b4754b9 | 2010-07-30 11:00:08 -0400 | [diff] [blame] | 2935 | def test_type_is_data(self): |
Jean-Paul Calderone | aa6c069 | 2010-09-08 22:43:09 -0400 | [diff] [blame] | 2936 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2937 | `PKCS7.type_is_data` returns `False` if the PKCS7 object is not of |
| 2938 | the type data. |
Jean-Paul Calderone | aa6c069 | 2010-09-08 22:43:09 -0400 | [diff] [blame] | 2939 | """ |
Jean-Paul Calderone | b4754b9 | 2010-07-30 11:00:08 -0400 | [diff] [blame] | 2940 | pkcs7 = load_pkcs7_data(FILETYPE_PEM, pkcs7Data) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2941 | assert not pkcs7.type_is_data() |
Jean-Paul Calderone | 97b28ca | 2010-07-30 10:56:07 -0400 | [diff] [blame] | 2942 | |
Jean-Paul Calderone | 4cbe05e | 2010-07-30 10:55:30 -0400 | [diff] [blame] | 2943 | def test_get_type_name(self): |
Jean-Paul Calderone | aa6c069 | 2010-09-08 22:43:09 -0400 | [diff] [blame] | 2944 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2945 | `PKCS7.get_type_name` returns a `str` giving the |
Alex Gaynor | 791212d | 2015-09-05 15:46:08 -0400 | [diff] [blame] | 2946 | type name. |
Jean-Paul Calderone | aa6c069 | 2010-09-08 22:43:09 -0400 | [diff] [blame] | 2947 | """ |
Jean-Paul Calderone | 4cbe05e | 2010-07-30 10:55:30 -0400 | [diff] [blame] | 2948 | pkcs7 = load_pkcs7_data(FILETYPE_PEM, pkcs7Data) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2949 | assert pkcs7.get_type_name() == b'pkcs7-signedData' |
Jean-Paul Calderone | 4cbe05e | 2010-07-30 10:55:30 -0400 | [diff] [blame] | 2950 | |
Jean-Paul Calderone | 4cbe05e | 2010-07-30 10:55:30 -0400 | [diff] [blame] | 2951 | def test_attribute(self): |
Jean-Paul Calderone | aa6c069 | 2010-09-08 22:43:09 -0400 | [diff] [blame] | 2952 | """ |
Alex Gaynor | 791212d | 2015-09-05 15:46:08 -0400 | [diff] [blame] | 2953 | If an attribute other than one of the methods tested here is accessed |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2954 | on an instance of `PKCS7`, `AttributeError` is raised. |
Jean-Paul Calderone | aa6c069 | 2010-09-08 22:43:09 -0400 | [diff] [blame] | 2955 | """ |
Jean-Paul Calderone | 4cbe05e | 2010-07-30 10:55:30 -0400 | [diff] [blame] | 2956 | pkcs7 = load_pkcs7_data(FILETYPE_PEM, pkcs7Data) |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2957 | with pytest.raises(AttributeError): |
| 2958 | pkcs7.foo |
Jean-Paul Calderone | 4cbe05e | 2010-07-30 10:55:30 -0400 | [diff] [blame] | 2959 | |
| 2960 | |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2961 | class TestNetscapeSPKI(_PKeyInteractionTestsMixin): |
Jean-Paul Calderone | dc138fa | 2009-06-27 14:32:07 -0400 | [diff] [blame] | 2962 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2963 | Tests for `OpenSSL.crypto.NetscapeSPKI`. |
Jean-Paul Calderone | dc138fa | 2009-06-27 14:32:07 -0400 | [diff] [blame] | 2964 | """ |
Alex Gaynor | aceb3e2 | 2015-09-05 12:00:22 -0400 | [diff] [blame] | 2965 | |
Jean-Paul Calderone | b972559 | 2010-08-03 18:17:22 -0400 | [diff] [blame] | 2966 | def signable(self): |
| 2967 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2968 | Return a new `NetscapeSPKI` for use with signing tests. |
Jean-Paul Calderone | b972559 | 2010-08-03 18:17:22 -0400 | [diff] [blame] | 2969 | """ |
| 2970 | return NetscapeSPKI() |
| 2971 | |
Jean-Paul Calderone | 6864905 | 2009-07-17 21:14:27 -0400 | [diff] [blame] | 2972 | def test_type(self): |
| 2973 | """ |
Alex Gaynor | 01f90a1 | 2019-02-07 09:14:48 -0500 | [diff] [blame] | 2974 | `NetscapeSPKI` can be used to create instances of that type. |
Jean-Paul Calderone | 6864905 | 2009-07-17 21:14:27 -0400 | [diff] [blame] | 2975 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2976 | assert is_consistent_type(NetscapeSPKI, 'NetscapeSPKI') |
Jean-Paul Calderone | 6864905 | 2009-07-17 21:14:27 -0400 | [diff] [blame] | 2977 | |
Jean-Paul Calderone | dc138fa | 2009-06-27 14:32:07 -0400 | [diff] [blame] | 2978 | def test_construction(self): |
| 2979 | """ |
Alex Gaynor | 01f90a1 | 2019-02-07 09:14:48 -0500 | [diff] [blame] | 2980 | `NetscapeSPKI` returns an instance of `NetscapeSPKI`. |
Jean-Paul Calderone | dc138fa | 2009-06-27 14:32:07 -0400 | [diff] [blame] | 2981 | """ |
| 2982 | nspki = NetscapeSPKI() |
Alex Gaynor | 01f90a1 | 2019-02-07 09:14:48 -0500 | [diff] [blame] | 2983 | assert isinstance(nspki, NetscapeSPKI) |
Jean-Paul Calderone | dc138fa | 2009-06-27 14:32:07 -0400 | [diff] [blame] | 2984 | |
Jean-Paul Calderone | 969efaa | 2010-08-03 18:19:19 -0400 | [diff] [blame] | 2985 | def test_invalid_attribute(self): |
| 2986 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2987 | Accessing a non-existent attribute of a `NetscapeSPKI` instance |
| 2988 | causes an `AttributeError` to be raised. |
Jean-Paul Calderone | 969efaa | 2010-08-03 18:19:19 -0400 | [diff] [blame] | 2989 | """ |
| 2990 | nspki = NetscapeSPKI() |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2991 | with pytest.raises(AttributeError): |
| 2992 | nspki.foo |
Jean-Paul Calderone | 969efaa | 2010-08-03 18:19:19 -0400 | [diff] [blame] | 2993 | |
Jean-Paul Calderone | 06ada9f | 2010-08-03 18:26:52 -0400 | [diff] [blame] | 2994 | def test_b64_encode(self): |
| 2995 | """ |
Alex Chan | b00ede2 | 2017-01-30 07:24:40 +0000 | [diff] [blame] | 2996 | `NetscapeSPKI.b64_encode` encodes the certificate to a base64 blob. |
Jean-Paul Calderone | 06ada9f | 2010-08-03 18:26:52 -0400 | [diff] [blame] | 2997 | """ |
| 2998 | nspki = NetscapeSPKI() |
| 2999 | blob = nspki.b64_encode() |
Alex Gaynor | 1257600 | 2019-11-18 00:18:50 -0500 | [diff] [blame] | 3000 | assert isinstance(blob, bytes) |
Jean-Paul Calderone | 06ada9f | 2010-08-03 18:26:52 -0400 | [diff] [blame] | 3001 | |
| 3002 | |
Paul Kehrer | 2c605ba | 2016-03-11 11:17:26 -0400 | [diff] [blame] | 3003 | class TestRevoked(object): |
| 3004 | """ |
Alex Chan | deec934 | 2016-12-19 22:00:38 +0000 | [diff] [blame] | 3005 | Tests for `OpenSSL.crypto.Revoked`. |
Paul Kehrer | 2c605ba | 2016-03-11 11:17:26 -0400 | [diff] [blame] | 3006 | """ |
| 3007 | def test_ignores_unsupported_revoked_cert_extension_get_reason(self): |
| 3008 | """ |
| 3009 | The get_reason method on the Revoked class checks to see if the |
| 3010 | extension is NID_crl_reason and should skip it otherwise. This test |
| 3011 | loads a CRL with extensions it should ignore. |
| 3012 | """ |
| 3013 | crl = load_crl(FILETYPE_PEM, crlDataUnsupportedExtension) |
| 3014 | revoked = crl.get_revoked() |
| 3015 | reason = revoked[1].get_reason() |
| 3016 | assert reason == b'Unspecified' |
| 3017 | |
| 3018 | def test_ignores_unsupported_revoked_cert_extension_set_new_reason(self): |
| 3019 | crl = load_crl(FILETYPE_PEM, crlDataUnsupportedExtension) |
| 3020 | revoked = crl.get_revoked() |
| 3021 | revoked[1].set_reason(None) |
| 3022 | reason = revoked[1].get_reason() |
| 3023 | assert reason is None |
| 3024 | |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 3025 | def test_construction(self): |
| 3026 | """ |
Alex Chan | deec934 | 2016-12-19 22:00:38 +0000 | [diff] [blame] | 3027 | Confirm we can create `OpenSSL.crypto.Revoked`. Check that it is |
| 3028 | empty. |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 3029 | """ |
| 3030 | revoked = Revoked() |
Alex Chan | deec934 | 2016-12-19 22:00:38 +0000 | [diff] [blame] | 3031 | assert isinstance(revoked, Revoked) |
| 3032 | assert type(revoked) == Revoked |
| 3033 | assert revoked.get_serial() == b'00' |
| 3034 | assert revoked.get_rev_date() is None |
| 3035 | assert revoked.get_reason() is None |
Jean-Paul Calderone | 4f74bfe | 2010-01-30 14:32:49 -0500 | [diff] [blame] | 3036 | |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 3037 | def test_serial(self): |
| 3038 | """ |
Jean-Paul Calderone | b98eae0 | 2010-01-30 13:18:04 -0500 | [diff] [blame] | 3039 | Confirm we can set and get serial numbers from |
Alex Chan | deec934 | 2016-12-19 22:00:38 +0000 | [diff] [blame] | 3040 | `OpenSSL.crypto.Revoked`. Confirm errors are handled with grace. |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 3041 | """ |
| 3042 | revoked = Revoked() |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 3043 | ret = revoked.set_serial(b'10b') |
Alex Chan | deec934 | 2016-12-19 22:00:38 +0000 | [diff] [blame] | 3044 | assert ret is None |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 3045 | ser = revoked.get_serial() |
Alex Chan | deec934 | 2016-12-19 22:00:38 +0000 | [diff] [blame] | 3046 | assert ser == b'010B' |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 3047 | |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 3048 | revoked.set_serial(b'31ppp') # a type error would be nice |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 3049 | ser = revoked.get_serial() |
Alex Chan | deec934 | 2016-12-19 22:00:38 +0000 | [diff] [blame] | 3050 | assert ser == b'31' |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 3051 | |
Alex Chan | deec934 | 2016-12-19 22:00:38 +0000 | [diff] [blame] | 3052 | with pytest.raises(ValueError): |
| 3053 | revoked.set_serial(b'pqrst') |
| 3054 | with pytest.raises(TypeError): |
| 3055 | revoked.set_serial(100) |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 3056 | |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 3057 | def test_date(self): |
| 3058 | """ |
Jean-Paul Calderone | b98eae0 | 2010-01-30 13:18:04 -0500 | [diff] [blame] | 3059 | Confirm we can set and get revocation dates from |
Alex Chan | deec934 | 2016-12-19 22:00:38 +0000 | [diff] [blame] | 3060 | `OpenSSL.crypto.Revoked`. Confirm errors are handled with grace. |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 3061 | """ |
| 3062 | revoked = Revoked() |
| 3063 | date = revoked.get_rev_date() |
Alex Chan | deec934 | 2016-12-19 22:00:38 +0000 | [diff] [blame] | 3064 | assert date is None |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 3065 | |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 3066 | now = datetime.now().strftime("%Y%m%d%H%M%SZ").encode("ascii") |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 3067 | ret = revoked.set_rev_date(now) |
Alex Chan | deec934 | 2016-12-19 22:00:38 +0000 | [diff] [blame] | 3068 | assert ret is None |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 3069 | date = revoked.get_rev_date() |
Alex Chan | deec934 | 2016-12-19 22:00:38 +0000 | [diff] [blame] | 3070 | assert date == now |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 3071 | |
Rick Dean | 6385faf | 2009-07-26 00:07:47 -0500 | [diff] [blame] | 3072 | def test_reason(self): |
| 3073 | """ |
Jean-Paul Calderone | b98eae0 | 2010-01-30 13:18:04 -0500 | [diff] [blame] | 3074 | Confirm we can set and get revocation reasons from |
Alex Chan | deec934 | 2016-12-19 22:00:38 +0000 | [diff] [blame] | 3075 | `OpenSSL.crypto.Revoked`. The "get" need to work as "set". |
| 3076 | Likewise, each reason of all_reasons() must work. |
Rick Dean | 6385faf | 2009-07-26 00:07:47 -0500 | [diff] [blame] | 3077 | """ |
| 3078 | revoked = Revoked() |
| 3079 | for r in revoked.all_reasons(): |
Jean-Paul Calderone | eacad4a | 2010-08-22 17:12:55 -0400 | [diff] [blame] | 3080 | for x in range(2): |
Rick Dean | 6385faf | 2009-07-26 00:07:47 -0500 | [diff] [blame] | 3081 | ret = revoked.set_reason(r) |
Alex Chan | deec934 | 2016-12-19 22:00:38 +0000 | [diff] [blame] | 3082 | assert ret is None |
Rick Dean | 6385faf | 2009-07-26 00:07:47 -0500 | [diff] [blame] | 3083 | reason = revoked.get_reason() |
Alex Chan | deec934 | 2016-12-19 22:00:38 +0000 | [diff] [blame] | 3084 | assert ( |
| 3085 | reason.lower().replace(b' ', b'') == |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 3086 | r.lower().replace(b' ', b'')) |
Alex Gaynor | aceb3e2 | 2015-09-05 12:00:22 -0400 | [diff] [blame] | 3087 | r = reason # again with the resp of get |
Rick Dean | 6385faf | 2009-07-26 00:07:47 -0500 | [diff] [blame] | 3088 | |
| 3089 | revoked.set_reason(None) |
Alex Chan | deec934 | 2016-12-19 22:00:38 +0000 | [diff] [blame] | 3090 | assert revoked.get_reason() is None |
Rick Dean | 6385faf | 2009-07-26 00:07:47 -0500 | [diff] [blame] | 3091 | |
Alex Chan | fb078d8 | 2017-04-20 11:16:15 +0100 | [diff] [blame] | 3092 | @pytest.mark.parametrize('reason', [object(), 1.0, u'foo']) |
| 3093 | def test_set_reason_wrong_args(self, reason): |
| 3094 | """ |
| 3095 | `Revoked.set_reason` raises `TypeError` if called with an argument |
| 3096 | which is neither `None` nor a byte string. |
| 3097 | """ |
| 3098 | revoked = Revoked() |
| 3099 | with pytest.raises(TypeError): |
| 3100 | revoked.set_reason(reason) |
| 3101 | |
Alex Chan | deec934 | 2016-12-19 22:00:38 +0000 | [diff] [blame] | 3102 | def test_set_reason_invalid_reason(self): |
Rick Dean | 6385faf | 2009-07-26 00:07:47 -0500 | [diff] [blame] | 3103 | """ |
Alex Chan | deec934 | 2016-12-19 22:00:38 +0000 | [diff] [blame] | 3104 | Calling `OpenSSL.crypto.Revoked.set_reason` with an argument which |
| 3105 | isn't a valid reason results in `ValueError` being raised. |
Rick Dean | 6385faf | 2009-07-26 00:07:47 -0500 | [diff] [blame] | 3106 | """ |
| 3107 | revoked = Revoked() |
Alex Chan | deec934 | 2016-12-19 22:00:38 +0000 | [diff] [blame] | 3108 | with pytest.raises(ValueError): |
| 3109 | revoked.set_reason(b'blue') |
Jean-Paul Calderone | 4f74bfe | 2010-01-30 14:32:49 -0500 | [diff] [blame] | 3110 | |
| 3111 | |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3112 | class TestCRL(object): |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 3113 | """ |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3114 | Tests for `OpenSSL.crypto.CRL`. |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 3115 | """ |
| 3116 | cert = load_certificate(FILETYPE_PEM, cleartextCertificatePEM) |
| 3117 | pkey = load_privatekey(FILETYPE_PEM, cleartextPrivateKeyPEM) |
| 3118 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 3119 | root_cert = load_certificate(FILETYPE_PEM, root_cert_pem) |
| 3120 | root_key = load_privatekey(FILETYPE_PEM, root_key_pem) |
| 3121 | intermediate_cert = load_certificate(FILETYPE_PEM, intermediate_cert_pem) |
| 3122 | intermediate_key = load_privatekey(FILETYPE_PEM, intermediate_key_pem) |
| 3123 | intermediate_server_cert = load_certificate( |
| 3124 | FILETYPE_PEM, intermediate_server_cert_pem) |
| 3125 | intermediate_server_key = load_privatekey( |
| 3126 | FILETYPE_PEM, intermediate_server_key_pem) |
| 3127 | |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 3128 | def test_construction(self): |
| 3129 | """ |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3130 | Confirm we can create `OpenSSL.crypto.CRL`. Check |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 3131 | that it is empty |
| 3132 | """ |
| 3133 | crl = CRL() |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3134 | assert isinstance(crl, CRL) |
| 3135 | assert crl.get_revoked() is None |
Jean-Paul Calderone | 2efd03e | 2010-01-30 13:59:55 -0500 | [diff] [blame] | 3136 | |
Jean-Paul Calderone | 6043279 | 2015-04-13 12:26:07 -0400 | [diff] [blame] | 3137 | def _get_crl(self): |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 3138 | """ |
Jean-Paul Calderone | 6043279 | 2015-04-13 12:26:07 -0400 | [diff] [blame] | 3139 | Get a new ``CRL`` with a revocation. |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 3140 | """ |
| 3141 | crl = CRL() |
| 3142 | revoked = Revoked() |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 3143 | now = datetime.now().strftime("%Y%m%d%H%M%SZ").encode("ascii") |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 3144 | revoked.set_rev_date(now) |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 3145 | revoked.set_serial(b'3ab') |
| 3146 | revoked.set_reason(b'sUpErSeDEd') |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 3147 | crl.add_revoked(revoked) |
Jean-Paul Calderone | 6043279 | 2015-04-13 12:26:07 -0400 | [diff] [blame] | 3148 | return crl |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 3149 | |
Jean-Paul Calderone | 6043279 | 2015-04-13 12:26:07 -0400 | [diff] [blame] | 3150 | def test_export_pem(self): |
| 3151 | """ |
| 3152 | If not passed a format, ``CRL.export`` returns a "PEM" format string |
| 3153 | representing a serial number, a revoked reason, and certificate issuer |
| 3154 | information. |
| 3155 | """ |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 3156 | # PEM format |
Paul Kehrer | 7d5a3bf | 2019-01-21 12:24:02 -0600 | [diff] [blame] | 3157 | dumped_crl = self._get_crl().export( |
Alex Gaynor | 173e4ba | 2017-06-30 08:01:12 -0700 | [diff] [blame] | 3158 | self.cert, self.pkey, days=20, digest=b"sha256" |
| 3159 | ) |
Paul Kehrer | 7d5a3bf | 2019-01-21 12:24:02 -0600 | [diff] [blame] | 3160 | crl = x509.load_pem_x509_crl(dumped_crl, backend) |
| 3161 | revoked = crl.get_revoked_certificate_by_serial_number(0x03AB) |
| 3162 | assert revoked is not None |
| 3163 | assert crl.issuer == x509.Name([ |
| 3164 | x509.NameAttribute(x509.NameOID.COUNTRY_NAME, u"US"), |
| 3165 | x509.NameAttribute(x509.NameOID.STATE_OR_PROVINCE_NAME, u"IL"), |
| 3166 | x509.NameAttribute(x509.NameOID.LOCALITY_NAME, u"Chicago"), |
| 3167 | x509.NameAttribute(x509.NameOID.ORGANIZATION_NAME, u"Testing"), |
| 3168 | x509.NameAttribute(x509.NameOID.COMMON_NAME, u"Testing Root CA"), |
| 3169 | ]) |
Jean-Paul Calderone | 6043279 | 2015-04-13 12:26:07 -0400 | [diff] [blame] | 3170 | |
Jean-Paul Calderone | 6043279 | 2015-04-13 12:26:07 -0400 | [diff] [blame] | 3171 | def test_export_der(self): |
| 3172 | """ |
| 3173 | If passed ``FILETYPE_ASN1`` for the format, ``CRL.export`` returns a |
| 3174 | "DER" format string representing a serial number, a revoked reason, and |
| 3175 | certificate issuer information. |
| 3176 | """ |
| 3177 | crl = self._get_crl() |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 3178 | |
| 3179 | # DER format |
Paul Kehrer | 7d5a3bf | 2019-01-21 12:24:02 -0600 | [diff] [blame] | 3180 | dumped_crl = self._get_crl().export( |
Alex Gaynor | 173e4ba | 2017-06-30 08:01:12 -0700 | [diff] [blame] | 3181 | self.cert, self.pkey, FILETYPE_ASN1, digest=b"md5" |
| 3182 | ) |
Paul Kehrer | 7d5a3bf | 2019-01-21 12:24:02 -0600 | [diff] [blame] | 3183 | crl = x509.load_der_x509_crl(dumped_crl, backend) |
| 3184 | revoked = crl.get_revoked_certificate_by_serial_number(0x03AB) |
| 3185 | assert revoked is not None |
| 3186 | assert crl.issuer == x509.Name([ |
| 3187 | x509.NameAttribute(x509.NameOID.COUNTRY_NAME, u"US"), |
| 3188 | x509.NameAttribute(x509.NameOID.STATE_OR_PROVINCE_NAME, u"IL"), |
| 3189 | x509.NameAttribute(x509.NameOID.LOCALITY_NAME, u"Chicago"), |
| 3190 | x509.NameAttribute(x509.NameOID.ORGANIZATION_NAME, u"Testing"), |
| 3191 | x509.NameAttribute(x509.NameOID.COMMON_NAME, u"Testing Root CA"), |
| 3192 | ]) |
Jean-Paul Calderone | 6043279 | 2015-04-13 12:26:07 -0400 | [diff] [blame] | 3193 | |
Alex Gaynor | e466bc9 | 2017-07-06 23:43:47 -0400 | [diff] [blame] | 3194 | # Flaky because we compare the output of running commands which sometimes |
| 3195 | # varies by 1 second |
| 3196 | @flaky.flaky |
Jean-Paul Calderone | 6043279 | 2015-04-13 12:26:07 -0400 | [diff] [blame] | 3197 | def test_export_text(self): |
| 3198 | """ |
| 3199 | If passed ``FILETYPE_TEXT`` for the format, ``CRL.export`` returns a |
| 3200 | text format string like the one produced by the openssl command line |
| 3201 | tool. |
| 3202 | """ |
| 3203 | crl = self._get_crl() |
| 3204 | |
Alex Gaynor | 173e4ba | 2017-06-30 08:01:12 -0700 | [diff] [blame] | 3205 | dumped_crl = crl.export( |
| 3206 | self.cert, self.pkey, FILETYPE_ASN1, digest=b"md5" |
| 3207 | ) |
Jean-Paul Calderone | 6043279 | 2015-04-13 12:26:07 -0400 | [diff] [blame] | 3208 | text = _runopenssl( |
Paul Kehrer | 7d5a3bf | 2019-01-21 12:24:02 -0600 | [diff] [blame] | 3209 | dumped_crl, b"crl", b"-noout", b"-text", b"-inform", b"DER", |
| 3210 | b"-nameopt", b"" |
Jean-Paul Calderone | 6043279 | 2015-04-13 12:26:07 -0400 | [diff] [blame] | 3211 | ) |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 3212 | |
| 3213 | # text format |
Alex Gaynor | 173e4ba | 2017-06-30 08:01:12 -0700 | [diff] [blame] | 3214 | dumped_text = crl.export( |
| 3215 | self.cert, self.pkey, type=FILETYPE_TEXT, digest=b"md5" |
| 3216 | ) |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3217 | assert text == dumped_text |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 3218 | |
Jean-Paul Calderone | 6043279 | 2015-04-13 12:26:07 -0400 | [diff] [blame] | 3219 | def test_export_custom_digest(self): |
| 3220 | """ |
| 3221 | If passed the name of a digest function, ``CRL.export`` uses a |
| 3222 | signature algorithm based on that digest function. |
| 3223 | """ |
| 3224 | crl = self._get_crl() |
Jean-Paul Calderone | cce22d0 | 2015-04-13 13:56:09 -0400 | [diff] [blame] | 3225 | dumped_crl = crl.export(self.cert, self.pkey, digest=b"sha1") |
Bulat Gaifullin | 1966c97 | 2014-09-22 09:47:20 +0400 | [diff] [blame] | 3226 | text = _runopenssl(dumped_crl, b"crl", b"-noout", b"-text") |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 3227 | text.index(b'Signature Algorithm: sha1') |
Bulat Gaifullin | 1966c97 | 2014-09-22 09:47:20 +0400 | [diff] [blame] | 3228 | |
Jean-Paul Calderone | cce22d0 | 2015-04-13 13:56:09 -0400 | [diff] [blame] | 3229 | def test_export_md5_digest(self): |
| 3230 | """ |
| 3231 | If passed md5 as the digest function, ``CRL.export`` uses md5 and does |
| 3232 | not emit a deprecation warning. |
| 3233 | """ |
| 3234 | crl = self._get_crl() |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3235 | with pytest.warns(None) as catcher: |
Jean-Paul Calderone | cce22d0 | 2015-04-13 13:56:09 -0400 | [diff] [blame] | 3236 | simplefilter("always") |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3237 | assert 0 == len(catcher) |
Jean-Paul Calderone | cce22d0 | 2015-04-13 13:56:09 -0400 | [diff] [blame] | 3238 | dumped_crl = crl.export(self.cert, self.pkey, digest=b"md5") |
| 3239 | text = _runopenssl(dumped_crl, b"crl", b"-noout", b"-text") |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 3240 | text.index(b'Signature Algorithm: md5') |
Jean-Paul Calderone | cce22d0 | 2015-04-13 13:56:09 -0400 | [diff] [blame] | 3241 | |
Jean-Paul Calderone | 6043279 | 2015-04-13 12:26:07 -0400 | [diff] [blame] | 3242 | def test_export_default_digest(self): |
| 3243 | """ |
Alex Gaynor | 173e4ba | 2017-06-30 08:01:12 -0700 | [diff] [blame] | 3244 | If not passed the name of a digest function, ``CRL.export`` raises a |
| 3245 | ``TypeError``. |
Jean-Paul Calderone | 6043279 | 2015-04-13 12:26:07 -0400 | [diff] [blame] | 3246 | """ |
| 3247 | crl = self._get_crl() |
Alex Gaynor | 173e4ba | 2017-06-30 08:01:12 -0700 | [diff] [blame] | 3248 | with pytest.raises(TypeError): |
| 3249 | crl.export(self.cert, self.pkey) |
Bulat Gaifullin | 1966c97 | 2014-09-22 09:47:20 +0400 | [diff] [blame] | 3250 | |
Jean-Paul Calderone | c7293bc | 2011-09-13 15:24:38 -0400 | [diff] [blame] | 3251 | def test_export_invalid(self): |
| 3252 | """ |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3253 | If `CRL.export` is used with an uninitialized `X509` instance, |
| 3254 | `OpenSSL.crypto.Error` is raised. |
Jean-Paul Calderone | c7293bc | 2011-09-13 15:24:38 -0400 | [diff] [blame] | 3255 | """ |
| 3256 | crl = CRL() |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3257 | with pytest.raises(Error): |
Alex Gaynor | 173e4ba | 2017-06-30 08:01:12 -0700 | [diff] [blame] | 3258 | crl.export(X509(), PKey(), digest=b"sha256") |
Jean-Paul Calderone | c7293bc | 2011-09-13 15:24:38 -0400 | [diff] [blame] | 3259 | |
Jean-Paul Calderone | 5651534 | 2010-01-30 13:49:38 -0500 | [diff] [blame] | 3260 | def test_add_revoked_keyword(self): |
| 3261 | """ |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3262 | `OpenSSL.CRL.add_revoked` accepts its single argument as the |
Jean-Paul Calderone | 64efa2c | 2011-09-11 10:00:09 -0400 | [diff] [blame] | 3263 | ``revoked`` keyword argument. |
Jean-Paul Calderone | 5651534 | 2010-01-30 13:49:38 -0500 | [diff] [blame] | 3264 | """ |
| 3265 | crl = CRL() |
| 3266 | revoked = Revoked() |
Paul Kehrer | b11bffc | 2016-03-10 18:30:29 -0400 | [diff] [blame] | 3267 | revoked.set_serial(b"01") |
Paul Kehrer | 2fe23b0 | 2016-03-09 22:02:15 -0400 | [diff] [blame] | 3268 | revoked.set_rev_date(b"20160310020145Z") |
Jean-Paul Calderone | 5651534 | 2010-01-30 13:49:38 -0500 | [diff] [blame] | 3269 | crl.add_revoked(revoked=revoked) |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3270 | assert isinstance(crl.get_revoked()[0], Revoked) |
Jean-Paul Calderone | 5651534 | 2010-01-30 13:49:38 -0500 | [diff] [blame] | 3271 | |
Jean-Paul Calderone | 883ca4b | 2010-01-30 13:55:13 -0500 | [diff] [blame] | 3272 | def test_export_wrong_args(self): |
| 3273 | """ |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3274 | Calling `OpenSSL.CRL.export` with arguments other than the certificate, |
Jean-Paul Calderone | f151586 | 2010-01-30 13:57:03 -0500 | [diff] [blame] | 3275 | private key, integer file type, and integer number of days it |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3276 | expects, results in a `TypeError` being raised. |
Jean-Paul Calderone | 883ca4b | 2010-01-30 13:55:13 -0500 | [diff] [blame] | 3277 | """ |
| 3278 | crl = CRL() |
Alex Gaynor | 85b4970 | 2015-09-05 16:30:59 -0400 | [diff] [blame] | 3279 | with pytest.raises(TypeError): |
| 3280 | crl.export(None, self.pkey, FILETYPE_PEM, 10) |
| 3281 | with pytest.raises(TypeError): |
| 3282 | crl.export(self.cert, None, FILETYPE_PEM, 10) |
| 3283 | with pytest.raises(TypeError): |
| 3284 | crl.export(self.cert, self.pkey, None, 10) |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3285 | with pytest.raises(TypeError): |
| 3286 | crl.export(self.cert, FILETYPE_PEM, None) |
Jean-Paul Calderone | f151586 | 2010-01-30 13:57:03 -0500 | [diff] [blame] | 3287 | |
Jean-Paul Calderone | ea19842 | 2010-01-30 13:58:23 -0500 | [diff] [blame] | 3288 | def test_export_unknown_filetype(self): |
| 3289 | """ |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3290 | Calling `OpenSSL.CRL.export` with a file type other than |
| 3291 | `FILETYPE_PEM`, `FILETYPE_ASN1`, or |
| 3292 | `FILETYPE_TEXT` results in a `ValueError` being raised. |
Jean-Paul Calderone | ea19842 | 2010-01-30 13:58:23 -0500 | [diff] [blame] | 3293 | """ |
| 3294 | crl = CRL() |
Alex Gaynor | 85b4970 | 2015-09-05 16:30:59 -0400 | [diff] [blame] | 3295 | with pytest.raises(ValueError): |
Alex Gaynor | 173e4ba | 2017-06-30 08:01:12 -0700 | [diff] [blame] | 3296 | crl.export(self.cert, self.pkey, 100, 10, digest=b"sha256") |
Jean-Paul Calderone | ea19842 | 2010-01-30 13:58:23 -0500 | [diff] [blame] | 3297 | |
Bulat Gaifullin | 925f786 | 2014-09-22 10:10:44 +0400 | [diff] [blame] | 3298 | def test_export_unknown_digest(self): |
Bulat Gaifullin | 5f9eea4 | 2014-09-23 19:35:15 +0400 | [diff] [blame] | 3299 | """ |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3300 | Calling `OpenSSL.CRL.export` with an unsupported digest results |
| 3301 | in a `ValueError` being raised. |
Bulat Gaifullin | 5f9eea4 | 2014-09-23 19:35:15 +0400 | [diff] [blame] | 3302 | """ |
Bulat Gaifullin | 925f786 | 2014-09-22 10:10:44 +0400 | [diff] [blame] | 3303 | crl = CRL() |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3304 | with pytest.raises(ValueError): |
| 3305 | crl.export( |
| 3306 | self.cert, self.pkey, FILETYPE_PEM, 10, b"strange-digest") |
Bulat Gaifullin | 925f786 | 2014-09-22 10:10:44 +0400 | [diff] [blame] | 3307 | |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 3308 | def test_get_revoked(self): |
| 3309 | """ |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3310 | Use python to create a simple CRL with two revocations. Get back the |
| 3311 | `Revoked` using `OpenSSL.CRL.get_revoked` and verify them. |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 3312 | """ |
| 3313 | crl = CRL() |
| 3314 | |
| 3315 | revoked = Revoked() |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 3316 | now = datetime.now().strftime("%Y%m%d%H%M%SZ").encode("ascii") |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 3317 | revoked.set_rev_date(now) |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 3318 | revoked.set_serial(b'3ab') |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 3319 | crl.add_revoked(revoked) |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 3320 | revoked.set_serial(b'100') |
| 3321 | revoked.set_reason(b'sUpErSeDEd') |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 3322 | crl.add_revoked(revoked) |
| 3323 | |
| 3324 | revs = crl.get_revoked() |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3325 | assert len(revs) == 2 |
| 3326 | assert type(revs[0]) == Revoked |
| 3327 | assert type(revs[1]) == Revoked |
| 3328 | assert revs[0].get_serial() == b'03AB' |
| 3329 | assert revs[1].get_serial() == b'0100' |
| 3330 | assert revs[0].get_rev_date() == now |
| 3331 | assert revs[1].get_rev_date() == now |
Jean-Paul Calderone | ecef6fa | 2010-01-30 13:47:18 -0500 | [diff] [blame] | 3332 | |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 3333 | def test_load_crl(self): |
| 3334 | """ |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3335 | Load a known CRL and inspect its revocations. Both EM and DER formats |
| 3336 | are loaded. |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 3337 | """ |
Jean-Paul Calderone | 3eb5cc7 | 2010-01-30 15:24:40 -0500 | [diff] [blame] | 3338 | crl = load_crl(FILETYPE_PEM, crlData) |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 3339 | revs = crl.get_revoked() |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3340 | assert len(revs) == 2 |
| 3341 | assert revs[0].get_serial() == b'03AB' |
| 3342 | assert revs[0].get_reason() is None |
| 3343 | assert revs[1].get_serial() == b'0100' |
| 3344 | assert revs[1].get_reason() == b'Superseded' |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 3345 | |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 3346 | der = _runopenssl(crlData, b"crl", b"-outform", b"DER") |
Jean-Paul Calderone | b98eae0 | 2010-01-30 13:18:04 -0500 | [diff] [blame] | 3347 | crl = load_crl(FILETYPE_ASN1, der) |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 3348 | revs = crl.get_revoked() |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3349 | assert len(revs) == 2 |
| 3350 | assert revs[0].get_serial() == b'03AB' |
| 3351 | assert revs[0].get_reason() is None |
| 3352 | assert revs[1].get_serial() == b'0100' |
| 3353 | assert revs[1].get_reason() == b'Superseded' |
Jean-Paul Calderone | 3eb5cc7 | 2010-01-30 15:24:40 -0500 | [diff] [blame] | 3354 | |
Jean-Paul Calderone | 3eb5cc7 | 2010-01-30 15:24:40 -0500 | [diff] [blame] | 3355 | def test_load_crl_bad_filetype(self): |
| 3356 | """ |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3357 | Calling `OpenSSL.crypto.load_crl` with an unknown file type raises a |
| 3358 | `ValueError`. |
Jean-Paul Calderone | 3eb5cc7 | 2010-01-30 15:24:40 -0500 | [diff] [blame] | 3359 | """ |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3360 | with pytest.raises(ValueError): |
| 3361 | load_crl(100, crlData) |
Jean-Paul Calderone | 3eb5cc7 | 2010-01-30 15:24:40 -0500 | [diff] [blame] | 3362 | |
Jean-Paul Calderone | 3eb5cc7 | 2010-01-30 15:24:40 -0500 | [diff] [blame] | 3363 | def test_load_crl_bad_data(self): |
| 3364 | """ |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3365 | Calling `OpenSSL.crypto.load_crl` with file data which can't be loaded |
| 3366 | raises a `OpenSSL.crypto.Error`. |
Jean-Paul Calderone | 3eb5cc7 | 2010-01-30 15:24:40 -0500 | [diff] [blame] | 3367 | """ |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3368 | with pytest.raises(Error): |
| 3369 | load_crl(FILETYPE_PEM, b"hello, world") |
Jean-Paul Calderone | 3eb5cc7 | 2010-01-30 15:24:40 -0500 | [diff] [blame] | 3370 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 3371 | def test_get_issuer(self): |
| 3372 | """ |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3373 | Load a known CRL and assert its issuer's common name is what we expect |
| 3374 | from the encoded crlData string. |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 3375 | """ |
| 3376 | crl = load_crl(FILETYPE_PEM, crlData) |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3377 | assert isinstance(crl.get_issuer(), X509Name) |
| 3378 | assert crl.get_issuer().CN == 'Testing Root CA' |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 3379 | |
Dominic Chen | f05b212 | 2015-10-13 16:32:35 +0000 | [diff] [blame] | 3380 | def test_dump_crl(self): |
| 3381 | """ |
| 3382 | The dumped CRL matches the original input. |
| 3383 | """ |
| 3384 | crl = load_crl(FILETYPE_PEM, crlData) |
| 3385 | buf = dump_crl(FILETYPE_PEM, crl) |
| 3386 | assert buf == crlData |
| 3387 | |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 3388 | def _make_test_crl(self, issuer_cert, issuer_key, certs=()): |
| 3389 | """ |
| 3390 | Create a CRL. |
| 3391 | |
| 3392 | :param list[X509] certs: A list of certificates to revoke. |
| 3393 | :rtype: CRL |
| 3394 | """ |
| 3395 | crl = CRL() |
| 3396 | for cert in certs: |
| 3397 | revoked = Revoked() |
| 3398 | # FIXME: This string splicing is an unfortunate implementation |
| 3399 | # detail that has been reported in |
| 3400 | # https://github.com/pyca/pyopenssl/issues/258 |
| 3401 | serial = hex(cert.get_serial_number())[2:].encode('utf-8') |
| 3402 | revoked.set_serial(serial) |
| 3403 | revoked.set_reason(b'unspecified') |
| 3404 | revoked.set_rev_date(b'20140601000000Z') |
| 3405 | crl.add_revoked(revoked) |
| 3406 | crl.set_version(1) |
| 3407 | crl.set_lastUpdate(b'20140601000000Z') |
| 3408 | crl.set_nextUpdate(b'20180601000000Z') |
| 3409 | crl.sign(issuer_cert, issuer_key, digest=b'sha512') |
| 3410 | return crl |
| 3411 | |
| 3412 | def test_verify_with_revoked(self): |
| 3413 | """ |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3414 | `verify_certificate` raises error when an intermediate certificate is |
| 3415 | revoked. |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 3416 | """ |
| 3417 | store = X509Store() |
| 3418 | store.add_cert(self.root_cert) |
| 3419 | store.add_cert(self.intermediate_cert) |
| 3420 | root_crl = self._make_test_crl( |
| 3421 | self.root_cert, self.root_key, certs=[self.intermediate_cert]) |
| 3422 | intermediate_crl = self._make_test_crl( |
| 3423 | self.intermediate_cert, self.intermediate_key, certs=[]) |
| 3424 | store.add_crl(root_crl) |
| 3425 | store.add_crl(intermediate_crl) |
| 3426 | store.set_flags( |
| 3427 | X509StoreFlags.CRL_CHECK | X509StoreFlags.CRL_CHECK_ALL) |
| 3428 | store_ctx = X509StoreContext(store, self.intermediate_server_cert) |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3429 | with pytest.raises(X509StoreContextError) as err: |
| 3430 | store_ctx.verify_certificate() |
| 3431 | assert err.value.args[0][2] == 'certificate revoked' |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 3432 | |
| 3433 | def test_verify_with_missing_crl(self): |
| 3434 | """ |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3435 | `verify_certificate` raises error when an intermediate certificate's |
| 3436 | CRL is missing. |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 3437 | """ |
| 3438 | store = X509Store() |
| 3439 | store.add_cert(self.root_cert) |
| 3440 | store.add_cert(self.intermediate_cert) |
| 3441 | root_crl = self._make_test_crl( |
| 3442 | self.root_cert, self.root_key, certs=[self.intermediate_cert]) |
| 3443 | store.add_crl(root_crl) |
| 3444 | store.set_flags( |
| 3445 | X509StoreFlags.CRL_CHECK | X509StoreFlags.CRL_CHECK_ALL) |
| 3446 | store_ctx = X509StoreContext(store, self.intermediate_server_cert) |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3447 | with pytest.raises(X509StoreContextError) as err: |
| 3448 | store_ctx.verify_certificate() |
| 3449 | assert err.value.args[0][2] == 'unable to get certificate CRL' |
| 3450 | assert err.value.certificate.get_subject().CN == 'intermediate-service' |
Dan Sully | 44e767a | 2016-06-04 18:05:27 -0700 | [diff] [blame] | 3451 | |
Paul Kehrer | 41c1024 | 2017-06-29 18:24:17 -0500 | [diff] [blame] | 3452 | def test_convert_from_cryptography(self): |
| 3453 | crypto_crl = x509.load_pem_x509_crl(crlData, backend) |
| 3454 | crl = CRL.from_cryptography(crypto_crl) |
| 3455 | assert isinstance(crl, CRL) |
| 3456 | |
| 3457 | def test_convert_from_cryptography_unsupported_type(self): |
| 3458 | with pytest.raises(TypeError): |
| 3459 | CRL.from_cryptography(object()) |
| 3460 | |
| 3461 | def test_convert_to_cryptography_key(self): |
| 3462 | crl = load_crl(FILETYPE_PEM, crlData) |
| 3463 | crypto_crl = crl.to_cryptography() |
| 3464 | assert isinstance(crypto_crl, x509.CertificateRevocationList) |
| 3465 | |
Jean-Paul Calderone | dc138fa | 2009-06-27 14:32:07 -0400 | [diff] [blame] | 3466 | |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3467 | class TestX509StoreContext(object): |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 3468 | """ |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3469 | Tests for `OpenSSL.crypto.X509StoreContext`. |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 3470 | """ |
| 3471 | root_cert = load_certificate(FILETYPE_PEM, root_cert_pem) |
| 3472 | intermediate_cert = load_certificate(FILETYPE_PEM, intermediate_cert_pem) |
Alex Gaynor | 3128750 | 2015-09-05 16:11:27 -0400 | [diff] [blame] | 3473 | intermediate_server_cert = load_certificate( |
| 3474 | FILETYPE_PEM, intermediate_server_cert_pem) |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 3475 | |
| 3476 | def test_valid(self): |
| 3477 | """ |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3478 | `verify_certificate` returns ``None`` when called with a certificate |
| 3479 | and valid chain. |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 3480 | """ |
| 3481 | store = X509Store() |
| 3482 | store.add_cert(self.root_cert) |
| 3483 | store.add_cert(self.intermediate_cert) |
| 3484 | store_ctx = X509StoreContext(store, self.intermediate_server_cert) |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3485 | assert store_ctx.verify_certificate() is None |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 3486 | |
| 3487 | def test_reuse(self): |
| 3488 | """ |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3489 | `verify_certificate` can be called multiple times with the same |
Jean-Paul Calderone | 06e01b9 | 2015-01-18 15:43:13 -0500 | [diff] [blame] | 3490 | ``X509StoreContext`` instance to produce the same result. |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 3491 | """ |
| 3492 | store = X509Store() |
| 3493 | store.add_cert(self.root_cert) |
| 3494 | store.add_cert(self.intermediate_cert) |
| 3495 | store_ctx = X509StoreContext(store, self.intermediate_server_cert) |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3496 | assert store_ctx.verify_certificate() is None |
| 3497 | assert store_ctx.verify_certificate() is None |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 3498 | |
| 3499 | def test_trusted_self_signed(self): |
| 3500 | """ |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3501 | `verify_certificate` returns ``None`` when called with a self-signed |
| 3502 | certificate and itself in the chain. |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 3503 | """ |
| 3504 | store = X509Store() |
| 3505 | store.add_cert(self.root_cert) |
| 3506 | store_ctx = X509StoreContext(store, self.root_cert) |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3507 | assert store_ctx.verify_certificate() is None |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 3508 | |
| 3509 | def test_untrusted_self_signed(self): |
| 3510 | """ |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3511 | `verify_certificate` raises error when a self-signed certificate is |
| 3512 | verified without itself in the chain. |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 3513 | """ |
| 3514 | store = X509Store() |
| 3515 | store_ctx = X509StoreContext(store, self.root_cert) |
Alex Gaynor | 85b4970 | 2015-09-05 16:30:59 -0400 | [diff] [blame] | 3516 | with pytest.raises(X509StoreContextError) as exc: |
| 3517 | store_ctx.verify_certificate() |
| 3518 | |
| 3519 | assert exc.value.args[0][2] == 'self signed certificate' |
| 3520 | assert exc.value.certificate.get_subject().CN == 'Testing Root CA' |
Jean-Paul Calderone | 517816e | 2015-01-18 15:39:26 -0500 | [diff] [blame] | 3521 | |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 3522 | def test_invalid_chain_no_root(self): |
| 3523 | """ |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3524 | `verify_certificate` raises error when a root certificate is missing |
| 3525 | from the chain. |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 3526 | """ |
| 3527 | store = X509Store() |
| 3528 | store.add_cert(self.intermediate_cert) |
| 3529 | store_ctx = X509StoreContext(store, self.intermediate_server_cert) |
Alex Gaynor | 85b4970 | 2015-09-05 16:30:59 -0400 | [diff] [blame] | 3530 | |
| 3531 | with pytest.raises(X509StoreContextError) as exc: |
| 3532 | store_ctx.verify_certificate() |
| 3533 | |
| 3534 | assert exc.value.args[0][2] == 'unable to get issuer certificate' |
| 3535 | assert exc.value.certificate.get_subject().CN == 'intermediate' |
Jean-Paul Calderone | 517816e | 2015-01-18 15:39:26 -0500 | [diff] [blame] | 3536 | |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 3537 | def test_invalid_chain_no_intermediate(self): |
| 3538 | """ |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3539 | `verify_certificate` raises error when an intermediate certificate is |
| 3540 | missing from the chain. |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 3541 | """ |
| 3542 | store = X509Store() |
| 3543 | store.add_cert(self.root_cert) |
| 3544 | store_ctx = X509StoreContext(store, self.intermediate_server_cert) |
Jean-Paul Calderone | 517816e | 2015-01-18 15:39:26 -0500 | [diff] [blame] | 3545 | |
Alex Gaynor | 85b4970 | 2015-09-05 16:30:59 -0400 | [diff] [blame] | 3546 | with pytest.raises(X509StoreContextError) as exc: |
| 3547 | store_ctx.verify_certificate() |
| 3548 | |
| 3549 | assert exc.value.args[0][2] == 'unable to get local issuer certificate' |
| 3550 | assert exc.value.certificate.get_subject().CN == 'intermediate-service' |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 3551 | |
Stephen Holsapple | 46a0925 | 2015-02-12 14:45:43 -0800 | [diff] [blame] | 3552 | def test_modification_pre_verify(self): |
| 3553 | """ |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3554 | `verify_certificate` can use a store context modified after |
Stephen Holsapple | 46a0925 | 2015-02-12 14:45:43 -0800 | [diff] [blame] | 3555 | instantiation. |
| 3556 | """ |
| 3557 | store_bad = X509Store() |
| 3558 | store_bad.add_cert(self.intermediate_cert) |
| 3559 | store_good = X509Store() |
| 3560 | store_good.add_cert(self.root_cert) |
| 3561 | store_good.add_cert(self.intermediate_cert) |
| 3562 | store_ctx = X509StoreContext(store_bad, self.intermediate_server_cert) |
Alex Gaynor | 85b4970 | 2015-09-05 16:30:59 -0400 | [diff] [blame] | 3563 | |
| 3564 | with pytest.raises(X509StoreContextError) as exc: |
| 3565 | store_ctx.verify_certificate() |
| 3566 | |
| 3567 | assert exc.value.args[0][2] == 'unable to get issuer certificate' |
| 3568 | assert exc.value.certificate.get_subject().CN == 'intermediate' |
| 3569 | |
Stephen Holsapple | 46a0925 | 2015-02-12 14:45:43 -0800 | [diff] [blame] | 3570 | store_ctx.set_store(store_good) |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3571 | assert store_ctx.verify_certificate() is None |
Stephen Holsapple | 46a0925 | 2015-02-12 14:45:43 -0800 | [diff] [blame] | 3572 | |
Thomas Sileo | e15e60a | 2016-11-22 18:13:30 +0100 | [diff] [blame] | 3573 | def test_verify_with_time(self): |
| 3574 | """ |
| 3575 | `verify_certificate` raises error when the verification time is |
| 3576 | set at notAfter. |
| 3577 | """ |
| 3578 | store = X509Store() |
| 3579 | store.add_cert(self.root_cert) |
| 3580 | store.add_cert(self.intermediate_cert) |
| 3581 | |
| 3582 | expire_time = self.intermediate_server_cert.get_notAfter() |
| 3583 | expire_datetime = datetime.strptime( |
| 3584 | expire_time.decode('utf-8'), '%Y%m%d%H%M%SZ' |
| 3585 | ) |
| 3586 | store.set_time(expire_datetime) |
| 3587 | |
| 3588 | store_ctx = X509StoreContext(store, self.intermediate_server_cert) |
| 3589 | with pytest.raises(X509StoreContextError) as exc: |
| 3590 | store_ctx.verify_certificate() |
| 3591 | |
| 3592 | assert exc.value.args[0][2] == 'certificate has expired' |
| 3593 | |
Stephen Holsapple | 46a0925 | 2015-02-12 14:45:43 -0800 | [diff] [blame] | 3594 | |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3595 | class TestSignVerify(object): |
James Yonan | 7c2e5d3 | 2010-02-27 05:45:50 -0700 | [diff] [blame] | 3596 | """ |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3597 | Tests for `OpenSSL.crypto.sign` and `OpenSSL.crypto.verify`. |
James Yonan | 7c2e5d3 | 2010-02-27 05:45:50 -0700 | [diff] [blame] | 3598 | """ |
Alex Gaynor | aceb3e2 | 2015-09-05 12:00:22 -0400 | [diff] [blame] | 3599 | |
James Yonan | 7c2e5d3 | 2010-02-27 05:45:50 -0700 | [diff] [blame] | 3600 | def test_sign_verify(self): |
Jean-Paul Calderone | f3cb9d8 | 2010-06-22 10:29:33 -0400 | [diff] [blame] | 3601 | """ |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3602 | `sign` generates a cryptographic signature which `verify` can check. |
Jean-Paul Calderone | f3cb9d8 | 2010-06-22 10:29:33 -0400 | [diff] [blame] | 3603 | """ |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 3604 | content = ( |
| 3605 | b"It was a bright cold day in April, and the clocks were striking " |
| 3606 | b"thirteen. Winston Smith, his chin nuzzled into his breast in an " |
| 3607 | b"effort to escape the vile wind, slipped quickly through the " |
| 3608 | b"glass doors of Victory Mansions, though not quickly enough to " |
| 3609 | b"prevent a swirl of gritty dust from entering along with him.") |
Jean-Paul Calderone | b98ce21 | 2010-06-22 09:46:27 -0400 | [diff] [blame] | 3610 | |
| 3611 | # sign the content with this private key |
Jean-Paul Calderone | f3cb9d8 | 2010-06-22 10:29:33 -0400 | [diff] [blame] | 3612 | priv_key = load_privatekey(FILETYPE_PEM, root_key_pem) |
Jean-Paul Calderone | b98ce21 | 2010-06-22 09:46:27 -0400 | [diff] [blame] | 3613 | # verify the content with this cert |
| 3614 | good_cert = load_certificate(FILETYPE_PEM, root_cert_pem) |
| 3615 | # certificate unrelated to priv_key, used to trigger an error |
| 3616 | bad_cert = load_certificate(FILETYPE_PEM, server_cert_pem) |
James Yonan | 7c2e5d3 | 2010-02-27 05:45:50 -0700 | [diff] [blame] | 3617 | |
Jean-Paul Calderone | d08feb0 | 2010-10-12 21:53:24 -0400 | [diff] [blame] | 3618 | for digest in ['md5', 'sha1']: |
James Yonan | 7c2e5d3 | 2010-02-27 05:45:50 -0700 | [diff] [blame] | 3619 | sig = sign(priv_key, content, digest) |
| 3620 | |
Alex Gaynor | aceb3e2 | 2015-09-05 12:00:22 -0400 | [diff] [blame] | 3621 | # Verify the signature of content, will throw an exception if |
| 3622 | # error. |
James Yonan | 7c2e5d3 | 2010-02-27 05:45:50 -0700 | [diff] [blame] | 3623 | verify(good_cert, sig, content, digest) |
| 3624 | |
| 3625 | # This should fail because the certificate doesn't match the |
| 3626 | # private key that was used to sign the content. |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3627 | with pytest.raises(Error): |
| 3628 | verify(bad_cert, sig, content, digest) |
James Yonan | 7c2e5d3 | 2010-02-27 05:45:50 -0700 | [diff] [blame] | 3629 | |
| 3630 | # This should fail because we've "tainted" the content after |
| 3631 | # signing it. |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3632 | with pytest.raises(Error): |
| 3633 | verify(good_cert, sig, content + b"tainted", digest) |
James Yonan | 7c2e5d3 | 2010-02-27 05:45:50 -0700 | [diff] [blame] | 3634 | |
| 3635 | # test that unknown digest types fail |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3636 | with pytest.raises(ValueError): |
| 3637 | sign(priv_key, content, "strange-digest") |
| 3638 | with pytest.raises(ValueError): |
| 3639 | verify(good_cert, sig, content, "strange-digest") |
James Yonan | 7c2e5d3 | 2010-02-27 05:45:50 -0700 | [diff] [blame] | 3640 | |
Abraham Martin | c5484ba | 2015-03-25 15:33:05 +0000 | [diff] [blame] | 3641 | def test_sign_verify_with_text(self): |
| 3642 | """ |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3643 | `sign` generates a cryptographic signature which |
| 3644 | `verify` can check. Deprecation warnings raised because using |
Alex Gaynor | 791212d | 2015-09-05 15:46:08 -0400 | [diff] [blame] | 3645 | text instead of bytes as content |
Abraham Martin | c5484ba | 2015-03-25 15:33:05 +0000 | [diff] [blame] | 3646 | """ |
Jean-Paul Calderone | 6462b07 | 2015-03-29 07:03:11 -0400 | [diff] [blame] | 3647 | content = ( |
Jean-Paul Calderone | 362c1f5 | 2015-03-29 08:01:39 -0400 | [diff] [blame] | 3648 | b"It was a bright cold day in April, and the clocks were striking " |
| 3649 | b"thirteen. Winston Smith, his chin nuzzled into his breast in an " |
| 3650 | b"effort to escape the vile wind, slipped quickly through the " |
| 3651 | b"glass doors of Victory Mansions, though not quickly enough to " |
| 3652 | b"prevent a swirl of gritty dust from entering along with him." |
Jean-Paul Calderone | 13a0e65 | 2015-03-29 07:58:51 -0400 | [diff] [blame] | 3653 | ).decode("ascii") |
Abraham Martin | c5484ba | 2015-03-25 15:33:05 +0000 | [diff] [blame] | 3654 | |
| 3655 | priv_key = load_privatekey(FILETYPE_PEM, root_key_pem) |
| 3656 | cert = load_certificate(FILETYPE_PEM, root_cert_pem) |
| 3657 | for digest in ['md5', 'sha1']: |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3658 | with pytest.warns(DeprecationWarning) as w: |
Abraham Martin | c5484ba | 2015-03-25 15:33:05 +0000 | [diff] [blame] | 3659 | simplefilter("always") |
| 3660 | sig = sign(priv_key, content, digest) |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3661 | assert ( |
| 3662 | "{0} for data is no longer accepted, use bytes".format( |
| 3663 | WARNING_TYPE_EXPECTED |
| 3664 | ) == str(w[-1].message)) |
Jean-Paul Calderone | 6462b07 | 2015-03-29 07:03:11 -0400 | [diff] [blame] | 3665 | |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3666 | with pytest.warns(DeprecationWarning) as w: |
Abraham Martin | c5484ba | 2015-03-25 15:33:05 +0000 | [diff] [blame] | 3667 | simplefilter("always") |
| 3668 | verify(cert, sig, content, digest) |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3669 | assert ( |
| 3670 | "{0} for data is no longer accepted, use bytes".format( |
| 3671 | WARNING_TYPE_EXPECTED |
| 3672 | ) == str(w[-1].message)) |
Abraham Martin | c5484ba | 2015-03-25 15:33:05 +0000 | [diff] [blame] | 3673 | |
Paul Kehrer | 59d2625 | 2017-07-20 10:45:54 +0200 | [diff] [blame] | 3674 | def test_sign_verify_ecdsa(self): |
| 3675 | """ |
| 3676 | `sign` generates a cryptographic signature which `verify` can check. |
| 3677 | ECDSA Signatures in the X9.62 format may have variable length, |
| 3678 | different from the length of the private key. |
| 3679 | """ |
| 3680 | content = ( |
| 3681 | b"It was a bright cold day in April, and the clocks were striking " |
| 3682 | b"thirteen. Winston Smith, his chin nuzzled into his breast in an " |
| 3683 | b"effort to escape the vile wind, slipped quickly through the " |
| 3684 | b"glass doors of Victory Mansions, though not quickly enough to " |
| 3685 | b"prevent a swirl of gritty dust from entering along with him." |
| 3686 | ).decode("ascii") |
| 3687 | priv_key = load_privatekey(FILETYPE_PEM, ec_root_key_pem) |
| 3688 | cert = load_certificate(FILETYPE_PEM, ec_root_cert_pem) |
| 3689 | sig = sign(priv_key, content, "sha1") |
| 3690 | verify(cert, sig, content, "sha1") |
| 3691 | |
Jean-Paul Calderone | 9828f66 | 2010-12-08 22:57:26 -0500 | [diff] [blame] | 3692 | def test_sign_nulls(self): |
| 3693 | """ |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3694 | `sign` produces a signature for a string with embedded nulls. |
Jean-Paul Calderone | 9828f66 | 2010-12-08 22:57:26 -0500 | [diff] [blame] | 3695 | """ |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 3696 | content = b"Watch out! \0 Did you see it?" |
Jean-Paul Calderone | 9828f66 | 2010-12-08 22:57:26 -0500 | [diff] [blame] | 3697 | priv_key = load_privatekey(FILETYPE_PEM, root_key_pem) |
| 3698 | good_cert = load_certificate(FILETYPE_PEM, root_cert_pem) |
| 3699 | sig = sign(priv_key, content, "sha1") |
| 3700 | verify(good_cert, sig, content, "sha1") |
| 3701 | |
Colleen Murphy | e09399b | 2016-03-01 17:40:49 -0800 | [diff] [blame] | 3702 | def test_sign_with_large_key(self): |
| 3703 | """ |
Alex Chan | 7be83a5 | 2017-01-24 15:19:29 +0000 | [diff] [blame] | 3704 | `sign` produces a signature for a string when using a long key. |
Colleen Murphy | e09399b | 2016-03-01 17:40:49 -0800 | [diff] [blame] | 3705 | """ |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 3706 | content = ( |
| 3707 | b"It was a bright cold day in April, and the clocks were striking " |
| 3708 | b"thirteen. Winston Smith, his chin nuzzled into his breast in an " |
| 3709 | b"effort to escape the vile wind, slipped quickly through the " |
| 3710 | b"glass doors of Victory Mansions, though not quickly enough to " |
| 3711 | b"prevent a swirl of gritty dust from entering along with him.") |
Colleen Murphy | e09399b | 2016-03-01 17:40:49 -0800 | [diff] [blame] | 3712 | |
| 3713 | priv_key = load_privatekey(FILETYPE_PEM, large_key_pem) |
| 3714 | sign(priv_key, content, "sha1") |
| 3715 | |
Jean-Paul Calderone | 9828f66 | 2010-12-08 22:57:26 -0500 | [diff] [blame] | 3716 | |
Alex Chan | 63ef9bc | 2016-12-19 12:02:06 +0000 | [diff] [blame] | 3717 | class TestEllipticCurve(object): |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 3718 | """ |
Alex Chan | 63ef9bc | 2016-12-19 12:02:06 +0000 | [diff] [blame] | 3719 | Tests for `_EllipticCurve`, `get_elliptic_curve`, and |
| 3720 | `get_elliptic_curves`. |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 3721 | """ |
Alex Gaynor | aceb3e2 | 2015-09-05 12:00:22 -0400 | [diff] [blame] | 3722 | |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 3723 | def test_set(self): |
| 3724 | """ |
Alex Chan | 63ef9bc | 2016-12-19 12:02:06 +0000 | [diff] [blame] | 3725 | `get_elliptic_curves` returns a `set`. |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 3726 | """ |
Alex Chan | 63ef9bc | 2016-12-19 12:02:06 +0000 | [diff] [blame] | 3727 | assert isinstance(get_elliptic_curves(), set) |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 3728 | |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 3729 | def test_a_curve(self): |
| 3730 | """ |
Alex Chan | 63ef9bc | 2016-12-19 12:02:06 +0000 | [diff] [blame] | 3731 | `get_elliptic_curve` can be used to retrieve a particular supported |
| 3732 | curve. |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 3733 | """ |
| 3734 | curves = get_elliptic_curves() |
Alex Chan | 63ef9bc | 2016-12-19 12:02:06 +0000 | [diff] [blame] | 3735 | curve = next(iter(curves)) |
| 3736 | assert curve.name == get_elliptic_curve(curve.name).name |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 3737 | |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 3738 | def test_not_a_curve(self): |
| 3739 | """ |
Alex Chan | 63ef9bc | 2016-12-19 12:02:06 +0000 | [diff] [blame] | 3740 | `get_elliptic_curve` raises `ValueError` if called with a name which |
| 3741 | does not identify a supported curve. |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 3742 | """ |
Alex Chan | 63ef9bc | 2016-12-19 12:02:06 +0000 | [diff] [blame] | 3743 | with pytest.raises(ValueError): |
| 3744 | get_elliptic_curve(u"this curve was just invented") |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 3745 | |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 3746 | def test_repr(self): |
| 3747 | """ |
| 3748 | The string representation of a curve object includes simply states the |
| 3749 | object is a curve and what its name is. |
| 3750 | """ |
| 3751 | curves = get_elliptic_curves() |
Alex Chan | 63ef9bc | 2016-12-19 12:02:06 +0000 | [diff] [blame] | 3752 | curve = next(iter(curves)) |
| 3753 | assert "<Curve %r>" % (curve.name,) == repr(curve) |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 3754 | |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 3755 | def test_to_EC_KEY(self): |
| 3756 | """ |
| 3757 | The curve object can export a version of itself as an EC_KEY* via the |
Alex Chan | 63ef9bc | 2016-12-19 12:02:06 +0000 | [diff] [blame] | 3758 | private `_EllipticCurve._to_EC_KEY`. |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 3759 | """ |
| 3760 | curves = get_elliptic_curves() |
Alex Chan | 63ef9bc | 2016-12-19 12:02:06 +0000 | [diff] [blame] | 3761 | curve = next(iter(curves)) |
| 3762 | # It's not easy to assert anything about this object. However, see |
| 3763 | # leakcheck/crypto.py for a test that demonstrates it at least does |
| 3764 | # not leak memory. |
| 3765 | curve._to_EC_KEY() |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 3766 | |
| 3767 | |
Jean-Paul Calderone | 1be7708 | 2014-04-30 18:17:41 -0400 | [diff] [blame] | 3768 | class EllipticCurveFactory(object): |
| 3769 | """ |
| 3770 | A helper to get the names of two curves. |
| 3771 | """ |
Alex Gaynor | aceb3e2 | 2015-09-05 12:00:22 -0400 | [diff] [blame] | 3772 | |
Jean-Paul Calderone | 1be7708 | 2014-04-30 18:17:41 -0400 | [diff] [blame] | 3773 | def __init__(self): |
| 3774 | curves = iter(get_elliptic_curves()) |
Alex Chan | 63ef9bc | 2016-12-19 12:02:06 +0000 | [diff] [blame] | 3775 | self.curve_name = next(curves).name |
| 3776 | self.another_curve_name = next(curves).name |
Jean-Paul Calderone | 1be7708 | 2014-04-30 18:17:41 -0400 | [diff] [blame] | 3777 | |
| 3778 | |
Alex Chan | 63ef9bc | 2016-12-19 12:02:06 +0000 | [diff] [blame] | 3779 | class TestEllipticCurveEquality(EqualityTestsMixin): |
Jean-Paul Calderone | 1be7708 | 2014-04-30 18:17:41 -0400 | [diff] [blame] | 3780 | """ |
Paul Kehrer | 7d5a3bf | 2019-01-21 12:24:02 -0600 | [diff] [blame] | 3781 | Tests `_EllipticCurve`'s implementation of ``==`` and ``!=``. |
Jean-Paul Calderone | 1be7708 | 2014-04-30 18:17:41 -0400 | [diff] [blame] | 3782 | """ |
| 3783 | curve_factory = EllipticCurveFactory() |
| 3784 | |
| 3785 | if curve_factory.curve_name is None: |
| 3786 | skip = "There are no curves available there can be no curve objects." |
| 3787 | |
Jean-Paul Calderone | 1be7708 | 2014-04-30 18:17:41 -0400 | [diff] [blame] | 3788 | def anInstance(self): |
| 3789 | """ |
| 3790 | Get the curve object for an arbitrary curve supported by the system. |
| 3791 | """ |
| 3792 | return get_elliptic_curve(self.curve_factory.curve_name) |
| 3793 | |
Jean-Paul Calderone | 1be7708 | 2014-04-30 18:17:41 -0400 | [diff] [blame] | 3794 | def anotherInstance(self): |
| 3795 | """ |
| 3796 | Get the curve object for an arbitrary curve supported by the system - |
| 3797 | but not the one returned by C{anInstance}. |
| 3798 | """ |
| 3799 | return get_elliptic_curve(self.curve_factory.another_curve_name) |
| 3800 | |
| 3801 | |
Alex Chan | 63ef9bc | 2016-12-19 12:02:06 +0000 | [diff] [blame] | 3802 | class TestEllipticCurveHash(object): |
Jean-Paul Calderone | 22c3124 | 2014-05-01 07:49:47 -0400 | [diff] [blame] | 3803 | """ |
Alex Chan | 63ef9bc | 2016-12-19 12:02:06 +0000 | [diff] [blame] | 3804 | Tests for `_EllipticCurve`'s implementation of hashing (thus use as |
| 3805 | an item in a `dict` or `set`). |
Jean-Paul Calderone | 22c3124 | 2014-05-01 07:49:47 -0400 | [diff] [blame] | 3806 | """ |
| 3807 | curve_factory = EllipticCurveFactory() |
| 3808 | |
| 3809 | if curve_factory.curve_name is None: |
| 3810 | skip = "There are no curves available there can be no curve objects." |
| 3811 | |
Jean-Paul Calderone | 22c3124 | 2014-05-01 07:49:47 -0400 | [diff] [blame] | 3812 | def test_contains(self): |
| 3813 | """ |
Alex Chan | 63ef9bc | 2016-12-19 12:02:06 +0000 | [diff] [blame] | 3814 | The ``in`` operator reports that a `set` containing a curve does |
| 3815 | contain that curve. |
Jean-Paul Calderone | 22c3124 | 2014-05-01 07:49:47 -0400 | [diff] [blame] | 3816 | """ |
| 3817 | curve = get_elliptic_curve(self.curve_factory.curve_name) |
| 3818 | curves = set([curve]) |
Alex Chan | 63ef9bc | 2016-12-19 12:02:06 +0000 | [diff] [blame] | 3819 | assert curve in curves |
Jean-Paul Calderone | 22c3124 | 2014-05-01 07:49:47 -0400 | [diff] [blame] | 3820 | |
Jean-Paul Calderone | 22c3124 | 2014-05-01 07:49:47 -0400 | [diff] [blame] | 3821 | def test_does_not_contain(self): |
| 3822 | """ |
Alex Chan | 63ef9bc | 2016-12-19 12:02:06 +0000 | [diff] [blame] | 3823 | The ``in`` operator reports that a `set` not containing a curve |
| 3824 | does not contain that curve. |
Jean-Paul Calderone | 22c3124 | 2014-05-01 07:49:47 -0400 | [diff] [blame] | 3825 | """ |
| 3826 | curve = get_elliptic_curve(self.curve_factory.curve_name) |
Alex Gaynor | 85b4970 | 2015-09-05 16:30:59 -0400 | [diff] [blame] | 3827 | curves = set([ |
| 3828 | get_elliptic_curve(self.curve_factory.another_curve_name) |
| 3829 | ]) |
Alex Chan | 63ef9bc | 2016-12-19 12:02:06 +0000 | [diff] [blame] | 3830 | assert curve not in curves |