- model.SignProfile:platform(windows/ios)、证书/口令/描述文件加密落盘,库里只留元数据 - 主密钥由 app.jwt_secret 派生,目录 0700、文件 0600;响应永不回证书内容与口令 - /dev/sign-profiles 上传/列表/详情/删除;iOS 强制要求 .mobileprovision - ProtectTask 增加 sign_profile_id(上传时可指定,校验归属),为重签做准备 - 单测:加解密往返/换钥失败/篡改检测/文件权限/平台归一化
132 lines
3.2 KiB
Go
132 lines
3.2 KiB
Go
package protect
|
||
|
||
// 证书资产:p12/pfx/mobileprovision 与口令一律 AES-256-GCM 加密落盘,
|
||
// 主密钥由 app.jwt_secret 派生(配置里已有,不需要额外密钥管理)。
|
||
// 数据库只存路径与元数据,私钥与口令永不出现在任何响应里。
|
||
|
||
import (
|
||
"crypto/aes"
|
||
"crypto/cipher"
|
||
"crypto/rand"
|
||
"crypto/sha256"
|
||
"encoding/hex"
|
||
"fmt"
|
||
"io"
|
||
"os"
|
||
"path/filepath"
|
||
"strings"
|
||
|
||
"verification-platform-backend/internal/config"
|
||
)
|
||
|
||
// masterKey 从 jwt_secret 派生 32 字节主密钥
|
||
func masterKey() ([]byte, error) {
|
||
secret := config.GetString("app.jwt_secret")
|
||
if secret == "" || secret == "your-secret-key" {
|
||
return nil, fmt.Errorf("app.jwt_secret 未配置,无法加密证书资产")
|
||
}
|
||
sum := sha256.Sum256([]byte("vfy-protect-signkey|" + secret))
|
||
return sum[:], nil
|
||
}
|
||
|
||
// EncryptBytes 返回 nonce||ciphertext
|
||
func EncryptBytes(plain []byte) ([]byte, error) {
|
||
key, err := masterKey()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return encryptWithKey(key, plain)
|
||
}
|
||
|
||
// encryptWithKey 用指定密钥加密(便于单测,不读配置)
|
||
func encryptWithKey(key, plain []byte) ([]byte, error) {
|
||
block, err := aes.NewCipher(key)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
gcm, err := cipher.NewGCM(block)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
nonce := make([]byte, gcm.NonceSize())
|
||
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
|
||
return nil, err
|
||
}
|
||
return gcm.Seal(nonce, nonce, plain, nil), nil
|
||
}
|
||
|
||
// DecryptBytes 解 nonce||ciphertext
|
||
func DecryptBytes(enc []byte) ([]byte, error) {
|
||
key, err := masterKey()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return decryptWithKey(key, enc)
|
||
}
|
||
|
||
// decryptWithKey 用指定密钥解密
|
||
func decryptWithKey(key, enc []byte) ([]byte, error) {
|
||
block, err := aes.NewCipher(key)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
gcm, err := cipher.NewGCM(block)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if len(enc) < gcm.NonceSize() {
|
||
return nil, fmt.Errorf("密文损坏")
|
||
}
|
||
nonce, body := enc[:gcm.NonceSize()], enc[gcm.NonceSize():]
|
||
return gcm.Open(nil, nonce, body, nil)
|
||
}
|
||
|
||
// WriteEncrypted 加密写文件(0600)
|
||
func WriteEncrypted(path string, plain []byte) error {
|
||
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
|
||
return err
|
||
}
|
||
enc, err := EncryptBytes(plain)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
return os.WriteFile(path, enc, 0o600)
|
||
}
|
||
|
||
// ReadEncrypted 读并解密
|
||
func ReadEncrypted(path string) ([]byte, error) {
|
||
enc, err := os.ReadFile(path)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return DecryptBytes(enc)
|
||
}
|
||
|
||
// ProfileDir 单个证书资产的目录
|
||
func ProfileDir(id uint) string {
|
||
return filepath.Join(Default().Dir, "keys", fmt.Sprintf("profile_%d", id))
|
||
}
|
||
|
||
// Fingerprint 只用于日志:不泄露内容,便于排查是不是同一份证书
|
||
func Fingerprint(b []byte) string {
|
||
sum := sha256.Sum256(b)
|
||
return hex.EncodeToString(sum[:6])
|
||
}
|
||
|
||
// 平台常量
|
||
const (
|
||
PlatformWindows = "windows"
|
||
PlatformIOS = "ios"
|
||
)
|
||
|
||
// NormalizePlatform 归一化平台名
|
||
func NormalizePlatform(p string) (string, error) {
|
||
switch strings.ToLower(strings.TrimSpace(p)) {
|
||
case "windows", "win", "pe":
|
||
return PlatformWindows, nil
|
||
case "ios", "ipa":
|
||
return PlatformIOS, nil
|
||
}
|
||
return "", fmt.Errorf("platform 只能是 windows 或 ios")
|
||
}
|