Skip to content

crypto/x509: Go rejected the CRL file with version number 1. #77871

Description

@onepeople158

Go version

go version go1.24.2 linux/amd64

Output of go env in your module/workspace:

"Conversion failed: input file is not a valid CRL: failed to parse CRL data with x509: x509: unsupported crl version".

What did you do?

Hello developer,I used Go to convert a DER-formatted CRL file with version number 1 to PEM format, but Go returned the error: "Conversion failed: input file is not a valid CRL: failed to parse CRL data with x509: x509: unsupported crl version".

What did you see happen?

code:

package main

import (
	"crypto/x509"
	"encoding/pem"
	"errors"
	"fmt"
	"os"
	"path/filepath"
	"strings"
)

func main() {
	if len(os.Args) != 3 {
		fmt.Println("Usage: crl-converter <input-file> <output-file>")
		fmt.Println("Example:")
		fmt.Println("  Convert DER to PEM: crl-converter input.crl output.pem")
		fmt.Println("  Convert PEM to DER: crl-converter input.pem output.der")
		os.Exit(1)
	}

	inputPath := os.Args[1]
	outputPath := os.Args[2]

	if err := convertCRL(inputPath, outputPath); err != nil {
		fmt.Printf("Conversion failed: %v\n", err)
		os.Exit(1)
	}

	fmt.Printf("Successfully converted %s to %s\n", inputPath, outputPath)
}

// parseCrlToDer analisa os dados de entrada (PEM ou DER) e retorna os bytes DER brutos da CRL.
// Esta é a função central que usa o pacote x509 para validação.
func parseCrlToDer(crlData []byte) ([]byte, error) {
	// 1. Tenta decodificar como PEM. O formato PEM é apenas um invólucro para os dados DER.
	block, _ := pem.Decode(crlData)
	if block != nil {
		if block.Type == "X509 CRL" {
			// Se for um bloco PEM de CRL, usamos seus bytes (que são DER) para a análise final.
			crlData = block.Bytes
		} else {
			return nil, fmt.Errorf("invalid PEM block type: %s", block.Type)
		}
	}

	// 2. Neste ponto, crlData deve conter bytes DER.
	// Usamos x509.ParseRevocationList para validar que é uma CRL válida e para obter a estrutura.
	// Esta é a principal etapa de "usar o pacote x509".
	crl, err := x509.ParseRevocationList(crlData)
	if err != nil {
		return nil, fmt.Errorf("failed to parse CRL data with x509: %w", err)
	}

	// 3. Retorna os bytes DER brutos da CRL analisada e validada.
	return crl.Raw, nil
}

func convertCRL(inputPath, outputPath string) error {
	inputData, err := os.ReadFile(inputPath)
	if err != nil {
		return fmt.Errorf("reading input file: %w", err)
	}

	// Usa nossa função centralizada para analisar a entrada e obter os bytes DER.
	derBytes, err := parseCrlToDer(inputData)
	if err != nil {
		return fmt.Errorf("input file is not a valid CRL: %w", err)
	}

	outputExt := strings.ToLower(filepath.Ext(outputPath))
	var outputData []byte

	switch outputExt {
	case ".pem":
		// Para converter para PEM, codificamos os bytes DER validados.
		pemBlock := &pem.Block{
			Type:  "X509 CRL",
			Bytes: derBytes,
		}
		outputData = pem.EncodeToMemory(pemBlock)
	case ".der":
		// Para converter para DER, simplesmente usamos os bytes DER validados.
		outputData = derBytes
	default:
		return errors.New("output file must have .pem or .der extension")
	}

	if err := os.WriteFile(outputPath, outputData, 0644); err != nil {
		return fmt.Errorf("writing output file: %w", err)
	}

	return nil
}

What did you expect to see?

Test Case:
ca-11216.zip

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReportIssues describing a possible bug in the Go implementation.NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions