2010-06-03 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.ServiceModel / System.ServiceModel.Security.Tokens / DerivedKeySecurityToken.cs
blobc3a77d790109d9a2ca5aa11359b6fbedcd62167b
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;
10 namespace System.ServiceModel.Security.Tokens
12 internal class DerivedKeySecurityToken : SecurityToken
14 string algorithm;
15 SecurityKeyIdentifierClause reference;
16 SecurityToken resolved_token; // store resolved one.
17 int? generation, offset, length;
18 // properties
19 string id, name, label;
20 byte [] nonce;
21 ReadOnlyCollection<SecurityKey> keys;
22 ReferenceList reflist;
24 public DerivedKeySecurityToken (string id, string algorithm,
25 SecurityKeyIdentifierClause reference,
26 SymmetricSecurityKey referencedKey,
27 string name,
28 int? generation,
29 int? offset,
30 int? length,
31 string label,
32 byte [] nonce)
34 algorithm = algorithm ?? SecurityAlgorithms.Psha1KeyDerivation;
36 this.id = id;
37 this.algorithm = algorithm;
38 this.reference = reference;
39 this.generation = generation;
40 this.offset = offset;
41 this.length = length;
42 this.nonce = nonce;
43 this.name = name;
44 this.label = label;
46 SecurityKey key = new InMemorySymmetricSecurityKey (
47 referencedKey.GenerateDerivedKey (
48 algorithm,
49 Encoding.UTF8.GetBytes (label ?? Constants.WsscDefaultLabel),
50 nonce,
51 (length ?? 32) * 8,
52 offset ?? 0));
53 keys = new ReadOnlyCollection<SecurityKey> (
54 new SecurityKey [] {key});
57 public override string Id {
58 get { return id; }
61 public override ReadOnlyCollection<SecurityKey> SecurityKeys {
62 get { return keys; }
65 public override DateTime ValidFrom {
66 get { return resolved_token.ValidFrom; }
69 public override DateTime ValidTo {
70 get { return resolved_token.ValidTo; }
73 internal ReferenceList ReferenceList {
74 get { return reflist; }
75 set { reflist = value; }
78 public SecurityKeyIdentifierClause TokenReference {
79 get { return reference; }
82 public int? Generation {
83 get { return generation; }
86 public int? Length {
87 get { return length; }
90 public int? Offset {
91 get { return offset; }
94 public string Label {
95 get { return label; }
98 public byte [] Nonce {
99 get { return nonce; }
102 public string Name {
103 get { return name; }
106 public override bool MatchesKeyIdentifierClause (
107 SecurityKeyIdentifierClause keyIdentifierClause)
109 LocalIdKeyIdentifierClause l = keyIdentifierClause
110 as LocalIdKeyIdentifierClause;
111 return l != null && l.LocalId == Id;
114 public override SecurityKey ResolveKeyIdentifierClause (
115 SecurityKeyIdentifierClause keyIdentifierClause)
117 return MatchesKeyIdentifierClause (keyIdentifierClause) ?
118 keys [0] : null;