Пример #1
0
        virtual internal void ToStringInternal(TpmStructPrinter p)
        {
            bool enabled = dbg.Enabled;

            dbg.Enabled = false;
            var members = GetFieldsToMarshal();

            dbg.Enabled = enabled;
            foreach (var mem in members)
            {
                MemberInfo memInfo = mem;
                object     memVal  = Globs.GetMember(memInfo, this);
                Type       memType = Globs.GetMemberType(memInfo);
                p.Print(memInfo.Name, Globs.ToCSharpStyle(memType.Name), memVal);
            }
        }
Пример #2
0
        /// <summary>
        /// Implements marshaling logic for most of the TPM object types.
        /// Can be overridden if a custom marshaling logic is required (e.g. when
        /// marshaling of a field depends on other field's value).
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        internal virtual void ToNet(Marshaller m)
        {
            var members = GetFieldsToMarshal();

            dbg.Indent();
            for (int i = 0; i < members.Length; ++i)
            {
                var    mem    = members[i];
                object memVal = Globs.GetMember(mem, this);
                dbg.Trace(i + ": " + mem.Name + " = " + memVal);
                if (mem.SizeLength > 0)
                {
                    bool arr = mem.WireType == MarshalType.VariableLengthArray;
                    int  len = arr ? (memVal == null ? 0 : ((Array)memVal).Length)
                                  : Marshaller.GetTpmRepresentation(memVal).Length;
                    dbg.Trace("Sending " + (arr ? "Array " : "Struct ") + mem.Name + " of size " + len);
                    m.PutSizeTag(len, mem.SizeLength, mem.SizeName);
                }
                m.Put(memVal, mem.Name);
            }
            dbg.Unindent();
        }
Пример #3
0
        protected void InternalWriteXml(XmlWriter w)
        {
            MemberInfo[] fields = GetType().GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);

            string thisObjectName = GetType().Name;

            foreach (MemberInfo f in fields)
            {
                var    mInfo = Globs.GetAttr <MarshalAsAttribute>(f);
                Object obj   = Globs.GetMember(f, this);

                string elementName = f.Name;
                elementName = elementName.TrimStart(new[] { '_' });
                w.WriteStartElement(elementName);
                switch (mInfo.MarshType)
                {
                case MarshalType.UnionSelector:
                case MarshalType.ArrayCount:
                    // ReSharper disable once PossibleNullReferenceException
                    w.WriteString(obj.ToString());
                    break;

                case MarshalType.Normal:
                    // ReSharper disable once CanBeReplacedWithTryCastAndCheckForNull
                    if (obj is TpmStructureBase)
                    {
                        //w.WriteStartElement(GetTypeName(obj));
                        ((TpmStructureBase)obj).InternalWriteXml(w);
                        //w.WriteEndElement();
                    }
                    else
                    {
                        // ReSharper disable once PossibleNullReferenceException
                        w.WriteString(obj.ToString());
                    }
                    break;

                case MarshalType.FixedLengthArray:
                case MarshalType.SpecialVariableLengthArray:
                case MarshalType.VariableLengthArray:
                    // ReSharper disable once PossibleNullReferenceException
                    Type elementType = obj.GetType().GetElementType();
                    var  supportedElementaryTypes = new[] { typeof(byte), typeof(ushort), typeof(uint) };
                    if (supportedElementaryTypes.Contains(elementType))
                    {
                        w.WriteValue(obj);
                    }
                    else
                    {
                        // ReSharper disable once UnusedVariable
                        foreach (Object o in (Array)obj)
                        {
                            // ReSharper disable once PossibleInvalidCastException
                            ((TpmStructureBase)obj).InternalWriteXml(w);
                        }
                    }
                    break;

                case MarshalType.Union:
                    // ReSharper disable once PossibleNullReferenceException
                    string typeName = obj.GetType().ToString();
                    w.WriteAttributeString("type", typeName);

                    //w.WriteStartElement(GetTypeName(obj));
                    ((TpmStructureBase)obj).InternalWriteXml(w);
                    //w.WriteEndElement();
                    break;

                default:
                    throw new NotImplementedException("NOT IMPLEMENTED");
                }
                w.WriteEndElement();
            }
        }
