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 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
/**
* 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
*
* XSECAlgorithmSupport := internal helpers for mapping from W3C/IETF algorithm URIs
*/
// XSEC
#include <xsec/dsig/DSIGConstants.hpp>
#include <xsec/enc/XSECCryptoKeyDSA.hpp>
#include <xsec/enc/XSECCryptoKeyRSA.hpp>
#include <xsec/enc/XSECCryptoKeyEC.hpp>
#include <xsec/enc/XSECCryptoKeyHMAC.hpp>
#include "../utils/XSECAlgorithmSupport.hpp"
#include <xercesc/util/XMLString.hpp>
#include <xercesc/util/XMLUniDefs.hpp>
XERCES_CPP_NAMESPACE_USE
// --------------------------------------------------------------------------------
// Some useful defines
// --------------------------------------------------------------------------------
static XMLCh s_dsa[] = {
chLatin_d,
chLatin_s,
chLatin_a,
chNull
};
static XMLCh s_rsa[] = {
chLatin_r,
chLatin_s,
chLatin_a,
chNull
};
static XMLCh s_ecdsa[] = {
chLatin_e,
chLatin_c,
chLatin_d,
chLatin_s,
chLatin_a,
chNull
};
static XMLCh s_hmac[] = {
chLatin_h,
chLatin_m,
chLatin_a,
chLatin_c,
chNull
};
static XMLCh s_sha1[] = {
chLatin_s,
chLatin_h,
chLatin_a,
chDigit_1,
chNull
};
static XMLCh s_sha224[] = {
chLatin_s,
chLatin_h,
chLatin_a,
chDigit_2,
chDigit_2,
chDigit_4,
chNull
};
static XMLCh s_sha256[] = {
chLatin_s,
chLatin_h,
chLatin_a,
chDigit_2,
chDigit_5,
chDigit_6,
chNull
};
static XMLCh s_sha384[] = {
chLatin_s,
chLatin_h,
chLatin_a,
chDigit_3,
chDigit_8,
chDigit_4,
chNull
};
static XMLCh s_sha512[] = {
chLatin_s,
chLatin_h,
chLatin_a,
chDigit_5,
chDigit_1,
chDigit_2,
chNull
};
static XMLCh s_md5[] = {
chLatin_m,
chLatin_d,
chDigit_5,
chNull
};
// --------------------------------------------------------------------------------
// URI Mappings
// --------------------------------------------------------------------------------
static bool getHashType(const XMLCh* URI, XSECCryptoHash::HashType& type)
{
if (XMLString::equals(URI, s_md5)) {
type = XSECCryptoHash::HASH_MD5;
return true;
}
if (XMLString::equals(URI, s_sha1)) {
type = XSECCryptoHash::HASH_SHA1;
return true;
}
if (XMLString::equals(URI, s_sha224)) {
type = XSECCryptoHash::HASH_SHA224;
return true;
}
if (XMLString::equals(URI, s_sha256)) {
type = XSECCryptoHash::HASH_SHA256;
return true;
}
if (XMLString::equals(URI, s_sha384)) {
type = XSECCryptoHash::HASH_SHA384;
return true;
}
if (XMLString::equals(URI, s_sha512)) {
type = XSECCryptoHash::HASH_SHA512;
return true;
}
type = XSECCryptoHash::HASH_NONE;
return false;
}
XSECCryptoHash::HashType XSECAlgorithmSupport::getHashType(const XMLCh* uri)
{
XSECCryptoHash::HashType type = XSECCryptoHash::HASH_NONE;
// Check this is a known prefix on the URI.
XMLSize_t blen = XMLString::stringLen(DSIGConstants::s_unicodeStrURISIGBASE);
XMLSize_t bmlen = XMLString::stringLen(DSIGConstants::s_unicodeStrURISIGBASEMORE);
XMLSize_t belen = XMLString::stringLen(DSIGConstants::s_unicodeStrURIXENC);
if (XMLString::compareNString(uri, DSIGConstants::s_unicodeStrURISIGBASE, blen) == 0) {
// This is actually cheating - this will return SHA256 (as an example), even if
// the base URI is the original DSIG uri (ie not base-more)
::getHashType(&uri[blen], type);
}
else if (XMLString::compareNString(uri, DSIGConstants::s_unicodeStrURISIGBASEMORE, bmlen) == 0) {
::getHashType(&uri[bmlen], type);
}
else if (XMLString::compareNString(uri, DSIGConstants::s_unicodeStrURIXENC, belen) == 0) {
::getHashType(&uri[belen], type);
}
return type;
}
bool XSECAlgorithmSupport::evalSignatureMethod(
const XMLCh* uri, const XSECCryptoKey* key, XSECCryptoHash::HashType& hashType
)
{
if (!key) {
return false;
}
// The easy ones!
if (XMLString::equals(uri, DSIGConstants::s_unicodeStrURIDSA_SHA1)) {
hashType = XSECCryptoHash::HASH_SHA1;
return dynamic_cast<const XSECCryptoKeyDSA*>(key) != NULL;
}
if (XMLString::equals(uri, DSIGConstants::s_unicodeStrURIRSA_SHA1)) {
hashType = XSECCryptoHash::HASH_SHA1;
return dynamic_cast<const XSECCryptoKeyRSA*>(key) != NULL;
}
if (XMLString::equals(uri, DSIGConstants::s_unicodeStrURIHMAC_SHA1)) {
hashType = XSECCryptoHash::HASH_SHA1;
return dynamic_cast<const XSECCryptoKeyHMAC*>(key) != NULL;
}
/* Check to see if we are one of the more exotic RSA signatures */
XMLSize_t cnt = XMLString::stringLen(DSIGConstants::s_unicodeStrURISIGBASEMORE);
if (XMLString::compareNString(uri, DSIGConstants::s_unicodeStrURISIGBASEMORE, cnt) == 0) {
// Have a "new" algorithm
if (XMLString::compareNString(&uri[cnt], s_hmac, 4) == 0) {
// HMAC
// Determine a trailing hash method
if (uri[cnt+4] != chDash)
return false;
return dynamic_cast<const XSECCryptoKeyHMAC*>(key) != NULL &&
::getHashType(&(uri[cnt+5]), hashType);
}
else if (XMLString::compareNString(&uri[cnt], s_rsa, 3) == 0) {
// RSA
if (uri[cnt+3] != chDash)
return false;
return dynamic_cast<const XSECCryptoKeyRSA*>(key) != NULL &&
::getHashType(&(uri[cnt+4]), hashType);
}
else if (XMLString::compareNString(&uri[cnt], s_ecdsa, 5) == 0) {
// ECDSA;
if (uri[cnt+5] != chDash)
return false;
return dynamic_cast<const XSECCryptoKeyEC*>(key) != NULL &&
::getHashType(&(uri[cnt+6]), hashType);
}
}
cnt = XMLString::stringLen(DSIGConstants::s_unicodeStrURISIGBASE11);
if (XMLString::compareNString(uri, DSIGConstants::s_unicodeStrURISIGBASE11, cnt) == 0) {
if (XMLString::compareNString(&uri[cnt], s_dsa, 3) == 0) {
// DSA
if (uri[cnt+3] != chDash)
return false;
return dynamic_cast<const XSECCryptoKeyDSA*>(key) != NULL &&
::getHashType(&(uri[cnt+4]), hashType);
}
}
return false;
}
XSECCryptoHash::HashType XSECAlgorithmSupport::getMGF1HashType(const XMLCh* uri)
{
// Check this is a known prefix on the URI.
XMLSize_t len = XMLString::stringLen(DSIGConstants::s_unicodeStrURIMGF1_BASE);
if (uri != NULL && XMLString::compareNString(uri, DSIGConstants::s_unicodeStrURIMGF1_BASE, len) == 0) {
XSECCryptoHash::HashType type;
::getHashType(&uri[len], type);
return type;
}
return XSECCryptoHash::HASH_NONE;
}
bool XSECAlgorithmSupport::evalCanonicalizationMethod(
const XMLCh* uri, bool& exclusive, bool& comments, bool& onedotone)
{
// Quick and dirty but inefficient
if (XMLString::equals(uri, DSIGConstants::s_unicodeStrURIC14N_NOC)) {
exclusive = false;
comments = false;
onedotone = false;
}
else if (XMLString::equals(uri, DSIGConstants::s_unicodeStrURIC14N_COM)) {
exclusive = false;
comments = true;
onedotone = false;
}
else if (XMLString::equals(uri, DSIGConstants::s_unicodeStrURIEXC_C14N_NOC)) {
exclusive = true;
comments = false;
onedotone = false;
}
else if (XMLString::equals(uri, DSIGConstants::s_unicodeStrURIEXC_C14N_COM)) {
exclusive = true;
comments = true;
onedotone = false;
}
else if (XMLString::equals(uri, DSIGConstants::s_unicodeStrURIC14N11_NOC)) {
exclusive = false;
comments = false;
onedotone = true;
}
else if (XMLString::equals(uri, DSIGConstants::s_unicodeStrURIC14N11_COM)) {
exclusive = false;
comments = true;
onedotone = true;
}
else {
// Unknown
return false;
}
return true;
}
|