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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* XSEC
*
* DSIGKeyName := Simply a string that names a key for an application to read
*
* Author(s): Berin Lautenbach
*
* $Id: DSIGKeyInfoPGPData.cpp 1125514 2011-05-20 19:08:33Z scantor $
*
*/
#include <xsec/dsig/DSIGKeyInfoPGPData.hpp>
#include <xsec/framework/XSECError.hpp>
#include <xsec/utils/XSECPlatformUtils.hpp>
#include <xsec/utils/XSECDOMUtils.hpp>
#include <xsec/dsig/DSIGSignature.hpp>
#include <xsec/framework/XSECEnv.hpp>
#include <xercesc/util/Janitor.hpp>
XERCES_CPP_NAMESPACE_USE
// --------------------------------------------------------------------------------
// Constructors and Destructors
// --------------------------------------------------------------------------------
DSIGKeyInfoPGPData::DSIGKeyInfoPGPData(const XSECEnv * env, DOMNode *pgpDataNode) :
DSIGKeyInfo(env),
mp_keyID(NULL),
mp_keyPacket(NULL),
mp_keyIDTextNode(NULL),
mp_keyPacketTextNode(NULL) {
mp_keyInfoDOMNode = pgpDataNode;
}
DSIGKeyInfoPGPData::DSIGKeyInfoPGPData(const XSECEnv * env) :
DSIGKeyInfo(env),
mp_keyID(NULL),
mp_keyPacket(NULL),
mp_keyIDTextNode(NULL),
mp_keyPacketTextNode(NULL) {
mp_keyInfoDOMNode = 0;
}
DSIGKeyInfoPGPData::~DSIGKeyInfoPGPData() {
};
// --------------------------------------------------------------------------------
// Load and Get functions
// --------------------------------------------------------------------------------
void DSIGKeyInfoPGPData::load(void) {
// Assuming we have a valid DOM_Node to start with, load the signing key so that it can
// be used later on
if (mp_keyInfoDOMNode == NULL) {
// Attempt to load an empty signature element
throw XSECException(XSECException::LoadEmptyInfoName);
}
if (!strEquals(getDSIGLocalName(mp_keyInfoDOMNode), "PGPData")) {
throw XSECException(XSECException::ExpectedDSIGChildNotFound,
"Expected a PGPData node");
}
// Now find the first element node containing either KeyID or KeyPacket
DOMNode * tmpElt = findFirstChildOfType(mp_keyInfoDOMNode, DOMNode::ELEMENT_NODE);
if (tmpElt == 0) {
throw XSECException(XSECException::ExpectedDSIGChildNotFound,
"Expected <PGPKeyID> or <PGPKeyPacket> children of PGPData node");
}
if (strEquals(getDSIGLocalName(tmpElt), "PGPKeyID")) {
// Find the text node
mp_keyIDTextNode = findFirstChildOfType(tmpElt, DOMNode::TEXT_NODE);
if (mp_keyIDTextNode == NULL) {
throw XSECException(XSECException::ExpectedDSIGChildNotFound,
"Expected a text node beneath PGPKeyID");
}
mp_keyID = mp_keyIDTextNode->getNodeValue();
do {
tmpElt = tmpElt->getNextSibling();
} while (tmpElt != NULL && tmpElt->getNodeType() != DOMNode::ELEMENT_NODE);
}
if (tmpElt != NULL && strEquals(getDSIGLocalName(tmpElt), "PGPKeyPacket")) {
// Find the text node
mp_keyPacketTextNode = findFirstChildOfType(tmpElt, DOMNode::TEXT_NODE);
if (mp_keyPacketTextNode == NULL) {
throw XSECException(XSECException::ExpectedDSIGChildNotFound,
"Expected a text node beneath PGPKeyPacket");
}
mp_keyPacket = mp_keyPacketTextNode->getNodeValue();
}
if (mp_keyPacketTextNode == NULL && mp_keyIDTextNode == NULL) {
throw XSECException(XSECException::ExpectedDSIGChildNotFound,
"Expected <PGPKeyID> or <PGPKeyPacket> children of PGPData node");
}
}
// --------------------------------------------------------------------------------
// Create and Set functions
// --------------------------------------------------------------------------------
DOMElement * DSIGKeyInfoPGPData::createBlankPGPData(const XMLCh * id, const XMLCh * packet) {
// Create the DOM Structure
safeBuffer str;
DOMDocument *doc = mp_env->getParentDocument();
const XMLCh * prefix = mp_env->getDSIGNSPrefix();
makeQName(str, prefix, "PGPData");
DOMElement *ret = doc->createElementNS(DSIGConstants::s_unicodeStrURIDSIG, str.rawXMLChBuffer());
mp_keyInfoDOMNode = ret;
mp_env->doPrettyPrint(ret);
if (id != NULL) {
makeQName(str, prefix, "PGPKeyID");
DOMElement * t = doc->createElementNS(DSIGConstants::s_unicodeStrURIDSIG, str.rawXMLChBuffer());
ret->appendChild(t);
mp_env->doPrettyPrint(ret);
mp_keyIDTextNode = doc->createTextNode(id);
t->appendChild(mp_keyIDTextNode);
mp_keyID = mp_keyIDTextNode->getNodeValue();
}
if (packet != NULL) {
makeQName(str, prefix, "PGPKeyPacket");
DOMElement * t = doc->createElementNS(DSIGConstants::s_unicodeStrURIDSIG, str.rawXMLChBuffer());
ret->appendChild(t);
mp_env->doPrettyPrint(ret);
mp_keyPacketTextNode = doc->createTextNode(packet);
t->appendChild(mp_keyPacketTextNode);
mp_keyPacket = mp_keyPacketTextNode->getNodeValue();
}
return ret;
}
void DSIGKeyInfoPGPData::setKeyID(const XMLCh * id) {
if (mp_keyInfoDOMNode == NULL) {
throw XSECException(XSECException::KeyInfoError,
"DSIGKeyInfoPGPData::setKeyID() called prior to load or createBlank");
}
if (mp_keyIDTextNode == 0) {
// Need to create
safeBuffer str;
DOMDocument *doc = mp_env->getParentDocument();
const XMLCh * prefix = mp_env->getDSIGNSPrefix();
makeQName(str, prefix, "PGPKeyID");
DOMElement * t = doc->createElementNS(DSIGConstants::s_unicodeStrURIDSIG, str.rawXMLChBuffer());
DOMNode * pkt = findFirstChildOfType(mp_keyInfoDOMNode, DOMNode::ELEMENT_NODE);
if (pkt != NULL) {
mp_keyInfoDOMNode->insertBefore(t, pkt);
if (mp_env->getPrettyPrintFlag() == true)
mp_keyInfoDOMNode->insertBefore(doc->createTextNode(DSIGConstants::s_unicodeStrNL), pkt);
}
else {
mp_keyInfoDOMNode->appendChild(t);
mp_env->doPrettyPrint(mp_keyInfoDOMNode);
}
mp_keyIDTextNode = doc->createTextNode(id);
t->appendChild(mp_keyIDTextNode);
}
else
mp_keyIDTextNode->setNodeValue(id);
mp_keyID = mp_keyIDTextNode->getNodeValue();
}
void DSIGKeyInfoPGPData::setKeyPacket(const XMLCh * packet) {
if (mp_keyInfoDOMNode == NULL) {
throw XSECException(XSECException::KeyInfoError,
"DSIGKeyInfoPGPData::setKeyID() called prior to load or createBlank");
}
if (mp_keyPacketTextNode == 0) {
// Need to create
safeBuffer str;
DOMDocument *doc = mp_env->getParentDocument();
const XMLCh * prefix = mp_env->getDSIGNSPrefix();
makeQName(str, prefix, "PGPKeyPacket");
DOMElement * t = doc->createElementNS(DSIGConstants::s_unicodeStrURIDSIG, str.rawXMLChBuffer());
mp_keyInfoDOMNode->appendChild(t);
mp_env->doPrettyPrint(mp_keyInfoDOMNode);
mp_keyPacketTextNode = doc->createTextNode(packet);
t->appendChild(mp_keyPacketTextNode);
}
else
mp_keyPacketTextNode->setNodeValue(packet);
mp_keyPacket = mp_keyPacketTextNode->getNodeValue();
}
|