// ParsePKCS8PrivateKey parses an unencrypted, PKCS#8 private key.
// See RFC 5208.
ParsePKCS8PrivateKey parses an unencrypted, PKCS#8 private key.
// ParsePKCS8PrivateKey parses an unencrypted PKCS#8 private key . // See RFC 5208 .
func ParsePKCS8PrivateKey(der []byte) (key interface{}, err error) {
	var privKey pkcs8
	if _, err := asn1.Unmarshal(der, &privKey); err != nil {
		return nil, err
	}
	switch {
	case privKey.Algo.Algorithm.Equal(oidPublicKeyRSA):
		key, err = ParsePKCS1PrivateKey(privKey.PrivateKey)
		if err != nil {
			return nil, errors.New("x509: failed to parse RSA private key embedded in PKCS#8: " + err.Error())
		}
		return key, nil
	case privKey.Algo.Algorithm.Equal(oidPublicKeyECDSA):
		bytes := privKey.Algo.Parameters.FullBytes
		namedCurveOID := new(asn1.ObjectIdentifier)
		if _, err := asn1.Unmarshal(bytes, namedCurveOID); err != nil {
			namedCurveOID = nil
		}
		if namedCurveOID.Equal(oidNamedCurveSm2) {
			key, err = parseSM2PrivateKey(namedCurveOID, privKey.PrivateKey)
			if err != nil {
				return nil, errors.New("x509: failed to parse sm2 private key embedded in PKCS#8: " + err.Error())
			}
		} else {
			key, err = parseECPrivateKey(namedCurveOID, privKey.PrivateKey)
			if err != nil {
				return nil, errors.New("x509: failed to parse EC private key embedded in PKCS#8: " + err.Error())
			}
		}
		return key, nil
	default:
		return nil, fmt.Errorf("x509: PKCS#8 wrapping contained private key with unknown algorithm: %v", privKey.Algo.Algorithm)
	}
}
func ParsePKCS8PrivateKey ( der [ ] byte ) ( key interface { } , err error ) { var privKey pkcs8 if _ , err := asn1 . Unmarshal ( der , & privKey ) ; err != nil { return nil , err } switch { case privKey . Algo . Algorithm . Equal ( oidPublicKeyRSA ) : key , err = ParsePKCS1PrivateKey ( privKey . PrivateKey ) if err != nil { return nil , errors . New ( " " + err . Error ( ) ) } return key , nil case privKey . Algo . Algorithm . Equal ( oidPublicKeyECDSA ) : bytes := privKey . Algo . Parameters . FullBytes namedCurveOID := new ( asn1 . ObjectIdentifier ) if _ , err := asn1 . Unmarshal ( bytes , namedCurveOID ) ; err != nil { namedCurveOID = nil } if namedCurveOID . Equal ( oidNamedCurveSm2 ) { key , err = parseSM2PrivateKey ( namedCurveOID , privKey . PrivateKey ) if err != nil { return nil , errors . New ( " " + err . Error ( ) ) } } else { key , err = parseECPrivateKey ( namedCurveOID , privKey . PrivateKey ) if err != nil { return nil , errors . New ( " " + err . Error ( ) ) } } return key , nil default : return nil , fmt . Errorf ( " " , privKey . Algo . Algorithm ) } }
ParsePKCS8PrivateKey parses an unencrypted, PKCS#8 private key.
ParsePKCS8PrivateKey parses an unencrypted PKCS#8 private key .