1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) 2 /* 3 * Sample code to test CAP protocol 4 * 5 * This file is provided under a dual BSD/GPLv2 license. When using or 6 * redistributing this file, you may do so under either license. 7 * 8 * GPL LICENSE SUMMARY 9 * 10 * Copyright(c) 2016 Google Inc. All rights reserved. 11 * Copyright(c) 2016 Linaro Ltd. All rights reserved. 12 * 13 * This program is free software; you can redistribute it and/or modify 14 * it under the terms of version 2 of the GNU General Public License as 15 * published by the Free Software Foundation. 16 * 17 * This program is distributed in the hope that it will be useful, but 18 * WITHOUT ANY WARRANTY; without even the implied warranty of 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 20 * General Public License version 2 for more details. 21 * 22 * BSD LICENSE 23 * 24 * Copyright(c) 2016 Google Inc. All rights reserved. 25 * Copyright(c) 2016 Linaro Ltd. All rights reserved. 26 * 27 * Redistribution and use in source and binary forms, with or without 28 * modification, are permitted provided that the following conditions 29 * are met: 30 * 31 * * Redistributions of source code must retain the above copyright 32 * notice, this list of conditions and the following disclaimer. 33 * * Redistributions in binary form must reproduce the above copyright 34 * notice, this list of conditions and the following disclaimer in 35 * the documentation and/or other materials provided with the 36 * distribution. 37 * * Neither the name of Google Inc. or Linaro Ltd. nor the names of 38 * its contributors may be used to endorse or promote products 39 * derived from this software without specific prior written 40 * permission. 41 * 42 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 43 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 44 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 45 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC. OR 46 * LINARO LTD. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 47 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 48 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 49 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 50 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 51 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 52 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 53 */ 54 55 #include <stdio.h> 56 #include <string.h> 57 #include <unistd.h> 58 #include <sys/ioctl.h> 59 #include <sys/stat.h> 60 #include <fcntl.h> 61 62 #include "../../greybus_authentication.h" 63 64 struct cap_ioc_get_endpoint_uid uid; 65 struct cap_ioc_get_ims_certificate cert = { 66 .certificate_class = 0, 67 .certificate_id = 0, 68 }; 69 70 struct cap_ioc_authenticate authenticate = { 71 .auth_type = 0, 72 .challenge = {0}, 73 }; 74 75 int main(int argc, char *argv[]) 76 { 77 unsigned int timeout = 10000; 78 char *capdev; 79 int fd, ret; 80 81 /* Make sure arguments are correct */ 82 if (argc != 2) { 83 printf("\nUsage: ./firmware <Path of the gb-cap-X dev>\n"); 84 return 0; 85 } 86 87 capdev = argv[1]; 88 89 printf("Opening %s authentication device\n", capdev); 90 91 fd = open(capdev, O_RDWR); 92 if (fd < 0) { 93 printf("Failed to open: %s\n", capdev); 94 return -1; 95 } 96 97 /* Get UID */ 98 printf("Get UID\n"); 99 100 ret = ioctl(fd, CAP_IOC_GET_ENDPOINT_UID, &uid); 101 if (ret < 0) { 102 printf("Failed to get UID: %s (%d)\n", capdev, ret); 103 ret = -1; 104 goto close_fd; 105 } 106 107 printf("UID received: 0x%llx\n", *(unsigned long long int *)(uid.uid)); 108 109 /* Get certificate */ 110 printf("Get IMS certificate\n"); 111 112 ret = ioctl(fd, CAP_IOC_GET_IMS_CERTIFICATE, &cert); 113 if (ret < 0) { 114 printf("Failed to get IMS certificate: %s (%d)\n", capdev, ret); 115 ret = -1; 116 goto close_fd; 117 } 118 119 printf("IMS Certificate size: %d\n", cert.cert_size); 120 121 /* Authenticate */ 122 printf("Authenticate module\n"); 123 124 memcpy(authenticate.uid, uid.uid, 8); 125 126 ret = ioctl(fd, CAP_IOC_AUTHENTICATE, &authenticate); 127 if (ret < 0) { 128 printf("Failed to authenticate module: %s (%d)\n", capdev, ret); 129 ret = -1; 130 goto close_fd; 131 } 132 133 printf("Authenticated, result (%02x), sig-size (%02x)\n", 134 authenticate.result_code, authenticate.signature_size); 135 136 close_fd: 137 close(fd); 138 139 return ret; 140 } 141