Class: Passbook::Signer

Inherits:
Object
  • Object
show all
Defined in:
lib/passbook/signer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Signer

Returns a new instance of Signer.



8
9
10
11
12
13
14
# File 'lib/passbook/signer.rb', line 8

def initialize(params = {})
  @certificate = params[:certificate] || Passbook.p12_certificate
  @password    = params[:password] || Passbook.p12_password
  @key         = params[:key] || (params.empty? ? Passbook.p12_key : nil)
  @wwdc_cert   = params[:wwdc_cert] || Passbook.wwdc_cert
  compute_cert
end

Instance Attribute Details

#certificateObject

Returns the value of attribute certificate.



6
7
8
# File 'lib/passbook/signer.rb', line 6

def certificate
  @certificate
end

#keyObject

Returns the value of attribute key.



6
7
8
# File 'lib/passbook/signer.rb', line 6

def key
  @key
end

#key_hashObject

Returns the value of attribute key_hash.



6
7
8
# File 'lib/passbook/signer.rb', line 6

def key_hash
  @key_hash
end

#p12_certObject

Returns the value of attribute p12_cert.



6
7
8
# File 'lib/passbook/signer.rb', line 6

def p12_cert
  @p12_cert
end

#passwordObject

Returns the value of attribute password.



6
7
8
# File 'lib/passbook/signer.rb', line 6

def password
  @password
end

#wwdc_certObject

Returns the value of attribute wwdc_cert.



6
7
8
# File 'lib/passbook/signer.rb', line 6

def wwdc_cert
  @wwdc_cert
end

Instance Method Details

#compute_certObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/passbook/signer.rb', line 29

def compute_cert
  @key_hash = {}
  if key
    @key_hash[:key]  = OpenSSL::PKey::RSA.new File.read(key), password
    @key_hash[:cert] = OpenSSL::X509::Certificate.new File.read(certificate)
  else
    p12 = OpenSSL::PKCS12.new File.read(certificate), password
    @key_hash[:key], @key_hash[:cert] = p12.key, p12.certificate
  end
end

#sign(data) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/passbook/signer.rb', line 16

def sign(data)
  wwdc  = OpenSSL::X509::Certificate.new File.read(wwdc_cert)
  pk7   = OpenSSL::PKCS7.sign key_hash[:cert], key_hash[:key], data.to_s, [wwdc], OpenSSL::PKCS7::BINARY | OpenSSL::PKCS7::DETACHED
  data  = OpenSSL::PKCS7.write_smime pk7

  str_debut = "filename=\"smime.p7s\"\n\n"
  data      = data[data.index(str_debut)+str_debut.length..data.length-1]
  str_end   = "\n\n------"
  data      = data[0..data.index(str_end)-1]

  Base64.decode64(data)
end