xmlsec-ruby 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,6 +6,8 @@
6
6
  #include <libxml/tree.h>
7
7
  #include <libxml/xmlmemory.h>
8
8
  #include <libxml/parser.h>
9
+ #include <libxml/xpath.h>
10
+ #include <libxml/xpathInternals.h>
9
11
 
10
12
  #include <xmlsec/xmlsec.h>
11
13
  #include <xmlsec/xmltree.h>
@@ -21,63 +23,56 @@ void cleanup(xmlSecDSigCtxPtr dsigCtx) ;
21
23
  int verify_document(xmlDocPtr doc, const char* key);
22
24
  int verify_file(const char* xmlMessage, const char* key);
23
25
  void xmlSecErrorCallback(const char* file, int line, const char* func, const char* errorObject, const char* errorSubject, int reason, const char* msg);
24
- static int
25
- xmlSecAppAddIDAttr(xmlNodePtr node, const xmlChar* attrName, const xmlChar* nodeName, const xmlChar* nsHref) {
26
- xmlAttrPtr attr, tmpAttr;
27
- xmlNodePtr cur;
28
- xmlChar* id;
29
-
30
- if((node == NULL) || (attrName == NULL) || (nodeName == NULL)) {
31
- return(-1);
32
- }
33
-
34
- /* process children first because it does not matter much but does simplify code */
35
- cur = xmlSecGetNextElementNode(node->children);
36
- while(cur != NULL) {
37
- if(xmlSecAppAddIDAttr(cur, attrName, nodeName, nsHref) < 0) {
38
- return(-1);
39
- }
40
- cur = xmlSecGetNextElementNode(cur->next);
41
- }
42
-
43
- /* node name must match */
44
- if(!xmlStrEqual(node->name, nodeName)) {
45
- return(0);
46
- }
47
-
48
- /* if nsHref is set then it also should match */
49
- if((nsHref != NULL) && (node->ns != NULL) && (!xmlStrEqual(nsHref, node->ns->href))) {
50
- return(0);
51
- }
52
-
53
- /* the attribute with name equal to attrName should exist */
54
- for(attr = node->properties; attr != NULL; attr = attr->next) {
55
- if(xmlStrEqual(attr->name, attrName)) {
56
- break;
57
- }
58
- }
59
- if(attr == NULL) {
60
- return(0);
61
- }
62
-
63
- /* and this attr should have a value */
64
- id = xmlNodeListGetString(node->doc, attr->children, 1);
65
- if(id == NULL) {
66
- return(0);
67
- }
68
-
69
- /* check that we don't have same ID already */
70
- tmpAttr = xmlGetID(node->doc, id);
71
- if(tmpAttr == NULL) {
72
- xmlAddID(NULL, node->doc, id, attr);
73
- } else if(tmpAttr != attr) {
74
- fprintf(stderr, "Error: duplicate ID attribute \"%s\"\n", id);
75
- xmlFree(id);
76
- return(-1);
77
- }
78
- xmlFree(id);
79
- return(0);
26
+ int assign_id_attributes(xmlDocPtr doc) {
27
+ // Assume the ID attribute is one of (ID | Id | id) and tell this to libxml
28
+ xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc);
29
+ if(xpathCtx == NULL) {
30
+ xmlFreeDoc(doc);
31
+ rb_raise(rb_eRuntimeError,"Error: unable to create new XPath context\n");
32
+ return(-1);
33
+ }
34
+ xmlChar* xpathExpr = "//*[@ID | @Id | @id]";
35
+
36
+ xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx);
37
+ if(xpathObj == NULL) {
38
+ xmlXPathFreeContext(xpathCtx);
39
+ xmlFreeDoc(doc);
40
+ rb_raise(rb_eRuntimeError,"Error: unable to evaluate xpath expression \"%s\"\n", xpathExpr);
41
+ return(-1);
42
+ }
43
+ xmlNodeSetPtr nodes = xpathObj->nodesetval;
44
+ int size = (nodes) ? nodes->nodeNr : 0;
45
+ char* idNames[] = {"ID", "Id", "id"};
46
+ xmlAttrPtr attr, tmp;
47
+ int i,j;
48
+ for(i = 0; i < size; i++) {
49
+ for(j=0; j<3;j++) {
50
+ tmp = xmlHasProp(nodes->nodeTab[i], idNames[j]);
51
+ if(tmp != NULL)
52
+ attr = tmp;
53
+ }
54
+ if(attr == NULL) {
55
+ xmlXPathFreeContext(xpathCtx);
56
+ return(-1);
57
+ }
58
+ xmlChar* name = xmlNodeListGetString(doc, attr->children, 1);
59
+ if(name == NULL) {
60
+ xmlXPathFreeContext(xpathCtx);
61
+ return(-1);
62
+ }
63
+ xmlAttrPtr tmp = xmlGetID(doc, name);
64
+ if(tmp != NULL) {
65
+ xmlFree(name);
66
+ return 0;
67
+ }
68
+ xmlAddID(NULL, doc, name, attr);
69
+ xmlFree(name);
70
+ }
71
+
72
+ xmlXPathFreeObject(xpathObj);
73
+ xmlXPathFreeContext(xpathCtx);
80
74
  }
