nokogiri-xmlsec-instructure 0.10.3 → 0.11.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.
- checksums.yaml +4 -4
- data/ext/nokogiri_ext_xmlsec/extconf.rb +8 -16
- data/lib/nokogiri-xmlsec.rb +3 -1
- data/lib/nokogiri_ext_xmlsec.bundle +0 -0
- data/lib/xmlsec/version.rb +3 -1
- data/lib/xmlsec.rb +99 -88
- metadata +11 -181
- data/.github/workflows/push.yml +0 -40
- data/.gitignore +0 -23
- data/.rspec +0 -2
- data/.tool-versions +0 -1
- data/Appraisals +0 -9
- data/Gemfile +0 -4
- data/Guardfile +0 -13
- data/LICENSE.txt +0 -22
- data/README.md +0 -132
- data/Rakefile +0 -30
- data/ext/nokogiri_ext_xmlsec/common.h +0 -13
- data/ext/nokogiri_ext_xmlsec/init.c +0 -71
- data/ext/nokogiri_ext_xmlsec/nokogiri_decrypt_with_key.c +0 -84
- data/ext/nokogiri_ext_xmlsec/nokogiri_encrypt_with_key.c +0 -210
- data/ext/nokogiri_ext_xmlsec/nokogiri_helpers_set_attribute_id.c +0 -93
- data/ext/nokogiri_ext_xmlsec/nokogiri_init.c +0 -30
- data/ext/nokogiri_ext_xmlsec/nokogiri_sign.c +0 -254
- data/ext/nokogiri_ext_xmlsec/nokogiri_verify_with.c +0 -261
- data/ext/nokogiri_ext_xmlsec/options.c +0 -166
- data/ext/nokogiri_ext_xmlsec/options.h +0 -36
- data/ext/nokogiri_ext_xmlsec/shutdown.c +0 -12
- data/ext/nokogiri_ext_xmlsec/util.c +0 -140
- data/ext/nokogiri_ext_xmlsec/util.h +0 -42
- data/ext/nokogiri_ext_xmlsec/xmlsecrb.h +0 -49
- data/gemfiles/nokogiri_12.5.gemfile +0 -7
- data/gemfiles/nokogiri_13.10.gemfile +0 -7
- data/nokogiri-xmlsec-instructure.gemspec +0 -41
- data/spec/fixtures/cert/server.crt +0 -14
- data/spec/fixtures/cert/server.csr +0 -11
- data/spec/fixtures/cert/server.key.decrypted +0 -15
- data/spec/fixtures/cert/server.key.encrypted +0 -18
- data/spec/fixtures/hate.xml +0 -7
- data/spec/fixtures/pwned.xml +0 -1
- data/spec/fixtures/rsa.pem +0 -15
- data/spec/fixtures/rsa.pub +0 -6
- data/spec/fixtures/sign2-doc.xml +0 -6
- data/spec/fixtures/sign2-result.xml +0 -25
- data/spec/fixtures/sign3-result.xml +0 -39
- data/spec/lib/nokogiri/xml/document/encryption_and_decryption_spec.rb +0 -55
- data/spec/lib/nokogiri/xml/document/signing_and_verifying_spec.rb +0 -122
- data/spec/lib/nokogiri/xml/document/unsafe_xml_spec.rb +0 -61
- data/spec/spec_helper.rb +0 -10
@@ -1,254 +0,0 @@
|
|
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
|
-
// :name - [optional] String with name of the rsa key.
|
18
|
-
// :uri - [optional] The URI attribute for the <Reference> node in the
|
19
|
-
// signature.
|
20
|
-
// :store_references - [optional] If true, the options hash will be modified,
|
21
|
-
// and this value will be replaced with pre-digest buffer for
|
22
|
-
// debugging purposes
|
23
|
-
VALUE sign(VALUE self, VALUE rb_opts) {
|
24
|
-
VALUE rb_exception_result = Qnil;
|
25
|
-
const char* exception_message = NULL;
|
26
|
-
|
27
|
-
xmlDocPtr doc = NULL;
|
28
|
-
xmlNodePtr envelopeNode = NULL;
|
29
|
-
xmlNodePtr signNode = NULL;
|
30
|
-
xmlNodePtr refNode = NULL;
|
31
|
-
xmlNodePtr keyInfoNode = NULL;
|
32
|
-
xmlSecDSigCtxPtr dsigCtx = NULL;
|
33
|
-
char *keyName = NULL;
|
34
|
-
char *certificate = NULL;
|
35
|
-
char *rsaKey = NULL;
|
36
|
-
char *refUri = NULL;
|
37
|
-
unsigned int rsaKeyLength = 0;
|
38
|
-
unsigned int certificateLength = 0;
|
39
|
-
VALUE rb_references = Qnil;
|
40
|
-
int store_references = 0;
|
41
|
-
VALUE rb_pre_digest_buffer_sym, rb_reference, rb_pre_digest_buffer;
|
42
|
-
xmlSecSize pos;
|
43
|
-
|
44
|
-
VALUE rb_rsa_key = rb_hash_aref(rb_opts, ID2SYM(rb_intern("key")));
|
45
|
-
VALUE rb_cert = rb_hash_aref(rb_opts, ID2SYM(rb_intern("cert")));
|
46
|
-
VALUE rb_signature_alg = rb_hash_aref(rb_opts, ID2SYM(rb_intern("signature_alg")));
|
47
|
-
VALUE rb_digest_alg = rb_hash_aref(rb_opts, ID2SYM(rb_intern("digest_alg")));
|
48
|
-
VALUE rb_uri = rb_hash_aref(rb_opts, ID2SYM(rb_intern("uri")));
|
49
|
-
VALUE rb_key_name = rb_hash_aref(rb_opts, ID2SYM(rb_intern("name")));
|
50
|
-
VALUE rb_store_references = rb_hash_aref(rb_opts, ID2SYM(rb_intern("store_references")));
|
51
|
-
|
52
|
-
resetXmlSecError();
|
53
|
-
|
54
|
-
Check_Type(rb_rsa_key, T_STRING);
|
55
|
-
Check_Type(rb_signature_alg, T_STRING);
|
56
|
-
Check_Type(rb_digest_alg, T_STRING);
|
57
|
-
|
58
|
-
rsaKey = RSTRING_PTR(rb_rsa_key);
|
59
|
-
rsaKeyLength = RSTRING_LEN(rb_rsa_key);
|
60
|
-
|
61
|
-
if (!NIL_P(rb_cert)) {
|
62
|
-
Check_Type(rb_cert, T_STRING);
|
63
|
-
certificate = RSTRING_PTR(rb_cert);
|
64
|
-
certificateLength = RSTRING_LEN(rb_cert);
|
65
|
-
}
|
66
|
-
if (!NIL_P(rb_key_name)) {
|
67
|
-
Check_Type(rb_key_name, T_STRING);
|
68
|
-
keyName = StringValueCStr(rb_key_name);
|
69
|
-
}
|
70
|
-
if (!NIL_P(rb_uri)) {
|
71
|
-
Check_Type(rb_uri, T_STRING);
|
72
|
-
refUri = StringValueCStr(rb_uri);
|
73
|
-
}
|
74
|
-
switch (TYPE(rb_store_references)) {
|
75
|
-
case T_TRUE:
|
76
|
-
store_references = 1;
|
77
|
-
break;
|
78
|
-
case T_FALSE:
|
79
|
-
case T_NIL:
|
80
|
-
break;
|
81
|
-
default:
|
82
|
-
Check_Type(rb_store_references, T_TRUE);
|
83
|
-
break;
|
84
|
-
}
|
85
|
-
|
86
|
-
xmlSecTransformId signature_algorithm = GetSignatureMethod(rb_signature_alg,
|
87
|
-
&rb_exception_result, &exception_message);
|
88
|
-
if (signature_algorithm == xmlSecTransformIdUnknown) {
|
89
|
-
// Propagate exception.
|
90
|
-
goto done;
|
91
|
-
}
|
92
|
-
|
93
|
-
Noko_Node_Get_Struct(self, xmlNode, envelopeNode);
|
94
|
-
doc = envelopeNode->doc;
|
95
|
-
// create signature template for enveloped signature.
|
96
|
-
signNode = xmlSecTmplSignatureCreate(doc, xmlSecTransformExclC14NId,
|
97
|
-
signature_algorithm, NULL);
|
98
|
-
if (signNode == NULL) {
|
99
|
-
rb_exception_result = rb_eSigningError;
|
100
|
-
exception_message = "failed to create signature template";
|
101
|
-
goto done;
|
102
|
-
}
|
103
|
-
|
104
|
-
// add <dsig:Signature/> node to the doc
|
105
|
-
xmlAddChild(envelopeNode, signNode);
|
106
|
-
|
107
|
-
// add reference
|
108
|
-
xmlSecTransformId digest_algorithm = GetDigestMethod(rb_digest_alg,
|
109
|
-
&rb_exception_result, &exception_message);
|
110
|
-
if (digest_algorithm == xmlSecTransformIdUnknown) {
|
111
|
-
// Propagate exception.
|
112
|
-
goto done;
|
113
|
-
}
|
114
|
-
refNode = xmlSecTmplSignatureAddReference(signNode, digest_algorithm,
|
115
|
-
NULL, (const xmlChar *)refUri, NULL);
|
116
|
-
if(refNode == NULL) {
|
117
|
-
rb_exception_result = rb_eSigningError;
|
118
|
-
exception_message = "failed to add reference to signature template";
|
119
|
-
goto done;
|
120
|
-
}
|
121
|
-
|
122
|
-
// add enveloped transform
|
123
|
-
if(xmlSecTmplReferenceAddTransform(refNode, xmlSecTransformEnvelopedId) == NULL) {
|
124
|
-
rb_exception_result = rb_eSigningError;
|
125
|
-
exception_message = "failed to add enveloped transform to reference";
|
126
|
-
goto done;
|
127
|
-
}
|
128
|
-
|
129
|
-
if(xmlSecTmplReferenceAddTransform(refNode, xmlSecTransformExclC14NId) == NULL) {
|
130
|
-
rb_exception_result = rb_eSigningError;
|
131
|
-
exception_message = "failed to add canonicalization transform to reference";
|
132
|
-
goto done;
|
133
|
-
}
|
134
|
-
|
135
|
-
// add <dsig:KeyInfo/>
|
136
|
-
keyInfoNode = xmlSecTmplSignatureEnsureKeyInfo(signNode, NULL);
|
137
|
-
if(keyInfoNode == NULL) {
|
138
|
-
rb_exception_result = rb_eSigningError;
|
139
|
-
exception_message = "failed to add key info";
|
140
|
-
goto done;
|
141
|
-
}
|
142
|
-
|
143
|
-
if(certificate) {
|
144
|
-
// add <dsig:X509Data/>
|
145
|
-
if(xmlSecTmplKeyInfoAddX509Data(keyInfoNode) == NULL) {
|
146
|
-
rb_exception_result = rb_eSigningError;
|
147
|
-
exception_message = "failed to add X509Data node";
|
148
|
-
goto done;
|
149
|
-
}
|
150
|
-
}
|
151
|
-
|
152
|
-
if(keyName) {
|
153
|
-
// add <dsig:KeyName/>
|
154
|
-
if(xmlSecTmplKeyInfoAddKeyName(keyInfoNode, NULL) == NULL) {
|
155
|
-
rb_exception_result = rb_eSigningError;
|
156
|
-
exception_message = "failed to add key name";
|
157
|
-
goto done;
|
158
|
-
}
|
159
|
-
}
|
160
|
-
|
161
|
-
// create signature context, we don't need keys manager in this example
|
162
|
-
dsigCtx = createDSigContext(NULL);
|
163
|
-
if(dsigCtx == NULL) {
|
164
|
-
rb_exception_result = rb_eSigningError;
|
165
|
-
exception_message = "failed to create signature context";
|
166
|
-
goto done;
|
167
|
-
}
|
168
|
-
if (store_references) {
|
169
|
-
dsigCtx->flags |= XMLSEC_DSIG_FLAGS_STORE_SIGNEDINFO_REFERENCES |
|
170
|
-
XMLSEC_DSIG_FLAGS_STORE_MANIFEST_REFERENCES;
|
171
|
-
}
|
172
|
-
|
173
|
-
// load private key, assuming that there is not password
|
174
|
-
dsigCtx->signKey = xmlSecCryptoAppKeyLoadMemory((xmlSecByte *)rsaKey,
|
175
|
-
rsaKeyLength,
|
176
|
-
xmlSecKeyDataFormatPem,
|
177
|
-
NULL, // password
|
178
|
-
NULL,
|
179
|
-
NULL);
|
180
|
-
if(dsigCtx->signKey == NULL) {
|
181
|
-
rb_exception_result = rb_eSigningError;
|
182
|
-
exception_message = "failed to load private key";
|
183
|
-
goto done;
|
184
|
-
}
|
185
|
-
|
186
|
-
if(keyName) {
|
187
|
-
// set key name
|
188
|
-
if(xmlSecKeySetName(dsigCtx->signKey, (xmlSecByte *)keyName) < 0) {
|
189
|
-
rb_exception_result = rb_eSigningError;
|
190
|
-
exception_message = "failed to set key name";
|
191
|
-
goto done;
|
192
|
-
}
|
193
|
-
}
|
194
|
-
|
195
|
-
if(certificate) {
|
196
|
-
// load certificate and add to the key
|
197
|
-
if(xmlSecCryptoAppKeyCertLoadMemory(dsigCtx->signKey,
|
198
|
-
(xmlSecByte *)certificate,
|
199
|
-
certificateLength,
|
200
|
-
xmlSecKeyDataFormatPem) < 0) {
|
201
|
-
rb_exception_result = rb_eSigningError;
|
202
|
-
exception_message = "failed to load certificate";
|
203
|
-
goto done;
|
204
|
-
}
|
205
|
-
}
|
206
|
-
|
207
|
-
// sign the template
|
208
|
-
if(xmlSecDSigCtxSign(dsigCtx, signNode) < 0) {
|
209
|
-
rb_exception_result = rb_eSigningError;
|
210
|
-
exception_message = "signature failed";
|
211
|
-
goto done;
|
212
|
-
}
|
213
|
-
if (store_references) {
|
214
|
-
rb_pre_digest_buffer_sym = ID2SYM(rb_intern("pre_digest_buffer"));
|
215
|
-
rb_references = rb_ary_new2(xmlSecPtrListGetSize(&dsigCtx->signedInfoReferences));
|
216
|
-
rb_hash_aset(rb_opts, ID2SYM(rb_intern("references")), rb_references);
|
217
|
-
|
218
|
-
for(pos = 0; pos < xmlSecPtrListGetSize(&dsigCtx->signedInfoReferences); ++pos) {
|
219
|
-
rb_reference = rb_hash_new();
|
220
|
-
rb_ary_push(rb_references, rb_reference);
|
221
|
-
xmlSecDSigReferenceCtxPtr dsigRefCtx = (xmlSecDSigReferenceCtxPtr)xmlSecPtrListGetItem(&dsigCtx->signedInfoReferences, pos);
|
222
|
-
xmlSecBufferPtr pre_digest_buffer = xmlSecDSigReferenceCtxGetPreDigestBuffer(dsigRefCtx);
|
223
|
-
if (pre_digest_buffer && xmlSecBufferGetData(pre_digest_buffer)) {
|
224
|
-
rb_pre_digest_buffer = rb_str_new((const char *)xmlSecBufferGetData(pre_digest_buffer), xmlSecBufferGetSize(pre_digest_buffer));
|
225
|
-
rb_hash_aset(rb_reference, rb_pre_digest_buffer_sym, rb_pre_digest_buffer);
|
226
|
-
}
|
227
|
-
}
|
228
|
-
}
|
229
|
-
|
230
|
-
done:
|
231
|
-
if(dsigCtx != NULL) {
|
232
|
-
xmlSecDSigCtxDestroy(dsigCtx);
|
233
|
-
}
|
234
|
-
|
235
|
-
xmlSecErrorsSetCallback(xmlSecErrorsDefaultCallback);
|
236
|
-
|
237
|
-
if(rb_exception_result != Qnil) {
|
238
|
-
// remove the signature node before raising an exception, so that
|
239
|
-
// the document is untouched
|
240
|
-
if (signNode != NULL) {
|
241
|
-
xmlUnlinkNode(signNode);
|
242
|
-
xmlFreeNode(signNode);
|
243
|
-
}
|
244
|
-
|
245
|
-
if (hasXmlSecLastError()) {
|
246
|
-
rb_raise(rb_exception_result, "%s, XmlSec error: %s", exception_message,
|
247
|
-
getXmlSecLastError());
|
248
|
-
} else {
|
249
|
-
rb_raise(rb_exception_result, "%s", exception_message);
|
250
|
-
}
|
251
|
-
}
|
252
|
-
|
253
|
-
return Qnil;
|
254
|
-
}
|
@@ -1,261 +0,0 @@
|
|
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
|
-
}
|
@@ -1,166 +0,0 @@
|
|
1
|
-
#include "options.h"
|
2
|
-
|
3
|
-
#include "common.h"
|
4
|
-
|
5
|
-
#if (XMLSEC_VERSION_MAJOR > 1) || (XMLSEC_VERSION_MAJOR == 1 && (XMLSEC_VERSION_MINOR > 2 || (XMLSEC_VERSION_MINOR == 2 && XMLSEC_VERSION_SUBMINOR >= 20)))
|
6
|
-
# define HAS_ECDSA 1
|
7
|
-
#else
|
8
|
-
# define HAS_ECDSA 0
|
9
|
-
#endif
|
10
|
-
|
11
|
-
// Key Transport Strings.
|
12
|
-
static const char RSA1_5[] = "rsa-1_5";
|
13
|
-
static const char RSA_OAEP_MGF1P[] = "rsa-oaep-mgf1p";
|
14
|
-
|
15
|
-
// Block Encryption Strings.
|
16
|
-
static const char TRIPLEDES_CBC[] = "tripledes-cbc";
|
17
|
-
static const char AES128_CBC[] = "aes128-cbc";
|
18
|
-
static const char AES256_CBC[] = "aes256-cbc";
|
19
|
-
static const char AES192_CBC[] = "aes192-cbc";
|
20
|
-
|
21
|
-
// Supported signature algorithms taken from #6 of
|
22
|
-
// http://www.w3.org/TR/xmldsig-core1/
|
23
|
-
static const char RSA_SHA1[] = "rsa-sha1";
|
24
|
-
static const char RSA_SHA224[] = "rsa-sha224";
|
25
|
-
static const char RSA_SHA256[] = "rsa-sha256";
|
26
|
-
static const char RSA_SHA384[] = "rsa-sha384";
|
27
|
-
static const char RSA_SHA512[] = "rsa-sha512";
|
28
|
-
static const char DSA_SHA1[] = "dsa-sha1";
|
29
|
-
|
30
|
-
#if HAS_ECDSA
|
31
|
-
static const char ECDSA_SHA1[] = "ecdsa-sha1";
|
32
|
-
static const char ECDSA_SHA224[] = "ecdsa-sha224";
|
33
|
-
static const char ECDSA_SHA256[] = "ecdsa-sha256";
|
34
|
-
static const char ECDSA_SHA384[] = "ecdsa-sha384";
|
35
|
-
static const char ECDSA_SHA512[] = "ecdsa-sha512";
|
36
|
-
static const char DSA_SHA256[] = "dsa-sha256";
|
37
|
-
#endif // HAS_ECDSA
|
38
|
-
|
39
|
-
// Supported digest algorithms taken from #6 of
|
40
|
-
// http://www.w3.org/TR/xmldsig-core1/
|
41
|
-
static const char DIGEST_SHA1[] = "sha1";
|
42
|
-
static const char DIGEST_SHA224[] = "sha224";
|
43
|
-
static const char DIGEST_SHA256[] = "sha256";
|
44
|
-
static const char DIGEST_SHA384[] = "sha384";
|
45
|
-
static const char DIGEST_SHA512[] = "sha512";
|
46
|
-
|
47
|
-
BOOL GetXmlEncOptions(VALUE rb_opts,
|
48
|
-
XmlEncOptions* options,
|
49
|
-
VALUE* rb_exception_result,
|
50
|
-
const char** exception_message) {
|
51
|
-
VALUE rb_block_encryption = rb_hash_aref(rb_opts, ID2SYM(rb_intern("block_encryption")));
|
52
|
-
VALUE rb_key_transport = rb_hash_aref(rb_opts, ID2SYM(rb_intern("key_transport")));
|
53
|
-
memset(options, 0, sizeof(XmlEncOptions));
|
54
|
-
|
55
|
-
if (NIL_P(rb_block_encryption) ||
|
56
|
-
TYPE(rb_block_encryption) != T_STRING ||
|
57
|
-
NIL_P(rb_key_transport) ||
|
58
|
-
TYPE(rb_key_transport) != T_STRING) {
|
59
|
-
*rb_exception_result = rb_eArgError;
|
60
|
-
*exception_message = "Must supply :block_encryption & :key_transport";
|
61
|
-
return FALSE;
|
62
|
-
}
|
63
|
-
|
64
|
-
char* blockEncryptionValue = RSTRING_PTR(rb_block_encryption);
|
65
|
-
int blockEncryptionLen = RSTRING_LEN(rb_block_encryption);
|
66
|
-
char* keyTransportValue = RSTRING_PTR(rb_key_transport);
|
67
|
-
int keyTransportLen = RSTRING_LEN(rb_key_transport);
|
68
|
-
|
69
|
-
if (strncmp(AES256_CBC, blockEncryptionValue, blockEncryptionLen) == 0) {
|
70
|
-
options->block_encryption = xmlSecTransformAes256CbcId;
|
71
|
-
options->key_type = "aes";
|
72
|
-
options->key_bits = 256;
|
73
|
-
} else if (strncmp(AES128_CBC, blockEncryptionValue, blockEncryptionLen) == 0) {
|
74
|
-
options->block_encryption = xmlSecTransformAes128CbcId;
|
75
|
-
options->key_type = "aes";
|
76
|
-
options->key_bits = 128;
|
77
|
-
} else if (strncmp(AES192_CBC, blockEncryptionValue, blockEncryptionLen) == 0) {
|
78
|
-
options->block_encryption = xmlSecTransformAes192CbcId;
|
79
|
-
options->key_type = "aes";
|
80
|
-
options->key_bits = 192;
|
81
|
-
} else if (strncmp(TRIPLEDES_CBC, blockEncryptionValue, blockEncryptionLen) == 0) {
|
82
|
-
options->block_encryption = xmlSecTransformDes3CbcId;
|
83
|
-
options->key_type = "des";
|
84
|
-
options->key_bits = 192;
|
85
|
-
} else {
|
86
|
-
*rb_exception_result = rb_eArgError;
|
87
|
-
*exception_message = "Unknown :block_encryption value";
|
88
|
-
return FALSE;
|
89
|
-
}
|
90
|
-
|
91
|
-
if (strncmp(RSA1_5, keyTransportValue, keyTransportLen) == 0) {
|
92
|
-
options->key_transport = xmlSecTransformRsaPkcs1Id;
|
93
|
-
} else if (strncmp(RSA_OAEP_MGF1P, keyTransportValue, keyTransportLen) == 0) {
|
94
|
-
options->key_transport = xmlSecTransformRsaOaepId;
|
95
|
-
} else {
|
96
|
-
*rb_exception_result = rb_eArgError;
|
97
|
-
*exception_message = "Unknown :key_transport value";
|
98
|
-
return FALSE;
|
99
|
-
}
|
100
|
-
|
101
|
-
return TRUE;
|
102
|
-
}
|
103
|
-
|
104
|
-
xmlSecTransformId GetSignatureMethod(VALUE rb_signature_alg,
|
105
|
-
VALUE* rb_exception_result,
|
106
|
-
const char** exception_message) {
|
107
|
-
const char* signatureAlgorithm = RSTRING_PTR(rb_signature_alg);
|
108
|
-
unsigned int signatureAlgorithmLength = RSTRING_LEN(rb_signature_alg);
|
109
|
-
|
110
|
-
if (strncmp(RSA_SHA1, signatureAlgorithm, signatureAlgorithmLength) == 0) {
|
111
|
-
return xmlSecTransformRsaSha1Id;
|
112
|
-
} else if (strncmp(RSA_SHA224, signatureAlgorithm, signatureAlgorithmLength) == 0) {
|
113
|
-
return xmlSecTransformRsaSha224Id;
|
114
|
-
} else if (strncmp(RSA_SHA256, signatureAlgorithm, signatureAlgorithmLength) == 0) {
|
115
|
-
return xmlSecTransformRsaSha256Id;
|
116
|
-
} else if (strncmp(RSA_SHA384, signatureAlgorithm, signatureAlgorithmLength) == 0) {
|
117
|
-
return xmlSecTransformRsaSha384Id;
|
118
|
-
} else if (strncmp(RSA_SHA512, signatureAlgorithm, signatureAlgorithmLength) == 0) {
|
119
|
-
return xmlSecTransformRsaSha512Id;
|
120
|
-
|
121
|
-
}
|
122
|
-
#if HAS_ECDSA
|
123
|
-
else if (strncmp(ECDSA_SHA1, signatureAlgorithm, signatureAlgorithmLength) == 0) {
|
124
|
-
return xmlSecTransformEcdsaSha1Id;
|
125
|
-
} else if (strncmp(ECDSA_SHA224, signatureAlgorithm, signatureAlgorithmLength) == 0) {
|
126
|
-
return xmlSecTransformEcdsaSha224Id;
|
127
|
-
} else if (strncmp(ECDSA_SHA256, signatureAlgorithm, signatureAlgorithmLength) == 0) {
|
128
|
-
return xmlSecTransformEcdsaSha256Id;
|
129
|
-
} else if (strncmp(ECDSA_SHA384, signatureAlgorithm, signatureAlgorithmLength) == 0) {
|
130
|
-
return xmlSecTransformEcdsaSha384Id;
|
131
|
-
} else if (strncmp(ECDSA_SHA512, signatureAlgorithm, signatureAlgorithmLength) == 0) {
|
132
|
-
return xmlSecTransformEcdsaSha512Id;
|
133
|
-
} else if (strncmp(DSA_SHA1, signatureAlgorithm, signatureAlgorithmLength) == 0) {
|
134
|
-
return xmlSecTransformDsaSha1Id;
|
135
|
-
} else if (strncmp(DSA_SHA256, signatureAlgorithm, signatureAlgorithmLength) == 0) {
|
136
|
-
return xmlSecTransformDsaSha256Id;
|
137
|
-
}
|
138
|
-
#endif // HAS_ECDSA
|
139
|
-
|
140
|
-
*rb_exception_result = rb_eArgError;
|
141
|
-
*exception_message = "Unknown :signature_alg";
|
142
|
-
return xmlSecTransformIdUnknown;
|
143
|
-
}
|
144
|
-
|
145
|
-
xmlSecTransformId GetDigestMethod(VALUE rb_digest_alg,
|
146
|
-
VALUE* rb_exception_result,
|
147
|
-
const char** exception_message) {
|
148
|
-
const char* digestAlgorithm = RSTRING_PTR(rb_digest_alg);
|
149
|
-
unsigned int digestAlgorithmLength = RSTRING_LEN(rb_digest_alg);
|
150
|
-
|
151
|
-
if (strncmp(DIGEST_SHA1, digestAlgorithm, digestAlgorithmLength) == 0) {
|
152
|
-
return xmlSecTransformSha1Id;
|
153
|
-
} else if (strncmp(DIGEST_SHA224, digestAlgorithm, digestAlgorithmLength) == 0) {
|
154
|
-
return xmlSecTransformSha224Id;
|
155
|
-
} else if (strncmp(DIGEST_SHA256, digestAlgorithm, digestAlgorithmLength) == 0) {
|
156
|
-
return xmlSecTransformSha256Id;
|
157
|
-
} else if (strncmp(DIGEST_SHA384, digestAlgorithm, digestAlgorithmLength) == 0) {
|
158
|
-
return xmlSecTransformSha384Id;
|
159
|
-
} else if (strncmp(DIGEST_SHA512, digestAlgorithm, digestAlgorithmLength) == 0) {
|
160
|
-
return xmlSecTransformSha512Id;
|
161
|
-
}
|
162
|
-
|
163
|
-
*rb_exception_result = rb_eArgError;
|
164
|
-
*exception_message = "Unknown :digest_algorithm";
|
165
|
-
return xmlSecTransformIdUnknown;
|
166
|
-
}
|