-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkey_management.php
More file actions
191 lines (164 loc) · 6.9 KB
/
key_management.php
File metadata and controls
191 lines (164 loc) · 6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
ini_set('display_errors', 0); // Display errors in the browser (for debugging purposes)
ini_set('log_errors', 1); // Enable error logging
ini_set('error_log', '/var/www/open-encrypt.com/html/error.log'); // Absolute path to the error log file
error_reporting(E_ALL); // Report all types of errors
session_start();
require_once 'include/utils.php';
require_once 'include/db_config.php';
require_once 'include/Database.php';
require_once 'include/encryption.php';
$db = new Database($conn);
// Handle logout
if (isset($_POST['logout'])) {
logout();
}
// Make sure user is logged in
if (!isset($_SESSION['user'])) {
redirect("login.php");
}
$username = $_SESSION['user'];
?>
<html>
<head>
<title>Open Encrypt - Key Management</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<header>
<h1><a href="index.html">Open Encrypt</a></h1>
<h2>Status: Development (11/18/2025)</h2>
<h3>Source: <a href="https://github.com/walters-labs/open-encrypt-web">https://github.com/walters-labs/open-encrypt-web</a></h3>
<h3>API Docs: <a href="https://docs.open-encrypt.com">https://docs.open-encrypt.com</a></h3>
<nav>
<a href="inbox.php" class="nav-link">Home</a>
<a href="send_message.php" class="nav-link">Send Message</a>
<a href="view_messages.php" class="nav-link">View Messages</a>
<a href="key_management.php" class="nav-link">Key Management</a>
<form method="post" style="display:inline;">
<input type="submit" name="logout" value="Logout">
</form>
</nav>
</header>
<hr>
<h2>Key Management: <?php echo htmlspecialchars($username); ?></h2>
<!-- Key Generation Form -->
<form method="post" style="display:inline-block; padding:5px; border:1px solid #000; background:#fff;">
<input type="radio" id="ring_lwe" name="encryption_method" value="ring_lwe" checked>
<label for="ring_lwe">ring-LWE</label>
<input type="radio" id="module_lwe" name="encryption_method" value="module_lwe">
<label for="module_lwe">module-LWE</label>
<br><br>
<input type="submit" name="key_gen" value="Generate Keys" style="padding:5px;">
</form>
<form method="post" style="display:inline-block; padding:5px;">
<input type="submit" name="save_keys" value="Save Public Key to Server" style="padding:5px;">
</form>
<form method="post" style="display:inline-block; padding:5px;">
<input type="submit" name="view_keys" value="View Public Key" style="padding:5px;">
</form>
<script>
function copyKey(divId) {
const keyDiv = document.getElementById(divId);
if (!keyDiv) return;
let keyText = keyDiv.textContent.replace(/\r?\n/g, '');
navigator.clipboard.writeText(keyText).then(() => alert("Key copied!"));
}
function downloadKey(divId, filename) {
const keyDiv = document.getElementById(divId);
if (!keyDiv) return;
let text = keyDiv.textContent.replace(/\r?\n/g, '');
const blob = new Blob([text], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url; a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
</script>
<?php
// Generate Keys
if (isset($_POST['key_gen'], $_POST['encryption_method'])) {
$encryption_method = $_POST['encryption_method'];
$json_keys = generate_keys($encryption_method);
$secret_key = trim($json_keys['secret']);
$public_key = trim($json_keys['public']);
echo "<h3>Secret Key ($encryption_method)</h3>";
echo '<div id="secret_key_box" class="key-box">';
echo chunk_split($secret_key, 64, "\n");
echo '</div><br>';
echo '<button onclick="copyKey(\'secret_key_box\')">Copy</button> ';
echo '<button onclick="downloadKey(\'secret_key_box\', \'' . $username . '_secret_key.txt\')">Save</button>';
echo "<h3>Public Key ($encryption_method)</h3>";
echo '<div id="public_key_box" class="key-box">';
echo chunk_split($public_key, 64, "\n");
echo '</div><br>';
echo '<button onclick="copyKey(\'public_key_box\')">Copy</button> ';
echo '<button onclick="downloadKey(\'public_key_box\', \'' . $username . '_public_key.txt\')">Save</button>';
$_SESSION['public_key'] = $public_key;
$_SESSION['encryption_method'] = $encryption_method;
}
// Save Public Key
if (isset($_POST['save_keys'], $_SESSION['public_key'], $_SESSION['encryption_method'])) {
$public_key = $_SESSION['public_key'];
$encryption_method = $_SESSION['encryption_method'];
if (!valid_public_key($public_key, $encryption_method)) {
error_log("Error: Invalid public key for user " . htmlspecialchars($username));
echo "<p>Error: Invalid public key.</p>";
} else {
$existing = $db->fetchOne("SELECT username FROM public_keys WHERE username = ?", [$username], "s");
if ($existing === null) {
$success = $db->execute(
"INSERT INTO public_keys (username, public_key, method) VALUES (?, ?, ?)",
[$username, $public_key, $encryption_method],
"sss"
);
} else {
$success = $db->execute(
"UPDATE public_keys SET public_key = ?, method = ? WHERE username = ?",
[$public_key, $encryption_method, $username],
"sss"
);
}
if ($success) {
echo "<p>Public key saved successfully.</p>";
unset($_SESSION['public_key'], $_SESSION['encryption_method']);
} else {
error_log("Error: Failed to save public key for user " . htmlspecialchars($username));
echo "<p>Error saving public key.</p>";
}
}
}
// View Public Key
if (isset($_POST['view_keys'])) {
$public_key = fetch_public_key($db, $username);
$encryption_method = fetch_encryption_method($db, $username);
if ($public_key && $encryption_method && valid_public_key($public_key, $encryption_method)) {
echo "<h3>Public Key ($encryption_method)</h3>";
echo '<div id="public_key_box" class="key-box">';
echo chunk_split($public_key, 64, "\n");
echo '</div><br>';
// Add copy and download buttons
echo '<button onclick="copyKey(\'public_key_box\')">Copy</button> ';
echo '<button onclick="downloadKey(\'public_key_box\', \'' . $username .'_public_key.txt\')">Save</button>';
$_SESSION['public_key'] = $public_key;
$_SESSION['encryption_method'] = $encryption_method;
} else {
error_log("Error: No valid public key found for user " . htmlspecialchars($username));
echo "<p>No valid public key found.</p>";
}
}
?>
<footer class="footer">
<p>
© 2025 Open Encrypt. All rights reserved. |
<a href="/privacy">Privacy Policy</a> |
<a href="/terms">Terms of Service</a> |
<a href="/contact">Contact</a>
</p>
</footer>
</body>
</html>