Pkcs9SigningTime.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // System.Security.Cryptography.Pkcs.Pkcs9SigningTime class
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  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. #if SECURITY_DEP
  30. using System.Globalization;
  31. using System.Text;
  32. using Mono.Security;
  33. namespace System.Security.Cryptography.Pkcs {
  34. public sealed class Pkcs9SigningTime : Pkcs9AttributeObject {
  35. internal const string oid = "1.2.840.113549.1.9.5";
  36. internal const string friendlyName = "Signing Time";
  37. private DateTime _signingTime;
  38. public Pkcs9SigningTime ()
  39. {
  40. // Pkcs9Attribute remove the "set" accessor on Oid :-(
  41. (this as AsnEncodedData).Oid = new Oid (oid, friendlyName);
  42. _signingTime = DateTime.Now;
  43. RawData = Encode ();
  44. }
  45. public Pkcs9SigningTime (DateTime signingTime)
  46. {
  47. (this as AsnEncodedData).Oid = new Oid (oid, friendlyName);
  48. _signingTime = signingTime;
  49. RawData = Encode ();
  50. }
  51. public Pkcs9SigningTime (byte[] encodedSigningTime)
  52. {
  53. if (encodedSigningTime == null)
  54. throw new ArgumentNullException ("encodedSigningTime");
  55. (this as AsnEncodedData).Oid = new Oid (oid, friendlyName);
  56. RawData = encodedSigningTime;
  57. Decode (encodedSigningTime);
  58. }
  59. public DateTime SigningTime {
  60. get { return _signingTime; }
  61. }
  62. public override void CopyFrom (AsnEncodedData asnEncodedData)
  63. {
  64. if (asnEncodedData == null)
  65. throw new ArgumentNullException ("asnEncodedData");
  66. Decode (asnEncodedData.RawData);
  67. Oid = asnEncodedData.Oid;
  68. RawData = asnEncodedData.RawData;
  69. }
  70. // internal stuff
  71. internal void Decode (byte[] attribute)
  72. {
  73. // Only UTCTIME is supported by FX 2.0
  74. if (attribute [0] != 0x17)
  75. throw new CryptographicException (Locale.GetText ("Only UTCTIME is supported."));
  76. ASN1 attr = new ASN1 (attribute);
  77. byte[] value = attr.Value;
  78. string date = Encoding.ASCII.GetString (value, 0, value.Length - 1);
  79. _signingTime = DateTime.ParseExact (date, "yyMMddHHmmss", null);
  80. }
  81. internal byte[] Encode ()
  82. {
  83. if (_signingTime.Year <= 1600)
  84. throw new ArgumentOutOfRangeException ("<= 1600");
  85. // Only UTCTIME is supported by FX 2.0
  86. if ((_signingTime.Year < 1950) || (_signingTime.Year >= 2050))
  87. throw new CryptographicException ("[1950,2049]");
  88. string date = _signingTime.ToString ("yyMMddHHmmss", CultureInfo.InvariantCulture) + "Z";
  89. ASN1 attr = new ASN1 (0x17, Encoding.ASCII.GetBytes (date));
  90. return attr.GetBytes ();
  91. }
  92. }
  93. }
  94. #endif