Merge pull request #324 from hrimfaxi/main

sm9: Prevent PublicKey() returning nil after unmarshaling master priavate key
This commit is contained in:
Sun Yimin 2025-04-15 17:31:55 +08:00 committed by GitHub
commit 5aacbc2011
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 2 deletions

View File

@ -114,7 +114,13 @@ func UnmarshalSignMasterPrivateKeyASN1(der []byte) (*SignMasterPrivateKey, error
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &SignMasterPrivateKey{privateKey: priv.Bytes(), internal: priv}, nil
master := &SignMasterPrivateKey{privateKey: priv.Bytes(), internal: priv}
master.publicKey = &SignMasterPublicKey{
publicKey: priv.PublicKey().Bytes(),
internal: priv.PublicKey(),
}
return master, nil
} }
// GenerateUserKey generate a signature private key for the given user. // GenerateUserKey generate a signature private key for the given user.
@ -370,7 +376,13 @@ func UnmarshalEncryptMasterPrivateKeyASN1(der []byte) (*EncryptMasterPrivateKey,
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &EncryptMasterPrivateKey{privateKey: privateKey.Bytes(), internal: privateKey}, nil
master := &EncryptMasterPrivateKey{privateKey: privateKey.Bytes(), internal: privateKey}
master.publicKey = &EncryptMasterPublicKey{
publicKey: privateKey.PublicKey().Bytes(),
internal: privateKey.PublicKey(),
}
return master, nil
} }
// Equal compares the receiver EncryptMasterPublicKey with another EncryptMasterPublicKey // Equal compares the receiver EncryptMasterPublicKey with another EncryptMasterPublicKey

View File

@ -27,6 +27,11 @@ func TestSignMasterPrivateKeyMarshalASN1(t *testing.T) {
if !masterKey.Equal(masterKey2) { if !masterKey.Equal(masterKey2) {
t.Errorf("expected %v, got %v", hex.EncodeToString(masterKey.Bytes()), hex.EncodeToString(masterKey2.Bytes())) t.Errorf("expected %v, got %v", hex.EncodeToString(masterKey.Bytes()), hex.EncodeToString(masterKey2.Bytes()))
} }
masterPubKey := masterKey2.PublicKey()
if masterPubKey == nil {
t.Fatal("cannot export public key")
}
} }
func TestSignMasterPublicKeyMarshalASN1(t *testing.T) { func TestSignMasterPublicKeyMarshalASN1(t *testing.T) {
@ -129,6 +134,11 @@ func TestEncryptMasterPrivateKeyMarshalASN1(t *testing.T) {
if !masterKey.Equal(masterKey2) { if !masterKey.Equal(masterKey2) {
t.Errorf("expected %v, got %v", hex.EncodeToString(masterKey.Bytes()), hex.EncodeToString(masterKey2.Bytes())) t.Errorf("expected %v, got %v", hex.EncodeToString(masterKey.Bytes()), hex.EncodeToString(masterKey2.Bytes()))
} }
masterPubKey := masterKey2.PublicKey()
if masterPubKey == nil {
t.Fatal("cannot export public key")
}
} }
func TestEncryptMasterPublicKeyMarshalASN1(t *testing.T) { func TestEncryptMasterPublicKeyMarshalASN1(t *testing.T) {