-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from AYM1607/better-keys-envs
Better server config
- Loading branch information
Showing
7 changed files
with
123 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package server | ||
|
||
import ( | ||
"crypto/ecdh" | ||
"errors" | ||
"os" | ||
|
||
"github.com/AYM1607/ccclip/pkg/crypto" | ||
) | ||
|
||
const ( | ||
privateKeyEnv = "CCCLIP_PRIVATE_KEY" | ||
publicKeyEnv = "CCCLIP_PUBLIC_KEY" | ||
privateKeyPathEnv = "CCCLIP_PRIVATE_KEY_PATH" | ||
publicKeyPathEnv = "CCCLIP_PUBLIC_KEY_PATH" | ||
) | ||
|
||
func loadKeys() (*ecdh.PrivateKey, *ecdh.PublicKey, error) { | ||
// Prioritize explicit keys over files. | ||
var pvk *ecdh.PrivateKey | ||
var pbk *ecdh.PublicKey | ||
|
||
if b64PrivateKey := os.Getenv(privateKeyEnv); b64PrivateKey != "" { | ||
pvk = crypto.PrivateKeyFromB64([]byte(b64PrivateKey)) | ||
} else if privateKeyPath := os.Getenv(privateKeyPathEnv); privateKeyPath != "" { | ||
pvk = crypto.LoadPrivateKeyFromFile(privateKeyPath) | ||
} else { | ||
return nil, nil, errors.New("no private key was found") | ||
} | ||
|
||
if b64PublicKey := os.Getenv(publicKeyEnv); b64PublicKey != "" { | ||
pbk = crypto.PublicKeyFromB64([]byte(b64PublicKey)) | ||
} else if publicKeyPath := os.Getenv(publicKeyPathEnv); publicKeyPath != "" { | ||
pbk = crypto.LoadPublicKeyFromFile(publicKeyPath) | ||
} else { | ||
return nil, nil, errors.New("to public key was found") | ||
} | ||
|
||
return pvk, pbk, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package crypto | ||
|
||
import ( | ||
"crypto/ecdh" | ||
"encoding/base64" | ||
"os" | ||
) | ||
|
||
func LoadPrivateKeyFromFile(fp string) *ecdh.PrivateKey { | ||
kb, err := loadKeyFile(fp) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return PrivateKeyFromBytes(kb) | ||
} | ||
|
||
func LoadPublicKeyFromFile(fp string) *ecdh.PublicKey { | ||
kb, err := loadKeyFile(fp) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return PublicKeyFromBytes(kb) | ||
} | ||
|
||
func SavePrivateKeyToFile(fp string, k *ecdh.PrivateKey) error { | ||
return saveKeyFile(fp, k.Bytes(), privateKeyFileMode) | ||
} | ||
|
||
func SavePublicKeyToFile(fp string, k *ecdh.PublicKey) error { | ||
return saveKeyFile(fp, k.Bytes(), publicKeyFileMode) | ||
} | ||
|
||
func loadKeyFile(fp string) ([]byte, error) { | ||
b64Key, err := os.ReadFile(fp) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
keyBytes := make([]byte, KeySize) | ||
base64.StdEncoding.Decode(keyBytes, b64Key) | ||
return keyBytes, nil | ||
} | ||
|
||
func saveKeyFile(fp string, key []byte, fm os.FileMode) error { | ||
b64Key := make([]byte, base64.StdEncoding.EncodedLen(len(key))) | ||
base64.StdEncoding.Encode(b64Key, key) | ||
|
||
return os.WriteFile(fp, b64Key, fm) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters