|
@@ -271,37 +271,14 @@ func (c *APIClient) UpdateWiFiCredentials(ssid, psk string) error {
|
|
|
// Returns OpenSSH public key format
|
|
// Returns OpenSSH public key format
|
|
|
func GenerateOrLoadSSHKey(keyPath string) (string, error) {
|
|
func GenerateOrLoadSSHKey(keyPath string) (string, error) {
|
|
|
// Check if key already exists
|
|
// Check if key already exists
|
|
|
|
|
+ pubKeyPath := keyPath + ".pub"
|
|
|
if _, err := os.Stat(keyPath); err == nil {
|
|
if _, err := os.Stat(keyPath); err == nil {
|
|
|
- // Load existing key
|
|
|
|
|
- privKeyBytes, err := os.ReadFile(keyPath)
|
|
|
|
|
|
|
+ // Key exists - read public key file
|
|
|
|
|
+ pubKeyBytes, err := os.ReadFile(pubKeyPath)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- return "", fmt.Errorf("failed to read existing key: %w", err)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- block, _ := pem.Decode(privKeyBytes)
|
|
|
|
|
- if block == nil {
|
|
|
|
|
- return "", fmt.Errorf("failed to decode PEM block")
|
|
|
|
|
|
|
+ return "", fmt.Errorf("failed to read public key: %w", err)
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- // Parse ED25519 private key
|
|
|
|
|
- privKey, err := ssh.ParseRawPrivateKey(privKeyBytes)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return "", fmt.Errorf("failed to parse private key: %w", err)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- ed25519Key, ok := privKey.(ed25519.PrivateKey)
|
|
|
|
|
- if !ok {
|
|
|
|
|
- return "", fmt.Errorf("key is not ED25519")
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // Extract public key
|
|
|
|
|
- pubKey := ed25519Key.Public().(ed25519.PublicKey)
|
|
|
|
|
- sshPubKey, err := ssh.NewPublicKey(pubKey)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return "", fmt.Errorf("failed to create SSH public key: %w", err)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return string(ssh.MarshalAuthorizedKey(sshPubKey)), nil
|
|
|
|
|
|
|
+ return string(pubKeyBytes), nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Generate new ED25519 key pair
|
|
// Generate new ED25519 key pair
|
|
@@ -336,7 +313,6 @@ func GenerateOrLoadSSHKey(keyPath string) (string, error) {
|
|
|
pubKeyStr := string(ssh.MarshalAuthorizedKey(sshPubKey))
|
|
pubKeyStr := string(ssh.MarshalAuthorizedKey(sshPubKey))
|
|
|
|
|
|
|
|
// Save public key
|
|
// Save public key
|
|
|
- pubKeyPath := keyPath + ".pub"
|
|
|
|
|
if err := os.WriteFile(pubKeyPath, []byte(pubKeyStr), 0644); err != nil {
|
|
if err := os.WriteFile(pubKeyPath, []byte(pubKeyStr), 0644); err != nil {
|
|
|
return "", fmt.Errorf("failed to write public key: %w", err)
|
|
return "", fmt.Errorf("failed to write public key: %w", err)
|
|
|
}
|
|
}
|