|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Patsura Dmitry https://github.com/ovr <[email protected]> |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
namespace SocialConnect\OpenIDConnect; |
|
8
|
|
|
|
|
9
|
|
|
use SocialConnect\OpenIDConnect\Exception\InvalidJWK; |
|
10
|
|
|
|
|
11
|
|
|
class JWK |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $kty; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $n; |
|
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $e; |
|
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param array $parameters |
|
30
|
|
|
* @throws InvalidJWK |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct($parameters) |
|
33
|
|
|
{ |
|
34
|
|
|
if (!isset($parameters['kty'])) { |
|
35
|
|
|
throw new InvalidJWK('Unknown kty'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$this->kty = $parameters['kty']; |
|
39
|
|
|
|
|
40
|
|
|
if (!isset($parameters['n'])) { |
|
41
|
|
|
throw new InvalidJWK('Unknown n'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$this->n = $parameters['n']; |
|
45
|
|
|
|
|
46
|
|
|
if (!isset($parameters['e'])) { |
|
47
|
|
|
throw new InvalidJWK('Unknown e'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$this->e = $parameters['e']; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getPublicKey() |
|
57
|
|
|
{ |
|
58
|
|
|
$modulus = JWT::urlsafeB64Decode($this->n); |
|
59
|
|
|
$publicExponent = JWT::urlsafeB64Decode($this->e); |
|
60
|
|
|
|
|
61
|
|
|
$components = array( |
|
62
|
|
|
'modulus' => pack('Ca*a*', 2, self::encodeLength(strlen($modulus)), $modulus), |
|
63
|
|
|
'publicExponent' => pack('Ca*a*', 2, self::encodeLength(strlen($publicExponent)), $publicExponent) |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
$publicKey = pack( |
|
67
|
|
|
'Ca*a*a*', |
|
68
|
|
|
48, |
|
69
|
|
|
self::encodeLength(strlen($components['modulus']) + strlen($components['publicExponent'])), |
|
70
|
|
|
$components['modulus'], |
|
71
|
|
|
$components['publicExponent'] |
|
72
|
|
|
); |
|
73
|
|
|
|
|
74
|
|
|
// sequence(oid(1.2.840.113549.1.1.1), null)) = rsaEncryption. |
|
75
|
|
|
$rsaOID = pack('H*', '300d06092a864886f70d0101010500'); // hex version of MA0GCSqGSIb3DQEBAQUA |
|
76
|
|
|
$publicKey = chr(0) . $publicKey; |
|
77
|
|
|
$publicKey = chr(3) . self::encodeLength(strlen($publicKey)) . $publicKey; |
|
78
|
|
|
$publicKey = pack( |
|
79
|
|
|
'Ca*a*', |
|
80
|
|
|
48, |
|
81
|
|
|
self::encodeLength(strlen($rsaOID . $RSAPublicKey)), |
|
|
|
|
|
|
82
|
|
|
$rsaOID . $publicKey |
|
83
|
|
|
); |
|
84
|
|
|
|
|
85
|
|
|
$publicKey = "-----BEGIN PUBLIC KEY-----\r\n" . |
|
86
|
|
|
chunk_split(base64_encode($publicKey), 64) . |
|
87
|
|
|
'-----END PUBLIC KEY-----'; |
|
88
|
|
|
|
|
89
|
|
|
return $publicKey; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* DER-encode the length |
|
94
|
|
|
* |
|
95
|
|
|
* DER supports lengths up to (2**8)**127, however, we'll only support lengths up to (2**8)**4. See |
|
96
|
|
|
* {@link http://itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf#p=13 X.690 paragraph 8.1.3} for more information. |
|
97
|
|
|
* |
|
98
|
|
|
* @access private |
|
99
|
|
|
* @param int $length |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
|
|
private static function encodeLength($length) |
|
103
|
|
|
{ |
|
104
|
|
|
if ($length <= 0x7F) { |
|
105
|
|
|
return chr($length); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$temp = ltrim(pack('N', $length), chr(0)); |
|
109
|
|
|
return pack('Ca*', 0x80 | strlen($temp), $temp); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.