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 | |
Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 17 | #define TRACE_TAG ADB |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 18 | |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 19 | #include "adb.h" |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 20 | #include "adb_auth.h" |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 21 | #include "transport.h" |
Dan Albert | ba3a251 | 2015-02-18 17:47:33 -0800 | [diff] [blame] | 22 | |
| 23 | #include <errno.h> |
| 24 | #include <stdio.h> |
| 25 | #include <string.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <unistd.h> |
| 28 | |
Elliott Hughes | 5cba504 | 2015-06-17 15:23:42 -0700 | [diff] [blame] | 29 | bool auth_required = true; |
Dan Albert | bd0b750 | 2015-02-18 18:22:45 -0800 | [diff] [blame] | 30 | |
Dan Albert | ba3a251 | 2015-02-18 17:47:33 -0800 | [diff] [blame] | 31 | void send_auth_request(atransport *t) |
| 32 | { |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 33 | LOG(INFO) << "Calling send_auth_request..."; |
Dan Albert | ba3a251 | 2015-02-18 17:47:33 -0800 | [diff] [blame] | 34 | |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 35 | if (!adb_auth_generate_token(t->token, sizeof(t->token))) { |
| 36 | PLOG(ERROR) << "Error generating token"; |
Dan Albert | ba3a251 | 2015-02-18 17:47:33 -0800 | [diff] [blame] | 37 | return; |
| 38 | } |
| 39 | |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 40 | apacket* p = get_apacket(); |
| 41 | memcpy(p->data, t->token, sizeof(t->token)); |
Dan Albert | ba3a251 | 2015-02-18 17:47:33 -0800 | [diff] [blame] | 42 | p->msg.command = A_AUTH; |
| 43 | p->msg.arg0 = ADB_AUTH_TOKEN; |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 44 | p->msg.data_length = sizeof(t->token); |
Dan Albert | ba3a251 | 2015-02-18 17:47:33 -0800 | [diff] [blame] | 45 | send_packet(p, t); |
| 46 | } |
| 47 | |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 48 | static void send_auth_publickey(atransport* t) { |
| 49 | LOG(INFO) << "Calling send_auth_publickey"; |
Dan Albert | ba3a251 | 2015-02-18 17:47:33 -0800 | [diff] [blame] | 50 | |
Elliott Hughes | e8b663f | 2016-05-26 22:43:19 -0700 | [diff] [blame] | 51 | std::string key = adb_auth_get_userkey(); |
| 52 | if (key.empty()) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 53 | D("Failed to get user public key"); |
Dan Albert | ba3a251 | 2015-02-18 17:47:33 -0800 | [diff] [blame] | 54 | return; |
| 55 | } |
| 56 | |
Elliott Hughes | e8b663f | 2016-05-26 22:43:19 -0700 | [diff] [blame] | 57 | if (key.size() >= MAX_PAYLOAD_V1) { |
| 58 | D("User public key too large (%zu B)", key.size()); |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | apacket* p = get_apacket(); |
| 63 | memcpy(p->data, key.c_str(), key.size() + 1); |
| 64 | |
Dan Albert | ba3a251 | 2015-02-18 17:47:33 -0800 | [diff] [blame] | 65 | p->msg.command = A_AUTH; |
| 66 | p->msg.arg0 = ADB_AUTH_RSAPUBLICKEY; |
Josh Gao | 8a0d077 | 2016-08-22 12:50:23 -0700 | [diff] [blame] | 67 | |
| 68 | // adbd expects a null-terminated string. |
| 69 | p->msg.data_length = key.size() + 1; |
Dan Albert | ba3a251 | 2015-02-18 17:47:33 -0800 | [diff] [blame] | 70 | send_packet(p, t); |
| 71 | } |
| 72 | |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 73 | void send_auth_response(uint8_t* token, size_t token_size, atransport* t) { |
Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 74 | std::shared_ptr<RSA> key = t->NextKey(); |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 75 | if (key == nullptr) { |
| 76 | // No more private keys to try, send the public key. |
| 77 | send_auth_publickey(t); |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | LOG(INFO) << "Calling send_auth_response"; |
| 82 | apacket* p = get_apacket(); |
| 83 | |
Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 84 | int ret = adb_auth_sign(key.get(), token, token_size, p->data); |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 85 | if (!ret) { |
| 86 | D("Error signing the token"); |
| 87 | put_apacket(p); |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | p->msg.command = A_AUTH; |
| 92 | p->msg.arg0 = ADB_AUTH_SIGNATURE; |
| 93 | p->msg.data_length = ret; |
| 94 | send_packet(p, t); |
| 95 | } |
| 96 | |
Dan Albert | ba3a251 | 2015-02-18 17:47:33 -0800 | [diff] [blame] | 97 | void adb_auth_verified(atransport *t) |
| 98 | { |
| 99 | handle_online(t); |
| 100 | send_connect(t); |
| 101 | } |