Skip to content

Commit 81cc2c6

Browse files
committed
优化 CA
1 parent d9b8059 commit 81cc2c6

File tree

11 files changed

+167
-25
lines changed

11 files changed

+167
-25
lines changed

cryptobin/ca/alias.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ import (
66
"github.com/deatil/go-cryptobin/x509"
77
)
88

9-
// pkix
109
type (
11-
// Subject 数据
10+
// Subject data
1211
PkixName = pkix.Name
1312

1413
// Extension
@@ -36,15 +35,14 @@ type (
3635
PkixAttributeTypeAndValueSET = pkix.AttributeTypeAndValueSET
3736
)
3837

39-
// x905
4038
type (
41-
// 证书
39+
// Certificate
4240
Certificate = x509.Certificate
4341

44-
// 证书请求
42+
// CertificateRequest
4543
CertificateRequest = x509.CertificateRequest
4644

47-
// 配置别名
45+
// VerifyOptions
4846
VerifyOptions = x509.VerifyOptions
4947

5048
// KeyUsage

cryptobin/ca/ca.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ func (typ PublicKeyType) String() string {
2929
return "SM2"
3030
case KeyTypeGost:
3131
return "Gost"
32+
case KeyTypeElGamal:
33+
return "ElGamal"
3234
default:
3335
return "unknown KeyType value " + strconv.Itoa(int(typ))
3436
}
@@ -42,24 +44,28 @@ const (
4244
KeyTypeEdDSA
4345
KeyTypeSM2
4446
KeyTypeGost
47+
KeyTypeElGamal
4548
)
4649

4750
// Options
4851
type Options struct {
4952
// public key type
5053
PublicKeyType PublicKeyType
5154

52-
// DSA ParameterSizes
55+
// generates DSA ParameterSizes
5356
ParameterSizes dsa.ParameterSizes
5457

55-
// ecc curve
58+
// generates ECC curve
5659
Curve elliptic.Curve
5760

58-
// gost curve
61+
// generates Gost curve
5962
GostCurve *gost.Curve
6063

6164
// generates RSA private key bit size
6265
Bits int
66+
67+
// generates ElGamal private key bit size and probability
68+
Bitsize, Probability int
6369
}
6470

6571
/**
@@ -102,6 +108,8 @@ func NewCA() CA {
102108
Curve: elliptic.P256(),
103109
GostCurve: gost.CurveDefault(),
104110
Bits: 2048,
111+
Bitsize: 256,
112+
Probability: 64,
105113
},
106114
Errors: make([]error, 0),
107115
}

cryptobin/ca/ca_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,44 @@ func Test_GenerateKey(t *testing.T) {
481481
assertEqual(pubkey22, obj.GetPublicKey(), "Test_GenerateKey-FromPublicKey")
482482
})
483483

484+
t.Run("GenerateElGamalKey", func(t *testing.T) {
485+
obj := New().
486+
SetPublicKeyType("ElGamal").
487+
WithBitsize(256).
488+
WithProbability(64).
489+
GenerateKey()
490+
491+
prikey := obj.CreatePrivateKey().ToKeyString()
492+
pubkey := obj.CreatePublicKey().ToKeyString()
493+
494+
assertError(obj.Error(), "Test_GenerateKey")
495+
assertNotEmpty(prikey, "Test_GenerateKey-prikey")
496+
assertNotEmpty(pubkey, "Test_GenerateKey-pubkey")
497+
498+
pass := []byte("12345678")
499+
prikey2 := obj.CreatePrivateKeyWithPassword(pass).ToKeyString()
500+
501+
assertNotEmpty(prikey2, "Test_GenerateKey-prikey2")
502+
503+
prikey22 := New().
504+
FromPrivateKey([]byte(prikey))
505+
506+
assertEqual(prikey22.GetPrivateKey(), obj.GetPrivateKey(), "Test_GenerateKey-FromPrivateKey")
507+
assertEqual(prikey22.GetPrivateKeyType().String(), "ElGamal", "Test_GenerateKey-GetPrivateKeyType")
508+
509+
prikey223 := New().
510+
FromPrivateKeyWithPassword([]byte(prikey2), pass)
511+
512+
assertEqual(prikey223.GetPrivateKey(), obj.GetPrivateKey(), "Test_GenerateKey-FromPrivateKeyWithPassword")
513+
assertEqual(prikey223.GetPrivateKeyType().String(), "ElGamal", "Test_GenerateKey-GetPrivateKeyType")
514+
515+
pubkey22 := New().
516+
FromPublicKey([]byte(pubkey))
517+
518+
assertEqual(pubkey22.GetPublicKey(), obj.GetPublicKey(), "Test_GenerateKey-FromPublicKey")
519+
assertEqual(pubkey22.GetPublicKeyType().String(), "ElGamal", "Test_GenerateKey-GetPublicKeyType")
520+
})
521+
484522
t.Run("GenerateRSAKey 2", func(t *testing.T) {
485523
obj := New().
486524
SetGenerateType("RSA").
@@ -592,6 +630,18 @@ func Test_GenerateKey2(t *testing.T) {
592630
assertNotEmpty(pubkey, "Test_GenerateKey2-pubkey")
593631
})
594632

633+
t.Run("GenerateElGamalKey", func(t *testing.T) {
634+
obj := New().
635+
GenerateElGamalKey(256, 64)
636+
637+
prikey := obj.CreatePrivateKey().ToKeyString()
638+
pubkey := obj.CreatePublicKey().ToKeyString()
639+
640+
assertError(obj.Error(), "Test_GenerateKey2")
641+
assertNotEmpty(prikey, "Test_GenerateKey2-prikey")
642+
assertNotEmpty(pubkey, "Test_GenerateKey2-pubkey")
643+
})
644+
595645
}
596646

597647
var prikey = `
@@ -759,6 +809,8 @@ func Test_Get(t *testing.T) {
759809
Curve: elliptic.P256(),
760810
GostCurve: gost.CurveIdGostR34102001CryptoProAParamSet(),
761811
Bits: 2048,
812+
Bitsize: 256,
813+
Probability: 64,
762814
}
763815

764816
newCA2 := CA{
@@ -785,6 +837,8 @@ func Test_Get(t *testing.T) {
785837
assertEqual(newCA2.GetCurve(), elliptic.P256(), "Test_Get-GetCurve")
786838
assertEqual(newCA2.GetGostCurve(), gost.CurveIdGostR34102001CryptoProAParamSet(), "Test_Get-GetGostCurve")
787839
assertEqual(newCA2.GetBits(), 2048, "Test_Get-GetBits")
840+
assertEqual(newCA2.GetBitsize(), 256, "Test_Get-GetBitsize")
841+
assertEqual(newCA2.GetProbability(), 64, "Test_Get-GetProbability")
788842

789843
assertEqual(newCA2.GetKeyData(), []byte("test-keyData"), "Test_Get-GetKeyData")
790844
assertEqual(newCA2.GetErrors(), []error{testerr}, "Test_Get-GetErrors")
@@ -864,6 +918,12 @@ func Test_With(t *testing.T) {
864918
tmp = newCA.WithBits(2048)
865919
assertEqual(tmp.options.Bits, 2048, "Test_Get-WithBits")
866920

921+
tmp = newCA.WithBitsize(2038)
922+
assertEqual(tmp.options.Bitsize, 2038, "Test_Get-WithBitsize")
923+
924+
tmp = newCA.WithProbability(2028)
925+
assertEqual(tmp.options.Probability, 2028, "Test_Get-WithProbability")
926+
867927
tmp = newCA.WithKeyData([]byte("test-keyData"))
868928
assertEqual(tmp.keyData, []byte("test-keyData"), "Test_Get-WithKeyData")
869929

cryptobin/ca/create.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/deatil/go-cryptobin/pkcs12"
1717
"github.com/deatil/go-cryptobin/gm/sm2"
1818
"github.com/deatil/go-cryptobin/pubkey/gost"
19+
"github.com/deatil/go-cryptobin/pubkey/elgamal"
1920
cryptobin_x509 "github.com/deatil/go-cryptobin/x509"
2021
pubkey_dsa "github.com/deatil/go-cryptobin/pubkey/dsa"
2122
)
@@ -136,6 +137,8 @@ func (this CA) CreatePrivateKey() CA {
136137
privateKeyBytes, err = sm2.MarshalPrivateKey(privateKey)
137138
case *gost.PrivateKey:
138139
privateKeyBytes, err = gost.MarshalPrivateKey(privateKey)
140+
case *elgamal.PrivateKey:
141+
privateKeyBytes, err = elgamal.MarshalPKCS8PrivateKey(privateKey)
139142
default:
140143
err = fmt.Errorf("unsupported private key type: %T", privateKey)
141144
}
@@ -182,6 +185,8 @@ func (this CA) CreatePrivateKeyWithPassword(password []byte, opts ...any) CA {
182185
privateKeyBytes, err = sm2.MarshalPrivateKey(prikey)
183186
case *gost.PrivateKey:
184187
privateKeyBytes, err = gost.MarshalPrivateKey(prikey)
188+
case *elgamal.PrivateKey:
189+
privateKeyBytes, err = elgamal.MarshalPKCS8PrivateKey(prikey)
185190
default:
186191
err = errors.New("privateKey error.")
187192
}
@@ -230,6 +235,8 @@ func (this CA) CreatePublicKey() CA {
230235
publicKeyBytes, err = sm2.MarshalPublicKey(pubkey)
231236
case *gost.PublicKey:
232237
publicKeyBytes, err = gost.MarshalPublicKey(pubkey)
238+
case *elgamal.PublicKey:
239+
publicKeyBytes, err = elgamal.MarshalPKCS8PublicKey(pubkey)
233240
default:
234241
err = errors.New("privateKey error.")
235242
}

cryptobin/ca/from.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/deatil/go-cryptobin/pkcs12"
1313
"github.com/deatil/go-cryptobin/gm/sm2"
1414
"github.com/deatil/go-cryptobin/pubkey/gost"
15+
"github.com/deatil/go-cryptobin/pubkey/elgamal"
1516
)
1617

1718
// Generate Key with Reader
@@ -62,6 +63,14 @@ func (this CA) GenerateKeyWithSeed(reader io.Reader) CA {
6263
return this.AppendError(err)
6364
}
6465

66+
this.privateKey = privateKey
67+
this.publicKey = &privateKey.PublicKey
68+
case KeyTypeElGamal:
69+
privateKey, err := elgamal.GenerateKey(reader, this.options.Bitsize, this.options.Probability)
70+
if err != nil {
71+
return this.AppendError(err)
72+
}
73+
6574
this.privateKey = privateKey
6675
this.publicKey = &privateKey.PublicKey
6776
}
@@ -287,3 +296,16 @@ func (this CA) GenerateGostKey(curve string) CA {
287296
func GenerateGostKey(curve string) CA {
288297
return defaultCA.GenerateGostKey(curve)
289298
}
299+
300+
// Generate ElGamal key
301+
func (this CA) GenerateElGamalKey(bitsize, probability int) CA {
302+
return this.SetPublicKeyType("ElGamal").
303+
WithBitsize(bitsize).
304+
WithProbability(probability).
305+
GenerateKey()
306+
}
307+
308+
// Generate ElGamal Key
309+
func GenerateElGamalKey(bitsize, probability int) CA {
310+
return defaultCA.GenerateElGamalKey(bitsize, probability)
311+
}

cryptobin/ca/get.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/deatil/go-cryptobin/x509"
1212
"github.com/deatil/go-cryptobin/gm/sm2"
1313
"github.com/deatil/go-cryptobin/pubkey/gost"
14+
"github.com/deatil/go-cryptobin/pubkey/elgamal"
1415
)
1516

1617
// 获取 cert
@@ -43,6 +44,8 @@ func (this CA) GetPrivateKeyType() PublicKeyType {
4344
return KeyTypeSM2
4445
case *gost.PrivateKey:
4546
return KeyTypeGost
47+
case *elgamal.PrivateKey:
48+
return KeyTypeElGamal
4649
}
4750

4851
return KeyTypeUnknown
@@ -68,6 +71,8 @@ func (this CA) GetPublicKeyType() PublicKeyType {
6871
return KeyTypeSM2
6972
case *gost.PublicKey:
7073
return KeyTypeGost
74+
case *elgamal.PublicKey:
75+
return KeyTypeElGamal
7176
}
7277

7378
return KeyTypeUnknown
@@ -98,6 +103,16 @@ func (this CA) GetBits() int {
98103
return this.options.Bits
99104
}
100105

106+
// get Options Bitsize
107+
func (this CA) GetBitsize() int {
108+
return this.options.Bitsize
109+
}
110+
111+
// get Options Probability
112+
func (this CA) GetProbability() int {
113+
return this.options.Probability
114+
}
115+
101116
// 获取 keyData
102117
func (this CA) GetKeyData() []byte {
103118
return this.keyData

cryptobin/ca/make.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ func (this CA) MakeCSR(
9696
CommonName: commonName,
9797
Organization: organization,
9898

99-
// Country: country,
10099
// OrganizationalUnit: organizationalUnit,
100+
// Country: country,
101101
// Locality: locality,
102102
// Province: province,
103103
// StreetAddress: streetAddress,
@@ -132,23 +132,29 @@ func getSignatureAlgorithm(name string) x509.SignatureAlgorithm {
132132
data := map[string]x509.SignatureAlgorithm {
133133
"MD2WithRSA": x509.MD2WithRSA,
134134
"MD5WithRSA": x509.MD5WithRSA,
135-
// "MD2WithRSA": x509.MD2WithRSA, // Unsupported.
136135
"SHA1WithRSA": x509.SHA1WithRSA,
137136
"SHA256WithRSA": x509.SHA256WithRSA,
138137
"SHA384WithRSA": x509.SHA384WithRSA,
139138
"SHA512WithRSA": x509.SHA512WithRSA,
140-
// "DSAWithSHA1": x509.DSAWithSHA1, // Unsupported.
141-
// "DSAWithSHA256": x509.DSAWithSHA256, // Unsupported.
139+
"SHA256WithRSAPSS": x509.SHA256WithRSAPSS,
140+
"SHA384WithRSAPSS": x509.SHA384WithRSAPSS,
141+
"SHA512WithRSAPSS": x509.SHA512WithRSAPSS,
142+
"PureEd25519": x509.PureEd25519,
143+
"DSAWithSHA1": x509.DSAWithSHA1,
144+
"DSAWithSHA256": x509.DSAWithSHA256,
142145
"ECDSAWithSHA1": x509.ECDSAWithSHA1,
143146
"ECDSAWithSHA256": x509.ECDSAWithSHA256,
144147
"ECDSAWithSHA384": x509.ECDSAWithSHA384,
145148
"ECDSAWithSHA512": x509.ECDSAWithSHA512,
146-
"SHA256WithRSAPSS": x509.SHA256WithRSAPSS,
147-
"SHA384WithRSAPSS": x509.SHA384WithRSAPSS,
148-
"SHA512WithRSAPSS": x509.SHA512WithRSAPSS,
149149
"SM2WithSM3": x509.SM2WithSM3,
150150
"SM2WithSHA1": x509.SM2WithSHA1,
151151
"SM2WithSHA256": x509.SM2WithSHA256,
152+
"SM3WithRSA": x509.SM3WithRSA,
153+
"GOST3410WithGOST34112001": x509.GOST3410WithGOST34112001,
154+
"GOST3410WithGOST34112012256": x509.GOST3410WithGOST34112012256,
155+
"GOST3410WithGOST34112012512": x509.GOST3410WithGOST34112012512,
156+
"ElGamalWithSHA1": x509.ElGamalWithSHA1,
157+
"ElGamalWithRIPEMD160": x509.ElGamalWithRIPEMD160,
152158
}
153159

154160
if alg, ok := data[name]; ok {

cryptobin/ca/parse.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/deatil/go-cryptobin/pkcs8"
1212
"github.com/deatil/go-cryptobin/gm/sm2"
1313
"github.com/deatil/go-cryptobin/pubkey/gost"
14+
"github.com/deatil/go-cryptobin/pubkey/elgamal"
1415
cryptobin_x509 "github.com/deatil/go-cryptobin/x509"
1516
pubkey_dsa "github.com/deatil/go-cryptobin/pubkey/dsa"
1617
)
@@ -23,15 +24,17 @@ var (
2324
)
2425

2526
var (
26-
oidPublicKeySM2 = asn1.ObjectIdentifier{1, 2, 156, 10197, 1, 301}
2727
oidPublicKeyRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 1}
2828
oidPublicKeyDSA = asn1.ObjectIdentifier{1, 2, 840, 10040, 4, 1}
2929
oidPublicKeyECDSA = asn1.ObjectIdentifier{1, 2, 840, 10045, 2, 1}
3030
oidPublicKeyEd25519 = asn1.ObjectIdentifier{1, 3, 101, 112}
31+
oidPublicKeySM2 = asn1.ObjectIdentifier{1, 2, 156, 10197, 1, 301}
32+
33+
oidGOSTPublicKey = asn1.ObjectIdentifier{1, 2, 643, 2, 2, 19}
34+
oidGost2012PublicKey256 = asn1.ObjectIdentifier{1, 2, 643, 7, 1, 1, 1, 1}
35+
oidGost2012PublicKey512 = asn1.ObjectIdentifier{1, 2, 643, 7, 1, 1, 1, 2}
3136

32-
oidGOSTPublicKey = asn1.ObjectIdentifier{1, 2, 643, 2, 2, 19}
33-
oidGost2012PublicKey256 = asn1.ObjectIdentifier{1, 2, 643, 7, 1, 1, 1, 1}
34-
oidGost2012PublicKey512 = asn1.ObjectIdentifier{1, 2, 643, 7, 1, 1, 1, 2}
37+
oidPublicKeyElGamal = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 3029, 1, 2, 1}
3538
)
3639

3740
type pkcs8Info struct {
@@ -89,6 +92,8 @@ func (this CA) ParsePKCS8PrivateKeyFromPEM(key []byte) (crypto.PrivateKey, error
8992
privKey.Algo.Algorithm.Equal(oidGost2012PublicKey256),
9093
privKey.Algo.Algorithm.Equal(oidGost2012PublicKey512):
9194
parsedKey, err = gost.ParsePrivateKey(block.Bytes)
95+
case privKey.Algo.Algorithm.Equal(oidPublicKeyElGamal):
96+
parsedKey, err = elgamal.ParsePKCS8PrivateKey(block.Bytes)
9297
default:
9398
return nil, ErrPrivateKeyError
9499
}
@@ -148,6 +153,8 @@ func (this CA) ParsePKCS8PrivateKeyFromPEMWithPassword(key []byte, password []by
148153
privKey.Algo.Algorithm.Equal(oidGost2012PublicKey256),
149154
privKey.Algo.Algorithm.Equal(oidGost2012PublicKey512):
150155
parsedKey, err = gost.ParsePrivateKey(blockDecrypted)
156+
case privKey.Algo.Algorithm.Equal(oidPublicKeyElGamal):
157+
parsedKey, err = elgamal.ParsePKCS8PrivateKey(blockDecrypted)
151158
default:
152159
return nil, ErrPrivateKeyError
153160
}
@@ -202,6 +209,8 @@ func (this CA) ParsePKCS8PublicKeyFromPEM(key []byte) (crypto.PublicKey, error)
202209
pubkey.Algo.Algorithm.Equal(oidGost2012PublicKey256),
203210
pubkey.Algo.Algorithm.Equal(oidGost2012PublicKey512):
204211
parsedKey, err = gost.ParsePublicKey(block.Bytes)
212+
case pubkey.Algo.Algorithm.Equal(oidPublicKeyElGamal):
213+
parsedKey, err = elgamal.ParsePKCS8PublicKey(block.Bytes)
205214
default:
206215
return nil, ErrPublicKeyError
207216
}

0 commit comments

Comments
 (0)