PKCS7.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. //
  2. // PKCS7.cs: PKCS #7 - Cryptographic Message Syntax Standard
  3. // http://www.rsasecurity.com/rsalabs/pkcs/pkcs-7/index.html
  4. //
  5. // Authors:
  6. // Sebastien Pouliot <sebastien@ximian.com>
  7. // Daniel Granath <dgranath#gmail.com>
  8. //
  9. // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
  10. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Collections;
  33. using System.Security.Cryptography;
  34. namespace MediaBrowser.Server.Mono.Security {
  35. public sealed class PKCS7 {
  36. public class Oid {
  37. // pkcs 1
  38. public const string rsaEncryption = "1.2.840.113549.1.1.1";
  39. // pkcs 7
  40. public const string data = "1.2.840.113549.1.7.1";
  41. public const string signedData = "1.2.840.113549.1.7.2";
  42. public const string envelopedData = "1.2.840.113549.1.7.3";
  43. public const string signedAndEnvelopedData = "1.2.840.113549.1.7.4";
  44. public const string digestedData = "1.2.840.113549.1.7.5";
  45. public const string encryptedData = "1.2.840.113549.1.7.6";
  46. // pkcs 9
  47. public const string contentType = "1.2.840.113549.1.9.3";
  48. public const string messageDigest = "1.2.840.113549.1.9.4";
  49. public const string signingTime = "1.2.840.113549.1.9.5";
  50. public const string countersignature = "1.2.840.113549.1.9.6";
  51. public Oid ()
  52. {
  53. }
  54. }
  55. private PKCS7 ()
  56. {
  57. }
  58. static public ASN1 Attribute (string oid, ASN1 value)
  59. {
  60. ASN1 attr = new ASN1 (0x30);
  61. attr.Add (ASN1Convert.FromOid (oid));
  62. ASN1 aset = attr.Add (new ASN1 (0x31));
  63. aset.Add (value);
  64. return attr;
  65. }
  66. static public ASN1 AlgorithmIdentifier (string oid)
  67. {
  68. ASN1 ai = new ASN1 (0x30);
  69. ai.Add (ASN1Convert.FromOid (oid));
  70. ai.Add (new ASN1 (0x05)); // NULL
  71. return ai;
  72. }
  73. static public ASN1 AlgorithmIdentifier (string oid, ASN1 parameters)
  74. {
  75. ASN1 ai = new ASN1 (0x30);
  76. ai.Add (ASN1Convert.FromOid (oid));
  77. ai.Add (parameters);
  78. return ai;
  79. }
  80. /*
  81. * IssuerAndSerialNumber ::= SEQUENCE {
  82. * issuer Name,
  83. * serialNumber CertificateSerialNumber
  84. * }
  85. */
  86. static public ASN1 IssuerAndSerialNumber (X509Certificate x509)
  87. {
  88. ASN1 issuer = null;
  89. ASN1 serial = null;
  90. ASN1 cert = new ASN1 (x509.RawData);
  91. int tbs = 0;
  92. bool flag = false;
  93. while (tbs < cert[0].Count) {
  94. ASN1 e = cert[0][tbs++];
  95. if (e.Tag == 0x02)
  96. serial = e;
  97. else if (e.Tag == 0x30) {
  98. if (flag) {
  99. issuer = e;
  100. break;
  101. }
  102. flag = true;
  103. }
  104. }
  105. ASN1 iasn = new ASN1 (0x30);
  106. iasn.Add (issuer);
  107. iasn.Add (serial);
  108. return iasn;
  109. }
  110. /*
  111. * ContentInfo ::= SEQUENCE {
  112. * contentType ContentType,
  113. * content [0] EXPLICIT ANY DEFINED BY contentType OPTIONAL
  114. * }
  115. * ContentType ::= OBJECT IDENTIFIER
  116. */
  117. public class ContentInfo {
  118. private string contentType;
  119. private ASN1 content;
  120. public ContentInfo ()
  121. {
  122. content = new ASN1 (0xA0);
  123. }
  124. public ContentInfo (string oid) : this ()
  125. {
  126. contentType = oid;
  127. }
  128. public ContentInfo (byte[] data)
  129. : this (new ASN1 (data)) {}
  130. public ContentInfo (ASN1 asn1)
  131. {
  132. // SEQUENCE with 1 or 2 elements
  133. if ((asn1.Tag != 0x30) || ((asn1.Count < 1) && (asn1.Count > 2)))
  134. throw new ArgumentException ("Invalid ASN1");
  135. if (asn1[0].Tag != 0x06)
  136. throw new ArgumentException ("Invalid contentType");
  137. contentType = ASN1Convert.ToOid (asn1[0]);
  138. if (asn1.Count > 1) {
  139. if (asn1[1].Tag != 0xA0)
  140. throw new ArgumentException ("Invalid content");
  141. content = asn1[1];
  142. }
  143. }
  144. public ASN1 ASN1 {
  145. get { return GetASN1(); }
  146. }
  147. public ASN1 Content {
  148. get { return content; }
  149. set { content = value; }
  150. }
  151. public string ContentType {
  152. get { return contentType; }
  153. set { contentType = value; }
  154. }
  155. internal ASN1 GetASN1 ()
  156. {
  157. // ContentInfo ::= SEQUENCE {
  158. ASN1 contentInfo = new ASN1 (0x30);
  159. // contentType ContentType, -> ContentType ::= OBJECT IDENTIFIER
  160. contentInfo.Add (ASN1Convert.FromOid (contentType));
  161. // content [0] EXPLICIT ANY DEFINED BY contentType OPTIONAL
  162. if ((content != null) && (content.Count > 0))
  163. contentInfo.Add (content);
  164. return contentInfo;
  165. }
  166. public byte[] GetBytes ()
  167. {
  168. return GetASN1 ().GetBytes ();
  169. }
  170. }
  171. /*
  172. * EncryptedData ::= SEQUENCE {
  173. * version INTEGER {edVer0(0)} (edVer0),
  174. * encryptedContentInfo EncryptedContentInfo
  175. * }
  176. */
  177. public class EncryptedData {
  178. private byte _version;
  179. private ContentInfo _content;
  180. private ContentInfo _encryptionAlgorithm;
  181. private byte[] _encrypted;
  182. public EncryptedData ()
  183. {
  184. _version = 0;
  185. }
  186. public EncryptedData (byte[] data)
  187. : this (new ASN1 (data))
  188. {
  189. }
  190. public EncryptedData (ASN1 asn1) : this ()
  191. {
  192. if ((asn1.Tag != 0x30) || (asn1.Count < 2))
  193. throw new ArgumentException ("Invalid EncryptedData");
  194. if (asn1 [0].Tag != 0x02)
  195. throw new ArgumentException ("Invalid version");
  196. _version = asn1 [0].Value [0];
  197. ASN1 encryptedContentInfo = asn1 [1];
  198. if (encryptedContentInfo.Tag != 0x30)
  199. throw new ArgumentException ("missing EncryptedContentInfo");
  200. ASN1 contentType = encryptedContentInfo [0];
  201. if (contentType.Tag != 0x06)
  202. throw new ArgumentException ("missing EncryptedContentInfo.ContentType");
  203. _content = new ContentInfo (ASN1Convert.ToOid (contentType));
  204. ASN1 contentEncryptionAlgorithm = encryptedContentInfo [1];
  205. if (contentEncryptionAlgorithm.Tag != 0x30)
  206. throw new ArgumentException ("missing EncryptedContentInfo.ContentEncryptionAlgorithmIdentifier");
  207. _encryptionAlgorithm = new ContentInfo (ASN1Convert.ToOid (contentEncryptionAlgorithm [0]));
  208. _encryptionAlgorithm.Content = contentEncryptionAlgorithm [1];
  209. ASN1 encryptedContent = encryptedContentInfo [2];
  210. if (encryptedContent.Tag != 0x80)
  211. throw new ArgumentException ("missing EncryptedContentInfo.EncryptedContent");
  212. _encrypted = encryptedContent.Value;
  213. }
  214. public ASN1 ASN1 {
  215. get { return GetASN1(); }
  216. }
  217. public ContentInfo ContentInfo {
  218. get { return _content; }
  219. }
  220. public ContentInfo EncryptionAlgorithm {
  221. get { return _encryptionAlgorithm; }
  222. }
  223. public byte[] EncryptedContent {
  224. get {
  225. if (_encrypted == null)
  226. return null;
  227. return (byte[]) _encrypted.Clone ();
  228. }
  229. }
  230. public byte Version {
  231. get { return _version; }
  232. set { _version = value; }
  233. }
  234. // methods
  235. internal ASN1 GetASN1 ()
  236. {
  237. return null;
  238. }
  239. public byte[] GetBytes ()
  240. {
  241. return GetASN1 ().GetBytes ();
  242. }
  243. }
  244. /*
  245. * EnvelopedData ::= SEQUENCE {
  246. * version Version,
  247. * recipientInfos RecipientInfos,
  248. * encryptedContentInfo EncryptedContentInfo
  249. * }
  250. *
  251. * RecipientInfos ::= SET OF RecipientInfo
  252. *
  253. * EncryptedContentInfo ::= SEQUENCE {
  254. * contentType ContentType,
  255. * contentEncryptionAlgorithm ContentEncryptionAlgorithmIdentifier,
  256. * encryptedContent [0] IMPLICIT EncryptedContent OPTIONAL
  257. * }
  258. *
  259. * EncryptedContent ::= OCTET STRING
  260. *
  261. */
  262. public class EnvelopedData {
  263. private byte _version;
  264. private ContentInfo _content;
  265. private ContentInfo _encryptionAlgorithm;
  266. private ArrayList _recipientInfos;
  267. private byte[] _encrypted;
  268. public EnvelopedData ()
  269. {
  270. _version = 0;
  271. _content = new ContentInfo ();
  272. _encryptionAlgorithm = new ContentInfo ();
  273. _recipientInfos = new ArrayList ();
  274. }
  275. public EnvelopedData (byte[] data)
  276. : this (new ASN1 (data))
  277. {
  278. }
  279. public EnvelopedData (ASN1 asn1) : this ()
  280. {
  281. if ((asn1[0].Tag != 0x30) || (asn1[0].Count < 3))
  282. throw new ArgumentException ("Invalid EnvelopedData");
  283. if (asn1[0][0].Tag != 0x02)
  284. throw new ArgumentException ("Invalid version");
  285. _version = asn1[0][0].Value[0];
  286. // recipientInfos
  287. ASN1 recipientInfos = asn1 [0][1];
  288. if (recipientInfos.Tag != 0x31)
  289. throw new ArgumentException ("missing RecipientInfos");
  290. for (int i=0; i < recipientInfos.Count; i++) {
  291. ASN1 recipientInfo = recipientInfos [i];
  292. _recipientInfos.Add (new RecipientInfo (recipientInfo));
  293. }
  294. ASN1 encryptedContentInfo = asn1[0][2];
  295. if (encryptedContentInfo.Tag != 0x30)
  296. throw new ArgumentException ("missing EncryptedContentInfo");
  297. ASN1 contentType = encryptedContentInfo [0];
  298. if (contentType.Tag != 0x06)
  299. throw new ArgumentException ("missing EncryptedContentInfo.ContentType");
  300. _content = new ContentInfo (ASN1Convert.ToOid (contentType));
  301. ASN1 contentEncryptionAlgorithm = encryptedContentInfo [1];
  302. if (contentEncryptionAlgorithm.Tag != 0x30)
  303. throw new ArgumentException ("missing EncryptedContentInfo.ContentEncryptionAlgorithmIdentifier");
  304. _encryptionAlgorithm = new ContentInfo (ASN1Convert.ToOid (contentEncryptionAlgorithm [0]));
  305. _encryptionAlgorithm.Content = contentEncryptionAlgorithm [1];
  306. ASN1 encryptedContent = encryptedContentInfo [2];
  307. if (encryptedContent.Tag != 0x80)
  308. throw new ArgumentException ("missing EncryptedContentInfo.EncryptedContent");
  309. _encrypted = encryptedContent.Value;
  310. }
  311. public ArrayList RecipientInfos {
  312. get { return _recipientInfos; }
  313. }
  314. public ASN1 ASN1 {
  315. get { return GetASN1(); }
  316. }
  317. public ContentInfo ContentInfo {
  318. get { return _content; }
  319. }
  320. public ContentInfo EncryptionAlgorithm {
  321. get { return _encryptionAlgorithm; }
  322. }
  323. public byte[] EncryptedContent {
  324. get {
  325. if (_encrypted == null)
  326. return null;
  327. return (byte[]) _encrypted.Clone ();
  328. }
  329. }
  330. public byte Version {
  331. get { return _version; }
  332. set { _version = value; }
  333. }
  334. internal ASN1 GetASN1 ()
  335. {
  336. // SignedData ::= SEQUENCE {
  337. ASN1 signedData = new ASN1 (0x30);
  338. // version Version -> Version ::= INTEGER
  339. /* byte[] ver = { _version };
  340. signedData.Add (new ASN1 (0x02, ver));
  341. // digestAlgorithms DigestAlgorithmIdentifiers -> DigestAlgorithmIdentifiers ::= SET OF DigestAlgorithmIdentifier
  342. ASN1 digestAlgorithms = signedData.Add (new ASN1 (0x31));
  343. if (hashAlgorithm != null) {
  344. string hashOid = CryptoConfig.MapNameToOid (hashAlgorithm);
  345. digestAlgorithms.Add (AlgorithmIdentifier (hashOid));
  346. }
  347. // contentInfo ContentInfo,
  348. ASN1 ci = contentInfo.ASN1;
  349. signedData.Add (ci);
  350. if ((mda == null) && (hashAlgorithm != null)) {
  351. // automatically add the messageDigest authenticated attribute
  352. HashAlgorithm ha = HashAlgorithm.Create (hashAlgorithm);
  353. byte[] idcHash = ha.ComputeHash (ci[1][0].Value);
  354. ASN1 md = new ASN1 (0x30);
  355. mda = Attribute (messageDigest, md.Add (new ASN1 (0x04, idcHash)));
  356. signerInfo.AuthenticatedAttributes.Add (mda);
  357. }
  358. // certificates [0] IMPLICIT ExtendedCertificatesAndCertificates OPTIONAL,
  359. if (certs.Count > 0) {
  360. ASN1 a0 = signedData.Add (new ASN1 (0xA0));
  361. foreach (X509Certificate x in certs)
  362. a0.Add (new ASN1 (x.RawData));
  363. }
  364. // crls [1] IMPLICIT CertificateRevocationLists OPTIONAL,
  365. if (crls.Count > 0) {
  366. ASN1 a1 = signedData.Add (new ASN1 (0xA1));
  367. foreach (byte[] crl in crls)
  368. a1.Add (new ASN1 (crl));
  369. }
  370. // signerInfos SignerInfos -> SignerInfos ::= SET OF SignerInfo
  371. ASN1 signerInfos = signedData.Add (new ASN1 (0x31));
  372. if (signerInfo.Key != null)
  373. signerInfos.Add (signerInfo.ASN1);*/
  374. return signedData;
  375. }
  376. public byte[] GetBytes () {
  377. return GetASN1 ().GetBytes ();
  378. }
  379. }
  380. /* RecipientInfo ::= SEQUENCE {
  381. * version Version,
  382. * issuerAndSerialNumber IssuerAndSerialNumber,
  383. * keyEncryptionAlgorithm KeyEncryptionAlgorithmIdentifier,
  384. * encryptedKey EncryptedKey
  385. * }
  386. *
  387. * KeyEncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
  388. *
  389. * EncryptedKey ::= OCTET STRING
  390. */
  391. public class RecipientInfo {
  392. private int _version;
  393. private string _oid;
  394. private byte[] _key;
  395. private byte[] _ski;
  396. private string _issuer;
  397. private byte[] _serial;
  398. public RecipientInfo () {}
  399. public RecipientInfo (ASN1 data)
  400. {
  401. if (data.Tag != 0x30)
  402. throw new ArgumentException ("Invalid RecipientInfo");
  403. ASN1 version = data [0];
  404. if (version.Tag != 0x02)
  405. throw new ArgumentException ("missing Version");
  406. _version = version.Value [0];
  407. // issuerAndSerialNumber IssuerAndSerialNumber
  408. ASN1 subjectIdentifierType = data [1];
  409. if ((subjectIdentifierType.Tag == 0x80) && (_version == 3)) {
  410. _ski = subjectIdentifierType.Value;
  411. }
  412. else {
  413. _issuer = X501.ToString (subjectIdentifierType [0]);
  414. _serial = subjectIdentifierType [1].Value;
  415. }
  416. ASN1 keyEncryptionAlgorithm = data [2];
  417. _oid = ASN1Convert.ToOid (keyEncryptionAlgorithm [0]);
  418. ASN1 encryptedKey = data [3];
  419. _key = encryptedKey.Value;
  420. }
  421. public string Oid {
  422. get { return _oid; }
  423. }
  424. public byte[] Key {
  425. get {
  426. if (_key == null)
  427. return null;
  428. return (byte[]) _key.Clone ();
  429. }
  430. }
  431. public byte[] SubjectKeyIdentifier {
  432. get {
  433. if (_ski == null)
  434. return null;
  435. return (byte[]) _ski.Clone ();
  436. }
  437. }
  438. public string Issuer {
  439. get { return _issuer; }
  440. }
  441. public byte[] Serial {
  442. get {
  443. if (_serial == null)
  444. return null;
  445. return (byte[]) _serial.Clone ();
  446. }
  447. }
  448. public int Version {
  449. get { return _version; }
  450. }
  451. }
  452. /*
  453. * SignedData ::= SEQUENCE {
  454. * version Version,
  455. * digestAlgorithms DigestAlgorithmIdentifiers,
  456. * contentInfo ContentInfo,
  457. * certificates [0] IMPLICIT ExtendedCertificatesAndCertificates OPTIONAL,
  458. * crls [1] IMPLICIT CertificateRevocationLists OPTIONAL,
  459. * signerInfos SignerInfos
  460. * }
  461. */
  462. public class SignedData {
  463. private byte version;
  464. private string hashAlgorithm;
  465. private ContentInfo contentInfo;
  466. private X509CertificateCollection certs;
  467. private ArrayList crls;
  468. private SignerInfo signerInfo;
  469. private bool mda;
  470. private bool signed;
  471. public SignedData ()
  472. {
  473. version = 1;
  474. contentInfo = new ContentInfo ();
  475. certs = new X509CertificateCollection ();
  476. crls = new ArrayList ();
  477. signerInfo = new SignerInfo ();
  478. mda = true;
  479. signed = false;
  480. }
  481. public SignedData (byte[] data)
  482. : this (new ASN1 (data))
  483. {
  484. }
  485. public SignedData (ASN1 asn1)
  486. {
  487. if ((asn1[0].Tag != 0x30) || (asn1[0].Count < 4))
  488. throw new ArgumentException ("Invalid SignedData");
  489. if (asn1[0][0].Tag != 0x02)
  490. throw new ArgumentException ("Invalid version");
  491. version = asn1[0][0].Value[0];
  492. contentInfo = new ContentInfo (asn1[0][2]);
  493. int n = 3;
  494. certs = new X509CertificateCollection ();
  495. if (asn1[0][n].Tag == 0xA0) {
  496. for (int i=0; i < asn1[0][n].Count; i++)
  497. certs.Add (new X509Certificate (asn1[0][n][i].GetBytes ()));
  498. n++;
  499. }
  500. crls = new ArrayList ();
  501. if (asn1[0][n].Tag == 0xA1) {
  502. for (int i=0; i < asn1[0][n].Count; i++)
  503. crls.Add (asn1[0][n][i].GetBytes ());
  504. n++;
  505. }
  506. if (asn1[0][n].Count > 0)
  507. signerInfo = new SignerInfo (asn1[0][n]);
  508. else
  509. signerInfo = new SignerInfo ();
  510. // Exchange hash algorithm Oid from SignerInfo
  511. if (signerInfo.HashName != null) {
  512. HashName = OidToName(signerInfo.HashName);
  513. }
  514. // Check if SignerInfo has authenticated attributes
  515. mda = (signerInfo.AuthenticatedAttributes.Count > 0);
  516. }
  517. public ASN1 ASN1 {
  518. get { return GetASN1(); }
  519. }
  520. public X509CertificateCollection Certificates {
  521. get { return certs; }
  522. }
  523. public ContentInfo ContentInfo {
  524. get { return contentInfo; }
  525. }
  526. public ArrayList Crls {
  527. get { return crls; }
  528. }
  529. public string HashName {
  530. get { return hashAlgorithm; }
  531. // todo add validation
  532. set {
  533. hashAlgorithm = value;
  534. signerInfo.HashName = value;
  535. }
  536. }
  537. public SignerInfo SignerInfo {
  538. get { return signerInfo; }
  539. }
  540. public byte Version {
  541. get { return version; }
  542. set { version = value; }
  543. }
  544. public bool UseAuthenticatedAttributes {
  545. get { return mda; }
  546. set { mda = value; }
  547. }
  548. public bool VerifySignature (AsymmetricAlgorithm aa)
  549. {
  550. if (aa == null) {
  551. return false;
  552. }
  553. RSAPKCS1SignatureDeformatter r = new RSAPKCS1SignatureDeformatter (aa);
  554. r.SetHashAlgorithm (hashAlgorithm);
  555. HashAlgorithm ha = HashAlgorithm.Create (hashAlgorithm);
  556. byte[] signature = signerInfo.Signature;
  557. byte[] hash = null;
  558. if (mda) {
  559. ASN1 asn = new ASN1 (0x31);
  560. foreach (ASN1 attr in signerInfo.AuthenticatedAttributes)
  561. asn.Add (attr);
  562. hash = ha.ComputeHash (asn.GetBytes ());
  563. } else {
  564. hash = ha.ComputeHash (contentInfo.Content[0].Value);
  565. }
  566. if (hash != null && signature != null) {
  567. return r.VerifySignature (hash, signature);
  568. }
  569. return false;
  570. }
  571. internal string OidToName (string oid)
  572. {
  573. switch (oid) {
  574. case "1.3.14.3.2.26" :
  575. return "SHA1";
  576. case "1.2.840.113549.2.2" :
  577. return "MD2";
  578. case "1.2.840.113549.2.5" :
  579. return "MD5";
  580. case "2.16.840.1.101.3.4.1" :
  581. return "SHA256";
  582. case "2.16.840.1.101.3.4.2" :
  583. return "SHA384";
  584. case "2.16.840.1.101.3.4.3" :
  585. return "SHA512";
  586. default :
  587. break;
  588. }
  589. // Unknown Oid
  590. return oid;
  591. }
  592. internal ASN1 GetASN1 ()
  593. {
  594. // SignedData ::= SEQUENCE {
  595. ASN1 signedData = new ASN1 (0x30);
  596. // version Version -> Version ::= INTEGER
  597. byte[] ver = { version };
  598. signedData.Add (new ASN1 (0x02, ver));
  599. // digestAlgorithms DigestAlgorithmIdentifiers -> DigestAlgorithmIdentifiers ::= SET OF DigestAlgorithmIdentifier
  600. ASN1 digestAlgorithms = signedData.Add (new ASN1 (0x31));
  601. if (hashAlgorithm != null) {
  602. string hashOid = CryptoConfig.MapNameToOID (hashAlgorithm);
  603. digestAlgorithms.Add (AlgorithmIdentifier (hashOid));
  604. }
  605. // contentInfo ContentInfo,
  606. ASN1 ci = contentInfo.ASN1;
  607. signedData.Add (ci);
  608. if (!signed && (hashAlgorithm != null)) {
  609. if (mda) {
  610. // Use authenticated attributes for signature
  611. // Automatically add the contentType authenticated attribute
  612. ASN1 ctattr = Attribute (Oid.contentType, ci[0]);
  613. signerInfo.AuthenticatedAttributes.Add (ctattr);
  614. // Automatically add the messageDigest authenticated attribute
  615. HashAlgorithm ha = HashAlgorithm.Create (hashAlgorithm);
  616. byte[] idcHash = ha.ComputeHash (ci[1][0].Value);
  617. ASN1 md = new ASN1 (0x30);
  618. ASN1 mdattr = Attribute (Oid.messageDigest, md.Add (new ASN1 (0x04, idcHash)));
  619. signerInfo.AuthenticatedAttributes.Add (mdattr);
  620. } else {
  621. // Don't use authenticated attributes for signature -- signature is content
  622. RSAPKCS1SignatureFormatter r = new RSAPKCS1SignatureFormatter (signerInfo.Key);
  623. r.SetHashAlgorithm (hashAlgorithm);
  624. HashAlgorithm ha = HashAlgorithm.Create (hashAlgorithm);
  625. byte[] sig = ha.ComputeHash (ci[1][0].Value);
  626. signerInfo.Signature = r.CreateSignature (sig);
  627. }
  628. signed = true;
  629. }
  630. // certificates [0] IMPLICIT ExtendedCertificatesAndCertificates OPTIONAL,
  631. if (certs.Count > 0) {
  632. ASN1 a0 = signedData.Add (new ASN1 (0xA0));
  633. foreach (X509Certificate x in certs)
  634. a0.Add (new ASN1 (x.RawData));
  635. }
  636. // crls [1] IMPLICIT CertificateRevocationLists OPTIONAL,
  637. if (crls.Count > 0) {
  638. ASN1 a1 = signedData.Add (new ASN1 (0xA1));
  639. foreach (byte[] crl in crls)
  640. a1.Add (new ASN1 (crl));
  641. }
  642. // signerInfos SignerInfos -> SignerInfos ::= SET OF SignerInfo
  643. ASN1 signerInfos = signedData.Add (new ASN1 (0x31));
  644. if (signerInfo.Key != null)
  645. signerInfos.Add (signerInfo.ASN1);
  646. return signedData;
  647. }
  648. public byte[] GetBytes ()
  649. {
  650. return GetASN1 ().GetBytes ();
  651. }
  652. }
  653. /*
  654. * SignerInfo ::= SEQUENCE {
  655. * version Version,
  656. * issuerAndSerialNumber IssuerAndSerialNumber,
  657. * digestAlgorithm DigestAlgorithmIdentifier,
  658. * authenticatedAttributes [0] IMPLICIT Attributes OPTIONAL,
  659. * digestEncryptionAlgorithm DigestEncryptionAlgorithmIdentifier,
  660. * encryptedDigest EncryptedDigest,
  661. * unauthenticatedAttributes [1] IMPLICIT Attributes OPTIONAL
  662. * }
  663. *
  664. * For version == 3 issuerAndSerialNumber may be replaced by ...
  665. */
  666. public class SignerInfo {
  667. private byte version;
  668. private X509Certificate x509;
  669. private string hashAlgorithm;
  670. private AsymmetricAlgorithm key;
  671. private ArrayList authenticatedAttributes;
  672. private ArrayList unauthenticatedAttributes;
  673. private byte[] signature;
  674. private string issuer;
  675. private byte[] serial;
  676. private byte[] ski;
  677. public SignerInfo ()
  678. {
  679. version = 1;
  680. authenticatedAttributes = new ArrayList ();
  681. unauthenticatedAttributes = new ArrayList ();
  682. }
  683. public SignerInfo (byte[] data)
  684. : this (new ASN1 (data)) {}
  685. // TODO: INCOMPLETE
  686. public SignerInfo (ASN1 asn1) : this ()
  687. {
  688. if ((asn1[0].Tag != 0x30) || (asn1[0].Count < 5))
  689. throw new ArgumentException ("Invalid SignedData");
  690. // version Version
  691. if (asn1[0][0].Tag != 0x02)
  692. throw new ArgumentException ("Invalid version");
  693. version = asn1[0][0].Value[0];
  694. // issuerAndSerialNumber IssuerAndSerialNumber
  695. ASN1 subjectIdentifierType = asn1 [0][1];
  696. if ((subjectIdentifierType.Tag == 0x80) && (version == 3)) {
  697. ski = subjectIdentifierType.Value;
  698. }
  699. else {
  700. issuer = X501.ToString (subjectIdentifierType [0]);
  701. serial = subjectIdentifierType [1].Value;
  702. }
  703. // digestAlgorithm DigestAlgorithmIdentifier
  704. ASN1 digestAlgorithm = asn1 [0][2];
  705. hashAlgorithm = ASN1Convert.ToOid (digestAlgorithm [0]);
  706. // authenticatedAttributes [0] IMPLICIT Attributes OPTIONAL
  707. int n = 3;
  708. ASN1 authAttributes = asn1 [0][n];
  709. if (authAttributes.Tag == 0xA0) {
  710. n++;
  711. for (int i=0; i < authAttributes.Count; i++)
  712. authenticatedAttributes.Add (authAttributes [i]);
  713. }
  714. // digestEncryptionAlgorithm DigestEncryptionAlgorithmIdentifier
  715. n++;
  716. // ASN1 digestEncryptionAlgorithm = asn1 [0][n++];
  717. // string digestEncryptionAlgorithmOid = ASN1Convert.ToOid (digestEncryptionAlgorithm [0]);
  718. // encryptedDigest EncryptedDigest
  719. ASN1 encryptedDigest = asn1 [0][n++];
  720. if (encryptedDigest.Tag == 0x04)
  721. signature = encryptedDigest.Value;
  722. // unauthenticatedAttributes [1] IMPLICIT Attributes OPTIONAL
  723. ASN1 unauthAttributes = asn1 [0][n];
  724. if ((unauthAttributes != null) && (unauthAttributes.Tag == 0xA1)) {
  725. for (int i=0; i < unauthAttributes.Count; i++)
  726. unauthenticatedAttributes.Add (unauthAttributes [i]);
  727. }
  728. }
  729. public string IssuerName {
  730. get { return issuer; }
  731. }
  732. public byte[] SerialNumber {
  733. get {
  734. if (serial == null)
  735. return null;
  736. return (byte[]) serial.Clone ();
  737. }
  738. }
  739. public byte[] SubjectKeyIdentifier {
  740. get {
  741. if (ski == null)
  742. return null;
  743. return (byte[]) ski.Clone ();
  744. }
  745. }
  746. public ASN1 ASN1 {
  747. get { return GetASN1(); }
  748. }
  749. public ArrayList AuthenticatedAttributes {
  750. get { return authenticatedAttributes; }
  751. }
  752. public X509Certificate Certificate {
  753. get { return x509; }
  754. set { x509 = value; }
  755. }
  756. public string HashName {
  757. get { return hashAlgorithm; }
  758. set { hashAlgorithm = value; }
  759. }
  760. public AsymmetricAlgorithm Key {
  761. get { return key; }
  762. set { key = value; }
  763. }
  764. public byte[] Signature {
  765. get {
  766. if (signature == null)
  767. return null;
  768. return (byte[]) signature.Clone ();
  769. }
  770. set {
  771. if (value != null) {
  772. signature = (byte[]) value.Clone ();
  773. }
  774. }
  775. }
  776. public ArrayList UnauthenticatedAttributes {
  777. get { return unauthenticatedAttributes; }
  778. }
  779. public byte Version {
  780. get { return version; }
  781. set { version = value; }
  782. }
  783. internal ASN1 GetASN1 ()
  784. {
  785. if ((key == null) || (hashAlgorithm == null))
  786. return null;
  787. byte[] ver = { version };
  788. ASN1 signerInfo = new ASN1 (0x30);
  789. // version Version -> Version ::= INTEGER
  790. signerInfo.Add (new ASN1 (0x02, ver));
  791. // issuerAndSerialNumber IssuerAndSerialNumber,
  792. signerInfo.Add (PKCS7.IssuerAndSerialNumber (x509));
  793. // digestAlgorithm DigestAlgorithmIdentifier,
  794. string hashOid = CryptoConfig.MapNameToOID (hashAlgorithm);
  795. signerInfo.Add (AlgorithmIdentifier (hashOid));
  796. // authenticatedAttributes [0] IMPLICIT Attributes OPTIONAL,
  797. ASN1 aa = null;
  798. if (authenticatedAttributes.Count > 0) {
  799. aa = signerInfo.Add (new ASN1 (0xA0));
  800. authenticatedAttributes.Sort(new SortedSet ());
  801. foreach (ASN1 attr in authenticatedAttributes)
  802. aa.Add (attr);
  803. }
  804. // digestEncryptionAlgorithm DigestEncryptionAlgorithmIdentifier,
  805. if (key is RSA) {
  806. signerInfo.Add (AlgorithmIdentifier (PKCS7.Oid.rsaEncryption));
  807. if (aa != null) {
  808. // Calculate the signature here; otherwise it must be set from SignedData
  809. RSAPKCS1SignatureFormatter r = new RSAPKCS1SignatureFormatter (key);
  810. r.SetHashAlgorithm (hashAlgorithm);
  811. byte[] tbs = aa.GetBytes ();
  812. tbs [0] = 0x31; // not 0xA0 for signature
  813. HashAlgorithm ha = HashAlgorithm.Create (hashAlgorithm);
  814. byte[] tbsHash = ha.ComputeHash (tbs);
  815. signature = r.CreateSignature (tbsHash);
  816. }
  817. }
  818. else if (key is DSA) {
  819. throw new NotImplementedException ("not yet");
  820. }
  821. else
  822. throw new CryptographicException ("Unknown assymetric algorithm");
  823. // encryptedDigest EncryptedDigest,
  824. signerInfo.Add (new ASN1 (0x04, signature));
  825. // unauthenticatedAttributes [1] IMPLICIT Attributes OPTIONAL
  826. if (unauthenticatedAttributes.Count > 0) {
  827. ASN1 ua = signerInfo.Add (new ASN1 (0xA1));
  828. unauthenticatedAttributes.Sort(new SortedSet ());
  829. foreach (ASN1 attr in unauthenticatedAttributes)
  830. ua.Add (attr);
  831. }
  832. return signerInfo;
  833. }
  834. public byte[] GetBytes ()
  835. {
  836. return GetASN1 ().GetBytes ();
  837. }
  838. }
  839. internal class SortedSet : IComparer {
  840. public int Compare (object x, object y)
  841. {
  842. if (x == null)
  843. return (y == null) ? 0 : -1;
  844. else if (y == null)
  845. return 1;
  846. ASN1 xx = x as ASN1;
  847. ASN1 yy = y as ASN1;
  848. if ((xx == null) || (yy == null)) {
  849. throw new ArgumentException (("Invalid objects."));
  850. }
  851. byte[] xb = xx.GetBytes ();
  852. byte[] yb = yy.GetBytes ();
  853. for (int i = 0; i < xb.Length; i++) {
  854. if (i == yb.Length)
  855. break;
  856. if (xb[i] == yb[i])
  857. continue;
  858. return (xb[i] < yb[i]) ? -1 : 1;
  859. }
  860. // The arrays are equal up to the shortest of them.
  861. if (xb.Length > yb.Length)
  862. return 1;
  863. else if (xb.Length < yb.Length)
  864. return -1;
  865. return 0;
  866. }
  867. }
  868. }
  869. }