Skip to content

Commit 2f67938

Browse files
authored
feat (Private Key Editing): Added private key format normalization (#1080)
* feat (Private Key Editing): Added private key format normalization Added the _normalizePrivateKey method to normalize private key formats: - Removes whitespace characters from Base64 content - Ensures the standard format of 64 characters per line - Ensures that the private key ends with a newline character * fix(private_key): Fixes PEM private key formatting issues while preserving metadata headers Properly handles metadata headers (such as Proc-Type and DEK-Info) in encrypted PEM keys and preserves these headers when cleaning up Base64 content. Additionally, optimizes the logic for removing whitespace characters and improves performance by using precompiled regular expressions. * refactor(ui): Remove unused ctx parameters and optimize the selection window caching logic - Remove unused egui::Context parameters from functions related to settings_page - Add a check for the length of items in the selection window cache to improve cache validity - Simplify the cache data structure and remove unnecessary online data validation logic * fix(private_key): Fixed an issue with matching header and footer tags in PEM-format private keys Added validation for consistency of header and footer tags in PEM-format private keys to ensure that the content following “BEGIN” and “END” is identical
1 parent 1bea565 commit 2f67938

1 file changed

Lines changed: 61 additions & 1 deletion

File tree

lib/view/page/private_key/edit.dart

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import 'package:server_box/data/provider/private_key.dart';
1212
import 'package:server_box/data/res/misc.dart';
1313

1414
const _format = 'text/plain';
15+
final _whitespaceRegex = RegExp(r'\s+');
16+
final _pemBeginRegex = RegExp(r'^-----BEGIN ([A-Z0-9 ]+)-----$');
17+
final _pemEndRegex = RegExp(r'^-----END ([A-Z0-9 ]+)-----$');
1518

1619
final class PrivateKeyEditPageArgs {
1720
final PrivateKeyInfo? pki;
@@ -116,6 +119,63 @@ class _PrivateKeyEditPageState extends ConsumerState<PrivateKeyEditPage> {
116119
return value.replaceAll('\r\n', '\n').replaceAll('\r', '\n');
117120
}
118121

122+
/// Normalizes the private key format:
123+
/// - Removes whitespace from Base64 content (spaces, tabs, etc.)
124+
/// - Ensures the key ends with a newline
125+
String _normalizePrivateKey(String key) {
126+
final lines = key.split('\n');
127+
// Guard: need at least header + body + footer (3 lines) for valid PEM
128+
if (lines.length < 3) return key;
129+
130+
final header = lines.first;
131+
final footer = lines.last;
132+
133+
// Validate PEM boundaries before mutating input
134+
final headerMatch = _pemBeginRegex.firstMatch(header);
135+
final footerMatch = _pemEndRegex.firstMatch(footer);
136+
if (headerMatch == null || footerMatch == null) {
137+
return key;
138+
}
139+
140+
// Ensure header and footer labels match
141+
final headerLabel = headerMatch.group(1);
142+
final footerLabel = footerMatch.group(1);
143+
if (headerLabel != footerLabel) {
144+
return key;
145+
}
146+
147+
// Extract Base64 content (everything between header and footer)
148+
final bodyLines = lines.sublist(1, lines.length - 1);
149+
150+
// Check for RFC 1421 metadata headers (e.g., Proc-Type, DEK-Info)
151+
// These appear in encrypted PEM keys and must be preserved
152+
final hasMetadataHeaders = bodyLines.any(
153+
(line) => line.contains(':') && !line.startsWith('-----'),
154+
);
155+
156+
if (hasMetadataHeaders) {
157+
// For encrypted keys, preserve structure and just ensure trailing newline
158+
if (!key.endsWith('\n')) {
159+
return '$key\n';
160+
}
161+
return key;
162+
}
163+
164+
// Remove all whitespace from Base64 content
165+
final cleanBody = bodyLines.join('').replaceAll(_whitespaceRegex, '');
166+
167+
// Rebuild the key with standard formatting (64 chars per line)
168+
final buffer = StringBuffer();
169+
buffer.writeln(header);
170+
for (var i = 0; i < cleanBody.length; i += 64) {
171+
final end = (i + 64 < cleanBody.length) ? i + 64 : cleanBody.length;
172+
buffer.writeln(cleanBody.substring(i, end));
173+
}
174+
buffer.writeln(footer);
175+
176+
return buffer.toString();
177+
}
178+
119179
Widget _buildFAB() {
120180
return FloatingActionButton(tooltip: l10n.save, onPressed: _onTapSave, child: const Icon(Icons.save));
121181
}
@@ -186,7 +246,7 @@ class _PrivateKeyEditPageState extends ConsumerState<PrivateKeyEditPage> {
186246

187247
void _onTapSave() async {
188248
final name = _nameController.text;
189-
final key = _standardizeLineSeparators(_keyController.text.trim());
249+
final key = _normalizePrivateKey(_standardizeLineSeparators(_keyController.text.trim()));
190250
final pwd = _pwdController.text;
191251
if (name.isEmpty || key.isEmpty) {
192252
context.showSnackBar(libL10n.empty);

0 commit comments

Comments
 (0)