Skip to content

Commit

Permalink
Merge pull request #3 from XigenChris/develop
Browse files Browse the repository at this point in the history
Merge develop changes into master
  • Loading branch information
AlphaRecon19 authored Apr 28, 2017
2 parents 819fb30 + f509d5f commit 4760ed9
Show file tree
Hide file tree
Showing 16 changed files with 549 additions and 136 deletions.
4 changes: 4 additions & 0 deletions ComodoDecodeCSR
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@ use Symfony\Component\Console\Application;
use Xigen\Console;

$application = new Application('ComodoDecodeCSR', '0.4');

$application->add(new Console\Check());
$application->add(new Console\Hashes());
$application->add(new Console\CreateFile());

$application->run();
130 changes: 109 additions & 21 deletions src/ComodoDecodeCSR.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,21 @@

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Psr7\Response;

class ComodoDecodeCSR
{
use Traits\ComodoDecodeCSR\Getters;
use Traits\ComodoDecodeCSR\Setters;
use Traits\GetSetUnset;

protected $MD5;
protected $SHA1;
protected $Endpoint = "https://secure.comodo.net/products/!decodeCSR";
protected $CSR;
/**
* An array of warnings that can be show after the test
* @var array
*/
protected $warnings = [];
protected $Form = [
'responseFormat' => 'N',
'showErrorCodes' => 'N',
Expand All @@ -40,6 +45,29 @@ class ComodoDecodeCSR
];
private $request;

private $forceSSL = false;

public function getCN()
{
$CSRInfo = $this->decodeCSR();
return $CSRInfo['subject']['CN'];
}

public function setCSR($csr)
{
$this->CSR = $csr;
//Check that this is a valid CSR
$this->decodeCSR();
$this->Form['csr'] = $csr;
}

protected function addWarning($code, $message)
{
$this->warnings[] = [
$code => $message
];
}

public function fetchHashes()
{
$client = new Client();
Expand All @@ -53,19 +81,39 @@ public function fetchHashes()

public function checkInstalled()
{
$domain = $this->getCN();
$URL = 'http://' . $domain . "/" . $this->getmd5() . '.txt';

$client = new Client();

try {
$request = $client->request('GET', $URL);
} catch (ClientException $e) {
$domain = $this->getCN();
} catch (\Exception $e) {
return false;
}

$response = $this->fetchDVCFile($domain);
if ($response == false) {
return false;
}

$check = $this->checkDVC($response);
if ($check === true) {
return $check;
}

//Try again but this time use https://
$this->forceSSL = true;

$response = $this->fetchDVCFile($domain);
if ($response == false) {
return false;
}

$response = "" . $request->getBody();
return $this->checkDVC($response);
$check = $this->checkDVC($response);
if ($check === true) {
//TODO Add a message to say then you will need to select 'HTTPS CSR
//Hash'
return $check;
}

return false;
}

public function generateDVC()
Expand All @@ -76,28 +124,41 @@ public function generateDVC()
return $DVC;
}

public function checkDVC($response)
/**
*
* @param GuzzleHttp\Psr7\Response $response
* @return bool
*/
public function checkDVC(Response $response)
{
$body = $response->getBody() . '';
$DVC = $this->generateDVC();

//Check if we received a 301 or 302 redirect
if ($response->getStatusCode() === 301 || $response->getStatusCode() == 302) {
return false;
}

//If the response matches the DVC value return true
if ($response === $DVC) {
if ($body === $DVC) {
return true;
}

//Check if last 2 characters are new lines
if (substr($response, -2) === "\n\n") {
$response = substr($response, 0, -2) . "\n";
if (substr($body, -2) === "\n\n") {
$body = substr($body, 0, -2) . "\n";
}

//Check if last character is not a new line
if (substr($response, -1) !== "\n") {
if (substr($body, -1) !== "\n") {
//Add said new line
$response = $response . "\n";
$body = $body . "\n";
}

var_dump($body, $DVC);

//Check it again
if ($response === $DVC) {
if ($body === $DVC) {
return true;
}

Expand All @@ -106,10 +167,14 @@ public function checkDVC($response)

private function decodeCSR()
{
$data = openssl_csr_get_public_key($this->getCSR());
$details = openssl_pkey_get_details($data);
$key = $details['key'];
$subject = openssl_csr_get_subject($this->getCSR());
try {
$data = openssl_csr_get_public_key($this->getCSR());
$details = openssl_pkey_get_details($data);
$key = $details['key'];
$subject = openssl_csr_get_subject($this->getCSR());
} catch (\Exception $e) {
throw new Exception("Invalid CSR");
}

return array(
"subject" => $subject,
Expand Down Expand Up @@ -138,4 +203,27 @@ private function processResponse()

return $data ? $data : false;
}

private function fetchDVCFile($domain)
{
//We do most of our DVC over http:// unless the site is fully SSL
$protocol = 'http://';

if ($this->forceSSL) {
$protocol = 'https://';
}

$url = $protocol . $domain . "/" . $this->getMD5() . '.txt';

$client = new Client(['allow_redirects' => false, 'verify' => false]);

try {
$response = $client->request('GET', $url);
} catch (ClientException $e) {
var_dump('te', $e);
return false;
}

return $response;
}
}
19 changes: 19 additions & 0 deletions src/Console/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,28 @@
namespace Xigen\Console;

use Xigen\Application;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command;

abstract class BaseCommand extends Command
{
/**
* Load a .csr file via an CLI argument
* @param Symfony\Component\Console\Input\InputInterface $input
* @param Symfony\Component\Console\Output\OutputInterface $output
* @return bool|void
*/
public function loadCSR(InputInterface $input, OutputInterface $output)
{
$csrFile = $input->getArgument('csr');
if (!file_exists($csrFile)) {
$output->writeln('<error>Unable to load '. $csrFile .'</error>');
$output->writeln('<error>Please check the path and try again</error>');

exit();
}

return file_get_contents($csrFile);
}
}
32 changes: 15 additions & 17 deletions src/Console/Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use Xigen\ComodoDecodeCSR;
use Xigen\Exception;

class Check extends BaseCommand
{
Expand All @@ -31,29 +32,26 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$csrFile = $input->getArgument('csr');
if (!file_exists($csrFile)) {
$output->writeln('<error>Unable to load '. $csrFile .'</error>');
$output->writeln('<error>Please check the path and try again</error>');
return false;
}
$comodoDecodeCSR = new ComodoDecodeCSR();

$csr = file_get_contents($csrFile);
try {
$comodoDecodeCSR->setCSR($this->loadCSR($input, $output));
$comodoDecodeCSR->fetchHashes();
} catch (Exception $e) {
$output->writeln('<error>Error!</error>');
$output->writeln('Invalid CSR');

$ComodoDecodeCSR = new ComodoDecodeCSR();
$ComodoDecodeCSR->setCSR($csr);
$ComodoDecodeCSR->fetchHashes();
return 3;
}

if ($ComodoDecodeCSR->checkInstalled()) {
$output->writeln('<info>Success!</info>');
$output->writeln('This domain should pass DVC');
if ($comodoDecodeCSR->checkInstalled()) {
$output->writeln('<info>Success!</info> This domain should pass DVC');

return true;
return 0;
}

$output->writeln('<error>Fail!</error>');
$output->writeln('There is something wrong with the validation file');
$output->writeln('<error>Fail!</error> There is something wrong with the validation file');

return false;
return 2;
}
}
62 changes: 62 additions & 0 deletions src/Console/CreateFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* @author Chris Hilsdon <[email protected]>
* @package ComodoDecodeCSR
* @copyright 2016 Xigen
* @license GNU General Public License v3
* @link https://github.com/XigenChris/ComodoDecodeCSR
*/

namespace Xigen\Console;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use Xigen\ComodoDecodeCSR;
use Xigen\Exception;

class CreateFile extends BaseCommand
{
protected function configure()
{
$this
->setName("createfile")
->setDescription("Creates the file needed for DVC")
->addArgument(
'csr',
InputArgument::REQUIRED,
'Location of csr file'
)
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$comodoDecodeCSR = new ComodoDecodeCSR();

try {
$comodoDecodeCSR->setCSR($this->loadCSR($input, $output));
} catch (Exception $e) {
$output->writeln('<error>Error!</error>');
$output->writeln('Invalid CSR');

return 3;
}

$hashes = $comodoDecodeCSR->fetchHashes();

if (!$hashes) {
$output->writeln('<error>Fail!</error>');
$output->writeln('Unable to fetch hashes');

return 2;
}

$output->writeln('<info>Filename:</info> ' . $hashes['md5'] . '.txt');
$output->writeln('<info>Contents:</info>');
$output->writeln($comodoDecodeCSR->generateDVC());
$output->writeln('<info>URL:</info> http://' . $comodoDecodeCSR->getCN() . '/' . $hashes['md5'] . '.txt');

return 0;
}
}
Loading

0 comments on commit 4760ed9

Please sign in to comment.