package pkcs8_test

import (
	"crypto/ecdsa"
	"crypto/elliptic"
	"crypto/rand"
	"crypto/rsa"
	"crypto/x509/pkix"
	"encoding/asn1"
	"encoding/pem"
	"testing"

	"github.com/emmansun/gmsm/pkcs"
	"github.com/emmansun/gmsm/pkcs8"
	"github.com/emmansun/gmsm/sm2"
	"github.com/emmansun/gmsm/sm9"
)

const rsa2048 = `-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDBMF0LikTFOU/T
8DUDSvORootvhUD67f6AXmEnntfXRvQ3O91+qt40tevS8JtFaq4gKxugRjjZRtni
50aUGcEZ4leq3DboBL9XH089IEmxxLbJeJIXxgPeRHrXRINvUSspwRrJkX6fnXyi
MdRhqdH2tG1yrXKkt9UvdSHfRYimDcJ+ry2zYlcbz9aoLDO1vEdS/IBu0jXAZ/Z/
xaEVfkoWMzZM2SU+lfJeyzobii00VXGuSQKnI8E/e16kDpBXJ6PFSm6EyZmAad6O
f+B9d/ZEXGQlbaooG54v5sGj54mg7m/75qMaxL2H8NER31gAeyvoyovfXI0vbswH
8AozxGwDAgMBAAECggEAautIY62nt/urKaIExQjDWvO59gOq3fW/5+3UGWh5DqUv
Xi5cvND2X/fbR4hwdu++5QDWrlKO/fmPd1wGnMrQK3IwkNiF7s1J1H74jN0EzEUR
4NlBCbVGyMnfrqo1j/M9T0OXfr1udgpkQyQO5epl0QM0m8ZQ78bqTvSlxXsnULbQ
py0Tx0uCWaP6FzDsZ+t2rj/SVH7hQNf8ITfQJhVol/n5Hza4+NRfp/DPXWZEvPlo
GeMs9PDCa16tw8wI9EUnmFaeFlmtJPdTs5rVo9Ya/zmtoxN6AGTCG0IE6YRvh3Qn
jttIp2QitOSBKmXpu1ZI6UTtimGgnfiJKK1BGVaMOQKBgQDfF6ZBMY/tLmDg1mgS
QQKAOWMB0/3CvzcM96R0VACO2vr1BbePMXQQ/i27rD001Xl2wNTsETRk1Ji6btwQ
64m4uxRSZ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VATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgjLFzKb/8hsdSmPft
s98RZ7AWzQnLDsMhy6v+/3BZlZ6hRANCAASKkodoH+hHmBfwoFfrvv1E+iMLt3g1
s6hxOUMbkv6ZTVFXND/3z9zlJli6/YGrlSnsHOJc0GbwSYD1AMwZyr0T
-----END PRIVATE KEY-----
`

const encryptedEC256aes = `-----BEGIN ENCRYPTED PRIVATE KEY-----
MIHsMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjVvKZtHlmIbAICCAAw
DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEL3jdkBvObn+QELgKVE2cnMEgZAl
wgo3AjtXevJaGgep5GsW2krw9S7dC7xG9dR33Z/a9nBnO1rKm7Htf0+986w/1vmj
4k3M2QiI/VY+tnDFE+46DLLKYtJGRT1aoAH+mwhzaQGwzJnKhbeA23aE0f7KWCAK
+f999+SeHWro7FiRZjHEYVVLGQr/I7K5Wyh24YjN2nR4CU4X+GQU25My/pgSRog=
-----END ENCRYPTED PRIVATE KEY-----
`

const ec128 = `-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgjLFzKb/8hsdSmPft
s98RZ7AWzQnLDsMhy6v+/3BZlZ6hRANCAASKkodoH+hHmBfwoFfrvv1E+iMLt3g1
s6hxOUMbkv6ZTVFXND/3z9zlJli6/YGrlSnsHOJc0GbwSYD1AMwZyr0T
-----END PRIVATE KEY-----`

