nokogiri-xmlsec-instructure 0.11.0 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,271 @@
1
+ #include "xmlsecrb.h"
2
+
3
+ #include "options.h"
4
+ #include "util.h"
5
+
6
+ // Appends an xmlsig <dsig:Signature> node to document stored in |self|
7
+ // with a signature based on the given key and cert.
8
+ //
9
+ // Expects a ruby hash for the signing arguments.
10
+ // Hash parameters:
11
+ // :key - A PEM encoded rsa key for signing.
12
+ // :cert - The public cert to include with the signature.
13
+ // :signature_alg - Algorithm identified by the URL fragment. Supported algorithms
14
+ // taken from http://www.w3.org/TR/xmldsig-core
15
+ // :digest_alg - Algorithm identified by the URL fragment. Supported algorithms
16
+ // taken from http://www.w3.org/TR/xmldsig-core
17
+ // :canon_alg - Algorithm identified by the URL fragment. Supported algorithms
18
+ // taken from http://www.w3.org/TR/xmldsig-core/#sec-c14nAlg
19
+ // Default is ExclC14N (exclusive canonicalization).
20
+ // :name - [optional] String with name of the rsa key.
21
+ // :uri - [optional] The URI attribute for the <Reference> node in the
22
+ // signature.
23
+ // :store_references - [optional] If true, the options hash will be modified,
24
+ // and this value will be replaced with pre-digest buffer for
25
+ // debugging purposes
26
+ VALUE sign(VALUE self, VALUE rb_opts) {
27
+ VALUE rb_exception_result = Qnil;
28
+ const char* exception_message = NULL;
29
+
30
+ xmlDocPtr doc = NULL;
31
+ xmlNodePtr envelopeNode = NULL;
32
+ xmlNodePtr signNode = NULL;
33
+ xmlNodePtr refNode = NULL;
34
+ xmlNodePtr keyInfoNode = NULL;
35
+ xmlSecDSigCtxPtr dsigCtx = NULL;
36
+ char *keyName = NULL;
37
+ char *certificate = NULL;
38
+ char *rsaKey = NULL;
39
+ char *refUri = NULL;
40
+ unsigned int rsaKeyLength = 0;
41
+ unsigned int certificateLength = 0;
42
+ VALUE rb_references = Qnil;
43
+ int store_references = 0;
44
+ VALUE rb_pre_digest_buffer_sym, rb_reference, rb_pre_digest_buffer;
45
+ xmlSecSize pos;
46
+
47
+ VALUE rb_rsa_key = rb_hash_aref(rb_opts, ID2SYM(rb_intern("key")));
48
+ VALUE rb_cert = rb_hash_aref(rb_opts, ID2SYM(rb_intern("cert")));
49
+ VALUE rb_signature_alg = rb_hash_aref(rb_opts, ID2SYM(rb_intern("signature_alg")));
50
+ VALUE rb_digest_alg = rb_hash_aref(rb_opts, ID2SYM(rb_intern("digest_alg")));
51
+ VALUE rb_canon_alg = rb_hash_aref(rb_opts, ID2SYM(rb_intern("canon_alg")));
52
+ VALUE rb_uri = rb_hash_aref(rb_opts, ID2SYM(rb_intern("uri")));
53
+ VALUE rb_key_name = rb_hash_aref(rb_opts, ID2SYM(rb_intern("name")));
54
+ VALUE rb_store_references = rb_hash_aref(rb_opts, ID2SYM(rb_intern("store_references")));
55
+
56
+ resetXmlSecError();
57
+
58
+ Check_Type(rb_rsa_key, T_STRING);
59
+ Check_Type(rb_signature_alg, T_STRING);
60
+ Check_Type(rb_digest_alg, T_STRING);
61
+
62
+ rsaKey = RSTRING_PTR(rb_rsa_key);
63
+ rsaKeyLength = RSTRING_LEN(rb_rsa_key);
64
+
65
+ if (!NIL_P(rb_cert)) {
66
+ Check_Type(rb_cert, T_STRING);
67
+ certificate = RSTRING_PTR(rb_cert);
68
+ certificateLength = RSTRING_LEN(rb_cert);
69
+ }
70
+ if (!NIL_P(rb_key_name)) {
71
+ Check_Type(rb_key_name, T_STRING);
72
+ keyName = StringValueCStr(rb_key_name);
73
+ }
74
+ if (!NIL_P(rb_uri)) {
75
+ Check_Type(rb_uri, T_STRING);
76
+ refUri = StringValueCStr(rb_uri);
77
+ }
78
+ if (!NIL_P(rb_canon_alg)){
79
+ Check_Type(rb_canon_alg, T_STRING);
80
+ }
81
+ switch (TYPE(rb_store_references)) {
82
+ case T_TRUE:
83
+ store_references = 1;
84
+ break;
85
+ case T_FALSE:
86
+ case T_NIL:
87
+ break;
88
+ default:
89
+ Check_Type(rb_store_references, T_TRUE);
90
+ break;
91
+ }
92
+
93
+ xmlSecTransformId canon_algorithm = xmlSecTransformExclC14NId; // default
94
+ if (!NIL_P(rb_canon_alg)){
95
+ canon_algorithm = GetCanonicalizationMethod(rb_canon_alg,
96
+ &rb_exception_result, &exception_message);
97
+ if (canon_algorithm == xmlSecTransformIdUnknown){
98
+ goto done;
99
+ }
100
+ }
101
+
102
+ xmlSecTransformId signature_algorithm = GetSignatureMethod(rb_signature_alg,
103
+ &rb_exception_result, &exception_message);
104
+ if (signature_algorithm == xmlSecTransformIdUnknown) {
105
+ // Propagate exception.
106
+ goto done;
107
+ }
108
+
109
+ Noko_Node_Get_Struct(self, xmlNode, envelopeNode);
110
+ doc = envelopeNode->doc;
111
+ // create signature template for enveloped signature.
112
+ signNode = xmlSecTmplSignatureCreate(doc, canon_algorithm,
113
+ signature_algorithm, NULL);
114
+ if (signNode == NULL) {
115
+ rb_exception_result = rb_eSigningError;
116
+ exception_message = "failed to create signature template";
117
+ goto done;
118
+ }
119
+
120
+ // add <dsig:Signature/> node to the doc
121
+ xmlAddChild(envelopeNode, signNode);
122
+
123
+ // add reference
124
+ xmlSecTransformId digest_algorithm = GetDigestMethod(rb_digest_alg,
125
+ &rb_exception_result, &exception_message);
126
+ if (digest_algorithm == xmlSecTransformIdUnknown) {
127
+ // Propagate exception.
128
+ goto done;
129
+ }
130
+
131
+ refNode = xmlSecTmplSignatureAddReference(signNode, digest_algorithm,
132
+ NULL, (const xmlChar *)refUri, NULL);
133
+ if(refNode == NULL) {
134
+ rb_exception_result = rb_eSigningError;
135
+ exception_message = "failed to add reference to signature template";
136
+ goto done;
137
+ }
138
+
139
+ // add enveloped transform
140
+ if(xmlSecTmplReferenceAddTransform(refNode, xmlSecTransformEnvelopedId) == NULL) {
141
+ rb_exception_result = rb_eSigningError;
142
+ exception_message = "failed to add enveloped transform to reference";
143
+ goto done;
144
+ }
145
+
146
+ if(xmlSecTmplReferenceAddTransform(refNode, canon_algorithm) == NULL){
147
+ rb_exception_result = rb_eSigningError;
148
+ exception_message = "failed to add canonicalization transform to reference";
149
+ goto done;
150
+ }
151
+
152
+ // add <dsig:KeyInfo/>
153
+ keyInfoNode = xmlSecTmplSignatureEnsureKeyInfo(signNode, NULL);
154
+ if(keyInfoNode == NULL) {
155
+ rb_exception_result = rb_eSigningError;
156
+ exception_message = "failed to add key info";
157
+ goto done;
158
+ }
159
+
160
+ if(certificate) {
161
+ // add <dsig:X509Data/>
162
+ if(xmlSecTmplKeyInfoAddX509Data(keyInfoNode) == NULL) {
163
+ rb_exception_result = rb_eSigningError;
164
+ exception_message = "failed to add X509Data node";
165
+ goto done;
166
+ }
167
+ }
168
+
169
+ if(keyName) {
170
+ // add <dsig:KeyName/>
171
+ if(xmlSecTmplKeyInfoAddKeyName(keyInfoNode, NULL) == NULL) {
172
+ rb_exception_result = rb_eSigningError;
173
+ exception_message = "failed to add key name";
174
+ goto done;
175
+ }
176
+ }
177
+
178
+ // create signature context, we don't need keys manager in this example
179
+ dsigCtx = createDSigContext(NULL);
180
+ if(dsigCtx == NULL) {
181
+ rb_exception_result = rb_eSigningError;
182
+ exception_message = "failed to create signature context";
183
+ goto done;
184
+ }
185
+ if (store_references) {
186
+ dsigCtx->flags |= XMLSEC_DSIG_FLAGS_STORE_SIGNEDINFO_REFERENCES |
187
+ XMLSEC_DSIG_FLAGS_STORE_MANIFEST_REFERENCES;
188
+ }
189
+
190
+ // load private key, assuming that there is not password
191
+ dsigCtx->signKey = xmlSecCryptoAppKeyLoadMemory((xmlSecByte *)rsaKey,
192
+ rsaKeyLength,
193
+ xmlSecKeyDataFormatPem,
194
+ NULL, // password
195
+ NULL,
196
+ NULL);
197
+ if(dsigCtx->signKey == NULL) {
198
+ rb_exception_result = rb_eSigningError;
199
+ exception_message = "failed to load private key";
200
+ goto done;
201
+ }
202
+
203
+ if(keyName) {
204
+ // set key name
205
+ if(xmlSecKeySetName(dsigCtx->signKey, (xmlSecByte *)keyName) < 0) {
206
+ rb_exception_result = rb_eSigningError;
207
+ exception_message = "failed to set key name";
208
+ goto done;
209
+ }
210
+ }
211
+
212
+ if(certificate) {
213
+ // load certificate and add to the key
214
+ if(xmlSecCryptoAppKeyCertLoadMemory(dsigCtx->signKey,
215
+ (xmlSecByte *)certificate,
216
+ certificateLength,
217
+ xmlSecKeyDataFormatPem) < 0) {
218
+ rb_exception_result = rb_eSigningError;
219
+ exception_message = "failed to load certificate";
220
+ goto done;
221
+ }
222
+ }
223
+
224
+ // sign the template
225
+ if(xmlSecDSigCtxSign(dsigCtx, signNode) < 0) {
226
+ rb_exception_result = rb_eSigningError;
227
+ exception_message = "signature failed";
228
+ goto done;
229
+ }
230
+ if (store_references) {
231
+ rb_pre_digest_buffer_sym = ID2SYM(rb_intern("pre_digest_buffer"));
232
+ rb_references = rb_ary_new2(xmlSecPtrListGetSize(&dsigCtx->signedInfoReferences));
233
+ rb_hash_aset(rb_opts, ID2SYM(rb_intern("references")), rb_references);
234
+
235
+ for(pos = 0; pos < xmlSecPtrListGetSize(&dsigCtx->signedInfoReferences); ++pos) {
236
+ rb_reference = rb_hash_new();
237
+ rb_ary_push(rb_references, rb_reference);
238
+ xmlSecDSigReferenceCtxPtr dsigRefCtx = (xmlSecDSigReferenceCtxPtr)xmlSecPtrListGetItem(&dsigCtx->signedInfoReferences, pos);
239
+ xmlSecBufferPtr pre_digest_buffer = xmlSecDSigReferenceCtxGetPreDigestBuffer(dsigRefCtx);
240
+ if (pre_digest_buffer && xmlSecBufferGetData(pre_digest_buffer)) {
241
+ rb_pre_digest_buffer = rb_str_new((const char *)xmlSecBufferGetData(pre_digest_buffer), xmlSecBufferGetSize(pre_digest_buffer));
242
+ rb_hash_aset(rb_reference, rb_pre_digest_buffer_sym, rb_pre_digest_buffer);
243
+ }
244
+ }
245
+ }
246
+
247
+ done:
248
+ if(dsigCtx != NULL) {
249
+ xmlSecDSigCtxDestroy(dsigCtx);
250
+ }
251
+
252
+ xmlSecErrorsSetCallback(xmlSecErrorsDefaultCallback);
253
+
254
+ if(rb_exception_result != Qnil) {
255
+ // remove the signature node before raising an exception, so that
256
+ // the document is untouched
257
+ if (signNode != NULL) {
258
+ xmlUnlinkNode(signNode);
259
+ xmlFreeNode(signNode);
260
+ }
261
+
262
+ if (hasXmlSecLastError()) {
263
+ rb_raise(rb_exception_result, "%s, XmlSec error: %s", exception_message,
264
+ getXmlSecLastError());
265
+ } else {
266
+ rb_raise(rb_exception_result, "%s", exception_message);
267
+ }
268
+ }
269
+
270
+ return Qnil;
271
+ }
@@ -0,0 +1,261 @@
1
+ #include "xmlsecrb.h"
2
+ #include "util.h"
3
+
4
+ // Constructs a xmlSecKeysMngrPtr and adds all the certs included in |rb_certs|
5
+ // array as trusted certificates.
6
+ static xmlSecKeysMngrPtr createKeyManagerWithRbCertArray(
7
+ VALUE rb_certs,
8
+ VALUE* rb_exception_result_out,
9
+ const char** exception_message_out) {
10
+ VALUE rb_exception_result = Qnil;
11
+ const char* exception_message = NULL;
12
+
13
+ int i = 0;
14
+ int numCerts = RARRAY_LEN(rb_certs);
15
+ xmlSecKeysMngrPtr keyManager = xmlSecKeysMngrCreate();
16
+ VALUE rb_cert = Qnil;
17
+ char *cert = NULL;
18
+ unsigned int certLength = 0;
19
+ int numSuccessful = 0;
20
+
21
+ if (keyManager == NULL) {
22
+ rb_exception_result = rb_eDecryptionError;
23
+ exception_message = "failed to create keys manager.";
24
+ goto done;
25
+ }
26
+
27
+ if (xmlSecCryptoAppDefaultKeysMngrInit(keyManager) < 0) {
28
+ rb_exception_result = rb_eKeystoreError;
29
+ exception_message = "could not initialize key manager";
30
+ goto done;
31
+ }
32
+
33
+ for (i = 0; i < numCerts; i++) {
34
+ rb_cert = RARRAY_PTR(rb_certs)[i];
35
+ rb_cert = rb_obj_as_string(rb_cert);
36
+ Check_Type(rb_cert, T_STRING);
37
+ cert = RSTRING_PTR(rb_cert);
38
+ certLength = RSTRING_LEN(rb_cert);
39
+
40
+ if(xmlSecCryptoAppKeysMngrCertLoadMemory(keyManager,
41
+ (xmlSecByte *)cert,
42
+ certLength,
43
+ xmlSecKeyDataFormatPem,
44
+ xmlSecKeyDataTypeTrusted) < 0) {
45
+ rb_warn("failed to load certificate at index %d", i);
46
+ } else {
47
+ numSuccessful++;
48
+ }
49
+ }
50
+
51
+ // note, numCerts could be zero, meaning that we should use system SSL certs
52
+ if (numSuccessful == 0 && numCerts != 0) {
53
+ rb_exception_result = rb_eKeystoreError;
54
+ exception_message = "Could not load any of the specified certificates for signature verification";
55
+ goto done;
56
+ }
57
+
58
+ done:
59
+ if (!NIL_P(rb_exception_result)) {
60
+ if (keyManager) {
61
+ xmlSecKeysMngrDestroy(keyManager);
62
+ keyManager = NULL;
63
+ }
64
+ }
65
+
66
+ *rb_exception_result_out = rb_exception_result;
67
+ *exception_message_out = exception_message;
68
+ return keyManager;
69
+ }
70
+
71
+ static int addRubyKeyToManager(VALUE rb_key, VALUE rb_value, VALUE rb_manager) {
72
+ xmlSecKeysMngrPtr keyManager = (xmlSecKeysMngrPtr)rb_manager;
73
+ char *keyName, *keyData;
74
+ unsigned int keyDataLength;
75
+ xmlSecKeyPtr key;
76
+
77
+ Check_Type(rb_key, T_STRING);
78
+ Check_Type(rb_value, T_STRING);
79
+ keyName = RSTRING_PTR(rb_key);
80
+ keyData = RSTRING_PTR(rb_value);
81
+ keyDataLength = RSTRING_LEN(rb_value);
82
+
83
+ // load key
84
+ key = xmlSecCryptoAppKeyLoadMemory((xmlSecByte *)keyData,
85
+ keyDataLength,
86
+ xmlSecKeyDataFormatPem,
87
+ NULL, // password
88
+ NULL, NULL);
89
+ if (key == NULL) {
90
+ rb_warn("failed to load '%s' public or private pem key", keyName);
91
+ return ST_CONTINUE;
92
+ }
93
+
94
+ // set key name
95
+ if (xmlSecKeySetName(key, BAD_CAST keyName) < 0) {
96
+ rb_warn("failed to set key name for key '%s'", keyName);
97
+ return ST_CONTINUE;
98
+ }
99
+
100
+ // add key to key manager; from now on the manager is responsible for
101
+ // destroying the key
102
+ if (xmlSecCryptoAppDefaultKeysMngrAdoptKey(keyManager, key) < 0) {
103
+ rb_warn("failed to add key '%s' to key manager", keyName);
104
+ return ST_CONTINUE;
105
+ }
106
+
107
+ return ST_CONTINUE;
108
+ }
109
+
110
+ // Constructs a xmlSecKeysMngr and adds all the named to key mappings
111
+ // specified by the |rb_hash| to the key manager.
112
+ //
113
+ // Caller takes ownership. Free with xmlSecKeysMngrDestroy().
114
+ static xmlSecKeysMngrPtr createKeyManagerFromNamedKeys(
115
+ VALUE rb_hash,
116
+ VALUE* rb_exception_result_out,
117
+ const char** exception_message_out) {
118
+ xmlSecKeysMngrPtr keyManager = xmlSecKeysMngrCreate();
119
+ if (keyManager == NULL) return NULL;
120
+ if (xmlSecCryptoAppDefaultKeysMngrInit(keyManager) < 0) {
121
+ *rb_exception_result_out = rb_eKeystoreError;
122
+ *exception_message_out = "could not initialize key manager";
123
+ xmlSecKeysMngrDestroy(keyManager);
124
+ return NULL;
125
+ }
126
+
127
+ rb_hash_foreach(rb_hash, addRubyKeyToManager, (VALUE)keyManager);
128
+
129
+ return keyManager;
130
+ }
131
+
132
+ VALUE verify_with(VALUE self, VALUE rb_opts) {
133
+ VALUE rb_exception_result = Qnil;
134
+ const char* exception_message = NULL;
135
+
136
+ xmlNodePtr node = NULL;
137
+ xmlSecDSigCtxPtr dsigCtx = NULL;
138
+ xmlSecKeysMngrPtr keyManager = NULL;
139
+ VALUE rb_certs, rb_cert;
140
+ VALUE rb_rsa_key;
141
+ VALUE rb_verification_time, rb_verification_depth, rb_verify_certificates;
142
+ char *rsa_key = NULL;
143
+ unsigned int rsa_key_length = 0;
144
+ VALUE result = Qfalse;
145
+
146
+ resetXmlSecError();
147
+
148
+ Check_Type(rb_opts, T_HASH);
149
+ Noko_Node_Get_Struct(self, xmlNode, node);
150
+
151
+ // verify start node
152
+ if(!xmlSecCheckNodeName(node, xmlSecNodeSignature, xmlSecDSigNs)) {
153
+ rb_exception_result = rb_eVerificationError;
154
+ exception_message = "Can only verify a Signature node";
155
+ goto done;
156
+ }
157
+
158
+ rb_certs = rb_hash_aref(rb_opts, ID2SYM(rb_intern("cert")));
159
+ if (NIL_P(rb_certs)) {
160
+ rb_certs = rb_hash_aref(rb_opts, ID2SYM(rb_intern("certs")));
161
+ }
162
+
163
+ rb_verification_depth = rb_hash_aref(rb_opts, ID2SYM(rb_intern("verification_depth")));
164
+ rb_verification_time = rb_hash_aref(rb_opts, ID2SYM(rb_intern("verification_time")));
165
+ rb_verify_certificates = rb_hash_aref(rb_opts, ID2SYM(rb_intern("verify_certificates")));
166
+
167
+ if (!NIL_P(rb_certs)) {
168
+ if(TYPE(rb_certs) != T_ARRAY) {
169
+ rb_cert = rb_certs;
170
+ rb_certs = rb_ary_new();
171
+ rb_ary_push(rb_certs, rb_cert);
172
+ }
173
+
174
+ keyManager = createKeyManagerWithRbCertArray(rb_certs, &rb_exception_result,
175
+ &exception_message);
176
+ if (keyManager == NULL) {
177
+ // Propagate exception.
178
+ goto done;
179
+ }
180
+ } else if (!NIL_P(rb_rsa_key = rb_hash_aref(rb_opts, ID2SYM(rb_intern("key"))))) {
181
+ Check_Type(rb_rsa_key, T_STRING);
182
+ rsa_key = RSTRING_PTR(rb_rsa_key);
183
+ rsa_key_length = RSTRING_LEN(rb_rsa_key);
184
+ } else {
185
+ keyManager = createKeyManagerFromNamedKeys(rb_opts, &rb_exception_result,
186
+ &exception_message);
187
+ if (keyManager == NULL) {
188
+ // Propagate exception.
189
+ goto done;
190
+ }
191
+ }
192
+
193
+ // Create signature context.
194
+ dsigCtx = createDSigContext(keyManager);
195
+ if(dsigCtx == NULL) {
196
+ rb_exception_result = rb_eVerificationError;
197
+ exception_message = "failed to create signature context";
198
+ goto done;
199
+ }
200
+
201
+ if(!NIL_P(rb_verification_time)) {
202
+ rb_verification_time = rb_Integer(rb_verification_time);
203
+ dsigCtx->keyInfoReadCtx.certsVerificationTime = (time_t)NUM2LONG(rb_verification_time);
204
+ }
205
+
206
+ if(rb_verify_certificates == Qfalse) {
207
+ dsigCtx->keyInfoReadCtx.flags |= XMLSEC_KEYINFO_FLAGS_X509DATA_DONT_VERIFY_CERTS;
208
+ }
209
+
210
+ if(!NIL_P(rb_verification_depth)) {
211
+ rb_verification_depth = rb_Integer(rb_verification_depth);
212
+ dsigCtx->keyInfoReadCtx.certsVerificationDepth = (time_t)NUM2LONG(rb_verification_depth);
213
+ }
214
+
215
+ if(rsa_key) {
216
+ // load public key
217
+ dsigCtx->signKey = xmlSecCryptoAppKeyLoadMemory((xmlSecByte *)rsa_key,
218
+ rsa_key_length,
219
+ xmlSecKeyDataFormatPem,
220
+ NULL, // password
221
+ NULL, NULL);
222
+ if(dsigCtx->signKey == NULL) {
223
+ rb_exception_result = rb_eVerificationError;
224
+ exception_message = "failed to load public pem key";
225
+ goto done;
226
+ }
227
+ }
228
+
229
+ // verify signature
230
+ if(xmlSecDSigCtxVerify(dsigCtx, node) < 0) {
231
+ rb_exception_result = rb_eVerificationError;
232
+ exception_message = "error occurred during signature verification";
233
+ goto done;
234
+ }
235
+
236
+ if(dsigCtx->status == xmlSecDSigStatusSucceeded) {
237
+ result = Qtrue;
238
+ }
239
+
240
+ done:
241
+ if(dsigCtx != NULL) {
242
+ xmlSecDSigCtxDestroy(dsigCtx);
243
+ }
244
+
245
+ if (keyManager != NULL) {
246
+ xmlSecKeysMngrDestroy(keyManager);
247
+ }
248
+
249
+ xmlSecErrorsSetCallback(xmlSecErrorsDefaultCallback);
250
+
251
+ if(!NIL_P(rb_exception_result)) {
252
+ if (hasXmlSecLastError()) {
253
+ rb_raise(rb_exception_result, "%s, XmlSec error: %s", exception_message,
254
+ getXmlSecLastError());
255
+ } else {
256
+ rb_raise(rb_exception_result, "%s", exception_message);
257
+ }
258
+ }
259
+
260
+ return result;
261
+ }