@@ -98,14 +98,14 @@ function ($domain) { return array("type" => "dns", "value" => $domain);},
9898 $ domains
9999 ))
100100 );
101- $ nonce = $ this -> client -> getLastNonce ();
101+
102102 $ finalizeUrl = $ response ['finalize ' ];
103103
104104 foreach ($ response ['authorizations ' ] as $ authz ) {
105105 // 1. getting authentication requirements
106106 // --------------------------------------
107107
108- $ response = $ this ->client -> get ($ authz );
108+ $ response = $ this ->signedRequest ($ authz, "" );
109109 $ domain = $ response ['identifier ' ]['value ' ];
110110 if (empty ($ response ['challenges ' ])) {
111111 throw new RuntimeException ("HTTP Challenge for $ domain is not available. Whole response: " .json_encode ($ response ));
@@ -148,40 +148,40 @@ function ($domain) { return array("type" => "dns", "value" => $domain);},
148148 $ this ->log ("Token for $ domain saved at $ tokenPath and should be available at $ uri " );
149149
150150 // simple self check
151- //// if ($payload !== trim(@file_get_contents($uri))) {
152- //// throw new RuntimeException("Please check $uri - token not available");
153- //// }
151+ if ($ payload !== trim (@file_get_contents ($ uri ))) {
152+ throw new RuntimeException ("Please check $ uri - token not available " );
153+ }
154154
155155 $ this ->log ("Sending request to challenge " );
156156
157157
158158 // send request to challenge
159- $ result = $ this -> signedRequest (
160- $ challenge [ ' url ' ],
161- array ( " keyAuthorization " => $ payload ),
162- $ nonce
163- );
164-
165- // waiting loop
166- $ loop = 0 ;
167- do {
159+ $ allowed_loops = 5 ;
160+ $ result = null ;
161+ while ( $ allowed_loops > 0 ) {
162+
163+ $ result = $ this -> signedRequest (
164+ $ challenge [ ' url ' ],
165+ array ( " keyAuthorization " => $ payload )
166+ ) ;
167+
168168 if (empty ($ result ['status ' ]) || $ result ['status ' ] == "invalid " ) {
169169 throw new RuntimeException ("Verification ended with error: " . json_encode ($ result ));
170170 }
171- $ ended = !($ result ['status ' ] === "pending " );
172171
173- if (!$ ended ) {
174- $ this ->log ("Verification pending, sleeping 1s " );
175- sleep (1 );
176- }
177-
178- if ($ loop > 5 ) {
179- throw new RuntimeException ("Verification timed out " );
172+ if ($ result ['status ' ] != "pending " ) {
173+ break ;
180174 }
181175
182- $ result = $ this ->signedRequest ($ challenge ['url ' ], "" );
183- $ loop ++;
184- } while (!$ ended );
176+ $ this ->log ("Verification pending, sleeping 1s " );
177+ sleep (1 );
178+
179+ $ allowed_loops --;
180+ }
181+
182+ if ($ allowed_loops == 0 && $ result ['status ' ] === "pending " ) {
183+ throw new RuntimeException ("Verification timed out " );
184+ }
185185
186186 $ this ->log ("Verification ended with status: $ {result['status ' ]}" );
187187
@@ -229,14 +229,9 @@ function ($domain) { return array("type" => "dns", "value" => $domain);},
229229 } else if ($ this ->client ->getLastCode () == 200 ) {
230230
231231 $ this ->log ("Got certificate! YAY! " );
232- $ certificates [] = $ this ->parsePemFromBody ($ result );
233-
234-
235- foreach ($ this ->client ->getLastLinks () as $ link ) {
236- $ this ->log ("Requesting chained cert at $ link " );
237- $ result = $ this ->client ->get ($ link );
238- $ certificates [] = $ this ->parsePemFromBody ($ result );
239- }
232+ $ serverCert = $ this ->parseFirstPemFromBody ($ result );
233+ $ certificates [] = $ serverCert ;
234+ $ certificates [] = substr ($ result , strlen ($ serverCert )); // rest of ca certs
240235
241236 break ;
242237 } else {
@@ -269,10 +264,11 @@ private function readPrivateKey($path)
269264 return $ key ;
270265 }
271266
272- private function parsePemFromBody ($ body )
267+ private function parseFirstPemFromBody ($ body )
273268 {
274- $ pem = chunk_split (base64_encode ($ body ), 64 , "\n" );
275- return "-----BEGIN CERTIFICATE----- \n" . $ pem . "-----END CERTIFICATE----- \n" ;
269+ preg_match ('~(-----BEGIN.*?END CERTIFICATE-----)~ ' , $ body , $ matches );
270+
271+ return $ matches [1 ];
276272 }
277273
278274 private function getDomainPath ($ domain )
@@ -588,4 +584,14 @@ public static function encode($input)
588584 {
589585 return str_replace ('= ' , '' , strtr (base64_encode ($ input ), '+/ ' , '-_ ' ));
590586 }
587+
588+ public static function decode ($ input )
589+ {
590+ $ remainder = strlen ($ input ) % 4 ;
591+ if ($ remainder ) {
592+ $ padlen = 4 - $ remainder ;
593+ $ input .= str_repeat ('= ' , $ padlen );
594+ }
595+ return base64_decode (strtr ($ input , '-_ ' , '+/ ' ));
596+ }
591597}
0 commit comments