SignedXml.cs 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Collections.ObjectModel;
  7. using System.Runtime.CompilerServices;
  8. using System.Runtime.InteropServices;
  9. using System.Security.Cryptography.X509Certificates;
  10. using System.Xml;
  11. namespace System.Security.Cryptography.Xml {
  12. public class SignedXml {
  13. /// <internalonly />
  14. protected Signature m_signature;
  15. /// <internalonly />
  16. protected string m_strSigningKeyName;
  17. private AsymmetricAlgorithm _signingKey;
  18. private XmlDocument _containingDocument;
  19. private IEnumerator _keyInfoEnum;
  20. private X509Certificate2Collection _x509Collection;
  21. private IEnumerator _x509Enum;
  22. private bool[] _refProcessed;
  23. private int[] _refLevelCache;
  24. internal XmlResolver _xmlResolver;
  25. internal XmlElement _context;
  26. private bool _bResolverSet;
  27. private Func<SignedXml, bool> _signatureFormatValidator = DefaultSignatureFormatValidator;
  28. private Collection<string> _safeCanonicalizationMethods;
  29. // Built in canonicalization algorithm URIs
  30. private static IList<string> s_knownCanonicalizationMethods;
  31. // Built in transform algorithm URIs (excluding canonicalization URIs)
  32. private static IList<string> s_defaultSafeTransformMethods;
  33. // additional HMAC Url identifiers
  34. private const string XmlDsigMoreHMACMD5Url = "http://www.w3.org/2001/04/xmldsig-more#hmac-md5";
  35. private const string XmlDsigMoreHMACSHA256Url = "http://www.w3.org/2001/04/xmldsig-more#hmac-sha256";
  36. private const string XmlDsigMoreHMACSHA384Url = "http://www.w3.org/2001/04/xmldsig-more#hmac-sha384";
  37. private const string XmlDsigMoreHMACSHA512Url = "http://www.w3.org/2001/04/xmldsig-more#hmac-sha512";
  38. private const string XmlDsigMoreHMACRIPEMD160Url = "http://www.w3.org/2001/04/xmldsig-more#hmac-ripemd160";
  39. // defines the XML encryption processing rules
  40. private EncryptedXml _exml;
  41. //
  42. // public constant Url identifiers most frequently used within the XML Signature classes
  43. //
  44. public const string XmlDsigNamespaceUrl = "http://www.w3.org/2000/09/xmldsig#";
  45. public const string XmlDsigMinimalCanonicalizationUrl = "http://www.w3.org/2000/09/xmldsig#minimal";
  46. public const string XmlDsigCanonicalizationUrl = XmlDsigC14NTransformUrl;
  47. public const string XmlDsigCanonicalizationWithCommentsUrl = XmlDsigC14NWithCommentsTransformUrl;
  48. public const string XmlDsigSHA1Url = "http://www.w3.org/2000/09/xmldsig#sha1";
  49. public const string XmlDsigDSAUrl = "http://www.w3.org/2000/09/xmldsig#dsa-sha1";
  50. public const string XmlDsigRSASHA1Url = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
  51. public const string XmlDsigHMACSHA1Url = "http://www.w3.org/2000/09/xmldsig#hmac-sha1";
  52. public const string XmlDsigSHA256Url = "http://www.w3.org/2001/04/xmlenc#sha256";
  53. public const string XmlDsigRSASHA256Url = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256";
  54. // Yes, SHA384 is in the xmldsig-more namespace even though all the other SHA variants are in xmlenc. That's the standard.
  55. public const string XmlDsigSHA384Url = "http://www.w3.org/2001/04/xmldsig-more#sha384";
  56. public const string XmlDsigRSASHA384Url = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha384";
  57. public const string XmlDsigSHA512Url = "http://www.w3.org/2001/04/xmlenc#sha512";
  58. public const string XmlDsigRSASHA512Url = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha512";
  59. internal static readonly string XmlDsigDigestDefault = XmlDsigSHA256Url;
  60. internal static readonly string XmlDsigRSADefault = XmlDsigRSASHA256Url;
  61. public const string XmlDsigC14NTransformUrl = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315";
  62. public const string XmlDsigC14NWithCommentsTransformUrl = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments";
  63. public const string XmlDsigExcC14NTransformUrl = "http://www.w3.org/2001/10/xml-exc-c14n#";
  64. public const string XmlDsigExcC14NWithCommentsTransformUrl = "http://www.w3.org/2001/10/xml-exc-c14n#WithComments";
  65. public const string XmlDsigBase64TransformUrl = "http://www.w3.org/2000/09/xmldsig#base64";
  66. public const string XmlDsigXPathTransformUrl = "http://www.w3.org/TR/1999/REC-xpath-19991116";
  67. public const string XmlDsigXsltTransformUrl = "http://www.w3.org/TR/1999/REC-xslt-19991116";
  68. public const string XmlDsigEnvelopedSignatureTransformUrl = "http://www.w3.org/2000/09/xmldsig#enveloped-signature";
  69. public const string XmlDecryptionTransformUrl = "http://www.w3.org/2002/07/decrypt#XML";
  70. public const string XmlLicenseTransformUrl = "urn:mpeg:mpeg21:2003:01-REL-R-NS:licenseTransform";
  71. //
  72. // public constructors
  73. //
  74. public SignedXml ()
  75. {
  76. Initialize (null);
  77. }
  78. public SignedXml (XmlDocument document)
  79. {
  80. if (document == null)
  81. throw new ArgumentNullException (nameof (document));
  82. Initialize (document.DocumentElement);
  83. }
  84. public SignedXml (XmlElement elem)
  85. {
  86. if (elem == null)
  87. throw new ArgumentNullException (nameof (elem));
  88. Initialize (elem);
  89. }
  90. private void Initialize (XmlElement element)
  91. {
  92. _containingDocument = (element == null ? null : element.OwnerDocument);
  93. _context = element;
  94. m_signature = new Signature ();
  95. m_signature.SignedXml = this;
  96. m_signature.SignedInfo = new SignedInfo ();
  97. _signingKey = null;
  98. _safeCanonicalizationMethods = new Collection<string> (KnownCanonicalizationMethods);
  99. }
  100. //
  101. // public properties
  102. //
  103. /// <internalonly />
  104. public string SigningKeyName
  105. {
  106. get { return m_strSigningKeyName; }
  107. set { m_strSigningKeyName = value; }
  108. }
  109. public XmlResolver Resolver
  110. {
  111. // This property only has a setter. The rationale for this is that we don't have a good value
  112. // to return when it has not been explicitely set, as we are using XmlSecureResolver by default
  113. set
  114. {
  115. _xmlResolver = value;
  116. _bResolverSet = true;
  117. }
  118. }
  119. internal bool ResolverSet
  120. {
  121. get { return _bResolverSet; }
  122. }
  123. public Func<SignedXml, bool> SignatureFormatValidator
  124. {
  125. get { return _signatureFormatValidator; }
  126. set { _signatureFormatValidator = value; }
  127. }
  128. public Collection<string> SafeCanonicalizationMethods
  129. {
  130. get { return _safeCanonicalizationMethods; }
  131. }
  132. public AsymmetricAlgorithm SigningKey
  133. {
  134. get { return _signingKey; }
  135. set { _signingKey = value; }
  136. }
  137. public EncryptedXml EncryptedXml
  138. {
  139. get
  140. {
  141. if (_exml == null)
  142. _exml = new EncryptedXml (_containingDocument); // default processing rules
  143. return _exml;
  144. }
  145. set { _exml = value; }
  146. }
  147. public Signature Signature
  148. {
  149. get { return m_signature; }
  150. }
  151. public SignedInfo SignedInfo
  152. {
  153. get { return m_signature.SignedInfo; }
  154. }
  155. public string SignatureMethod
  156. {
  157. get { return m_signature.SignedInfo.SignatureMethod; }
  158. }
  159. public string SignatureLength
  160. {
  161. get { return m_signature.SignedInfo.SignatureLength; }
  162. }
  163. public byte[] SignatureValue
  164. {
  165. get { return m_signature.SignatureValue; }
  166. }
  167. public KeyInfo KeyInfo
  168. {
  169. get { return m_signature.KeyInfo; }
  170. set { m_signature.KeyInfo = value; }
  171. }
  172. public XmlElement GetXml ()
  173. {
  174. // If we have a document context, then return a signature element in this context
  175. if (_containingDocument != null)
  176. return m_signature.GetXml (_containingDocument);
  177. else
  178. return m_signature.GetXml ();
  179. }
  180. public void LoadXml (XmlElement value)
  181. {
  182. if (value == null)
  183. throw new ArgumentNullException (nameof (value));
  184. m_signature.LoadXml (value);
  185. if (_context == null)
  186. _context = value;
  187. _bCacheValid = false;
  188. }
  189. //
  190. // public methods
  191. //
  192. public void AddReference (Reference reference)
  193. {
  194. m_signature.SignedInfo.AddReference (reference);
  195. }
  196. public void AddObject (DataObject dataObject)
  197. {
  198. m_signature.AddObject (dataObject);
  199. }
  200. public bool CheckSignature ()
  201. {
  202. AsymmetricAlgorithm signingKey;
  203. return CheckSignatureReturningKey (out signingKey);
  204. }
  205. public bool CheckSignatureReturningKey (out AsymmetricAlgorithm signingKey)
  206. {
  207. SignedXmlDebugLog.LogBeginSignatureVerification (this, _context);
  208. signingKey = null;
  209. bool bRet = false;
  210. AsymmetricAlgorithm key = null;
  211. if (!CheckSignatureFormat ())
  212. return false;
  213. do {
  214. key = GetPublicKey ();
  215. if (key != null) {
  216. bRet = CheckSignature (key);
  217. SignedXmlDebugLog.LogVerificationResult (this, key, bRet);
  218. }
  219. } while (key != null && bRet == false);
  220. signingKey = key;
  221. return bRet;
  222. }
  223. public bool CheckSignature (AsymmetricAlgorithm key)
  224. {
  225. if (!CheckSignatureFormat ())
  226. return false;
  227. if (!CheckSignedInfo (key)) {
  228. SignedXmlDebugLog.LogVerificationFailure (this, SR.Log_VerificationFailed_SignedInfo);
  229. return false;
  230. }
  231. // Now is the time to go through all the references and see if their DigestValues are good
  232. if (!CheckDigestedReferences ()) {
  233. SignedXmlDebugLog.LogVerificationFailure (this, SR.Log_VerificationFailed_References);
  234. return false;
  235. }
  236. SignedXmlDebugLog.LogVerificationResult (this, key, true);
  237. return true;
  238. }
  239. public bool CheckSignature (KeyedHashAlgorithm macAlg)
  240. {
  241. if (!CheckSignatureFormat ())
  242. return false;
  243. if (!CheckSignedInfo (macAlg)) {
  244. SignedXmlDebugLog.LogVerificationFailure (this, SR.Log_VerificationFailed_SignedInfo);
  245. return false;
  246. }
  247. if (!CheckDigestedReferences ()) {
  248. SignedXmlDebugLog.LogVerificationFailure (this, SR.Log_VerificationFailed_References);
  249. return false;
  250. }
  251. SignedXmlDebugLog.LogVerificationResult (this, macAlg, true);
  252. return true;
  253. }
  254. public bool CheckSignature (X509Certificate2 certificate, bool verifySignatureOnly)
  255. {
  256. if (!verifySignatureOnly) {
  257. // Check key usages to make sure it is good for signing.
  258. foreach (X509Extension extension in certificate.Extensions) {
  259. if (string.Compare (extension.Oid.Value, "2.5.29.15" /* szOID_KEY_USAGE */, StringComparison.OrdinalIgnoreCase) == 0) {
  260. X509KeyUsageExtension keyUsage = new X509KeyUsageExtension ();
  261. keyUsage.CopyFrom (extension);
  262. SignedXmlDebugLog.LogVerifyKeyUsage (this, certificate, keyUsage);
  263. bool validKeyUsage = (keyUsage.KeyUsages & X509KeyUsageFlags.DigitalSignature) != 0 ||
  264. (keyUsage.KeyUsages & X509KeyUsageFlags.NonRepudiation) != 0;
  265. if (!validKeyUsage) {
  266. SignedXmlDebugLog.LogVerificationFailure (this, SR.Log_VerificationFailed_X509KeyUsage);
  267. return false;
  268. }
  269. break;
  270. }
  271. }
  272. // Do the chain verification to make sure the certificate is valid.
  273. X509Chain chain = new X509Chain ();
  274. chain.ChainPolicy.ExtraStore.AddRange (BuildBagOfCerts());
  275. bool chainVerified = chain.Build (certificate);
  276. SignedXmlDebugLog.LogVerifyX509Chain (this, chain, certificate);
  277. if (!chainVerified) {
  278. SignedXmlDebugLog.LogVerificationFailure (this, SR.Log_VerificationFailed_X509Chain);
  279. return false;
  280. }
  281. }
  282. using (AsymmetricAlgorithm publicKey = Utils.GetAnyPublicKey (certificate)) {
  283. if (!CheckSignature (publicKey))
  284. return false;
  285. }
  286. SignedXmlDebugLog.LogVerificationResult (this, certificate, true);
  287. return true;
  288. }
  289. public void ComputeSignature ()
  290. {
  291. SignedXmlDebugLog.LogBeginSignatureComputation (this, _context);
  292. BuildDigestedReferences ();
  293. // Load the key
  294. AsymmetricAlgorithm key = SigningKey;
  295. if (key == null)
  296. throw new CryptographicException (SR.Cryptography_Xml_LoadKeyFailed);
  297. // Check the signature algorithm associated with the key so that we can accordingly set the signature method
  298. if (SignedInfo.SignatureMethod == null) {
  299. if (key is DSA) {
  300. SignedInfo.SignatureMethod = XmlDsigDSAUrl;
  301. } else if (key is RSA) {
  302. if (SignedInfo.SignatureMethod == null)
  303. SignedInfo.SignatureMethod = XmlDsigRSADefault;
  304. } else {
  305. throw new CryptographicException (SR.Cryptography_Xml_CreatedKeyFailed);
  306. }
  307. }
  308. // See if there is a signature description class defined in the Config file
  309. SignatureDescription signatureDescription = CryptoHelpers.CreateFromName<SignatureDescription> (SignedInfo.SignatureMethod);
  310. if (signatureDescription == null)
  311. throw new CryptographicException (SR.Cryptography_Xml_SignatureDescriptionNotCreated);
  312. HashAlgorithm hashAlg = signatureDescription.CreateDigest ();
  313. if (hashAlg == null)
  314. throw new CryptographicException (SR.Cryptography_Xml_CreateHashAlgorithmFailed);
  315. byte[] hashvalue = GetC14NDigest (hashAlg);
  316. AsymmetricSignatureFormatter asymmetricSignatureFormatter = signatureDescription.CreateFormatter (key);
  317. SignedXmlDebugLog.LogSigning (this, key, signatureDescription, hashAlg, asymmetricSignatureFormatter);
  318. m_signature.SignatureValue = asymmetricSignatureFormatter.CreateSignature (hashAlg);
  319. }
  320. public void ComputeSignature (KeyedHashAlgorithm macAlg)
  321. {
  322. if (macAlg == null)
  323. throw new ArgumentNullException (nameof (macAlg));
  324. HMAC hash = macAlg as HMAC;
  325. if (hash == null)
  326. throw new CryptographicException (SR.Cryptography_Xml_SignatureMethodKeyMismatch);
  327. int signatureLength;
  328. if (m_signature.SignedInfo.SignatureLength == null)
  329. signatureLength = hash.HashSize;
  330. else
  331. signatureLength = Convert.ToInt32 (m_signature.SignedInfo.SignatureLength, null);
  332. // signatureLength should be less than hash size
  333. if (signatureLength < 0 || signatureLength > hash.HashSize)
  334. throw new CryptographicException (SR.Cryptography_Xml_InvalidSignatureLength);
  335. if (signatureLength % 8 != 0)
  336. throw new CryptographicException (SR.Cryptography_Xml_InvalidSignatureLength2);
  337. BuildDigestedReferences ();
  338. switch (hash.HashName) {
  339. case "SHA1":
  340. SignedInfo.SignatureMethod = SignedXml.XmlDsigHMACSHA1Url;
  341. break;
  342. case "SHA256":
  343. SignedInfo.SignatureMethod = SignedXml.XmlDsigMoreHMACSHA256Url;
  344. break;
  345. case "SHA384":
  346. SignedInfo.SignatureMethod = SignedXml.XmlDsigMoreHMACSHA384Url;
  347. break;
  348. case "SHA512":
  349. SignedInfo.SignatureMethod = SignedXml.XmlDsigMoreHMACSHA512Url;
  350. break;
  351. case "MD5":
  352. SignedInfo.SignatureMethod = SignedXml.XmlDsigMoreHMACMD5Url;
  353. break;
  354. case "RIPEMD160":
  355. SignedInfo.SignatureMethod = SignedXml.XmlDsigMoreHMACRIPEMD160Url;
  356. break;
  357. default:
  358. throw new CryptographicException (SR.Cryptography_Xml_SignatureMethodKeyMismatch);
  359. }
  360. byte[] hashValue = GetC14NDigest (hash);
  361. SignedXmlDebugLog.LogSigning (this, hash);
  362. m_signature.SignatureValue = new byte [signatureLength / 8];
  363. Buffer.BlockCopy (hashValue, 0, m_signature.SignatureValue, 0, signatureLength / 8);
  364. }
  365. //
  366. // virtual methods
  367. //
  368. protected virtual AsymmetricAlgorithm GetPublicKey ()
  369. {
  370. if (KeyInfo == null)
  371. throw new CryptographicException (SR.Cryptography_Xml_KeyInfoRequired);
  372. if (_x509Enum != null) {
  373. AsymmetricAlgorithm key = GetNextCertificatePublicKey ();
  374. if (key != null)
  375. return key;
  376. }
  377. if (_keyInfoEnum == null)
  378. _keyInfoEnum = KeyInfo.GetEnumerator ();
  379. // In our implementation, we move to the next KeyInfo clause which is an RSAKeyValue, DSAKeyValue or KeyInfoX509Data
  380. while (_keyInfoEnum.MoveNext()) {
  381. RSAKeyValue rsaKeyValue = _keyInfoEnum.Current as RSAKeyValue;
  382. if (rsaKeyValue != null)
  383. return rsaKeyValue.Key;
  384. DSAKeyValue dsaKeyValue = _keyInfoEnum.Current as DSAKeyValue;
  385. if (dsaKeyValue != null)
  386. return dsaKeyValue.Key;
  387. KeyInfoX509Data x509Data = _keyInfoEnum.Current as KeyInfoX509Data;
  388. if (x509Data != null) {
  389. _x509Collection = Utils.BuildBagOfCerts (x509Data, CertUsageType.Verification);
  390. if (_x509Collection.Count > 0) {
  391. _x509Enum = _x509Collection.GetEnumerator ();
  392. AsymmetricAlgorithm key = GetNextCertificatePublicKey ();
  393. if (key != null)
  394. return key;
  395. }
  396. }
  397. }
  398. return null;
  399. }
  400. private X509Certificate2Collection BuildBagOfCerts ()
  401. {
  402. X509Certificate2Collection collection = new X509Certificate2Collection ();
  403. if (KeyInfo != null) {
  404. foreach (KeyInfoClause clause in KeyInfo) {
  405. KeyInfoX509Data x509Data = clause as KeyInfoX509Data;
  406. if (x509Data != null)
  407. collection.AddRange (Utils.BuildBagOfCerts (x509Data, CertUsageType.Verification));
  408. }
  409. }
  410. return collection;
  411. }
  412. private AsymmetricAlgorithm GetNextCertificatePublicKey ()
  413. {
  414. while (_x509Enum.MoveNext ()) {
  415. X509Certificate2 certificate = (X509Certificate2)_x509Enum.Current;
  416. if (certificate != null)
  417. return Utils.GetAnyPublicKey (certificate);
  418. }
  419. return null;
  420. }
  421. public virtual XmlElement GetIdElement (XmlDocument document, string idValue)
  422. {
  423. return DefaultGetIdElement (document, idValue);
  424. }
  425. internal static XmlElement DefaultGetIdElement (XmlDocument document, string idValue)
  426. {
  427. if (document == null)
  428. return null;
  429. try {
  430. XmlConvert.VerifyNCName (idValue);
  431. } catch (XmlException) {
  432. // Identifiers are required to be an NCName
  433. // (xml:id version 1.0, part 4, paragraph 2, bullet 1)
  434. //
  435. // If it isn't an NCName, it isn't allowed to match.
  436. return null;
  437. }
  438. // Get the element with idValue
  439. XmlElement elem = document.GetElementById (idValue);
  440. if (elem != null) {
  441. // Have to check for duplicate ID values from the DTD.
  442. XmlDocument docClone = (XmlDocument)document.CloneNode (true);
  443. XmlElement cloneElem = docClone.GetElementById (idValue);
  444. // If it's null here we want to know about it, because it means that
  445. // GetElementById failed to work across the cloning, and our uniqueness
  446. // test is invalid.
  447. System.Diagnostics.Debug.Assert (cloneElem != null);
  448. // Guard against null anyways
  449. if (cloneElem != null) {
  450. cloneElem.Attributes.RemoveAll ();
  451. XmlElement cloneElem2 = docClone.GetElementById (idValue);
  452. if (cloneElem2 != null)
  453. throw new CryptographicException (SR.Cryptography_Xml_InvalidReference);
  454. }
  455. return elem;
  456. }
  457. elem = GetSingleReferenceTarget (document, "Id", idValue);
  458. if (elem != null)
  459. return elem;
  460. elem = GetSingleReferenceTarget (document, "id", idValue);
  461. if (elem != null)
  462. return elem;
  463. elem = GetSingleReferenceTarget (document, "ID", idValue);
  464. return elem;
  465. }
  466. //
  467. // private methods
  468. //
  469. private bool _bCacheValid;
  470. private byte[] _digestedSignedInfo;
  471. private static bool DefaultSignatureFormatValidator (SignedXml signedXml)
  472. {
  473. // Reject the signature if it uses a truncated HMAC
  474. if (signedXml.DoesSignatureUseTruncatedHmac ())
  475. return false;
  476. // Reject the signature if it uses a canonicalization algorithm other than
  477. // one of the ones explicitly allowed
  478. if (!signedXml.DoesSignatureUseSafeCanonicalizationMethod ())
  479. return false;
  480. // Otherwise accept it
  481. return true;
  482. }
  483. // Validation function to see if the current signature is signed with a truncated HMAC - one which
  484. // has a signature length of fewer bits than the whole HMAC output.
  485. private bool DoesSignatureUseTruncatedHmac ()
  486. {
  487. // If we're not using the SignatureLength property, then we're not truncating the signature length
  488. if (SignedInfo.SignatureLength == null)
  489. return false;
  490. // See if we're signed witn an HMAC algorithm
  491. HMAC hmac = CryptoHelpers.CreateFromName<HMAC> (SignatureMethod);
  492. if (hmac == null)
  493. return false; // We aren't signed with an HMAC algorithm, so we cannot have a truncated HMAC
  494. // Figure out how many bits the signature is using
  495. int actualSignatureSize = 0;
  496. if (!int.TryParse (SignedInfo.SignatureLength, out actualSignatureSize))
  497. return true; // If the value wasn't a valid integer, then we'll conservatively reject it all together
  498. // Make sure the full HMAC signature size is the same size that was specified in the XML
  499. // signature. If the actual signature size is not exactly the same as the full HMAC size, then
  500. // reject the signature.
  501. return actualSignatureSize != hmac.HashSize;
  502. }
  503. // Validation function to see if the signature uses a canonicalization algorithm from our list
  504. // of approved algorithm URIs.
  505. private bool DoesSignatureUseSafeCanonicalizationMethod ()
  506. {
  507. foreach (string safeAlgorithm in SafeCanonicalizationMethods) {
  508. if (string.Equals (safeAlgorithm, SignedInfo.CanonicalizationMethod, StringComparison.OrdinalIgnoreCase))
  509. return true;
  510. }
  511. SignedXmlDebugLog.LogUnsafeCanonicalizationMethod (this, SignedInfo.CanonicalizationMethod, SafeCanonicalizationMethods);
  512. return false;
  513. }
  514. private bool ReferenceUsesSafeTransformMethods (Reference reference)
  515. {
  516. TransformChain transformChain = reference.TransformChain;
  517. int transformCount = transformChain.Count;
  518. for (int i = 0; i < transformCount; i++) {
  519. Transform transform = transformChain [i];
  520. if (!IsSafeTransform (transform.Algorithm))
  521. return false;
  522. }
  523. return true;
  524. }
  525. private bool IsSafeTransform (string transformAlgorithm)
  526. {
  527. // All canonicalization algorithms are valid transform algorithms.
  528. foreach (string safeAlgorithm in SafeCanonicalizationMethods) {
  529. if (string.Equals (safeAlgorithm, transformAlgorithm, StringComparison.OrdinalIgnoreCase))
  530. return true;
  531. }
  532. foreach (string safeAlgorithm in DefaultSafeTransformMethods) {
  533. if (string.Equals (safeAlgorithm, transformAlgorithm, StringComparison.OrdinalIgnoreCase))
  534. return true;
  535. }
  536. SignedXmlDebugLog.LogUnsafeTransformMethod (
  537. this,
  538. transformAlgorithm,
  539. SafeCanonicalizationMethods,
  540. DefaultSafeTransformMethods);
  541. return false;
  542. }
  543. // Get a list of the built in canonicalization algorithms, as well as any that the machine admin has
  544. // added to the valid set.
  545. private static IList<string> KnownCanonicalizationMethods {
  546. get {
  547. if (s_knownCanonicalizationMethods == null) {
  548. // Start with the list that the machine admin added, if any
  549. List<string> safeAlgorithms = new List<string> ();
  550. // Built in algorithms
  551. safeAlgorithms.Add (XmlDsigC14NTransformUrl);
  552. safeAlgorithms.Add (XmlDsigC14NWithCommentsTransformUrl);
  553. safeAlgorithms.Add (XmlDsigExcC14NTransformUrl);
  554. safeAlgorithms.Add (XmlDsigExcC14NWithCommentsTransformUrl);
  555. s_knownCanonicalizationMethods = safeAlgorithms;
  556. }
  557. return s_knownCanonicalizationMethods;
  558. }
  559. }
  560. private static IList<string> DefaultSafeTransformMethods {
  561. get {
  562. if (s_defaultSafeTransformMethods == null) {
  563. List<string> safeAlgorithms = new List<string> ();
  564. // Built in algorithms
  565. // KnownCanonicalizationMethods don't need to be added here, because
  566. // the validator will automatically accept those.
  567. //
  568. // xmldsig 6.6.1:
  569. // Any canonicalization algorithm that can be used for
  570. // CanonicalizationMethod can be used as a Transform.
  571. safeAlgorithms.Add (XmlDsigEnvelopedSignatureTransformUrl);
  572. safeAlgorithms.Add (XmlDsigBase64TransformUrl);
  573. safeAlgorithms.Add (XmlLicenseTransformUrl);
  574. safeAlgorithms.Add (XmlDecryptionTransformUrl);
  575. s_defaultSafeTransformMethods = safeAlgorithms;
  576. }
  577. return s_defaultSafeTransformMethods;
  578. }
  579. }
  580. private byte[] GetC14NDigest (HashAlgorithm hash)
  581. {
  582. bool isKeyedHashAlgorithm = hash is KeyedHashAlgorithm;
  583. if (isKeyedHashAlgorithm || !_bCacheValid || !SignedInfo.CacheValid) {
  584. string baseUri = (_containingDocument == null ? null : _containingDocument.BaseURI);
  585. XmlResolver resolver = (_bResolverSet ? _xmlResolver : new XmlSecureResolver (new XmlUrlResolver (), baseUri));
  586. XmlDocument doc = Utils.PreProcessElementInput (SignedInfo.GetXml (), resolver, baseUri);
  587. // Add non default namespaces in scope
  588. CanonicalXmlNodeList namespaces = (_context == null ? null : Utils.GetPropagatedAttributes (_context));
  589. SignedXmlDebugLog.LogNamespacePropagation (this, namespaces);
  590. Utils.AddNamespaces (doc.DocumentElement, namespaces);
  591. Transform c14nMethodTransform = SignedInfo.CanonicalizationMethodObject;
  592. c14nMethodTransform.Resolver = resolver;
  593. c14nMethodTransform.BaseURI = baseUri;
  594. SignedXmlDebugLog.LogBeginCanonicalization (this, c14nMethodTransform);
  595. c14nMethodTransform.LoadInput (doc);
  596. SignedXmlDebugLog.LogCanonicalizedOutput (this, c14nMethodTransform);
  597. _digestedSignedInfo = c14nMethodTransform.GetDigestedOutput (hash);
  598. _bCacheValid = !isKeyedHashAlgorithm;
  599. }
  600. return _digestedSignedInfo;
  601. }
  602. private int GetReferenceLevel (int index, ArrayList references)
  603. {
  604. if (_refProcessed [index]) return _refLevelCache [index];
  605. _refProcessed [index] = true;
  606. Reference reference = (Reference)references [index];
  607. if (reference.Uri == null || reference.Uri.Length == 0 || (reference.Uri.Length > 0 && reference.Uri [0] != '#')) {
  608. _refLevelCache [index] = 0;
  609. return 0;
  610. }
  611. if (reference.Uri.Length > 0 && reference.Uri [0] == '#')
  612. {
  613. string idref = Utils.ExtractIdFromLocalUri (reference.Uri);
  614. if (idref == "xpointer(/)") {
  615. _refLevelCache [index] = 0;
  616. return 0;
  617. }
  618. // If this is pointing to another reference
  619. for (int j = 0; j < references.Count; ++j) {
  620. if (((Reference)references [j]).Id == idref) {
  621. _refLevelCache [index] = GetReferenceLevel (j, references) + 1;
  622. return (_refLevelCache [index]);
  623. }
  624. }
  625. // Then the reference points to an object tag
  626. _refLevelCache [index] = 0;
  627. return 0;
  628. }
  629. // Malformed reference
  630. throw new CryptographicException (SR.Cryptography_Xml_InvalidReference);
  631. }
  632. private class ReferenceLevelSortOrder : IComparer
  633. {
  634. private ArrayList _references;
  635. public ReferenceLevelSortOrder ()
  636. {
  637. }
  638. public ArrayList References
  639. {
  640. get { return _references; }
  641. set { _references = value; }
  642. }
  643. public int Compare (object a, object b)
  644. {
  645. Reference referenceA = a as Reference;
  646. Reference referenceB = b as Reference;
  647. // Get the indexes
  648. int iIndexA = 0;
  649. int iIndexB = 0;
  650. int i = 0;
  651. foreach (Reference reference in References) {
  652. if (reference == referenceA) iIndexA = i;
  653. if (reference == referenceB) iIndexB = i;
  654. i++;
  655. }
  656. int iLevelA = referenceA.SignedXml.GetReferenceLevel (iIndexA, References);
  657. int iLevelB = referenceB.SignedXml.GetReferenceLevel (iIndexB, References);
  658. return iLevelA.CompareTo (iLevelB);
  659. }
  660. }
  661. private void BuildDigestedReferences ()
  662. {
  663. // Default the DigestMethod and Canonicalization
  664. ArrayList references = SignedInfo.References;
  665. // Reset the cache
  666. _refProcessed = new bool [references.Count];
  667. _refLevelCache = new int [references.Count];
  668. ReferenceLevelSortOrder sortOrder = new ReferenceLevelSortOrder ();
  669. sortOrder.References = references;
  670. // Don't alter the order of the references array list
  671. ArrayList sortedReferences = new ArrayList ();
  672. foreach (Reference reference in references)
  673. sortedReferences.Add (reference);
  674. sortedReferences.Sort (sortOrder);
  675. CanonicalXmlNodeList nodeList = new CanonicalXmlNodeList ();
  676. foreach (DataObject obj in m_signature.ObjectList)
  677. nodeList.Add (obj.GetXml ());
  678. foreach (Reference reference in sortedReferences) {
  679. if (reference.DigestMethod == null)
  680. reference.DigestMethod = XmlDsigDigestDefault;
  681. SignedXmlDebugLog.LogSigningReference (this, reference);
  682. reference.UpdateHashValue (_containingDocument, nodeList);
  683. // If this reference has an Id attribute, add it
  684. if (reference.Id != null)
  685. nodeList.Add (reference.GetXml ());
  686. }
  687. }
  688. private bool CheckDigestedReferences ()
  689. {
  690. ArrayList references = m_signature.SignedInfo.References;
  691. for (int i = 0; i < references.Count; ++i) {
  692. Reference digestedReference = (Reference)references [i];
  693. if (!ReferenceUsesSafeTransformMethods (digestedReference))
  694. return false;
  695. SignedXmlDebugLog.LogVerifyReference (this, digestedReference);
  696. byte[] calculatedHash = null;
  697. try {
  698. calculatedHash = digestedReference.CalculateHashValue (_containingDocument, m_signature.ReferencedItems);
  699. } catch (CryptoSignedXmlRecursionException) {
  700. SignedXmlDebugLog.LogSignedXmlRecursionLimit (this, digestedReference);
  701. return false;
  702. }
  703. // Compare both hashes
  704. SignedXmlDebugLog.LogVerifyReferenceHash (this, digestedReference, calculatedHash, digestedReference.DigestValue);
  705. if (!CryptographicEquals (calculatedHash, digestedReference.DigestValue))
  706. return false;
  707. }
  708. return true;
  709. }
  710. // Methods _must_ be marked both No Inlining and No Optimization to be fully opted out of optimization.
  711. // This is because if a candidate method is inlined, its method level attributes, including the NoOptimization
  712. // attribute, are lost.
  713. // This method makes no attempt to disguise the length of either of its inputs. It is assumed the attacker has
  714. // knowledge of the algorithms used, and thus the output length. Length is difficult to properly blind in modern CPUs.
  715. [MethodImpl (MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
  716. private static bool CryptographicEquals (byte[] a, byte[] b)
  717. {
  718. System.Diagnostics.Debug.Assert (a != null);
  719. System.Diagnostics.Debug.Assert (b != null);
  720. int result = 0;
  721. // Short cut if the lengths are not identical
  722. if (a.Length != b.Length)
  723. return false;
  724. unchecked
  725. {
  726. // Normally this caching doesn't matter, but with the optimizer off, this nets a non-trivial speedup.
  727. int aLength = a.Length;
  728. for (int i = 0; i < aLength; i++) {
  729. // We use subtraction here instead of XOR because the XOR algorithm gets ever so
  730. // slightly faster as more and more differences pile up.
  731. // This cannot overflow more than once (and back to 0) because bytes are 1 byte
  732. // in length, and result is 4 bytes. The OR propagates all set bytes, so the differences
  733. // can't add up and overflow a second time.
  734. result = result | (a [i] - b [i]);
  735. }
  736. }
  737. return (0 == result);
  738. }
  739. // If we have a signature format validation callback, check to see if this signature's format (not
  740. // the signature itself) is valid according to the validator. A return value of true indicates that
  741. // the signature format is acceptable, false means that the format is not valid.
  742. private bool CheckSignatureFormat ()
  743. {
  744. if (_signatureFormatValidator == null) {
  745. // No format validator means that we default to accepting the signature. (This is
  746. // effectively compatibility mode with v3.5).
  747. return true;
  748. }
  749. SignedXmlDebugLog.LogBeginCheckSignatureFormat (this, _signatureFormatValidator);
  750. bool formatValid = _signatureFormatValidator (this);
  751. SignedXmlDebugLog.LogFormatValidationResult (this, formatValid);
  752. return formatValid;
  753. }
  754. private bool CheckSignedInfo (AsymmetricAlgorithm key)
  755. {
  756. if (key == null)
  757. throw new ArgumentNullException (nameof (key));
  758. SignedXmlDebugLog.LogBeginCheckSignedInfo (this, m_signature.SignedInfo);
  759. SignatureDescription signatureDescription = CryptoHelpers.CreateFromName<SignatureDescription> (SignatureMethod);
  760. if (signatureDescription == null)
  761. throw new CryptographicException (SR.Cryptography_Xml_SignatureDescriptionNotCreated);
  762. // Let's see if the key corresponds with the SignatureMethod
  763. Type ta = Type.GetType (signatureDescription.KeyAlgorithm);
  764. if (!IsKeyTheCorrectAlgorithm (key, ta))
  765. return false;
  766. HashAlgorithm hashAlgorithm = signatureDescription.CreateDigest ();
  767. if (hashAlgorithm == null)
  768. throw new CryptographicException (SR.Cryptography_Xml_CreateHashAlgorithmFailed);
  769. byte[] hashval = GetC14NDigest (hashAlgorithm);
  770. AsymmetricSignatureDeformatter asymmetricSignatureDeformatter = signatureDescription.CreateDeformatter (key);
  771. SignedXmlDebugLog.LogVerifySignedInfo (this,
  772. key,
  773. signatureDescription,
  774. hashAlgorithm,
  775. asymmetricSignatureDeformatter,
  776. hashval,
  777. m_signature.SignatureValue);
  778. return asymmetricSignatureDeformatter.VerifySignature (hashval, m_signature.SignatureValue);
  779. }
  780. private bool CheckSignedInfo (KeyedHashAlgorithm macAlg)
  781. {
  782. if (macAlg == null)
  783. throw new ArgumentNullException (nameof (macAlg));
  784. SignedXmlDebugLog.LogBeginCheckSignedInfo (this, m_signature.SignedInfo);
  785. int signatureLength;
  786. if (m_signature.SignedInfo.SignatureLength == null)
  787. signatureLength = macAlg.HashSize;
  788. else
  789. signatureLength = Convert.ToInt32 (m_signature.SignedInfo.SignatureLength, null);
  790. // signatureLength should be less than hash size
  791. if (signatureLength < 0 || signatureLength > macAlg.HashSize)
  792. throw new CryptographicException (SR.Cryptography_Xml_InvalidSignatureLength);
  793. if (signatureLength % 8 != 0)
  794. throw new CryptographicException (SR.Cryptography_Xml_InvalidSignatureLength2);
  795. if (m_signature.SignatureValue == null)
  796. throw new CryptographicException (SR.Cryptography_Xml_SignatureValueRequired);
  797. if (m_signature.SignatureValue.Length != signatureLength / 8)
  798. throw new CryptographicException (SR.Cryptography_Xml_InvalidSignatureLength);
  799. // Calculate the hash
  800. byte[] hashValue = GetC14NDigest (macAlg);
  801. SignedXmlDebugLog.LogVerifySignedInfo (this, macAlg, hashValue, m_signature.SignatureValue);
  802. for (int i = 0; i < m_signature.SignatureValue.Length; i++)
  803. if (m_signature.SignatureValue [i] != hashValue [i]) return false;
  804. return true;
  805. }
  806. private static XmlElement GetSingleReferenceTarget (XmlDocument document, string idAttributeName, string idValue)
  807. {
  808. // idValue has already been tested as an NCName (unless overridden for compatibility), so there's no
  809. // escaping that needs to be done here.
  810. string xPath = "//*[@" + idAttributeName + "=\"" + idValue + "\"]";
  811. // http://www.w3.org/TR/xmldsig-core/#sec-ReferenceProcessingModel says that for the form URI="#chapter1":
  812. //
  813. // Identifies a node-set containing the element with ID attribute value 'chapter1' ...
  814. //
  815. // Note that it uses the singular. Therefore, if the match is ambiguous, we should consider the document invalid.
  816. //
  817. // In this case, we'll treat it the same as having found nothing across all fallbacks (but shortcut so that we don't
  818. // fall into a trap of finding a secondary element which wasn't the originally signed one).
  819. XmlNodeList nodeList = document.SelectNodes (xPath);
  820. if (nodeList == null || nodeList.Count == 0)
  821. return null;
  822. if (nodeList.Count == 1)
  823. return nodeList [0] as XmlElement;
  824. throw new CryptographicException (SR.Cryptography_Xml_InvalidReference);
  825. }
  826. private static bool IsKeyTheCorrectAlgorithm (AsymmetricAlgorithm key, Type expectedType)
  827. {
  828. Type actualType = key.GetType ();
  829. if (actualType == expectedType)
  830. return true;
  831. // This check exists solely for compatibility with 4.6. Normally, we would expect "expectedType" to be the superclass type and
  832. // the actualType to be the subclass.
  833. if (expectedType.IsSubclassOf (actualType))
  834. return true;
  835. //
  836. // "expectedType" comes from the KeyAlgorithm property of a SignatureDescription. The BCL SignatureDescription classes have historically
  837. // denoted provider-specific implementations ("RSACryptoServiceProvider") rather than the base class for the algorithm ("RSA"). We could
  838. // change those (at the risk of creating other compat problems) but we have no control over third party SignatureDescriptions.
  839. //
  840. // So, in the absence of a better approach, walk up the parent hierarchy until we find the ancestor that's a direct subclass of
  841. // AsymmetricAlgorithm and treat that as the algorithm identifier.
  842. //
  843. while (expectedType != null && expectedType.BaseType != typeof (AsymmetricAlgorithm))
  844. expectedType = expectedType.BaseType;
  845. if (expectedType == null)
  846. return false; // SignatureDescription specified something that isn't even a subclass of AsymmetricAlgorithm. For compatibility with 4.6, return false rather throw.
  847. if (actualType.IsSubclassOf (expectedType))
  848. return true;
  849. return false;
  850. }
  851. }
  852. }