summary refs log tree commit diff
path: root/crypto/src/tsp/TimeStampRequest.cs
blob: 8acf9367f6d1774e6d74d5db5cbe41873f433262 (plain) (blame)
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
153
154
155
156
157
158
159
using System;
using System.Collections.Generic;
using System.IO;

using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Cmp;
using Org.BouncyCastle.Asn1.Tsp;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.X509;

namespace Org.BouncyCastle.Tsp
{
	/**
	 * Base class for an RFC 3161 Time Stamp Request.
	 */
	public class TimeStampRequest
		: X509ExtensionBase
	{
		private TimeStampReq req;

		public TimeStampRequest(
			TimeStampReq req)
		{
			this.req = req;
		}

		/**
		* Create a TimeStampRequest from the past in byte array.
		*
		* @param req byte array containing the request.
		* @throws IOException if the request is malformed.
		*/
		public TimeStampRequest(
			byte[] req)
			: this(new Asn1InputStream(req))
		{
		}

		/**
		* Create a TimeStampRequest from the past in input stream.
		*
		* @param in input stream containing the request.
		* @throws IOException if the request is malformed.
		*/
		public TimeStampRequest(
			Stream input)
			: this(new Asn1InputStream(input))
		{
		}

		private TimeStampRequest(
			Asn1InputStream str)
		{
			try
			{
				this.req = TimeStampReq.GetInstance(str.ReadObject());
			}
			catch (InvalidCastException e)
			{
				throw new IOException("malformed request: " + e);
			}
			catch (ArgumentException e)
			{
				throw new IOException("malformed request: " + e);
			}
		}

		public int Version
		{
            get { return req.Version.IntValueExact; }
		}

		public string MessageImprintAlgOid
		{
            get { return req.MessageImprint.HashAlgorithm.Algorithm.Id; }
		}

		public byte[] GetMessageImprintDigest()
		{
			return req.MessageImprint.GetHashedMessage();
		}

		public string ReqPolicy
		{
			get
			{
				return req.ReqPolicy == null
					?	null
					:	req.ReqPolicy.Id;
			}
		}

		public BigInteger Nonce
		{
			get
			{
				return req.Nonce == null
					?	null
					:	req.Nonce.Value;
			}
		}

		public bool CertReq
		{
			get
			{
				return req.CertReq == null
					?	false
					:	req.CertReq.IsTrue;
			}
		}

		/**
		* Validate the timestamp request, checking the digest to see if it is of an
		* accepted type and whether it is of the correct length for the algorithm specified.
		*
		* @param algorithms a set of string OIDS giving accepted algorithms.
		* @param policies if non-null a set of policies we are willing to sign under.
		* @param extensions if non-null a set of extensions we are willing to accept.
		* @throws TspException if the request is invalid, or processing fails.
		*/
		public void Validate(IList<string> algorithms, IList<string> policies, IList<string> extensions)
		{
			if (!algorithms.Contains(this.MessageImprintAlgOid))
				throw new TspValidationException("request contains unknown algorithm", PkiFailureInfo.BadAlg);

            if (policies != null && this.ReqPolicy != null && !policies.Contains(this.ReqPolicy))
				throw new TspValidationException("request contains unknown policy", PkiFailureInfo.UnacceptedPolicy);

            if (this.Extensions != null && extensions != null)
			{
				foreach (DerObjectIdentifier oid in this.Extensions.ExtensionOids)
				{
					if (!extensions.Contains(oid.Id))
						throw new TspValidationException("request contains unknown extension", PkiFailureInfo.UnacceptedExtension);
				}
			}

			int digestLength = TspUtil.GetDigestLength(this.MessageImprintAlgOid);

			if (digestLength != this.GetMessageImprintDigest().Length)
				throw new TspValidationException("imprint digest the wrong length", PkiFailureInfo.BadDataFormat);
		}

		/**
		 * return the ASN.1 encoded representation of this object.
		 */
		public byte[] GetEncoded() => req.GetEncoded();

		internal X509Extensions Extensions => req.Extensions;

		public virtual bool HasExtensions => Extensions != null;

		public virtual IList<DerObjectIdentifier> GetExtensionOids() => TspUtil.GetExtensionOids(Extensions);

		protected override X509Extensions GetX509Extensions() => Extensions;
	}
}