xmlsig 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/README.rdoc +0 -0
  2. data/ext/xmlsig/BioWrap.h +98 -0
  3. data/ext/xmlsig/DSig.cpp +109 -0
  4. data/ext/xmlsig/DSig.h +81 -0
  5. data/ext/xmlsig/DSigCtx.h +72 -0
  6. data/ext/xmlsig/Exceptions.cpp +151 -0
  7. data/ext/xmlsig/Exceptions.h +214 -0
  8. data/ext/xmlsig/Key.cpp +582 -0
  9. data/ext/xmlsig/Key.h +338 -0
  10. data/ext/xmlsig/KeyInfoCtx.h +67 -0
  11. data/ext/xmlsig/KeyStore.cpp +180 -0
  12. data/ext/xmlsig/KeyStore.h +157 -0
  13. data/ext/xmlsig/KeysMngrWrap.h +62 -0
  14. data/ext/xmlsig/NodeSet.h +60 -0
  15. data/ext/xmlsig/Signer.cpp +691 -0
  16. data/ext/xmlsig/Signer.h +373 -0
  17. data/ext/xmlsig/TrustVerifier.cpp +145 -0
  18. data/ext/xmlsig/TrustVerifier.h +174 -0
  19. data/ext/xmlsig/Verifier.cpp +677 -0
  20. data/ext/xmlsig/Verifier.h +313 -0
  21. data/ext/xmlsig/X509Certificate.cpp +362 -0
  22. data/ext/xmlsig/X509Certificate.h +146 -0
  23. data/ext/xmlsig/XPath.cpp +173 -0
  24. data/ext/xmlsig/XPath.h +156 -0
  25. data/ext/xmlsig/XPathCtx.h +68 -0
  26. data/ext/xmlsig/XmlCharBuf.h +60 -0
  27. data/ext/xmlsig/XmlDoc.cpp +278 -0
  28. data/ext/xmlsig/XmlDoc.h +157 -0
  29. data/ext/xmlsig/XmlElement.cpp +151 -0
  30. data/ext/xmlsig/XmlElement.h +134 -0
  31. data/ext/xmlsig/countptr.h +260 -0
  32. data/ext/xmlsig/extconf.rb +58 -0
  33. data/ext/xmlsig/runtests.rb +23 -0
  34. data/ext/xmlsig/swig/countptr.i +27 -0
  35. data/ext/xmlsig/swig/exceptions.i +79 -0
  36. data/ext/xmlsig/swig/ruby.i +17 -0
  37. data/ext/xmlsig/swig/xmlsig.i +405 -0
  38. data/ext/xmlsig/t/tc_cert.rb +34 -0
  39. data/ext/xmlsig/t/tc_interface.rb +158 -0
  40. data/ext/xmlsig/t/tc_signer.rb +501 -0
  41. data/ext/xmlsig/t/tc_tsik.rb +490 -0
  42. data/ext/xmlsig/t/tc_verifier.rb +151 -0
  43. data/ext/xmlsig/t/tsik_interop/sign.rb +48 -0
  44. data/ext/xmlsig/t/tsik_interop/verify.rb +31 -0
  45. data/ext/xmlsig/t/tsik_interop/verify_own.rb +46 -0
  46. data/ext/xmlsig/xmlsig.cpp +13363 -0
  47. data/lib/xmlsig.rb +1 -0
  48. metadata +113 -0
