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));
		}
示例#2
0
		public TimeStampReq(
			MessageImprint		messageImprint,
			DerObjectIdentifier	tsaPolicy,
			DerInteger			nonce,
			DerBoolean			certReq,
			X509Extensions		extensions)
		{
			// default
			this.version = new DerInteger(1);

			this.messageImprint = messageImprint;
			this.tsaPolicy = tsaPolicy;
			this.nonce = nonce;
			this.certReq = certReq;
			this.extensions = extensions;
		}
示例#3
0
		private TimeStampReq(
			Asn1Sequence seq)
		{
			int nbObjects = seq.Count;
			int seqStart = 0;

			// version
			version = DerInteger.GetInstance(seq[seqStart++]);

			// messageImprint
			messageImprint = MessageImprint.GetInstance(seq[seqStart++]);

			for (int opt = seqStart; opt < nbObjects; opt++)
			{
				// tsaPolicy
				if (seq[opt] is DerObjectIdentifier)
				{
					tsaPolicy = DerObjectIdentifier.GetInstance(seq[opt]);
				}
				// nonce
				else if (seq[opt] is DerInteger)
				{
					nonce = DerInteger.GetInstance(seq[opt]);
				}
				// certReq
				else if (seq[opt] is DerBoolean)
				{
					certReq = DerBoolean.GetInstance(seq[opt]);
				}
				// extensions
				else if (seq[opt] is Asn1TaggedObject)
				{
					Asn1TaggedObject tagged = (Asn1TaggedObject) seq[opt];
					if (tagged.TagNo == 0)
					{
						extensions = X509Extensions.GetInstance(tagged, false);
					}
				}
			}
		}
		//------------------------------------------------------------------------------

		public TimeStampToken Generate(
			TimeStampRequest	request,
			BigInteger			serialNumber,
			DateTime			genTime)
		{
			DerObjectIdentifier digestAlgOID = new DerObjectIdentifier(request.MessageImprintAlgOid);

			AlgorithmIdentifier algID = new AlgorithmIdentifier(digestAlgOID, DerNull.Instance);
			MessageImprint messageImprint = new MessageImprint(algID, request.GetMessageImprintDigest());

			Accuracy accuracy = null;
			if (accuracySeconds > 0 || accuracyMillis > 0 || accuracyMicros > 0)
			{
				DerInteger seconds = null;
				if (accuracySeconds > 0)
				{
					seconds = new DerInteger(accuracySeconds);
				}

				DerInteger millis = null;
				if (accuracyMillis > 0)
				{
					millis = new DerInteger(accuracyMillis);
				}

				DerInteger micros = null;
				if (accuracyMicros > 0)
				{
					micros = new DerInteger(accuracyMicros);
				}

				accuracy = new Accuracy(seconds, millis, micros);
			}

			DerBoolean derOrdering = null;
			if (ordering)
			{
				derOrdering = DerBoolean.GetInstance(ordering);
			}

			DerInteger nonce = null;
			if (request.Nonce != null)
			{
				nonce = new DerInteger(request.Nonce);
			}

			DerObjectIdentifier tsaPolicy = new DerObjectIdentifier(tsaPolicyOID);
			if (request.ReqPolicy != null)
			{
				tsaPolicy = new DerObjectIdentifier(request.ReqPolicy);
			}

			TstInfo tstInfo = new TstInfo(tsaPolicy, messageImprint,
				new DerInteger(serialNumber), new DerGeneralizedTime(genTime), accuracy,
				derOrdering, nonce, tsa, request.Extensions);

			try
			{
				CmsSignedDataGenerator signedDataGenerator = new CmsSignedDataGenerator();

				byte[] derEncodedTstInfo = tstInfo.GetDerEncoded();

				if (request.CertReq)
				{
					signedDataGenerator.AddCertificates(x509Certs);
				}

				signedDataGenerator.AddCrls(x509Crls);
				signedDataGenerator.AddSigner(key, cert, digestOID, signedAttr, unsignedAttr);

				CmsSignedData signedData = signedDataGenerator.Generate(
					PkcsObjectIdentifiers.IdCTTstInfo.Id,
					new CmsProcessableByteArray(derEncodedTstInfo),
					true);

				return new TimeStampToken(signedData);
			}
			catch (CmsException cmsEx)
			{
				throw new TspException("Error generating time-stamp token", cmsEx);
			}
			catch (IOException e)
			{
				throw new TspException("Exception encoding info", e);
			}
			catch (X509StoreException e)
			{
				throw new TspException("Exception handling CertStore", e);
			}
//			catch (InvalidAlgorithmParameterException e)
//			{
//				throw new TspException("Exception handling CertStore CRLs", e);
//			}
		}
