Exemplo n.º 1
0
func run(inputFilename string, outputFilename string, outputIntermediateOnly bool,
	outputDerFormat bool, includeSystem bool) error {
	inputFile, err := openInputFile(inputFilename)
	if err != nil {
		return err
	}

	outputFile, err := openOutputFile(outputFilename)
	if err != nil {
		return err
	}

	data, err := ioutil.ReadAll(inputFile)
	if err != nil {
		return err
	}

	cert, err := certUtil.DecodeCertificate(data)
	if err != nil {
		return err
	}

	certs, err := certUtil.FetchCertificateChain(cert)
	if err != nil {
		return err
	}

	if includeSystem {
		certs, err = certUtil.AddRootCA(certs)
		if err != nil {
			return err
		}
	}

	if outputIntermediateOnly {
		certs = certs[1:]
	}

	if !outputDerFormat {
		data = certUtil.EncodeCertificates(certs)
	} else {
		data = certUtil.EncodeCertificatesDER(certs)
	}

	_, err = outputFile.Write(data)
	if err != nil {
		return err
	}

	for i, cert := range certs {
		fmt.Fprintf(os.Stderr, "%d: %s\n", i+1, cert.Subject.CommonName)
	}
	fmt.Fprintf(os.Stderr, "Certificate chain complete.\n")
	fmt.Fprintf(os.Stderr, "Total %d certificate(s) found.\n", len(certs))

	return nil
}
Exemplo n.º 2
0
func (s *SSSL) Resolve(chainPath string) ([]byte, error) {
	logrus.Println("Incomplete certificate chain found, attempting to resolve this")
	b, err := ioutil.ReadFile(chainPath)
	if err != nil {
		return nil, err
	}

	cert, err := certUtil.DecodeCertificate(b)
	if err != nil {
		return nil, err
	}

	certs, err := certUtil.FetchCertificateChain(cert)
	if err != nil {
		return nil, err
	}

	return certUtil.EncodeCertificates(certs), nil
}