const encryptedEC128aes = `-----BEGIN ENCRYPTED PRIVATE KEY-----
MIHeMEkGCSqGSIb3DQEFDTA8MBsGCSqGSIb3DQEFDDAOBAg7qE4RYQEEugICCAAw
HQYJYIZIAWUDBAECBBBa+6eKv6il/iEjOw8/AmEHBIGQ24YmBiMfzjJjFU+PAwXr
zCfR3NPOHBwn3+BkpyivaezSrFWIF919cnDyI15Omd+Iz2oljrT/R4IDC9NOmoAy
5uKixYGAOi74Qr9kdgrT2Bfvu9wq+dYqPwLjR4WFHl2ofrLn7RCaOa8mOh3bgfHP
SnXPiACchx53PDh6bZTIZ0V9v0ymcMuXf758OXbUmSGN
-----END ENCRYPTED PRIVATE KEY-----`

const encryptedEC256aes128sha1 = `-----BEGIN ENCRYPTED PRIVATE KEY-----
MIHeMEkGCSqGSIb3DQEFDTA8MBsGCSqGSIb3DQEFDDAOBAgEoFG3x07DbQICCAAw
HQYJYIZIAWUDBAECBBCRN9PNX9rBqXhaHLUOsv7YBIGQFfXAPPV+COWABJdSarog
eUHFNaQ+R6x55Tz/mquNIwiOrP9DNoEd1PGtKaHaO+ACSEQwMfrGeh8BuNV69EwP
bhsob/MZeexRbrLe2YN7Y7/Y0wpujalGlliMvs35f1fpq/9RfVU+qRpFED2lT4dm
zOuhMC9Oo3oMYlbEXAT9mq33MkGKMUth2ek/bQIvnCHG
-----END ENCRYPTED PRIVATE KEY-----
`

const encryptedPBEWithMD5AndDES = `-----BEGIN ENCRYPTED PRIVATE KEY-----
MIGwMBsGCSqGSIb3DQEFAzAOBAhsKeK+cnfdjAICCAAEgZAE5GZMjPQCLLifGK0r
ytlpt23Qas1KI6x7qmIP6oeYflCWT0Iv7AqK2cT8YK7s5Yy3j21YiHEG5FCr8Qb+
GMlgQsRGkeU5y0I9zLZrhH9qOVJEuDLckCjMKbFXUEwx5YeBhQKTosB/quA5v9Lp
6SSLtKShYgx/MDJDarcAuj0whmNyTXijDGAMImltuqwsIUg=
-----END ENCRYPTED PRIVATE KEY-----
`

const encyptedPBEWithSha1AndDES = `-----BEGIN ENCRYPTED PRIVATE KEY-----
MIGwMBsGCSqGSIb3DQEFCjAOBAiFq+R6absk/wICCAAEgZB7T7BEaGkyMqw8xN9e
ldSPFAAXzcsPx83w1jvD8TvM7uwqUu9k0+2FnSMcuhOHjX03AFZ2JJXZZBWxZJ24
GEWLwQYJIJ16el9n2DVPkp1qqbsXPMCyHR9hW4Qxt/9aXZmTLdqpAhQ9BfTSmpQp
Yt+/s6eXMOHP2C0sp5aOxnIjkorzfgasO/Y8JtMukVlKzqU=
-----END ENCRYPTED PRIVATE KEY-----
`

const encryptedPBEWithSha1AndRC2_64 = `-----BEGIN ENCRYPTED PRIVATE KEY-----
MIGwMBsGCSqGSIb3DQEFCzAOBAhNHGnZio3lKwICCAAEgZCGUGQ3b6zS/iBlJ6BY
mpmuPBmGnuwtOtTFshWaZL8kPUROkZVBrKt6a/oM0vsTbyEDeii9ktt2cnd3plwh
fqnOmJmOBwHVeltjRLYYFzs2JgX4bXSc9eg+/AugvsDPj+dgk0yMsRLjKoZw/w/U
qZTZq/iFYg4Q80Ew7gJUBaMdA6Af8K/YDc3If7y78L0AD74=
-----END ENCRYPTED PRIVATE KEY-----
`

const encryptedPBEWithMD5AndRC2_40 = `-----BEGIN ENCRYPTED PRIVATE KEY-----
MIGwMBsGCSqGSIb3DQEFBjAOBAiZRjrXPXpYwgICCAAEgZBilgJ9rYtNt7Ih59zF
jnErhxny2wRPoK1Ng/bLNijlBnppryizyAHuujNmpDRf77pYFmszaaprWZDs2bQw
tlhqw20XVOBG2PhHXsL9LXfbm7lJOVpMtBYtbduascC1aA5Dref9L3nBNlP5zdMp
TFgdUgkic4/tuw6b5E3Ysn3ugAlPTAMm7b8Nd0Hs0P/81nA=
-----END ENCRYPTED PRIVATE KEY-----
`

func TestParsePKCS8PrivateKeyRSA(t *testing.T) {
	keyList := []struct {
		name      string
		clear     string
		encrypted string
	}{
		{
			name:      "encryptedRSA2048aes",
			clear:     rsa2048,
			encrypted: encryptedRSA2048aes,
		},
		{
			name:      "encryptedRSA2048des3",
			clear:     rsa2048,
			encrypted: encryptedRSA2048des3,
		},
	}
	for i, key := range keyList {
		t.Run(key.name, func(t *testing.T) {
			block, _ := pem.Decode([]byte(key.encrypted))
			_, err := pkcs8.ParsePKCS8PrivateKeyRSA(block.Bytes, []byte("password"))
			if err != nil {
				t.Errorf("%d: ParsePKCS8PrivateKeyRSA returned: %s", i, err)
			}
			_, err = pkcs8.ParsePKCS8PrivateKeyRSA(block.Bytes, []byte("wrong password"))
			if err == nil {
				t.Errorf("%d: should have failed", i)
			}
			_, err = pkcs8.ParsePKCS8PrivateKeyRSA(block.Bytes)
			if err == nil {
				t.Errorf("%d: should have failed", i)
			}

			block, _ = pem.Decode([]byte(key.clear))
			_, err = pkcs8.ParsePKCS8PrivateKeyRSA(block.Bytes)
			if err != nil {
				t.Errorf("%d: ParsePKCS8PrivateKeyRSA returned: %s", i, err)
			}
		})
	}
}

func TestParsePKCS8PrivateKeyECDSA(t *testing.T) {
	keyList := []struct {
		name      string
		clear     string
		encrypted string
	}{
		{
			name:      "encryptedEC256aes",
			clear:     ec256,
			encrypted: encryptedEC256aes,
		},
	}
	for i, key := range keyList {
		t.Run(key.name, func(t *testing.T) {
			block, _ := pem.Decode([]byte(key.encrypted))
			_, err := pkcs8.ParsePKCS8PrivateKeyECDSA(block.Bytes, []byte("password"))
			if err != nil {
				t.Errorf("%d: ParsePKCS8PrivateKeyECDSA returned: %s", i, err)
			}
			_, err = pkcs8.ParsePKCS8PrivateKeyECDSA(block.Bytes, []byte("wrong password"))
			if err == nil {
				t.Errorf("%d: should have failed", i)
			}
			_, err = pkcs8.ParsePKCS8PrivateKeyECDSA(block.Bytes)
			if err == nil {
				t.Errorf("%d: should have failed", i)
			}

			block, _ = pem.Decode([]byte(key.clear))
			_, err = pkcs8.ParsePKCS8PrivateKeyECDSA(block.Bytes)
			if err != nil {
				t.Errorf("%d: ParsePKCS8PrivateKeyECDSA returned: %s", i, err)
			}
		})
	}
}

type testPrivateKey struct {
	name      string
	clear     string
	encrypted string
	password  string
}

