@@ -22,6 +22,7 @@ public function sign(JSignParam $params)
2222 {
2323 try {
2424 $ this ->validation ($ params );
25+ $ this ->repackCertificateIfPasswordIsUnicode ($ params );
2526
2627 $ commandSign = $ this ->commandSign ($ params );
2728 \exec ($ commandSign , $ output );
@@ -51,6 +52,22 @@ public function sign(JSignParam $params)
5152 }
5253 }
5354
55+ /**
56+ * JSignPdf don't works as well at CLI interfaceif the password have
57+ * unicode chars. As workaround, I changed the password certificate in
58+ * memory.
59+ */
60+ private function repackCertificateIfPasswordIsUnicode (JSignParam $ params )
61+ {
62+ if (!mb_detect_encoding ($ params ->getPassword (), 'ASCII ' , true )) {
63+ $ password = md5 (microtime ());
64+ $ certInfo = $ this ->pkcs12Read ($ params );
65+ $ newCert = $ this ->exportToPkcs12 ($ certInfo ['cert ' ], $ certInfo ['pkey ' ], $ password );
66+ $ params ->setPassword ($ password );
67+ $ params ->setCertificate ($ newCert );
68+ }
69+ }
70+
5471 public function getVersion (JSignParam $ params )
5572 {
5673 $ java = $ this ->javaCommand ($ params );
@@ -75,8 +92,8 @@ private function validation(JSignParam $params)
7592 $ this ->throwIf (empty ($ params ->getPdf ()), 'PDF is Empty or Invalid. ' );
7693 $ this ->throwIf (empty ($ params ->getCertificate ()), 'Certificate is Empty or Invalid. ' );
7794 $ this ->throwIf (empty ($ params ->getPassword ()), 'Certificate Password is Empty. ' );
78- $ this ->throwIf (!$ this ->isPasswordCertificateValid ($ params-> getCertificate (), $ params -> getPassword () ), 'Certificate Password Invalid. ' );
79- $ this ->throwIf ($ this ->isExpiredCertificate ($ params-> getCertificate (), $ params -> getPassword () ), 'Certificate expired. ' );
95+ $ this ->throwIf (!$ this ->isPasswordCertificateValid ($ params ), 'Certificate Password Invalid. ' );
96+ $ this ->throwIf ($ this ->isExpiredCertificate ($ params ), 'Certificate expired. ' );
8097 if ($ params ->isUseJavaInstalled ()) {
8198 $ javaVersion = exec ("java -version 2>&1 " );
8299 $ hasJavaVersion = strpos ($ javaVersion , 'not found ' ) === false ;
@@ -135,9 +152,9 @@ private function throwIf($condition, $message)
135152 throw new Exception ($ message );
136153 }
137154
138- private function isPasswordCertificateValid ($ certificate , $ password )
155+ private function isPasswordCertificateValid (JSignParam $ params )
139156 {
140- return $ this ->pkcs12Read ($ certificate , $ password );
157+ return $ this ->pkcs12Read ($ params );
141158 }
142159
143160 /**
@@ -153,8 +170,10 @@ private function isPasswordCertificateValid($certificate, $password)
153170 * https://github.com/php/php-src/issues/12128
154171 * https://www.php.net/manual/en/function.openssl-pkcs12-read.php#128992
155172 */
156- private function pkcs12Read ($ certificate , $ password )
173+ private function pkcs12Read (JSignParam $ params )
157174 {
175+ $ certificate = $ params ->getCertificate ();
176+ $ password = $ params ->getPassword ();
158177 if (openssl_pkcs12_read ($ certificate , $ certInfo , $ password )) {
159178 return $ certInfo ;
160179 }
@@ -175,6 +194,7 @@ private function pkcs12Read($certificate, $password)
175194 REPACK_COMMAND
176195 );
177196 $ certificateRepacked = file_get_contents ($ tempEncriptedRepacked );
197+ $ params ->setCertificate ($ certificateRepacked );
178198 unlink ($ tempPassword );
179199 unlink ($ tempEncriptedOriginal );
180200 unlink ($ tempEncriptedRepacked );
@@ -185,9 +205,21 @@ private function pkcs12Read($certificate, $password)
185205 return [];
186206 }
187207
188- private function isExpiredCertificate ($ certificate , $ password )
208+ public function exportToPkcs12 (\OpenSSLCertificate |string $ certificate , \OpenSSLAsymmetricKey |\OpenSSLCertificate |string $ privateKey , string $ password )
209+ {
210+ $ certContent = null ;
211+ openssl_pkcs12_export (
212+ $ certificate ,
213+ $ certContent ,
214+ $ privateKey ,
215+ $ password ,
216+ );
217+ return $ certContent ;
218+ }
219+
220+ private function isExpiredCertificate (JSignParam $ params )
189221 {
190- $ certInfo = $ this ->pkcs12Read ($ certificate , $ password );
222+ $ certInfo = $ this ->pkcs12Read ($ params );
191223 $ certificate = openssl_x509_parse ($ certInfo ['cert ' ]);
192224 $ dateCert = date_create ()->setTimestamp ($ certificate ['validTo_time_t ' ]);
193225 return $ dateCert <= date_create ();
0 commit comments