* MonoTouch/MonoPInvokeCallbackAttribute.cs: Added.
[mono.git] / mcs / class / Mono.Security / Mono.Security.X509.Extensions / CertificatePoliciesExtension.cs
1 //
2 // CertificatePoliciesExtension.cs: Handles X.509 CertificatePolicies extensions.
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) 2004 Novell (http://www.novell.com)
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Collections;
33 using System.Text;
34
35 using Mono.Security;
36 using Mono.Security.X509;
37
38 namespace Mono.Security.X509.Extensions {
39
40         /*
41          * id-ce-certificatePolicies OBJECT IDENTIFIER ::=  { id-ce 32 }
42          * 
43          * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificate-policies 0 }
44          * 
45          * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
46          * 
47          * PolicyInformation ::= SEQUENCE {
48          *    policyIdentifier   CertPolicyId,
49          *    policyQualifiers   SEQUENCE SIZE (1..MAX) OF PolicyQualifierInfo OPTIONAL 
50          * }
51          * 
52          * CertPolicyId ::= OBJECT IDENTIFIER
53          * 
54          * PolicyQualifierInfo ::= SEQUENCE {
55          *    policyQualifierId  PolicyQualifierId,
56          *    qualifier          ANY DEFINED BY policyQualifierId 
57          * }
58          * 
59          * -- policyQualifierIds for Internet policy qualifiers
60          * id-qt          OBJECT IDENTIFIER ::=  { id-pkix 2 }
61          * id-qt-cps      OBJECT IDENTIFIER ::=  { id-qt 1 }
62          * id-qt-unotice  OBJECT IDENTIFIER ::=  { id-qt 2 }
63          * 
64          * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
65          * 
66          * Qualifier ::= CHOICE {
67          *    cPSuri           CPSuri,
68          *    userNotice       UserNotice 
69          * }
70          * 
71          * CPSuri ::= IA5String
72          * 
73          * UserNotice ::= SEQUENCE {
74          *    noticeRef        NoticeReference OPTIONAL,
75          *    explicitText     DisplayText OPTIONAL
76          * }
77          * 
78          * NoticeReference ::= SEQUENCE {
79          *    organization     DisplayText,
80          *    noticeNumbers    SEQUENCE OF INTEGER 
81          * }
82          * 
83          * DisplayText ::= CHOICE {
84          *    ia5String        IA5String      (SIZE (1..200)),
85          *    visibleString    VisibleString  (SIZE (1..200)),
86          *    bmpString        BMPString      (SIZE (1..200)),
87          *    utf8String       UTF8String     (SIZE (1..200)) 
88          * }
89          */
90
91         // note: partial implementation (only policyIdentifier OID are supported)
92         public class CertificatePoliciesExtension : X509Extension {
93
94                 private Hashtable policies;
95
96                 public CertificatePoliciesExtension () : base () 
97                 {
98                         extnOid = "2.5.29.32";
99                         policies = new Hashtable ();
100                 }
101
102                 public CertificatePoliciesExtension (ASN1 asn1) : base (asn1)
103                 {
104                 }
105
106                 public CertificatePoliciesExtension (X509Extension extension) : base (extension)
107                 {
108                 }
109
110                 protected override void Decode () 
111                 {
112                         policies = new Hashtable ();
113                         ASN1 sequence = new ASN1 (extnValue.Value);
114                         if (sequence.Tag != 0x30)
115                                 throw new ArgumentException ("Invalid CertificatePolicies extension");
116                         // for every policy OID
117                         for (int i=0; i < sequence.Count; i++) {
118                                 policies.Add (ASN1Convert.ToOid (sequence [i][0]), null);
119                         }
120                 }
121
122                 public override string Name {
123                         get { return "Certificate Policies"; }
124                 }
125
126                 public override string ToString () 
127                 {
128                         StringBuilder sb = new StringBuilder ();
129                         int n = 1;
130                         foreach (DictionaryEntry policy in policies) {
131                                 sb.Append ("[");
132                                 sb.Append (n++);
133                                 sb.Append ("]Certificate Policy:");
134                                 sb.Append (Environment.NewLine);
135                                 sb.Append ("\tPolicyIdentifier=");
136                                 sb.Append ((string)policy.Key);
137                                 sb.Append (Environment.NewLine);
138                         }
139                         return sb.ToString ();
140                 }
141         }
142 }