示例#5
0
		private TstInfo(
			Asn1Sequence seq)
		{
			IEnumerator e = seq.GetEnumerator();

			// version
			e.MoveNext();
			version = DerInteger.GetInstance(e.Current);

			// tsaPolicy
			e.MoveNext();
			tsaPolicyId = DerObjectIdentifier.GetInstance(e.Current);

			// messageImprint
			e.MoveNext();
			messageImprint = MessageImprint.GetInstance(e.Current);

			// serialNumber
			e.MoveNext();
			serialNumber = DerInteger.GetInstance(e.Current);

			// genTime
			e.MoveNext();
			genTime = DerGeneralizedTime.GetInstance(e.Current);

			// default for ordering
			ordering = DerBoolean.False;

			while (e.MoveNext())
			{
				Asn1Object o = (Asn1Object) e.Current;

				if (o is Asn1TaggedObject)
				{
					DerTaggedObject tagged = (DerTaggedObject) o;

					switch (tagged.TagNo)
					{
						case 0:
							tsa = GeneralName.GetInstance(tagged, true);
							break;
						case 1:
							extensions = X509Extensions.GetInstance(tagged, false);
							break;
						default:
							throw new ArgumentException("Unknown tag value " + tagged.TagNo);
					}
				}

				if (o is DerSequence)
				{
					accuracy = Accuracy.GetInstance(o);
				}

				if (o is DerBoolean)
				{
					ordering = DerBoolean.GetInstance(o);
				}

				if (o is DerInteger)
				{
					nonce = DerInteger.GetInstance(o);
				}
			}
		}
示例#6
0
		public TstInfo(
			DerObjectIdentifier	tsaPolicyId,
			MessageImprint		messageImprint,
			DerInteger			serialNumber,
			DerGeneralizedTime	genTime,
			Accuracy			accuracy,
			DerBoolean			ordering,
			DerInteger			nonce,
			GeneralName			tsa,
			X509Extensions		extensions)
		{
			this.version = new DerInteger(1);
			this.tsaPolicyId = tsaPolicyId;
			this.messageImprint = messageImprint;
			this.serialNumber = serialNumber;
			this.genTime = genTime;
			this.accuracy = accuracy;
			this.ordering = ordering;
			this.nonce = nonce;
			this.tsa = tsa;
			this.extensions = extensions;
		}
示例#7
0
        private TstInfo(
            Asn1Sequence seq)
        {
            IEnumerator e = seq.GetEnumerator();

            // version
            e.MoveNext();
            version = DerInteger.GetInstance(e.Current);

            // tsaPolicy
            e.MoveNext();
            tsaPolicyId = DerObjectIdentifier.GetInstance(e.Current);

            // messageImprint
            e.MoveNext();
            messageImprint = MessageImprint.GetInstance(e.Current);

            // serialNumber
            e.MoveNext();
            serialNumber = DerInteger.GetInstance(e.Current);

            // genTime
            e.MoveNext();
            genTime = DerGeneralizedTime.GetInstance(e.Current);

            // default for ordering
            ordering = DerBoolean.False;

            while (e.MoveNext())
            {
                Asn1Object o = (Asn1Object)e.Current;

                if (o is Asn1TaggedObject)
                {
                    DerTaggedObject tagged = (DerTaggedObject)o;

                    switch (tagged.TagNo)
                    {
                    case 0:
                        tsa = GeneralName.GetInstance(tagged, true);
                        break;

                    case 1:
                        extensions = X509Extensions.GetInstance(tagged, false);
                        break;

                    default:
                        throw new ArgumentException("Unknown tag value " + tagged.TagNo);
                    }
                }

                if (o is DerSequence)
                {
                    accuracy = Accuracy.GetInstance(o);
                }

                if (o is DerBoolean)
                {
                    ordering = DerBoolean.GetInstance(o);
                }

                if (o is DerInteger)
                {
                    nonce = DerInteger.GetInstance(o);
                }
            }
        }