@@ -85,8 +85,8 @@ class CertificateGenerator
8585 openssl_x509_export_to_file ($ this ->ca , $ file );
8686 }
8787
88- public function saveNewCertAndKey (
89- $ commonNameForCert , $ certFile , $ keyFile , $ keyLength = null , $ subjectAltName = null
88+ private function generateCertAndKey (
89+ $ commonNameForCert , $ file , $ keyLength = null , $ subjectAltName = null
9090 ) {
9191 $ dn = [
9292 'countryName ' => 'BY ' ,
@@ -117,51 +117,53 @@ $subjectAltNameConfig
117117basicConstraints = CA:FALSE
118118$ subjectAltNameConfig
119119CONFIG ;
120- $ configFile = $ certFile . '.cnf ' ;
120+ $ configFile = $ file . '.cnf ' ;
121121 file_put_contents ($ configFile , $ configCode );
122122
123- try {
124- $ config = [
125- 'config ' => $ configFile ,
126- 'req_extensions ' => 'v3_req ' ,
127- 'x509_extensions ' => 'usr_cert ' ,
128- ];
129-
130- $ this ->lastKey = self ::generateKey ($ keyLength );
131- $ csr = openssl_csr_new ($ dn , $ this ->lastKey , $ config );
132- $ this ->lastCert = openssl_csr_sign (
133- $ csr ,
134- $ this ->ca ,
135- $ this ->caKey ,
136- /* days */ 2 ,
137- $ config ,
138- );
139- if (!$ this ->lastCert ) {
140- throw new Exception ('Failed to create certificate ' );
141- }
142-
143- $ certText = '' ;
144- openssl_x509_export ($ this ->lastCert , $ certText );
145-
146- $ keyText = '' ;
147- openssl_pkey_export ($ this ->lastKey , $ keyText , null , $ config );
148-
149- if ($ certFile === $ keyFile ) {
150- file_put_contents ($ certFile , $ certText . PHP_EOL . $ keyText );
151- } else {
152- file_put_contents ($ certFile , $ certText );
153- file_put_contents ($ keyFile , $ keyText );
154- }
155- } finally {
156- unlink ($ configFile );
157- }
158- }
123+ $ config = [
124+ 'config ' => $ configFile ,
125+ 'req_extensions ' => 'v3_req ' ,
126+ 'x509_extensions ' => 'usr_cert ' ,
127+ ];
128+
129+ $ this ->lastKey = self ::generateKey ($ keyLength );
130+ $ csr = openssl_csr_new ($ dn , $ this ->lastKey , $ config );
131+ $ this ->lastCert = openssl_csr_sign (
132+ $ csr ,
133+ $ this ->ca ,
134+ $ this ->caKey ,
135+ /* days */ 2 ,
136+ $ config ,
137+ );
159138
139+ return $ config ;
140+ }
160141
161142 public function saveNewCertAsFileWithKey (
162143 $ commonNameForCert , $ file , $ keyLength = null , $ subjectAltName = null
163144 ) {
164- $ this ->saveNewCertAndKey ($ commonNameForCert , $ file , $ file , $ keyLength , $ subjectAltName );
145+ $ config = $ this ->generateCertAndKey ($ commonNameForCert , $ file , $ keyLength , $ subjectAltName );
146+
147+ $ certText = '' ;
148+ openssl_x509_export ($ this ->lastCert , $ certText );
149+
150+ $ keyText = '' ;
151+ openssl_pkey_export ($ this ->lastKey , $ keyText , null , $ config );
152+
153+ file_put_contents ($ file , $ certText . PHP_EOL . $ keyText );
154+
155+ unlink ($ config ['config ' ]);
156+ }
157+
158+ public function saveNewCertAndKey (
159+ $ commonNameForCert , $ certFile , $ keyFile , $ keyLength = null , $ subjectAltName = null
160+ ) {
161+ $ config = $ this ->generateCertAndKey ($ commonNameForCert , $ certFile , $ keyLength , $ subjectAltName );
162+
163+ openssl_x509_export_to_file ($ this ->lastCert , $ certFile );
164+ openssl_pkey_export_to_file ($ this ->lastKey , $ keyFile , null , $ config );
165+
166+ unlink ($ config ['config ' ]);
165167 }
166168
167169 public function getCertDigest ($ algo )
0 commit comments