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