Passed
Pull Request — master (#3)
by Jaime Pérez
02:39
created

PEMCertificatesMock::stripHeaders()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 6
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 11
rs 9.6111
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
/**
12
 * Class \SimpleSAML\TestUtils\PEMCertificatesMock
13
 */
14
class PEMCertificatesMock
15
{
16
    public const ALG_SIG_RSA = 'rsa';
17
    public const ALG_SIG_DSA = 'dsa';
18
19
    public const CERTIFICATE_DIR_RSA = '/tests/resources/certificates/rsa-pem';
20
    public const CERTIFICATE_DIR_DSA = '/tests/resources/certificates/dsa-pem';
21
22
    public const PUBLIC_KEY = 'signed.simplesamlphp.org.crt';
23
    public const PRIVATE_KEY = 'signed.simplesamlphp.org_nopasswd.key';
24
    public const PRIVATE_KEY_PROTECTED = 'signed.simplesamlphp.org.key';
25
    public const OTHER_PUBLIC_KEY = 'other.simplesamlphp.org.crt';
26
    public const OTHER_PRIVATE_KEY = 'other.simplesamlphp.org_nopasswd.key';
27
    public const OTHER_PRIVATE_KEY_PROTECTED = 'other.simplesamlphp.org.key';
28
    public const SELFSIGNED_PUBLIC_KEY = 'selfsigned.simplesamlphp.org.crt';
29
    public const SELFSIGNED_PRIVATE_KEY = 'selfsigned.simplesamlphp.org_nopasswd.key';
30
    public const SELFSIGNED_PRIVATE_KEY_PROTECTED = 'selfsigned.simplesamlphp.org.key';
31
    public const BROKEN_PUBLIC_KEY = 'broken.simplesamlphp.org.crt';
32
    public const BROKEN_PRIVATE_KEY = 'broken.simplesamlphp.org.key';
33
    public const CORRUPTED_PUBLIC_KEY = 'corrupted.simplesamlphp.org.crt';
34
    public const CORRUPTED_PRIVATE_KEY = 'corrupted.simplesamlphp.org.key';
35
36
37
    /**
38
     * @param string $file The file we should load
39
     * @param string $sig_alg  One of rsa|dsa
40
     * @return string The file contents
41
     */
42
    public static function loadPlainCertificateFile(string $file, $sig_alg = self::ALG_SIG_RSA)
43
    {
44
        $base = dirname(dirname(dirname(__FILE__)));
45
        if ($sig_alg === self::ALG_SIG_RSA) {
46
            return file_get_contents($base . self::CERTIFICATE_DIR_RSA . DIRECTORY_SEPARATOR . $file);
47
        } else {
48
            return file_get_contents($base . self::CERTIFICATE_DIR_DSA . DIRECTORY_SEPARATOR . $file);
49
        }
50
    }
51
52
53
    /**
54
     * @param string $hash_alg
55
     * @param string The file to use
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSecurity\TestUtils\The was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
56
     * @param string $sig_alg  One of rsa|dsa
57
     * @return \SimpleSAML\XMLSecurity\XMLSecurityKey
58
     */
59
    public static function getPublicKey(
60
        string $hash_alg,
61
        string $file,
62
        string $sig_alg = self::ALG_SIG_RSA
63
    ): XMLSecurityKey {
64
        $publicKey = new XMLSecurityKey($hash_alg, ['type' => 'public']);
65
        $publicKey->loadKey(self::getPlainPublicKey($file, $sig_alg));
66
        return $publicKey;
67
    }
68
69
70
    /**
71
     * @param string $hash_alg
72
     * @param string The file to use
73
     * @param string $sig_alg  One of rsa|dsa
74
     * @return \SimpleSAML\XMLSecurity\XMLSecurityKey
75
     */
76
    public static function getPrivateKey(
77
        string $hash_alg,
78
        string $file,
79
        string $sig_alg = self::ALG_SIG_RSA
80
    ): XMLSecurityKey {
81
        $privateKey = new XMLSecurityKey($hash_alg, ['type' => 'private']);
82
        $privateKey->loadKey(self::getPlainPrivateKey($file, $sig_alg));
83
        return $privateKey;
84
    }
85
86
87
    /**
88
     * @param string $file The file to use
89
     * @param string $sig_alg  One of rsa|dsa
90
     * @return string
91
     */
92
    public static function getPlainPublicKey(
93
        string $file = self::PUBLIC_KEY,
94
        string $sig_alg = self::ALG_SIG_RSA
95
    ): string {
96
        return self::loadPlainCertificateFile($file, $sig_alg);
97
    }
98
99
100
    /**
101
     * @param string $file The file to use
102
     * @param string $sig_alg  One of rsa|dsa
103
     * @return string
104
     */
105
    public static function getPlainPrivateKey(
106
        string $file = self::PRIVATE_KEY,
107
        string $sig_alg = self::ALG_SIG_RSA
108
    ): string {
109
        return self::loadPlainCertificateFile($file, $sig_alg);
110
    }
111
112
113
    /**
114
     * @param string $file The file to use
115
     * @param string $sig_alg  One of rsa|dsa
116
     * @return string
117
     */
118
    public static function getPlainPublicKeyContents(
119
        string $file = self::PUBLIC_KEY,
120
        string $sig_alg = self::ALG_SIG_RSA
121
    ): string {
122
        return CertificateUtils::stripHeaders(self::loadPlainCertificateFile($file, $sig_alg), CertificateUtils::PUBLIC_KEY_PATTERN);
123
    }
124
125
126
    /**
127
     * @param string $file The file to use
128
     * @param string $sig_alg  One of rsa|dsa
129
     * @return string
130
     */
131
    public static function getPlainPrivateKeyContents(
132
        string $file = self::PRIVATE_KEY,
133
        string $sig_alg = self::ALG_SIG_RSA
134
    ): string {
135
        return CertificateUtils::stripHeaders(self::loadPlainCertificateFile($file, $sig_alg), CertificateUtils::PRIVATE_KEY_PATTERN);
136
    }
137
}
138