// Package keys holds the importer's RSA key pair, which the catalog's team seals IMVU account passwords for // (RSA-OAEP with SHA-256, in their browser): t5de-hub's docs/imports.md, "IMVU accounts". package keys import ( "crypto/rand" "crypto/rsa" "crypto/sha256" "crypto/x509" "encoding/base64" "encoding/hex" "encoding/json" "encoding/pem" "errors" "fmt" "os" "path/filepath" ) // Bits is the size of keys Load makes. const Bits = 3072 // FileName is the private key's file in the data folder. const FileName = "importer-key.pem" // Key is the importer's key pair. type Key struct { private *rsa.PrivateKey // PublicKey is the public key as base64 SPKI (DER), as the hub takes it. PublicKey string // Fingerprint is sha256 of the SPKI, hex, as the console shows it. Fingerprint string } // Load reads the key pair from dir, or makes one (and says so) if there's none. func Load(dir string) (k *Key, made bool, err error) { path := filepath.Join(dir, FileName) data, err := os.ReadFile(path) if errors.Is(err, os.ErrNotExist) { priv, err := rsa.GenerateKey(rand.Reader, Bits) if err != nil { return nil, false, err } der, err := x509.MarshalPKCS8PrivateKey(priv) if err != nil { return nil, false, err } if err := os.MkdirAll(dir, 0o700); err != nil { return nil, false, err } tmp := path + ".tmp" if err := os.WriteFile(tmp, pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: der}), 0o600); err != nil { return nil, false, err } if err := os.Rename(tmp, path); err != nil { return nil, false, err } k, err := fromPrivate(priv) return k, true, err } if err != nil { return nil, false, err } block, _ := pem.Decode(data) if block == nil || block.Type != "PRIVATE KEY" { return nil, false, fmt.Errorf("%s isn't a PEM private key", path) } parsed, err := x509.ParsePKCS8PrivateKey(block.Bytes) if err != nil { return nil, false, fmt.Errorf("%s: %w", path, err) } priv, ok := parsed.(*rsa.PrivateKey) if !ok { return nil, false, fmt.Errorf("%s isn't an RSA key", path) } k, err = fromPrivate(priv) return k, false, err } func fromPrivate(priv *rsa.PrivateKey) (*Key, error) { spki, err := x509.MarshalPKIXPublicKey(&priv.PublicKey) if err != nil { return nil, err } sum := sha256.Sum256(spki) return &Key{private: priv, PublicKey: base64.StdEncoding.EncodeToString(spki), Fingerprint: hex.EncodeToString(sum[:])}, nil } // Open opens a sealed password: base64 of RSA-OAEP (SHA-256) over `{"imvuId": …, "password": …}`. It must be for // imvuID, so a sealed password can't be moved to another account. func (k *Key) Open(sealed string, imvuID int64) (string, error) { ct, err := base64.StdEncoding.DecodeString(sealed) if err != nil { return "", errors.New("the sealed password isn't base64") } plain, err := rsa.DecryptOAEP(sha256.New(), nil, k.private, ct, nil) if err != nil { return "", errors.New("the sealed password can't be opened with this importer's key") } var v struct { IMVUID int64 `json:"imvuId"` Password string `json:"password"` } if err := json.Unmarshal(plain, &v); err != nil { return "", errors.New("the sealed password isn't what the console seals") } if v.IMVUID != imvuID { return "", errors.New("the sealed password is for another IMVU account") } if v.Password == "" { return "", errors.New("the sealed password is empty") } return v.Password, nil }