Dan Albert | 056ad0e | 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 | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 17 | #define TRACE_TAG ADB |
Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 18 | |
| 19 | #include "sysdeps.h" |
| 20 | #include "adb_auth.h" |
Dan Albert | 056ad0e | 2015-02-18 17:47:33 -0800 | [diff] [blame] | 21 | |
| 22 | #include <errno.h> |
| 23 | #include <stdio.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <unistd.h> |
| 27 | |
| 28 | #include "adb.h" |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 29 | #include "transport.h" |
Dan Albert | 056ad0e | 2015-02-18 17:47:33 -0800 | [diff] [blame] | 30 | |
Elliott Hughes | d7eb854 | 2015-06-17 15:23:42 -0700 | [diff] [blame] | 31 | bool auth_required = true; |
Dan Albert | 432ffe2 | 2015-02-18 18:22:45 -0800 | [diff] [blame] | 32 | |
Dan Albert | 056ad0e | 2015-02-18 17:47:33 -0800 | [diff] [blame] | 33 | void send_auth_request(atransport *t) |
| 34 | { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 35 | D("Calling send_auth_request"); |
Dan Albert | 056ad0e | 2015-02-18 17:47:33 -0800 | [diff] [blame] | 36 | apacket *p; |
| 37 | int ret; |
| 38 | |
| 39 | ret = adb_auth_generate_token(t->token, sizeof(t->token)); |
| 40 | if (ret != sizeof(t->token)) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 41 | D("Error generating token ret=%d", ret); |
Dan Albert | 056ad0e | 2015-02-18 17:47:33 -0800 | [diff] [blame] | 42 | return; |
| 43 | } |
| 44 | |
| 45 | p = get_apacket(); |
| 46 | memcpy(p->data, t->token, ret); |
| 47 | p->msg.command = A_AUTH; |
| 48 | p->msg.arg0 = ADB_AUTH_TOKEN; |
| 49 | p->msg.data_length = ret; |
| 50 | send_packet(p, t); |
| 51 | } |
| 52 | |
| 53 | void send_auth_response(uint8_t *token, size_t token_size, atransport *t) |
| 54 | { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 55 | D("Calling send_auth_response"); |
Dan Albert | 056ad0e | 2015-02-18 17:47:33 -0800 | [diff] [blame] | 56 | apacket *p = get_apacket(); |
| 57 | int ret; |
| 58 | |
| 59 | ret = adb_auth_sign(t->key, token, token_size, p->data); |
| 60 | if (!ret) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 61 | D("Error signing the token"); |
Dan Albert | 056ad0e | 2015-02-18 17:47:33 -0800 | [diff] [blame] | 62 | put_apacket(p); |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | p->msg.command = A_AUTH; |
| 67 | p->msg.arg0 = ADB_AUTH_SIGNATURE; |
| 68 | p->msg.data_length = ret; |
| 69 | send_packet(p, t); |
| 70 | } |
| 71 | |
| 72 | void send_auth_publickey(atransport *t) |
| 73 | { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 74 | D("Calling send_auth_publickey"); |
Elliott Hughes | e0a6e2a | 2016-05-26 22:43:19 -0700 | [diff] [blame] | 75 | std::string key = adb_auth_get_userkey(); |
| 76 | if (key.empty()) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 77 | D("Failed to get user public key"); |
Dan Albert | 056ad0e | 2015-02-18 17:47:33 -0800 | [diff] [blame] | 78 | return; |
| 79 | } |
| 80 | |
Elliott Hughes | e0a6e2a | 2016-05-26 22:43:19 -0700 | [diff] [blame] | 81 | if (key.size() >= MAX_PAYLOAD_V1) { |
| 82 | D("User public key too large (%zu B)", key.size()); |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | apacket* p = get_apacket(); |
| 87 | memcpy(p->data, key.c_str(), key.size() + 1); |
| 88 | |
Dan Albert | 056ad0e | 2015-02-18 17:47:33 -0800 | [diff] [blame] | 89 | p->msg.command = A_AUTH; |
| 90 | p->msg.arg0 = ADB_AUTH_RSAPUBLICKEY; |
Elliott Hughes | e0a6e2a | 2016-05-26 22:43:19 -0700 | [diff] [blame] | 91 | p->msg.data_length = key.size(); |
Dan Albert | 056ad0e | 2015-02-18 17:47:33 -0800 | [diff] [blame] | 92 | send_packet(p, t); |
| 93 | } |
| 94 | |
| 95 | void adb_auth_verified(atransport *t) |
| 96 | { |
| 97 | handle_online(t); |
| 98 | send_connect(t); |
| 99 | } |