func (xmlNode *XmlNode) addNextSibling(node Node) (err error) { nodeType := node.NodeType() if nodeType == XML_DOCUMENT_NODE || nodeType == XML_HTML_DOCUMENT_NODE { err = ERR_CANNOT_MAKE_DUCMENT_AS_CHILD return } nodePtr := node.NodePtr() if xmlNode.NodePtr() == nodePtr { return } ret := xmlNode.isAccestor(nodePtr) if ret < 0 { return } else if ret == 0 { if !xmlNode.Document.RemoveUnlinkedNode(nodePtr) { C.xmlUnlinkNodeWithCheck((*C.xmlNode)(nodePtr)) } C.xmlAddNextSibling(xmlNode.Ptr, (*C.xmlNode)(nodePtr)) } else if ret > 0 { node.Remove() } /* childPtr := C.xmlAddNextSibling(xmlNode.Ptr, (*C.xmlNode)(nodePtr)) if nodeType == XML_TEXT_NODE && childPtr != (*C.xmlNode)(nodePtr) { //check the retured pointer //if it is not the text node just added, it means that the text node is freed because it has merged into other nodes //then we should invalid this node, because we do not want to have a dangling pointer //node.Remove() } */ return }
// 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 }
func (xmlNode *XmlNode) addNextSibling(node Node) (err error) { nodeType := node.NodeType() if nodeType == XML_DOCUMENT_NODE || nodeType == XML_HTML_DOCUMENT_NODE { err = ERR_CANNOT_MAKE_DUCMENT_AS_CHILD return } nodePtr := node.NodePtr() C.xmlUnlinkNode((*C.xmlNode)(nodePtr)) C.xmlAddNextSibling(xmlNode.Ptr, (*C.xmlNode)(nodePtr)) /* childPtr := C.xmlAddNextSibling(xmlNode.Ptr, (*C.xmlNode)(nodePtr)) if nodeType == XML_TEXT_NODE && childPtr != (*C.xmlNode)(nodePtr) { //check the retured pointer //if it is not the text node just added, it means that the text node is freed because it has merged into other nodes //then we should invalid this node, because we do not want to have a dangling pointer //node.Remove() } */ return }
func (xmlNode *XmlNode) addNextSibling(node Node) (err error) { nodeType := node.NodeType() if nodeType == XML_DOCUMENT_NODE || nodeType == XML_HTML_DOCUMENT_NODE { err = ERR_CANNOT_MAKE_DUCMENT_AS_CHILD return } nodePtr := node.NodePtr() if xmlNode.NodePtr() == nodePtr { return } ret := xmlNode.isAccestor(nodePtr) if ret < 0 { return } else if ret == 0 { if !xmlNode.Document.RemoveUnlinkedNode(nodePtr) { C.xmlUnlinkNodeWithCheck((*C.xmlNode)(nodePtr)) } C.xmlAddNextSibling(xmlNode.Ptr, (*C.xmlNode)(nodePtr)) } else if ret > 0 { node.Remove() } return }
// xmlAddNextSibling func (cur *Node) AddNextSibling(elem Node) *Node { return makeNode(C.xmlAddNextSibling(cur.Ptr, elem.Ptr)) }
// xmlAddChildList func (parent *Node) AddChildList(cur Node) *Node { return makeNode(C.xmlAddNextSibling(parent.Ptr, cur.Ptr)) }