예제 #1
0
            /// <summary>
            /// Tries to read a previously written object.
            /// </summary>
            /// <param name="already">The available object if it exists.</param>
            /// <returns>
            /// The read state. When <see cref="ReadState.Success"/> is false, the object must be read
            /// and <see cref="ReadState.SetReadResult(T)"/> must be called.
            /// </returns>
            public ReadState TryRead(out T already)
            {
                byte b = _r.ReadByte();

                switch (b)
                {
                case 0: already = default(T); return(new ReadState());

                case 1: already = _objects[_r.ReadNonNegativeSmallInt32()]; return(new ReadState(1));

                default: already = default(T); return(new ReadState(this, b));
                }
            }
예제 #2
0
        internal ECKey(ISystemClock clock, ICKBinaryReader r)
            : base(clock, r)
        {
            r.ReadByte();
            Oid?curveName = HelperAndExtensions.ReadNullableOid(r);

            Debug.Assert(curveName != null);
            _parameters = new ECParameters()
            {
                Curve = ECCurve.CreateFromOid(curveName),
                D     = ReadBytes(r),
                Q     = new ECPoint()
                {
                    X = ReadBytes(r), Y = ReadBytes(r)
                }
            };
            if (r.ReadBoolean())
            {
                _ecdh = ECDiffieHellman.Create(_parameters);
            }
            else
            {
                _ec = ECDsa.Create(_parameters);
            }
            JWKCurveName = GetJWKCurveName(_parameters.Curve.Oid);
 /// <summary>
 /// Reads a key previously written by <see cref="Write(ICKBinaryWriter, KeyBase)"/>.
 /// </summary>
 /// <param name="clock">The system clock.</param>
 /// <param name="r">The reader.</param>
 /// <returns>The key.</returns>
 public static KeyBase Read(ISystemClock clock, ICKBinaryReader r)
 {
     r.ReadByte();
     return(r.ReadChar() switch
     {
         'R' => new RSAKey(clock, r),
         'S' => new SymmetricKey(clock, r),
         'E' => new ECKey(clock, r),
         _ => throw new InvalidDataException("Invalid Key type."),
     });
예제 #4
0
 public KeyRequirement(ICKBinaryReader r)
 {
     r.ReadByte(); // Version.
     KeyType       = r.ReadEnum <KeyType>();
     Operations    = r.ReadEnum <KeyOperations>();
     KeySizeInBits = r.ReadNullableInt32(  );
     CurveName     = HelperAndExtensions.ReadNullableOid(r);
     if (r.ReadBoolean())
     {
         InitiatorAlgorithmName = r.ReadString();
     }
 }
예제 #5
0
        /// <summary>
        /// Reads a <see cref="CKTraitContext"/> that has been previously written by <see cref="Write"/>.
        /// </summary>
        /// <param name="r">The binary reader to use.</param>
        public static CKTraitContext Read(ICKBinaryReader r)
        {
            byte vS       = r.ReadByte();
            bool shared   = (vS & 128) != 0;
            bool withTags = (vS & 64) != 0;

            var name      = shared ? r.ReadSharedString() : r.ReadString();
            var sep       = r.ReadChar();
            var tagReader = withTags ? r : null;

            return(shared ? Bind(name, sep, tagReader) : new CKTraitContext(name, sep, false, tagReader));
        }
 private protected KeyBase(ISystemClock clock, ICKBinaryReader r)
 {
     Clock = clock;
     r.ReadByte();
     Kty          = r.ReadEnum <KeyType>();
     Kid          = JsonEncodedText.Encode(r.ReadString());
     Operations   = r.ReadEnum <KeyOperations>();
     CreationDate = r.ReadDateTime();
     BestBefore   = r.ReadDateTime();
     MaxUseCount  = r.ReadUInt32();
     _oUseCount   = r.ReadInt32();
 }
예제 #7
0
 internal RSAKey(ISystemClock clock, ICKBinaryReader r)
     : base(clock, r)
 {
     r.ReadByte();
     _parameters = new RSAParameters()
     {
         D        = ReadBytes(r),
         DP       = ReadBytes(r),
         DQ       = ReadBytes(r),
         InverseQ = ReadBytes(r),
         P        = ReadBytes(r),
         Q        = ReadBytes(r),
         Modulus  = ReadBytes(r),
         Exponent = ReadBytes(r)
     };
     _rsa = RSA.Create(_parameters);
예제 #8
0
 private protected AsymmetricKey(ISystemClock clock, ICKBinaryReader r)
     : base(clock, r)
 {
     r.ReadByte(); // Version
 }
 internal SymmetricKey(ISystemClock clock, ICKBinaryReader r)
     : base(clock, r)
 {
     r.ReadByte();
     _key = r.ReadBytes(r.ReadNonNegativeSmallInt32());
 }
 /// <summary>
 /// Simple deserialization constructor.
 /// </summary>
 /// <param name="r">The reader.</param>
 public OneTimePassword(ICKBinaryReader r)
 {
     r.ReadByte(); // Version
     Password   = r.ReadString();
     Expiration = r.ReadDateTime();
 }