Dan Albert | ba3a251 | 2015-02-18 17:47:33 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | #define TRACE_TAG TRACE_ADB |
| 18 | |
| 19 | #include <errno.h> |
| 20 | #include <stdio.h> |
| 21 | #include <string.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <unistd.h> |
| 24 | |
| 25 | #include "adb.h" |
| 26 | #include "adb_auth.h" |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame^] | 27 | #include "transport.h" |
Dan Albert | ba3a251 | 2015-02-18 17:47:33 -0800 | [diff] [blame] | 28 | #include "sysdeps.h" |
| 29 | |
Dan Albert | bd0b750 | 2015-02-18 18:22:45 -0800 | [diff] [blame] | 30 | int auth_enabled = 0; |
| 31 | |
Dan Albert | ba3a251 | 2015-02-18 17:47:33 -0800 | [diff] [blame] | 32 | void send_auth_request(atransport *t) |
| 33 | { |
| 34 | D("Calling send_auth_request\n"); |
| 35 | apacket *p; |
| 36 | int ret; |
| 37 | |
| 38 | ret = adb_auth_generate_token(t->token, sizeof(t->token)); |
| 39 | if (ret != sizeof(t->token)) { |
| 40 | D("Error generating token ret=%d\n", ret); |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | p = get_apacket(); |
| 45 | memcpy(p->data, t->token, ret); |
| 46 | p->msg.command = A_AUTH; |
| 47 | p->msg.arg0 = ADB_AUTH_TOKEN; |
| 48 | p->msg.data_length = ret; |
| 49 | send_packet(p, t); |
| 50 | } |
| 51 | |
| 52 | void send_auth_response(uint8_t *token, size_t token_size, atransport *t) |
| 53 | { |
| 54 | D("Calling send_auth_response\n"); |
| 55 | apacket *p = get_apacket(); |
| 56 | int ret; |
| 57 | |
| 58 | ret = adb_auth_sign(t->key, token, token_size, p->data); |
| 59 | if (!ret) { |
| 60 | D("Error signing the token\n"); |
| 61 | put_apacket(p); |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | p->msg.command = A_AUTH; |
| 66 | p->msg.arg0 = ADB_AUTH_SIGNATURE; |
| 67 | p->msg.data_length = ret; |
| 68 | send_packet(p, t); |
| 69 | } |
| 70 | |
| 71 | void send_auth_publickey(atransport *t) |
| 72 | { |
| 73 | D("Calling send_auth_publickey\n"); |
| 74 | apacket *p = get_apacket(); |
| 75 | int ret; |
| 76 | |
| 77 | ret = adb_auth_get_userkey(p->data, sizeof(p->data)); |
| 78 | if (!ret) { |
| 79 | D("Failed to get user public key\n"); |
| 80 | put_apacket(p); |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | p->msg.command = A_AUTH; |
| 85 | p->msg.arg0 = ADB_AUTH_RSAPUBLICKEY; |
| 86 | p->msg.data_length = ret; |
| 87 | send_packet(p, t); |
| 88 | } |
| 89 | |
| 90 | void adb_auth_verified(atransport *t) |
| 91 | { |
| 92 | handle_online(t); |
| 93 | send_connect(t); |
| 94 | } |