func testParsePKCS8PrivateKey(t *testing.T, i int, key *testPrivateKey) {
	block, _ := pem.Decode([]byte(key.encrypted))
	_, err := pkcs8.ParsePKCS8PrivateKey(block.Bytes, []byte(key.password))
	if err != nil {
		t.Errorf("%d: ParsePKCS8PrivateKey returned: %s", i, err)
	}
	_, err = pkcs8.ParsePKCS8PrivateKey(block.Bytes, []byte("wrong password"))
	if err == nil {
		t.Errorf("%d: should have failed", i)
	}
	_, err = pkcs8.ParsePKCS8PrivateKey(block.Bytes)
	if err == nil {
		t.Errorf("%d: should have failed", i)
	}

	if key.clear != "" {
		block, _ = pem.Decode([]byte(key.clear))
		_, err = pkcs8.ParsePKCS8PrivateKey(block.Bytes)
		if err != nil {
			t.Errorf("%d: ParsePKCS8PrivateKey returned: %s", i, err)
		}
	}
}
func TestParsePKCS8PrivateKey(t *testing.T) {
	keyList := []testPrivateKey{
		{
			name:      "encryptedRSA2048aes",
			clear:     rsa2048,
			encrypted: encryptedRSA2048aes,
			password:  "password",
		},
		{
			name:      "encryptedRSA2048des3",
			clear:     rsa2048,
			encrypted: encryptedRSA2048des3,
			password:  "password",
		},
		{
			name:      "encryptedRSA2048scrypt",
			clear:     rsa2048,
			encrypted: encryptedRSA2048scrypt,
			password:  "password",
		},
		{
			name:      "encryptedEC256aes",
			clear:     ec256,
			encrypted: encryptedEC256aes,
			password:  "password",
		},
		{
			name:      "encryptedEC256aes128sha1",
			clear:     ec256,
			encrypted: encryptedEC256aes128sha1,
			password:  "password",
		},
		{
			name:      "encryptedEC128aes",
			clear:     ec128,
			encrypted: encryptedEC128aes,
			password:  "password",
		},
	}
	for i, key := range keyList {
		t.Run(key.name, func(t *testing.T) {
			testParsePKCS8PrivateKey(t, i, &key)
		})
	}
}

func TestConvertPrivateKeyToPKCS8(t *testing.T) {
	for i, password := range [][]byte{nil, []byte("password")} {
		var args [][]byte
		if password != nil {
			args = append(args, password)
		}
		rsaPrivateKey, err := rsa.GenerateKey(rand.Reader, 2048)
		if err != nil {
			t.Fatalf("%d: GenerateKey returned: %s", i, err)
		}
		der, err := pkcs8.ConvertPrivateKeyToPKCS8(rsaPrivateKey, args...)
		if err != nil {
			t.Fatalf("%d: ConvertPrivateKeyToPKCS8 returned: %s", i, err)
		}
		decodedRSAPrivateKey, err := pkcs8.ParsePKCS8PrivateKey(der, args...)
		if err != nil {
			t.Fatalf("%d: ParsePKCS8PrivateKey returned: %s", i, err)
		}
		if rsaPrivateKey.D.Cmp(decodedRSAPrivateKey.(*rsa.PrivateKey).D) != 0 {
			t.Fatalf("%d: Decoded key does not match original key", i)
		}

		for _, curve := range []elliptic.Curve{
			elliptic.P224(), elliptic.P256(), elliptic.P384(), elliptic.P521(),
		} {
			ecPrivateKey, err := ecdsa.GenerateKey(curve, rand.Reader)
			if err != nil {
				t.Fatalf("%d, %s: GenerateKey returned: %s", i, curve, err)
			}
			der, err = pkcs8.ConvertPrivateKeyToPKCS8(ecPrivateKey, args...)
			if err != nil {
				t.Fatalf("%d, %s: ConvertPrivateKeyToPKCS8 returned: %s", i, curve, err)
			}
			decodedECPrivateKey, err := pkcs8.ParsePKCS8PrivateKey(der, args...)
			if err != nil {
				t.Fatalf("%d, %s: ParsePKCS8PrivateKey returned: %s", i, curve, err)
			}
			if ecPrivateKey.D.Cmp(decodedECPrivateKey.(*ecdsa.PrivateKey).D) != 0 {
				t.Fatalf("%d, %s: Decoded key does not match original key", i, curve)
			}
		}
	}
}

