Merge pull request #1502 from madrang/SafeHandle.CloseTestDispose
[mono.git] / mcs / class / System.Security / System.Security.Cryptography.Pkcs / Pkcs9SigningTime.cs
1 //
2 // System.Security.Cryptography.Pkcs.Pkcs9SigningTime class
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004-2005 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 using System.Globalization;
33 using System.Text;
34
35 using Mono.Security;
36
37 namespace System.Security.Cryptography.Pkcs {
38
39         public sealed class Pkcs9SigningTime : Pkcs9AttributeObject {
40
41                 internal const string oid = "1.2.840.113549.1.9.5";
42                 internal const string friendlyName = "Signing Time";
43
44                 private DateTime _signingTime;
45
46                 public Pkcs9SigningTime () 
47                 {
48                         // Pkcs9Attribute remove the "set" accessor on Oid :-(
49                         (this as AsnEncodedData).Oid = new Oid (oid, friendlyName);
50                         _signingTime = DateTime.Now;
51                         RawData = Encode ();
52                 }
53
54                 public Pkcs9SigningTime (DateTime signingTime)
55                 {
56                         (this as AsnEncodedData).Oid = new Oid (oid, friendlyName);
57                         _signingTime = signingTime;
58                         RawData = Encode ();
59                 }
60
61                 public Pkcs9SigningTime (byte[] encodedSigningTime)
62                 {
63                         if (encodedSigningTime == null)
64                                 throw new ArgumentNullException ("encodedSigningTime");
65
66                         (this as AsnEncodedData).Oid = new Oid (oid, friendlyName);
67                         RawData = encodedSigningTime;
68                         Decode (encodedSigningTime);
69                 }
70
71                 public DateTime SigningTime {
72                         get { return _signingTime; }
73                 }
74
75                 public override void CopyFrom (AsnEncodedData asnEncodedData)
76                 {
77                         if (asnEncodedData == null)
78                                 throw new ArgumentNullException ("asnEncodedData");
79
80                         Decode (asnEncodedData.RawData);
81                         Oid = asnEncodedData.Oid;
82                         RawData = asnEncodedData.RawData;
83                 }
84
85                 // internal stuff
86
87                 internal void Decode (byte[] attribute)
88                 {
89                         // Only UTCTIME is supported by FX 2.0
90                         if (attribute [0] != 0x17)
91                                 throw new CryptographicException (Locale.GetText ("Only UTCTIME is supported."));
92
93                         ASN1 attr = new ASN1 (attribute);
94                         byte[] value = attr.Value;
95                         string date = Encoding.ASCII.GetString (value, 0, value.Length - 1);
96                         _signingTime = DateTime.ParseExact (date, "yyMMddHHmmss", null);
97                 }
98
99                 internal byte[] Encode ()
100                 {
101                         if (_signingTime.Year <= 1600)
102                                 throw new ArgumentOutOfRangeException ("<= 1600");
103                         // Only UTCTIME is supported by FX 2.0
104                         if ((_signingTime.Year < 1950) || (_signingTime.Year >= 2050))
105                                 throw new CryptographicException ("[1950,2049]");
106
107                         string date = _signingTime.ToString ("yyMMddHHmmss", CultureInfo.InvariantCulture) + "Z";
108                         ASN1 attr = new ASN1 (0x17, Encoding.ASCII.GetBytes (date));
109                         return attr.GetBytes ();
110                 }
111         }
112 }
113
114 #endif