2 // System.Security.Cryptography.Pkcs.Pkcs9SigningTime class
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004-2005 Novell Inc. (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
32 using System.Globalization;
37 namespace System.Security.Cryptography.Pkcs {
39 public sealed class Pkcs9SigningTime : Pkcs9AttributeObject {
41 internal const string oid = "1.2.840.113549.1.9.5";
42 internal const string friendlyName = "Signing Time";
44 private DateTime _signingTime;
46 public Pkcs9SigningTime ()
48 // Pkcs9Attribute remove the "set" accessor on Oid :-(
49 (this as AsnEncodedData).Oid = new Oid (oid, friendlyName);
50 _signingTime = DateTime.Now;
54 public Pkcs9SigningTime (DateTime signingTime)
56 (this as AsnEncodedData).Oid = new Oid (oid, friendlyName);
57 _signingTime = signingTime;
61 public Pkcs9SigningTime (byte[] encodedSigningTime)
63 if (encodedSigningTime == null)
64 throw new ArgumentNullException ("encodedSigningTime");
66 (this as AsnEncodedData).Oid = new Oid (oid, friendlyName);
67 RawData = encodedSigningTime;
68 Decode (encodedSigningTime);
71 public DateTime SigningTime {
72 get { return _signingTime; }
75 public override void CopyFrom (AsnEncodedData asnEncodedData)
77 if (asnEncodedData == null)
78 throw new ArgumentNullException ("asnEncodedData");
80 Decode (asnEncodedData.RawData);
81 Oid = asnEncodedData.Oid;
82 RawData = asnEncodedData.RawData;
87 internal void Decode (byte[] attribute)
89 // Only UTCTIME is supported by FX 2.0
90 if (attribute [0] != 0x17)
91 throw new CryptographicException (Locale.GetText ("Only UTCTIME is supported."));
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);
99 internal byte[] Encode ()
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]");
107 string date = _signingTime.ToString ("yyMMddHHmmss", CultureInfo.InvariantCulture) + "Z";
108 ASN1 attr = new ASN1 (0x17, Encoding.ASCII.GetBytes (date));
109 return attr.GetBytes ();