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 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
|
//
// SignedXmlTest.cs - NUnit Test Cases for SignedXml
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
//
// (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) 2004-2005, 2008 Novell, Inc (http://www.novell.com)
//
using System;
using System.Globalization;
using System.IO;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography.Xml;
using System.Text;
using System.Xml;
using NUnit.Framework;
namespace MonoTests.System.Security.Cryptography.Xml {
public class SignedXmlEx : SignedXml {
// required to test protected GetPublicKey in SignedXml
public AsymmetricAlgorithm PublicGetPublicKey ()
{
return base.GetPublicKey ();
}
}
[TestFixture]
public class SignedXmlTest {
private const string signature = "<Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><SignedInfo><CanonicalizationMethod Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\" /><SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\" /><Reference URI=\"#MyObjectId\"><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" /><DigestValue>CTnnhjxUQHJmD+t1MjVXrOW+MCA=</DigestValue></Reference></SignedInfo><SignatureValue>dbFt6Zw3vR+Xh7LbM/vuifyFA7gPh/NlDM2Glz/SJBsveISieuTBpZlk/zavAeuXR/Nu0Ztt4OP4tCOg09a2RNlrTP0dhkeEfL1jTzpnVaLHuQbCiwOWCgbRif7Xt7N12FuiHYb3BltP/YyXS4E12NxlGlqnDiFA1v/mkK5+C1o=</SignatureValue><KeyInfo><KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><RSAKeyValue><Modulus>hEfTJNa2idz2u+fSYDDG4Lx/xuk4aBbvOPVNqgc1l9Y8t7Pt+ZyF+kkF3uUl8Y0700BFGAsprnhwrWENK+PGdtvM5796ZKxCCa0ooKkofiT4355HqK26hpV8dvj38vq/rkJe1jHZgkTKa+c/0vjcYZOI/RT/IZv9JfXxVWLuLxk=</Modulus><Exponent>EQ==</Exponent></RSAKeyValue></KeyValue></KeyInfo><Object Id=\"MyObjectId\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><ObjectListTag xmlns=\"\" /></Object></Signature>";
[Test]
public void StaticValues ()
{
Assert.AreEqual ("http://www.w3.org/TR/2001/REC-xml-c14n-20010315", SignedXml.XmlDsigCanonicalizationUrl, "XmlDsigCanonicalizationUrl");
Assert.AreEqual ("http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments", SignedXml.XmlDsigCanonicalizationWithCommentsUrl, "XmlDsigCanonicalizationWithCommentsUrl");
Assert.AreEqual ("http://www.w3.org/2000/09/xmldsig#dsa-sha1", SignedXml.XmlDsigDSAUrl, "XmlDsigDSAUrl");
Assert.AreEqual ("http://www.w3.org/2000/09/xmldsig#hmac-sha1", SignedXml.XmlDsigHMACSHA1Url, "XmlDsigHMACSHA1Url");
Assert.AreEqual ("http://www.w3.org/2000/09/xmldsig#minimal", SignedXml.XmlDsigMinimalCanonicalizationUrl, "XmlDsigMinimalCanonicalizationUrl");
Assert.AreEqual ("http://www.w3.org/2000/09/xmldsig#", SignedXml.XmlDsigNamespaceUrl, "XmlDsigNamespaceUrl");
Assert.AreEqual ("http://www.w3.org/2000/09/xmldsig#rsa-sha1", SignedXml.XmlDsigRSASHA1Url, "XmlDsigRSASHA1Url");
Assert.AreEqual ("http://www.w3.org/2000/09/xmldsig#sha1", SignedXml.XmlDsigSHA1Url, "XmlDsigSHA1Url");
}
[Test]
public void Constructor_Empty ()
{
XmlDocument doc = new XmlDocument ();
doc.LoadXml (signature);
XmlNodeList xnl = doc.GetElementsByTagName ("Signature", SignedXml.XmlDsigNamespaceUrl);
XmlElement xel = (XmlElement) xnl [0];
SignedXml sx = new SignedXml (doc);
sx.LoadXml (xel);
Assert.IsTrue (sx.CheckSignature (), "CheckSignature");
}
[Test]
public void Constructor_XmlDocument ()
{
XmlDocument doc = new XmlDocument ();
doc.LoadXml (signature);
XmlNodeList xnl = doc.GetElementsByTagName ("Signature", SignedXml.XmlDsigNamespaceUrl);
XmlElement xel = (XmlElement) xnl [0];
SignedXml sx = new SignedXml (doc);
sx.LoadXml (doc.DocumentElement);
Assert.IsTrue (sx.CheckSignature (), "CheckSignature");
}
[Test]
#if NET_2_0
[Ignore ("2.0 throws a NullReferenceException - reported as FDBK25892")]
// http://lab.msdn.microsoft.com/ProductFeedback/viewfeedback.aspx?feedbackid=02dd9730-d1ad-4170-8c82-36858c55fbe2
#endif
[ExpectedException (typeof (ArgumentNullException))]
public void Constructor_XmlDocument_Null ()
{
XmlDocument doc = null;
SignedXml sx = new SignedXml (doc);
}
[Test]
public void Constructor_XmlElement ()
{
XmlDocument doc = new XmlDocument ();
doc.LoadXml (signature);
XmlNodeList xnl = doc.GetElementsByTagName ("Signature", SignedXml.XmlDsigNamespaceUrl);
XmlElement xel = (XmlElement) xnl [0];
SignedXml sx = new SignedXml (doc.DocumentElement);
sx.LoadXml (xel);
Assert.IsTrue (sx.CheckSignature (), "CheckSignature");
}
[Test]
#if !NET_2_0
[ExpectedException (typeof (CryptographicException))]
#endif
public void Constructor_XmlElement_WithoutLoadXml ()
{
XmlDocument doc = new XmlDocument ();
doc.LoadXml (signature);
XmlNodeList xnl = doc.GetElementsByTagName ("Signature", SignedXml.XmlDsigNamespaceUrl);
XmlElement xel = (XmlElement) xnl [0];
SignedXml sx = new SignedXml (doc.DocumentElement);
Assert.IsTrue (!sx.CheckSignature (), "!CheckSignature");
// SignedXml (XmlElement) != SignedXml () + LoadXml (XmlElement)
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void Constructor_XmlElement_Null ()
{
XmlElement xel = null;
SignedXml sx = new SignedXml (xel);
}
// sample from MSDN (url)
public SignedXml MSDNSample ()
{
// Create example data to sign.
XmlDocument document = new XmlDocument ();
XmlNode node = document.CreateNode (XmlNodeType.Element, "", "MyElement", "samples");
node.InnerText = "This is some text";
document.AppendChild (node);
// Create the SignedXml message.
SignedXml signedXml = new SignedXml ();
// Create a data object to hold the data to sign.
DataObject dataObject = new DataObject ();
dataObject.Data = document.ChildNodes;
dataObject.Id = "MyObjectId";
// Add the data object to the signature.
signedXml.AddObject (dataObject);
// Create a reference to be able to package everything into the
// message.
Reference reference = new Reference ();
reference.Uri = "#MyObjectId";
// Add it to the message.
signedXml.AddReference (reference);
return signedXml;
}
[Test]
[ExpectedException (typeof (CryptographicException))]
public void SignatureMethodMismatch ()
{
SignedXml signedXml = MSDNSample ();
RSA key = RSA.Create ();
signedXml.SigningKey = key;
signedXml.SignedInfo.SignatureMethod = SignedXml.XmlDsigHMACSHA1Url;
// Add a KeyInfo.
KeyInfo keyInfo = new KeyInfo ();
keyInfo.AddClause (new RSAKeyValue (key));
signedXml.KeyInfo = keyInfo;
Assert.IsNotNull (signedXml.SignatureMethod, "SignatureMethod");
// Compute the signature - causes unsupported algorithm by the key.
signedXml.ComputeSignature ();
}
[Test]
public void AsymmetricRSASignature ()
{
SignedXml signedXml = MSDNSample ();
RSA key = RSA.Create ();
signedXml.SigningKey = key;
// Add a KeyInfo.
KeyInfo keyInfo = new KeyInfo ();
keyInfo.AddClause (new RSAKeyValue (key));
signedXml.KeyInfo = keyInfo;
Assert.AreEqual (1, signedXml.KeyInfo.Count, "KeyInfo");
Assert.IsNull (signedXml.SignatureLength, "SignatureLength");
Assert.IsNull (signedXml.SignatureMethod, "SignatureMethod");
Assert.IsNull (signedXml.SignatureValue, "SignatureValue");
Assert.IsNull (signedXml.SigningKeyName, "SigningKeyName");
// Compute the signature.
signedXml.ComputeSignature ();
Assert.IsNull (signedXml.SigningKeyName, "SigningKeyName");
Assert.AreEqual (SignedXml.XmlDsigRSASHA1Url, signedXml.SignatureMethod, "SignatureMethod");
Assert.AreEqual (128, signedXml.SignatureValue.Length, "SignatureValue");
Assert.IsNull (signedXml.SigningKeyName, "SigningKeyName");
// Get the XML representation of the signature.
XmlElement xmlSignature = signedXml.GetXml ();
// LAMESPEC: we must reload the signature or it won't work
// MS framework throw a "malformed element"
SignedXml vrfy = new SignedXml ();
vrfy.LoadXml (xmlSignature);
// assert that we can verify our own signature
Assert.IsTrue (vrfy.CheckSignature (), "RSA-Compute/Verify");
}
[Test]
public void AsymmetricDSASignature ()
{
SignedXml signedXml = MSDNSample ();
DSA key = DSA.Create ();
signedXml.SigningKey = key;
// Add a KeyInfo.
KeyInfo keyInfo = new KeyInfo ();
keyInfo.AddClause (new DSAKeyValue (key));
signedXml.KeyInfo = keyInfo;
Assert.AreEqual (1, signedXml.KeyInfo.Count, "KeyInfo");
Assert.IsNull (signedXml.SignatureLength, "SignatureLength");
Assert.IsNull (signedXml.SignatureMethod, "SignatureMethod");
Assert.IsNull (signedXml.SignatureValue, "SignatureValue");
Assert.IsNull (signedXml.SigningKeyName, "SigningKeyName");
// Compute the signature.
signedXml.ComputeSignature ();
Assert.IsNull (signedXml.SignatureLength, "SignatureLength");
Assert.AreEqual (SignedXml.XmlDsigDSAUrl, signedXml.SignatureMethod, "SignatureMethod");
Assert.AreEqual (40, signedXml.SignatureValue.Length, "SignatureValue");
Assert.IsNull (signedXml.SigningKeyName, "SigningKeyName");
// Get the XML representation of the signature.
XmlElement xmlSignature = signedXml.GetXml ();
// LAMESPEC: we must reload the signature or it won't work
// MS framework throw a "malformed element"
SignedXml vrfy = new SignedXml ();
vrfy.LoadXml (xmlSignature);
// assert that we can verify our own signature
Assert.IsTrue (vrfy.CheckSignature (), "DSA-Compute/Verify");
}
[Test]
public void SymmetricHMACSHA1Signature ()
{
SignedXml signedXml = MSDNSample ();
// Compute the signature.
byte[] secretkey = Encoding.Default.GetBytes ("password");
HMACSHA1 hmac = new HMACSHA1 (secretkey);
#if NET_2_0
Assert.AreEqual (0, signedXml.KeyInfo.Count, "KeyInfo");
#else
Assert.IsNull (signedXml.KeyInfo, "KeyInfo");
#endif
Assert.IsNull (signedXml.SignatureLength, "SignatureLength");
Assert.IsNull (signedXml.SignatureMethod, "SignatureMethod");
Assert.IsNull (signedXml.SignatureValue, "SignatureValue");
Assert.IsNull (signedXml.SigningKeyName, "SigningKeyName");
signedXml.ComputeSignature (hmac);
#if NET_2_0
Assert.AreEqual (0, signedXml.KeyInfo.Count, "KeyInfo");
#else
Assert.IsNull (signedXml.KeyInfo, "KeyInfo");
#endif
Assert.IsNull (signedXml.SignatureLength, "SignatureLength");
Assert.AreEqual (SignedXml.XmlDsigHMACSHA1Url, signedXml.SignatureMethod, "SignatureMethod");
Assert.AreEqual (20, signedXml.SignatureValue.Length, "SignatureValue");
Assert.IsNull (signedXml.SigningKeyName, "SigningKeyName");
// Get the XML representation of the signature.
XmlElement xmlSignature = signedXml.GetXml ();
// LAMESPEC: we must reload the signature or it won't work
// MS framework throw a "malformed element"
SignedXml vrfy = new SignedXml ();
vrfy.LoadXml (xmlSignature);
// assert that we can verify our own signature
Assert.IsTrue (vrfy.CheckSignature (hmac), "HMACSHA1-Compute/Verify");
}
[Test]
[ExpectedException (typeof (CryptographicException))]
public void SymmetricMACTripleDESSignature ()
{
SignedXml signedXml = MSDNSample ();
// Compute the signature.
byte[] secretkey = Encoding.Default.GetBytes ("password");
MACTripleDES hmac = new MACTripleDES (secretkey);
signedXml.ComputeSignature (hmac);
}
// Using empty constructor
// LAMESPEC: The two other constructors don't seems to apply in verifying signatures
[Test]
public void AsymmetricRSAVerify ()
{
string value = "<Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><SignedInfo><CanonicalizationMethod Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\" /><SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\" /><Reference URI=\"#MyObjectId\"><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" /><DigestValue>/Vvq6sXEVbtZC8GwNtLQnGOy/VI=</DigestValue></Reference></SignedInfo><SignatureValue>A6XuE8Cy9iOffRXaW9b0+dUcMUJQnlmwLsiqtQnADbCtZXnXAaeJ6nGnQ4Mm0IGi0AJc7/2CoJReXl7iW4hltmFguG1e3nl0VxCyCTHKGOCo1u8R3K+B1rTaenFbSxs42EM7/D9KETsPlzfYfis36yM3PqatiCUOsoMsAiMGzlc=</SignatureValue><KeyInfo><KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><RSAKeyValue><Modulus>tI8QYIpbG/m6JLyvP+S3X8mzcaAIayxomyTimSh9UCpEucRnGvLw0P73uStNpiF7wltTZA1HEsv+Ha39dY/0j/Wiy3RAodGDRNuKQao1wu34aNybZ673brbsbHFUfw/o7nlKD2xO84fbajBZmKtBBDy63NHt+QL+grSrREPfCTM=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue></KeyValue></KeyInfo><Object Id=\"MyObjectId\"><MyElement xmlns=\"samples\">This is some text</MyElement></Object></Signature>";
XmlDocument doc = new XmlDocument ();
doc.LoadXml (value);
SignedXml v1 = new SignedXml ();
v1.LoadXml (doc.DocumentElement);
Assert.IsTrue (v1.CheckSignature (), "RSA-CheckSignature()");
SignedXml v2 = new SignedXml ();
v2.LoadXml (doc.DocumentElement);
AsymmetricAlgorithm key = null;
bool vrfy = v2.CheckSignatureReturningKey (out key);
Assert.IsTrue (vrfy, "RSA-CheckSignatureReturningKey()");
SignedXml v3 = new SignedXml ();
v3.LoadXml (doc.DocumentElement);
Assert.IsTrue (v3.CheckSignature (key), "RSA-CheckSignature(key)");
}
// Using empty constructor
// LAMESPEC: The two other constructors don't seems to apply in verifying signatures
[Test]
public void AsymmetricDSAVerify ()
{
string value = "<Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><SignedInfo><CanonicalizationMethod Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\" /><SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#dsa-sha1\" /><Reference URI=\"#MyObjectId\"><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" /><DigestValue>/Vvq6sXEVbtZC8GwNtLQnGOy/VI=</DigestValue></Reference></SignedInfo><SignatureValue>BYz/qRGjGsN1yMFPxWa3awUZm1y4I/IxOQroMxkOteRGgk1HIwhRYw==</SignatureValue><KeyInfo><KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><DSAKeyValue><P>iglVaZ+LsSL8Y0aDXmFMBwva3xHqIypr3l/LtqBH9ziV2Sh1M4JVasAiKqytWIWt/s/Uk8Ckf2tO2Ww1vsNi1NL+Kg9T7FE52sn380/rF0miwGkZeidzm74OWhykb3J+wCTXaIwOzAWI1yN7FoeoN7wzF12jjlSXAXeqPMlViqk=</P><Q>u4sowiJMHilNRojtdmIuQY2YnB8=</Q><G>SdnN7d+wn1n+HH4Hr8MIryIRYgcXdbZ5TH7jAnuWc1koqRc1AZfcYAZ6RDf+orx6Lzn055FTFiN+1NHQfGUtXJCWW0zz0FVV1NJux7WRj8vGTldjJ5ef0oCenkpwDjcIxWsZgVobve4GPoyN1sAc1scnkJB59oupibklmF4y72A=</G><Y>XejzS8Z51yfl0zbYnxSYYbHqreSLjNCoGPB/KjM1TOyV5sMjz0StKtGrFWryTWc7EgvFY7kUth4e04VKf9HbK8z/FifHTXj8+Tszbjzw8GfInnBwLN+vJgbpnjtypmiI5Bm2nLiRbfkdAHP+OrKtr/EauM9GQfYuaxm3/Vj8B84=</Y><J>vGwGg9wqwwWP9xsoPoXu6kHArJtadiNKe9azBiUx5Ob883gd5wlKfEcGuKkBmBySGbgwxyOsIBovd9Kk48hF01ymfQzAAuHR0EdJECSsTsTTKVTLQNBU32O+PRbLYpv4E8kt6rNL83JLJCBY</J><Seed>sqzn8J6fd2gtEyq6YOqiUSHgPE8=</Seed><PgenCounter>sQ==</PgenCounter></DSAKeyValue></KeyValue></KeyInfo><Object Id=\"MyObjectId\"><MyElement xmlns=\"samples\">This is some text</MyElement></Object></Signature>";
XmlDocument doc = new XmlDocument ();
doc.LoadXml (value);
SignedXml v1 = new SignedXml ();
v1.LoadXml (doc.DocumentElement);
Assert.IsTrue (v1.CheckSignature (), "DSA-CheckSignature()");
SignedXml v2 = new SignedXml ();
v2.LoadXml (doc.DocumentElement);
AsymmetricAlgorithm key = null;
bool vrfy = v2.CheckSignatureReturningKey (out key);
Assert.IsTrue (vrfy, "DSA-CheckSignatureReturningKey()");
SignedXml v3 = new SignedXml ();
v3.LoadXml (doc.DocumentElement);
Assert.IsTrue (v3.CheckSignature (key), "DSA-CheckSignature(key)");
}
[Test]
public void SymmetricHMACSHA1Verify ()
{
string value = "<Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><SignedInfo><CanonicalizationMethod Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\" /><SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#hmac-sha1\" /><Reference URI=\"#MyObjectId\"><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" /><DigestValue>/Vvq6sXEVbtZC8GwNtLQnGOy/VI=</DigestValue></Reference></SignedInfo><SignatureValue>e2RxYr5yGbvTqZLCFcgA2RAC0yE=</SignatureValue><Object Id=\"MyObjectId\"><MyElement xmlns=\"samples\">This is some text</MyElement></Object></Signature>";
XmlDocument doc = new XmlDocument ();
doc.LoadXml (value);
SignedXml v1 = new SignedXml ();
v1.LoadXml (doc.DocumentElement);
byte[] secretkey = Encoding.Default.GetBytes ("password");
HMACSHA1 hmac = new HMACSHA1 (secretkey);
Assert.IsTrue (v1.CheckSignature (hmac), "HMACSHA1-CheckSignature(key)");
}
[Test]
// adapted from http://bugzilla.ximian.com/show_bug.cgi?id=52084
public void GetIdElement ()
{
XmlDocument doc = new XmlDocument ();
doc.LoadXml (signature);
SignedXml v1 = new SignedXml ();
v1.LoadXml (doc.DocumentElement);
Assert.IsTrue (v1.CheckSignature (), "CheckSignature");
XmlElement xel = v1.GetIdElement (doc, "MyObjectId");
Assert.IsTrue (xel.InnerXml.StartsWith ("<ObjectListTag"), "GetIdElement");
}
[Test]
public void GetPublicKey ()
{
XmlDocument doc = new XmlDocument ();
doc.LoadXml (signature);
SignedXmlEx sxe = new SignedXmlEx ();
sxe.LoadXml (doc.DocumentElement);
AsymmetricAlgorithm aa1 = sxe.PublicGetPublicKey ();
Assert.IsTrue ((aa1 is RSA), "First Public Key is RSA");
AsymmetricAlgorithm aa2 = sxe.PublicGetPublicKey ();
Assert.IsNull (aa2, "Second Public Key is null");
}
#if NET_2_0
[Test]
// [ExpectedException (typeof (ArgumentNullException))]
public void AddObject_Null ()
{
SignedXml sx = new SignedXml ();
// still no ArgumentNullExceptions for this one
sx.AddObject (null);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void AddReference_Null ()
{
SignedXml sx = new SignedXml ();
sx.AddReference (null);
}
#else
[Test]
public void Add_Null ()
{
SignedXml sx = new SignedXml ();
// no ArgumentNull exceptions for those
sx.AddObject (null);
sx.AddReference (null);
}
#endif
[Test]
[ExpectedException (typeof (CryptographicException))]
public void GetXml_WithoutInfo ()
{
SignedXml sx = new SignedXml ();
XmlElement xel = sx.GetXml ();
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void LoadXml_Null ()
{
SignedXml sx = new SignedXml ();
sx.LoadXml (null);
}
[Test]
public void SigningKeyName ()
{
SignedXmlEx sxe = new SignedXmlEx ();
Assert.IsNull (sxe.SigningKeyName, "SigningKeyName");
sxe.SigningKeyName = "mono";
Assert.AreEqual ("mono", sxe.SigningKeyName, "SigningKeyName");
}
[Test]
public void CheckSignatureEmptySafe ()
{
SignedXml sx;
KeyInfoClause kic;
KeyInfo ki;
// empty keyinfo passes...
sx = new SignedXml ();
sx.KeyInfo = new KeyInfo ();
Assert.IsTrue (!sx.CheckSignature ());
// with empty KeyInfoName
kic = new KeyInfoName ();
ki = new KeyInfo ();
ki.AddClause (kic);
sx.KeyInfo = ki;
Assert.IsTrue (!sx.CheckSignature ());
}
[Test]
#if !NET_2_0
[ExpectedException (typeof (CryptographicException))]
#endif
public void CheckSignatureEmpty ()
{
SignedXml sx = new SignedXml ();
Assert.IsTrue (!sx.CheckSignature ());
}
[Test]
[ExpectedException (typeof (CryptographicException))]
public void ComputeSignatureNoSigningKey ()
{
XmlDocument doc = new XmlDocument ();
doc.LoadXml ("<foo/>");
SignedXml signedXml = new SignedXml (doc);
Reference reference = new Reference ();
reference.Uri = "";
XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform ();
reference.AddTransform (env);
signedXml.AddReference (reference);
signedXml.ComputeSignature ();
}
[Test]
[ExpectedException (typeof (CryptographicException))]
public void ComputeSignatureMissingReferencedObject ()
{
XmlDocument doc = new XmlDocument ();
doc.LoadXml ("<foo/>");
SignedXml signedXml = new SignedXml (doc);
DSA key = DSA.Create ();
signedXml.SigningKey = key;
Reference reference = new Reference ();
reference.Uri = "#bleh";
XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform ();
reference.AddTransform (env);
signedXml.AddReference (reference);
signedXml.ComputeSignature ();
}
[Test]
public void DataReferenceToNonDataObject ()
{
XmlDocument doc = new XmlDocument ();
doc.LoadXml ("<foo Id='id:1'/>");
SignedXml signedXml = new SignedXml (doc);
DSA key = DSA.Create ();
signedXml.SigningKey = key;
Reference reference = new Reference ();
reference.Uri = "#id:1";
XmlDsigC14NTransform t = new XmlDsigC14NTransform ();
reference.AddTransform (t);
signedXml.AddReference (reference);
signedXml.ComputeSignature ();
}
[Test]
public void SignElementWithoutPrefixedNamespace ()
{
string input = "<Action xmlns='urn:foo'>http://tempuri.org/IFoo/Echo</Action>";
string expected = @"<Signature xmlns=""http://www.w3.org/2000/09/xmldsig#""><SignedInfo><CanonicalizationMethod Algorithm=""http://www.w3.org/TR/2001/REC-xml-c14n-20010315"" /><SignatureMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#hmac-sha1"" /><Reference URI=""#_1""><Transforms><Transform Algorithm=""http://www.w3.org/TR/2001/REC-xml-c14n-20010315"" /></Transforms><DigestMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#sha1"" /><DigestValue>zdFEZB8rNzvpNG/gFEJBWk/M5Nk=</DigestValue></Reference></SignedInfo><SignatureValue>+OyGVzrHjmKSDWLNyvgx8pjbPfM=</SignatureValue><Object Id=""_1""><Action xmlns=""urn:foo"">http://tempuri.org/IFoo/Echo</Action></Object></Signature>";
byte [] key = Convert.FromBase64String (
"1W5EigVnbnRjGLbg99ElieOmuUgYO+KcwMJtE35SAGI=");
Transform t = new XmlDsigC14NTransform ();
Assert.AreEqual (expected, SignWithHMACSHA1 (input, key, t), "#1");
// The second result should be still identical.
Assert.AreEqual (expected, SignWithHMACSHA1 (input, key, t), "#2");
}
[Test]
public void SignElementWithPrefixedNamespace ()
{
string input = "<a:Action xmlns:a='urn:foo'>http://tempuri.org/IFoo/Echo</a:Action>";
string expected = @"<Signature xmlns=""http://www.w3.org/2000/09/xmldsig#""><SignedInfo><CanonicalizationMethod Algorithm=""http://www.w3.org/TR/2001/REC-xml-c14n-20010315"" /><SignatureMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#hmac-sha1"" /><Reference URI=""#_1""><Transforms><Transform Algorithm=""http://www.w3.org/TR/2001/REC-xml-c14n-20010315"" /></Transforms><DigestMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#sha1"" /><DigestValue>6i5FlqkEfJOdUaOMCK7xn0I0HDg=</DigestValue></Reference></SignedInfo><SignatureValue>tASp+e2A0xqcm02jKg5TGqlhKpI=</SignatureValue><Object Id=""_1""><a:Action xmlns:a=""urn:foo"">http://tempuri.org/IFoo/Echo</a:Action></Object></Signature>";
byte [] key = Convert.FromBase64String (
"1W5EigVnbnRjGLbg99ElieOmuUgYO+KcwMJtE35SAGI=");
Assert.AreEqual (SignWithHMACSHA1 (input, key), expected);
}
string SignWithHMACSHA1 (string input, byte [] key)
{
return SignWithHMACSHA1 (input, key, new XmlDsigC14NTransform ());
}
string SignWithHMACSHA1 (string input, byte [] key, Transform transform)
{
XmlDocument doc = new XmlDocument ();
doc.LoadXml (input);
SignedXml sxml = new SignedXml (doc);
HMACSHA1 keyhash = new HMACSHA1 (key);
DataObject d = new DataObject ();
//d.Data = doc.SelectNodes ("//*[local-name()='Body']/*");
d.Data = doc.SelectNodes ("//*[local-name()='Action']");
d.Id = "_1";
sxml.AddObject (d);
Reference r = new Reference ("#_1");
r.AddTransform (transform);
r.DigestMethod = SignedXml.XmlDsigSHA1Url;
sxml.SignedInfo.AddReference (r);
sxml.ComputeSignature (keyhash);
StringWriter sw = new StringWriter ();
XmlWriter w = new XmlTextWriter (sw);
sxml.GetXml ().WriteTo (w);
w.Close ();
return sw.ToString ();
}
[Test]
public void GetIdElement_Null ()
{
SignedXml sign = new SignedXml ();
Assert.IsNull (sign.GetIdElement (null, "value"));
Assert.IsNull (sign.GetIdElement (new XmlDocument (), null));
}
#if NET_2_0
[Test]
[Category ("NotWorking")] // bug #79483
public void DigestValue_CRLF ()
{
XmlDocument doc = CreateSomeXml ("\r\n");
XmlDsigExcC14NTransform transform = new XmlDsigExcC14NTransform ();
transform.LoadInput (doc);
Stream s = (Stream) transform.GetOutput ();
string output = Stream2String (s);
Assert.AreEqual ("<person>
\n <birthplace>Brussels</birthplace>
\n</person>", output, "#1");
s.Position = 0;
HashAlgorithm hash = HashAlgorithm.Create ("System.Security.Cryptography.SHA1CryptoServiceProvider");
byte[] digest = hash.ComputeHash (s);
Assert.AreEqual ("IKbfdK2/DMfXyezCf5QggVCXfk8=", Convert.ToBase64String (digest), "#2");
X509Certificate2 cert = new X509Certificate2 (_pkcs12, "mono");
SignedXml signedXml = new SignedXml (doc);
signedXml.SigningKey = cert.PrivateKey;
signedXml.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigExcC14NTransformUrl;
Reference reference = new Reference ();
reference.Uri = "";
XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform ();
reference.AddTransform (env);
signedXml.AddReference (reference);
KeyInfo keyInfo = new KeyInfo ();
KeyInfoX509Data x509KeyInfo = new KeyInfoX509Data ();
x509KeyInfo.AddCertificate (new X509Certificate2 (_cert));
x509KeyInfo.AddCertificate (cert);
keyInfo.AddClause (x509KeyInfo);
signedXml.KeyInfo = keyInfo;
signedXml.ComputeSignature ();
digest = reference.DigestValue;
Assert.AreEqual ("e3dsi1xK8FAx1vsug7J203JbEAU=", Convert.ToBase64String (digest), "#3");
Assert.AreEqual ("<SignedInfo xmlns=\"http://www.w3.org/2000/09/xmldsig#\">"
+ "<CanonicalizationMethod Algorithm=\"http://www.w3.org/2001/10/xml-exc-c14n#\" />"
+ "<SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\" />"
+ "<Reference URI=\"\">"
+ "<Transforms>"
+ "<Transform Algorithm=\"http://www.w3.org/2000/09/xmldsig#enveloped-signature\" />"
+ "</Transforms>"
+ "<DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" />"
+ "<DigestValue>e3dsi1xK8FAx1vsug7J203JbEAU=</DigestValue>"
+ "</Reference>"
+ "</SignedInfo>", signedXml.SignedInfo.GetXml ().OuterXml, "#4");
}
[Test]
public void DigestValue_LF ()
{
XmlDocument doc = CreateSomeXml ("\n");
XmlDsigExcC14NTransform transform = new XmlDsigExcC14NTransform ();
transform.LoadInput (doc);
Stream s = (Stream) transform.GetOutput ();
string output = Stream2String (s);
Assert.AreEqual ("<person>\n <birthplace>Brussels</birthplace>\n</person>", output, "#1");
s.Position = 0;
HashAlgorithm hash = HashAlgorithm.Create ("System.Security.Cryptography.SHA1CryptoServiceProvider");
byte[] digest = hash.ComputeHash (s);
Assert.AreEqual ("e3dsi1xK8FAx1vsug7J203JbEAU=", Convert.ToBase64String (digest), "#2");
X509Certificate2 cert = new X509Certificate2 (_pkcs12, "mono");
SignedXml signedXml = new SignedXml (doc);
signedXml.SigningKey = cert.PrivateKey;
signedXml.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigExcC14NTransformUrl;
Reference reference = new Reference ();
reference.Uri = "";
XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform ();
reference.AddTransform (env);
signedXml.AddReference (reference);
KeyInfo keyInfo = new KeyInfo ();
KeyInfoX509Data x509KeyInfo = new KeyInfoX509Data ();
x509KeyInfo.AddCertificate (new X509Certificate2 (_cert));
x509KeyInfo.AddCertificate (cert);
keyInfo.AddClause (x509KeyInfo);
signedXml.KeyInfo = keyInfo;
signedXml.ComputeSignature ();
digest = reference.DigestValue;
Assert.AreEqual ("e3dsi1xK8FAx1vsug7J203JbEAU=", Convert.ToBase64String (digest), "#3");
Assert.AreEqual ("<SignedInfo xmlns=\"http://www.w3.org/2000/09/xmldsig#\">"
+ "<CanonicalizationMethod Algorithm=\"http://www.w3.org/2001/10/xml-exc-c14n#\" />"
+ "<SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\" />"
+ "<Reference URI=\"\">"
+ "<Transforms>"
+ "<Transform Algorithm=\"http://www.w3.org/2000/09/xmldsig#enveloped-signature\" />"
+ "</Transforms>"
+ "<DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" />"
+ "<DigestValue>e3dsi1xK8FAx1vsug7J203JbEAU=</DigestValue>"
+ "</Reference>"
+ "</SignedInfo>", signedXml.SignedInfo.GetXml ().OuterXml, "#4");
}
[Test]
[Category ("NotWorking")] // bug #79483
public void SignedXML_CRLF_Invalid ()
{
X509Certificate2 cert = new X509Certificate2 (_pkcs12, "mono");
XmlDocument doc = new XmlDocument ();
doc.LoadXml (string.Format (CultureInfo.InvariantCulture,
"<person>{0}" +
" <birthplace>Brussels</birthplace>{0}" +
"<Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\">" +
"<SignedInfo>" +
"<CanonicalizationMethod Algorithm=\"http://www.w3.org/2001/10/xml-exc-c14n#\" />" +
"<SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\" />" +
"<Reference URI=\"\">" +
"<Transforms>" +
"<Transform Algorithm=\"http://www.w3.org/2000/09/xmldsig#enveloped-signature\" />" +
"</Transforms>" +
"<DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" />" +
"<DigestValue>IKbfdK2/DMfXyezCf5QggVCXfk8=</DigestValue>" +
"</Reference>" +
"</SignedInfo>" +
"<SignatureValue>" +
"JuSd68PyARsZqGKSo5xX5yYHDuu6whHEhoXqxxFmGeEdvkKY2bgroWJ1ZTGHGr" +
"VI7mtG3h0w1ibOKdltm9j4lZaZWo87CAJiJ2syeLbMyIVSw6OyZEsiFF/VqLKK" +
"4T4AO6q7HYsC55zJrOvL1j9IIr8zBnJfvBdKckf0lczYbXc=" +
"</SignatureValue>" +
"<KeyInfo>" +
"<X509Data>" +
"<X509Certificate>" +
"MIIBozCCAQygAwIBAgIQHc+8iURSTUarmg4trmrnGTANBgkqhkiG9w0BAQUFAD" +
"ARMQ8wDQYDVQQDEwZOb3ZlbGwwIBcNMDYwOTIxMDcyNjUxWhgPMjA5MDAxMjEw" +
"ODI2NTFaMA8xDTALBgNVBAMTBE1vbm8wgZ0wDQYJKoZIhvcNAQEBBQADgYsAMI" +
"GHAoGBAJhFB1KHv2WzsHqih9Mvm3KffEOSMv+sh1mPW3sWI/95VOOVqJnhemMM" +
"s82phSbNZeoPHny4btbykbRRaRQv94rtIM6geJR1e2c5mfJWtHSq3EYQarHC68" +
"cAZvCAmQZGa1eQRNRqcTSKX8yfqH0SouIE9ohJtpiluNe+Xgk5fKv3AgERMA0G" +
"CSqGSIb3DQEBBQUAA4GBAE6pqSgK8QKRHSh6YvYs9oRh1n8iREco7QmZCFj7UB" +
"kn/QgJ9mKsT8o12VnYHqBCEwBNaT1ay3z/SR4/Z383zuu4Y6xxjqOqnM6gtwUV" +
"u5/0hvz+ThtuTjItG6Ny5JkLZZQt/XbI5kg920t9jq3vbHBMuX2HxivwQe5sug" +
"jPaTEY" +
"</X509Certificate>" +
"<X509Certificate>" +
"MIIBpTCCAQ6gAwIBAgIQXo6Lr3rrSkW4xmNPRbHMbjANBgkqhkiG9w0BAQUFAD" +
"ARMQ8wDQYDVQQDEwZOb3ZlbGwwIBcNMDYwOTIxMDcxNDE4WhgPMjA5MDAxMjEw" +
"ODE0MThaMBExDzANBgNVBAMTBk1pZ3VlbDCBnTANBgkqhkiG9w0BAQEFAAOBiw" +
"AwgYcCgYEArCkeSZ6U3U3Fm2qSuQsM7xvvsSzZGQLPDUHFQ/BZxA7LiGRfXbmO" +
"yPkkYRYItXdy0yDl/8rAjelaL8jQ4me6Uexyeq+5xEgHn9VbNJny5apGNi4kF1" +
"8DR5DK9Zme9d6icusgW8krv3//5SVE8ao7X5qrIOGS825eCJL73YWbxKkCAREw" +
"DQYJKoZIhvcNAQEFBQADgYEASqBgYTkIJpDO28ZEXnF5Q/G3xDR/MxhdcrCISJ" +
"tDbuGVZzK+xhFhiYD5Q1NiGhD4oDIVJPwKmZH4L3YP96iSh6RdtO27V05ET/X5" +
"yWMKdeIsq6r9jXXv7NaWTmvNfMLKLNgEBCJ00+wN0u4xHUC7yCJc0KNQ3fjDLU" +
"AT1oaVjWI=" +
"</X509Certificate>" +
"</X509Data>" +
"</KeyInfo>" +
"</Signature>" +
"</person>", "\r\n"));
SignedXml signedXml = new SignedXml (doc);
XmlNodeList nodeList = doc.GetElementsByTagName ("Signature");
signedXml.LoadXml ((XmlElement) nodeList [0]);
Assert.IsTrue (!signedXml.CheckSignature (), "#2");
}
[Test]
[Category ("NotWorking")] // bug #79483
public void SignedXML_CRLF_Valid ()
{
X509Certificate2 cert = new X509Certificate2 (_pkcs12, "mono");
XmlDocument doc = CreateSignedXml (cert, SignedXml.XmlDsigExcC14NTransformUrl, "\r\n");
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture, "<person>{0}" +
" <birthplace>Brussels</birthplace>{0}" +
"<Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\">" +
"<SignedInfo>" +
"<CanonicalizationMethod Algorithm=\"http://www.w3.org/2001/10/xml-exc-c14n#\" />" +
"<SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\" />" +
"<Reference URI=\"\">" +
"<Transforms>" +
"<Transform Algorithm=\"http://www.w3.org/2000/09/xmldsig#enveloped-signature\" />" +
"</Transforms>" +
"<DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" />" +
"<DigestValue>e3dsi1xK8FAx1vsug7J203JbEAU=</DigestValue>" +
"</Reference>" +
"</SignedInfo>" +
"<SignatureValue>" +
"X29nbkOR/Xk3KwsEpEvpDOqfI6/NTtiewIxNqKMrPCoM0HLawK5HKsCw3lL07C" +
"8SwqvoXJL9VS05gsSia85YCB8NPDeHuHY3CPGT7DVpgeHFA0oefMnOi8IAqKD2" +
"nx29A222u5OmwbDO0qFqbtsgvIFiP5YJg04cwmnqs+eL+WA=" +
"</SignatureValue>" +
"<KeyInfo>" +
"<X509Data>" +
"<X509Certificate>" +
"MIIBozCCAQygAwIBAgIQHc+8iURSTUarmg4trmrnGTANBgkqhkiG9w0BAQUFAD" +
"ARMQ8wDQYDVQQDEwZOb3ZlbGwwIBcNMDYwOTIxMDcyNjUxWhgPMjA5MDAxMjEw" +
"ODI2NTFaMA8xDTALBgNVBAMTBE1vbm8wgZ0wDQYJKoZIhvcNAQEBBQADgYsAMI" +
"GHAoGBAJhFB1KHv2WzsHqih9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
"</person>", "\r\n"), doc.OuterXml, "#1");
}
[Test]
[Ignore ("This is a bad test case which should basically just check the computed signature value instead of comparing XML document literal string, and thus caused inconsistency between .NET 1.1 and .NET 2.0. Not deleting this test case, to easily find the reason for potentially happening regression in the future (which should not waste time).")]
public void SignedXML_LF_Valid ()
{
X509Certificate2 cert = new X509Certificate2 (_pkcs12, "mono");
XmlDocument doc = CreateSignedXml (cert, SignedXml.XmlDsigExcC14NTransformUrl, "\n");
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture, "<person>{0}" +
" <birthplace>Brussels</birthplace>{0}" +
"<Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\">" +
"<SignedInfo>" +
"<CanonicalizationMethod Algorithm=\"http://www.w3.org/2001/10/xml-exc-c14n#\" />" +
"<SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\" />" +
"<Reference URI=\"\">" +
"<Transforms>" +
"<Transform Algorithm=\"http://www.w3.org/2000/09/xmldsig#enveloped-signature\" />" +
"</Transforms>" +
"<DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" />" +
"<DigestValue>e3dsi1xK8FAx1vsug7J203JbEAU=</DigestValue>" +
"</Reference>" +
"</SignedInfo>" +
"<SignatureValue>" +
"X29nbkOR/Xk3KwsEpEvpDOqfI6/NTtiewIxNqKMrPCoM0HLawK5HKsCw3lL07C" +
"8SwqvoXJL9VS05gsSia85YCB8NPDeHuHY3CPGT7DVpgeHFA0oefMnOi8IAqKD2" +
"nx29A222u5OmwbDO0qFqbtsgvIFiP5YJg04cwmnqs+eL+WA=" +
"</SignatureValue>" +
"<KeyInfo>" +
"<X509Data>" +
"<X509Certificate>" +
"MIIBozCCAQygAwIBAgIQHc+8iURSTUarmg4trmrnGTANBgkqhkiG9w0BAQUFAD" +
"ARMQ8wDQYDVQQDEwZOb3ZlbGwwIBcNMDYwOTIxMDcyNjUxWhgPMjA5MDAxMjEw" +
"ODI2NTFaMA8xDTALBgNVBA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Q3fjDLU" +
"AT1oaVjWI=" +
"</X509Certificate>" +
"</X509Data>" +
"</KeyInfo>" +
"</Signature>" +
"</person>", "\n"), doc.OuterXml, "#1");
}
[Test] // part of bug #79454
public void MultipleX509Certificates ()
{
XmlDocument doc = null;
X509Certificate2 cert = new X509Certificate2 (_pkcs12, "mono");
doc = CreateSignedXml (cert, SignedXml.XmlDsigExcC14NTransformUrl, "\n");
Assert.IsTrue (VerifySignedXml (doc), "#1");
doc = CreateSignedXml (cert, SignedXml.XmlDsigExcC14NWithCommentsTransformUrl, "\n");
Assert.IsTrue (VerifySignedXml (doc), "#2");
doc = CreateSignedXml (cert, SignedXml.XmlDsigCanonicalizationUrl, "\n");
Assert.IsTrue (VerifySignedXml (doc), "#3");
doc = CreateSignedXml (cert, SignedXml.XmlDsigCanonicalizationWithCommentsUrl, "\n");
Assert.IsTrue (VerifySignedXml (doc), "#4");
}
// creates a signed XML document with two certificates in the X509Data
// element, with the second being the one that should be used to verify
// the signature
static XmlDocument CreateSignedXml (X509Certificate2 cert, string canonicalizationMethod, string lineFeed)
{
XmlDocument doc = CreateSomeXml (lineFeed);
SignedXml signedXml = new SignedXml (doc);
signedXml.SigningKey = cert.PrivateKey;
signedXml.SignedInfo.CanonicalizationMethod = canonicalizationMethod;
Reference reference = new Reference ();
reference.Uri = "";
XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform ();
reference.AddTransform (env);
signedXml.AddReference (reference);
KeyInfo keyInfo = new KeyInfo ();
KeyInfoX509Data x509KeyInfo = new KeyInfoX509Data ();
x509KeyInfo.AddCertificate (new X509Certificate2 (_cert));
x509KeyInfo.AddCertificate (cert);
keyInfo.AddClause (x509KeyInfo);
signedXml.KeyInfo = keyInfo;
signedXml.ComputeSignature ();
XmlElement xmlDigitalSignature = signedXml.GetXml ();
doc.DocumentElement.AppendChild (doc.ImportNode (xmlDigitalSignature, true));
return doc;
}
static bool VerifySignedXml (XmlDocument signedDoc)
{
SignedXml signedXml = new SignedXml (signedDoc);
XmlNodeList nodeList = signedDoc.GetElementsByTagName ("Signature");
signedXml.LoadXml ((XmlElement) nodeList [0]);
return signedXml.CheckSignature ();
}
static XmlDocument CreateSomeXml (string lineFeed)
{
StringWriter sw = new StringWriter ();
sw.NewLine = lineFeed;
XmlTextWriter xtw = new XmlTextWriter (sw);
xtw.Formatting = Formatting.Indented;
xtw.WriteStartElement ("person");
xtw.WriteElementString ("birthplace", "Brussels");
xtw.WriteEndElement ();
xtw.Flush ();
XmlDocument doc = new XmlDocument ();
doc.PreserveWhitespace = true;
doc.Load (new StringReader (sw.ToString ()));
return doc;
}
string Stream2String (Stream s)
{
StringBuilder sb = new StringBuilder ();
int b = s.ReadByte ();
while (b != -1) {
sb.Append (Convert.ToChar (b));
b = s.ReadByte ();
}
return sb.ToString ();
}
private static byte [] _cert = new byte [] {
0x30, 0x82, 0x01, 0xa3, 0x30, 0x82, 0x01, 0x0c, 0xa0, 0x03, 0x02,
0x01, 0x02, 0x02, 0x10, 0x1d, 0xcf, 0xbc, 0x89, 0x44, 0x52, 0x4d,
0x46, 0xab, 0x9a, 0x0e, 0x2d, 0xae, 0x6a, 0xe7, 0x19, 0x30, 0x0d,
0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05,
0x05, 0x00, 0x30, 0x11, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03, 0x55,
0x04, 0x03, 0x13, 0x06, 0x4e, 0x6f, 0x76, 0x65, 0x6c, 0x6c, 0x30,
0x20, 0x17, 0x0d, 0x30, 0x36, 0x30, 0x39, 0x32, 0x31, 0x30, 0x37,
0x32, 0x36, 0x35, 0x31, 0x5a, 0x18, 0x0f, 0x32, 0x30, 0x39, 0x30,
0x30, 0x31, 0x32, 0x31, 0x30, 0x38, 0x32, 0x36, 0x35, 0x31, 0x5a,
0x30, 0x0f, 0x31, 0x0d, 0x30, 0x0b, 0x06, 0x03, 0x55, 0x04, 0x03,
0x13, 0x04, 0x4d, 0x6f, 0x6e, 0x6f, 0x30, 0x81, 0x9d, 0x30, 0x0d,
0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
0x05, 0x00, 0x03, 0x81, 0x8b, 0x00, 0x30, 0x81, 0x87, 0x02, 0x81,
0x81, 0x00, 0x98, 0x45, 0x07, 0x52, 0x87, 0xbf, 0x65, 0xb3, 0xb0,
0x7a, 0xa2, 0x87, 0xd3, 0x2f, 0x9b, 0x72, 0x9f, 0x7c, 0x43, 0x92,
0x32, 0xff, 0xac, 0x87, 0x59, 0x8f, 0x5b, 0x7b, 0x16, 0x23, 0xff,
0x79, 0x54, 0xe3, 0x95, 0xa8, 0x99, 0xe1, 0x7a, 0x63, 0x0c, 0xb3,
0xcd, 0xa9, 0x85, 0x26, 0xcd, 0x65, 0xea, 0x0f, 0x1e, 0x7c, 0xb8,
0x6e, 0xd6, 0xf2, 0x91, 0xb4, 0x51, 0x69, 0x14, 0x2f, 0xf7, 0x8a,
0xed, 0x20, 0xce, 0xa0, 0x78, 0x94, 0x75, 0x7b, 0x67, 0x39, 0x99,
0xf2, 0x56, 0xb4, 0x74, 0xaa, 0xdc, 0x46, 0x10, 0x6a, 0xb1, 0xc2,
0xeb, 0xc7, 0x00, 0x66, 0xf0, 0x80, 0x99, 0x06, 0x46, 0x6b, 0x57,
0x90, 0x44, 0xd4, 0x6a, 0x71, 0x34, 0x8a, 0x5f, 0xcc, 0x9f, 0xa8,
0x7d, 0x12, 0xa2, 0xe2, 0x04, 0xf6, 0x88, 0x49, 0xb6, 0x98, 0xa5,
0xb8, 0xd7, 0xbe, 0x5e, 0x09, 0x39, 0x7c, 0xab, 0xf7, 0x02, 0x01,
0x11, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
0x01, 0x01, 0x05, 0x05, 0x00, 0x03, 0x81, 0x81, 0x00, 0x4e, 0xa9,
0xa9, 0x28, 0x0a, 0xf1, 0x02, 0x91, 0x1d, 0x28, 0x7a, 0x62, 0xf6,
0x2c, 0xf6, 0x84, 0x61, 0xd6, 0x7f, 0x22, 0x44, 0x47, 0x28, 0xed,
0x09, 0x99, 0x08, 0x58, 0xfb, 0x50, 0x19, 0x27, 0xfd, 0x08, 0x09,
0xf6, 0x62, 0xac, 0x4f, 0xca, 0x35, 0xd9, 0x59, 0xd8, 0x1e, 0xa0,
0x42, 0x13, 0x00, 0x4d, 0x69, 0x3d, 0x5a, 0xcb, 0x7c, 0xff, 0x49,
0x1e, 0x3f, 0x67, 0x7f, 0x37, 0xce, 0xeb, 0xb8, 0x63, 0xac, 0x71,
0x8e, 0xa3, 0xaa, 0x9c, 0xce, 0xa0, 0xb7, 0x05, 0x15, 0xbb, 0x9f,
0xf4, 0x86, 0xfc, 0xfe, 0x4e, 0x1b, 0x6e, 0x4e, 0x32, 0x2d, 0x1b,
0xa3, 0x72, 0xe4, 0x99, 0x0b, 0x65, 0x94, 0x2d, 0xfd, 0x76, 0xc8,
0xe6, 0x48, 0x3d, 0xdb, 0x4b, 0x7d, 0x8e, 0xad, 0xef, 0x6c, 0x70,
0x4c, 0xb9, 0x7d, 0x87, 0xc6, 0x2b, 0xf0, 0x41, 0xee, 0x6c, 0xba,
0x08, 0xcf, 0x69, 0x31, 0x18 };
private static byte [] _pkcs12 = new byte [] {
0x30, 0x82, 0x05, 0x8d, 0x02, 0x01, 0x03, 0x30, 0x82, 0x05, 0x47,
0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01,
0xa0, 0x82, 0x05, 0x38, 0x04, 0x82, 0x05, 0x34, 0x30, 0x82, 0x05,
0x30, 0x30, 0x82, 0x02, 0x3f, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
0xf7, 0x0d, 0x01, 0x07, 0x06, 0xa0, 0x82, 0x02, 0x30, 0x30, 0x82,
0x02, 0x2c, 0x02, 0x01, 0x00, 0x30, 0x82,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x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07,
0x01, 0xa0, 0x82, 0x02, 0xda, 0x04, 0x82, 0x02, 0xd6, 0x30, 0x82,
0x02, 0xd2, 0x30, 0x82, 0x02, 0xce, 0x06, 0x0b, 0x2a, 0x86, 0x48,
0x86, 0xf7, 0x0d, 0x01, 0x0c, 0x0a, 0x01, 0x02, 0xa0, 0x82, 0x02,
0xa6, 0x30, 0x82, 0x02, 0xa2, 0x30, 0x1c, 0x06, 0x0a, 0x2a, 0x86,
0x48, 0x86, 0xf7, 0x0d, 0x01, 0x0c, 0x01, 0x03, 0x30, 0x0e, 0x04,
0x08, 0xe0, 0x21, 0x4f, 0x90, 0x7d, 0x86, 0x72, 0xc7, 0x02, 0x02,
0x07, 0xd0, 0x04, 0x82, 0x02, 0x80, 0x92, 0xac, 0xe8, 0x52, 0xa6,
0x3e, 0xed, 0x3d, 0xbc, 0x28, 0x5f, 0xb9, 0x45, 0x76, 0x27, 0x95,
0xf8, 0x6a, 0xc5, 0x17, 0x97, 0x46, 0x58, 0xe9, 0x15, 0x7c, 0x68,
0x62, 0x67, 0xb5, 0x2f, 0x1b, 0x64, 0x27, 0x9d, 0xfd, 0x67, 0x66,
0x42, 0x21, 0x5c, 0xf4, 0x64, 0x37, 0xcc, 0xc0, 0x04, 0x01, 0x91,
0x6c, 0x6b, 0x84, 0x96, 0xae, 0x04, 0xfe, 0xcc, 0x88, 0x6a, 0x84,
0xd7, 0x59, 0x28, 0x78, 0xc9, 0xb4, 0xf6, 0x4d, 0x86, 0x8d, 0x59,
0xc6, 0x74, 0x30, 0xca, 0x2f, 0x0a, 0xa7, 0x66, 0x99, 0xf4, 0x8f,
0x44, 0x6d, 0x97, 0x3c, 0xd6, 0xdb, 0xd6, 0x31, 0x8c, 0xf7, 0x75,
0xd9, 0x0b, 0xf5, 0xd2, 0x27, 0x80, 0x81, 0x28, 0x0f, 0x6b, 0x8b,
0x45, 0x11, 0x08, 0x1d, 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3d,
0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a,
0x05, 0x00, 0x04, 0x14, 0x32, 0x55, 0x07, 0xa2, 0x67, 0xf3, 0x76,
0x4d, 0x0b, 0x6f, 0xa4, 0xa0, 0x7b, 0xce, 0x2f, 0xc5, 0xff, 0xbe,
0x3e, 0x38, 0x04, 0x14, 0x52, 0xf8, 0xb3, 0xeb, 0xc3, 0xda, 0x79,
0xfa, 0x75, 0x89, 0x67, 0x33, 0x01, 0xd0, 0xb0, 0x13, 0xfa, 0x11,
0x94, 0xac, 0x02, 0x02, 0x07, 0xd0 };
public SignedXml SignHMAC (string uri, KeyedHashAlgorithm mac, bool ok)
{
string input = "<foo/>";
XmlDocument doc = new XmlDocument ();
doc.LoadXml (input);
SignedXml sig = new SignedXml (doc);
Reference r = new Reference ("");
r.AddTransform (new XmlDsigEnvelopedSignatureTransform ());
r.DigestMethod = uri;
sig.AddReference (r);
sig.ComputeSignature (mac);
doc.DocumentElement.AppendChild (doc.ImportNode (sig.GetXml (), true));
// doc.Save (System.Console.Out);
sig.LoadXml (doc.DocumentElement ["Signature"]);
Assert.AreEqual (ok, sig.CheckSignature (mac), "CheckSignature");
return sig;
}
static byte [] hmackey = new byte [0];
private const string more256 = "http://www.w3.org/2001/04/xmldsig-more#hmac-sha256";
private const string more384 = "http://www.w3.org/2001/04/xmldsig-more#hmac-sha384";
private const string more512 = "http://www.w3.org/2001/04/xmldsig-more#hmac-sha512";
private const string moreripe = "http://www.w3.org/2001/04/xmldsig-more#hmac-ripemd160";
[Test]
public void SignHMAC_SHA256 ()
{
SignedXml sign = SignHMAC (EncryptedXml.XmlEncSHA256Url, new HMACSHA256 (hmackey), true);
Assert.AreEqual (more256, sign.SignatureMethod, "SignatureMethod");
}
[Test]
public void SignHMAC_SHA256_Bad ()
{
SignedXml sign = SignHMAC (more256, new HMACSHA256 (hmackey), false);
Assert.AreEqual (more256, sign.SignatureMethod, "SignatureMethod");
}
[Test]
public void VerifyHMAC_SHA256 ()
{
string xml = "<?xml version=\"1.0\" encoding=\"Windows-1252\"?><foo><Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><SignedInfo><CanonicalizationMethod Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\" /><SignatureMethod Algorithm=\"http://www.w3.org/2001/04/xmldsig-more#hmac-sha256\" /><Reference URI=\"\"><Transforms><Transform Algorithm=\"http://www.w3.org/2000/09/xmldsig#enveloped-signature\" /></Transforms><DigestMethod Algorithm=\"http://www.w3.org/2001/04/xmlenc#sha256\" /><DigestValue>sKG2hDPEHiPrzpd3QA8BZ0eMzMbSEPPMh9QqXgkP7Cs=</DigestValue></Reference></SignedInfo><SignatureValue>Faad3KInJdIpaGcBn5e04Zv080u45fSjAKqrgevdWQw=</SignatureValue></Signature></foo>";
XmlDocument doc = new XmlDocument ();
doc.LoadXml (xml);
SignedXml sign = new SignedXml (doc);
sign.LoadXml (doc.DocumentElement ["Signature"]);
// verify MS-generated signature
Assert.IsTrue (sign.CheckSignature (new HMACSHA256 (hmackey)));
}
[Test]
public void SignHMAC_SHA512 ()
{
SignedXml sign = SignHMAC (EncryptedXml.XmlEncSHA512Url, new HMACSHA512 (hmackey), true);
Assert.AreEqual (more512, sign.SignatureMethod, "SignatureMethod");
}
[Test]
public void SignHMAC_SHA512_Bad ()
{
SignedXml sign = SignHMAC (more512, new HMACSHA512 (hmackey), false);
Assert.AreEqual (more512, sign.SignatureMethod, "SignatureMethod");
}
[Test]
public void VerifyHMAC_SHA512 ()
{
string xml = "<?xml version=\"1.0\" encoding=\"Windows-1252\"?><foo><Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><SignedInfo><CanonicalizationMethod Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\" /><SignatureMethod Algorithm=\"http://www.w3.org/2001/04/xmldsig-more#hmac-sha512\" /><Reference URI=\"\"><Transforms><Transform Algorithm=\"http://www.w3.org/2000/09/xmldsig#enveloped-signature\" /></Transforms><DigestMethod Algorithm=\"http://www.w3.org/2001/04/xmlenc#sha512\" /><DigestValue>2dvMkpTUE8Z76ResJG9pwPpVJNYo7t6s2L02V81xUVJ0oF8yJ7v8CTojjuL76s0iVdBxAOhP80Ambd1YaSkwSw==</DigestValue></Reference></SignedInfo><SignatureValue>wFenihRlm1x5/n0cifdX/TDOYqlbg2oVIbD/gyrAc0Q2hiIUTtwfBIMY5rQhKcErSz6YNoIl8RBQBmww/0rv5g==</SignatureValue></Signature></foo>";
XmlDocument doc = new XmlDocument ();
doc.LoadXml (xml);
SignedXml sign = new SignedXml (doc);
sign.LoadXml (doc.DocumentElement ["Signature"]);
// verify MS-generated signature
Assert.IsTrue (sign.CheckSignature (new HMACSHA512 (hmackey)));
}
[Test]
public void SignHMAC_SHA384 ()
{
// works as long as the string can be used by CryptoConfig to create
// an instance of the required hash algorithm
SignedXml sign = SignHMAC ("SHA384", new HMACSHA384 (hmackey), true);
Assert.AreEqual (more384, sign.SignatureMethod, "SignatureMethod");
}
[Test]
public void SignHMAC_SHA384_Bad ()
{
// we can't verity the signature if the URI is used
SignedXml sign = SignHMAC (more384, new HMACSHA384 (hmackey), false);
Assert.AreEqual (more384, sign.SignatureMethod, "SignatureMethod");
}
[Test]
public void VerifyHMAC_SHA384 ()
{
string xml = "<?xml version=\"1.0\" encoding=\"Windows-1252\"?><foo><Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><SignedInfo><CanonicalizationMethod Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\" /><SignatureMethod Algorithm=\"http://www.w3.org/2001/04/xmldsig-more#hmac-sha384\" /><Reference URI=\"\"><Transforms><Transform Algorithm=\"http://www.w3.org/2000/09/xmldsig#enveloped-signature\" /></Transforms><DigestMethod Algorithm=\"SHA384\" /><DigestValue>kH9C0LeZocNVXhjfzpz00M5fc3WJf0QU8gxK4I7pAN7HN602yHo8yYDSlG14b5YS</DigestValue></Reference></SignedInfo><SignatureValue>LgydOfhv8nqpFLFPC+xg3ZnjC8D+V3mpzxv6GOdH1HDdw1r+LH/BFM2U7dntxgf0</SignatureValue></Signature></foo>";
XmlDocument doc = new XmlDocument ();
doc.LoadXml (xml);
SignedXml sign = new SignedXml (doc);
sign.LoadXml (doc.DocumentElement ["Signature"]);
// verify MS-generated signature
Assert.IsTrue (sign.CheckSignature (new HMACSHA384 (hmackey)));
}
[Test]
public void SignHMAC_RIPEMD160 ()
{
// works as long as the string can be used by CryptoConfig to create
// an instance of the required hash algorithm
SignedXml sign = SignHMAC ("RIPEMD160", new HMACRIPEMD160 (hmackey), true);
Assert.AreEqual (moreripe, sign.SignatureMethod, "SignatureMethod");
}
[Test]
public void SignHMAC_RIPEMD160_Bad ()
{
// we can't verity the signature if the URI is used
SignedXml sign = SignHMAC (moreripe, new HMACRIPEMD160 (hmackey), false);
Assert.AreEqual (moreripe, sign.SignatureMethod, "SignatureMethod");
}
[Test]
public void VerifyHMAC_RIPEMD160 ()
{
string xml = "<?xml version=\"1.0\" encoding=\"Windows-1252\"?><foo><Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><SignedInfo><CanonicalizationMethod Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\" /><SignatureMethod Algorithm=\"http://www.w3.org/2001/04/xmldsig-more#hmac-ripemd160\" /><Reference URI=\"\"><Transforms><Transform Algorithm=\"http://www.w3.org/2000/09/xmldsig#enveloped-signature\" /></Transforms><DigestMethod Algorithm=\"RIPEMD160\" /><DigestValue>+uyqtoSDPvgrsXCl1KCimhFpOVw=</DigestValue></Reference></SignedInfo><SignatureValue>8E2m/A5lyU6mBug7uskBqpDHuvs=</SignatureValue></Signature></foo>";
XmlDocument doc = new XmlDocument ();
doc.LoadXml (xml);
SignedXml sign = new SignedXml (doc);
sign.LoadXml (doc.DocumentElement ["Signature"]);
// verify MS-generated signature
Assert.IsTrue (sign.CheckSignature (new HMACRIPEMD160 (hmackey)));
}
#endif
// CVE-2009-0217
// * a 0-length signature is the worse case - it accepts anything
// * between 1-7 bits length are considered invalid (not a multiple of 8)
// * a 8 bits signature would have one chance, out of 256, to be valid
// * and so on... until we hit (output-length / 2) or 80 bits (see ERRATUM)
static bool erratum = true; // xmldsig erratum for CVE-2009-0217
static SignedXml GetSignedXml (string xml)
{
XmlDocument doc = new XmlDocument ();
doc.LoadXml (xml);
SignedXml sign = new SignedXml (doc);
sign.LoadXml (doc.DocumentElement);
return sign;
}
static void CheckErratum (SignedXml signed, KeyedHashAlgorithm hmac, string message)
{
if (erratum) {
try {
signed.CheckSignature (hmac);
Assert.Fail (message + ": unexcepted success");
}
catch (CryptographicException) {
}
catch (Exception e) {
Assert.Fail (message + ": unexcepted " + e.ToString ());
}
} else {
Assert.IsTrue (signed.CheckSignature (hmac), message);
}
}
private void HmacMustBeMultipleOfEightBits (int bits)
{
string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<Signature xmlns=""http://www.w3.org/2000/09/xmldsig#"">
<SignedInfo>
<CanonicalizationMethod Algorithm=""http://www.w3.org/TR/2001/REC-xml-c14n-20010315"" />
<SignatureMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#hmac-sha1"" >
<HMACOutputLength>{0}</HMACOutputLength>
</SignatureMethod>
<Reference URI=""#object"">
<DigestMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#sha1"" />
<DigestValue>nz4GS0NbH2SrWlD/4fX313CoTzc=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>
gA==
</SignatureValue>
<Object Id=""object"">some other text</Object>
</Signature>
";
SignedXml sign = GetSignedXml (String.Format (xml, bits));
// only multiple of 8 bits are supported
sign.CheckSignature (new HMACSHA1 (Encoding.ASCII.GetBytes ("secret")));
}
[Test]
public void HmacMustBeMultipleOfEightBits ()
{
for (int i = 1; i < 160; i++) {
// The .NET framework only supports multiple of 8 bits
if (i % 8 == 0)
continue;
try {
HmacMustBeMultipleOfEightBits (i);
Assert.Fail ("Unexpected Success " + i.ToString ());
}
catch (CryptographicException) {
}
catch (Exception e) {
Assert.Fail ("Unexpected Exception " + i.ToString () + " : " + e.ToString ());
}
}
}
[Test]
[Category ("NotDotNet")] // will fail until a fix is available
public void VerifyHMAC_ZeroLength ()
{
string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<Signature xmlns=""http://www.w3.org/2000/09/xmldsig#"">
<SignedInfo>
<CanonicalizationMethod Algorithm=""http://www.w3.org/TR/2001/REC-xml-c14n-20010315"" />
<SignatureMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#hmac-sha1"" >
<HMACOutputLength>0</HMACOutputLength>
</SignatureMethod>
<Reference URI=""#object"">
<DigestMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#sha1"" />
<DigestValue>nz4GS0NbH2SrWlD/4fX313CoTzc=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>
</SignatureValue>
<Object Id=""object"">some other text</Object>
</Signature>
";
SignedXml sign = GetSignedXml (xml);
CheckErratum (sign, new HMACSHA1 (Encoding.ASCII.GetBytes ("no clue")), "1");
CheckErratum (sign, new HMACSHA1 (Encoding.ASCII.GetBytes ("")), "2");
CheckErratum (sign, new HMACSHA1 (Encoding.ASCII.GetBytes ("oops")), "3");
CheckErratum (sign, new HMACSHA1 (Encoding.ASCII.GetBytes ("secret")), "4");
}
[Test]
[Category ("NotDotNet")] // will fail until a fix is available
public void VerifyHMAC_SmallerThanMinimumLength ()
{
// 72 is a multiple of 8 but smaller than the minimum of 80 bits
string xml = @"<Signature xmlns=""http://www.w3.org/2000/09/xmldsig#""><SignedInfo><CanonicalizationMethod Algorithm=""http://www.w3.org/TR/2001/REC-xml-c14n-20010315"" /><SignatureMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#hmac-sha1""><HMACOutputLength>72</HMACOutputLength></SignatureMethod><Reference URI=""#object""><DigestMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#sha1"" /><DigestValue>nz4GS0NbH2SrWlD/4fX313CoTzc=</DigestValue></Reference></SignedInfo><SignatureValue>2dimB+P5Aw5K</SignatureValue><Object Id=""object"">some other text</Object></Signature>";
SignedXml sign = GetSignedXml (xml);
CheckErratum (sign, new HMACSHA1 (Encoding.ASCII.GetBytes ("secret")), "72");
}
[Test]
public void VerifyHMAC_MinimumLength ()
{
// 80 bits is the minimum (and the half-size of HMACSHA1)
string xml = @"<Signature xmlns=""http://www.w3.org/2000/09/xmldsig#""><SignedInfo><CanonicalizationMethod Algorithm=""http://www.w3.org/TR/2001/REC-xml-c14n-20010315"" /><SignatureMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#hmac-sha1""><HMACOutputLength>80</HMACOutputLength></SignatureMethod><Reference URI=""#object""><DigestMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#sha1"" /><DigestValue>nz4GS0NbH2SrWlD/4fX313CoTzc=</DigestValue></Reference></SignedInfo><SignatureValue>jVQPtLj61zNYjw==</SignatureValue><Object Id=""object"">some other text</Object></Signature>";
SignedXml sign = GetSignedXml (xml);
Assert.IsTrue (sign.CheckSignature (new HMACSHA1 (Encoding.ASCII.GetBytes ("secret"))));
}
#if NET_2_0
[Test]
[Category ("NotDotNet")] // will fail until a fix is available
public void VerifyHMAC_SmallerHalfLength ()
{
// 80bits is smaller than the half-size of HMACSHA256
string xml = @"<Signature xmlns=""http://www.w3.org/2000/09/xmldsig#""><SignedInfo><CanonicalizationMethod Algorithm=""http://www.w3.org/TR/2001/REC-xml-c14n-20010315"" /><SignatureMethod Algorithm=""http://www.w3.org/2001/04/xmldsig-more#hmac-sha256""><HMACOutputLength>80</HMACOutputLength></SignatureMethod><Reference URI=""#object""><DigestMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#sha1"" /><DigestValue>nz4GS0NbH2SrWlD/4fX313CoTzc=</DigestValue></Reference></SignedInfo><SignatureValue>vPtw7zKVV/JwQg==</SignatureValue><Object Id=""object"">some other text</Object></Signature>";
SignedXml sign = GetSignedXml (xml);
CheckErratum (sign, new HMACSHA256 (Encoding.ASCII.GetBytes ("secret")), "80");
}
[Test]
public void VerifyHMAC_HalfLength ()
{
// 128 is the half-size of HMACSHA256
string xml = @"<Signature xmlns=""http://www.w3.org/2000/09/xmldsig#""><SignedInfo><CanonicalizationMethod Algorithm=""http://www.w3.org/TR/2001/REC-xml-c14n-20010315"" /><SignatureMethod Algorithm=""http://www.w3.org/2001/04/xmldsig-more#hmac-sha256""><HMACOutputLength>128</HMACOutputLength></SignatureMethod><Reference URI=""#object""><DigestMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#sha1"" /><DigestValue>nz4GS0NbH2SrWlD/4fX313CoTzc=</DigestValue></Reference></SignedInfo><SignatureValue>aegpvkAwOL8gN/CjSnW6qw==</SignatureValue><Object Id=""object"">some other text</Object></Signature>";
SignedXml sign = GetSignedXml (xml);
Assert.IsTrue (sign.CheckSignature (new HMACSHA256 (Encoding.ASCII.GetBytes ("secret"))));
}
#endif
[Test]
public void VerifyHMAC_FullLength ()
{
string xml = @"<Signature xmlns=""http://www.w3.org/2000/09/xmldsig#""><SignedInfo><CanonicalizationMethod Algorithm=""http://www.w3.org/TR/2001/REC-xml-c14n-20010315"" /><SignatureMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#hmac-sha1"" /><Reference URI=""#object""><DigestMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#sha1"" /><DigestValue>7/XTsHaBSOnJ/jXD5v0zL6VKYsk=</DigestValue></Reference></SignedInfo><SignatureValue>a0goL9esBUKPqtFYgpp2KST4huk=</SignatureValue><Object Id=""object"">some text</Object></Signature>";
SignedXml sign = GetSignedXml (xml);
Assert.IsTrue (sign.CheckSignature (new HMACSHA1 (Encoding.ASCII.GetBytes ("secret"))));
}
[Test]
[ExpectedException (typeof (CryptographicException))]
public void VerifyHMAC_HMACOutputLength_Signature_Mismatch ()
{
string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<Signature xmlns=""http://www.w3.org/2000/09/xmldsig#"">
<SignedInfo>
<CanonicalizationMethod Algorithm=""http://www.w3.org/TR/2001/REC-xml-c14n-20010315"" />
<SignatureMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#hmac-sha1"" >
<HMACOutputLength>80</HMACOutputLength>
</SignatureMethod>
<Reference URI=""#object"">
<DigestMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#sha1"" />
<DigestValue>nz4GS0NbH2SrWlD/4fX313CoTzc=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>
</SignatureValue>
<Object Id=""object"">some other text</Object>
</Signature>
";
SignedXml sign = GetSignedXml (xml);
sign.CheckSignature (new HMACSHA1 (Encoding.ASCII.GetBytes ("no clue")));
}
[Test]
[ExpectedException (typeof (FormatException))]
public void VerifyHMAC_HMACOutputLength_Invalid ()
{
string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<Signature xmlns=""http://www.w3.org/2000/09/xmldsig#"">
<SignedInfo>
<CanonicalizationMethod Algorithm=""http://www.w3.org/TR/2001/REC-xml-c14n-20010315"" />
<SignatureMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#hmac-sha1"" >
<HMACOutputLength>I wish this was not a string property</HMACOutputLength>
</SignatureMethod>
<Reference URI=""#object"">
<DigestMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#sha1"" />
<DigestValue>nz4GS0NbH2SrWlD/4fX313CoTzc=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>
</SignatureValue>
<Object Id=""object"">some other text</Object>
</Signature>
";
SignedXml sign = GetSignedXml (xml);
sign.CheckSignature (new HMACSHA1 (Encoding.ASCII.GetBytes ("no clue")));
}
}
}
|