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  * XKMSRecoverRequestImpl := 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 "XKMSRecoverRequestImpl.hpp"
41 #include "XKMSRecoverKeyBindingImpl.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 
XKMSRecoverRequestImpl(const XSECEnv * env)56 XKMSRecoverRequestImpl::XKMSRecoverRequestImpl(const XSECEnv * env) :
57 m_request(env),
58 m_msg(m_request.m_msg),
59 mp_authentication(NULL),
60 mp_recoverKeyBinding(NULL) {
61 }
62 
XKMSRecoverRequestImpl(const XSECEnv * env,DOMElement * node)63 XKMSRecoverRequestImpl::XKMSRecoverRequestImpl(const XSECEnv * env, DOMElement * node) :
64 m_request(env, node),
65 m_msg(m_request.m_msg),
66 mp_authentication(NULL),
67 mp_recoverKeyBinding(NULL) {
68 }
69 
~XKMSRecoverRequestImpl()70 XKMSRecoverRequestImpl::~XKMSRecoverRequestImpl() {
71 
72 	if (mp_authentication != NULL)
73 		delete mp_authentication;
74 	if (mp_recoverKeyBinding != NULL)
75 		delete mp_recoverKeyBinding;
76 
77 }
78 
79 // --------------------------------------------------------------------------------
80 //           Load
81 // --------------------------------------------------------------------------------
82 
load(void)83 void XKMSRecoverRequestImpl::load(void) {
84 
85 	if (m_msg.mp_messageAbstractTypeElement == NULL) {
86 
87 		// Attempt to load an empty element
88 		throw XSECException(XSECException::XKMSError,
89 			"XKMSRecoverRequest::load - called on empty DOM");
90 
91 	}
92 
93 	if (!strEquals(getXKMSLocalName(m_msg.mp_messageAbstractTypeElement),
94 									XKMSConstants::s_tagRecoverRequest)) {
95 
96 		throw XSECException(XSECException::XKMSError,
97 			"XKMSRecoverRequest::load - called on incorrect node");
98 
99 	}
100 
101 	// Load the base message
102 	m_request.load();
103 
104 	// Now check for any RecoverKeyBinding elements
105 	DOMElement * tmpElt = findFirstElementChild(m_msg.mp_messageAbstractTypeElement);
106 	while (tmpElt != NULL && !strEquals(getXKMSLocalName(tmpElt), XKMSConstants::s_tagRecoverKeyBinding)) {
107 		tmpElt = findNextElementChild(tmpElt);
108 	}
109 
110 	if (tmpElt != NULL) {
111 
112 		XSECnew(mp_recoverKeyBinding, XKMSRecoverKeyBindingImpl(m_msg.mp_env, tmpElt));
113 		mp_recoverKeyBinding->load();
114 
115 		tmpElt = findNextElementChild(tmpElt);
116 
117 	}
118 	else {
119 
120 		throw XSECException(XSECException::ExpectedXKMSChildNotFound,
121 			"XKMSRecoverRequest::load - Expected RecoverKeyBinding node");
122 
123 	}
124 
125 	// Authentication Element
126 
127 	if (tmpElt != NULL && strEquals(getXKMSLocalName(tmpElt), XKMSConstants::s_tagAuthentication)) {
128 
129 		XSECnew(mp_authentication, XKMSAuthenticationImpl(m_msg.mp_env, tmpElt));
130 		mp_authentication->load(mp_recoverKeyBinding->getId());
131 
132 	}
133 	else {
134 
135 		throw XSECException(XSECException::ExpectedXKMSChildNotFound,
136 			"XKMSRecoverRequest::load - Expected Authentication node");
137 
138 	}
139 
140 }
141 
142 
143 // --------------------------------------------------------------------------------
144 //           Create
145 // --------------------------------------------------------------------------------
146 
147 DOMElement * XKMSRecoverRequestImpl::
createBlankRecoverRequest(const XMLCh * service,const XMLCh * id)148 	createBlankRecoverRequest(const XMLCh * service, const XMLCh * id) {
149 
150 	return m_request.createBlankRequestAbstractType(
151 		XKMSConstants::s_tagRecoverRequest, service, id);
152 
153 }
154 
155 // --------------------------------------------------------------------------------
156 //           MessageType
157 // --------------------------------------------------------------------------------
158 
getMessageType(void)159 XKMSMessageAbstractType::messageType XKMSRecoverRequestImpl::getMessageType(void) {
160 
161 	return XKMSMessageAbstractTypeImpl::RecoverRequest;
162 
163 }
164 
165 // --------------------------------------------------------------------------------
166 //           Get Methods
167 // --------------------------------------------------------------------------------
168 
getRecoverKeyBinding(void) const169 XKMSRecoverKeyBinding * XKMSRecoverRequestImpl::getRecoverKeyBinding(void) const {
170 
171 	return mp_recoverKeyBinding;
172 
173 }
174 
getAuthentication(void) const175 XKMSAuthentication * XKMSRecoverRequestImpl::getAuthentication (void) const {
176 
177 	return mp_authentication;
178 
179 }
180 
181 // --------------------------------------------------------------------------------
182 //           Set Methods
183 // --------------------------------------------------------------------------------
184 
addRecoverKeyBinding(XKMSStatus::StatusValue status)185 XKMSRecoverKeyBinding * XKMSRecoverRequestImpl::addRecoverKeyBinding(XKMSStatus::StatusValue status) {
186 
187 	if (mp_recoverKeyBinding != NULL)
188 		return mp_recoverKeyBinding;
189 
190 
191 	// OK - Nothing exists, so we need to create from scratch
192 
193 	XSECnew(mp_recoverKeyBinding, XKMSRecoverKeyBindingImpl(m_msg.mp_env));
194 	DOMElement * elt = mp_recoverKeyBinding->createBlankRecoverKeyBinding(status);
195 
196 	// Insert
197 
198 	DOMElement * be = findFirstElementChild(m_msg.mp_messageAbstractTypeElement);
199 
200 	while (be != NULL &&
201 		!strEquals(getXKMSLocalName(be), XKMSConstants::s_tagAuthentication) &&
202 		!strEquals(getXKMSLocalName(be), XKMSConstants::s_tagRevocationCode)) {
203 		be = findNextElementChild(be);
204 	}
205 
206 	if (be == NULL) {
207 		m_msg.mp_env->doPrettyPrint(m_msg.mp_messageAbstractTypeElement);
208 		m_msg.mp_messageAbstractTypeElement->appendChild(elt);
209 		m_msg.mp_env->doPrettyPrint(m_msg.mp_messageAbstractTypeElement);
210 		return mp_recoverKeyBinding;
211 	}
212 
213 	m_msg.mp_messageAbstractTypeElement->insertBefore(elt, be);
214 	if (m_msg.mp_env->getPrettyPrintFlag() == true) {
215 		m_msg.mp_messageAbstractTypeElement->insertBefore(
216 			m_msg.mp_env->getParentDocument()->createTextNode(DSIGConstants::s_unicodeStrNL),
217 			be);
218 	}
219 
220 	return mp_recoverKeyBinding;
221 
222 }
223 
addAuthentication(void)224 XKMSAuthentication * XKMSRecoverRequestImpl::addAuthentication(void) {
225 
226 	if (mp_authentication != NULL)
227 		return mp_authentication;
228 
229 	if (mp_recoverKeyBinding == NULL) {
230 		throw XSECException(XSECException::XKMSError,
231 			"XKMSRecoverRequestImpl::addAuthentication - called prior to key infos being added");
232 	}
233 
234 	XSECnew(mp_authentication, XKMSAuthenticationImpl(m_msg.mp_env));
235 	DOMElement * e =
236 		mp_authentication->createBlankAuthentication(mp_recoverKeyBinding->getId());
237 
238 	DOMElement * be = findFirstElementChild(m_msg.mp_messageAbstractTypeElement);
239 
240 	while (be != NULL && !strEquals(getXKMSLocalName(be), XKMSConstants::s_tagRevocationCode))
241 		be = findNextElementChild(be);
242 
243 	if (be == NULL) {
244 		m_msg.mp_env->doPrettyPrint(m_msg.mp_messageAbstractTypeElement);
245 		m_msg.mp_messageAbstractTypeElement->appendChild(e);
246 		m_msg.mp_env->doPrettyPrint(m_msg.mp_messageAbstractTypeElement);
247 		return mp_authentication;
248 	}
249 
250 	m_msg.mp_messageAbstractTypeElement->insertBefore(e, be);
251 	if (m_msg.mp_env->getPrettyPrintFlag() == true) {
252 		m_msg.mp_messageAbstractTypeElement->insertBefore(
253 			m_msg.mp_env->getParentDocument()->createTextNode(DSIGConstants::s_unicodeStrNL),
254 			be);
255 	}
256 
257 	return mp_authentication;
258 
259 }
260 
261 #endif /* XSEC_XKMS_ENABLED */
262