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