PkiArchiveControlBuilder.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cms;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Crmf;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Cms;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  11. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crmf
  12. {
  13. public class PkiArchiveControlBuilder
  14. {
  15. private CmsEnvelopedDataGenerator envGen;
  16. private CmsProcessableByteArray keyContent;
  17. /// <summary>
  18. ///Basic constructor - specify the contents of the PKIArchiveControl structure.
  19. /// </summary>
  20. /// <param name="privateKeyInfo">the private key to be archived.</param>
  21. /// <param name="generalName">the general name to be associated with the private key.</param>
  22. ///
  23. public PkiArchiveControlBuilder(PrivateKeyInfo privateKeyInfo, GeneralName generalName)
  24. {
  25. EncKeyWithID encKeyWithID = new EncKeyWithID(privateKeyInfo, generalName);
  26. try
  27. {
  28. this.keyContent = new CmsProcessableByteArray(CrmfObjectIdentifiers.id_ct_encKeyWithID, encKeyWithID.GetEncoded());
  29. }
  30. catch (IOException e)
  31. {
  32. throw new InvalidOperationException("unable to encode key and general name info", e);
  33. }
  34. this.envGen = new CmsEnvelopedDataGenerator();
  35. }
  36. ///<summary>Add a recipient generator to this control.</summary>
  37. ///<param name="recipientGen"> recipient generator created for a specific recipient.</param>
  38. ///<returns>this builder object.</returns>
  39. public PkiArchiveControlBuilder AddRecipientGenerator(RecipientInfoGenerator recipientGen)
  40. {
  41. envGen.AddRecipientInfoGenerator(recipientGen);
  42. return this;
  43. }
  44. /// <summary>Build the PKIArchiveControl using the passed in encryptor to encrypt its contents.</summary>
  45. /// <param name="contentEncryptor">a suitable content encryptor.</param>
  46. /// <returns>a PKIArchiveControl object.</returns>
  47. public PkiArchiveControl Build(ICipherBuilderWithKey contentEncryptor)
  48. {
  49. CmsEnvelopedData envContent = envGen.Generate(keyContent, contentEncryptor);
  50. EnvelopedData envD = EnvelopedData.GetInstance(envContent.ContentInfo.Content);
  51. return new PkiArchiveControl(new PkiArchiveOptions(new EncryptedKey(envD)));
  52. }
  53. }
  54. }
  55. #pragma warning restore
  56. #endif