/**
  * @Route("/key", name="key")
  */
 public function keyAction(Request $request)
 {
     $config = array("digest_alg" => "sha512", "private_key_bits" => 4096, "private_key_type" => OPENSSL_KEYTYPE_RSA);
     $keyDir = $this->getParameter('key_dir');
     if (file_exists($keyDir . '/public')) {
         return new Response(file_get_contents($keyDir . '/public'));
     } else {
         $rsaKey = openssl_pkey_new(array('private_key_bits' => 1024, 'private_key_type' => OPENSSL_KEYTYPE_RSA));
         $privKey = openssl_pkey_get_private($rsaKey);
         openssl_pkey_export($privKey, $pem);
         //Private Key
         $pubKey = sshEncodePublicKey($rsaKey);
         //Public Key
         $umask = umask(066);
         file_put_contents($keyDir . '/private', $pem);
         //save private key into file
         file_put_contents($keyDir . '/public', $pubKey);
         //save public key into file
         return new Response($pubKey);
     }
 }
Esempio n. 2
0
function generateCrypto($filename)
{
    echo "Generating new crypto...\n";
    // Remove any old key material
    $prvFile = "./Config/" . $filename;
    $pubFile = "./Config/" . $filename . ".pub";
    if (is_readable($prvFile)) {
        unlink($prvFile);
    }
    if (is_readable($pubFile)) {
        unlink($pubFile);
    }
    // Set options for the key material generation
    $keymatOptions = array(“private_key_bits” => 4096, "private_key_type" => OPENSSL_KEYTYPE_RSA);
    // Create a new key pair
    $rsaKey = openssl_pkey_new($keymatOptions);
    // Retrieve the public key to $pem variable
    $privKey = openssl_pkey_get_private($rsaKey);
    openssl_pkey_export($privKey, $privateKey);
    // Retrieve the public key
    $publicKey = sshEncodePublicKey($rsaKey);
    // Export the files
    writeFile($prvFile, $privateKey);
    writeFile($pubFile, $publicKey);
    if (!file_exists("./Config/{$filename}") || !file_exists("./Config/{$filename}.pub")) {
        die("ERROR: Failed to create key material\n");
    }
}
function get_public_ssh_key($this_key_name, $my_passPhrase)
{
    $config = $_SESSION['config'];
    if (!is_dir($config['ssh_pubkey_path'])) {
        mkdir($config['ssh_pubkey_path'], 0777, true) or die('Fatal: Unable to create ssh public key folder');
    }
    $name = base64_encode(substr($this_key_name, 0, strrpos($this_key_name, '.')));
    $ext = substr($this_key_name, strrpos($this_key_name, '.'));
    $my_base64_keyfile = $name . $ext;
    $my_key_filename = $config['key_path'] . $name . $ext;
    $fp = fopen($my_key_filename, "r") or die('Fatal: Error opening Private Key');
    $my_key_x509 = fread($fp, filesize($my_key_filename)) or die('Fatal: Error reading Private Key');
    fclose($fp) or die('Fatal: Error closing Private Key');
    $my_private_key = openssl_pkey_get_private($my_key_x509, $my_passPhrase) or die('Fatal: Error decoding Private Key. Passphrase Incorrect');
    $my_public_key = sshEncodePublicKey(openssl_pkey_get_details($my_private_key));
    $application_type = 'application/octet-stream';
    download_header_code($this_key_name . ".ssh.pub", $my_public_key, $application_type);
}