[asp.net] Implemented CustomErrorsRedirectMode
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Security.Tokens / DerivedKeySecurityToken.cs
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
10 namespace System.ServiceModel.Security.Tokens
11 {
12         internal class DerivedKeySecurityToken : SecurityToken
13         {
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;
23
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)
33                 {
34                         algorithm = algorithm ?? SecurityAlgorithms.Psha1KeyDerivation;
35
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;
45
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});
55                 }
56
57                 public override string Id {
58                         get { return id; }
59                 }
60
61                 public override ReadOnlyCollection<SecurityKey> SecurityKeys {
62                         get { return keys; }
63                 }
64
65                 public override DateTime ValidFrom {
66                         get { return resolved_token.ValidFrom; }
67                 }
68
69                 public override DateTime ValidTo {
70                         get { return resolved_token.ValidTo; }
71                 }
72
73                 internal ReferenceList ReferenceList {
74                         get { return reflist; }
75                         set { reflist = value; }
76                 }
77
78                 public SecurityKeyIdentifierClause TokenReference {
79                         get { return reference; }
80                 }
81
82                 public int? Generation {
83                         get { return generation; }
84                 }
85
86                 public int? Length {
87                         get { return length; }
88                 }
89
90                 public int? Offset {
91                         get { return offset; }
92                 }
93
94                 public string Label {
95                         get { return label; }
96                 }
97
98                 public byte [] Nonce {
99                         get { return nonce; }
100                 }
101
102                 public string Name {
103                         get { return name; }
104                 }
105
106                 public override bool MatchesKeyIdentifierClause (
107                         SecurityKeyIdentifierClause keyIdentifierClause)
108                 {
109                         LocalIdKeyIdentifierClause l = keyIdentifierClause
110                                 as LocalIdKeyIdentifierClause;
111                         return l != null && l.LocalId == Id;
112                 }
113
114                 public override SecurityKey ResolveKeyIdentifierClause (
115                         SecurityKeyIdentifierClause keyIdentifierClause)
116                 {
117                         return MatchesKeyIdentifierClause (keyIdentifierClause) ?
118                                 keys [0] : null;
119                 }
120         }
121 }