mirror of
https://github.com/casdoor/casdoor.git
synced 2025-05-23 02:35:49 +08:00
fix: add more cert algorithms like ES256 and PS256 (#2793)
This commit is contained in:
parent
0dd474d5fc
commit
2dc755f529
@ -16,6 +16,7 @@ package object
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/casdoor/casdoor/util"
|
"github.com/casdoor/casdoor/util"
|
||||||
"github.com/xorm-io/core"
|
"github.com/xorm-io/core"
|
||||||
@ -206,7 +207,17 @@ func (p *Cert) GetId() string {
|
|||||||
|
|
||||||
func (p *Cert) populateContent() error {
|
func (p *Cert) populateContent() error {
|
||||||
if p.Certificate == "" || p.PrivateKey == "" {
|
if p.Certificate == "" || p.PrivateKey == "" {
|
||||||
certificate, privateKey, err := generateRsaKeys(p.BitSize, p.ExpireInYears, p.Name, p.Owner)
|
var err error
|
||||||
|
var certificate, privateKey string
|
||||||
|
if strings.HasPrefix(p.CryptoAlgorithm, "RS") {
|
||||||
|
certificate, privateKey, err = generateRsaKeys(p.BitSize, util.ParseInt(p.CryptoAlgorithm[2:]), p.ExpireInYears, p.Name, p.Owner)
|
||||||
|
} else if strings.HasPrefix(p.CryptoAlgorithm, "ES") {
|
||||||
|
certificate, privateKey, err = generateEsKeys(p.BitSize, util.ParseInt(p.CryptoAlgorithm[2:]), p.ExpireInYears, p.Name, p.Owner)
|
||||||
|
} else if strings.HasPrefix(p.CryptoAlgorithm, "PS") {
|
||||||
|
certificate, privateKey, err = generateRsaPssKeys(p.BitSize, util.ParseInt(p.CryptoAlgorithm[2:]), p.ExpireInYears, p.Name, p.Owner)
|
||||||
|
} else {
|
||||||
|
err = fmt.Errorf("Crypto algorithm %s is not found", p.CryptoAlgorithm)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -15,16 +15,19 @@
|
|||||||
package object
|
package object
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/ecdsa"
|
||||||
|
"crypto/elliptic"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"crypto/rsa"
|
"crypto/rsa"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"crypto/x509/pkix"
|
"crypto/x509/pkix"
|
||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func generateRsaKeys(bitSize int, expireInYears int, commonName string, organization string) (string, string, error) {
|
func generateRsaKeys(bitSize int, algorithmType int, expireInYears int, commonName string, organization string) (string, string, error) {
|
||||||
// https://stackoverflow.com/questions/64104586/use-golang-to-get-rsa-key-the-same-way-openssl-genrsa
|
// https://stackoverflow.com/questions/64104586/use-golang-to-get-rsa-key-the-same-way-openssl-genrsa
|
||||||
// https://stackoverflow.com/questions/43822945/golang-can-i-create-x509keypair-using-rsa-key
|
// https://stackoverflow.com/questions/43822945/golang-can-i-create-x509keypair-using-rsa-key
|
||||||
|
|
||||||
@ -55,6 +58,132 @@ func generateRsaKeys(bitSize int, expireInYears int, commonName string, organiza
|
|||||||
BasicConstraintsValid: true,
|
BasicConstraintsValid: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
switch algorithmType {
|
||||||
|
case 256:
|
||||||
|
tml.SignatureAlgorithm = x509.SHA256WithRSA
|
||||||
|
case 384:
|
||||||
|
tml.SignatureAlgorithm = x509.SHA384WithRSA
|
||||||
|
case 512:
|
||||||
|
tml.SignatureAlgorithm = x509.SHA512WithRSA
|
||||||
|
default:
|
||||||
|
return "", "", fmt.Errorf("unsupported algorithm type")
|
||||||
|
}
|
||||||
|
|
||||||
|
cert, err := x509.CreateCertificate(rand.Reader, &tml, &tml, &key.PublicKey, key)
|
||||||
|
if err != nil {
|
||||||
|
return "", "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate a pem block with the certificate
|
||||||
|
certPem := pem.EncodeToMemory(&pem.Block{
|
||||||
|
Type: "CERTIFICATE",
|
||||||
|
Bytes: cert,
|
||||||
|
})
|
||||||
|
|
||||||
|
return string(certPem), string(privateKeyPem), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func generateEsKeys(bitSize int, algorithmType int, expireInYears int, commonName string, organization string) (string, string, error) {
|
||||||
|
var curve elliptic.Curve
|
||||||
|
switch algorithmType {
|
||||||
|
case 256:
|
||||||
|
curve = elliptic.P256()
|
||||||
|
case 384:
|
||||||
|
curve = elliptic.P384()
|
||||||
|
case 512:
|
||||||
|
curve = elliptic.P521() // ES512(P521,SHA512)
|
||||||
|
default:
|
||||||
|
return "", "", fmt.Errorf("unsupported algorithm type")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate ECDSA key pair.
|
||||||
|
privateKey, err := ecdsa.GenerateKey(curve, rand.Reader)
|
||||||
|
if err != nil {
|
||||||
|
return "", "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Encode private key to PEM format.
|
||||||
|
privateKeyBytes, err := x509.MarshalECPrivateKey(privateKey)
|
||||||
|
if err != nil {
|
||||||
|
return "", "", err
|
||||||
|
}
|
||||||
|
privateKeyPem := pem.EncodeToMemory(&pem.Block{
|
||||||
|
Type: "EC PRIVATE KEY",
|
||||||
|
Bytes: privateKeyBytes,
|
||||||
|
})
|
||||||
|
|
||||||
|
// Generate certificate template.
|
||||||
|
template := x509.Certificate{
|
||||||
|
NotBefore: time.Now(),
|
||||||
|
NotAfter: time.Now().AddDate(expireInYears, 0, 0),
|
||||||
|
SerialNumber: big.NewInt(time.Now().Unix()),
|
||||||
|
Subject: pkix.Name{
|
||||||
|
CommonName: commonName,
|
||||||
|
Organization: []string{organization},
|
||||||
|
},
|
||||||
|
BasicConstraintsValid: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate certificate.
|
||||||
|
certBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &privateKey.PublicKey, privateKey)
|
||||||
|
if err != nil {
|
||||||
|
return "", "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Encode certificate to PEM format.
|
||||||
|
certPem := pem.EncodeToMemory(&pem.Block{
|
||||||
|
Type: "CERTIFICATE",
|
||||||
|
Bytes: certBytes,
|
||||||
|
})
|
||||||
|
|
||||||
|
return string(certPem), string(privateKeyPem), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func generateRsaPssKeys(bitSize int, algorithmType int, expireInYears int, commonName string, organization string) (string, string, error) {
|
||||||
|
// Generate RSA key.
|
||||||
|
key, err := rsa.GenerateKey(rand.Reader, bitSize)
|
||||||
|
if err != nil {
|
||||||
|
return "", "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Encode private key to PKCS#8 ASN.1 PEM.
|
||||||
|
privateKeyBytes, err := x509.MarshalPKCS8PrivateKey(key)
|
||||||
|
if err != nil {
|
||||||
|
return "", "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
privateKeyPem := pem.EncodeToMemory(
|
||||||
|
&pem.Block{
|
||||||
|
Type: "RSA PSS PRIVATE KEY",
|
||||||
|
Bytes: privateKeyBytes,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
tml := x509.Certificate{
|
||||||
|
// you can add any attr that you need
|
||||||
|
NotBefore: time.Now(),
|
||||||
|
NotAfter: time.Now().AddDate(expireInYears, 0, 0),
|
||||||
|
// you have to generate a different serial number each execution
|
||||||
|
SerialNumber: big.NewInt(123456),
|
||||||
|
Subject: pkix.Name{
|
||||||
|
CommonName: commonName,
|
||||||
|
Organization: []string{organization},
|
||||||
|
},
|
||||||
|
BasicConstraintsValid: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the signature algorithm based on the hash function
|
||||||
|
switch algorithmType {
|
||||||
|
case 256:
|
||||||
|
tml.SignatureAlgorithm = x509.SHA256WithRSAPSS
|
||||||
|
case 384:
|
||||||
|
tml.SignatureAlgorithm = x509.SHA384WithRSAPSS
|
||||||
|
case 512:
|
||||||
|
tml.SignatureAlgorithm = x509.SHA512WithRSAPSS
|
||||||
|
default:
|
||||||
|
return "", "", fmt.Errorf("unsupported algorithm type")
|
||||||
|
}
|
||||||
|
|
||||||
cert, err := x509.CreateCertificate(rand.Reader, &tml, &tml, &key.PublicKey, key)
|
cert, err := x509.CreateCertificate(rand.Reader, &tml, &tml, &key.PublicKey, key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", err
|
return "", "", err
|
||||||
|
@ -23,7 +23,35 @@ import (
|
|||||||
|
|
||||||
func TestGenerateRsaKeys(t *testing.T) {
|
func TestGenerateRsaKeys(t *testing.T) {
|
||||||
fileId := "token_jwt_key"
|
fileId := "token_jwt_key"
|
||||||
certificate, privateKey, err := generateRsaKeys(4096, 20, "Casdoor Cert", "Casdoor Organization")
|
certificate, privateKey, err := generateRsaKeys(4096, 512, 20, "Casdoor Cert", "Casdoor Organization")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write certificate (aka certificate) to file.
|
||||||
|
util.WriteStringToPath(certificate, fmt.Sprintf("%s.pem", fileId))
|
||||||
|
|
||||||
|
// Write private key to file.
|
||||||
|
util.WriteStringToPath(privateKey, fmt.Sprintf("%s.key", fileId))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGenerateEsKeys(t *testing.T) {
|
||||||
|
fileId := "token_jwt_key"
|
||||||
|
certificate, privateKey, err := generateEsKeys(4096, 256, 20, "Casdoor Cert", "Casdoor Organization")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write certificate (aka certificate) to file.
|
||||||
|
util.WriteStringToPath(certificate, fmt.Sprintf("%s.pem", fileId))
|
||||||
|
|
||||||
|
// Write private key to file.
|
||||||
|
util.WriteStringToPath(privateKey, fmt.Sprintf("%s.key", fileId))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGenerateRsaPssKeys(t *testing.T) {
|
||||||
|
fileId := "token_jwt_key"
|
||||||
|
certificate, privateKey, err := generateRsaPssKeys(4096, 256, 20, "Casdoor Cert", "Casdoor Organization")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -171,9 +171,13 @@ class CertEditPage extends React.Component {
|
|||||||
<Col span={22} >
|
<Col span={22} >
|
||||||
<Select virtual={false} style={{width: "100%"}} value={this.state.cert.cryptoAlgorithm} onChange={(value => {
|
<Select virtual={false} style={{width: "100%"}} value={this.state.cert.cryptoAlgorithm} onChange={(value => {
|
||||||
this.updateCertField("cryptoAlgorithm", value);
|
this.updateCertField("cryptoAlgorithm", value);
|
||||||
if (value === "RS256") {
|
if (value === "RS256" || value === "PS256") {
|
||||||
this.updateCertField("bitSize", 2048);
|
this.updateCertField("bitSize", 2048);
|
||||||
} else if (value === "HS256" || value === "ES256") {
|
} else if (value === "RS384" || value === "PS384") {
|
||||||
|
this.updateCertField("bitSize", 2048);
|
||||||
|
} else if (value === "RS512" || value === "PS512") {
|
||||||
|
this.updateCertField("bitSize", 2048);
|
||||||
|
} else if (value === "ES256") {
|
||||||
this.updateCertField("bitSize", 256);
|
this.updateCertField("bitSize", 256);
|
||||||
} else if (value === "ES384") {
|
} else if (value === "ES384") {
|
||||||
this.updateCertField("bitSize", 384);
|
this.updateCertField("bitSize", 384);
|
||||||
@ -188,10 +192,14 @@ class CertEditPage extends React.Component {
|
|||||||
{
|
{
|
||||||
[
|
[
|
||||||
{id: "RS256", name: "RS256 (RSA + SHA256)"},
|
{id: "RS256", name: "RS256 (RSA + SHA256)"},
|
||||||
{id: "HS256", name: "HS256 (HMAC + SHA256)"},
|
{id: "RS384", name: "RS384 (RSA + SHA384)"},
|
||||||
|
{id: "RS512", name: "RS512 (RSA + SHA512)"},
|
||||||
{id: "ES256", name: "ES256 (ECDSA using P-256 + SHA256)"},
|
{id: "ES256", name: "ES256 (ECDSA using P-256 + SHA256)"},
|
||||||
{id: "ES384", name: "ES384 (ECDSA using P-384 + SHA256)"},
|
{id: "ES384", name: "ES384 (ECDSA using P-384 + SHA384)"},
|
||||||
{id: "ES521", name: "ES521 (ECDSA using P-521 + SHA256)"},
|
{id: "ES512", name: "ES512 (ECDSA using P-521 + SHA512)"},
|
||||||
|
{id: "PS256", name: "PS256 (RSASSA-PSS using SHA256 and MGF1 with SHA256)"},
|
||||||
|
{id: "PS384", name: "PS384 (RSASSA-PSS using SHA384 and MGF1 with SHA384)"},
|
||||||
|
{id: "PS512", name: "PS512 (RSASSA-PSS using SHA512 and MGF1 with SHA512)"},
|
||||||
].map((item, index) => <Option key={index} value={item.id}>{item.name}</Option>)
|
].map((item, index) => <Option key={index} value={item.id}>{item.name}</Option>)
|
||||||
}
|
}
|
||||||
</Select>
|
</Select>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user