ContentInfo.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // ContentInfo.cs - System.Security.Cryptography.Pkcs.ContentInfo
  3. //
  4. // Author:
  5. // Sebastien Pouliot ([email protected])
  6. //
  7. // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
  8. //
  9. using System;
  10. using System.Security.Cryptography;
  11. using Mono.Security;
  12. namespace System.Security.Cryptography.Pkcs {
  13. /*
  14. * ContentInfo ::= SEQUENCE {
  15. * contentType ContentType,
  16. * content [0] EXPLICIT ANY DEFINED BY contentType OPTIONAL
  17. * }
  18. * ContentType ::= OBJECT IDENTIFIER
  19. */
  20. public sealed class ContentInfo {
  21. private Oid _oid;
  22. private byte[] _content;
  23. // constructors
  24. public ContentInfo (byte[] content)
  25. : this (new Oid ("1.2.840.113549.1.7.1"), content) {}
  26. public ContentInfo (Oid oid, byte[] content)
  27. {
  28. if (oid == null)
  29. throw new ArgumentNullException ("oid");
  30. if (content == null)
  31. throw new ArgumentNullException ("content");
  32. _oid = oid;
  33. _content = content;
  34. }
  35. // properties
  36. public byte[] Content {
  37. get { return _content; }
  38. }
  39. public Oid ContentType {
  40. get { return _oid; }
  41. }
  42. // static methods
  43. [MonoTODO("Incomplete OID support")]
  44. public static Oid GetContentType (byte[] encodedMessage)
  45. {
  46. // FIXME: compatibility with fx 1.2.3400.0
  47. if (encodedMessage == null)
  48. throw new NullReferenceException ();
  49. // throw new ArgumentNullException ("algorithm");
  50. try {
  51. PKCS7.ContentInfo ci = new PKCS7.ContentInfo (encodedMessage);
  52. switch (ci.ContentType) {
  53. // TODO - there are probably more - need testing
  54. case PKCS7.signedData:
  55. return new Oid (ci.ContentType);
  56. default:
  57. throw new CryptographicException ("Bad ASN1 - invalid OID");
  58. }
  59. }
  60. catch (Exception e) {
  61. throw new CryptographicException ("Bad ASN1 - invalid structure", e);
  62. }
  63. }
  64. }
  65. }