mirrored from https://www.bouncycastle.org/repositories/bc-csharp
-
Notifications
You must be signed in to change notification settings - Fork 602
Expand file tree
/
Copy pathCMSEnvelopedDataParser.cs
More file actions
152 lines (133 loc) · 5.72 KB
/
Copy pathCMSEnvelopedDataParser.cs
File metadata and controls
152 lines (133 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System.IO;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Cms;
using Org.BouncyCastle.Asn1.X509;
namespace Org.BouncyCastle.Cms
{
/**
* Parsing class for an CMS Enveloped Data object from an input stream.
* <p>
* Note: that because we are in a streaming mode only one recipient can be tried and it is important
* that the methods on the parser are called in the appropriate order.
* </p>
* <p>
* Example of use - assuming the first recipient matches the private key we have.
* <pre>
* CmsEnvelopedDataParser ep = new CmsEnvelopedDataParser(inputStream);
*
* RecipientInformationStore recipients = ep.GetRecipientInfos();
*
* Collection c = recipients.GetRecipients();
* Iterator it = c.iterator();
*
* if (it.hasNext())
* {
* RecipientInformation recipient = (RecipientInformation)it.next();
*
* CMSTypedStream recData = recipient.GetContentStream(privateKey);
*
* processDataStream(recData.GetContentStream());
* }
* </pre>
* Note: this class does not introduce buffering - if you are processing large files you should create
* the parser with:
* <pre>
* CmsEnvelopedDataParser ep = new CmsEnvelopedDataParser(new BufferedInputStream(inputStream, bufSize));
* </pre>
* where bufSize is a suitably large buffer size.
* </p>
* <p>
* <b>Stream handling note:</b>
* <ul>
* <li>The constructor reads only enough of the supplied Stream to expose the
* CMS structure metadata (originator info, recipient infos, content-encryption
* algorithm). The encrypted content is drained lazily by the caller via
* {@link RecipientInformation#GetContentStream} /
* {@link RecipientInformation#GetContent}.</li>
* <li>The supplied Stream is <b>not closed automatically</b>. Call
* {@link #Close()} on this parser (inherited from
* {@link CmsContentInfoParser}) to close the underlying Stream, or close
* it yourself.</li>
* </ul>
* </p>
*/
public class CmsEnvelopedDataParser
: CmsContentInfoParser
{
internal RecipientInformationStore recipientInfoStore;
internal EnvelopedDataParser envelopedData;
private AlgorithmIdentifier _encAlg;
private Asn1.Cms.AttributeTable _unprotectedAttributes;
private bool _attrNotRead;
private OriginatorInformation m_originatorInformation;
public CmsEnvelopedDataParser(byte[] envelopedData)
: this(new MemoryStream(envelopedData, false))
{
}
public CmsEnvelopedDataParser(Stream envelopedData)
: base(envelopedData)
{
this._attrNotRead = true;
this.envelopedData = new EnvelopedDataParser(
(Asn1SequenceParser)this.contentInfo.GetContent(Asn1Tags.Sequence));
// TODO Validate version?
//DerInteger version = this.envelopedData.Version;
var originatorInfo = this.envelopedData.GetOriginatorInfo();
m_originatorInformation = originatorInfo == null ? null : new OriginatorInformation(originatorInfo);
//
// read the recipients
//
Asn1Set recipientInfos = Asn1Set.GetInstance(this.envelopedData.GetRecipientInfos().ToAsn1Object());
//
// read the encrypted content info
//
EncryptedContentInfoParser encInfo = this.envelopedData.GetEncryptedContentInfo();
this._encAlg = encInfo.ContentEncryptionAlgorithm;
CmsReadable readable = new CmsProcessableInputStream(
((Asn1OctetStringParser)encInfo.GetEncryptedContent(Asn1Tags.OctetString)).GetOctetStream());
CmsSecureReadable secureReadable = new CmsEnvelopedHelper.CmsEnvelopedSecureReadable(
this._encAlg, readable);
//
// build the RecipientInformationStore
//
this.recipientInfoStore = CmsEnvelopedHelper.BuildRecipientInformationStore(
recipientInfos, secureReadable);
}
public AlgorithmIdentifier EncryptionAlgorithmID => _encAlg;
/**
* return the object identifier for the content encryption algorithm.
*/
public string EncryptionAlgOid => _encAlg.Algorithm.GetID();
/**
* return the ASN.1 encoded encryption algorithm parameters, or null if there aren't any.
*/
public Asn1Object EncryptionAlgParams => _encAlg.Parameters?.ToAsn1Object();
/**
* Return the originator information associated with this message if present.
*
* @return OriginatorInformation, null if not present.
*/
public OriginatorInformation OriginatorInformation => m_originatorInformation;
/**
* return a store of the intended recipients for this message
*/
public RecipientInformationStore GetRecipientInfos() => this.recipientInfoStore;
/**
* return a table of the unprotected attributes indexed by the OID of the attribute.
* @throws IOException
*/
public Asn1.Cms.AttributeTable GetUnprotectedAttributes()
{
if (_unprotectedAttributes == null && _attrNotRead)
{
Asn1SetParser asn1Set = this.envelopedData.GetUnprotectedAttrs();
_attrNotRead = false;
if (asn1Set != null)
{
_unprotectedAttributes = CmsUtilities.ParseAttributeTable(asn1Set);
}
}
return _unprotectedAttributes;
}
}
}