Пример #4
0
        /// <summary>
        /// Implements unmarshaling logic for most of the TPM object types.
        /// Can be overridden if a custom unmarshaling logic is required (e.g.
        /// when unmarshaling of a field depends on other field's value).
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        internal virtual void ToHost(Marshaller m)
        {
            dbg.Indent();
            var  members      = GetFieldsToMarshal(true);
            uint mshlStartPos = m.GetGetPos();

            for (int i = 0; i < members.Length; ++i)
            {
                TpmStructMemberInfo memInfo = members[i];
                Type memType  = Globs.GetMemberType(memInfo);
                var  wireType = memInfo.WireType;
                int  size     = -1;
                switch (wireType)
                {
                case MarshalType.Union:
                {
                    dbg.Trace("Union " + memType.Name +
                              " with selector " + memInfo.Tag.Value);
                    var elt = UnionElementFromSelector(memType, memInfo.Tag.Value);
                    memInfo.Value = m.Get(elt, memType.Name);
                    break;
                }

                case MarshalType.FixedLengthArray:
                {
                    object arr = Globs.GetMember(memInfo, this);
                    memInfo.Value = m.GetArray(memType.GetElementType(),
                                               (arr as Array).Length, memInfo.Name);
                    break;
                }

                case MarshalType.SpecialVariableLengthArray:
                {
                    size = CryptoLib.DigestSize((TpmAlgId)members[i - 1].Value);
                    UnmarshalArray(m, memInfo, memType, size);
                    break;
                }

                case MarshalType.VariableLengthArray:
                {
                    size = m.GetSizeTag(memInfo.SizeLength, memInfo.SizeName);
                    UnmarshalArray(m, memInfo, memType, size);
                    break;
                }

                case MarshalType.EncryptedVariableLengthArray:
                {
                    uint unmarshaled = m.GetGetPos() - mshlStartPos;
                    size = m.SizedStructLen[m.SizedStructLen.Count - 1] - (int)unmarshaled;
                    UnmarshalArray(m, memInfo, memType, size);
                    break;
                }

                case MarshalType.SizedStruct:
                {
                    size = m.GetSizeTag(memInfo.SizeLength, memInfo.SizeName);
                    if (size == 0)
                    {
                        break;
                    }
                    m.SizedStructLen.Add(size);
                    memInfo.Value = m.Get(memType, memInfo.Name);
                    int unmSize = Marshaller.GetTpmRepresentation(memInfo.Value).Length;
                    if (unmSize != size)
                    {
                        if (unmSize < size && memType.Name == "TpmPublic")
                        {
                            var pub     = memInfo.Value as TpmPublic;
                            var label   = Marshaller.GetTpmRepresentation(pub.unique);
                            var context = m.GetArray(typeof(byte), size - unmSize, "")
                                          as byte[];
                            pub.unique = new TpmDerive(label, context);
                        }
                        else
                        {
                            var msg = string.Format("Invalid size {0} (instead of "
                                                    + "{1}) for unmarshaled {2}.{3}",
                                                    unmSize, size, this.GetType(), memInfo.Name);
                            throw new TssException(msg);
                        }
                    }
                    m.SizedStructLen.RemoveAt(m.SizedStructLen.Count - 1);
                    break;
                }

                default:
                    // Only attempt unmarshaling a field, if it is not sized or
                    // if its size is non-zero.
                    if (memInfo.Tag == null ||
                        memInfo.Tag.GetValueAsUInt() != 0)
                    {
                        memInfo.Value = m.Get(memType, memInfo.Name);
                    }
                    break;
                }
                dbg.Trace((i + 1) + ": " + wireType + " " + memInfo.Name +
                          (size != -1 ? " of size " + size : ""));
                // Some property values are dynamically obtained from their linked fields.
                // Correspondingly, they do not have a setter, so we bypass them here.
                Debug.Assert(wireType != MarshalType.LengthOfStruct && wireType != MarshalType.ArrayCount);
                if (wireType != MarshalType.UnionSelector)
                {
                    Globs.SetMember(memInfo, this, memInfo.Value);
                }
            }
            dbg.Unindent();
        }
Пример #5
0
        internal virtual void ToHost(Marshaller m)
        {
            var members = GetFieldsToMarshal(true);

            dbg.Indent();
            for (int i = 0; i < members.Length; ++i)
            {
                TpmStructMemberInfo memInfo = members[i];
                Type memType = Globs.GetMemberType(memInfo);
                var  wt      = members[i].WireType;
                switch (wt)
                {
                case MarshalType.Union:
                {
                    dbg.Trace("Union " + memType.Name + " with selector " + memInfo.Tag.Value);
                    memInfo.Value = m.Get(UnionElementFromSelector(memType, memInfo.Tag.Value), memType.Name);
                    break;
                }

                case MarshalType.FixedLengthArray:
                {
                    object arr = Globs.GetMember(memInfo, this);
                    memInfo.Value = m.GetArray(memType.GetElementType(), (arr as Array).Length, memInfo.Name);
                    break;
                }

                case MarshalType.VariableLengthArray:
                {
                    int size = m.GetSizeTag(memInfo.SizeLength, memInfo.SizeName);
                    memInfo.Value = m.GetArray(memType.GetElementType(), size, memInfo.Name);
                    Debug.Assert(size == ((Array)memInfo.Value).Length);
                    dbg.Trace("Received Array " + memInfo.Name + " of size " + size);
                    break;
                }

                case MarshalType.SizedStruct:
                {
                    int size = m.GetSizeTag(memInfo.SizeLength, memInfo.SizeName);
                    if (size != 0)
                    {
                        memInfo.Value = m.Get(memType, memInfo.Name);
                        Debug.Assert(size == Marshaller.GetTpmRepresentation(memInfo.Value).Length);
                    }
                    dbg.Trace("Received Struct " + memInfo.Name + " of size " + size);
                    break;
                }

                default:
                    // Only attempt unmarshaling a field, if it is not sized or
                    // if its size is non-zero.
                    if (memInfo.Tag == null ||
                        memInfo.Tag.GetValueAsUInt() != 0)
                    {
                        memInfo.Value = m.Get(memType, memInfo.Name);
                    }
                    break;
                }
                dbg.Trace((i + 1) + ": " + memInfo.Name + " = " + memInfo.Value);
                // Some property values are dynamically obtained from their linked fields.
                // Correspondingly, they do not have a setter, so we bypass them here.
                Debug.Assert(wt != MarshalType.LengthOfStruct && wt != MarshalType.ArrayCount);
                if (wt != MarshalType.UnionSelector)
                {
                    Globs.SetMember(memInfo, this, memInfo.Value);
                }
            }
            dbg.Unindent();
        }