Class: Wsaa::CmsSigner

Inherits:
Object
  • Object
show all
Defined in:
lib/wsaa/cms_signer.rb

Overview

Signs data using CMS/PKCS#7 format.

Creates cryptographic signatures compatible with WSAA requirements.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cert_path:, pkey_path:) ⇒ CmsSigner

Creates a new CmsSigner.

Parameters:

  • cert_path (String)

    Path to the certificate file in PEM format.

  • pkey_path (String)

    Path to the private key file in PEM format.

Raises:

  • (SigningError)

    If the certificate or key cannot be loaded.



21
22
23
24
# File 'lib/wsaa/cms_signer.rb', line 21

def initialize(cert_path:, pkey_path:)
  @certificate = load_certificate(cert_path)
  @private_key = load_private_key(pkey_path)
end

Instance Attribute Details

#certificateOpenSSL::X509::Certificate (readonly)

The signing certificate.

Returns:

  • (OpenSSL::X509::Certificate)

    the current value of certificate



12
13
14
# File 'lib/wsaa/cms_signer.rb', line 12

def certificate
  @certificate
end

#private_keyOpenSSL::PKey::RSA (readonly)

The private key for signing.

Returns:

  • (OpenSSL::PKey::RSA)

    the current value of private_key



12
13
14
# File 'lib/wsaa/cms_signer.rb', line 12

def private_key
  @private_key
end

Instance Method Details

#sign(data) ⇒ String

Signs data and returns the base64-encoded CMS signature.

Parameters:

  • data (String)

    The data to sign.

Returns:

  • (String)

    Base64-encoded PKCS#7/CMS signature.

Raises:



32
33
34
35
36
37
38
# File 'lib/wsaa/cms_signer.rb', line 32

def sign(data)
  flags = OpenSSL::PKCS7::BINARY | OpenSSL::PKCS7::NOSMIMECAP
  pkcs7 = OpenSSL::PKCS7.sign(certificate, private_key, data, [], flags)
  Base64.strict_encode64(pkcs7.to_der)
rescue OpenSSL::PKCS7::PKCS7Error => e
  raise SigningError, "Failed to sign data: #{e.message}"
end