github.com/jasoet/gopki@v1.15.0/internal/crypto/ed25519_pkcs7_simple.go (about)

     1  package crypto
     2  
     3  import (
     4  	"crypto/ed25519"
     5  	"crypto/x509"
     6  	"encoding/hex"
     7  	"fmt"
     8  )
     9  
    10  // SimpleEd25519PKCS7 creates a minimal working Ed25519 PKCS#7 signature for testing
    11  func SimpleEd25519PKCS7(data []byte, privateKey ed25519.PrivateKey, certificate *x509.Certificate) ([]byte, error) {
    12  	// For now, let's create a minimal structure that we can verify works
    13  	// This is a temporary implementation to test the integration
    14  
    15  	if len(privateKey) != ed25519.PrivateKeySize {
    16  		return nil, fmt.Errorf("invalid Ed25519 private key size")
    17  	}
    18  
    19  	if certificate == nil {
    20  		return nil, fmt.Errorf("certificate is required")
    21  	}
    22  
    23  	// Create Ed25519 signature
    24  	signature := ed25519.Sign(privateKey, data)
    25  
    26  	// Create a simple PKCS#7-like structure for testing
    27  	// This is a placeholder that embeds the signature and certificate
    28  	result := make([]byte, 0, len(signature)+len(certificate.Raw)+100)
    29  
    30  	// Add a simple header to identify this as our Ed25519 PKCS#7
    31  	header := []byte("ED25519-PKCS7-V1:")
    32  	result = append(result, header...)
    33  
    34  	// Add certificate length and certificate
    35  	certLen := len(certificate.Raw)
    36  	result = append(result, byte(certLen>>24), byte(certLen>>16), byte(certLen>>8), byte(certLen))
    37  	result = append(result, certificate.Raw...)
    38  
    39  	// Add signature length and signature
    40  	sigLen := len(signature)
    41  	result = append(result, byte(sigLen>>24), byte(sigLen>>16), byte(sigLen>>8), byte(sigLen))
    42  	result = append(result, signature...)
    43  
    44  	return result, nil
    45  }
    46  
    47  // VerifySimpleEd25519PKCS7 verifies our simple Ed25519 PKCS#7 signature
    48  func VerifySimpleEd25519PKCS7(data []byte, pkcs7Data []byte) (*Ed25519PKCS7Info, error) {
    49  	header := []byte("ED25519-PKCS7-V1:")
    50  	if len(pkcs7Data) < len(header) {
    51  		return nil, fmt.Errorf("invalid PKCS#7 data: too short")
    52  	}
    53  
    54  	// Check header
    55  	if string(pkcs7Data[:len(header)]) != string(header) {
    56  		return nil, fmt.Errorf("not a simple Ed25519 PKCS#7 signature")
    57  	}
    58  
    59  	offset := len(header)
    60  
    61  	// Read certificate length
    62  	if len(pkcs7Data) < offset+4 {
    63  		return nil, fmt.Errorf("invalid PKCS#7 data: missing certificate length")
    64  	}
    65  	certLen := int(pkcs7Data[offset])<<24 | int(pkcs7Data[offset+1])<<16 | int(pkcs7Data[offset+2])<<8 | int(pkcs7Data[offset+3])
    66  	offset += 4
    67  
    68  	// Read certificate
    69  	if len(pkcs7Data) < offset+certLen {
    70  		return nil, fmt.Errorf("invalid PKCS#7 data: missing certificate")
    71  	}
    72  	certData := pkcs7Data[offset : offset+certLen]
    73  	offset += certLen
    74  
    75  	cert, err := x509.ParseCertificate(certData)
    76  	if err != nil {
    77  		return nil, fmt.Errorf("failed to parse certificate: %w", err)
    78  	}
    79  
    80  	// Read signature length
    81  	if len(pkcs7Data) < offset+4 {
    82  		return nil, fmt.Errorf("invalid PKCS#7 data: missing signature length")
    83  	}
    84  	sigLen := int(pkcs7Data[offset])<<24 | int(pkcs7Data[offset+1])<<16 | int(pkcs7Data[offset+2])<<8 | int(pkcs7Data[offset+3])
    85  	offset += 4
    86  
    87  	// Read signature
    88  	if len(pkcs7Data) < offset+sigLen {
    89  		return nil, fmt.Errorf("invalid PKCS#7 data: missing signature")
    90  	}
    91  	signature := pkcs7Data[offset : offset+sigLen]
    92  
    93  	// Verify signature
    94  	ed25519PubKey, ok := cert.PublicKey.(ed25519.PublicKey)
    95  	if !ok {
    96  		return nil, fmt.Errorf("certificate does not contain Ed25519 public key")
    97  	}
    98  
    99  	if !ed25519.Verify(ed25519PubKey, data, signature) {
   100  		return nil, fmt.Errorf("signature verification failed")
   101  	}
   102  
   103  	return &Ed25519PKCS7Info{
   104  		Certificate: cert,
   105  		Signature:   signature,
   106  		Verified:    true,
   107  	}, nil
   108  }
   109  
   110  // IsSimpleEd25519PKCS7 checks if data is our simple format
   111  func IsSimpleEd25519PKCS7(pkcs7Data []byte) bool {
   112  	header := []byte("ED25519-PKCS7-V1:")
   113  	if len(pkcs7Data) < len(header) {
   114  		return false
   115  	}
   116  	return string(pkcs7Data[:len(header)]) == string(header)
   117  }
   118  
   119  // DebugPKCS7Structure helps debug PKCS#7 structures
   120  func DebugPKCS7Structure(data []byte) string {
   121  	if len(data) > 100 {
   122  		return fmt.Sprintf("PKCS#7 data (%d bytes): %s...", len(data), hex.EncodeToString(data[:100]))
   123  	}
   124  	return fmt.Sprintf("PKCS#7 data (%d bytes): %s", len(data), hex.EncodeToString(data))
   125  }