|
| 1 | +/* |
| 2 | +Copyright 2021 RadonDB. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package syncer |
| 18 | + |
| 19 | +import ( |
| 20 | + "crypto/ecdsa" |
| 21 | + "crypto/elliptic" |
| 22 | + "crypto/rand" |
| 23 | + "crypto/x509" |
| 24 | + "encoding/pem" |
| 25 | + "log" |
| 26 | + |
| 27 | + "golang.org/x/crypto/ssh" |
| 28 | +) |
| 29 | + |
| 30 | +// ssh -o UserKnownHostsFile=/dev/null |
| 31 | +func GenSSHKey() (pubkey, privekey []byte, err error) { |
| 32 | + |
| 33 | + bitSize := 4096 |
| 34 | + |
| 35 | + privateKey, err := generatePrivateKey(bitSize) |
| 36 | + if err != nil { |
| 37 | + return nil, nil, err |
| 38 | + } |
| 39 | + |
| 40 | + publicKeyBytes, err := generatePublicKey(&privateKey.PublicKey) |
| 41 | + if err != nil { |
| 42 | + return nil, nil, err |
| 43 | + } |
| 44 | + |
| 45 | + privateKeyBytes := encodePrivateKeyToPEM(privateKey) |
| 46 | + |
| 47 | + return publicKeyBytes, privateKeyBytes, err |
| 48 | + |
| 49 | +} |
| 50 | + |
| 51 | +// generatePrivateKey creates a RSA Private Key of specified byte size |
| 52 | +func generatePrivateKey(bitSize int) (*ecdsa.PrivateKey, error) { |
| 53 | + // Private Key generation |
| 54 | + privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 55 | + if err != nil { |
| 56 | + return nil, err |
| 57 | + } |
| 58 | + |
| 59 | + // Validate Private Key |
| 60 | + // err = privateKey.Validate() |
| 61 | + // if err != nil { |
| 62 | + // return nil, err |
| 63 | + // } |
| 64 | + |
| 65 | + log.Println("Private Key generated") |
| 66 | + return privateKey, nil |
| 67 | +} |
| 68 | + |
| 69 | +// encodePrivateKeyToPEM encodes Private Key from RSA to PEM format |
| 70 | +func encodePrivateKeyToPEM(privateKey *ecdsa.PrivateKey) []byte { |
| 71 | + // Get ASN.1 DER format |
| 72 | + privDER, err := x509.MarshalECPrivateKey(privateKey) |
| 73 | + if err != nil { |
| 74 | + panic(err) |
| 75 | + } |
| 76 | + // pem.Block |
| 77 | + privBlock := pem.Block{ |
| 78 | + Type: "EC PRIVATE KEY", |
| 79 | + Headers: nil, |
| 80 | + Bytes: privDER, |
| 81 | + } |
| 82 | + |
| 83 | + // Private key in PEM format |
| 84 | + privatePEM := pem.EncodeToMemory(&privBlock) |
| 85 | + |
| 86 | + return privatePEM |
| 87 | +} |
| 88 | + |
| 89 | +// generatePublicKey take a rsa.PublicKey and return bytes suitable for writing to .pub file |
| 90 | +// returns in the format "ssh-rsa ..." |
| 91 | +func generatePublicKey(privatekey *ecdsa.PublicKey) ([]byte, error) { |
| 92 | + publicKey, err := ssh.NewPublicKey(privatekey) |
| 93 | + if err != nil { |
| 94 | + return nil, err |
| 95 | + } |
| 96 | + |
| 97 | + pubKeyBytes := ssh.MarshalAuthorizedKey(publicKey) |
| 98 | + |
| 99 | + log.Println("Public key generated") |
| 100 | + return pubKeyBytes, nil |
| 101 | +} |
0 commit comments