Passed
Pull Request — master (#3)
by Tim
02:04
created

PEMCertificatesMock::getPlainPublicKeyContents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\TestUtils;
6
7
use Exception;
8
use SimpleSAML\XMLSecurity\XMLSecurityKey;
9
use SimpleSAML\XMLSecurity\Utils\Certificate as CertificateUtils;
10
11
use function dirname;
12
use function file_get_contents;
13
use function preg_match;
14
use function preg_replace;
15
16
/**
17
 * Class \SimpleSAML\TestUtils\PEMCertificatesMock
18
 */
19
class PEMCertificatesMock
20
{
21
    public const ALG_SIG_RSA = 'rsa';
22
    public const ALG_SIG_DSA = 'dsa';
23
24
    public const CERTIFICATE_DIR_RSA = '/tests/resources/certificates/rsa-pem';
25
    public const CERTIFICATE_DIR_DSA = '/tests/resources/certificates/dsa-pem';
26
27
    public const PUBLIC_KEY = 'signed.simplesamlphp.org.crt';
28
    public const PRIVATE_KEY = 'signed.simplesamlphp.org_nopasswd.key';
29
    public const PRIVATE_KEY_PROTECTED = 'signed.simplesamlphp.org.key';
30
    public const OTHER_PUBLIC_KEY = 'other.simplesamlphp.org.crt';
31
    public const OTHER_PRIVATE_KEY = 'other.simplesamlphp.org_nopasswd.key';
32
    public const OTHER_PRIVATE_KEY_PROTECTED = 'other.simplesamlphp.org.key';
33
    public const SELFSIGNED_PUBLIC_KEY = 'selfsigned.simplesamlphp.org.crt';
34
    public const SELFSIGNED_PRIVATE_KEY = 'selfsigned.simplesamlphp.org_nopasswd.key';
35
    public const SELFSIGNED_PRIVATE_KEY_PROTECTED = 'selfsigned.simplesamlphp.org.key';
36
    public const BROKEN_PUBLIC_KEY = 'broken.simplesamlphp.org.crt';
37
    public const BROKEN_PRIVATE_KEY = 'broken.simplesamlphp.org.key';
38
    public const CORRUPTED_PUBLIC_KEY = 'corrupted.simplesamlphp.org.crt';
39
    public const CORRUPTED_PRIVATE_KEY = 'corrupted.simplesamlphp.org.key';
40
41
42
    /**
43
     * @param string $file The file we should load
44
     * @param string $sig_alg  One of rsa|dsa
45
     * @return string The file contents
46
     */
47
    public static function loadPlainCertificateFile(string $file, $sig_alg = self::ALG_SIG_RSA)
48
    {
49
        $base = dirname(dirname(dirname(__FILE__)));
50
        if ($sig_alg === self::ALG_SIG_RSA) {
51
            return file_get_contents($base . self::CERTIFICATE_DIR_RSA . DIRECTORY_SEPARATOR . $file);
52
        } else {
53
            return file_get_contents($base . self::CERTIFICATE_DIR_DSA . DIRECTORY_SEPARATOR . $file);
54
        }
55
    }
56
57
58
    /**
59
     * @param string $hash_alg
60
     * @param string $file The file to use
61
     * @param string $sig_alg  One of rsa|dsa
62
     * @return \SimpleSAML\XMLSecurity\XMLSecurityKey
63
     */
64
    public static function getPublicKey(
65
        string $hash_alg,
66
        string $file,
67
        string $sig_alg = self::ALG_SIG_RSA
68
    ): XMLSecurityKey {
69
        $publicKey = new XMLSecurityKey($hash_alg, ['type' => 'public']);
70
        $publicKey->loadKey(self::getPlainPublicKey($file, $sig_alg));
71
        return $publicKey;
72
    }
73
74
75
    /**
76
     * @param string $hash_alg
77
     * @param string $file The file to use
78
     * @param string $sig_alg  One of rsa|dsa
79
     * @return \SimpleSAML\XMLSecurity\XMLSecurityKey
80
     */
81
    public static function getPrivateKey(
82
        string $hash_alg,
83
        string $file,
84
        string $sig_alg = self::ALG_SIG_RSA
85
    ): XMLSecurityKey {
86
        $privateKey = new XMLSecurityKey($hash_alg, ['type' => 'private']);
87
        $privateKey->loadKey(self::getPlainPrivateKey($file, $sig_alg));
88
        return $privateKey;
89
    }
90
91
92
    /**
93
     * @param string $file The file to use
94
     * @param string $sig_alg  One of rsa|dsa
95
     * @return string
96
     */
97
    public static function getPlainPublicKey(
98
        string $file = self::PUBLIC_KEY,
99
        string $sig_alg = self::ALG_SIG_RSA
100
    ): string {
101
        return self::loadPlainCertificateFile($file, $sig_alg);
102
    }
103
104
105
    /**
106
     * @param string $file The file to use
107
     * @param string $sig_alg  One of rsa|dsa
108
     * @return string
109
     */
110
    public static function getPlainPrivateKey(
111
        string $file = self::PRIVATE_KEY,
112
        string $sig_alg = self::ALG_SIG_RSA
113
    ): string {
114
        return self::loadPlainCertificateFile($file, $sig_alg);
115
    }
116
117
118
    /**
119
     * @param string $file The file to use
120
     * @param string $sig_alg  One of rsa|dsa
121
     * @return string
122
     */
123
    public static function getPlainPublicKeyContents(
124
        string $file = self::PUBLIC_KEY,
125
        string $sig_alg = self::ALG_SIG_RSA
126
    ): string {
127
        return CertificateUtils::stripHeaders(self::loadPlainCertificateFile($file, $sig_alg), CertificateUtils::PUBLIC_KEY_PATTERN);
128
    }
129
130
131
    /**
132
     * @param string $file The file to use
133
     * @param string $sig_alg  One of rsa|dsa
134
     * @return string
135
     */
136
    public static function getPlainPrivateKeyContents(
137
        string $file = self::PRIVATE_KEY,
138
        string $sig_alg = self::ALG_SIG_RSA
139
    ): string {
140
        return CertificateUtils::stripHeaders(self::loadPlainCertificateFile($file, $sig_alg), CertificateUtils::PRIVATE_KEY_PATTERN);
141
    }
142
}
143