generateKeyPair creates a EC keypair (P256) and stores them in the homedir. returns the generated public key from the successful keypair generation
(fullName string)
| 144 | // generateKeyPair creates a EC keypair (P256) and stores them in the homedir. |
| 145 | // returns the generated public key from the successful keypair generation |
| 146 | func generateKeyPair(fullName string) ([]byte, error) { |
| 147 | pubKeyName := fullName + ".pub" |
| 148 | |
| 149 | exist, err := config.FileExists(pubKeyName) |
| 150 | if err != nil { |
| 151 | return nil, err |
| 152 | } |
| 153 | if exist { |
| 154 | return os.ReadFile(pubKeyName) |
| 155 | } |
| 156 | |
| 157 | key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 158 | if err != nil { |
| 159 | return nil, err |
| 160 | } |
| 161 | parsed, err := x509.MarshalECPrivateKey(key) |
| 162 | if err != nil { |
| 163 | return nil, err |
| 164 | } |
| 165 | |
| 166 | if err := writeKey(fullName, pem.EncodeToMemory(&pem.Block{ |
| 167 | Type: "EC PRIVATE KEY", |
| 168 | Bytes: parsed, |
| 169 | })); err != nil { |
| 170 | return nil, err |
| 171 | } |
| 172 | |
| 173 | pub, err := gossh.NewPublicKey(&key.PublicKey) |
| 174 | if err != nil { |
| 175 | return nil, err |
| 176 | } |
| 177 | data := gossh.MarshalAuthorizedKey(pub) |
| 178 | |
| 179 | if err := writeKey(pubKeyName, data); err != nil { |
| 180 | return nil, err |
| 181 | } |
| 182 | |
| 183 | return data, nil |
| 184 | } |
| 185 | |
| 186 | // writeKey will write a key to disk in DER format (it's a standard pem key) |
| 187 | func writeKey(filename string, data []byte) error { |
no test coverage detected