1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//!
//! Wrapper for XmlSec Signature Context
//!
use crate::bindings;
use crate::XmlSecKey;
use crate::XmlSecError;
use crate::XmlSecResult;
use crate::XmlNode;
use crate::XmlDocument;
use std::os::raw::c_uchar;
use std::mem::forget;
use std::ptr::null_mut;
/// Signature signing/veryfying context
pub struct XmlSecSignatureContext
{
ctx: *mut bindings::xmlSecDSigCtx,
}
impl XmlSecSignatureContext
{
/// Builds a context, ensuring xmlsec is initialized.
pub fn new() -> Self
{
crate::xmlsec::guarantee_xmlsec_init();
let ctx = unsafe { bindings::xmlSecDSigCtxCreate(null_mut()) };
if ctx.is_null() {
panic!("Failed to create dsig context");
}
Self {ctx}
}
/// Sets the key to use for signature or verification. In case a key had
/// already been set, the latter one gets released in the optional return.
pub fn insert_key(&mut self, key: XmlSecKey) -> Option<XmlSecKey>
{
let mut old = None;
unsafe {
if ! (*self.ctx).signKey.is_null() {
old = Some(XmlSecKey::from_ptr((*self.ctx).signKey));
}
(*self.ctx).signKey = XmlSecKey::leak(key);
}
old
}
/// Releases a currently set key returning `Some(key)` or None otherwise.
pub fn release_key(&mut self) -> Option<XmlSecKey>
{
unsafe {
if (*self.ctx).signKey.is_null() {
None
} else {
let key = XmlSecKey::from_ptr((*self.ctx).signKey);
(*self.ctx).signKey = null_mut();
Some(key)
}
}
}
/// UNTESTED
pub fn sign_node(&self, node: &XmlNode) -> XmlSecResult<()>
{
self.key_is_set()?;
let node = node.node_ptr() as bindings::xmlNodePtr;
self.sign_node_raw(node)
}
/// Takes a [`XmlDocument`][xmldoc] and attempts to sign it. For this to work it has to have a properly structured
/// `<dsig:Signature>` node within, and a XmlSecKey must have been previously set with [`insert_key`][inskey].
///
/// # Errors
///
/// If key has not been previously set or document is malformed.
///
/// [xmldoc]: http://kwarc.github.io/rust-libxml/libxml/tree/document/struct.Document.html
/// [inskey]: struct.XmlSecSignatureContext.html#method.insert_key
pub fn sign_document(&self, doc: &XmlDocument) -> XmlSecResult<()>
{
self.key_is_set()?;
let root = find_root(doc)?;
let sig = find_signode(root)?;
self.sign_node_raw(sig)
}
/// UNTESTED
pub fn verify_node(&self, node: &XmlNode) -> XmlSecResult<bool>
{
self.key_is_set()?;
let node = node.node_ptr() as bindings::xmlNodePtr;
self.verify_node_raw(node)
}
/// Takes a [`XmlDocument`][xmldoc] and attempts to verify its signature. For this to work it has to have a properly
/// structured and signed `<dsig:Signature>` node within, and a XmlSecKey must have been previously set with
/// [`insert_key`][inskey].
///
/// # Errors
///
/// If key has not been previously set or document is malformed.
///
/// [xmldoc]: http://kwarc.github.io/rust-libxml/libxml/tree/document/struct.Document.html
/// [inskey]: struct.XmlSecSignatureContext.html#method.insert_key
pub fn verify_document(&self, doc: &XmlDocument) -> XmlSecResult<bool>
{
self.key_is_set()?;
let root = find_root(doc)?;
let sig = find_signode(root)?;
self.verify_node_raw(sig)
}
/// # Safety
///
/// Returns a raw pointer to the underlying xmlsec signature context. Beware that it is still managed by this
/// wrapping object and will be deallocated once `self` gets dropped.
pub unsafe fn as_ptr(&self) -> *mut bindings::xmlSecDSigCtx
{
self.ctx
}
/// # Safety
///
/// Returns a raw pointer to the underlying xmlsec signature context. Beware that it will be forgotten by this
/// wrapping object and *must* be deallocated manually by the callee.
pub unsafe fn into_ptr(self) -> *mut bindings::xmlSecDSigCtx
{
let ctx = self.ctx; // keep a copy of the pointer
forget(self); // release our copy of the pointer without deallocating it
ctx // return the only remaining copy
}
}
impl XmlSecSignatureContext
{
fn key_is_set(&self) -> XmlSecResult<()>
{
unsafe {
if ! (*self.ctx).signKey.is_null() {
Ok(())
} else {
Err(XmlSecError::KeyNotLoaded)
}
}
}
fn sign_node_raw(&self, node: *mut bindings::xmlNode) -> XmlSecResult<()>
{
let rc = unsafe { bindings::xmlSecDSigCtxSign(self.ctx, node) };
if rc < 0 {
Err(XmlSecError::SigningError)
} else {
Ok(())
}
}
fn verify_node_raw(&self, node: *mut bindings::xmlNode) -> XmlSecResult<bool>
{
let rc = unsafe { bindings::xmlSecDSigCtxVerify(self.ctx, node) };
if rc < 0 {
return Err(XmlSecError::VerifyError);
}
match unsafe { (*self.ctx).status }
{
bindings::xmlSecDSigStatus_xmlSecDSigStatusUnknown => Ok(false),
bindings::xmlSecDSigStatus_xmlSecDSigStatusSucceeded => Ok(true),
bindings::xmlSecDSigStatus_xmlSecDSigStatusInvalid => Ok(false),
_ => panic!("Failed to interprete xmlSecDSigStatus code")
}
}
}
impl Drop for XmlSecSignatureContext
{
fn drop(&mut self)
{
unsafe { bindings::xmlSecDSigCtxDestroy(self.ctx) };
}
}
fn find_root(doc: &XmlDocument) -> XmlSecResult<*mut bindings::xmlNode>
{
if let Some(root) = doc.get_root_element()
{
let rawroot = root.node_ptr() as *mut bindings::xmlNode;
let signode = find_signode(rawroot)?;
Ok(signode)
} else {
Err(XmlSecError::RootNotFound)
}
}
fn find_signode(tree: *mut bindings::xmlNode) -> XmlSecResult<*mut bindings::xmlNode>
{
let signode = unsafe {bindings::xmlSecFindNode(
tree,
&bindings::xmlSecNodeSignature as *const c_uchar,
&bindings::xmlSecDSigNs as *const c_uchar,
) };
if signode.is_null() {
return Err(XmlSecError::NodeNotFound);
}
Ok(signode)
}