summary refs log tree commit diff
path: root/Crypto/src/tsp/TimeStampRequestGenerator.cs
blob: 2c698e47627213bbd3bb57b70d85ff30adc68ebd (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
using System;
using System.Collections;

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

using Org.BouncyCastle.Utilities;

namespace Org.BouncyCastle.Tsp
{
	/**
	 * Generator for RFC 3161 Time Stamp Request objects.
	 */
	public class TimeStampRequestGenerator
	{
		private DerObjectIdentifier reqPolicy;

		private DerBoolean certReq;

		private IDictionary extensions = Platform.CreateHashtable();
        private IList       extOrdering = Platform.CreateArrayList();

		public void SetReqPolicy(
			string reqPolicy)
		{
			this.reqPolicy = new DerObjectIdentifier(reqPolicy);
		}

		public void SetCertReq(
			bool certReq)
		{
			this.certReq = DerBoolean.GetInstance(certReq);
		}

		/**
		 * add a given extension field for the standard extensions tag (tag 3)
		 * @throws IOException
		 */
		[Obsolete("Use method taking DerObjectIdentifier")]
		public void AddExtension(
			string			oid,
			bool			critical,
			Asn1Encodable	value)
		{
			this.AddExtension(oid, critical, value.GetEncoded());
		}

		/**
		* add a given extension field for the standard extensions tag
		* The value parameter becomes the contents of the octet string associated
		* with the extension.
		*/
		[Obsolete("Use method taking DerObjectIdentifier")]
		public void AddExtension(
			string	oid,
			bool	critical,
			byte[]	value)
		{
			DerObjectIdentifier derOid = new DerObjectIdentifier(oid);
			extensions[derOid] = new X509Extension(critical, new DerOctetString(value));
			extOrdering.Add(derOid);
		}

		/**
		 * add a given extension field for the standard extensions tag (tag 3)
		 * @throws IOException
		 */
		public virtual void AddExtension(
			DerObjectIdentifier	oid,
			bool				critical,
			Asn1Encodable 		extValue)
		{
			this.AddExtension(oid, critical, extValue.GetEncoded());
		}

		/**
		 * add a given extension field for the standard extensions tag
		 * The value parameter becomes the contents of the octet string associated
		 * with the extension.
		 */
		public virtual void AddExtension(
			DerObjectIdentifier	oid,
			bool				critical,
			byte[]				extValue)
		{
			extensions.Add(oid, new X509Extension(critical, new DerOctetString(extValue)));
			extOrdering.Add(oid);
		}

		public TimeStampRequest Generate(
			string	digestAlgorithm,
			byte[]	digest)
		{
			return this.Generate(digestAlgorithm, digest, null);
		}

		public TimeStampRequest Generate(
			string		digestAlgorithmOid,
			byte[]		digest,
			BigInteger	nonce)
		{
			if (digestAlgorithmOid == null)
			{
				throw new ArgumentException("No digest algorithm specified");
			}

			DerObjectIdentifier digestAlgOid = new DerObjectIdentifier(digestAlgorithmOid);

			AlgorithmIdentifier algID = new AlgorithmIdentifier(digestAlgOid, DerNull.Instance);
			MessageImprint messageImprint = new MessageImprint(algID, digest);

			X509Extensions  ext = null;

			if (extOrdering.Count != 0)
			{
				ext = new X509Extensions(extOrdering, extensions);
			}

			DerInteger derNonce = nonce == null
				?	null
				:	new DerInteger(nonce);

			return new TimeStampRequest(
				new TimeStampReq(messageImprint, reqPolicy, derNonce, certReq, ext));
		}

		public virtual TimeStampRequest Generate(DerObjectIdentifier digestAlgorithm, byte[] digest)
		{
			return Generate(digestAlgorithm.Id, digest);
		}

		public virtual TimeStampRequest Generate(DerObjectIdentifier digestAlgorithm, byte[] digest, BigInteger nonce)
		{
			return Generate(digestAlgorithm.Id, digest, nonce);
		}
	}
}