Exemple #1
0
func (m Metadata) Serialize() (string, error) {
	doc := dom.CreateDocument()
	defer doc.Free()

	root, err := m.MakeXMLNode(doc)
	if err != nil {
		return "", err
	}

	doc.SetDocumentElement(root)
	return doc.Dump(true), nil
}
Exemple #2
0
func serialize(n MakeXMLNoder) (string, error) {
	d := dom.CreateDocument()
	defer d.Free()
	root, err := n.MakeXMLNode(d)
	if err != nil {
		return "", err
	}
	// note: no need to gc the root separately, as it's done by
	// d.Free()
	if err := d.SetDocumentElement(root); err != nil {
		return "", err
	}
	return dom.C14NSerialize{}.Serialize(d)
}
Exemple #3
0
func (key *RSA) Serialize() (string, error) {
	doc := dom.CreateDocument()
	defer doc.AutoFree()
	doc.MakeMortal()

	root, err := key.MakeXMLNode(doc)
	if err != nil {
		return "", err
	}
	doc.SetDocumentElement(root)

	doc.MakePersistent()

	return doc.Dump(true), nil
}
Exemple #4
0
func TestSignature(t *testing.T) {
	xmlsec.Init()
	defer xmlsec.Shutdown()

	doc := dom.CreateDocument()
	defer doc.Free()

	message, err := doc.CreateElement("Message")
	if !assert.NoError(t, err, "CreateElement succeeds") {
		return
	}
	doc.SetDocumentElement(message)

	data, err := doc.CreateElement("Data")
	if !assert.NoError(t, err, "CreateElement succeeds") {
		return
	}
	message.AddChild(data)
	data.AppendText("Hello, World!")

	sig, err := dsig.NewSignature(message, dsig.ExclC14N, dsig.RsaSha1, "")
	if !assert.NoError(t, err, "NewSignature succeeds") {
		return
	}

	if !assert.NoError(t, sig.AddReference(dsig.Sha1, "", "", ""), "AddReference succeeds") {
		return
	}

	if !assert.NoError(t, sig.AddTransform(dsig.Enveloped), "AddTransform succeeds") {
		return
	}

	if !assert.NoError(t, sig.AddKeyValue(), "AddKeyValue succeeds") {
		return
	}

	if !assert.NoError(t, sig.AddX509Data(), "AddX509Data succeeds") {
		return
	}

	keyfile := filepath.Join("test", "key.pem")
	certfile := filepath.Join("test", "cert.pem")
	key, err := crypto.LoadKeyFromFile(keyfile, crypto.KeyDataFormatPem)
	if !assert.NoError(t, err, "Load key from file succeeds") {
		return
	}

	key.LoadCertFromFile(certfile, crypto.KeyDataFormatPem)

	if !assert.NoError(t, sig.Sign(key), "Sign succeeds") {
		return
	}

	t.Logf("%s", doc.Dump(true))

	verify, err := dsig.NewSignatureVerify()
	if !assert.NoError(t, err, "NewSignatureVerify succeeds") {
		return
	}

	if !assert.NoError(t, verify.VerifyString(doc.Dump(false)), "VerifyString succeeds") {
		return
	}
}