Update bootstrap: 2026-01-21 01:17:52
This commit is contained in:
43
nbcrypt
43
nbcrypt
@@ -3,6 +3,10 @@ set -euo pipefail
|
||||
|
||||
# nbcrypt - SSH Agent ベースの暗号化/復号ツール
|
||||
# Ed25519 署名で決定的な鍵導出を行う
|
||||
#
|
||||
# 環境変数: NB_SIGN_VIA_SSH, NB_SSH_PORT
|
||||
# DevContainer 等で Agent が使えないとき、NB_SIGN_VIA_SSH=user@host を指定すると
|
||||
# ホストの ~/.ssh/id_ed25519 で署名してもらう。NB_SSH_PORT でポート指定(未設定時 22)。
|
||||
|
||||
SCRIPT_NAME="$(basename "$0")"
|
||||
KEY_SEED_MESSAGE="nbase2-secret-key-seed-v1"
|
||||
@@ -62,7 +66,8 @@ Text commands support stdin:
|
||||
echo "encrypted" | $SCRIPT_NAME dec -
|
||||
|
||||
Requirements:
|
||||
- SSH Agent with id_ed25519 key, or ~/.ssh/id_ed25519 file, or BWS_ACCESS_TOKEN
|
||||
- SSH Agent with id_ed25519 key, or ~/.ssh/id_ed25519 file, or BWS_ACCESS_TOKEN,
|
||||
or NB_SIGN_VIA_SSH (DevContainer: sign via host's ~/.ssh/id_ed25519; NB_SSH_PORT for port)
|
||||
- ssh-keygen and openssl commands must be available
|
||||
|
||||
Examples:
|
||||
@@ -314,7 +319,7 @@ install_bws() {
|
||||
|
||||
derive_key() {
|
||||
# Ed25519 署名から暗号鍵を導出(同じメッセージなら同じ署名で決定的)
|
||||
# 優先順位: 1. SSH Agent, 2. 秘密鍵ファイル, 3. BWS (check_ssh_agent で処理)
|
||||
# 優先順位: 1. SSH Agent, 2. 秘密鍵ファイル, 3. NB_SIGN_VIA_SSH, 4. BWS (check_ssh_agent で処理)
|
||||
|
||||
local temp_message=$(mktemp)
|
||||
local temp_sig=$(mktemp)
|
||||
@@ -327,7 +332,8 @@ derive_key() {
|
||||
echo -n "$KEY_SEED_MESSAGE" > "$temp_message"
|
||||
|
||||
# 1. SSH Agent から Ed25519 の公開鍵を取得を試みる
|
||||
if ssh-add -L 2>/dev/null | grep "ssh-ed25519" | head -1 | awk '{print $1 " " $2}' > "$temp_pubkey" 2>/dev/null && [ -s "$temp_pubkey" ]; then
|
||||
ssh-add -L 2>/dev/null | grep "ssh-ed25519" | head -1 | awk '{print $1 " " $2}' > "$temp_pubkey" 2>/dev/null
|
||||
if [ -s "$temp_pubkey" ]; then
|
||||
# Agent から取得できた場合
|
||||
echo "key1 $(cat "$temp_pubkey")" > "$temp_allowed"
|
||||
|
||||
@@ -339,7 +345,7 @@ derive_key() {
|
||||
fi
|
||||
fi
|
||||
|
||||
# 2. SSH Agent がない場合、秘密鍵ファイルから直接署名を試みる
|
||||
# 2. 秘密鍵ファイルから直接署名を試みる
|
||||
local key_file="${NB_KEY_FILE:-${HOME}/.ssh/id_ed25519}"
|
||||
if [ -f "$key_file" ]; then
|
||||
# 公開鍵を抽出
|
||||
@@ -355,8 +361,17 @@ derive_key() {
|
||||
fi
|
||||
fi
|
||||
|
||||
# 3. どちらも失敗した場合、エラー(BWS は check_ssh_agent で処理される)
|
||||
error "Could not derive key from SSH Agent or private key file. Please ensure SSH Agent is running with Ed25519 key, or set NB_KEY_FILE to a valid private key path."
|
||||
# 3. ホスト SSH: NB_SIGN_VIA_SSH / NB_SSH_PORT(未設定時 nodoka@host.docker.internal:60516)で ~/.ssh/id_ed25519 に署名してもらう
|
||||
if ssh -o BatchMode=yes -o ConnectTimeout=10 -o StrictHostKeyChecking=accept-new -p "${NB_SSH_PORT:-60516}" "${NB_SIGN_VIA_SSH:-nodoka@host.docker.internal}" 'echo -n "nbase2-secret-key-seed-v1" | ssh-keygen -Y sign -f ~/.ssh/id_ed25519 -n "nbase2"' > "$temp_sig" 2>/dev/null; then
|
||||
_key=$(cat "$temp_sig" | base64 -d 2>/dev/null | sha256sum | head -c 64)
|
||||
if [[ "${_key:-}" =~ ^[0-9a-f]{64}$ ]]; then
|
||||
echo -n "$_key"
|
||||
return 0
|
||||
fi
|
||||
fi
|
||||
|
||||
# 4. どれも失敗した場合、エラー(BWS は check_ssh_agent で処理される)
|
||||
error "Could not derive key from SSH Agent, private key file, or host. Ensure SSH Agent has Ed25519, or set NB_KEY_FILE. Host: NB_SIGN_VIA_SSH/NB_SSH_PORT (default nodoka@host.docker.internal:60516)."
|
||||
}
|
||||
|
||||
encrypt_file() {
|
||||
@@ -367,9 +382,9 @@ encrypt_file() {
|
||||
error "Input file not found: $input"
|
||||
fi
|
||||
|
||||
# 秘密鍵ファイルがある場合は check_ssh_agent をスキップ
|
||||
# 秘密鍵ファイルまたは NB_SIGN_VIA_SSH がある場合は check_ssh_agent をスキップ
|
||||
local key_file="${NB_KEY_FILE:-${HOME}/.ssh/id_ed25519}"
|
||||
if [ ! -f "$key_file" ]; then
|
||||
if [ ! -f "$key_file" ] && [ -z "${NB_SIGN_VIA_SSH:-}" ]; then
|
||||
check_ssh_agent
|
||||
fi
|
||||
|
||||
@@ -422,9 +437,9 @@ decrypt_file() {
|
||||
error "Ed25519 key not found in SSH Agent after BWS setup"
|
||||
fi
|
||||
else
|
||||
# 秘密鍵ファイルがある場合は check_ssh_agent をスキップ
|
||||
# 秘密鍵ファイルまたは NB_SIGN_VIA_SSH がある場合は check_ssh_agent をスキップ
|
||||
local key_file="${NB_KEY_FILE:-${HOME}/.ssh/id_ed25519}"
|
||||
if [ ! -f "$key_file" ]; then
|
||||
if [ ! -f "$key_file" ] && [ -z "${NB_SIGN_VIA_SSH:-}" ]; then
|
||||
check_ssh_agent
|
||||
fi
|
||||
fi
|
||||
@@ -449,9 +464,9 @@ decrypt_file() {
|
||||
encrypt_text() {
|
||||
local input_text="$1"
|
||||
|
||||
# 秘密鍵ファイルがある場合は check_ssh_agent をスキップ
|
||||
# 秘密鍵ファイルまたは NB_SIGN_VIA_SSH がある場合は check_ssh_agent をスキップ
|
||||
local key_file="${NB_KEY_FILE:-${HOME}/.ssh/id_ed25519}"
|
||||
if [ ! -f "$key_file" ]; then
|
||||
if [ ! -f "$key_file" ] && [ -z "${NB_SIGN_VIA_SSH:-}" ]; then
|
||||
check_ssh_agent
|
||||
fi
|
||||
|
||||
@@ -478,9 +493,9 @@ encrypt_text() {
|
||||
decrypt_text() {
|
||||
local input_text="$1"
|
||||
|
||||
# 秘密鍵ファイルがある場合は check_ssh_agent をスキップ
|
||||
# 秘密鍵ファイルまたは NB_SIGN_VIA_SSH がある場合は check_ssh_agent をスキップ
|
||||
local key_file="${NB_KEY_FILE:-${HOME}/.ssh/id_ed25519}"
|
||||
if [ ! -f "$key_file" ]; then
|
||||
if [ ! -f "$key_file" ] && [ -z "${NB_SIGN_VIA_SSH:-}" ]; then
|
||||
check_ssh_agent
|
||||
fi
|
||||
|
||||
|
||||
Reference in New Issue
Block a user