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.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/passbook/signer.rb', line 13

def initialize(params = {})

  # Path to your X509 cert. This is downloaded after generating
  # a certificate from your apple Pass Type ID on apple's developer site
  @certificate = params[:certificate] || Passbook.certificate

  # Path to the .pem file generated from public key of the RSA keypair
  # that was generated when you made a Certificate Signing Request
  # It'll be in your keychain under the "Common Name" you specified
  # for the signing request.
  @rsa_private_key         = params[:rsa_private_key] || Passbook.rsa_private_key

  # this should be the password that goes along with the rsa public key
  @password    = params[:password] || Passbook.password

  # "Apple Intermediate Certificate Worldwide Developer Relations" certificate
  # downloaded from here <https://www.apple.com/certificateauthority/>
  # Path to your Apple Intermediate Certificate Worldwide Developer Relations
  # cert.
  # downloaded from here https://www.apple.com/certificateauthority/
  # download that .cer file (binary)
  @apple_intermediate_cert   = params[:apple_intermediate_cert] || Passbook.apple_intermediate_cert
  compute_cert
end

Instance Attribute Details

#apple_intermediate_certObject

Returns the value of attribute apple_intermediate_cert.



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

def apple_intermediate_cert
  @apple_intermediate_cert
end

#certificateObject

Returns the value of attribute certificate.



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

def certificate
  @certificate
end

#key_hashObject (readonly)

Returns the value of attribute key_hash.



11
12
13
# File 'lib/passbook/signer.rb', line 11

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

#rsa_private_keyObject

Returns the value of attribute rsa_private_key.



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

def rsa_private_key
  @rsa_private_key
end

Instance Method Details

#compute_certObject



61
62
63
64
65
66
# File 'lib/passbook/signer.rb', line 61

def compute_cert
  @key_hash = {
    rsa_private_key: OpenSSL::PKey::RSA.new(file_data(rsa_private_key), password),
    certificate: OpenSSL::X509::Certificate.new(file_data(certificate))
  }
end

#file_data(data) ⇒ Object



68
69
70
71
72
73
# File 'lib/passbook/signer.rb', line 68

def file_data(data)
  raise "file_data passed nil" if data.nil?
  return data if data.is_a? String

  data.respond_to?(:read) ? data.read : File.read(data)
end

#sign(data) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/passbook/signer.rb', line 38

def sign(data)
  apple_cert  = OpenSSL::X509::Certificate.new file_data(apple_intermediate_cert)
  # In PKCS#7 SignedData, attached and detached formats are supported… In
  # detached format, data that is signed is not embedded inside the
  # SignedData package instead it is placed at some external location…

  pk7   = OpenSSL::PKCS7.sign(
    key_hash[:certificate],
    key_hash[:rsa_private_key],
    data.to_s,
    [apple_cert],
    OpenSSL::PKCS7::BINARY | OpenSSL::PKCS7::DETACHED
  )
  pk7_data  = OpenSSL::PKCS7.write_smime pk7

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

  Base64.decode64(pk7_data)
end