@@ -84,34 +84,42 @@ def self.import_federal_data(authorization_code, _state_code)
8484 return
8585 end
8686
87+ decrypted_response = decrypt_response (
88+ cert_finder . client_key ,
89+ Base64 . decode64 ( response . header [ 'SESSION-KEY' ] ) ,
90+ Base64 . decode64 ( response . header [ 'INITIALIZATION-VECTOR' ] ) ,
91+ Base64 . decode64 ( JSON . parse ( response . body ) [ 'taxReturn' ] ) ,
92+ Base64 . decode64 ( response . header [ 'AUTHENTICATION-TAG' ] )
93+ )
94+
95+ decrypted_json = JSON . parse ( decrypted_response )
96+ decrypted_json [ 'xml' ] = Nokogiri ::XML ( decrypted_json [ 'xml' ] ) . to_xml
97+
98+ decrypted_json
99+ end
100+
101+ def self . decrypt_response ( private_key , encrypted_secret , initialization_vector , encrypted_data , authentication_tag = nil )
87102 decipher = OpenSSL ::Cipher . new ( 'aes-256-gcm' )
88103 decipher . decrypt
89- client_key = cert_finder . client_key
90- encrypted_session_key = Base64 . decode64 ( response . header [ 'SESSION-KEY' ] )
91104
92105 label = ''
93106 md_oaep = OpenSSL ::Digest ::SHA256
94107 md_mgf1 = OpenSSL ::Digest ::SHA1
95108
96- decipher . key = client_key . private_decrypt_oaep ( encrypted_session_key , label , md_oaep , md_mgf1 )
97- decipher . iv = Base64 . decode64 ( response . header [ 'INITIALIZATION-VECTOR' ] )
98- encrypted_tax_return_bytes = Base64 . decode64 ( JSON . parse ( response . body ) [ 'taxReturn' ] )
109+ decipher . key = private_key . private_decrypt_oaep ( encrypted_secret , label , md_oaep , md_mgf1 )
110+ decipher . iv = initialization_vector
99111
100112 if ENV [ 'IRS_API_LOCALHOST' ]
101- decipher . auth_tag = Base64 . decode64 ( response . header [ 'AUTHENTICATION-TAG' ] )
113+ decipher . auth_tag = authentication_tag
102114 else
103- char_array = encrypted_tax_return_bytes . unpack ( "C*" )
104- encrypted_tax_return_bytes = char_array [ 0 ..-17 ] . pack ( "C*" )
115+ char_array = encrypted_data . unpack ( "C*" )
116+ encrypted_data = char_array [ 0 ..-17 ] . pack ( "C*" )
105117 auth_tag = char_array . last ( 16 ) . pack ( "C*" )
106118
107119 decipher . auth_tag = auth_tag
108120 end
109- plain = decipher . update ( encrypted_tax_return_bytes ) + decipher . final
110-
111- decrypted_json = JSON . parse ( plain )
112- decrypted_json [ 'xml' ] = Nokogiri ::XML ( decrypted_json [ 'xml' ] ) . to_xml
113121
114- decrypted_json
122+ decipher . update ( encrypted_data ) + decipher . final
115123 end
116124
117125 private
0 commit comments