Certificate::convertToCertificate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\Utils;
6
7
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
8
9
use function array_unshift;
10
use function chunk_split;
11
use function implode;
12
use function is_array;
13
use function preg_match;
14
15
/**
16
 * Collection of Utility functions specifically for certificates
17
 */
18
class Certificate
19
{
20
    /**
21
     * The pattern that the contents of a certificate should adhere to
22
     */
23
    public const CERTIFICATE_PATTERN = '/^-----BEGIN CERTIFICATE-----([^-]*)^-----END CERTIFICATE-----/m';
24
    public const PUBLIC_KEY_PATTERN = '/^-----BEGIN PUBLIC KEY-----([^-]*)^-----END PUBLIC KEY-----/m';
25
    public const PRIVATE_KEY_PATTERN = '/^-----BEGIN RSA PRIVATE KEY-----([^-]*)^-----END RSA PRIVATE KEY-----/m';
26
27
28
    /**
29
     * @param string $certificate
30
     * @param string $pattern
31
     *
32
     * @return bool
33
     */
34
    public static function hasValidStructure(string $certificate, string $pattern = self::PUBLIC_KEY_PATTERN): bool
35
    {
36
        return !!preg_match($pattern, $certificate);
37
    }
38
39
40
    /**
41
     * @param string $X509CertificateContents
42
     *
43
     * @return string
44
     */
45
    public static function convertToCertificate(string $X509CertificateContents): string
46
    {
47
        return "-----BEGIN CERTIFICATE-----\n"
48
                . chunk_split($X509CertificateContents, 64, "\n")
49
                . "-----END CERTIFICATE-----";
50
    }
51
52
53
    /**
54
     * @param array<string, mixed>|string $issuer
55
     *
56
     * @return string
57
     */
58
    public static function parseIssuer(array|string $issuer): string
59
    {
60
        if (is_array($issuer)) {
0 ignored issues
show
introduced by
The condition is_array($issuer) is always true.
Loading history...
61
            $parts = [];
62
            foreach ($issuer as $key => $value) {
63
                array_unshift($parts, $key . '=' . $value);
64
            }
65
            return implode(',', $parts);
66
        }
67
68
        return $issuer;
69
    }
70
71
72
    /**
73
     * @param string $key The PEM-encoded key
74
     * @param string $pattern The pattern to use
75
     * @return string The stripped key
76
     */
77
    public static function stripHeaders(string $key, string $pattern = self::PUBLIC_KEY_PATTERN): string
78
    {
79
        $matches = [];
80
        $result = preg_match($pattern, $key, $matches);
81
        if ($result === false) {
82
            throw new InvalidArgumentException('Could not find content matching the provided pattern.');
83
        }
84
85
        return preg_replace('/\s+/', '', $matches[1]);
86
    }
87
}
88