get rid of hacky newDoc2()
This commit is contained in:
@@ -48,7 +48,7 @@ func Decrypt(privateKey []byte, doc []byte) ([]byte, error) {
|
|||||||
return nil, popError()
|
return nil, popError()
|
||||||
}
|
}
|
||||||
|
|
||||||
parsedDoc, err := newDoc(doc)
|
parsedDoc, err := newDoc(doc, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -139,7 +139,7 @@ func Encrypt(publicKey, doc []byte, opts EncryptOptions) ([]byte, error) {
|
|||||||
return nil, mustPopError()
|
return nil, mustPopError()
|
||||||
}
|
}
|
||||||
|
|
||||||
parsedDoc, err := newDoc(doc)
|
parsedDoc, err := newDoc(doc, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -88,7 +88,7 @@ func (testSuite *EncryptTest) TestEncrypt(c *C) {
|
|||||||
actualPlaintext, err := Decrypt(testSuite.Key, encryptedString)
|
actualPlaintext, err := Decrypt(testSuite.Key, encryptedString)
|
||||||
c.Assert(err, IsNil)
|
c.Assert(err, IsNil)
|
||||||
|
|
||||||
plaintextDoc, _ := newDoc(testSuite.Plaintext)
|
plaintextDoc, _ := newDoc(testSuite.Plaintext, nil)
|
||||||
expectedPlaintext := dumpDoc(plaintextDoc)
|
expectedPlaintext := dumpDoc(plaintextDoc)
|
||||||
|
|
||||||
// Big blobs of XML are hard to debug. They are easier to handle when
|
// Big blobs of XML are hard to debug. They are easier to handle when
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ func Sign(key []byte, doc []byte, opts SignatureOptions) ([]byte, error) {
|
|||||||
return nil, errors.New("failed to load pem key")
|
return nil, errors.New("failed to load pem key")
|
||||||
}
|
}
|
||||||
|
|
||||||
parsedDoc, err := newDoc2(doc, opts)
|
parsedDoc, err := newDoc(doc, opts.XMLID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -134,7 +134,7 @@ func Verify(publicKey []byte, doc []byte, opts SignatureOptions) error {
|
|||||||
}
|
}
|
||||||
defer C.xmlSecDSigCtxDestroy(dsigCtx)
|
defer C.xmlSecDSigCtxDestroy(dsigCtx)
|
||||||
|
|
||||||
parsedDoc, err := newDoc2(doc, opts)
|
parsedDoc, err := newDoc(doc, opts.XMLID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
26
xmlsec.go
26
xmlsec.go
@@ -49,7 +49,7 @@ func init() {
|
|||||||
C.xmlSecErrorsSetCallback((C.xmlSecErrorsCallback)(unsafe.Pointer(C.onError_cgo)))
|
C.xmlSecErrorsSetCallback((C.xmlSecErrorsCallback)(unsafe.Pointer(C.onError_cgo)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func newDoc(buf []byte) (*C.xmlDoc, error) {
|
func newDoc(buf []byte, idattrs []XMLIDOption) (*C.xmlDoc, error) {
|
||||||
ctx := C.xmlCreateMemoryParserCtxt((*C.char)(unsafe.Pointer(&buf[0])),
|
ctx := C.xmlCreateMemoryParserCtxt((*C.char)(unsafe.Pointer(&buf[0])),
|
||||||
C.int(len(buf)))
|
C.int(len(buf)))
|
||||||
if ctx == nil {
|
if ctx == nil {
|
||||||
@@ -68,29 +68,7 @@ func newDoc(buf []byte) (*C.xmlDoc, error) {
|
|||||||
return nil, errors.New("parse failed")
|
return nil, errors.New("parse failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
return doc, nil
|
for _, idattr := range idattrs {
|
||||||
}
|
|
||||||
|
|
||||||
func newDoc2(buf []byte, opts SignatureOptions) (*C.xmlDoc, error) {
|
|
||||||
ctx := C.xmlCreateMemoryParserCtxt((*C.char)(unsafe.Pointer(&buf[0])),
|
|
||||||
C.int(len(buf)))
|
|
||||||
if ctx == nil {
|
|
||||||
return nil, errors.New("error creating parser")
|
|
||||||
}
|
|
||||||
defer C.xmlFreeParserCtxt(ctx)
|
|
||||||
|
|
||||||
C.xmlParseDocument(ctx)
|
|
||||||
|
|
||||||
if ctx.wellFormed == C.int(0) {
|
|
||||||
return nil, errors.New("malformed XML")
|
|
||||||
}
|
|
||||||
|
|
||||||
doc := ctx.myDoc
|
|
||||||
if doc == nil {
|
|
||||||
return nil, errors.New("parse failed")
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, idattr := range opts.XMLID {
|
|
||||||
addIDAttr(C.xmlDocGetRootElement(doc),
|
addIDAttr(C.xmlDocGetRootElement(doc),
|
||||||
idattr.AttributeName, idattr.ElementName, idattr.ElementNamespace)
|
idattr.AttributeName, idattr.ElementName, idattr.ElementNamespace)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user