Exemplo n.º 1
0
// Sign the given context with the given private key - which is a PEM or hsm: key
// A hsm: key is a urn 'key' that points to a specific key/action in a goeleven interface to a HSM
// See https://github.com/wayf-dk/goeleven
func (xp *Xp) Sign(context *C.xmlNode, privatekey, pw, cert, algo string) (err error) {
	contextHash := Hash(algos[algo].algo, xp.C14n(context))
	contextDigest := base64.StdEncoding.EncodeToString(contextHash)
	signaturexml := `<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<ds:SignedInfo>
  <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
  <ds:SignatureMethod Algorithm=""/>
  <ds:Reference URI="">
    <ds:Transforms>
      <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
      <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
    </ds:Transforms>
    <ds:DigestMethod Algorithm=""/>
    <ds:DigestValue></ds:DigestValue>
  </ds:Reference>
</ds:SignedInfo>
<ds:SignatureValue></ds:SignatureValue>
</ds:Signature>`

	signature := C.xmlNewDocFragment(xp.doc)
	var res C.xmlNodePtr
	buf := ([]byte)(signaturexml)
	C.xmlParseBalancedChunkMemory(xp.doc, nil, nil, 0, (*C.xmlChar)(&buf[0]), &res)
	C.xmlAddChildList(signature, res)
	C.xmlAddNextSibling(C.xmlFirstElementChild(context), signature)

	id := xp.Query1(context, "@ID")

	signedInfo := xp.QueryDashP(signature, `ds:Signature/ds:SignedInfo[1]`, "", nil)
	xp.QueryDashP(signedInfo, `ds:SignatureMethod[1]/@Algorithm`, algos[algo].signature, nil)
	xp.QueryDashP(signedInfo, `ds:Reference/@URI`, "#"+id, nil)
	xp.QueryDashP(signedInfo, `ds:Reference/ds:DigestMethod[1]/@Algorithm`, algos[algo].digest, nil)
	xp.QueryDashP(signedInfo, `ds:Reference/ds:DigestValue[1]`, contextDigest, nil)

	signedInfoC14n := xp.C14n(signedInfo)
	digest := Hash(algos[algo].algo, signedInfoC14n)

	var signaturevalue []byte
	if strings.HasPrefix(privatekey, "hsm:") {
		signaturevalue, err = signGoEleven(digest, privatekey, algo)
	} else {
		signaturevalue, err = signGo(digest, privatekey, pw, algo)
	}
	signatureval := base64.StdEncoding.EncodeToString(signaturevalue)
	xp.QueryDashP(signature, `ds:Signature/ds:SignatureValue`, signatureval, nil)
	xp.QueryDashP(signature, `ds:Signature/ds:KeyInfo/ds:X509Data/ds:X509Certificate`, cert, nil)
	//	log.Println(xp.Pp())
	return
}
Exemplo n.º 2
0
// xmlFirstElementChild
func (node *Node) FirstChild() *Node {
	cnode := C.xmlFirstElementChild(node.Ptr)
	return makeNode(cnode)
}
Exemplo n.º 3
0
func (parent *C.xmlNode) FirstElementChild() (res *C.xmlNode) {
	res = C.xmlFirstElementChild(parent)
	return
}
Exemplo n.º 4
0
func XmlFirstElementChild(n unsafe.Pointer) unsafe.Pointer {
	return unsafe.Pointer(C.xmlFirstElementChild(C.xmlNodePtr(n)))
}