drqwbh2150 2019-03-17 19:50
浏览 139

来自PHP的2C2P PKC7解密 - > Ruby

I'm trying to convert a bit of PHP code to Ruby. The code snippet is located here: https://developer.2c2p.com/docs/read-payment-response-1 (located at the bottom of the page) and I'm trying to understand how I'd go about implementing this in Ruby. I've got as far as:

      payment_response = params.dig(:paymentResponse)
      cert_store = OpenSSL::X509::Store.new
      my_cert =  OpenSSL::X509::Certificate.new(File.read('config/twoctwop_keys/demo2.crt'))
      signature = OpenSSL::PKCS7.new(File.read('config/twoctwop_keys/demo2.pem'))
      signature.verify([my_cert], cert_store, payment_response, OpenSSL::PKCS7::NOVERIFY)
      signature.data

However with the above I seem to be getting the following error:

ArgumentError: Could not parse the PKCS7: nested asn1 error

from the following line:

OpenSSL::PKCS7.new(File.read('config/twoctwop_keys/demo2.pem'))

Without pasting the contents of the demo2.pem file it seems to contain multiple certificates since I can see:

Bag Attributes
    Microsoft Local Key set: <No Values>
    localKeyID: 01 00 00 00
    friendlyName: omitted
    Microsoft CSP Name: Microsoft RSA SChannel Cryptographic Provider
Key Attributes
    X509v3 Key Usage: 10
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,3A2DD8162BC67378
   ..... key......
-----END RSA PRIVATE KEY-----
Bag Attributes
    localKeyID: 01 00 00 00
    friendlyName: demo2.2c2p.com
subject=/C=SG/ST=Singapore/L=Singapore/O=xx Pte Ltd./OU=xxx IT/CN=xxx
issuer=xxxx
-----BEGIN CERTIFICATE-----
   ....key....
-----END CERTIFICATE-----
Bag Attributes: <Empty Attributes>
subject=/CN=SinaptIQ CA
issuer=/CN=SinaptIQ CA
-----BEGIN CERTIFICATE-----
  ....key....
-----END CERTIFICATE-----

Is there something that I'm not understanding here.

  • 写回答

1条回答 默认 最新

  • douzhanjia0773 2019-03-17 20:00
    关注

    It's as simple as doing:

    payment_response = params.dig(:paymentResponse)
    certificate = OpenSSL::X509::Certificate.new(File.read('config/twoctwop_keys/demo2.crt'))
    private_key = OpenSSL::PKey::RSA.new(File.read('config/twoctwop_keys/demo2.pem'), <passphrase>)
    body = OpenSSL::PKCS7.new(Base64.strict_decode64(payment_response))
    Hashie::Mash.new(Hash.from_xml(@body.decrypt(@private_key, @certificate))['PaymentResponse'])
    
    
    评论

报告相同问题?