func TestMarshalPrivateKey(t *testing.T) {
	for i, tt := range []struct {
		password []byte
		opts     *pkcs8.Opts
	}{
		{
			password: nil,
			opts:     nil,
		},
		{
			password: []byte("password"),
			opts: &pkcs8.Opts{
				Cipher: pkcs.SM4CBC,
				KDFOpts: pkcs8.PBKDF2Opts{
					SaltSize: 8, IterationCount: 2048, HMACHash: pkcs8.SM3,
				},
			},
		},
		{
			password: []byte("password"),
			opts: &pkcs8.Opts{
				Cipher: pkcs.SM4GCM,
				KDFOpts: pkcs8.PBKDF2Opts{
					SaltSize: 8, IterationCount: 2048, HMACHash: pkcs8.SM3,
				},
			},
		},
		{
			password: []byte("password"),
			opts: &pkcs8.Opts{
				Cipher: pkcs.AES128CBC,
				KDFOpts: pkcs8.PBKDF2Opts{
					SaltSize: 8, IterationCount: 2048, HMACHash: pkcs8.SHA224,
				},
			},
		},
		{
			password: []byte("password"),
			opts: &pkcs8.Opts{
				Cipher: pkcs.AES128CBC,
				KDFOpts: pkcs8.PBKDF2Opts{
					SaltSize: 8, IterationCount: 2048, HMACHash: pkcs8.SHA256,
				},
			},
		},
		{
			password: []byte("password"),
			opts: &pkcs8.Opts{
				Cipher: pkcs.AES128CBC,
				KDFOpts: pkcs8.PBKDF2Opts{
					SaltSize: 8, IterationCount: 2048, HMACHash: pkcs8.SHA512,
				},
			},
		},
		{
			password: []byte("password"),
			opts: &pkcs8.Opts{
				Cipher: pkcs.AES128CBC,
				KDFOpts: pkcs8.PBKDF2Opts{
					SaltSize: 8, IterationCount: 2048, HMACHash: pkcs8.SHA384,
				},
			},
		},
		{
			password: []byte("password"),
			opts: &pkcs8.Opts{
				Cipher: pkcs.AES128CBC,
				KDFOpts: pkcs8.PBKDF2Opts{
					SaltSize: 8, IterationCount: 2048, HMACHash: pkcs8.SHA512_224,
				},
			},
		},
		{
			password: []byte("password"),
			opts: &pkcs8.Opts{
				Cipher: pkcs.AES128CBC,
				KDFOpts: pkcs8.PBKDF2Opts{
					SaltSize: 8, IterationCount: 2048, HMACHash: pkcs8.SHA512_256,
				},
			},
		},
		{
			password: []byte("password"),
			opts: &pkcs8.Opts{
				Cipher: pkcs.AES192CBC,
				KDFOpts: pkcs8.PBKDF2Opts{
					SaltSize: 8, IterationCount: 1000, HMACHash: pkcs8.SHA256,
				},
			},
		},
		{
			password: []byte("password"),
			opts: &pkcs8.Opts{
				Cipher: pkcs.AES256CBC,
				KDFOpts: pkcs8.PBKDF2Opts{
					SaltSize: 16, IterationCount: 2000, HMACHash: pkcs8.SHA256,
				},
			},
		},
		{
			password: []byte("password"),
			opts: &pkcs8.Opts{
				Cipher: pkcs.AES128GCM,
				KDFOpts: pkcs8.PBKDF2Opts{
					SaltSize: 8, IterationCount: 2048, HMACHash: pkcs8.SHA256,
				},
			},
		},
		{
			password: []byte("password"),
			opts: &pkcs8.Opts{
				Cipher: pkcs.AES192GCM,
				KDFOpts: pkcs8.PBKDF2Opts{
					SaltSize: 8, IterationCount: 10000, HMACHash: pkcs8.SHA256,
				},
			},
		},
		{
			password: []byte("password"),
			opts: &pkcs8.Opts{
				Cipher: pkcs.AES256GCM,
				KDFOpts: pkcs8.PBKDF2Opts{
					SaltSize: 16, IterationCount: 16, HMACHash: pkcs8.SHA256,
				},
			},
		},
		{
			password: []byte("password"),
			opts: &pkcs8.Opts{
				Cipher: pkcs.DESCBC,
				KDFOpts: pkcs8.PBKDF2Opts{
					SaltSize: 16, IterationCount: 16, HMACHash: pkcs8.SHA1,
				},
			},
		},
		{
			password: []byte("password"),
			opts: &pkcs8.Opts{
				Cipher: pkcs.TripleDESCBC,
				KDFOpts: pkcs8.PBKDF2Opts{
					SaltSize: 16, IterationCount: 16, HMACHash: pkcs8.SHA1,
				},
			},
		},
		{
			password: []byte("password"),
			opts: &pkcs8.Opts{
				Cipher: pkcs.AES256CBC,
				KDFOpts: pkcs8.ScryptOpts{
					CostParameter:            1 << 2,
					BlockSize:                8,
					ParallelizationParameter: 1,
					SaltSize:                 16,
				},
			},
		},
	} {
		rsaPrivateKey, err := rsa.GenerateKey(rand.Reader, 2048)
		if err != nil {
			t.Fatalf("%d: GenerateKey returned: %s", i, err)
		}
		der, err := pkcs8.MarshalPrivateKey(rsaPrivateKey, tt.password, tt.opts)
		if err != nil {
			t.Fatalf("%d: MarshalPrivateKey returned: %s", i, err)
		}
		decodedRSAPrivateKey, _, err := pkcs8.ParsePrivateKey(der, tt.password)
		if err != nil {
			t.Fatalf("%d: ParsePKCS8PrivateKey returned: %s", i, err)
		}
		if rsaPrivateKey.D.Cmp(decodedRSAPrivateKey.(*rsa.PrivateKey).D) != 0 {
			t.Fatalf("%d: Decoded key does not match original key", i)
		}

		sm2PrivateKey, err := sm2.GenerateKey(rand.Reader)
		if err != nil {
			t.Fatalf("%d: GenerateKey returned: %s", i, err)
		}

		der, err = pkcs8.MarshalPrivateKey(sm2PrivateKey, tt.password, tt.opts)
		if err != nil {
			t.Fatalf("%d: MarshalPrivateKey returned: %s", i, err)
		}

		decodedSM2PrivateKey, err := pkcs8.ParsePKCS8PrivateKeySM2(der, tt.password)
		if err != nil {
			t.Fatalf("%d: ParsePKCS8PrivateKey returned: %s", i, err)
		}
		if !sm2PrivateKey.Equal(decodedSM2PrivateKey) {
			t.Fatalf("%d: Decoded key does not match original key", i)
		}

		sm9SignMasterPrivateKey, err := sm9.GenerateSignMasterKey(rand.Reader)
		if err != nil {
			t.Fatalf("%d: GenerateKey returned: %s", i, err)
		}

		der, err = pkcs8.MarshalPrivateKey(sm9SignMasterPrivateKey, tt.password, tt.opts)
		if err != nil {
			t.Fatalf("%d: MarshalPrivateKey returned: %s", i, err)
		}
		decodedSM9SignMasterPrivateKey, err := pkcs8.ParseSM9SignMasterPrivateKey(der, tt.password)
		if err != nil {
			t.Fatalf("%d: ParseSM9SignMasterPrivateKey returned: %s", i, err)
		}

		_, err = pkcs8.ParseSM9SignPrivateKey(der, tt.password)
		if err == nil {
			t.Fatalf("%d: ParseSM9SignPrivateKey should return error", i)
		}

		if !sm9SignMasterPrivateKey.Equal(decodedSM9SignMasterPrivateKey) {
			t.Fatalf("%d: Decoded key does not match original key", i)
		}

		sm9SignPrivateKey, err := sm9SignMasterPrivateKey.GenerateUserKey([]byte("emmansun"), 0x01)
		if err != nil {
			t.Fatalf("%d: GenerateUserKey returned: %s", i, err)
		}
		der, err = pkcs8.MarshalPrivateKey(sm9SignPrivateKey, tt.password, tt.opts)
		if err != nil {
			t.Fatalf("%d: MarshalPrivateKey returned: %s", i, err)
		}
		decodedSM9SignPrivateKey, err := pkcs8.ParseSM9SignPrivateKey(der, tt.password)
		if err != nil {
			t.Fatalf("%d: ParseSM9SignPrivateKey returned: %s", i, err)
		}
		_, err = pkcs8.ParseSM9SignMasterPrivateKey(der, tt.password)
		if err == nil {
			t.Fatalf("%d: ParseSM9SignMasterPrivateKey should return error", i)
		}
		if !sm9SignPrivateKey.Equal(decodedSM9SignPrivateKey) {
			t.Fatalf("%d: Decoded key does not match original key", i)
		}

		sm9EncMasterPrivateKey, err := sm9.GenerateEncryptMasterKey(rand.Reader)
		if err != nil {
			t.Fatalf("%d: GenerateKey returned: %s", i, err)
		}

		der, err = pkcs8.MarshalPrivateKey(sm9EncMasterPrivateKey, tt.password, tt.opts)
		if err != nil {
			t.Fatalf("%d: MarshalPrivateKey returned: %s", i, err)
		}
		decodedSM9EncMasterPrivateKey, err := pkcs8.ParseSM9EncryptMasterPrivateKey(der, tt.password)
		if err != nil {
			t.Fatalf("%d: ParseSM9EncryptMasterPrivateKey returned: %s", i, err)
		}
		_, err = pkcs8.ParseSM9EncryptPrivateKey(der, tt.password)
		if err == nil {
			t.Fatalf("%d: ParseSM9EncryptPrivateKey should return error", i)
		}
		if !sm9EncMasterPrivateKey.Equal(decodedSM9EncMasterPrivateKey) {
			t.Fatalf("%d: Decoded key does not match original key", i)
		}

		sm9EncPrivateKey, err := sm9EncMasterPrivateKey.GenerateUserKey([]byte("emmansun"), 0x02)
		if err != nil {
			t.Fatalf("%d: GenerateUserKey returned: %s", i, err)
		}
		der, err = pkcs8.MarshalPrivateKey(sm9EncPrivateKey, tt.password, tt.opts)
		if err != nil {
			t.Fatalf("%d: MarshalPrivateKey returned: %s", i, err)
		}
		decodedSM9EncPrivateKey, err := pkcs8.ParseSM9EncryptPrivateKey(der, tt.password)
		if err != nil {
			t.Fatalf("%d: ParseSM9EncryptPrivateKey returned: %s", i, err)
		}
		_, err = pkcs8.ParseSM9EncryptMasterPrivateKey(der, tt.password)
		if err == nil {
			t.Fatalf("%d: ParseSM9EncryptMasterPrivateKey should return error", i)
		}
		if !sm9EncPrivateKey.Equal(decodedSM9EncPrivateKey) {
			t.Fatalf("%d: Decoded key does not match original key", i)
		}

		for _, curve := range []elliptic.Curve{
			elliptic.P224(), elliptic.P256(), elliptic.P384(), elliptic.P521(),
		} {
			ecPrivateKey, err := ecdsa.GenerateKey(curve, rand.Reader)
			if err != nil {
				t.Fatalf("%d, %s: ConvertPrivateKeyToPKCS8 returned: %s", i, curve, err)
			}
			der, err = pkcs8.MarshalPrivateKey(ecPrivateKey, tt.password, tt.opts)
			if err != nil {
				t.Fatalf("%d, %s: ConvertPrivateKeyToPKCS8 returned: %s", i, curve, err)
			}
			decodedECPrivateKey, _, err := pkcs8.ParsePrivateKey(der, tt.password)
			if err != nil {
				t.Fatalf("%d, %s: ParsePKCS8PrivateKey returned: %s", i, curve, err)
			}
			if ecPrivateKey.D.Cmp(decodedECPrivateKey.(*ecdsa.PrivateKey).D) != 0 {
				t.Fatalf("%d, %s: Decoded key does not match original key", i, curve)
			}
		}
	}
}

