1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//==================================================================================================
18//
19// Module Name: FactBootEnc
20//
21// General Description: Factory bootstrap encoder. Converts special bytes from parm.txt to their HEX
22//                      presentation for the factory bootstrap.
23//
24//==================================================================================================
25
26package com.mot.dm.core;
27
28import java.io.*;
29import com.mot.dm.io.Node;
30import com.mot.dm.io.NodeLoader;
31import com.mot.dm.tool.Util;
32
33
34public class FactBootEnc {
35  //GUID
36  public static final String GUID_HEX_BOOTSTRAP =
37      "Factory_bootstrap_settings_encoding_HEX:";
38
39  public static final String SEP = File.separator;
40
41  // HEX encoding
42  public static final String DMACC_FACTORY_BOOTSTRAP_CLIENTPW_HEX =
43      "EBE8EFEEECEC";
44  public static final String DMACC_FACTORY_BOOTSTRAP_SERVERPW_HEX =
45      "FCE9E2E4E0E0";
46  public static final String DMACC_FACTORY_BOOTSTRAP_USERNAME_HEX =
47      "E0E5E7EAEBEB";
48
49  //for DM 1.1.2    ./SyncML/DMAcc/[]/....
50  public static String BOOTSTRAP_ROOT_112 = SEP.concat("Dmt").concat(SEP).concat("SyncML").concat(SEP).concat("DMAcc").concat(SEP);
51  public static String CLIENTPW_NODE_112 = "ClientPW".concat(SEP).concat("parm.txt");
52  public static String SERVERPW_NODE_112 = "ServerPW".concat(SEP).concat("parm.txt");
53  public static String USERNAME_NODE_112 = "UserName".concat(SEP).concat("parm.txt");
54
55  //for DM 1.2    ./DMAcc/[]/AppAuth/[]/....
56  public static String BOOTSTRAP_ROOT_12 = SEP.concat("Dmt").concat(SEP).concat("DMAcc").concat(SEP);
57  public static String BOOTSTRAP_MID_12 = "AppAuth".concat(SEP);
58  public static String CLIENTPW_NODE_12 = "AAuthSecret".concat(SEP).concat("parm.txt");
59  public static String SERVERPW_NODE_12 = "AAuthSecret".concat(SEP).concat("parm.txt");
60  public static String USERNAME_NODE_12 = "AAuthName".concat(SEP).concat("parm.txt");
61
62  //returns GUID_HEX_BOOTSTRAP + HEX factory bootstrap value or null in case if
63  //it is  not factory bootstrap
64  public static final String checkForBootstrapValue(String path) throws
65      Exception {
66
67    //for DM 1.1.2
68    if ( path.indexOf(BOOTSTRAP_ROOT_112) > -1) {
69      if (path.endsWith(CLIENTPW_NODE_112) ||
70          path.endsWith(SERVERPW_NODE_112) ||
71          path.endsWith(USERNAME_NODE_112)) {
72        return hexBootstrapValue(path);
73      }
74    }
75    //for DM 1.2
76    else if (path.indexOf(BOOTSTRAP_ROOT_12)  > -1 &&
77                path.indexOf(BOOTSTRAP_MID_12) > -1) {
78      if (path.endsWith(CLIENTPW_NODE_12) ||
79          path.endsWith(SERVERPW_NODE_12) ||
80          path.endsWith(USERNAME_NODE_12)) {
81        return hexBootstrapValue(path);
82      }
83    }
84
85    return null;
86  }
87
88  //returns GUID_HEX_BOOTSTRAP + HEX factory bootstrap value or null in case if
89  //it is  not a factory bootstrap
90  public static final String hexBootstrapValue(String path) throws Exception {
91      Reader reader = null;
92      BufferedReader br = null;
93      String hexEncodedValue = null;
94      try
95    {
96        Node node = NodeLoader.getInstance(path);
97        reader = NodeLoader.getReader(node);
98        br = new BufferedReader(reader);
99        String line;
100        //Read lines from parm.txt
101        while ( (line = br.readLine()) != null) {
102          line = line.toUpperCase();
103                 //check for ClientPW
104          if (line.indexOf(DMACC_FACTORY_BOOTSTRAP_CLIENTPW_HEX) != -1) {
105            hexEncodedValue = DMACC_FACTORY_BOOTSTRAP_CLIENTPW_HEX;
106            break;
107          }      //check for ServerPW
108          else if (line.indexOf(DMACC_FACTORY_BOOTSTRAP_SERVERPW_HEX) != -1) {
109            hexEncodedValue = DMACC_FACTORY_BOOTSTRAP_SERVERPW_HEX;
110            break;
111          }      //check for UserName
112          else if (line.indexOf(DMACC_FACTORY_BOOTSTRAP_USERNAME_HEX) != -1) {
113            hexEncodedValue = DMACC_FACTORY_BOOTSTRAP_USERNAME_HEX;
114            break;
115          }
116        }
117    }
118      catch (Exception e) {
119      throw e;
120    }
121    finally {
122      try {
123        br.close();
124      }
125      catch (Exception e) {}
126
127      try {
128        reader.close();
129      }
130      catch (Exception e) {}
131    }
132
133
134
135
136    if (hexEncodedValue != null) {
137      //factory bootstrap profile found
138      hexEncodedValue = GUID_HEX_BOOTSTRAP.concat(hexEncodedValue);
139    }
140
141    return hexEncodedValue;
142  }
143
144
145  // Test for the functions checkForBootstrapValue() and hexBootstrapValue()
146  public void testMe(String path) throws Exception {
147    File f = new File(path);
148    if (f.isDirectory()) {
149      File[] listFiles = f.listFiles();
150      for (int i = 0; i < listFiles.length; i++) {
151        if (listFiles[i].isDirectory()) {
152          testMe(listFiles[i].getAbsolutePath());
153        }
154        else {
155          String val = checkForBootstrapValue(listFiles[i].getAbsolutePath());
156          if (val != null) {
157            System.out.println("path: " + listFiles[i].getAbsolutePath());
158            System.out.println("val:  " + val + "\n");
159          }
160        }
161      }
162    }
163    else {
164      String val = checkForBootstrapValue(f.getAbsolutePath());
165      if (val != null) {
166        System.out.println("path: " + f.getAbsolutePath());
167        System.out.println("val:  " + val + "\n");
168      }
169    }
170  }
171
172  public static void main(String[] args) throws Exception {
173    if (args.length != 1) {
174      System.out.println("usage:  java FactBootEnc <path>/Dmt");
175      System.exit(1);
176    }
177    FactBootEnc t = new FactBootEnc();
178    t.testMe(args[0]);
179  }
180}
181