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