File: SecurityKeyElement.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (233 lines) | stat: -rw-r--r-- 9,570 bytes parent folder | download | duplicates (7)
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
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//------------------------------------------------------------

using System;
using System.IdentityModel.Selectors;

namespace System.IdentityModel.Tokens
{

    /// <summary>
    /// SecurityKeyElement provides delayed resolution of security keys by resolving the SecurityKeyIdentifierClause or SecurityKeyIdentifier 
    /// only when cryptographic functions are needed.  This allows a key clause or identifier that is never used by an application
    /// to be serialized and deserialzied on and off the wire without issue.
    /// </summary>
    public class SecurityKeyElement : SecurityKey
    {
        SecurityKey _securityKey;
        object _keyLock;
        SecurityTokenResolver _securityTokenResolver;
        SecurityKeyIdentifier _securityKeyIdentifier;

        /// <summary>
        /// Constructor to use when working with SecurityKeyIdentifierClauses
        /// </summary>
        /// <param name="securityKeyIdentifierClause">SecurityKeyIdentifierClause that represents a SecuriytKey</param>
        /// <param name="securityTokenResolver">SecurityTokenResolver that can be resolved to a SecurityKey</param>
        /// <exception cref="ArgumentNullException">Thrown if the 'clause' is null</exception>
        public SecurityKeyElement(SecurityKeyIdentifierClause securityKeyIdentifierClause, SecurityTokenResolver securityTokenResolver)
        {
            if (securityKeyIdentifierClause == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("securityKeyIdentifierClause");
            }

            Initialize(new SecurityKeyIdentifier(securityKeyIdentifierClause), securityTokenResolver);
        }

        /// <summary>
        /// Constructor to use when working with SecurityKeyIdentifiers
        /// </summary>
        /// <param name="securityKeyIdentifier">SecurityKeyIdentifier that represents a SecuriytKey</param>
        /// <param name="securityTokenResolver">SecurityTokenResolver that can be resolved to a SecurityKey</param>
        /// <exception cref="ArgumentNullException">Thrown if the 'securityKeyIdentifier' is null</exception>
        public SecurityKeyElement(SecurityKeyIdentifier securityKeyIdentifier, SecurityTokenResolver securityTokenResolver)
        {
            if (securityKeyIdentifier == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("securityKeyIdentifier");
            }

            Initialize(securityKeyIdentifier, securityTokenResolver);
        }

        void Initialize(SecurityKeyIdentifier securityKeyIdentifier, SecurityTokenResolver securityTokenResolver)
        {
            _keyLock = new object();
            _securityKeyIdentifier = securityKeyIdentifier;
            _securityTokenResolver = securityTokenResolver;
        }

        /// <summary>
        /// Decrypts a key using the specified algorithm.
        /// </summary>
        /// <param name="algorithm">Algorithm to use when decrypting the key.</param>
        /// <param name="keyData">Bytes representing the encrypted key.</param>
        /// <returns>Decrypted bytes.</returns>
        public override byte[] DecryptKey(string algorithm, byte[] keyData)
        {
            if (_securityKey == null)
            {
                ResolveKey();
            }

            return _securityKey.DecryptKey(algorithm, keyData);
        }

        /// <summary>
        /// Encrypts a key using the specified algorithm.
        /// </summary>
        /// <param name="algorithm">Algorithm to use when encrypting the key.</param>
        /// <param name="keyData">Bytes representing the key.</param>
        /// <returns>Encrypted bytes.</returns>
        public override byte[] EncryptKey(string algorithm, byte[] keyData)
        {
            if (_securityKey == null)
            {
                ResolveKey();
            }

            return _securityKey.EncryptKey(algorithm, keyData);
        }

        /// <summary>
        /// Answers question: is the algorithm Asymmetric.
        /// </summary>
        /// <param name="algorithm">Algorithm to check.</param>
        /// <returns>True if algorithm will be processed by runtime as Asymmetric.</returns>
        public override bool IsAsymmetricAlgorithm(string algorithm)
        {
            // Copied from System.IdentityModel.CryptoHelper
            // no need to ResolveKey

            switch (algorithm)
            {
                case SecurityAlgorithms.DsaSha1Signature:
                case SecurityAlgorithms.RsaSha1Signature:
                case SecurityAlgorithms.RsaSha256Signature:
                case SecurityAlgorithms.RsaOaepKeyWrap:
                case SecurityAlgorithms.RsaV15KeyWrap:
                    return true;
                default:
                    return false;
            }
        }

