1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Sop\CryptoEncoding; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Implements PEM file encoding and decoding. |
9
|
|
|
* |
10
|
|
|
* @link 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 = /* @formatter:off */ '/' . |
34
|
|
|
/* line start */ '(?:^|[\r\n])' . |
35
|
|
|
/* header */ '-----BEGIN (.+?)-----[\r\n]+' . |
36
|
|
|
/* payload */ '(.+?)' . |
37
|
|
|
/* trailer */ '[\r\n]+-----END \\1-----' . |
38
|
|
|
'/ms'; /* @formatter:on */ |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Content type. |
42
|
|
|
* |
43
|
|
|
* @var string $_type |
44
|
|
|
*/ |
45
|
|
|
protected $_type; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Payload. |
49
|
|
|
* |
50
|
|
|
* @var string $_data |
51
|
|
|
*/ |
52
|
|
|
protected $_data; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Constructor. |
56
|
|
|
* |
57
|
|
|
* @param string $type Content type |
58
|
|
|
* @param string $data Payload |
59
|
|
|
*/ |
60
|
|
|
public function __construct(string $type, string $data) |
61
|
|
|
{ |
62
|
|
|
$this->_type = $type; |
63
|
|
|
$this->_data = $data; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Initialize from a PEM-formatted string. |
68
|
|
|
* |
69
|
|
|
* @param string $str |
70
|
|
|
* @throws \UnexpectedValueException If string is not valid PEM |
71
|
|
|
* @return self |
72
|
|
|
*/ |
73
|
|
|
public static function fromString(string $str): self |
74
|
|
|
{ |
75
|
|
|
if (!preg_match(self::PEM_REGEX, $str, $match)) { |
76
|
|
|
throw new \UnexpectedValueException("Not a PEM formatted string."); |
77
|
|
|
} |
78
|
|
|
$payload = preg_replace('/\s+/', "", $match[2]); |
79
|
|
|
$data = base64_decode($payload, true); |
80
|
|
|
if (false === $data) { |
81
|
|
|
throw new \UnexpectedValueException("Failed to decode PEM data."); |
82
|
|
|
} |
83
|
|
|
return new self($match[1], $data); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Initialize from a file. |
88
|
|
|
* |
89
|
|
|
* @param string $filename Path to file |
90
|
|
|
* @throws \RuntimeException If file reading fails |
91
|
|
|
* @return self |
92
|
|
|
*/ |
93
|
|
|
public static function fromFile(string $filename): self |
94
|
|
|
{ |
95
|
|
|
if (!is_readable($filename) || |
96
|
|
|
false === ($str = file_get_contents($filename))) { |
97
|
|
|
throw new \RuntimeException("Failed to read $filename."); |
98
|
|
|
} |
99
|
|
|
return self::fromString($str); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get content type. |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public function type(): string |
108
|
|
|
{ |
109
|
|
|
return $this->_type; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get payload. |
114
|
|
|
* |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
public function data(): string |
118
|
|
|
{ |
119
|
|
|
return $this->_data; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Encode to PEM string. |
124
|
|
|
* |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
public function string(): string |
128
|
|
|
{ |
129
|
|
|
return "-----BEGIN {$this->_type}-----\n" . |
130
|
|
|
trim(chunk_split(base64_encode($this->_data), 64, "\n")) . "\n" . |
131
|
|
|
"-----END {$this->_type}-----"; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
public function __toString() |
139
|
|
|
{ |
140
|
|
|
return $this->string(); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|