@@ -36,15 +36,14 @@ type pkixPublicKey struct {
3636
3737func ParsePrivateKey (der []byte ) (* PrivateKey , error ) {
3838 var privKey pkcs8
39-
4039 if _ , err := asn1 .Unmarshal (der , & privKey ); err != nil {
4140 return nil , err
4241 }
4342
43+ // check PrivateKey OID
4444 if ! privKey .Algo .Algorithm .Equal (oidSM2 ) &&
4545 ! privKey .Algo .Algorithm .Equal (oidPublicKeySM2 ) {
46- err := errors .New ("sm2: unknown private key algorithm" )
47- return nil , err
46+ return nil , errors .New ("sm2: unknown private key algorithm" )
4847 }
4948
5049 bytes := privKey .Algo .Parameters .FullBytes
@@ -61,13 +60,14 @@ func MarshalPrivateKey(key *PrivateKey) ([]byte, error) {
6160 var r pkcs8
6261 var algo pkix.AlgorithmIdentifier
6362
64- if key .Curve != P256 () {
63+ oid , ok := oidFromNamedCurve (key .Curve )
64+ if ! ok {
6565 return nil , errors .New ("sm2: unsupported SM2 curve" )
6666 }
6767
68- oidBytes , err := asn1 .Marshal (oidPublicKeySM2 )
68+ oidBytes , err := asn1 .Marshal (oid )
6969 if err != nil {
70- return nil , errors .New ("sm2: failed to marshal algo param: " + err . Error () )
70+ return nil , errors .New ("sm2: failed to marshal algo param" )
7171 }
7272
7373 algo .Algorithm = oidSM2
@@ -76,14 +76,9 @@ func MarshalPrivateKey(key *PrivateKey) ([]byte, error) {
7676 algo .Parameters .IsCompound = false
7777 algo .Parameters .FullBytes = oidBytes
7878
79- oid , ok := oidFromNamedCurve (key .Curve )
80- if ! ok {
81- return nil , errors .New ("sm2: unknown curve" )
82- }
83-
8479 r .Version = 0
8580 r .Algo = algo
86- r .PrivateKey , err = marshalSM2PrivateKeyWithOID (key , oid )
81+ r .PrivateKey , err = marshalSM2PrivateKeyWithOID (key , nil )
8782 if err != nil {
8883 return nil , err
8984 }
@@ -98,12 +93,24 @@ func ParsePublicKey(der []byte) (*PublicKey, error) {
9893 return nil , err
9994 }
10095
96+ // check PublicKey OID
10197 if ! pubkey .Algo .Algorithm .Equal (oidSM2 ) &&
10298 ! pubkey .Algo .Algorithm .Equal (oidPublicKeySM2 ) {
103- return nil , errors .New ("sm2: not sm2 curve " )
99+ return nil , errors .New ("sm2: unknown publicKey key algorithm " )
104100 }
105101
106- c := P256 ()
102+ bytes := pubkey .Algo .Parameters .FullBytes
103+
104+ namedCurveOID := new (asn1.ObjectIdentifier )
105+ if _ , err := asn1 .Unmarshal (bytes , namedCurveOID ); err != nil {
106+ namedCurveOID = nil
107+ }
108+
109+ // get curve from oid
110+ c := namedCurveFromOID (* namedCurveOID )
111+ if c == nil {
112+ return nil , errors .New ("sm2: unknown curve" )
113+ }
107114
108115 x , y := sm2curve .Unmarshal (c , pubkey .BitString .Bytes )
109116
@@ -120,13 +127,14 @@ func MarshalPublicKey(key *PublicKey) ([]byte, error) {
120127 var r pkixPublicKey
121128 var algo pkix.AlgorithmIdentifier
122129
123- if key .Curve != P256 () {
130+ oid , ok := oidFromNamedCurve (key .Curve )
131+ if ! ok {
124132 return nil , errors .New ("sm2: unsupported SM2 curve" )
125133 }
126134
127- oidBytes , err := asn1 .Marshal (oidPublicKeySM2 )
135+ oidBytes , err := asn1 .Marshal (oid )
128136 if err != nil {
129- return nil , errors .New ("sm2: failed to marshal algo param: " + err . Error () )
137+ return nil , errors .New ("sm2: failed to marshal algo param" )
130138 }
131139
132140 algo .Algorithm = oidSM2
0 commit comments