Esempio n. 1
0
    public Stream Open(Stream outStream, string contentOID, string compressionOID)
    {
        BerSequenceGenerator berSequenceGenerator = new BerSequenceGenerator(outStream);

        berSequenceGenerator.AddObject(CmsObjectIdentifiers.CompressedData);
        BerSequenceGenerator berSequenceGenerator2 = new BerSequenceGenerator(berSequenceGenerator.GetRawOutputStream(), 0, isExplicit: true);

        berSequenceGenerator2.AddObject(new DerInteger(0));
        berSequenceGenerator2.AddObject(new AlgorithmIdentifier(new DerObjectIdentifier("1.2.840.113549.1.9.16.3.8")));
        BerSequenceGenerator berSequenceGenerator3 = new BerSequenceGenerator(berSequenceGenerator2.GetRawOutputStream());

        berSequenceGenerator3.AddObject(new DerObjectIdentifier(contentOID));
        Stream output = CmsUtilities.CreateBerOctetOutputStream(berSequenceGenerator3.GetRawOutputStream(), 0, isExplicit: true, _bufferSize);

        return(new CmsCompressedOutputStream(new ZOutputStream(output, -1), berSequenceGenerator, berSequenceGenerator2, berSequenceGenerator3));
    }
Esempio n. 2
0
    public Stream Open(Stream outStream, string signedContentType, bool encapsulate, Stream dataOutputStream)
    {
        if (outStream == null)
        {
            throw new ArgumentNullException("outStream");
        }
        if (!outStream.CanWrite)
        {
            throw new ArgumentException("Expected writeable stream", "outStream");
        }
        if (dataOutputStream != null && !dataOutputStream.CanWrite)
        {
            throw new ArgumentException("Expected writeable stream", "dataOutputStream");
        }
        _messageDigestsLocked = true;
        BerSequenceGenerator berSequenceGenerator = new BerSequenceGenerator(outStream);

        berSequenceGenerator.AddObject(CmsObjectIdentifiers.SignedData);
        BerSequenceGenerator berSequenceGenerator2 = new BerSequenceGenerator(berSequenceGenerator.GetRawOutputStream(), 0, isExplicit: true);
        DerObjectIdentifier  derObjectIdentifier   = (signedContentType == null) ? null : new DerObjectIdentifier(signedContentType);

        berSequenceGenerator2.AddObject(CalculateVersion(derObjectIdentifier));
        Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector();

        foreach (string messageDigestOid in _messageDigestOids)
        {
            asn1EncodableVector.Add(new AlgorithmIdentifier(new DerObjectIdentifier(messageDigestOid), DerNull.Instance));
        }
        byte[] encoded = new DerSet(asn1EncodableVector).GetEncoded();
        berSequenceGenerator2.GetRawOutputStream().Write(encoded, 0, encoded.Length);
        BerSequenceGenerator berSequenceGenerator3 = new BerSequenceGenerator(berSequenceGenerator2.GetRawOutputStream());

        berSequenceGenerator3.AddObject(derObjectIdentifier);
        Stream s = encapsulate ? CmsUtilities.CreateBerOctetOutputStream(berSequenceGenerator3.GetRawOutputStream(), 0, isExplicit: true, _bufferSize) : null;
        Stream safeTeeOutputStream = GetSafeTeeOutputStream(dataOutputStream, s);
        Stream outStream2          = AttachDigestsToOutputStream(_messageDigests.Values, safeTeeOutputStream);

        return(new CmsSignedDataOutputStream(this, outStream2, signedContentType, berSequenceGenerator, berSequenceGenerator2, berSequenceGenerator3));
    }
 protected Stream Open(Stream outStr, AlgorithmIdentifier macAlgId, ICipherParameters cipherParameters, Asn1EncodableVector recipientInfos)
 {
     try
     {
         BerSequenceGenerator berSequenceGenerator = new BerSequenceGenerator(outStr);
         berSequenceGenerator.AddObject(CmsObjectIdentifiers.AuthenticatedData);
         BerSequenceGenerator berSequenceGenerator2 = new BerSequenceGenerator(berSequenceGenerator.GetRawOutputStream(), 0, isExplicit: true);
         berSequenceGenerator2.AddObject(new DerInteger(AuthenticatedData.CalculateVersion(null)));
         Stream        rawOutputStream = berSequenceGenerator2.GetRawOutputStream();
         Asn1Generator asn1Generator   = _berEncodeRecipientSet ? ((Asn1Generator) new BerSetGenerator(rawOutputStream)) : ((Asn1Generator) new DerSetGenerator(rawOutputStream));
         foreach (Asn1Encodable recipientInfo in recipientInfos)
         {
             asn1Generator.AddObject(recipientInfo);
         }
         asn1Generator.Close();
         berSequenceGenerator2.AddObject(macAlgId);
         BerSequenceGenerator berSequenceGenerator3 = new BerSequenceGenerator(rawOutputStream);
         berSequenceGenerator3.AddObject(CmsObjectIdentifiers.Data);
         Stream output = CmsUtilities.CreateBerOctetOutputStream(berSequenceGenerator3.GetRawOutputStream(), 0, isExplicit: false, _bufferSize);
         IMac   mac    = MacUtilities.GetMac(macAlgId.Algorithm);
         mac.Init(cipherParameters);
         Stream macStream = new TeeOutputStream(output, new MacOutputStream(mac));
         return(new CmsAuthenticatedDataOutputStream(macStream, mac, berSequenceGenerator, berSequenceGenerator2, berSequenceGenerator3));
     }
     catch (SecurityUtilityException e)
     {
         throw new CmsException("couldn't create cipher.", e);
     }
     catch (InvalidKeyException e2)
     {
         throw new CmsException("key invalid in message.", e2);
     }
     catch (IOException e3)
     {
         throw new CmsException("exception decoding algorithm parameters.", e3);
     }
 }
