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