MaxstARSDK  3.5.0
SignatureAuthManager.cs
1 /*==============================================================================
2 Copyright 2017 Maxst, Inc. All Rights Reserved.
3 ==============================================================================*/
4 
5 using UnityEngine;
6 using System.Collections;
7 using System.IO;
8 using System.Runtime.InteropServices;
9 using System.Collections.Generic;
10 using System.Linq;
11 using System.Text;
12 using System;
13 using System.Xml;
14 
15 /*
16  * This class should be run Windows runtime and Mac runtime only (Should not call in Editor mode)
17  */
18 namespace maxstAR
19 {
20  public class SignatureAuthManager
21  {
22 
23  private static SignatureAuthManager instance = null;
24 
25  public static SignatureAuthManager getInstance()
26  {
27  if (instance == null)
28  {
29  instance = new SignatureAuthManager();
30  }
31 
32  return instance;
33  }
34 
35  private const int SIGNATURE_MAX_LEN = 128;
36  private const string APP_SIGNATURE_FILE_NAME = "ApplicationSignature.xml";
37 
38  private string applicationId;
39 
40  /*
41  * Create current application id as hash string.
42  * If ApplicationSignature.xml file does not exist create that file and set current application Id.
43  * Read xml file and extract application id.
44  * If app id is not same set application id and delete signature.
45  * Save xml string to ApplicationSignature.xml file.
46  */
47  public void setAppIdAndSignature()
48  {
49  makeAppIdFromAssemblyDll();
50  createSignatureFileIfNeeded();
51  resetAppIdAndSignature();
52  setSignatureFromFile();
53  }
54 
55  private void makeAppIdFromAssemblyDll()
56  {
57  string assemblyFilePath = null;
58 
59  if (Application.platform == RuntimePlatform.WindowsPlayer)
60  {
61  assemblyFilePath = Application.dataPath + "/Managed/" + "Assembly-CSharp.dll";
62  }
63  else if (Application.platform == RuntimePlatform.OSXPlayer)
64  {
65  assemblyFilePath = Application.dataPath + "/Resources/Data/Managed/" + "Assembly-CSharp.dll";
66  }
67 
68  // Read Assembly-CSharp.dll file
69  FileStream assemblyFS = File.Open(assemblyFilePath, FileMode.Open);
70  byte[] assemblyBytes = new byte[1024];
71  assemblyFS.Read(assemblyBytes, 0, assemblyBytes.Length);
72  assemblyFS.Close();
73 
74  int size = Marshal.SizeOf(assemblyBytes[0]) * assemblyBytes.Length;
75 
76  IntPtr unmanagedArrayPtr = Marshal.AllocHGlobal(size);
77 
78  try
79  {
80  // Copy the array to unmanaged memory.
81  Marshal.Copy(assemblyBytes, 0, unmanagedArrayPtr, assemblyBytes.Length);
82  }
83  finally
84  {
85 
86  }
87 
88  byte[] appIdBytes = Enumerable.Repeat((byte)0x00, SIGNATURE_MAX_LEN).ToArray();
89  int[] appIdLength = new int[1];
90 
91  // Use common hash function of native dll
92  // TODO : Block PC version signature authentication
93  //MaxstARAPI.createAppId(unmanagedArrayPtr, assemblyBytes.Length, appIdBytes, appIdLength);
94 
95  byte[] newAppIdBytes = new byte[appIdLength[0]];
96 
97  Array.Copy(appIdBytes, newAppIdBytes, appIdLength[0]);
98 
99  applicationId = Encoding.Default.GetString(newAppIdBytes);
100 
101  Marshal.FreeHGlobal(unmanagedArrayPtr);
102  }
103 
104  private void createSignatureFileIfNeeded()
105  {
106  if (!File.Exists(Directory.GetCurrentDirectory() + "/" + APP_SIGNATURE_FILE_NAME))
107  {
108  XmlDocument doc = new XmlDocument();
109  XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
110  doc.AppendChild(docNode);
111 
112  XmlNode rootNode = doc.CreateElement("MaxstARSDK");
113  doc.AppendChild(rootNode);
114 
115  XmlNode appIdElement = doc.CreateElement("AppId");
116  appIdElement.InnerText = applicationId;
117  rootNode.AppendChild(appIdElement);
118 
119  XmlNode appSigElement = doc.CreateElement("AppSignature");
120  appSigElement.InnerText = "_Invalid_Signature_";
121  rootNode.AppendChild(appSigElement);
122 
123  doc.Save(Directory.GetCurrentDirectory() + "/" + APP_SIGNATURE_FILE_NAME);
124  }
125  }
126 
127  private void resetAppIdAndSignature()
128  {
129  XmlDocument xmldoc = new XmlDocument();
130  xmldoc.Load(Directory.GetCurrentDirectory() + "/" + APP_SIGNATURE_FILE_NAME);
131  XmlElement docRoot = xmldoc.DocumentElement;
132 
133  XmlNode appIdNode = docRoot.SelectSingleNode("AppId");
134  XmlNode appSigNode = docRoot.SelectSingleNode("AppSignature");
135 
136  string previousAppId = appIdNode.InnerText;
137 
138  if (String.Compare(previousAppId, applicationId) != 0)
139  {
140  appIdNode.InnerText = applicationId;
141  appSigNode.InnerText = "_Invalid_Signature_";
142  xmldoc.Save(Directory.GetCurrentDirectory() + "/" + APP_SIGNATURE_FILE_NAME);
143  }
144  }
145 
146  private void setSignatureFromFile()
147  {
148  XmlDocument xmldoc = new XmlDocument();
149  xmldoc.Load(Directory.GetCurrentDirectory() + "/" + APP_SIGNATURE_FILE_NAME);
150  XmlElement docRoot = xmldoc.DocumentElement;
151 
152  XmlNode appIdNode = docRoot.SelectSingleNode("AppId");
153  XmlNode appSigNode = docRoot.SelectSingleNode("AppSignature");
154 
155  string signature = appSigNode.InnerText;
156  if (signature != null && signature.Length > 0)
157  {
158  // Pass app signature to native dll
159  // TODO : Block PC version signature authentication
160  //MaxstARAPI.SetAppSignature(signature);
161  }
162  }
163  }
164 }