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