|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * This file is part of the PHPASN1 library. |
| 4 | + * |
| 5 | + * Copyright © Friedrich Große <[email protected]> |
| 6 | + * |
| 7 | + * For the full copyright and license information, please view the LICENSE |
| 8 | + * file that was distributed with this source code. |
| 9 | + */ |
| 10 | + |
| 11 | +namespace FG\Test\X509\CSR; |
| 12 | + |
| 13 | +use FG\ASN1\ASNObject; |
| 14 | +use FG\X509\CSR\CSR; |
| 15 | +use FG\Test\ASN1TestCase; |
| 16 | +use FG\ASN1\OID; |
| 17 | + |
| 18 | +class CSRSignatureTest extends ASN1TestCase |
| 19 | +{ |
| 20 | + private $pem = '-----BEGIN RSA PRIVATE KEY----- |
| 21 | +MIIEpAIBAAKCAQEA06GG0gy12nbyp5yiuNfsgBAt4uRWmUgkA1kQuCdDoFhLTjMs |
| 22 | +ROreQJGKVbyPMiY1HwML/XaC0NRZtqMssrwmKvh7KGb6QEmz7dCVdk/eU7IF480/ |
| 23 | +93BZmGYH2sdqG40pTwblagw8D21Cx+wTwHHkAmX4Rzc86Cw+GPSAgh0kD9TygEtJ |
| 24 | +xcA/xd/NUHj5FlwDclxGZH7B89g/OSVWOqrOYFpQfl5SSTAMc1bgyUQMW1rh5sIR |
| 25 | +W0xuCUCIcBsaoibYGE/R/5TPdoMhqYAmbyXlJAED8eozIeY5XKaWe+jnr+XlRkFY |
| 26 | +UOJbkt+91slQ+WS7zWMS2UVzkZT7ha7cgR+fIwIDAQABAoIBADCjUO75iNn6uG4Z |
| 27 | +K7S7u1j7XvBkdhqoX859K8CHFZ3GbRg93pDAQfApAtgNjAOEkHTeKGVKVWpVQ4Ec |
| 28 | +I2u4njUGApgYgq8/wSCI7bDRTlGB+qSOSYM+yPijim6XoiVO3g2Rkiw7P5p0DAMS |
| 29 | +mL+D7Vk3wkXrxg2+DU+C8f31YJJafY+1IW1GfNWsbUxzzN4k31Txxo3FNI0t+MlV |
| 30 | +tzmdwxQYiHXrHa5etqidH2KP/uelkyHPBNp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/LtPB4cJgZeUx91Yod1Fg2C04Nbnw== |
| 46 | +-----END RSA PRIVATE KEY-----'; |
| 47 | + |
| 48 | + private $cert = '-----BEGIN CERTIFICATE REQUEST----- |
| 49 | +MIICRzCCAbMCAQAwgYcxCzAJBgNVBAYTAlVTMQkwBwYDVQQIEwAxFjAUBgNVBAcT |
| 50 | +DVNhbiBGcmFuc2lzY28xDzANBgNVBAoTBkdpdGh1YjELMAkGA1UECxMCSVQxEzAR |
| 51 | +BgNVBAMTCmdpdGh1Yi5vcmcxIjAgBgkqhkiG9w0BCQEWE25vLXJlcGx5QGdpdGh1 |
| 52 | +Yi5vcmcwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDToYbSDLXadvKn |
| 53 | +nKK41+yAEC3i5FaZSCQDWRC4J0OgWEtOMyxE6t5AkYpVvI8yJjUfAwv9doLQ1Fm2 |
| 54 | +oyyyvCYq+HsoZvpASbPt0JV2T95TsgXjzT/3cFmYZgfax2objSlPBuVqDDwPbULH |
| 55 | +7BPAceQCZfhHNzzoLD4Y9ICCHSQP1PKAS0nFwD/F381QePkWXANyXEZkfsHz2D85 |
| 56 | +JVY6qs5gWlB+XlJJMAxzVuDJRAxbWuHmwhFbTG4JQIhwGxqiJtgYT9H/lM92gyGp |
| 57 | +gCZvJeUkAQPx6jMh5jlcppZ76Oev5eVGQVhQ4luS373WyVD5ZLvNYxLZRXORlPuF |
| 58 | +rtyBH58jAgMBAAEwDQYJKoZIhvcNAQELBQADfwAKAAAEAQAAAAAACAAABQAAAAAA |
| 59 | +AAAAAAAAAgAAAAAAAAgABQAHAAAAAAAAANcAAAAAAAAAAAsAfgwAAAAAAAALAAAN |
| 60 | +DQAADwAAAAANAAAAAAAACwALAAAAAPYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
| 61 | +AAAAAAAAAAAAAAg= |
| 62 | +-----END CERTIFICATE REQUEST-----'; |
| 63 | + |
| 64 | + public function testSignatureSubject() |
| 65 | + { |
| 66 | + $private_key = openssl_pkey_get_private($this->pem); |
| 67 | + $pem = openssl_pkey_get_details($private_key)['key']; |
| 68 | + $pemparts = explode("\n", trim($pem)); |
| 69 | + array_pop($pemparts); |
| 70 | + array_shift($pemparts); |
| 71 | + $binarykey = base64_decode(implode('', $pemparts)); |
| 72 | + $asn1KeyWrapper = ASNObject::fromBinary($binarykey); |
| 73 | + |
| 74 | + // Now get the key out in hex format |
| 75 | + $asn1Key = $asn1KeyWrapper->getContent(); |
| 76 | + $hexkey = $asn1Key[1]->getContent(); |
| 77 | + |
| 78 | + $csr = new CSR( 'github.org', '[email protected]', 'Github', 'San Fransisco', '', 'US', 'IT', $hexkey); |
| 79 | + $digest = $csr->getSignatureSubject(); |
| 80 | + $this->assertEquals(bin2hex($digest), '308201b3020100308187310b30090603550406130255533109300706035504081300311630140603550407130d53616e204672616e736973636f310f300d060355040a1306476974687562310b3009060355040b13024954311330110603550403130a6769746875622e6f72673122302006092a864886f70d01090116136e6f2d7265706c79406769746875622e6f726730820122300d06092a864886f70d01010105000382010f003082010a0282010100d3a186d20cb5da76f2a79ca2b8d7ec80102de2e456994824035910b82743a0584b4e332c44eade40918a55bc8f3226351f030bfd7682d0d459b6a32cb2bc262af87b2866fa4049b3edd095764fde53b205e3cd3ff77059986607dac76a1b8d294f06e56a0c3c0f6d42c7ec13c071e40265f847373ce82c3e18f480821d240fd4f2804b49c5c03fc5dfcd5078f9165c03725c46647ec1f3d83f3925563aaace605a507e5e5249300c7356e0c9440c5b5ae1e6c2115b4c6e094088701b1aa226d8184fd1ff94cf768321a980266f25e5240103f1ea3321e6395ca6967be8e7afe5e546415850e25b92dfbdd6c950f964bbcd6312d945739194fb85aedc811f9f230203010001'); |
| 81 | + } |
| 82 | + |
| 83 | + public function testAddSignature() |
| 84 | + { |
| 85 | + $private_key = openssl_pkey_get_private($this->pem); |
| 86 | + $pem = openssl_pkey_get_details($private_key)['key']; |
| 87 | + $pemparts = explode("\n", trim($pem)); |
| 88 | + array_pop($pemparts); |
| 89 | + array_shift($pemparts); |
| 90 | + $binarykey = base64_decode(implode('', $pemparts)); |
| 91 | + $asn1KeyWrapper = ASNObject::fromBinary($binarykey); |
| 92 | + |
| 93 | + // Now get the key out in hex format |
| 94 | + $asn1Key = $asn1KeyWrapper->getContent(); |
| 95 | + $hexkey = $asn1Key[1]->getContent(); |
| 96 | + |
| 97 | + $csr = new CSR( 'github.org', '[email protected]', 'Github', 'San Fransisco', '', 'US', 'IT', $hexkey); |
| 98 | + $digest = $csr->getSignatureSubject(); |
| 99 | + $signature = null; |
| 100 | + openssl_sign($digest, $signature, $private_key, OPENSSL_ALGO_SHA256); |
| 101 | + $csr->setSignature($signature, OID::SHA256_WITH_RSA_SIGNATURE); |
| 102 | + |
| 103 | + $cert = str_replace("\r\n", "\n", $csr->__toString()); |
| 104 | + $expected = str_replace("\r\n", "\n", $this->cert); |
| 105 | + $this->assertEquals($expected, $cert); |
| 106 | + } |
| 107 | + |
| 108 | + public function testAddSignatureTwice() |
| 109 | + { |
| 110 | + $private_key = openssl_pkey_get_private($this->pem); |
| 111 | + $pem = openssl_pkey_get_details($private_key)['key']; |
| 112 | + $pemparts = explode("\n", trim($pem)); |
| 113 | + array_pop($pemparts); |
| 114 | + array_shift($pemparts); |
| 115 | + $binarykey = base64_decode(implode('', $pemparts)); |
| 116 | + $asn1KeyWrapper = ASNObject::fromBinary($binarykey); |
| 117 | + |
| 118 | + // Now get the key out in hex format |
| 119 | + $asn1Key = $asn1KeyWrapper->getContent(); |
| 120 | + $hexkey = $asn1Key[1]->getContent(); |
| 121 | + |
| 122 | + $csr = new CSR( 'github.org', '[email protected]', 'Github', 'San Fransisco', '', 'US', 'IT', $hexkey); |
| 123 | + $digest = $csr->getSignatureSubject(); |
| 124 | + $signature = null; |
| 125 | + openssl_sign($digest, $signature, $private_key, OPENSSL_ALGO_SHA256); |
| 126 | + $csr->setSignature($signature, OID::SHA256_WITH_RSA_SIGNATURE); |
| 127 | + $csr->setSignature($signature, OID::SHA256_WITH_RSA_SIGNATURE); |
| 128 | + |
| 129 | + $cert = str_replace("\r\n", "\n", $csr->__toString()); |
| 130 | + $expected = str_replace("\r\n", "\n", $this->cert); |
| 131 | + $this->assertEquals($expected, $cert); |
| 132 | + } |
| 133 | + |
| 134 | + public function testCertificate() |
| 135 | + { |
| 136 | + $private_key = openssl_pkey_get_private($this->pem); |
| 137 | + $pem = openssl_pkey_get_details($private_key)['key']; |
| 138 | + $pemparts = explode("\n", trim($pem)); |
| 139 | + array_pop($pemparts); |
| 140 | + array_shift($pemparts); |
| 141 | + $binarykey = base64_decode(implode('', $pemparts)); |
| 142 | + $asn1KeyWrapper = ASNObject::fromBinary($binarykey); |
| 143 | + |
| 144 | + // Now get the key out in hex format |
| 145 | + $asn1Key = $asn1KeyWrapper->getContent(); |
| 146 | + $hexkey = $asn1Key[1]->getContent(); |
| 147 | + |
| 148 | + $csr = new CSR( 'github.org', '[email protected]', 'Github', 'San Fransisco', '', 'US', 'IT', $hexkey); |
| 149 | + $digest = $csr->getSignatureSubject(); |
| 150 | + $signature = null; |
| 151 | + openssl_sign($digest, $signature, $private_key, OPENSSL_ALGO_SHA256); |
| 152 | + $csr->setSignature($signature, OID::SHA256_WITH_RSA_SIGNATURE); |
| 153 | + |
| 154 | + $cert = $csr->__toString(); |
| 155 | + $key = openssl_csr_get_public_key($cert); |
| 156 | + $certPem = openssl_pkey_get_details($key)['key']; |
| 157 | + |
| 158 | + $this->assertEquals($certPem, $pem); |
| 159 | + } |
| 160 | + |
| 161 | +} |
0 commit comments