Class: FE::Signer

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

Constant Summary collapse

C14N =
"http://www.w3.org/TR/2001/REC-xml-c14n-20010315"
DSIG =
"http://www.w3.org/2000/09/xmldsig#"
NOKOGIRI_OPTIONS =
Nokogiri::XML::ParseOptions::STRICT | Nokogiri::XML::ParseOptions::NONET | Nokogiri::XML::ParseOptions::NOENT
RSA_SHA1 =
"http://www.w3.org/2000/09/xmldsig#rsa-sha1"
RSA_SHA256 =
"http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"
RSA_SHA384 =
"http://www.w3.org/2001/04/xmldsig-more#rsa-sha384"
RSA_SHA512 =
"http://www.w3.org/2001/04/xmldsig-more#rsa-sha512"
SHA1 =
"http://www.w3.org/2000/09/xmldsig#sha1"
SHA256 =
"http://www.w3.org/2001/04/xmlenc#sha256"
SHA384 =
"http://www.w3.org/2001/04/xmldsig-more#sha384"
SHA512 =
"http://www.w3.org/2001/04/xmlenc#sha512"
ENVELOPED_SIG =
"http://www.w3.org/2000/09/xmldsig#enveloped-signature"
INC_PREFIX_LIST =
"#default samlp saml ds xs xsi md"
NAMESPACES =
"#default ds xs xsi xades xsd"
XADES =
"http://uri.etsi.org/01903/v1.3.2#"
XADES141 =
"http://uri.etsi.org/01903/v1.4.1#"
SIGNATURE_POLICY =
"https://tribunet.hacienda.go.cr/docs/esquemas/2016/v4/Resolucion%20Comprobantes%20Electronicos%20%20DGT-R-48-2016.pdf"

Instance Method Summary collapse

Constructor Details

#initialize(key_path, key_password, input_xml, output_path = nil) ⇒ Signer

Returns a new instance of Signer.



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

def initialize(key_path, key_password,input_xml, output_path=nil)
  @doc = REXML::Document.new(File.read(input_xml))
  @doc.context[:attribute_quote] = :quote
  @doc << REXML::XMLDecl.new(REXML::XMLDecl::DEFAULT_VERSION,REXML::XMLDecl::DEFAULT_ENCODING, REXML::XMLDecl::DEFAULT_STANDALONE)
  @p12 = OpenSSL::PKCS12.new(File.read("tmp/pruebas.p12"),"8753")
  @x509 = @p12.certificate
  @output_path = output_path
end

Instance Method Details

#signObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/facturacr/signer/signer.rb', line 38

def sign
  #Build parts for Digest Calculation
  key_info = build_key_info_element
  signed_properties = build_signed_properties_element
  signed_info_element = build_signed_info_element(key_info,signed_properties)
  # Compute Signature
  signed_info_canon = canonicalize_document(signed_info_element)
  signature_value = compute_signature(@p12.key,algorithm(RSA_SHA256).new,signed_info_canon)
  
  # delete parts namespaces
  delete_namespaces(signed_info_element)
  delete_namespaces(key_info)
  delete_namespaces(signed_properties)
  
  # Created Signature element and add parts
  signature_element = REXML::Element.new("ds:Signature").add_namespace('ds', DSIG)
  signature_element.add_attribute("Id","xmldsig-#{uuid}")
  
  signature_element.add_element(signed_info_element)
  signature_element.add_element("ds:SignatureValue","Id"=>"xmldsig-#{uuid}-sigvalue").text = signature_value
  signature_element.add_element(key_info)
  
  object = signature_element.add_element("ds:Object")
  qualifying_properties = object.add_element("xades:QualifyingProperties", {"Target"=>"#xmldsig-#{uuid}"})
  qualifying_properties.add_namespace("xades", XADES)
  qualifying_properties.add_namespace("xades141", XADES141)
  
  qualifying_properties.add_element(signed_properties)
  
  @doc.root.add_element(signature_element)
  
  File.open(@output_path,"w"){|f| f.write(@doc.to_s)} if @output_path
  
  @doc
end