Esempio n. 4
0
 private Stream Open(Stream outStream, AlgorithmIdentifier encAlgID, ICipherParameters cipherParameters, Asn1EncodableVector recipientInfos)
 {
     try
     {
         BerSequenceGenerator berSequenceGenerator = new BerSequenceGenerator(outStream);
         berSequenceGenerator.AddObject(CmsObjectIdentifiers.EnvelopedData);
         BerSequenceGenerator berSequenceGenerator2 = new BerSequenceGenerator(berSequenceGenerator.GetRawOutputStream(), 0, isExplicit: true);
         berSequenceGenerator2.AddObject(Version);
         Stream        rawOutputStream = berSequenceGenerator2.GetRawOutputStream();
         Asn1Generator asn1Generator   = _berEncodeRecipientSet ? ((Asn1Generator) new BerSetGenerator(rawOutputStream)) : ((Asn1Generator) new DerSetGenerator(rawOutputStream));
         foreach (Asn1Encodable recipientInfo in recipientInfos)
         {
             asn1Generator.AddObject(recipientInfo);
         }
         asn1Generator.Close();
         BerSequenceGenerator berSequenceGenerator3 = new BerSequenceGenerator(rawOutputStream);
         berSequenceGenerator3.AddObject(CmsObjectIdentifiers.Data);
         berSequenceGenerator3.AddObject(encAlgID);
         Stream          stream = CmsUtilities.CreateBerOctetOutputStream(berSequenceGenerator3.GetRawOutputStream(), 0, isExplicit: false, _bufferSize);
         IBufferedCipher cipher = CipherUtilities.GetCipher(encAlgID.Algorithm);
         cipher.Init(forEncryption: true, new ParametersWithRandom(cipherParameters, rand));
         CipherStream outStream2 = new CipherStream(stream, null, cipher);
         return(new CmsEnvelopedDataOutputStream(this, outStream2, berSequenceGenerator, berSequenceGenerator2, berSequenceGenerator3));
     }
     catch (SecurityUtilityException e)
     {
         throw new CmsException("couldn't create cipher.", e);
     }
     catch (InvalidKeyException e2)
     {
         throw new CmsException("key invalid in message.", e2);
     }
     catch (IOException e3)
     {
         throw new CmsException("exception decoding algorithm parameters.", e3);
     }
 }