databag/net/server/internal/keyUtil.go

88 lines
2.2 KiB
Go
Raw Normal View History

2022-01-17 05:55:25 +00:00
package databag
import (
2022-07-22 19:28:14 +00:00
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
2022-01-17 05:55:25 +00:00
)
2022-07-23 04:39:44 +00:00
//GenerateRsaKeyPair creates a public/private key for a new account
2022-01-19 23:03:06 +00:00
func GenerateRsaKeyPair() (*rsa.PrivateKey, *rsa.PublicKey, string, error) {
keyType := getStrConfigValue(CNFKeyType, "RSA4096");
if keyType == "RSA2048" {
privkey, _ := rsa.GenerateKey(rand.Reader, 2048)
2022-07-22 19:28:14 +00:00
return privkey, &privkey.PublicKey, "RSA2048", nil
} else if keyType == "RSA4096" {
privkey, _ := rsa.GenerateKey(rand.Reader, 4096)
2022-07-22 19:28:14 +00:00
return privkey, &privkey.PublicKey, "RSA2048", nil
} else {
return nil, nil, "", errors.New("invalid key setting")
}
2022-01-17 05:55:25 +00:00
}
2022-07-23 05:47:09 +00:00
//ExportRsaPrivateKeyAsPemStr exports account private key
2022-01-17 05:55:25 +00:00
func ExportRsaPrivateKeyAsPemStr(privkey *rsa.PrivateKey) string {
2022-07-22 19:28:14 +00:00
privkeyBytes := x509.MarshalPKCS1PrivateKey(privkey)
privkeyPEM := pem.EncodeToMemory(
&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: privkeyBytes,
},
)
return string(privkeyPEM)
2022-01-17 05:55:25 +00:00
}
2022-07-23 04:39:44 +00:00
//ParseRsaPrivateKeyFromPemStr loads account private key
2022-01-17 05:55:25 +00:00
func ParseRsaPrivateKeyFromPemStr(privPEM string) (*rsa.PrivateKey, error) {
2022-07-22 19:28:14 +00:00
block, _ := pem.Decode([]byte(privPEM))
if block == nil {
return nil, errors.New("failed to parse PEM block containing the key")
}
2022-01-17 05:55:25 +00:00
2022-07-22 19:28:14 +00:00
priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
2022-01-17 05:55:25 +00:00
2022-07-22 19:28:14 +00:00
return priv, nil
2022-01-17 05:55:25 +00:00
}
2022-07-23 04:39:44 +00:00
//ExportRsaPublicKeyAsPemStr exports account public key
2022-01-17 05:55:25 +00:00
func ExportRsaPublicKeyAsPemStr(pubkey *rsa.PublicKey) (string, error) {
2022-07-22 19:28:14 +00:00
pubkeyBytes, err := x509.MarshalPKIXPublicKey(pubkey)
if err != nil {
return "", err
}
pubkeyPEM := pem.EncodeToMemory(
&pem.Block{
Type: "RSA PUBLIC KEY",
Bytes: pubkeyBytes,
},
)
2022-01-17 05:55:25 +00:00
2022-07-22 19:28:14 +00:00
return string(pubkeyPEM), nil
2022-01-17 05:55:25 +00:00
}
2022-07-23 04:39:44 +00:00
//ParseRsaPublicKeyFromPemStr loads account public key
2022-01-17 05:55:25 +00:00
func ParseRsaPublicKeyFromPemStr(pubPEM string) (*rsa.PublicKey, error) {
2022-07-22 19:28:14 +00:00
block, _ := pem.Decode([]byte(pubPEM))
if block == nil {
return nil, errors.New("failed to parse PEM block containing the key")
}
2022-01-17 05:55:25 +00:00
2022-07-22 19:28:14 +00:00
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
2022-01-17 05:55:25 +00:00
2022-07-22 19:28:14 +00:00
switch pub := pub.(type) {
case *rsa.PublicKey:
return pub, nil
default:
break // fall through
}
return nil, errors.New("Key type is not RSA")
2022-01-17 05:55:25 +00:00
}