DerivedKeySecurityToken.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.IdentityModel.Selectors;
  4. using System.IdentityModel.Tokens;
  5. using System.Security.Cryptography.Xml;
  6. using System.ServiceModel;
  7. using System.ServiceModel.Security;
  8. using System.Text;
  9. namespace System.ServiceModel.Security.Tokens
  10. {
  11. internal class DerivedKeySecurityToken : SecurityToken
  12. {
  13. string algorithm;
  14. SecurityKeyIdentifierClause reference;
  15. SecurityToken resolved_token; // store resolved one.
  16. int? generation, offset, length;
  17. // properties
  18. string id, name, label;
  19. byte [] nonce;
  20. ReadOnlyCollection<SecurityKey> keys;
  21. ReferenceList reflist;
  22. public DerivedKeySecurityToken (string id, string algorithm,
  23. SecurityKeyIdentifierClause reference,
  24. SymmetricSecurityKey referencedKey,
  25. string name,
  26. int? generation,
  27. int? offset,
  28. int? length,
  29. string label,
  30. byte [] nonce)
  31. {
  32. algorithm = algorithm ?? SecurityAlgorithms.Psha1KeyDerivation;
  33. this.id = id;
  34. this.algorithm = algorithm;
  35. this.reference = reference;
  36. this.generation = generation;
  37. this.offset = offset;
  38. this.length = length;
  39. this.nonce = nonce;
  40. this.name = name;
  41. this.label = label;
  42. SecurityKey key = new InMemorySymmetricSecurityKey (
  43. referencedKey.GenerateDerivedKey (
  44. algorithm,
  45. Encoding.UTF8.GetBytes (label ?? Constants.WsscDefaultLabel),
  46. nonce,
  47. (length ?? 32) * 8,
  48. offset ?? 0));
  49. keys = new ReadOnlyCollection<SecurityKey> (
  50. new SecurityKey [] {key});
  51. }
  52. public override string Id {
  53. get { return id; }
  54. }
  55. public override ReadOnlyCollection<SecurityKey> SecurityKeys {
  56. get { return keys; }
  57. }
  58. public override DateTime ValidFrom {
  59. get { return resolved_token.ValidFrom; }
  60. }
  61. public override DateTime ValidTo {
  62. get { return resolved_token.ValidTo; }
  63. }
  64. internal ReferenceList ReferenceList {
  65. get { return reflist; }
  66. set { reflist = value; }
  67. }
  68. public SecurityKeyIdentifierClause TokenReference {
  69. get { return reference; }
  70. }
  71. public int? Generation {
  72. get { return generation; }
  73. }
  74. public int? Length {
  75. get { return length; }
  76. }
  77. public int? Offset {
  78. get { return offset; }
  79. }
  80. public string Label {
  81. get { return label; }
  82. }
  83. public byte [] Nonce {
  84. get { return nonce; }
  85. }
  86. public string Name {
  87. get { return name; }
  88. }
  89. public override bool MatchesKeyIdentifierClause (
  90. SecurityKeyIdentifierClause keyIdentifierClause)
  91. {
  92. LocalIdKeyIdentifierClause l = keyIdentifierClause
  93. as LocalIdKeyIdentifierClause;
  94. return l != null && l.LocalId == Id;
  95. }
  96. public override SecurityKey ResolveKeyIdentifierClause (
  97. SecurityKeyIdentifierClause keyIdentifierClause)
  98. {
  99. return MatchesKeyIdentifierClause (keyIdentifierClause) ?
  100. keys [0] : null;
  101. }
  102. }
  103. }