1// SPDX-FileCopyrightText: 2025 Romain Maneschi <romain@gitroot.dev>
2//
3// SPDX-License-Identifier: EUPL-1.2
4
5package user
6
7import (
8 "bytes"
9 "context"
10 "crypto/ed25519"
11 "crypto/rand"
12 "encoding/pem"
13 "errors"
14 "fmt"
15 "io"
16 "os"
17 "path/filepath"
18
19 "github.com/42wim/sshsig"
20 "github.com/samber/oops"
21 "golang.org/x/crypto/ssh"
22)
23
24type Signer struct {
25 private ed25519.PrivateKey
26 signer ssh.Signer
27 public string
28}
29
30func generateIfNotExist(path, name string) (*Signer, error) {
31 privateSshKeyPath := filepath.Join(path, fmt.Sprintf("%s.priv", name))
32 publicSshKeyPath := filepath.Join(path, fmt.Sprintf("%s.pub", name))
33 errorHandler := oops.With("privateSshKeyPath", privateSshKeyPath)
34
35 err := os.MkdirAll(path, os.ModePerm)
36 if err != nil {
37 return nil, errorHandler.Wrapf(err, "can't MkdirAll")
38 }
39
40 var privateKey ed25519.PrivateKey
41 if _, err := os.Stat(privateSshKeyPath); err == nil {
42 content, err := os.ReadFile(privateSshKeyPath)
43 if err != nil {
44 return nil, errorHandler.Wrapf(err, "can't read private key")
45 }
46 privateKeyRaw, err := ssh.ParseRawPrivateKey(content)
47 if err != nil {
48 return nil, errorHandler.Wrapf(err, "can't parse private key")
49 }
50 pk, ok := privateKeyRaw.(*ed25519.PrivateKey)
51 if !ok {
52 return nil, errorHandler.Wrapf(err, "private key is not ed25519")
53 }
54 privateKey = *pk
55 } else if errors.Is(err, os.ErrNotExist) {
56 _, privateKey, err = ed25519.GenerateKey(rand.Reader)
57 if err != nil {
58 return nil, errorHandler.Wrapf(err, "can't generate private key")
59 }
60
61 p, err := ssh.MarshalPrivateKey(privateKey, "GitRoot master ssh key")
62 if err != nil {
63 return nil, errorHandler.Wrapf(err, "can't marshal private key")
64 }
65 fpriv, err := os.Create(privateSshKeyPath)
66 if err != nil {
67 return nil, errorHandler.Wrapf(err, "can't create file private key")
68 }
69 err = pem.Encode(fpriv, p)
70 if err != nil {
71 return nil, errorHandler.Wrapf(err, "can't encode pem private key")
72 }
73
74 sshSigner, err := ssh.NewSignerFromSigner(privateKey)
75 if err != nil {
76 return nil, errorHandler.Wrapf(err, "can't create signer from private key")
77 }
78
79 sshString := bytes.TrimSuffix(ssh.MarshalAuthorizedKey(sshSigner.PublicKey()), []byte("\n"))
80 fpub, err := os.Create(publicSshKeyPath)
81 if err != nil {
82 return nil, errorHandler.Wrapf(err, "can't create file public key")
83 }
84 _, err = fpub.Write(sshString)
85 if err != nil {
86 return nil, errorHandler.Wrapf(err, "can't write file public key")
87 }
88 err = fpub.Close()
89 if err != nil {
90 return nil, errorHandler.Wrapf(err, "can't close file public key")
91 }
92 } else {
93 return nil, errorHandler.Wrapf(err, "can't stat file")
94 }
95 sshSigner, _ := ssh.NewSignerFromSigner(privateKey)
96 return &Signer{signer: sshSigner, private: privateKey, public: string(bytes.TrimSuffix(ssh.MarshalAuthorizedKey(sshSigner.PublicKey()), []byte("\n")))}, nil
97}
98
99func (s *Signer) PublicKey() string {
100 return s.public
101}
102
103func (s *Signer) Signer() ssh.Signer {
104 return s.signer
105}
106
107func (s *Signer) Sign(ctx context.Context, message io.Reader) ([]byte, error) {
108 p, err := ssh.MarshalPrivateKey(s.private, "GitRoot master ssh key")
109 if err != nil {
110 return nil, oops.Wrapf(err, "can't marshal private key")
111 }
112 return sshsig.Sign(pem.EncodeToMemory(p), message, "git")
113}