Added all the parts to allow uploading p12 files into Cloudlog plus storing the data

pull/594/head
Peter Goodhall 2020-08-17 17:02:54 +01:00
rodzic 7852fbe6d1
commit 90831f407b
7 zmienionych plików z 297 dodań i 18 usunięć

Wyświetl plik

@ -21,7 +21,7 @@ $config['migration_enabled'] = TRUE;
| be upgraded / downgraded to.
|
*/
$config['migration_version'] = 42;
$config['migration_version'] = 43;
/*
|--------------------------------------------------------------------------

Wyświetl plik

@ -117,7 +117,7 @@ return array(
'json' => array('application/json', 'text/json'),
'pem' => array('application/x-x509-user-cert', 'application/x-pem-file', 'application/octet-stream'),
'p10' => array('application/x-pkcs10', 'application/pkcs10'),
'p12' => 'application/x-pkcs12',
'p12' => 'application/octet-stream',
'p7a' => 'application/x-pkcs7-signature',
'p7c' => array('application/pkcs7-mime', 'application/x-pkcs7-mime'),
'p7m' => array('application/pkcs7-mime', 'application/x-pkcs7-mime'),

Wyświetl plik

@ -34,10 +34,157 @@ class Lotw extends CI_Controller {
$this->load->view('interface_assets/footer');
}
public function key() {
/*
|--------------------------------------------------------------------------
| Function: cert_upload
|--------------------------------------------------------------------------
|
| Nothing fancy just shows the cert_upload form for uploading p12 files
|
*/
public function cert_upload() {
// Set Page Title
$data['page_title'] = "Logbook of the World";
// Load Views
$this->load->view('interface_assets/header', $data);
$this->load->view('lotw_views/upload_cert', array('error' => ' ' ));
$this->load->view('interface_assets/footer');
}
/*
|--------------------------------------------------------------------------
| Function: do_cert_upload
|--------------------------------------------------------------------------
|
| do_cert_upload is called from cert_upload form submit and handles uploading
| and processing of p12 files and storing the data into mysql
|
*/
public function do_cert_upload()
{
$config['upload_path'] = './uploads/lotw/certs';
$config['allowed_types'] = 'p12';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile'))
{
// Upload of P12 Failed
$error = array('error' => $this->upload->display_errors());
// Set Page Title
$data['page_title'] = "Logbook of the World";
// Load Views
$this->load->view('interface_assets/header', $data);
$this->load->view('lotw_views/upload_cert', $error);
$this->load->view('interface_assets/footer');
}
else
{
// Load database queries
$this->load->model('LotwCert');
//Upload of P12 successful
$data = array('upload_data' => $this->upload->data());
$info = $this->decrypt_key($data['upload_data']['full_path']);
// Check to see if certificate is already in the system
$new_certficiate = $this->LotwCert->find_cert($info['issued_callsign'], $this->session->userdata('user_id'));
// Check DXCC & Store Country Name
$this->load->model('Logbook_model');
$dxcc_check = $this->Logbook_model->check_dxcc_table($info['issued_callsign'], $info['validFrom']);
$dxcc = $dxcc_check[1];
if($new_certficiate == 0) {
// New Certificate Store in Database
// Store Certificate Data into MySQL
$this->LotwCert->store_certficiate($this->session->userdata('user_id'), $info['issued_callsign'], $dxcc, $info['validFrom'], $info['validTo_Date'], $info['pem_key']);
// Cert success flash message
$this->session->set_flashdata('Success', $info['issued_callsign'].' Certficiate Imported.');
} else {
// Certficiate is in the system time to update
$this->LotwCert->update_certficiate($this->session->userdata('user_id'), $info['issued_callsign'], $dxcc, $info['validFrom'], $info['validTo_Date'], $info['pem_key']);
// Cert success flash message
$this->session->set_flashdata('Success', $info['issued_callsign'].' Certficiate Updated.');
}
// p12 certificate processed time to delete the file
unlink($data['upload_data']['full_path']);
// Get Array of the logged in users LOTW certs.
$data['lotw_cert_results'] = $this->LotwCert->lotw_certs($this->session->userdata('user_id'));
// Set Page Title
$data['page_title'] = "Logbook of the World";
// Load Views
$this->load->view('interface_assets/header', $data);
$this->load->view('lotw_views/index');
$this->load->view('interface_assets/footer');
}
}
/*
|--------------------------------------------------------------------------
| Function: delete_cert
|--------------------------------------------------------------------------
|
| Deletes LOTW certificate from the MySQL table
|
*/
public function delete_cert($cert_id) {
$this->load->model('LotwCert');
$this->LotwCert->delete_certficiate($this->session->userdata('user_id'), $cert_id);
$this->session->set_flashdata('Success', 'Certficiate Deleted.');
redirect('/lotw/');
}
/*
|--------------------------------------------------------------------------
| Function: peter
|--------------------------------------------------------------------------
|
| Temp function to test development bits
|
*/
public function peter() {
$this->load->model('LotwCert');
$this->load->model('Logbook_model');
$dxcc = $this->Logbook_model->check_dxcc_table("2M0SQL", "2020-05-07 17:20:27");
print_r($dxcc);
// Get Array of the logged in users LOTW certs.
echo $this->LotwCert->find_cert($this->session->userdata('user_id'), "2M0SQL");
}
/*
|--------------------------------------------------------------------------
| Function: decrypt_key
|--------------------------------------------------------------------------
|
| Accepts p12 file and optional password and encrypts the file returning
| the required fields for LOTW and the PEM Key
|
*/
public function decrypt_key($file, $password = "") {
$results = array();
$password = "";
$filename = file_get_contents('file:///mnt/c/lotw/php/file-to-read.p12');
$password = $password; // Only needed if 12 has a password set
$filename = file_get_contents('file://'.$file);
$worked = openssl_pkcs12_read($filename, $results, $password);
if($worked) {
// Reading p12 successful
@ -48,11 +195,20 @@ class Lotw extends CI_Controller {
// Store PEM Key in Array
$data['pem_key'] = $result;
} else {
echo openssl_error_string();
// Error Log Error Message
log_message('error', openssl_error_string());
// Set warning message redirect to LOTW main page
$this->session->set_flashdata('Warning', openssl_error_string());
redirect('/lotw/');
}
} else {
// Reading p12 failed
echo openssl_error_string();
// Reading p12 failed log error message
log_message('error', openssl_error_string());
// Set warning message redirect to LOTW main page
$this->session->set_flashdata('Warning', openssl_error_string());
redirect('/lotw/');
}
// Read Cert Data
@ -61,10 +217,10 @@ class Lotw extends CI_Controller {
// Store Variables
$data['issued_callsign'] = $certdata['subject']['undefined'];
$data['issued_name'] = $certdata['subject']['commonName'];
$data['validFrom_Date'] = date("d-m-Y H:i:s", strtotime($certdata['validFrom']));
$data['validTo_Date'] = date("d-m-Y H:i:s", strtotime($certdata['validTo']));
$data['validFrom'] = $certdata['extensions']['1.3.6.1.4.1.12348.1.2'];
$data['validTo_Date'] = $certdata['extensions']['1.3.6.1.4.1.12348.1.3'];
print_r($data);
return $data;
}
private function loadFromFile($filepath)
@ -431,7 +587,7 @@ class Lotw extends CI_Controller {
$key = "";
$pkeyid = openssl_pkey_get_private($key, 'cloudlog');
$pkeyid = openssl_pkey_get_private($key, 'peter');
//openssl_sign($plaintext, $signature, $pkeyid, OPENSSL_ALGO_SHA1 );
//openssl_free_key($pkeyid);

Wyświetl plik

@ -0,0 +1,21 @@
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Migration_add_key_to_lotw_certs extends CI_Migration {
public function up()
{
$fields = array(
'cert_key TEXT',
);
$this->dbforge->add_column('lotw_certs', $fields);
}
public function down()
{
$this->dbforge->drop_column('lotw_certs', 'key');
}
}

Wyświetl plik

@ -18,10 +18,52 @@ class LotwCert extends CI_Model {
*/
function lotw_certs($user_id) {
$this->db->where('user_id', $user_id);
$this->db->group_by("callsign");
$this->db->order_by('cert_dxcc', 'ASC');
$query = $this->db->get('lotw_certs');
return $query;
}
function find_cert($callsign, $user_id) {
$this->db->where('user_id', $user_id);
$this->db->where('callsign', $callsign);
$query = $this->db->get('lotw_certs');
return $query->num_rows();
}
function store_certficiate($user_id, $callsign, $dxcc, $date_created, $date_expires, $cert_key) {
$data = array(
'user_id' => $user_id,
'callsign' => $callsign,
'cert_dxcc' => $dxcc,
'date_created' => $date_created,
'date_expires' => $date_expires,
'cert_key' => $cert_key,
);
$this->db->insert('lotw_certs', $data);
}
function update_certficiate($user_id, $callsign, $dxcc, $date_created, $date_expires, $cert_key) {
$data = array(
'cert_dxcc' => $dxcc,
'date_created' => $date_created,
'date_expires' => $date_expires,
'cert_key' => $cert_key,
);
$this->db->where('user_id', $user_id);
$this->db->where('callsign', $callsign);
$this->db->update('lotw_certs', $data);
}
function delete_certficiate($user_id, $lotw_cert_id) {
$this->db->where('lotw_cert_id', $lotw_cert_id);
$this->db->where('user_id', $user_id);
$this->db->delete('lotw_certs');
}
function empty_table($table) {
$this->db->empty_table($table);

Wyświetl plik

@ -5,7 +5,7 @@
<!-- Card Starts -->
<div class="card">
<div class="card-header">
<a class="btn btn-success btn-sm float-right" href="#" role="button"><i class="fas fa-cloud-upload-alt"></i> Upload Certificate</a>Available Certificates
<a class="btn btn-success btn-sm float-right" href="<?php echo site_url('/lotw/cert_upload'); ?>" role="button"><i class="fas fa-cloud-upload-alt"></i> Upload Certificate</a>Available Certificates
</div>
<div class="card-body">
@ -15,6 +15,12 @@
</div>
<?php } ?>
<?php if(isset($_SESSION['Success'])) { ?>
<div class="alert alert-success" role="alert">
<?php echo $_SESSION['Success']; ?>
</div>
<?php } ?>
<?php if ($lotw_cert_results->num_rows() > 0) { ?>
<div class="table-responsive">
@ -26,6 +32,7 @@
<th scope="col">Date Created</th>
<th scope="col">Date Expires</th>
<th scope="col">Status</th>
<th scope="col">Options</th>
</tr>
</thead>
@ -34,13 +41,33 @@
<?php foreach ($lotw_cert_results->result() as $row) { ?>
<tr>
<td><?php echo $row->callsign; ?></td>
<td><?php echo $row->cert_dxcc; ?></td>
<td><?php echo $row->date_created; ?></td>
<td><?php echo $row->date_expires; ?></td>
<td></td>
<td><?php echo ucfirst($row->cert_dxcc); ?></td>
<td><?php
$valid_form = strtotime( $row->date_created );
$new_valid_from = date($this->config->item('qso_date_format'), $valid_form );
echo $new_valid_from; ?>
</td>
<td>
<?php
$valid_to = strtotime( $row->date_expires );
$new_valid_to = date($this->config->item('qso_date_format'), $valid_to );
echo $new_valid_to; ?>
</td>
<td>
<?php $current_date = date('Y-m-d H:i:s'); ?>
<?php if ($current_date <= $row->date_expires) { ?>
<span class="badge badge-success">Valid</span>
<?php } else { ?>
<span class="badge badge-dark">Expired</span>
<?php } ?>
</td>
<td>
<a class="btn btn-primary btn-sm" href="<?php echo site_url('lotw/delete_cert/'.$row->lotw_cert_id); ?>" role="button"><i class="far fa-trash-alt"></i> Delete</a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>

Wyświetl plik

@ -0,0 +1,33 @@
<div class="container lotw">
<h1><?php echo $page_title; ?></h1>
<!-- Card Starts -->
<div class="card">
<div class="card-header">
Upload Certificate
</div>
<div class="card-body">
<?php if($error != " ") { ?>
<div class="alert alert-danger" role="alert">
<?php echo $error; ?>
</div>
<?php } ?>
<?php echo form_open_multipart('lotw/do_cert_upload');?>
<div class="form-group">
<label for="exampleFormControlFile1">Upload LoTW P12 File</label>
<input type="file" name="userfile" class="form-control-file" id="exampleFormControlFile1">
</div>
<button type="submit" value="upload" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
<!-- Card Ends -->
</div>