parse

package
v0.80.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 2, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Cmd = &cobra.Command{
	Use:   "pem-to-pfx",
	Short: "Convert PEM (cer/key) to PFX",
	Args:  cobra.NoArgs,
	Run: func(c *cobra.Command, args []string) {
		tlsCert, err := tls.LoadX509KeyPair(FlagInputCert, FlagInputKey)
		if err != nil {
			log.Fatal(err)
		}
		cert, err := x509.ParseCertificate(tlsCert.Certificate[0])
		if err != nil {
			log.Fatal(err)
		}

		var caCerts []*x509.Certificate
		if FlagInputCaCert != "" {
			f, err := os.ReadFile(FlagInputCaCert)
			if err != nil {
				log.Fatal(err)
			}
			block, _ := pem.Decode(f)
			if block == nil {
				panic("failed to parse certificate PEM")
			}
			ca, err := x509.ParseCertificate(block.Bytes)
			if err != nil {
				log.Fatal(err)
			}
			caCerts = append(caCerts, ca)
		}

		pfxBytes, err := pkcs12.Encode(rand.Reader, tlsCert.PrivateKey, cert, caCerts, FlagPassword)

		if err != nil {
			log.Fatal(err)
		}

		_, _, _, err = pkcs12.DecodeChain(pfxBytes, pkcs12.DefaultPassword)
		if err != nil {
			log.Fatal(err)
		}

		err = os.WriteFile(
			FlagOutput,
			pfxBytes,
			os.ModePerm,
		)
		if err != nil {
			log.Fatal(err)
		}
	},
}
View Source
var FlagInputCaCert string
View Source
var FlagInputCert string
View Source
var FlagInputKey string
View Source
var FlagOutput string
View Source
var FlagPassword string

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL