@@ -38,55 +38,57 @@ private function __construct(array $data)
3838 /**
3939 * @return RSAKey
4040 */
41- public static function createFromPEM ( string $ pem ): self
41+ public static function createFromKeyDetails ( array $ details ): self
4242 {
43- $ data = self ::loadPEM ($ pem );
43+ $ values = ['kty ' => 'RSA ' ];
44+ $ keys = [
45+ 'n ' => 'n ' ,
46+ 'e ' => 'e ' ,
47+ 'd ' => 'd ' ,
48+ 'p ' => 'p ' ,
49+ 'q ' => 'q ' ,
50+ 'dp ' => 'dmp1 ' ,
51+ 'dq ' => 'dmq1 ' ,
52+ 'qi ' => 'iqmp ' ,
53+ ];
54+ foreach ($ details as $ key => $ value ) {
55+ if (\in_array ($ key , $ keys , true )) {
56+ $ value = Base64Url::encode ($ value );
57+ $ values [\array_search ($ key , $ keys , true )] = $ value ;
58+ }
59+ }
4460
45- return new self ($ data );
61+ return new self ($ values );
4662 }
4763
4864 /**
4965 * @return RSAKey
5066 */
51- public static function createFromJWK (JWK $ jwk ): self
52- {
53- return new self ($ jwk ->all ());
54- }
55-
56- private static function loadPEM (string $ data ): array
67+ public static function createFromPEM (string $ pem ): self
5768 {
58- $ res = \openssl_pkey_get_private ($ data );
69+ $ res = \openssl_pkey_get_private ($ pem );
5970 if (false === $ res ) {
60- $ res = \openssl_pkey_get_public ($ data );
71+ $ res = \openssl_pkey_get_public ($ pem );
6172 }
6273 if (false === $ res ) {
6374 throw new \InvalidArgumentException ('Unable to load the key. ' );
6475 }
6576
6677 $ details = \openssl_pkey_get_details ($ res );
78+ \openssl_free_key ($ res );
6779 if (!\array_key_exists ('rsa ' , $ details )) {
6880 throw new \InvalidArgumentException ('Unable to load the key. ' );
6981 }
7082
71- $ values = ['kty ' => 'RSA ' ];
72- $ keys = [
73- 'n ' => 'n ' ,
74- 'e ' => 'e ' ,
75- 'd ' => 'd ' ,
76- 'p ' => 'p ' ,
77- 'q ' => 'q ' ,
78- 'dp ' => 'dmp1 ' ,
79- 'dq ' => 'dmq1 ' ,
80- 'qi ' => 'iqmp ' ,
81- ];
82- foreach ($ details ['rsa ' ] as $ key => $ value ) {
83- if (\in_array ($ key , $ keys , true )) {
84- $ value = Base64Url::encode ($ value );
85- $ values [\array_search ($ key , $ keys , true )] = $ value ;
86- }
87- }
83+ return self ::createFromKeyDetails ($ details ['rsa ' ]);
84+ }
8885
89- return $ values ;
86+ /**
87+ * @return RSAKey
88+ */
89+ public static function createFromJWK (JWK $ jwk ): self
90+ {
91+ return new self ($ jwk ->all ());
9092 }
9193
9294 public function isPublic (): bool
0 commit comments