        /// <summary>
        /// Answers question: is the algorithm is supported by this key.
        /// </summary>
        /// <param name="algorithm">Algorithm to check.</param>
        /// <returns>True if algorithm is supported by this key.</returns>
        public override bool IsSupportedAlgorithm(string algorithm)
        {
            if (_securityKey == null)
            {
                ResolveKey();
            }

            return _securityKey.IsSupportedAlgorithm(algorithm);
        }

        /// <summary>
        /// Answers question: is the algorithm Symmetric.
        /// </summary>
        /// <param name="algorithm">Algorithm to check.</param>
        /// <returns>True if algorithm will be processed by runtime as Symmetric.</returns>
        public override bool IsSymmetricAlgorithm(string algorithm)
        {
            // Copied from System.IdentityModel.CryptoHelper
            // no need to ResolveKey.

            switch (algorithm)
            {
                case SecurityAlgorithms.DsaSha1Signature:
                case SecurityAlgorithms.RsaSha1Signature:
                case SecurityAlgorithms.RsaSha256Signature:
                case SecurityAlgorithms.RsaOaepKeyWrap:
                case SecurityAlgorithms.RsaV15KeyWrap:
                    return false;
                case SecurityAlgorithms.HmacSha1Signature:
                case SecurityAlgorithms.HmacSha256Signature:
                case SecurityAlgorithms.Aes128Encryption:
                case SecurityAlgorithms.Aes192Encryption:
                case SecurityAlgorithms.Aes256Encryption:
                case SecurityAlgorithms.TripleDesEncryption:
                case SecurityAlgorithms.Aes128KeyWrap:
                case SecurityAlgorithms.Aes192KeyWrap:
                case SecurityAlgorithms.Aes256KeyWrap:
                case SecurityAlgorithms.TripleDesKeyWrap:
                case SecurityAlgorithms.Psha1KeyDerivation:
                case SecurityAlgorithms.Psha1KeyDerivationDec2005:
                    return true;
                default:
                    return false;
            }
        }

        /// <summary>
        /// Gets the key size in bits.
        /// </summary>
        /// <returns>Key size in bits.</returns>
        public override int KeySize
        {
            get
            {
                if (_securityKey == null)
                {
                    ResolveKey();
                }

                return _securityKey.KeySize;
            }
        }

        /// <summary>
        /// Attempts to resolve the _securityKeyIdentifier into a securityKey.  If successful, the private _securityKey is set.
        /// Uses the tokenresolver that was passed in, it may be the case a keyIdentifier can 
        /// generate a securityKey.  A RSA key can generate a key with just the public part.
        /// </summary>
        /// <returns>void</returns>
        void ResolveKey()
        {

            if (_securityKeyIdentifier == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("ski");
            }

            if (_securityKey == null)
            {
                lock (_keyLock)
                {
                    if (_securityKey == null)
                    {

                        if (_securityTokenResolver != null)
                        {
                            for (int i = 0; i < _securityKeyIdentifier.Count; ++i)
                            {
                                if (_securityTokenResolver.TryResolveSecurityKey(_securityKeyIdentifier[i], out _securityKey))
                                {
                                    return;
                                }
                            }
                        }

                        // most likely a public key, do this last
                        if (_securityKeyIdentifier.CanCreateKey)
                        {
                            _securityKey = _securityKeyIdentifier.CreateKey();
                            return;
                        }

                        throw DiagnosticUtility.ExceptionUtility.ThrowHelper(
                            new SecurityTokenException(SR.GetString(SR.ID2080,
                                        _securityTokenResolver == null ? "null" : _securityTokenResolver.ToString(),
                                        _securityKeyIdentifier == null ? "null" : _securityKeyIdentifier.ToString())), System.Diagnostics.TraceEventType.Error);
                    }
                }
            }
        }
    }
}