Debian Codegraph @

lib/3rdParty/composer/spomky-labs/pki-framework/src/CryptoEncoding/PEM.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace SpomkyLabs\Pki\CryptoEncoding;
6
7 use function is_string;
8 use RuntimeException;
9 use Stringable;
10 use UnexpectedValueException;
11
12 /**
13 * Implements PEM file encoding and decoding.
14 *
15 * @see https://tools.ietf.org/html/rfc7468
16 */
17 final class PEM implements Stringable
18 {
19 // well-known PEM types
20 final public const TYPE_CERTIFICATE = 'CERTIFICATE';
21
22 final public const TYPE_CRL = 'X509 CRL';
23
24 final public const TYPE_CERTIFICATE_REQUEST = 'CERTIFICATE REQUEST';
25
26 final public const TYPE_ATTRIBUTE_CERTIFICATE = 'ATTRIBUTE CERTIFICATE';
27
28 final public const TYPE_PRIVATE_KEY = 'PRIVATE KEY';
29
30 final public const TYPE_PUBLIC_KEY = 'PUBLIC KEY';
31
32 final public const TYPE_ENCRYPTED_PRIVATE_KEY = 'ENCRYPTED PRIVATE KEY';
33
34 final public const TYPE_RSA_PRIVATE_KEY = 'RSA PRIVATE KEY';
35
36 final public const TYPE_RSA_PUBLIC_KEY = 'RSA PUBLIC KEY';
37
38 final public const TYPE_EC_PRIVATE_KEY = 'EC PRIVATE KEY';
39
40 final public const TYPE_PKCS7 = 'PKCS7';
41
42 final public const TYPE_CMS = 'CMS';
43
44 /**
45 * Regular expression to match PEM block.
46 *
47 * @var string
48 */
49 final public const PEM_REGEX = '/' .
50 /* line start */
51 '(?:^|[\r\n])' .
52 /* header */
53 '-----BEGIN (.+?)-----[\r\n]+' .
54 /* payload */
55 '(.+?)' .
56 /* trailer */
57 '[\r\n]+-----END \\1-----' .
58 '/ms';
59
60 /**
61 * @param string $type Content type
62 * @param string $data Payload
63 */
64 private function __construct(
65 private readonly string $type,
66 private readonly string $data
67 ) {
68 }
69
70 public function __toString(): string
71 {
72 return $this->string();
73 }
74
75 public static function create(string $_type, string $_data): self
76 {
77 return new self($_type, $_data);
78 }
79
80 /**
81 * Initialize from a PEM-formatted string.
82 */
83 public static function fromString(string $str): self
84 {
85 if (preg_match(self::PEM_REGEX, $str, $match) !== 1) {
86 throw new UnexpectedValueException('Not a PEM formatted string.');
87 }
88 $payload = preg_replace('/\s+/', '', $match[2]);
89 if (! is_string($payload)) {
90 throw new UnexpectedValueException('Failed to decode PEM data.');
91 }
92 $data = base64_decode($payload, true);
93 if ($data === false) {
94 throw new UnexpectedValueException('Failed to decode PEM data.');
95 }
96 return self::create($match[1], $data);
97 }
98
99 /**
100 * Initialize from a file.
101 *
102 * @param string $filename Path to file
103 */
104 public static function fromFile(string $filename): self
105 {
106 if (! is_readable($filename)) {
107 throw new RuntimeException("Failed to read {$filename}.");
108 }
109 $str = file_get_contents($filename);
110 if ($str === false) {
111 throw new RuntimeException("Failed to read {$filename}.");
112 }
113 return self::fromString($str);
114 }
115
116 /**
117 * Get content type.
118 */
119 public function type(): string
120 {
121 return $this->type;
122 }
123
124 public function data(): string
125 {
126 return $this->data;
127 }
128
129 /**
130 * Encode to PEM string.
131 */
132 public function string(): string
133 {
134 return "-----BEGIN {$this->type}-----\n" .
135 trim(chunk_split(base64_encode($this->data), 64, "\n")) . "\n" .
136 "-----END {$this->type}-----";
137 }
138 }