Passed
Pull Request — master (#61)
by
unknown
12:50
created

RSAKeyTransportTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 35
c 4
b 0
f 0
dl 0
loc 79
rs 10
wmc 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\Test\Alg\KeyTransport;
6
7
use PHPUnit\Framework\TestCase;
8
use SimpleSAML\XMLSecurity\Alg\KeyTransport\KeyTransportAlgorithmFactory;
9
use SimpleSAML\XMLSecurity\Constants as C;
10
use SimpleSAML\XMLSecurity\Key\PrivateKey;
11
use SimpleSAML\XMLSecurity\Key\PublicKey;
12
use SimpleSAML\XMLSecurity\TestUtils\PEMCertificatesMock;
13
14
/**
15
 * Tests for \SimpleSAML\XMLSecurity\Alg\KeyTransport\RSA.
16
 *
17
 * @package simplesamlphp/xml-security
18
 */
19
class RSAKeyTransportTest extends TestCase
20
{
21
    /** @var string */
22
    public const PLAINTEXT = 'plaintext';
23
24
    /** @var \SimpleSAML\XMLSecurity\Key\PrivateKey */
25
    protected static PrivateKey $privateKey;
26
27
    /** @var \SimpleSAML\XMLSecurity\Key\PublicKey */
28
    protected static PublicKey $publicKey;
29
30
    /** @var \SimpleSAML\XMLSecurity\Alg\KeyTransport\KeyTransportAlgorithmFactory */
31
    protected static KeyTransportAlgorithmFactory $factory;
32
33
34
    /**
35
     *
36
     */
37
    public static function setUpBeforeClass(): void
38
    {
39
        self::$publicKey = PEMCertificatesMock::getPublicKey(PEMCertificatesMock::PUBLIC_KEY);
40
        self::$privateKey = PEMCertificatesMock::getPrivateKey(PEMCertificatesMock::PRIVATE_KEY);
41
        self::$factory = new KeyTransportAlgorithmFactory([]);
42
    }
43
44
45
    /**
46
     * Test encrypting with RSA.
47
     */
48
    public function testEncrypt(): void
49
    {
50
        // test RSA 1.5
51
        $rsa = self::$factory->getAlgorithm(C::KEY_TRANSPORT_RSA_1_5, self::$publicKey);
52
        $encrypted = $rsa->encrypt(self::PLAINTEXT);
53
        $this->assertNotEmpty($encrypted);
54
        $this->assertEquals(128, strlen($encrypted));
55
56
        // test RSA-OAEP
57
        $rsa = self::$factory->getAlgorithm(C::KEY_TRANSPORT_OAEP, self::$publicKey);
58
        $encrypted = $rsa->encrypt(self::PLAINTEXT);
59
        $this->assertNotEmpty($encrypted);
60
        $this->assertEquals(128, strlen($encrypted));
61
62
        // test RSA-OAEP-MGF1P
63
        $rsa = self::$factory->getAlgorithm(C::KEY_TRANSPORT_OAEP_MGF1P, self::$publicKey);
64
        $encrypted = $rsa->encrypt(self::PLAINTEXT);
65
        $this->assertNotEmpty($encrypted);
66
        $this->assertEquals(128, strlen($encrypted));
67
    }
68
69
70
    /**
71
     * Test decrypting with RSA.
72
     *
73
     * NOTE: if you change the key material, you have to replace $ciphertext with a
74
     *       base64 encoded version of the $encrypted var from ::testEncrypt
75
     */
76
    public function testDecrypt(): void
77
    {
78
        // test RSA-OAEP-MGF1P
79
        $ciphertext = "0Ok/N3BV5LUxmr8IDXQQhtzQEJzD5uSN5kOVjzPkzesjlSVR9qv819MPBL8yfSMdUSQWVq1N/w" .
80
                      "A6fgclGb/keGZOtjSkHZnZEZvXEOQItFjS6MbQc+TzNmRd6FSkuPUmwQ1V+NwxTPCIwXSSd0Aj" .
81
                      "7oHb7xRdBhoFuDrSbYAvATQ=";
82
        $rsa = self::$factory->getAlgorithm(C::KEY_TRANSPORT_OAEP_MGF1P, self::$privateKey);
83
        $plaintext = $rsa->decrypt(base64_decode($ciphertext, true));
84
        $this->assertEquals(self::PLAINTEXT, $plaintext);
85
86
        // test RSA-OAEP (should behave the same as MGF1P)
87
        $rsa = self::$factory->getAlgorithm(C::KEY_TRANSPORT_OAEP, self::$privateKey);
88
        $plaintext = $rsa->decrypt(base64_decode($ciphertext, true));
89
        $this->assertEquals(self::PLAINTEXT, $plaintext);
90
91
        // test RSA-1.5
92
        $ciphertext = "ZAnYBqqM5T/kg+P8fb3UfDU1gyUIpndpqQN2qpmJso2z6His6WOkh5JFVN/wz+agvyR54kMmII" .
93
                      "afiDsy5izSk6+QZ5kMOgRLrmnh+RYZXjvCL6i1NXzaLw8yZLBvlP01SNMv/BBq640yzbG9U2ZN" .
94
                      "nxBLDvBmbJBxzt6XCowXQS8=";
95
        $rsa = self::$factory->getAlgorithm(C::KEY_TRANSPORT_RSA_1_5, self::$privateKey);
96
        $plaintext = $rsa->decrypt(base64_decode($ciphertext, true));
97
        $this->assertEquals(self::PLAINTEXT, $plaintext);
98
    }
99
}
100