From 47b7344e2efd3da385161d1a253ad139f95734c8 Mon Sep 17 00:00:00 2001 From: yggverse Date: Fri, 2 Aug 2024 21:07:06 +0300 Subject: [PATCH] implement identity model --- src/Abstract/Model/Identity.php | 77 +++++++++++++++++++++++++++++++ src/Interface/Model/Identity.php | 40 ++++++++++++++++ src/Model/Identity/Gemini.php | 78 ++++++++++++++++++++++++++++++++ 3 files changed, 195 insertions(+) create mode 100644 src/Abstract/Model/Identity.php create mode 100644 src/Interface/Model/Identity.php create mode 100644 src/Model/Identity/Gemini.php diff --git a/src/Abstract/Model/Identity.php b/src/Abstract/Model/Identity.php new file mode 100644 index 00000000..cf6027b7 --- /dev/null +++ b/src/Abstract/Model/Identity.php @@ -0,0 +1,77 @@ + $bits, + 'private_key_type' => $type + ] + ); + + if ($key) + { + return $key; + } + + throw new Exception; + } + + // Generate a new certificate signing request (CSR) + public static function csr( + OpenSSLAsymmetricKey $key + ): OpenSSLCertificateSigningRequest + { + $csr = openssl_csr_new( + [ + // 'commonName' => $commonName @TODO + ], + $key + ); + + if ($csr) + { + return $csr; + } + + throw new Exception; + } + + // Sign the CSR + public static function sign( + OpenSSLCertificateSigningRequest $csr, + OpenSSLCertificate|OpenSSLAsymmetricKey|array|string $key, + OpenSSLCertificate|string|null $crt = null, // self-signed + int $days = self::CSR_SIGN_DAYS + ): OpenSSLCertificate + { + $x509 = openssl_csr_sign( + $csr, + $crt, + $key, + $days + ); + + if ($x509) + { + return $x509; + } + + throw new Exception; + } +} \ No newline at end of file diff --git a/src/Interface/Model/Identity.php b/src/Interface/Model/Identity.php new file mode 100644 index 00000000..4cc06cd9 --- /dev/null +++ b/src/Interface/Model/Identity.php @@ -0,0 +1,40 @@ +_key = $key ? $key : self::new(); + + // Init self-signed certificate + $this->_crt = $crt ? $crt : self::sign( + self::csr( + $this->_key + ), + $this->_key, + null, + self::CSR_SIGN_DAYS + ); + } + + // Get certificate + public function crt( + ?OpenSSLCertificate $crt = null + ): string + { + $pem = ''; + + $result = openssl_x509_export( + $crt ? $crt : $this->_crt, + $pem + ); + + if ($result) + { + return $pem; + } + + throw new Exception; + } + + // Get private key + public function key( + ?OpenSSLAsymmetricKey $key = null + ): string + { + $pem = ''; + + $result = openssl_pkey_export( + $key ? $key : $this->_key, + $pem + ); + + if ($result) + { + return $pem; + } + + throw new Exception; + } +} \ No newline at end of file