From 90831f407bfe18de6241bc0e998b9bab61f1ca41 Mon Sep 17 00:00:00 2001 From: Peter Goodhall Date: Mon, 17 Aug 2020 17:02:54 +0100 Subject: [PATCH] Added all the parts to allow uploading p12 files into Cloudlog plus storing the data --- application/config/migration.php | 2 +- application/config/mimes.php | 2 +- application/controllers/Lotw.php | 176 +++++++++++++++++- .../migrations/043_add_key_to_lotw_certs.php | 21 +++ application/models/LotwCert.php | 42 +++++ application/views/lotw_views/index.php | 39 +++- application/views/lotw_views/upload_cert.php | 33 ++++ 7 files changed, 297 insertions(+), 18 deletions(-) create mode 100644 application/migrations/043_add_key_to_lotw_certs.php create mode 100644 application/views/lotw_views/upload_cert.php diff --git a/application/config/migration.php b/application/config/migration.php index bb6a07c3..613947b1 100644 --- a/application/config/migration.php +++ b/application/config/migration.php @@ -21,7 +21,7 @@ $config['migration_enabled'] = TRUE; | be upgraded / downgraded to. | */ -$config['migration_version'] = 42; +$config['migration_version'] = 43; /* |-------------------------------------------------------------------------- diff --git a/application/config/mimes.php b/application/config/mimes.php index e203c998..86486228 100644 --- a/application/config/mimes.php +++ b/application/config/mimes.php @@ -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'), diff --git a/application/controllers/Lotw.php b/application/controllers/Lotw.php index c6130645..ebd7aeb2 100644 --- a/application/controllers/Lotw.php +++ b/application/controllers/Lotw.php @@ -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); diff --git a/application/migrations/043_add_key_to_lotw_certs.php b/application/migrations/043_add_key_to_lotw_certs.php new file mode 100644 index 00000000..7a7f2b89 --- /dev/null +++ b/application/migrations/043_add_key_to_lotw_certs.php @@ -0,0 +1,21 @@ +dbforge->add_column('lotw_certs', $fields); + } + + public function down() + { + $this->dbforge->drop_column('lotw_certs', 'key'); + } +} \ No newline at end of file diff --git a/application/models/LotwCert.php b/application/models/LotwCert.php index b26c712e..c567121c 100644 --- a/application/models/LotwCert.php +++ b/application/models/LotwCert.php @@ -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); diff --git a/application/views/lotw_views/index.php b/application/views/lotw_views/index.php index 6366fcc4..13afe825 100644 --- a/application/views/lotw_views/index.php +++ b/application/views/lotw_views/index.php @@ -5,7 +5,7 @@
- Upload CertificateAvailable Certificates + Upload CertificateAvailable Certificates
@@ -15,6 +15,12 @@
+ + + + num_rows() > 0) { ?>
@@ -26,6 +32,7 @@ Date Created Date Expires Status + Options @@ -34,13 +41,33 @@ result() as $row) { ?> callsign; ?> - cert_dxcc; ?> - date_created; ?> - date_expires; ?> - + cert_dxcc); ?> + date_created ); + $new_valid_from = date($this->config->item('qso_date_format'), $valid_form ); + echo $new_valid_from; ?> + + + date_expires ); + $new_valid_to = date($this->config->item('qso_date_format'), $valid_to ); + echo $new_valid_to; ?> + + + + + date_expires) { ?> + Valid + + Expired + + + + Delete + - +
diff --git a/application/views/lotw_views/upload_cert.php b/application/views/lotw_views/upload_cert.php new file mode 100644 index 00000000..9defc471 --- /dev/null +++ b/application/views/lotw_views/upload_cert.php @@ -0,0 +1,33 @@ +
+ +

+ + +
+
+ Upload Certificate +
+ +
+ + + + + + +
+ + +
+ + + + + +
+
+ + +