afosto / yaac

Yet another ACME client: a decoupled LetsEncrypt client
Other
219 stars 86 forks source link

Helper.php: explode PHP_EOL on WINDOWS #8

Closed JobirYusupov closed 3 years ago

JobirYusupov commented 4 years ago

class TestAcme2Controller extends ZControlCmd {

public function file_force_contents( $fullPath, $contents, $flags = 0 ){
    $parts = explode( '/', $fullPath );
    array_pop( $parts );
    $dir = implode( '/', $parts );

    if( !is_dir( $dir ) )
        mkdir( $dir, 0777, true );

    file_put_contents( $fullPath, $contents, $flags );
}

public function actionRun()
{
    //Prepare flysystem
    $adapter = new Local('data');

    $filesystem = new Filesystem($adapter);

    //Construct the client
    $client = new Client([
        'username' => 'jobiryusupov0@gmail.com',
        'fs'       => $filesystem,
        'mode'     => Client::MODE_STAGING,
    ]);

    $order = $client->createOrder(['vadeacme.zoft.uz']);

    $authorizations = $client->authorize($order);

    foreach ($authorizations as $authorization) {
        $file = $authorization->getFile();
        file_put_contents($file->getFilename(), $file->getContents());

        $this->file_force_contents( Root.'/execut/web/eyuf/.well-known/acme-challenge/'.$file->getFilename(), $file->getContents(), LOCK_EX );

    }

    if (!$client->selfTest($authorization, Client::VALIDATION_HTTP)) {
        throw new \Exception('Count not verify ownership via HTTP');
    }

    foreach ($authorizations as $authorization) {
        $client->validate($authorization->getHttpChallenge(), 15);
    }

    if ($client->isReady($order)) {
        $certificate = $client->getCertificate($order);  //error here

        file_put_contents('certificate.cert', $certificate->getCertificate());
        $this->file_force_contents( Root.'/execut/web/eyuf/.well-known/acme-challenge/private.key', $certificate->getPrivateKey(), LOCK_EX );
    }

}

}

error: Exception 'GuzzleHttp\Exception\ClientException' with message 'Client error: POST https://acme-staging-v02.api.letsencrypt.org/acme/finalize/13081845/84000364 resulted in a 400 Bad Request response: { "type": "urn:ietf:params:acme:error:malformed", "detail": "Error parsing certificate request: asn1: syntax error: (truncated...) '

bakkerpeter commented 4 years ago

@JobirYusupov Could you post the full response of the server here? The snippet suggests that you still have issues with openSSL as I think the CSR is not valid (that is created on the fly).

bakkerpeter commented 4 years ago

Closing this, feel free to re-open if you wish to add something.

mxprogramer commented 4 years ago

I found the solution, error was on toDer function on Helper.php, I'm on windows and this returned an empty string. Changed line from this: $lines = explode(PHP_EOL, $pem); To this: $lines = preg_split('/\r\n|\r|\n/', $pem);

Now is working as expected

Thanks

trusteddigital commented 4 years ago

Just a note to say that I had this exact problem! Thank you @mxprogramer for posting your workaround. I wonder if we could do something to check if windows or not and do an if for future releases. I'll have a think at some point.

bakkerpeter commented 4 years ago

Thanks for the update. To my knowledge PHP_EOL was was working the same on windows as on Linux, therefore I could not comprehend the situation, but it seems like you have ran into something that is also described here

https://stackoverflow.com/questions/35625268/php-eol-on-windows-and-linux

https://eidson.info/post/php_eol_is_broken

I agree and think we should change this to your suggestion. So maybe just replace explode(PHP_EOL... with preg_match(...

Could you go ahead and add a PR?

Cheers and thanks for taking to time to point it out!

Op zo 28 jun. 2020 19:29 schreef Matt Moore notifications@github.com:

Just a note to say that I had this exact problem! Thank you @mxprogramer https://github.com/mxprogramer for posting your workaround. I wonder if we could do something to check if windows or not and do an if for future releases. I'll have a think at some point.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/afosto/yaac/issues/8#issuecomment-650797342, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFROB2EK2KS3SPWHFZYOA53RY54W7ANCNFSM4MFGJ3EA .

bakkerpeter commented 3 years ago

Fixed with https://github.com/afosto/yaac/pull/34 Thanks @gpibarra