data/README.rdoc ADDED
File without changes
@@ -0,0 +1,98 @@
1
+ /*
2
+ * (C) Copyright 2006 VeriSign, Inc.
3
+ * Developed by Sxip Identity
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ #ifndef _BIOWRAP_H
18
+ #define _BIOWRAP_H
19
+
20
+ #include <openssl/bio.h>
21
+ #include <iostream>
22
+ #include "Exceptions.h"
23
+
24
+ /// @cond NO_INTERFACE
25
+ /**
26
+ * Wrap OpenSSL BIO object
27
+ */
28
+ class BioWrap
29
+ {
30
+ public:
31
+ /**
32
+ * Create a new memory BIO.
33
+ * @throws MemoryError if one couldn't be allocated
34
+ */
35
+ BioWrap () : mem(0)
36
+ {
37
+ mem = BIO_new(BIO_s_mem());
38
+ if (mem == NULL)
39
+ {
40
+ THROW_NORET(MemoryError, "Couldn't allocate BIO");
41
+ }
42
+ }
43
+ /**
44
+ * Destroy BIO, freeing its memory.
45
+ */
46
+ ~BioWrap ()
47
+ {
48
+ if (mem)
49
+ {
50
+ BIO_free_all(mem);
51
+ mem = 0;
52
+ }
53
+ }
54
+ /**
55
+ * Write the buffer to the BIO.
56
+ * @param buf Character buffer to write
57
+ * @param size Size of buffer
58
+ * @throws IOError on write failure
59
+ * @return 0 on success, <0 on failure
60
+ */
61
+ int write (xmlChar* buf, xmlSecSize size)
62
+ {
63
+ // cast size to int (same as in xmlsec library)
64
+ int ret = BIO_write(mem, buf, (int)size);
65
+ if (ret <= 0)
66
+ {
67
+ THROW(IOError, "BIO write failure", ret);
68
+ }
69
+ return ret;
70
+ }
71
+ /**
72
+ * Cast to a BIO pointer.
73
+ */
74
+ operator BIO* ()
75
+ {
76
+ return mem;
77
+ }
78
+ /**
79
+ * Cast to a void pointer, good for null checks.
80
+ */
81
+ operator const void* ()
82
+ {
83
+ return mem;
84
+ }
85
+ /**
86
+ * Cast to an integer, nonzero if BIO is valid.
87
+ */
88
+ operator int ()
89
+ {
90
+ return mem != 0;
91
+ }
92
+
93
+ protected:
94
+ BIO* mem;
95
+ };
96
+ /// @endcond
97
+
98
+ #endif
@@ -0,0 +1,109 @@
1
+ /*
2
+ * (C) Copyright 2006 VeriSign, Inc.
3
+ * Developed by Sxip Identity
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ #include <stdio.h>
18
+ #include <string>
19
+
20
+ #include <libxml/tree.h>
21
+ #include <libxml/xmlmemory.h>
22
+ #include <libxml/parser.h>
23
+
24
+ #ifndef XMLSEC_NO_XSLT
25
+ #include <libxslt/xslt.h>
26
+ #endif /* XMLSEC_NO_XSLT */
27
+
28
+ #include <xmlsec/xmlsec.h>
29
+ #include <xmlsec/xmltree.h>
30
+ #include <xmlsec/xmldsig.h>
31
+ #include <xmlsec/openssl/app.h>
32
+ #include <xmlsec/openssl/crypto.h>
33
+
34
+ #include "DSig.h"
35
+ #include "Exceptions.h"
36
+
37
+ static int dsigInitialized = 0;
38
+
39
+ extern "C" const char *xmlsec_lt_dlerror(void);
40
+
41
+
42
+ int dsigInit ()
43
+ {
44
+ if (dsigInitialized)
45
+ {
46
+ return 0;
47
+ }
48
+ else
49
+ {
50
+ dsigInitialized = 1;
51
+ }
52
+
53
+ /* Init libxml and libxslt libraries */
54
+ xmlInitParser();
55
+ LIBXML_TEST_VERSION
56
+ xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
57
+ xmlSubstituteEntitiesDefault(1);
58
+
59
+ #ifndef XMLSEC_NO_XSLT
60
+ xmlIndentTreeOutput = 1;
61
+ #endif // XMLSEC_NO_XSLT
62
+
63
+ // Init xmlsec library
64
+ if (xmlSecInit() < 0)
65
+ {
66
+ THROW(LibError, "xmlsec initialization failed", -1);
67
+ }
68
+ // Check loaded library version
69
+ if (xmlSecCheckVersion() != 1)
70
+ {
71
+ THROW(LibError, "Loaded xmlsec library version is not compatible", -1);
72
+ }
73
+ if (xmlSecOpenSSLAppInit(NULL) < 0)
74
+ {
75
+ THROW(LibError, "OpenSSL application initialization failed", -1);
76
+ }
77
+ // Init crypto library
78
+ if (xmlSecOpenSSLInit() < 0)
79
+ {
80
+ THROW(LibError, "xmlsec OpenSSL initialization failed", -1);
81
+ }
82
+ initErrorHandler();
83
+ return 0;
84
+ }
85
+
86
+
87
+ int dsigShutdown ()
88
+ {
89
+ if (!dsigInitialized)
90
+ {
91
+ return -1;
92
+ }
93
+ // Shutdown xmlsec-crypto library
94
+ xmlSecOpenSSLShutdown();
95
+
96
+ // Shutdown crypto library
97
+ //xmlSecCryptoAppShutdown();
98
+
99
+ // Shutdown xmlsec library
100
+ xmlSecShutdown();
101
+
102
+ // Shutdown libxslt/libxml
103
+ #ifndef XMLSEC_NO_XSLT
104
+ xsltCleanupGlobals();
105
+ #endif // XMLSEC_NO_XSLT
106
+
107
+ xmlCleanupParser();
108
+ return 0;
109
+ }
data/ext/xmlsig/DSig.h ADDED
@@ -0,0 +1,81 @@
1
+ /*
2
+ * (C) Copyright 2006 VeriSign, Inc.
3
+ * Developed by Sxip Identity
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ #ifndef _DSIG_H
18
+ #define _DSIG_H
19
+
20
+ #include "Exceptions.h"
21
+
22
+ /**
23
+ * \mainpage
24
+ *
25
+ * XMLSig is a C++ wrapper around the xmlsec library, providing a simple
26
+ * object oriented interface for dynamic languages. Its main objectives
27
+ * are:
28
+ *
29
+ * - To be a fully compliant XML Signature implementation.
30
+ * See http://www.w3.org/TR/xmldsig-core/.
31
+ * - To have an API resembling Apache TSIK.
32
+ * See http://incubator.apache.org/tsik/.
33
+ *
34
+ * Secondary objectives include:
35
+ *
36
+ * - Make it easy to bind to many dynamic languages.
37
+ * This can be seen in the minimal amount of type-mapping needed to
38
+ * bind to XMLSig. Only XMLSig objects and common C/C++ types are
39
+ * exposed. This gives the XMLSig interface a
40
+ * lowest-common-denominator feel, and it is expected that language
41
+ * enthusiasts will create wrapper modules that have a more
42
+ * language-specific feel.
43
+ * - Play nicely with native language objects as much as possible.
44
+ * One goal of XMLSig is to provide language-specific methods so
45
+ * that developers can still use their language's standard libraries
46
+ * with XMLSig. For example, XMLSig encapsulates XML processing,
47
+ * but different languages have their own favorite XML libraries, so
48
+ * XMLSig should make it possible for developers to use their
49
+ * language's standard XML API.
50
+ *
51
+ * \section license License
52
+ *
53
+ * (C) Copyright 2006 VeriSign, Inc.
54
+ * Developed by Sxip Identity
55
+ *
56
+ * Licensed under the Apache License, Version 2.0 (the "License");
57
+ * you may not use this file except in compliance with the License.
58
+ * You may obtain a copy of the License at
59
+ *
60
+ * http://www.apache.org/licenses/LICENSE-2.0
61
+ *
62
+ * Unless required by applicable law or agreed to in writing, software
63
+ * distributed under the License is distributed on an "AS IS" BASIS,
64
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
65
+ * See the License for the specific language governing permissions and
66
+ * limitations under the License.
67
+ */
68
+
69
+ /**
70
+ * Initialize the library. Initializes the libxml2, libxslt, xmlsec
71
+ * and OpenSSL libraries. Also calls initErrorHandler. This
72
+ * function may be called more than once.
73
+ */
74
+ int dsigInit();
75
+ /**
76
+ * Shutdown the library. Calls the de-initialize functions for the
77
+ * libxml2, libxslt, xmlsec and OpenSSL libraries.
78
+ */
79
+ int dsigShutdown();
80
+
81
+ #endif
@@ -0,0 +1,72 @@
1
+ /*
2
+ * (C) Copyright 2006 VeriSign, Inc.
3
+ * Developed by Sxip Identity
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ #ifndef _DSIGCTX_H
18
+ #define _DSIGCTX_H
19
+
20
+ #include <xmlsec/xmlsec.h>
21
+ #include <xmlsec/xmldsig.h>
22
+ #include <assert.h>
23
+ #include "Exceptions.h"
24
+
25
+ class DSigCtx
26
+ {
27
+ public:
28
+ DSigCtx (xmlSecKeysMngrPtr keysMngr = 0)
29
+ : dsigCtx (0)
30
+ {
31
+ dsigCtx = xmlSecDSigCtxCreate(keysMngr);
32
+ if (!dsigCtx)
33
+ {
34
+ THROW_NORET(MemoryError, "Couldn't create DSIG context");
35
+ }
36
+ }
37
+ ~DSigCtx ()
38
+ {
39
+ if (dsigCtx)
40
+ {
41
+ xmlSecDSigCtxDestroy(dsigCtx);
42
+ }
43
+ }
44
+
45
+ operator int ()
46
+ {
47
+ return dsigCtx != NULL;
48
+ }
49
+ int operator! ()
50
+ {
51
+ return dsigCtx == NULL;
52
+ }
53
+ xmlSecDSigCtxPtr operator-> ()
54
+ {
55
+ assert(dsigCtx);
56
+ return dsigCtx;
57
+ }
58
+ operator xmlSecDSigCtxPtr ()
59
+ {
60
+ return dsigCtx;
61
+ }
62
+
63
+ void dump (FILE* file)
64
+ {
65
+ xmlSecDSigCtxDebugDump(dsigCtx, file);
66
+ }
67
+
68
+ protected:
69
+ xmlSecDSigCtxPtr dsigCtx;
70
+ };
71
+
72
+ #endif
@@ -0,0 +1,151 @@
1
+ /*
2
+ * (C) Copyright 2006 VeriSign, Inc.
3
+ * Developed by Sxip Identity
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ #include "Exceptions.h"
18
+ #include <xmlsec/xmlsec.h>
19
+ #include <xmlsec/errors.h>
20
+ #include <libxml/xmlerror.h>
21
+ #include <libxslt/xsltutils.h>
22
+ #include <iostream>
23
+ #include <stdarg.h>
24
+
25
+ #ifdef _WIN32
26
+ #define snprintf _snprintf
27
+ #define vsnprintf _vsnprintf
28
+ #endif
29
+
30
+ #define SEC_ERRORSTR_SIZE 2048
31
+ char sec_error_str[SEC_ERRORSTR_SIZE] = "";
32
+ #define ERRORSTR_SIZE 1024
33
+ char xml_error_str[ERRORSTR_SIZE] = "";
34
+ char xslt_error_str[ERRORSTR_SIZE] = "";
35
+
36
+
37
+ void secErrorCallback (const char *file,
38
+ int line,
39
+ const char *func,
40
+ const char *errorObject,
41
+ const char *errorSubject,
42
+ int reason,
43
+ const char *msg)
44
+ {
45
+ const char* error_msg = NULL;
46
+
47
+ for (xmlSecSize i = 0;
48
+ (i < XMLSEC_ERRORS_MAX_NUMBER) && (xmlSecErrorsGetMsg(i) != NULL);
49
+ ++i)
50
+ {
51
+ if (xmlSecErrorsGetCode(i) == reason)
52
+ {
53
+ error_msg = xmlSecErrorsGetMsg(i);
54
+ break;
55
+ }
56
+ }
57
+ char* str = sec_error_str;
58
+ for (; (*str && ((str - sec_error_str) < SEC_ERRORSTR_SIZE)); str++);
59
+ snprintf(str, SEC_ERRORSTR_SIZE - (str - sec_error_str),
60
+ "func=%s:file=%s:line=%d:obj=%s:subj=%s:error=%d:%s:%s\n",
61
+ (func != NULL) ? func : "unknown",
62
+ (file != NULL) ? file : "unknown",
63
+ line,
64
+ (errorObject != NULL) ? errorObject : "unknown",
65
+ (errorSubject != NULL) ? errorSubject : "unknown",
66
+ reason,
67
+ (error_msg != NULL) ? error_msg : "",
68
+ (msg != NULL) ? msg : "");
69
+ #ifdef DEBUG_EXCEPTIONS
70
+ fprintf(stderr,
71
+ "func=%s:file=%s:line=%d:obj=%s:subj=%s:error=%d:%s:%s\n",
72
+ (func != NULL) ? func : "unknown",
73
+ (file != NULL) ? file : "unknown",
74
+ line,
75
+ (errorObject != NULL) ? errorObject : "unknown",
76
+ (errorSubject != NULL) ? errorSubject : "unknown",
77
+ reason,
78
+ (error_msg != NULL) ? error_msg : "",
79
+ (msg != NULL) ? msg : "");
80
+ #endif // DEBUG_EXCEPTIONS
81
+ }
82
+
83
+
84
+ void xmlErrorCallback (void* str, const char* msg, ...)
85
+ {
86
+ va_list args;
87
+ va_start(args, msg);
88
+ vsnprintf((char*)str, ERRORSTR_SIZE, msg, args);
89
+ #ifdef DEBUG_EXCEPTIONS
90
+ vfprintf(stderr, msg, args);
91
+ #endif // DEBUG_EXCEPTIONS
92
+ va_end(args);
93
+ }
94
+
95
+
96
+ void initErrorHandler ()
97
+ {
98
+ xmlSecErrorsSetCallback(secErrorCallback);
99
+ xmlSetGenericErrorFunc(xml_error_str, xmlErrorCallback);
100
+ #ifndef XMLSEC_NO_XSLT
101
+ xsltSetGenericErrorFunc(xslt_error_str, xmlErrorCallback);
102
+ #endif
103
+ }
104
+
105
+
106
+ LibError::LibError ()
107
+ : DsigException()
108
+ {
109
+ appendAll();
110
+ }
111
+
112
+
113
+ LibError::LibError (string what_str)
114
+ : DsigException(what_str)
115
+ {
116
+ appendAll();
117
+ }
118
+
119
+
120
+ void LibError::clearErrorLogs ()
121
+ {
122
+ char* strs[] = { sec_error_str, xml_error_str, xslt_error_str, "" };
123
+ for (char** str = strs; **str; str++)
124
+ {
125
+ **str = '\0';
126
+ }
127
+ }
128
+
129
+ void LibError::appendAll ()
130
+ {
131
+ char* strs[] = { sec_error_str, xml_error_str, xslt_error_str, "" };
132
+ for (char** str = strs; **str; str++)
133
+ {
134
+ appendWhat(*str);
135
+ }
136
+ }
137
+
138
+
139
+ void LibError::appendWhat (char* str)
140
+ {
141
+ if (*str)
142
+ {
143
+ if (what_str.length() && (what_str[what_str.length()] != '\n'))
144
+ {
145
+ what_str += "\n";
146
+ }
147
+ what_str += str;
148
+ *str = '\0';
149
+ }
150
+ }
151
+