1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 /*
21  * XSEC
22  *
23  * XKMSRevokeRequestImpl := Implementation for RegisterRequest Messages
24  *
25  * $Id:$
26  *
27  */
28 
29 // XSEC Includes
30 
31 #include <xsec/dsig/DSIGReference.hpp>
32 #include <xsec/framework/XSECDefs.hpp>
33 #include <xsec/framework/XSECEnv.hpp>
34 #include <xsec/framework/XSECError.hpp>
35 
36 #ifdef XSEC_XKMS_ENABLED
37 
38 #include "../../utils/XSECDOMUtils.hpp"
39 
40 #include "XKMSRevokeRequestImpl.hpp"
41 #include "XKMSRevokeKeyBindingImpl.hpp"
42 #include "XKMSAuthenticationImpl.hpp"
43 
44 #include <xsec/xkms/XKMSConstants.hpp>
45 #include <xsec/xkms/XKMSStatus.hpp>
46 
47 #include <xercesc/dom/DOM.hpp>
48 #include <xercesc/util/XMLUniDefs.hpp>
49 
50 XERCES_CPP_NAMESPACE_USE
51 
52 // --------------------------------------------------------------------------------
53 //           Construct/Destruct
54 // --------------------------------------------------------------------------------
55 
XKMSRevokeRequestImpl(const XSECEnv * env)56 XKMSRevokeRequestImpl::XKMSRevokeRequestImpl(const XSECEnv * env) :
57 m_request(env),
58 m_msg(m_request.m_msg),
59 mp_authentication(NULL),
60 mp_revokeKeyBinding(NULL),
61 mp_revocationCodeElement(NULL) {
62 }
63 
XKMSRevokeRequestImpl(const XSECEnv * env,DOMElement * node)64 XKMSRevokeRequestImpl::XKMSRevokeRequestImpl(const XSECEnv * env, DOMElement * node) :
65 m_request(env, node),
66 m_msg(m_request.m_msg),
67 mp_authentication(NULL),
68 mp_revokeKeyBinding(NULL),
69 mp_revocationCodeElement(NULL) {
70 }
71 
~XKMSRevokeRequestImpl()72 XKMSRevokeRequestImpl::~XKMSRevokeRequestImpl() {
73 
74 	if (mp_authentication != NULL)
75 		delete mp_authentication;
76 	if (mp_revokeKeyBinding != NULL)
77 		delete mp_revokeKeyBinding;
78 
79 }
80 
81 // --------------------------------------------------------------------------------
82 //           Load
83 // --------------------------------------------------------------------------------
84 
load(void)85 void XKMSRevokeRequestImpl::load(void) {
86 
87 	if (m_msg.mp_messageAbstractTypeElement == NULL) {
88 
89 		// Attempt to load an empty element
90 		throw XSECException(XSECException::XKMSError,
91 			"XKMSRevokeRequest::load - called on empty DOM");
92 
93 	}
94 
95 	if (!strEquals(getXKMSLocalName(m_msg.mp_messageAbstractTypeElement),
96 									XKMSConstants::s_tagRevokeRequest)) {
97 
98 		throw XSECException(XSECException::XKMSError,
99 			"XKMSRevokeRequest::load - called on incorrect node");
100 
101 	}
102 
103 	// Load the base message
104 	m_request.load();
105 
106 	// Now check for any RevokeKeyBinding elements
107 	DOMElement * tmpElt = findFirstElementChild(m_msg.mp_messageAbstractTypeElement);
108 	while (tmpElt != NULL && !strEquals(getXKMSLocalName(tmpElt), XKMSConstants::s_tagRevokeKeyBinding)) {
109 		tmpElt = findNextElementChild(tmpElt);
110 	}
111 
112 	if (tmpElt != NULL) {
113 
114 		XSECnew(mp_revokeKeyBinding, XKMSRevokeKeyBindingImpl(m_msg.mp_env, tmpElt));
115 		mp_revokeKeyBinding->load();
116 
117 		tmpElt = findNextElementChild(tmpElt);
118 
119 	}
120 	else {
121 
122 		throw XSECException(XSECException::ExpectedXKMSChildNotFound,
123 			"XKMSRevokeRequest::load - Expected RevokeKeyBinding node");
124 
125 	}
126 
127 	// Authentication Element | RevocationCode Element
128 
129 	if (tmpElt != NULL && strEquals(getXKMSLocalName(tmpElt), XKMSConstants::s_tagAuthentication)) {
130 
131 		XSECnew(mp_authentication, XKMSAuthenticationImpl(m_msg.mp_env, tmpElt));
132 		mp_authentication->load(mp_revokeKeyBinding->getId());
133 
134 	}
135 	else if (tmpElt != NULL && strEquals(getXKMSLocalName(tmpElt), XKMSConstants::s_tagRevocationCode)) {
136 
137 		mp_revocationCodeElement = tmpElt;
138 
139 	}
140 	else {
141 
142 		throw XSECException(XSECException::ExpectedXKMSChildNotFound,
143 			"XKMSRevokeRequest::load - Expected Authentication or RevocationCode nodes");
144 
145 	}
146 
147 }
148 
149 
150 // --------------------------------------------------------------------------------
151 //           Create
152 // --------------------------------------------------------------------------------
153 
154 DOMElement * XKMSRevokeRequestImpl::
createBlankRevokeRequest(const XMLCh * service,const XMLCh * id)155 	createBlankRevokeRequest(const XMLCh * service, const XMLCh * id) {
156 
157 	return m_request.createBlankRequestAbstractType(
158 		XKMSConstants::s_tagRevokeRequest, service, id);
159 
160 }
161 
162 // --------------------------------------------------------------------------------
163 //           MessageType
164 // --------------------------------------------------------------------------------
165 
getMessageType(void)166 XKMSMessageAbstractType::messageType XKMSRevokeRequestImpl::getMessageType(void) {
167 
168 	return XKMSMessageAbstractTypeImpl::RevokeRequest;
169 
170 }
171 
172 // --------------------------------------------------------------------------------
173 //           Get Methods
174 // --------------------------------------------------------------------------------
175 
getRevokeKeyBinding(void) const176 XKMSRevokeKeyBinding * XKMSRevokeRequestImpl::getRevokeKeyBinding(void) const {
177 
178 	return mp_revokeKeyBinding;
179 
180 }
181 
getAuthentication(void) const182 XKMSAuthentication * XKMSRevokeRequestImpl::getAuthentication (void) const {
183 
184 	return mp_authentication;
185 
186 }
187 
getRevocationCode(void) const188 const XMLCh * XKMSRevokeRequestImpl::getRevocationCode(void) const {
189 
190 	if (mp_revocationCodeElement == NULL)
191 		return NULL;
192 
193 	DOMNode * t = findFirstChildOfType(mp_revocationCodeElement, DOMNode::TEXT_NODE);
194 
195 	if (t == NULL) {
196 		throw XSECException(XSECException::ExpectedXKMSChildNotFound,
197 			"XKMSRevokeRequestImpl::getRevocationCode - expected TEXT node");
198 	}
199 
200 	return t->getNodeValue();
201 }
202 
203 // --------------------------------------------------------------------------------
204 //           Set Methods
205 // --------------------------------------------------------------------------------
206 
addRevokeKeyBinding(XKMSStatus::StatusValue status)207 XKMSRevokeKeyBinding * XKMSRevokeRequestImpl::addRevokeKeyBinding(XKMSStatus::StatusValue status) {
208 
209 	if (mp_revokeKeyBinding != NULL)
210 		return mp_revokeKeyBinding;
211 
212 
213 	// OK - Nothing exists, so we need to create from scratch
214 
215 	XSECnew(mp_revokeKeyBinding, XKMSRevokeKeyBindingImpl(m_msg.mp_env));
216 	DOMElement * elt = mp_revokeKeyBinding->createBlankRevokeKeyBinding(status);
217 
218 	// Insert
219 
220 	DOMElement * be = findFirstElementChild(m_msg.mp_messageAbstractTypeElement);
221 
222 	while (be != NULL &&
223 		!strEquals(getXKMSLocalName(be), XKMSConstants::s_tagAuthentication) &&
224 		!strEquals(getXKMSLocalName(be), XKMSConstants::s_tagRevocationCode)) {
225 		be = findNextElementChild(be);
226 	}
227 
228 	if (be == NULL) {
229 		m_msg.mp_env->doPrettyPrint(m_msg.mp_messageAbstractTypeElement);
230 		m_msg.mp_messageAbstractTypeElement->appendChild(elt);
231 		m_msg.mp_env->doPrettyPrint(m_msg.mp_messageAbstractTypeElement);
232 		return mp_revokeKeyBinding;
233 	}
234 
235 	m_msg.mp_messageAbstractTypeElement->insertBefore(elt, be);
236 	if (m_msg.mp_env->getPrettyPrintFlag() == true) {
237 		m_msg.mp_messageAbstractTypeElement->insertBefore(
238 			m_msg.mp_env->getParentDocument()->createTextNode(DSIGConstants::s_unicodeStrNL),
239 			be);
240 	}
241 
242 	return mp_revokeKeyBinding;
243 
244 }
245 
addAuthentication(void)246 XKMSAuthentication * XKMSRevokeRequestImpl::addAuthentication(void) {
247 
248 	if (mp_authentication != NULL)
249 		return mp_authentication;
250 
251 	if (mp_revokeKeyBinding == NULL) {
252 		throw XSECException(XSECException::XKMSError,
253 			"XKMSRevokeRequestImpl::addAuthentication - called prior to key infos being added");
254 	}
255 
256 	XSECnew(mp_authentication, XKMSAuthenticationImpl(m_msg.mp_env));
257 	DOMElement * e =
258 		mp_authentication->createBlankAuthentication(mp_revokeKeyBinding->getId());
259 
260 	DOMElement * be = findFirstElementChild(m_msg.mp_messageAbstractTypeElement);
261 
262 	while (be != NULL && !strEquals(getXKMSLocalName(be), XKMSConstants::s_tagRevocationCode))
263 		be = findNextElementChild(be);
264 
265 	if (be == NULL) {
266 		m_msg.mp_env->doPrettyPrint(m_msg.mp_messageAbstractTypeElement);
267 		m_msg.mp_messageAbstractTypeElement->appendChild(e);
268 		m_msg.mp_env->doPrettyPrint(m_msg.mp_messageAbstractTypeElement);
269 		return mp_authentication;
270 	}
271 
272 	m_msg.mp_messageAbstractTypeElement->insertBefore(e, be);
273 	if (m_msg.mp_env->getPrettyPrintFlag() == true) {
274 		m_msg.mp_messageAbstractTypeElement->insertBefore(
275 			m_msg.mp_env->getParentDocument()->createTextNode(DSIGConstants::s_unicodeStrNL),
276 			be);
277 	}
278 
279 	return mp_authentication;
280 
281 }
282 
addRevocationCode(const XMLCh * code)283 void XKMSRevokeRequestImpl::addRevocationCode(const XMLCh * code) {
284 
285 	safeBuffer str;
286 	DOMDocument *doc = m_msg.mp_env->getParentDocument();
287 	const XMLCh * prefix = m_msg.mp_env->getXKMSNSPrefix();
288 
289 	makeQName(str, prefix, XKMSConstants::s_tagRevocationCode);
290 
291 	mp_revocationCodeElement = doc->createElementNS(XKMSConstants::s_unicodeStrURIXKMS,
292 												str.rawXMLChBuffer());
293 
294 	m_msg.mp_messageAbstractTypeElement->appendChild(mp_revocationCodeElement);
295 	m_msg.mp_env->doPrettyPrint(m_msg.mp_messageAbstractTypeElement);
296 
297 	mp_revocationCodeElement->appendChild(
298 		m_msg.mp_env->getParentDocument()->createTextNode(code));
299 
300 }
301 
302 #endif /* XSEC_XKMS_ENABLED */
303