Advertisement
Guest User

TeslaCardEmulationService.java

a guest
Nov 27th, 2019
1,376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.97 KB | None | 0 0
  1. package com.teslamotors.plugins.ble;
  2.  
  3. import android.content.ComponentName;
  4. import android.content.Intent;
  5. import android.content.ServiceConnection;
  6. import android.nfc.cardemulation.HostApduService;
  7. import android.os.Bundle;
  8. import android.os.Handler;
  9. import android.os.IBinder;
  10. import android.os.Message;
  11. import android.os.Messenger;
  12. import android.os.Parcelable;
  13. import android.os.RemoteException;
  14. import com.teslamotors.plugins.ble.p140b.C3295c;
  15. import com.teslamotors.plugins.ble.p140b.C3299e;
  16. import com.teslamotors.plugins.ble.p140b.C3304h;
  17. import com.teslamotors.plugins.client.C3356b;
  18. import java.io.ByteArrayOutputStream;
  19. import java.io.IOException;
  20. import java.util.ArrayList;
  21. import java.util.Arrays;
  22.  
  23. public class TeslaCardEmulationService extends HostApduService {
  24.     /* access modifiers changed from: private */
  25.  
  26.     /* renamed from: c */
  27.     public static final String f9558c = "TeslaCardEmulationService";
  28.  
  29.     /* renamed from: d */
  30.     private static final byte[] f9559d = {111, 0};
  31.  
  32.     /* renamed from: e */
  33.     private static final byte[] f9560e = {110, 0};
  34.  
  35.     /* renamed from: f */
  36.     private static final byte[] f9561f = {109, 0};
  37.  
  38.     /* renamed from: g */
  39.     private static final byte[] f9562g = {-112, 0};
  40.  
  41.     /* renamed from: h */
  42.     private static final byte[] f9563h = {109, 0};
  43.  
  44.     /* renamed from: i */
  45.     private static final byte[] f9564i = {107, 0};
  46.  
  47.     /* renamed from: a */
  48.     Messenger f9565a;
  49.  
  50.     /* renamed from: b */
  51.     boolean f9566b;
  52.  
  53.     /* renamed from: j */
  54.     private final Messenger f9567j = new Messenger(new C3277a());
  55.  
  56.     /* renamed from: k */
  57.     private final ServiceConnection f9568k = new ServiceConnection() {
  58.         public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
  59.             C3356b.m16680b(TeslaCardEmulationService.f9558c, "onServiceConnected");
  60.             TeslaCardEmulationService.this.f9565a = new Messenger(iBinder);
  61.             TeslaCardEmulationService.this.f9566b = true;
  62.         }
  63.  
  64.         public void onServiceDisconnected(ComponentName componentName) {
  65.             if (TeslaCardEmulationService.this.f9566b) {
  66.                 TeslaCardEmulationService.this.f9566b = false;
  67.                 C3356b.m16680b(TeslaCardEmulationService.f9558c, "onServiceDisconnected");
  68.             }
  69.             TeslaCardEmulationService.this.f9565a = null;
  70.         }
  71.     };
  72.  
  73.     /* renamed from: com.teslamotors.plugins.ble.TeslaCardEmulationService$a */
  74.     class C3277a extends Handler {
  75.         C3277a() {
  76.         }
  77.  
  78.         public void handleMessage(Message message) {
  79.             Bundle data = message.getData();
  80.             data.setClassLoader(getClass().getClassLoader());
  81.             switch (C3299e.m16415a(message.what)) {
  82.                 case NFCGetPublicKeyBytesInfo:
  83.                     Parcelable parcelable = data.getParcelable(C3299e.NFCGetPublicKeyBytesInfo.mo16523b());
  84.                     if (parcelable != null) {
  85.                         TeslaCardEmulationService.this.m16295a((C3304h) parcelable);
  86.                         return;
  87.                     }
  88.                     C3356b.m16680b(TeslaCardEmulationService.f9558c, "Pub key not ready yet, resend the request");
  89.                     Message obtain = Message.obtain();
  90.                     obtain.what = C3299e.NFCGetPublicKeyBytes.mo16522a();
  91.                     sendMessage(obtain);
  92.                     return;
  93.                 case NFCEncryptWithSharedSecretResponse:
  94.                     Parcelable parcelable2 = data.getParcelable(C3299e.NFCEncryptWithSharedSecretResponse.mo16523b());
  95.                     if (parcelable2 != null) {
  96.                         TeslaCardEmulationService.this.m16294a((C3295c) parcelable2);
  97.                         return;
  98.                     } else {
  99.                         C3356b.m16680b(TeslaCardEmulationService.f9558c, "NFC auth response not ready yet");
  100.                         return;
  101.                     }
  102.                 case RegisterComplete:
  103.                     C3356b.m16680b(TeslaCardEmulationService.f9558c, "Successfully registered to received callbacks from BLE service");
  104.                     return;
  105.                 default:
  106.                     return;
  107.             }
  108.         }
  109.     }
  110.  
  111.     public void onCreate() {
  112.         C3356b.m16680b(f9558c, "onCreate called");
  113.         bindService(new Intent(this, BLEService.class), this.f9568k, 1);
  114.     }
  115.  
  116.     public void onDestroy() {
  117.         if (this.f9566b) {
  118.             this.f9566b = false;
  119.         }
  120.         this.f9565a = null;
  121.         unbindService(this.f9568k);
  122.         C3356b.m16680b(f9558c, "onDestroy called");
  123.     }
  124.  
  125.     public void onDeactivated(int i) {
  126.         String str = f9558c;
  127.         StringBuilder sb = new StringBuilder();
  128.         sb.append("Deactivated: ");
  129.         sb.append(i);
  130.         C3356b.m16680b(str, sb.toString());
  131.     }
  132.  
  133.     /* renamed from: a */
  134.     private static byte[] m16297a(short s) {
  135.         return new byte[]{(byte) (s >> 8), (byte) (s & 255)};
  136.     }
  137.  
  138.     /* renamed from: a */
  139.     private boolean m16296a(Message message) {
  140.         String str = f9558c;
  141.         StringBuilder sb = new StringBuilder();
  142.         sb.append("Sending message:");
  143.         sb.append(C3299e.m16416b(message.what));
  144.         C3356b.m16680b(str, sb.toString());
  145.         boolean z = false;
  146.         if (this.f9566b) {
  147.             Messenger messenger = this.f9565a;
  148.             if (messenger != null) {
  149.                 try {
  150.                     message.replyTo = this.f9567j;
  151.                     messenger.send(message);
  152.                     z = true;
  153.                     String str2 = f9558c;
  154.                     StringBuilder sb2 = new StringBuilder();
  155.                     sb2.append("Sent to Server from client :");
  156.                     sb2.append(this.f9565a.toString());
  157.                     sb2.append("Reply to ");
  158.                     sb2.append(this.f9567j.toString());
  159.                     C3356b.m16680b(str2, sb2.toString());
  160.                     return true;
  161.                 } catch (RemoteException e) {
  162.                     String str3 = f9558c;
  163.                     StringBuilder sb3 = new StringBuilder();
  164.                     sb3.append("Failed to send message - Remote Exception! : ");
  165.                     sb3.append(e.getMessage());
  166.                     C3356b.m16681c(str3, sb3.toString());
  167.                     return z;
  168.                 }
  169.             }
  170.         }
  171.         C3356b.m16680b(f9558c, "Failed to send message - service not bound");
  172.         return false;
  173.     }
  174.  
  175.     /* access modifiers changed from: private */
  176.     /* renamed from: a */
  177.     public void m16295a(C3304h hVar) {
  178.         byte[] b = hVar.mo16537b();
  179.         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  180.         if (b != null) {
  181.             try {
  182.                 byteArrayOutputStream.write(b);
  183.                 byteArrayOutputStream.write(f9562g);
  184.             } catch (IOException e) {
  185.                 String str = f9558c;
  186.                 StringBuilder sb = new StringBuilder();
  187.                 sb.append("IOException adding public key to array");
  188.                 sb.append(e.getMessage());
  189.                 C3356b.m16681c(str, sb.toString());
  190.             }
  191.         } else {
  192.             byteArrayOutputStream.write(m16297a(28435));
  193.         }
  194.         sendResponseApdu(byteArrayOutputStream.toByteArray());
  195.         String str2 = f9558c;
  196.         StringBuilder sb2 = new StringBuilder();
  197.         sb2.append("APDU Response (Get Pub key):");
  198.         sb2.append(byteArrayOutputStream.toByteArray().toString());
  199.         C3356b.m16680b(str2, sb2.toString());
  200.         String str3 = f9558c;
  201.         StringBuilder sb3 = new StringBuilder();
  202.         sb3.append("APDU Response (Get Pub key):Length =");
  203.         sb3.append(byteArrayOutputStream.size());
  204.         C3356b.m16680b(str3, sb3.toString());
  205.     }
  206.  
  207.     /* access modifiers changed from: private */
  208.     /* renamed from: a */
  209.     public void m16294a(C3295c cVar) {
  210.         byte[] a = cVar.mo16505a();
  211.         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  212.         if (a != null) {
  213.             try {
  214.                 byteArrayOutputStream.write(Arrays.copyOfRange(a, 0, 16));
  215.                 byteArrayOutputStream.write(f9562g);
  216.             } catch (IOException e) {
  217.                 String str = f9558c;
  218.                 StringBuilder sb = new StringBuilder();
  219.                 sb.append("Exception adding encrypted response to the message");
  220.                 sb.append(e.getMessage());
  221.                 C3356b.m16681c(str, sb.toString());
  222.             }
  223.         } else {
  224.             byteArrayOutputStream.write(m16297a(28442));
  225.         }
  226.         sendResponseApdu(byteArrayOutputStream.toByteArray());
  227.         String str2 = f9558c;
  228.         StringBuilder sb2 = new StringBuilder();
  229.         sb2.append("APDU Response (Get Auth response):");
  230.         sb2.append(byteArrayOutputStream.toByteArray().toString());
  231.         C3356b.m16680b(str2, sb2.toString());
  232.         String str3 = f9558c;
  233.         StringBuilder sb3 = new StringBuilder();
  234.         sb3.append("APDU Response (Get Auth response):Length =");
  235.         sb3.append(byteArrayOutputStream.size());
  236.         C3356b.m16680b(str3, sb3.toString());
  237.     }
  238.  
  239.     public byte[] processCommandApdu(byte[] bArr, Bundle bundle) {
  240.         C3356b.m16680b(f9558c, "Called Tesla Logic applet");
  241.         byte[] bArr2 = f9562g;
  242.         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  243.         byte b = bArr[1];
  244.         if (b == -92) {
  245.             bArr2 = f9562g;
  246.             C3356b.m16680b(f9558c, "Called to enable Tesla Logic applet");
  247.         } else if (b != 4) {
  248.             if (b != 17) {
  249.                 if (b == 20) {
  250.                     try {
  251.                         byteArrayOutputStream.write(m16297a(49));
  252.                     } catch (IOException e) {
  253.                         String str = f9558c;
  254.                         StringBuilder sb = new StringBuilder();
  255.                         sb.append("Exception when sending form factor");
  256.                         sb.append(e.getMessage());
  257.                         C3356b.m16681c(str, sb.toString());
  258.                     }
  259.                 } else if (b != 27) {
  260.                     bArr2 = f9563h;
  261.                 } else {
  262.                     C3356b.m16680b(f9558c, "Got Vehicle Info");
  263.                     if (bArr[4] < 30) {
  264.                         byte[] copyOfRange = Arrays.copyOfRange(bArr, 5, bArr[4] + 5);
  265.                         String str2 = f9558c;
  266.                         StringBuilder sb2 = new StringBuilder();
  267.                         sb2.append("From Vehicle Protobuf : ");
  268.                         sb2.append(C3332g.m16627a(copyOfRange));
  269.                         C3356b.m16680b(str2, sb2.toString());
  270.                         Message obtain = Message.obtain();
  271.                         obtain.what = C3299e.NFCProtoMessage.mo16522a();
  272.                         Bundle bundle2 = new Bundle();
  273.                         bundle2.putByteArray(C3299e.NFCProtoMessage.mo16523b(), copyOfRange);
  274.                         obtain.setData(bundle2);
  275.                         if (!m16296a(obtain)) {
  276.                             bArr2 = m16297a(28442);
  277.                         }
  278.                     } else {
  279.                         C3356b.m16680b(f9558c, "Ignore Vehicle proto, too long info");
  280.                     }
  281.                 }
  282.             } else if (((short) bArr[2]) != 0) {
  283.                 bArr2 = f9564i;
  284.             } else if (bArr[4] != 81) {
  285.                 bArr2 = m16297a(28441);
  286.             } else {
  287.                 byte[] copyOfRange2 = Arrays.copyOfRange(bArr, 5, 70);
  288.                 byte[] copyOfRange3 = Arrays.copyOfRange(bArr, 70, 86);
  289.                 String str3 = f9558c;
  290.                 StringBuilder sb3 = new StringBuilder();
  291.                 sb3.append("Requested Vehicle Public Key :");
  292.                 sb3.append(C3332g.m16627a(copyOfRange2));
  293.                 C3356b.m16680b(str3, sb3.toString());
  294.                 String str4 = f9558c;
  295.                 StringBuilder sb4 = new StringBuilder();
  296.                 sb4.append("Requested Vehicle Token :");
  297.                 sb4.append(C3332g.m16627a(copyOfRange3));
  298.                 C3356b.m16680b(str4, sb4.toString());
  299.                 Message obtain2 = Message.obtain();
  300.                 obtain2.what = C3299e.NFCEncryptWithSharedSecret.mo16522a();
  301.                 Bundle bundle3 = new Bundle();
  302.                 ArrayList arrayList = new ArrayList();
  303.                 arrayList.add(C3332g.m16627a(copyOfRange3));
  304.                 arrayList.add(C3332g.m16627a(copyOfRange2));
  305.                 bundle3.putStringArrayList(C3299e.NFCEncryptWithSharedSecret.mo16523b(), arrayList);
  306.                 obtain2.setData(bundle3);
  307.                 if (m16296a(obtain2)) {
  308.                     return null;
  309.                 }
  310.                 bArr2 = m16297a(28442);
  311.             }
  312.         } else if (((short) bArr[2]) != 0) {
  313.             bArr2 = f9564i;
  314.         } else {
  315.             Message obtain3 = Message.obtain();
  316.             obtain3.what = C3299e.NFCGetPublicKeyBytes.mo16522a();
  317.             if (m16296a(obtain3)) {
  318.                 return null;
  319.             }
  320.             bArr2 = m16297a(28435);
  321.         }
  322.         try {
  323.             byteArrayOutputStream.write(bArr2);
  324.         } catch (IOException e2) {
  325.             String str5 = f9558c;
  326.             StringBuilder sb5 = new StringBuilder();
  327.             sb5.append("Exception adding status code");
  328.             sb5.append(e2.getMessage());
  329.             C3356b.m16681c(str5, sb5.toString());
  330.         }
  331.         String str6 = f9558c;
  332.         StringBuilder sb6 = new StringBuilder();
  333.         sb6.append("APDU Response :");
  334.         sb6.append(byteArrayOutputStream.toByteArray().toString());
  335.         C3356b.m16680b(str6, sb6.toString());
  336.         String str7 = f9558c;
  337.         StringBuilder sb7 = new StringBuilder();
  338.         sb7.append("APDU Response :Length =");
  339.         sb7.append(byteArrayOutputStream.size());
  340.         C3356b.m16680b(str7, sb7.toString());
  341.         return byteArrayOutputStream.toByteArray();
  342.     }
  343. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement