2004-04-28 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / corlib / Mono.Security.X509.Extensions / BasicConstraintsExtension.cs
1 //
2 // BasicConstraintsExtension.cs: Handles X.509 BasicConstrains extensions.
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // (C) 2004 Novell (http://www.novell.com)
9 //
10
11 using System;
12 using System.Globalization;
13 using System.Text;
14
15 using Mono.Security;
16 using Mono.Security.X509;
17
18 namespace Mono.Security.X509.Extensions {
19
20         // References:
21         // 1.   RFC 3280: Internet X.509 Public Key Infrastructure, Section 4.2.1.10
22         //      http://www.ietf.org/rfc/rfc3280.txt
23
24         /* id-ce-basicConstraints OBJECT IDENTIFIER ::=  { id-ce 19 }
25          * 
26          * BasicConstraints ::= SEQUENCE {
27          *      cA                      BOOLEAN DEFAULT FALSE,
28          *      pathLenConstraint       INTEGER (0..MAX) OPTIONAL 
29          * }
30          */
31 #if INSIDE_CORLIB
32         internal
33 #else
34         public 
35 #endif
36         class BasicConstraintsExtension : X509Extension {
37
38                 private bool cA;
39                 private int pathLenConstraint;
40
41                 public BasicConstraintsExtension () : base () 
42                 {
43                         extnOid = "2.5.29.19";
44                 }
45
46                 public BasicConstraintsExtension (ASN1 asn1) : base (asn1) {}
47
48                 public BasicConstraintsExtension (X509Extension extension) : base (extension) {}
49
50                 protected override void Decode () 
51                 {
52                         // default values
53                         cA = false;
54                         pathLenConstraint = 0; // no constraint
55
56                         ASN1 sequence = new ASN1 (extnValue.Value);
57                         if (sequence.Tag != 0x30)
58                                 throw new ArgumentException ("Invalid BasicConstraints extension");
59                         int n = 0;
60                         ASN1 a = sequence [n++];
61                         if ((a != null) && (a.Tag == 0x01)) {
62                                 cA = (a.Value [0] == 0xFF);
63                                 a = sequence [n++];
64                         }
65                         if ((a != null) && (a.Tag == 0x02))
66                                 pathLenConstraint = ASN1Convert.ToInt32 (a);
67                 }
68
69                 protected override void Encode () 
70                 {
71                         if (extnValue == null) {
72                                 extnValue = new ASN1 (0x30);
73                                 if (cA)
74                                         extnValue.Add (new ASN1 (0x01, new byte[] { 0xFF }));
75                                 if (pathLenConstraint > 0)
76                                         extnValue.Add (ASN1Convert.FromInt32 (pathLenConstraint));
77                         }
78                 }
79
80                 public bool CertificateAuthority {
81                         get { return cA; }
82                         set { cA = value; }
83                 }
84
85                 public override string Name {
86                         get { return "Basic Constraints"; }
87                 }
88
89                 public int PathLenConstraint {
90                         get { return pathLenConstraint; }
91                         set { pathLenConstraint = value; }
92                 }
93
94                 public override string ToString () 
95                 {
96                         StringBuilder sb = new StringBuilder ();
97                         sb.Append ("Subject Type=");
98                         sb.Append ((cA) ? "CA" : "End Entity");
99                         sb.Append (Environment.NewLine);
100                         sb.Append ("Path Length Constraint=");
101                         if (pathLenConstraint == 0)
102                                 sb.Append ("None");
103                         else
104                                 sb.Append (pathLenConstraint.ToString (CultureInfo.InvariantCulture));
105                         sb.Append (Environment.NewLine);
106                         return sb.ToString ();
107                 }
108         }
109 }