fix: improve certificate parsing and file extensions

- Fix PKCS12 container parsing by using consistent password
- Add proper file extensions for certificate downloads (.key, .csr, .crt)
- Improve private key parsing by removing unnecessary Base64 decoding
- Add error logging for certificate parsing failures
- Clean up certificate serializer code

The main changes include:
- Using P12_PASSWORD consistently across generation and parsing
- Adding proper file extensions for different certificate types
- Fixing private key parsing to handle PEM format correctly
- Adding detailed error logging for debugging purposes
- Removing redundant code comments and improving code clarity

This commit improves the reliability of certificate handling
and provides better user experience with correct file extensions.
This commit is contained in:
oleghasjanov 2025-03-14 10:55:33 +02:00
parent 0925fa4d4b
commit fe90d787c2
4 changed files with 29 additions and 5 deletions

View file

@ -52,6 +52,25 @@ class Certificate < ApplicationRecord
@p_csr ||= OpenSSL::X509::Request.new(csr) if csr
end
def parsed_private_key
return nil if private_key.blank?
OpenSSL::PKey::RSA.new(private_key)
rescue OpenSSL::PKey::RSAError => e
Rails.logger.error("Failed to parse private key: #{e.message}")
nil
end
def parsed_p12
return nil if p12.blank?
decoded_p12 = Base64.decode64(p12)
OpenSSL::PKCS12.new(decoded_p12, Certificates::CertificateGenerator::P12_PASSWORD)
rescue OpenSSL::PKCS12::PKCS12Error => e
Rails.logger.error("Failed to parse PKCS12: #{e.message}")
nil
end
def revoked?
status == REVOKED
end