Files
securebit-chat/example_master_key_usage.js
lockbitchat 6bed472e09 feat(security): implement core crypto and logging hardening
- removed all logging of raw IV, JWK, session tokens, verification codes
  → logs now only include error codes, timestamps and short non-reversible SHA-256 hashes (first 4 bytes)
- replaced global master key storage with PBKDF2-derived non-extractable AES key
  → master key lives only in session memory and is cleared on timeout/focus-out
- added password-based derivation (PBKDF2) for master key initialization
- migrated persistent key storage to AES-GCM wrapped ciphertext in IndexedDB
  → JWK export → encrypt with session master key → store ciphertext + IV + metadata
  → unwrap + import as non-extractable on restore
- removed busy-wait loops and direct `window.gc()` calls
  → replaced with async non-blocking cleanup via setTimeout/Promise/WebWorker
2025-10-02 03:25:38 -04:00

62 lines
3.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Пример использования новой системы мастер-ключей
import { EnhancedSecureWebRTCManager } from './src/network/EnhancedSecureWebRTCManager.js';
// Создание менеджера WebRTC
const webrtcManager = new EnhancedSecureWebRTCManager(
(message) => console.log('Received:', message),
(status) => console.log('Status:', status),
(keyData) => console.log('Key exchange:', keyData),
(verificationData) => console.log('Verification required:', verificationData)
);
// Настройка callback для запроса пароля
webrtcManager.setMasterKeyPasswordCallback((isRetry, callback) => {
// В реальном приложении здесь должен быть UI для ввода пароля
const message = isRetry ?
'Неверный пароль. Введите мастер-пароль повторно:' :
'Введите мастер-пароль для разблокировки безопасного хранилища:';
// Пример с prompt (в реальном приложении используйте модальное окно)
const password = prompt(message);
callback(password);
});
// Настройка callback для истечения сессии
webrtcManager.setMasterKeySessionExpiredCallback((reason) => {
console.warn(`Сессия мастер-ключа истекла: ${reason}`);
// Уведомить пользователя
if (reason === 'inactivity') {
alert('Сессия заблокирована из-за неактивности. Потребуется повторный ввод пароля.');
} else if (reason === 'timeout') {
alert('Сессия истекла по таймауту. Потребуется повторный ввод пароля.');
}
});
// Проверка статуса мастер-ключа
console.log('Мастер-ключ разблокирован:', webrtcManager.isMasterKeyUnlocked());
console.log('Статус сессии:', webrtcManager.getMasterKeySessionStatus());
// Ручная блокировка мастер-ключа
// webrtcManager.lockMasterKey();
// Пример использования в реальном приложении:
async function initializeSecureConnection() {
try {
// При первом обращении к зашифрованным ключам будет запрошен пароль
const offer = await webrtcManager.createSecureOffer();
console.log('Secure offer created:', offer);
// Мастер-ключ теперь разблокирован и будет автоматически заблокирован:
// - через 15 минут бездействия
// - через 5 минут после потери фокуса окна
// - при ручном вызове lockMasterKey()
} catch (error) {
console.error('Failed to create secure offer:', error);
}
}
// Запуск примера
initializeSecureConnection();