summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--crypto/src/asn1/cms/ecc/MQVuserKeyingMaterial.cs63
1 files changed, 26 insertions, 37 deletions
diff --git a/crypto/src/asn1/cms/ecc/MQVuserKeyingMaterial.cs b/crypto/src/asn1/cms/ecc/MQVuserKeyingMaterial.cs
index 047017351..68a6367e5 100644
--- a/crypto/src/asn1/cms/ecc/MQVuserKeyingMaterial.cs
+++ b/crypto/src/asn1/cms/ecc/MQVuserKeyingMaterial.cs
@@ -1,10 +1,8 @@
 using System;
 
-using Org.BouncyCastle.Utilities;
-
 namespace Org.BouncyCastle.Asn1.Cms.Ecc
 {
-	public class MQVuserKeyingMaterial
+    public class MQVuserKeyingMaterial
 		: Asn1Encodable
 	{
         public static MQVuserKeyingMaterial GetInstance(object obj)
@@ -21,44 +19,34 @@ namespace Org.BouncyCastle.Asn1.Cms.Ecc
             return new MQVuserKeyingMaterial(Asn1Sequence.GetInstance(obj, isExplicit));
         }
 
-        private OriginatorPublicKey	ephemeralPublicKey;
-		private Asn1OctetString		addedukm;
 
-		public MQVuserKeyingMaterial(
-			OriginatorPublicKey	ephemeralPublicKey,
-			Asn1OctetString		addedukm)
-		{
-			// TODO Check ephemeralPublicKey not null
+        private readonly OriginatorPublicKey m_ephemeralPublicKey;
+		private readonly Asn1OctetString m_addedukm;
 
-			this.ephemeralPublicKey = ephemeralPublicKey;
-			this.addedukm = addedukm;
+        public MQVuserKeyingMaterial(OriginatorPublicKey ephemeralPublicKey, Asn1OctetString addedukm)
+        {
+            m_ephemeralPublicKey = ephemeralPublicKey ?? throw new ArgumentNullException(nameof(ephemeralPublicKey));
+			m_addedukm = addedukm;
 		}
 
-		private MQVuserKeyingMaterial(
-			Asn1Sequence seq)
+		private MQVuserKeyingMaterial(Asn1Sequence seq)
 		{
-			// TODO Check seq has either 1 or 2 elements
+            int count = seq.Count, pos = 0;
+            if (count < 1 || count > 2)
+                throw new ArgumentException("Bad sequence size: " + count, nameof(seq));
 
-			this.ephemeralPublicKey = OriginatorPublicKey.GetInstance(seq[0]);
+            m_ephemeralPublicKey = OriginatorPublicKey.GetInstance(seq[pos++]);
+			m_addedukm = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 0, true, Asn1OctetString.GetInstance);
 
-			if (seq.Count > 1)
-			{
-				this.addedukm = Asn1OctetString.GetInstance(
-					(Asn1TaggedObject)seq[1], true);
-			}
+            if (pos != count)
+                throw new ArgumentException("Unexpected elements in sequence", nameof(seq));
 		}
 
-        public OriginatorPublicKey EphemeralPublicKey
-		{
-			get { return ephemeralPublicKey; }
-		}
+        public OriginatorPublicKey EphemeralPublicKey => m_ephemeralPublicKey;
 
-		public Asn1OctetString AddedUkm
-		{
-			get { return addedukm; }
-		}
+		public Asn1OctetString AddedUkm => m_addedukm;
 
-		/**
+        /**
 		* Produce an object suitable for an Asn1OutputStream.
 		* <pre>
 		* MQVuserKeyingMaterial ::= SEQUENCE {
@@ -66,11 +54,12 @@ namespace Org.BouncyCastle.Asn1.Cms.Ecc
 		*   addedukm [0] EXPLICIT UserKeyingMaterial OPTIONAL  }
 		* </pre>
 		*/
-		public override Asn1Object ToAsn1Object()
-		{
-			Asn1EncodableVector v = new Asn1EncodableVector(ephemeralPublicKey);
-            v.AddOptionalTagged(true, 0, addedukm);
-			return new DerSequence(v);
-		}
-	}
+        public override Asn1Object ToAsn1Object()
+        {
+            Asn1EncodableVector v = new Asn1EncodableVector(2);
+            v.Add(m_ephemeralPublicKey);
+            v.AddOptionalTagged(true, 0, m_addedukm);
+            return new DerSequence(v);
+        }
+    }
 }