Passed
Branch master (ff35cf)
by Tim
02:03
created

PEMCertificatesMock::buildCertsPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\TestUtils;
6
7
use SimpleSAML\XMLSecurity\Key\PrivateKey;
8
use SimpleSAML\XMLSecurity\Key\PublicKey;
9
use SimpleSAML\XMLSecurity\Key\X509Certificate;
10
use SimpleSAML\XMLSecurity\Utils\Certificate as CertificateUtils;
11
12
use function dirname;
13
use function file_get_contents;
14
use function trim;
15
16
/**
17
 * Class \SimpleSAML\TestUtils\PEMCertificatesMock
18
 */
19
class PEMCertificatesMock
20
{
21
    public const CERTS_DIR = 'resources/certificates';
22
    public const KEYS_DIR = 'resources/keys';
23
    public const PASSPHRASE = '1234';
24
25
    public const CERTIFICATE = 'signed.simplesamlphp.org.crt';
26
    public const PUBLIC_KEY = 'signed.simplesamlphp.org.pub';
27
    public const PRIVATE_KEY = 'signed.simplesamlphp.org.key';
28
    public const OTHER_CERTIFICATE = 'other.simplesamlphp.org.crt';
29
    public const OTHER_PUBLIC_KEY = 'other.simplesamlphp.org.pub';
30
    public const OTHER_PRIVATE_KEY = 'other.simplesamlphp.org.key';
31
    public const SELFSIGNED_CERTIFICATE = 'selfsigned.simplesamlphp.org.crt';
32
    public const SELFSIGNED_PUBLIC_KEY = 'selfsigned.simplesamlphp.org.pub';
33
    public const SELFSIGNED_PRIVATE_KEY = 'selfsigned.simplesamlphp.org.key';
34
    public const BROKEN_CERTIFICATE = 'broken.simplesamlphp.org.crt';
35
    public const BROKEN_PUBLIC_KEY = 'broken.simplesamlphp.org.pub';
36
    public const BROKEN_PRIVATE_KEY = 'broken.simplesamlphp.org.key';
37
    public const CORRUPTED_CERTIFICATE = 'corrupted.simplesamlphp.org.crt';
38
    public const CORRUPTED_PUBLIC_KEY = 'corrupted.simplesamlphp.org.pub';
39
    public const CORRUPTED_PRIVATE_KEY = 'corrupted.simplesamlphp.org.key';
40
41
42
    /**
43
     * @param string $file The file to use
44
     * @return string
45
     */
46
    public static function buildKeysPath(string $file): string
47
    {
48
        $base = dirname(__FILE__, 3);
49
        return 'file://' . $base . DIRECTORY_SEPARATOR . self::KEYS_DIR . DIRECTORY_SEPARATOR . $file;
50
    }
51
52
53
    /**
54
     * @param string $file The file to use
55
     * @return string
56
     */
57
    public static function buildCertsPath(string $file): string
58
    {
59
        $base = dirname(__FILE__, 3);
60
        return 'file://' . $base . DIRECTORY_SEPARATOR . self::CERTS_DIR . DIRECTORY_SEPARATOR . $file;
61
    }
62
63
64
    /**
65
     * @param string $file The file we should load
66
     * @return string The file contents
67
     */
68
    public static function loadPlainCertificateFile(string $file): string
69
    {
70
        return trim(file_get_contents(self::buildCertsPath($file)));
71
    }
72
73
74
    /**
75
     * @param string $file The file we should load
76
     * @return string The file contents
77
     */
78
    public static function loadPlainKeyFile(string $file): string
79
    {
80
        return trim(file_get_contents(self::buildKeysPath($file)));
81
    }
82
83
84
    /**
85
     * @param string $file The file to use
86
     * @return \SimpleSAML\XMLSecurity\Key\X509Certificate
87
     */
88
    public static function getCertificate(string $file): X509Certificate
89
    {
90
        $path = self::buildCertsPath($file);
91
        return X509Certificate::fromFile($path);
92
    }
93
94
95
    /**
96
     * @param string $file The file to use
97
     * @return \SimpleSAML\XMLSecurity\Key\PublicKey
98
     */
99
    public static function getPublicKey(string $file): PublicKey
100
    {
101
        $path = self::buildKeysPath($file);
102
        return PublicKey::fromFile($path);
103
    }
104
105
106
    /**
107
     * @param string $file The file to use
108
     * @param string $passphrase The passphrase to use
109
     * @return \SimpleSAML\XMLSecurity\Key\PrivateKey
110
     */
111
    public static function getPrivateKey(string $file, string $passphrase = self::PASSPHRASE): PrivateKey
112
    {
113
        $path = self::buildKeysPath($file);
114
        return PrivateKey::fromFile($path, $passphrase);
115
    }
116
117
118
    /**
119
     * @param string $file The file to use
120
     * @return string
121
     */
122
    public static function getPlainCertificate(
123
        string $file = self::CERTIFICATE
124
    ): string {
125
        return self::loadPlainCertificateFile($file);
126
    }
127
128
129
    /**
130
     * @param string $file The file to use
131
     * @return string
132
     */
133
    public static function getPlainPublicKey(
134
        string $file = self::PUBLIC_KEY
135
    ): string {
136
        return self::loadPlainKeyFile($file);
137
    }
138
139
140
    /**
141
     * @param string $file The file to use
142
     * @return string
143
     */
144
    public static function getPlainPrivateKey(
145
        string $file = self::PRIVATE_KEY
146
    ): string {
147
        return self::loadPlainKeyFile($file);
148
    }
149
150
151
    /**
152
     * @param string $file The file to use
153
     * @return string
154
     */
155
    public static function getPlainCertificateContents(
156
        string $file = self::CERTIFICATE
157
    ): string {
158
        return CertificateUtils::stripHeaders(
159
            self::loadPlainCertificateFile($file),
160
            CertificateUtils::CERTIFICATE_PATTERN
161
        );
162
    }
163
164
165
    /**
166
     * @param string $file The file to use
167
     * @return string
168
     */
169
    public static function getPlainPublicKeyContents(
170
        string $file = self::PUBLIC_KEY
171
    ): string {
172
        return CertificateUtils::stripHeaders(
173
            self::loadPlainKeyFile($file),
174
            CertificateUtils::PUBLIC_KEY_PATTERN
175
        );
176
    }
177
178
179
    /**
180
     * @param string $file The file to use
181
     * @return string
182
     */
183
    public static function getPlainPrivateKeyContents(
184
        string $file = self::PRIVATE_KEY
185
    ): string {
186
        return CertificateUtils::stripHeaders(
187
            self::loadPlainCertificateFile($file),
188
            CertificateUtils::PRIVATE_KEY_PATTERN
189
        );
190
    }
191
}
192