type unknown int

func TestUnknownTypeFailure(t *testing.T) {
	badInput := unknown(0)
	_, err := pkcs8.ConvertPrivateKeyToPKCS8(badInput, []byte("password"))
	if err == nil {
		t.Fatal("expected error")
	}
}

// for encrypted private-key information
type encryptedPrivateKeyInfo struct {
	EncryptionAlgorithm pkix.AlgorithmIdentifier
	EncryptedData       []byte
}

func TestParseInvalidPrivateKey(t *testing.T) {
	// test parse pem directly
	_, err := pkcs8.ParsePKCS8PrivateKeyECDSA([]byte(encryptedEC256aes), []byte("password"))
	if err == nil || err.Error() != "pkcs8: this method just supports DER-encoded key" {
		t.Errorf("should be error: pkcs8: this method just supports DER-encoded key")
	}
	_, err = pkcs8.ParsePKCS8PrivateKeyECDSA(nil, []byte("password"))
	if err == nil || err.Error() != "pkcs8: only PKCS #5 v2.0 supported" {
		t.Errorf("should be error: pkcs8: only PKCS #5 v2.0 supported")
	}

	var privKey encryptedPrivateKeyInfo
	privKey.EncryptionAlgorithm.Algorithm = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 5, 2}
	data, err := asn1.Marshal(privKey)
	if err != nil {
		t.Fatal(err)
	}
	_, err = pkcs8.ParsePKCS8PrivateKeyECDSA(data, []byte("password"))
	if err == nil || err.Error() != "pkcs8: only part of PBES1/PBES2 supported" {
		t.Errorf("should be error: only part of PBES1/PBES2 supported")
	}

	privKey.EncryptionAlgorithm.Algorithm = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 5, 13}
	data, err = asn1.Marshal(privKey)
	if err != nil {
		t.Fatal(err)
	}
	_, err = pkcs8.ParsePKCS8PrivateKeyECDSA(data, []byte("password"))
	if err == nil || err.Error() != "pkcs8: invalid PBES2 parameters" {
		t.Errorf("should be error: pkcs8: invalid PBES2 parameters")
	}
}

