예제 #1
0
파일: Program.cs 프로젝트: tes001/TSS.MSR
        static bool GetOwnerAuthFromOS(out byte[] ownerAuth)
        {
            ownerAuth = new byte[0];

            // open context
            TbsWrapper.TBS_CONTEXT_PARAMS contextParams;
            UIntPtr tbsContext = UIntPtr.Zero;

            contextParams.Version = TbsWrapper.TBS_CONTEXT_VERSION.TWO;
            contextParams.Flags   = TbsWrapper.TBS_CONTEXT_CREATE_FLAGS.IncludeTpm20;
            TbsWrapper.TBS_RESULT result = TbsWrapper.NativeMethods.Tbsi_Context_Create(ref contextParams, ref tbsContext);

            if (result != TbsWrapper.TBS_RESULT.TBS_SUCCESS)
            {
                return(false);
            }
            if (tbsContext == UIntPtr.Zero)
            {
                return(false);
            }

            // get owner auth size
            uint ownerAuthSize = 0;

            TbsWrapper.TBS_OWNERAUTH_TYPE ownerType = TbsWrapper.TBS_OWNERAUTH_TYPE.TBS_OWNERAUTH_TYPE_STORAGE_20;
            result = TbsWrapper.NativeMethods.Tbsi_Get_OwnerAuth(tbsContext, ownerType, ownerAuth, ref ownerAuthSize);
            if (result != TbsWrapper.TBS_RESULT.TBS_SUCCESS &&
                result != TbsWrapper.TBS_RESULT.TBS_E_INSUFFICIENT_BUFFER)
            {
                ownerType = TbsWrapper.TBS_OWNERAUTH_TYPE.TBS_OWNERAUTH_TYPE_FULL;
                result    = TbsWrapper.NativeMethods.Tbsi_Get_OwnerAuth(tbsContext, ownerType, ownerAuth, ref ownerAuthSize);
                if (result != TbsWrapper.TBS_RESULT.TBS_SUCCESS &&
                    result != TbsWrapper.TBS_RESULT.TBS_E_INSUFFICIENT_BUFFER)
                {
                    Console.WriteLine(Globs.GetResourceString("Failed to get ownerAuth."));
                    return(false);
                }
            }
            // get owner auth itself
            ownerAuth = new byte[ownerAuthSize];
            result    = TbsWrapper.NativeMethods.Tbsi_Get_OwnerAuth(tbsContext, ownerType, ownerAuth, ref ownerAuthSize);
            if (result != TbsWrapper.TBS_RESULT.TBS_SUCCESS)
            {
                Console.WriteLine(Globs.GetResourceString("Failed to get ownerAuth."));
                return(false);
            }

            TbsWrapper.NativeMethods.Tbsip_Context_Close(tbsContext);

            return(true);
        }