1 // ==++==
2 //
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 //
5 // ==--==
6 // <OWNER>Microsoft</OWNER>
7 //
8 
9 //
10 // AsymmetricAlgorithm.cs
11 //
12 
13 namespace System.Security.Cryptography {
14     [System.Runtime.InteropServices.ComVisible(true)]
15     public abstract class AsymmetricAlgorithm : IDisposable {
16         protected int KeySizeValue;
17         protected KeySizes[] LegalKeySizesValue;
18 
19         //
20         // public constructors
21         //
22 
AsymmetricAlgorithm()23         protected AsymmetricAlgorithm() {}
24 
25         // AsymmetricAlgorithm implements IDisposable
26 
Dispose()27         public void Dispose() {
28             Clear();
29         }
30 
Clear()31         public void Clear() {
32             Dispose(true);
33             GC.SuppressFinalize(this);
34         }
35 
Dispose(bool disposing)36         protected virtual void Dispose(bool disposing)
37         {
38             return;
39         }
40 
41         //
42         // public properties
43         //
44 
45         public virtual int KeySize {
46             get { return KeySizeValue; }
47             set {
48                 int   i;
49                 int   j;
50 
51                 for (i=0; i<LegalKeySizesValue.Length; i++) {
52                     if (LegalKeySizesValue[i].SkipSize == 0) {
53                         if (LegalKeySizesValue[i].MinSize == value) { // assume MinSize = MaxSize
54                             KeySizeValue = value;
55                             return;
56                         }
57                     } else {
58                         for (j = LegalKeySizesValue[i].MinSize; j<=LegalKeySizesValue[i].MaxSize;
59                              j += LegalKeySizesValue[i].SkipSize) {
60                             if (j == value) {
61                                 KeySizeValue = value;
62                                 return;
63                             }
64                         }
65                     }
66                 }
67                 throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidKeySize"));
68             }
69         }
70 
71         public virtual KeySizes[] LegalKeySizes {
72             get { return (KeySizes[]) LegalKeySizesValue.Clone(); }
73         }
74 
75         // This method must be implemented by derived classes. In order to conform to the contract, it cannot be abstract.
76         public virtual String SignatureAlgorithm {
77             get {
78                 throw new NotImplementedException();
79             }
80         }
81 
82         // This method must be implemented by derived classes. In order to conform to the contract, it cannot be abstract.
83         public virtual String KeyExchangeAlgorithm {
84             get {
85                 throw new NotImplementedException();
86             }
87         }
88 
89         //
90         // public methods
91         //
92 
Create()93         static public AsymmetricAlgorithm Create() {
94 #if FULL_AOT_RUNTIME
95             return new RSACryptoServiceProvider ();
96 #else
97             // Use the crypto config system to return an instance of
98             // the default AsymmetricAlgorithm on this machine
99             return Create("System.Security.Cryptography.AsymmetricAlgorithm");
100 #endif
101         }
102 
Create(String algName)103         static public AsymmetricAlgorithm Create(String algName) {
104             return (AsymmetricAlgorithm) CryptoConfig.CreateFromName(algName);
105         }
106 
107         // This method must be implemented by derived classes. In order to conform to the contract, it cannot be abstract.
FromXmlString(String xmlString)108         public virtual void FromXmlString(String xmlString) {
109             throw new NotImplementedException();
110         }
111 
112         // This method must be implemented by derived classes. In order to conform to the contract, it cannot be abstract.
ToXmlString(bool includePrivateParameters)113         public virtual String ToXmlString(bool includePrivateParameters) {
114             throw new NotImplementedException();
115         }
116     }
117 }
118