75
+
81
76
 
82
77
  /* functions */
83
78
  int verify_file(const char* xmlMessage, const char* key) {
@@ -98,25 +93,19 @@ int verify_document(xmlDocPtr doc, const char* key) {
98
93
  int res = 0;
99
94
 
100
95
  if ((doc == NULL) || (xmlDocGetRootElement(doc) == NULL)){
101
- cleanup(dsigCtx);
102
96
  rb_raise(rb_eRuntimeError, "unable to parse XML document");
103
97
  }
104
98
 
105
99
  /* find start node */
106
100
  node = xmlSecFindNode(xmlDocGetRootElement(doc), xmlSecNodeSignature, xmlSecDSigNs);
107
101
  if(node == NULL) {
108
- cleanup(dsigCtx);
109
102
  rb_raise(rb_eRuntimeError, "could not find start node in XML document");
110
103
  }
111
104
 
112
- xmlNodePtr cur = xmlSecGetNextElementNode(doc->children);
113
- while(cur != NULL) {
114
- if(xmlSecAppAddIDAttr(cur, "ID", "Response", "urn:oasis:names:tc:SAML:2.0:protocol") < 0) {
115
- cleanup(dsigCtx);
116
- rb_raise(rb_eRuntimeError, "could not define ID attribute");
117
- }
118
- cur = xmlSecGetNextElementNode(cur->next);
105
+ if(assign_id_attributes(doc) < 0) {
106
+ rb_raise(rb_eRuntimeError, "Could not find ID attribute in document");
119
107
  }
108
+
120
109
 
121
110
  /* create signature context */
122
111
  dsigCtx = xmlSecDSigCtxCreate(NULL);
@@ -1,6 +1,6 @@
1
1
  /* ----------------------------------------------------------------------------
2
2
  * This file was automatically generated by SWIG (http://www.swig.org).
3
- * Version 2.0.0
3
+ * Version 2.0.1
4
4
  *
5
5
  * This file is not intended to be easily readable and contains a number of
6
6
  * coding conventions designed to improve portability and efficiency. Do not make
@@ -281,7 +281,7 @@
281
281
  /*
282
282
  Flags/methods for returning states.
283
283
 
284
- The SWIG conversion methods, as ConvertPtr, return and integer
284
+ The SWIG conversion methods, as ConvertPtr, return an integer
285
285
  that tells if the conversion was successful or not. And if not,
286
286
  an error code can be returned (see swigerrors.swg for the codes).
287
287
 
@@ -998,7 +998,7 @@ static VALUE _mSWIG = Qnil;
998
998
 
999
999
  /* Define custom exceptions for errors that do not map to existing Ruby
1000
1000
  exceptions. Note this only works for C++ since a global cannot be
1001
- initialized by a funtion in C. For C, fallback to rb_eRuntimeError.*/
1001
+ initialized by a function in C. For C, fallback to rb_eRuntimeError.*/
1002
1002
 
1003
1003
  SWIGINTERN VALUE
1004
1004
  getNullReferenceError(void) {
@@ -1807,7 +1807,7 @@ static VALUE mXmlsec;
1807
1807
  #define SWIG_RUBY_THREAD_END_BLOCK
1808
1808
 
1809
1809
 
1810
- #define SWIGVERSION 0x020000
1810
+ #define SWIGVERSION 0x020001
1811
1811
  #define SWIG_VERSION SWIGVERSION
1812
1812
 
1813
1813
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xmlsec-ruby
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 5
10
- version: 0.0.5
9
+ - 6
10
+ version: 0.0.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Victor Lin
@@ -15,11 +15,11 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-21 00:00:00 -07:00
18
+ date: 2010-12-14 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
22
- description: "\txmlsec-ruby is project using SWIG to create ruby bindings\n\
22
+ description: "\txmlsec-ruby is a project using SWIG to create ruby bindings\n\
23
23
  \tfor the xmlsec library (http://www.aleksey.com/xmlsec/). \n"
24
24
  email: victor@coupa.com
25
25
  executables: []