1 //------------------------------------------------------------------------------
2 // <copyright file="DesigntimeLicenseContextSerializer.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //------------------------------------------------------------------------------
6 
7 namespace System.ComponentModel.Design {
8     using System.Runtime.Remoting;
9     using System.Runtime.Serialization.Formatters.Binary;
10     using System.Runtime.Serialization;
11     using System.Security;
12     using System.Security.Permissions;
13     using System.Collections;
14     using System.ComponentModel;
15     using System.Diagnostics;
16     using System;
17     using Microsoft.Win32;
18     using System.IO;
19     using System.Diagnostics.CodeAnalysis;
20 
21     /// <devdoc>
22     ///    <para>
23     ///       Provides support for design-time license context serialization.
24     ///    </para>
25     /// </devdoc>
26     [HostProtection(SharedState = true)]
27     [System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.InheritanceDemand, Name = "FullTrust")]
28     [System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.LinkDemand, Name="FullTrust")]
29     public class DesigntimeLicenseContextSerializer {
30 
31         // not creatable...
32         //
DesigntimeLicenseContextSerializer()33         private DesigntimeLicenseContextSerializer() {
34         }
35 
36         /// <devdoc>
37         ///    <para>
38         ///       Serializes the licenses within the specified design-time license context
39         ///       using the specified key and output stream.
40         ///    </para>
41         /// </devdoc>
Serialize(Stream o, string cryptoKey, DesigntimeLicenseContext context)42         public static void Serialize(Stream o, string cryptoKey, DesigntimeLicenseContext context) {
43             IFormatter formatter = new BinaryFormatter();
44             formatter.Serialize(o, new object[] {cryptoKey, context.savedLicenseKeys});
45         }
46 
47         [SuppressMessage("Microsoft.Security", "CA2107:ReviewDenyAndPermitOnlyUsage")] // Use of PermitOnly here is appropriate. This was a previous war-approved security bug fix.
Deserialize(Stream o, string cryptoKey, RuntimeLicenseContext context)48         internal static void Deserialize(Stream o, string cryptoKey, RuntimeLicenseContext context) {
49             IFormatter formatter = new BinaryFormatter();
50 
51             object obj = formatter.Deserialize(o);
52 
53             if (obj is object[]) {
54                 object[] value = (object[])obj;
55                 if (value[0] is string && (string)value[0] == cryptoKey) {
56                     context.savedLicenseKeys = (Hashtable)value[1];
57                 }
58             }
59         }
60     }
61 }
62