| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- // Transport Security Layer (TLS)
- // Copyright (c) 2003-2004 Carlos Guzman Alvarez
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using System;
- using System.Collections;
- using System.Globalization;
- using System.Security.Cryptography;
- namespace Mono.Security.Protocol.Tls
- {
- internal sealed class CipherSuiteCollection : ICollection, IList, IEnumerable
- {
- #region Fields
- private ArrayList cipherSuites;
- private SecurityProtocolType protocol;
- #endregion
- #region Indexers
- public CipherSuite this[string name]
- {
- get { return (CipherSuite)this.cipherSuites[this.IndexOf(name)]; }
- set { this.cipherSuites[this.IndexOf(name)] = (CipherSuite)value; }
- }
- public CipherSuite this[int index]
- {
- get { return (CipherSuite)this.cipherSuites[index]; }
- set { this.cipherSuites[index] = (CipherSuite)value; }
- }
- public CipherSuite this[short code]
- {
- get { return (CipherSuite)this.cipherSuites[this.IndexOf(code)]; }
- set { this.cipherSuites[this.IndexOf(code)] = (CipherSuite)value; }
- }
- object IList.this[int index]
- {
- get { return this[index]; }
- set { this[index] = (CipherSuite)value; }
- }
- #endregion
- #region ICollection Properties
- bool ICollection.IsSynchronized
- {
- get { return this.cipherSuites.IsSynchronized; }
- }
- object ICollection.SyncRoot
- {
- get { return this.cipherSuites.SyncRoot; }
- }
- public int Count
- {
- get { return this.cipherSuites.Count; }
- }
-
- #endregion
- #region IList Properties
- public bool IsFixedSize
- {
- get { return this.cipherSuites.IsFixedSize; }
- }
- public bool IsReadOnly
- {
- get { return this.cipherSuites.IsReadOnly; }
- }
- #endregion
- #region Constructors
- public CipherSuiteCollection(SecurityProtocolType protocol) : base()
- {
- this.protocol = protocol;
- this.cipherSuites = new ArrayList();
- }
- #endregion
- #region ICollection Methods
- public void CopyTo(Array array, int index)
- {
- this.cipherSuites.CopyTo(array, index);
- }
-
- #endregion
- #region IEnumerable Methods
- IEnumerator IEnumerable.GetEnumerator()
- {
- return this.cipherSuites.GetEnumerator();
- }
- #endregion
- #region IList Methods
- public void Clear()
- {
- this.cipherSuites.Clear();
- }
-
- bool IList.Contains(object value)
- {
- return this.cipherSuites.Contains(value as CipherSuite);
- }
- public int IndexOf(string name)
- {
- int index = 0;
- foreach (CipherSuite cipherSuite in this.cipherSuites)
- {
- if (this.cultureAwareCompare(cipherSuite.Name, name))
- {
- return index;
- }
- index++;
- }
- return -1;
- }
- public int IndexOf(short code)
- {
- int index = 0;
- foreach (CipherSuite cipherSuite in this.cipherSuites)
- {
- if (cipherSuite.Code == code)
- {
- return index;
- }
- index++;
- }
- return -1;
- }
- int IList.IndexOf(object value)
- {
- return this.cipherSuites.IndexOf(value as CipherSuite);
- }
- void IList.Insert(int index, object value)
- {
- this.cipherSuites.Insert(index, value as CipherSuite);
- }
- void IList.Remove(object value)
- {
- this.cipherSuites.Remove(value as CipherSuite);
- }
- void IList.RemoveAt(int index)
- {
- this.cipherSuites.RemoveAt(index);
- }
- public CipherSuite Add(
- short code, string name, CipherAlgorithmType cipherType,
- HashAlgorithmType hashType, ExchangeAlgorithmType exchangeType,
- bool exportable, bool blockMode, byte keyMaterialSize,
- byte expandedKeyMaterialSize, short effectiveKeyBytes,
- byte ivSize, byte blockSize)
- {
- switch (this.protocol)
- {
- case SecurityProtocolType.Default:
- case SecurityProtocolType.Tls:
- return this.add(
- new TlsCipherSuite(
- code, name, cipherType, hashType, exchangeType, exportable,
- blockMode, keyMaterialSize, expandedKeyMaterialSize,
- effectiveKeyBytes, ivSize, blockSize));
- case SecurityProtocolType.Ssl3:
- return this.add(
- new SslCipherSuite(
- code, name, cipherType, hashType, exchangeType, exportable,
- blockMode, keyMaterialSize, expandedKeyMaterialSize,
- effectiveKeyBytes, ivSize, blockSize));
- case SecurityProtocolType.Ssl2:
- default:
- throw new NotSupportedException("Unsupported security protocol type.");
- }
- }
- private TlsCipherSuite add(TlsCipherSuite cipherSuite)
- {
- this.cipherSuites.Add(cipherSuite);
- return cipherSuite;
- }
- private SslCipherSuite add(SslCipherSuite cipherSuite)
- {
- this.cipherSuites.Add(cipherSuite);
- return cipherSuite;
- }
- int IList.Add(object value)
- {
- return this.cipherSuites.Add(value as CipherSuite);
- }
-
- private bool cultureAwareCompare(string strA, string strB)
- {
- return CultureInfo.CurrentCulture.CompareInfo.Compare(
- strA,
- strB,
- CompareOptions.IgnoreKanaType | CompareOptions.IgnoreWidth |
- CompareOptions.IgnoreCase) == 0 ? true : false;
- }
- #endregion
- }
- }
|