Merge pull request #557 from jack-pappas/gacutil-patch
[mono.git] / mcs / class / System / System.Security.Cryptography.X509Certificates / X509ChainElement.cs
1 //
2 // X509ChainElement.cs - System.Security.Cryptography.X509Certificates.X509ChainElement
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2005-2006 Novell Inc. (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 #if SECURITY_DEP
31
32 namespace System.Security.Cryptography.X509Certificates {
33
34         public class X509ChainElement {
35
36                 private X509Certificate2 certificate;
37                 private X509ChainStatus[] status;
38                 private string info;
39                 private X509ChainStatusFlags compressed_status_flags;
40
41                 // constructors
42
43                 // only accessible from X509Chain.ChainElements
44                 internal X509ChainElement (X509Certificate2 certificate)
45                 {
46                         this.certificate = certificate;
47                         // so far String.Empty is the only thing I've seen. 
48                         // The interesting stuff is inside X509ChainStatus.Information
49                         info = String.Empty;
50                 }
51
52                 // properties
53
54                 public X509Certificate2 Certificate {
55                         get { return certificate; }
56                 }
57
58                 public X509ChainStatus[] ChainElementStatus {
59                         get { return status; }
60                 }
61
62                 public string Information {
63                         get { return info; }
64                 }
65
66                 // private stuff
67
68                 internal X509ChainStatusFlags StatusFlags {
69                         get { return compressed_status_flags; }
70                         set { compressed_status_flags = value; }
71                 }
72
73                 private int Count (X509ChainStatusFlags flags)
74                 {
75                         int size = 0;
76                         int n = 0;
77                         int f = (int) flags;
78                         int m = 0x1;
79                         while (n++ < 32) {
80                                 if ((f & m) == m)
81                                         size++;
82                                 m <<= 1;
83                         }
84                         return size;
85                 }
86
87                 private void Set (X509ChainStatus[] status, ref int position, X509ChainStatusFlags flags, X509ChainStatusFlags mask)
88                 {
89                         if ((flags & mask) != 0) {
90                                 status [position].Status = mask;
91                                 status [position].StatusInformation = X509ChainStatus.GetInformation (mask);
92                                 position++;
93                         }
94                 }
95
96                 internal void UncompressFlags ()
97                 {
98                         if (compressed_status_flags == X509ChainStatusFlags.NoError) {
99                                 status = new X509ChainStatus [0];
100                         } else {
101                                 int size = Count (compressed_status_flags);
102                                 status = new X509ChainStatus [size];
103
104                                 int n = 0;
105                                 // process every possible error
106                                 Set (status, ref n, compressed_status_flags, X509ChainStatusFlags.UntrustedRoot);
107                                 Set (status, ref n, compressed_status_flags, X509ChainStatusFlags.NotTimeValid);
108                                 Set (status, ref n, compressed_status_flags, X509ChainStatusFlags.NotTimeNested);
109                                 Set (status, ref n, compressed_status_flags, X509ChainStatusFlags.Revoked);
110                                 Set (status, ref n, compressed_status_flags, X509ChainStatusFlags.NotSignatureValid);
111                                 Set (status, ref n, compressed_status_flags, X509ChainStatusFlags.NotValidForUsage);
112                                 Set (status, ref n, compressed_status_flags, X509ChainStatusFlags.RevocationStatusUnknown);
113                                 Set (status, ref n, compressed_status_flags, X509ChainStatusFlags.Cyclic);
114                                 Set (status, ref n, compressed_status_flags, X509ChainStatusFlags.InvalidExtension);
115                                 Set (status, ref n, compressed_status_flags, X509ChainStatusFlags.InvalidPolicyConstraints);
116                                 Set (status, ref n, compressed_status_flags, X509ChainStatusFlags.InvalidBasicConstraints);
117                                 Set (status, ref n, compressed_status_flags, X509ChainStatusFlags.InvalidNameConstraints);
118                                 Set (status, ref n, compressed_status_flags, X509ChainStatusFlags.HasNotSupportedNameConstraint);
119                                 Set (status, ref n, compressed_status_flags, X509ChainStatusFlags.HasNotDefinedNameConstraint);
120                                 Set (status, ref n, compressed_status_flags, X509ChainStatusFlags.HasNotPermittedNameConstraint);
121                                 Set (status, ref n, compressed_status_flags, X509ChainStatusFlags.HasExcludedNameConstraint);
122                                 Set (status, ref n, compressed_status_flags, X509ChainStatusFlags.PartialChain);
123                                 Set (status, ref n, compressed_status_flags, X509ChainStatusFlags.CtlNotTimeValid);
124                                 Set (status, ref n, compressed_status_flags, X509ChainStatusFlags.CtlNotSignatureValid);
125                                 Set (status, ref n, compressed_status_flags, X509ChainStatusFlags.CtlNotValidForUsage);
126                                 Set (status, ref n, compressed_status_flags, X509ChainStatusFlags.OfflineRevocation);
127                                 Set (status, ref n, compressed_status_flags, X509ChainStatusFlags.NoIssuanceChainPolicy);
128                         }
129                 }
130         }
131 }
132
133 #endif