func TestParseLegacyPBES1PrivateKey(t *testing.T) {
	block, _ := pem.Decode([]byte(encryptedPBEWithMD5AndDES))
	_, err := pkcs8.ParsePKCS8PrivateKey(block.Bytes, []byte("12345678"))
	if err != nil {
		t.Errorf("ParsePKCS8PrivateKey returned: %s", err)
	}

	block, _ = pem.Decode([]byte(encyptedPBEWithSha1AndDES))
	_, err = pkcs8.ParsePKCS8PrivateKey(block.Bytes, []byte("12345678"))
	if err != nil {
		t.Errorf("ParsePKCS8PrivateKey returned: %s", err)
	}

	block, _ = pem.Decode([]byte(encryptedPBEWithSha1AndRC2_64))
	_, err = pkcs8.ParsePKCS8PrivateKey(block.Bytes, []byte("12345678"))
	if err != nil {
		t.Errorf("ParsePKCS8PrivateKey returned: %s", err)
	}

	block, _ = pem.Decode([]byte(encryptedPBEWithMD5AndRC2_40))
	_, err = pkcs8.ParsePKCS8PrivateKey(block.Bytes, []byte("12345678"))
	if err != nil {
		t.Errorf("ParsePKCS8PrivateKey returned: %s", err)
	}

	_, err = pkcs8.ParsePKCS8PrivateKey(block.Bytes, []byte("wrong pwd"))
	if err != pkcs.ErrPBEDecryption {
		t.Errorf("should have failed")
	}
}

func TestShangMiPBES(t *testing.T) {
	block, _ := pem.Decode([]byte(encryptedPBEWithMD5AndDES))
	priv, err := pkcs8.ParsePKCS8PrivateKey(block.Bytes, []byte("12345678"))
	if err != nil {
		t.Errorf("ParsePKCS8PrivateKey returned: %s", err)
	}

	der, err := pkcs8.MarshalPrivateKey(priv, []byte("12345678"), pkcs.NewSMPBESEncrypter(16, 2048))
	if err != nil {
		t.Fatalf("MarshalPrivateKey returned: %s", err)
	}
	_, _, err = pkcs8.ParsePrivateKey(der, []byte("12345678"))
	if err != nil {
		t.Fatalf("ParsePrivateKey returned: %s", err)
	}
}
