1 /* 2 * Copyright (c) 2018 Yubico AB. All rights reserved. 3 * Use of this source code is governed by a BSD-style 4 * license that can be found in the LICENSE file. 5 */ 6 7 #include <assert.h> 8 #include <cbor.h> 9 #include <fido.h> 10 #include <string.h> 11 12 #define FAKE_DEV_HANDLE ((void *)0xdeadbeef) 13 14 static const unsigned char cdh[32] = { 15 0xf9, 0x64, 0x57, 0xe7, 0x2d, 0x97, 0xf6, 0xbb, 16 0xdd, 0xd7, 0xfb, 0x06, 0x37, 0x62, 0xea, 0x26, 17 0x20, 0x44, 0x8e, 0x69, 0x7c, 0x03, 0xf2, 0x31, 18 0x2f, 0x99, 0xdc, 0xaf, 0x3e, 0x8a, 0x91, 0x6b, 19 }; 20 21 static const unsigned char authdata[198] = { 22 0x58, 0xc4, 0x49, 0x96, 0x0d, 0xe5, 0x88, 0x0e, 23 0x8c, 0x68, 0x74, 0x34, 0x17, 0x0f, 0x64, 0x76, 24 0x60, 0x5b, 0x8f, 0xe4, 0xae, 0xb9, 0xa2, 0x86, 25 0x32, 0xc7, 0x99, 0x5c, 0xf3, 0xba, 0x83, 0x1d, 26 0x97, 0x63, 0x41, 0x00, 0x00, 0x00, 0x00, 0xf8, 27 0xa0, 0x11, 0xf3, 0x8c, 0x0a, 0x4d, 0x15, 0x80, 28 0x06, 0x17, 0x11, 0x1f, 0x9e, 0xdc, 0x7d, 0x00, 29 0x40, 0x53, 0xfb, 0xdf, 0xaa, 0xce, 0x63, 0xde, 30 0xc5, 0xfe, 0x47, 0xe6, 0x52, 0xeb, 0xf3, 0x5d, 31 0x53, 0xa8, 0xbf, 0x9d, 0xd6, 0x09, 0x6b, 0x5e, 32 0x7f, 0xe0, 0x0d, 0x51, 0x30, 0x85, 0x6a, 0xda, 33 0x68, 0x70, 0x85, 0xb0, 0xdb, 0x08, 0x0b, 0x83, 34 0x2c, 0xef, 0x44, 0xe2, 0x36, 0x88, 0xee, 0x76, 35 0x90, 0x6e, 0x7b, 0x50, 0x3e, 0x9a, 0xa0, 0xd6, 36 0x3c, 0x34, 0xe3, 0x83, 0xe7, 0xd1, 0xbd, 0x9f, 37 0x25, 0xa5, 0x01, 0x02, 0x03, 0x26, 0x20, 0x01, 38 0x21, 0x58, 0x20, 0x17, 0x5b, 0x27, 0xa6, 0x56, 39 0xb2, 0x26, 0x0c, 0x26, 0x0c, 0x55, 0x42, 0x78, 40 0x17, 0x5d, 0x4c, 0xf8, 0xa2, 0xfd, 0x1b, 0xb9, 41 0x54, 0xdf, 0xd5, 0xeb, 0xbf, 0x22, 0x64, 0xf5, 42 0x21, 0x9a, 0xc6, 0x22, 0x58, 0x20, 0x87, 0x5f, 43 0x90, 0xe6, 0xfd, 0x71, 0x27, 0x9f, 0xeb, 0xe3, 44 0x03, 0x44, 0xbc, 0x8d, 0x49, 0xc6, 0x1c, 0x31, 45 0x3b, 0x72, 0xae, 0xd4, 0x53, 0xb1, 0xfe, 0x5d, 46 0xe1, 0x30, 0xfc, 0x2b, 0x1e, 0xd2, 47 }; 48 49 static const unsigned char authdata_dupkeys[200] = { 50 0x58, 0xc6, 0x49, 0x96, 0x0d, 0xe5, 0x88, 0x0e, 51 0x8c, 0x68, 0x74, 0x34, 0x17, 0x0f, 0x64, 0x76, 52 0x60, 0x5b, 0x8f, 0xe4, 0xae, 0xb9, 0xa2, 0x86, 53 0x32, 0xc7, 0x99, 0x5c, 0xf3, 0xba, 0x83, 0x1d, 54 0x97, 0x63, 0x41, 0x00, 0x00, 0x00, 0x00, 0xf8, 55 0xa0, 0x11, 0xf3, 0x8c, 0x0a, 0x4d, 0x15, 0x80, 56 0x06, 0x17, 0x11, 0x1f, 0x9e, 0xdc, 0x7d, 0x00, 57 0x40, 0x53, 0xfb, 0xdf, 0xaa, 0xce, 0x63, 0xde, 58 0xc5, 0xfe, 0x47, 0xe6, 0x52, 0xeb, 0xf3, 0x5d, 59 0x53, 0xa8, 0xbf, 0x9d, 0xd6, 0x09, 0x6b, 0x5e, 60 0x7f, 0xe0, 0x0d, 0x51, 0x30, 0x85, 0x6a, 0xda, 61 0x68, 0x70, 0x85, 0xb0, 0xdb, 0x08, 0x0b, 0x83, 62 0x2c, 0xef, 0x44, 0xe2, 0x36, 0x88, 0xee, 0x76, 63 0x90, 0x6e, 0x7b, 0x50, 0x3e, 0x9a, 0xa0, 0xd6, 64 0x3c, 0x34, 0xe3, 0x83, 0xe7, 0xd1, 0xbd, 0x9f, 65 0x25, 0xa6, 0x01, 0x02, 0x01, 0x02, 0x03, 0x26, 66 0x20, 0x01, 0x21, 0x58, 0x20, 0x17, 0x5b, 0x27, 67 0xa6, 0x56, 0xb2, 0x26, 0x0c, 0x26, 0x0c, 0x55, 68 0x42, 0x78, 0x17, 0x5d, 0x4c, 0xf8, 0xa2, 0xfd, 69 0x1b, 0xb9, 0x54, 0xdf, 0xd5, 0xeb, 0xbf, 0x22, 70 0x64, 0xf5, 0x21, 0x9a, 0xc6, 0x22, 0x58, 0x20, 71 0x87, 0x5f, 0x90, 0xe6, 0xfd, 0x71, 0x27, 0x9f, 72 0xeb, 0xe3, 0x03, 0x44, 0xbc, 0x8d, 0x49, 0xc6, 73 0x1c, 0x31, 0x3b, 0x72, 0xae, 0xd4, 0x53, 0xb1, 74 0xfe, 0x5d, 0xe1, 0x30, 0xfc, 0x2b, 0x1e, 0xd2, 75 }; 76 77 static const unsigned char authdata_unsorted_keys[198] = { 78 0x58, 0xc4, 0x49, 0x96, 0x0d, 0xe5, 0x88, 0x0e, 79 0x8c, 0x68, 0x74, 0x34, 0x17, 0x0f, 0x64, 0x76, 80 0x60, 0x5b, 0x8f, 0xe4, 0xae, 0xb9, 0xa2, 0x86, 81 0x32, 0xc7, 0x99, 0x5c, 0xf3, 0xba, 0x83, 0x1d, 82 0x97, 0x63, 0x41, 0x00, 0x00, 0x00, 0x00, 0xf8, 83 0xa0, 0x11, 0xf3, 0x8c, 0x0a, 0x4d, 0x15, 0x80, 84 0x06, 0x17, 0x11, 0x1f, 0x9e, 0xdc, 0x7d, 0x00, 85 0x40, 0x53, 0xfb, 0xdf, 0xaa, 0xce, 0x63, 0xde, 86 0xc5, 0xfe, 0x47, 0xe6, 0x52, 0xeb, 0xf3, 0x5d, 87 0x53, 0xa8, 0xbf, 0x9d, 0xd6, 0x09, 0x6b, 0x5e, 88 0x7f, 0xe0, 0x0d, 0x51, 0x30, 0x85, 0x6a, 0xda, 89 0x68, 0x70, 0x85, 0xb0, 0xdb, 0x08, 0x0b, 0x83, 90 0x2c, 0xef, 0x44, 0xe2, 0x36, 0x88, 0xee, 0x76, 91 0x90, 0x6e, 0x7b, 0x50, 0x3e, 0x9a, 0xa0, 0xd6, 92 0x3c, 0x34, 0xe3, 0x83, 0xe7, 0xd1, 0xbd, 0x9f, 93 0x25, 0xa5, 0x03, 0x26, 0x01, 0x02, 0x20, 0x01, 94 0x21, 0x58, 0x20, 0x17, 0x5b, 0x27, 0xa6, 0x56, 95 0xb2, 0x26, 0x0c, 0x26, 0x0c, 0x55, 0x42, 0x78, 96 0x17, 0x5d, 0x4c, 0xf8, 0xa2, 0xfd, 0x1b, 0xb9, 97 0x54, 0xdf, 0xd5, 0xeb, 0xbf, 0x22, 0x64, 0xf5, 98 0x21, 0x9a, 0xc6, 0x22, 0x58, 0x20, 0x87, 0x5f, 99 0x90, 0xe6, 0xfd, 0x71, 0x27, 0x9f, 0xeb, 0xe3, 100 0x03, 0x44, 0xbc, 0x8d, 0x49, 0xc6, 0x1c, 0x31, 101 0x3b, 0x72, 0xae, 0xd4, 0x53, 0xb1, 0xfe, 0x5d, 102 0xe1, 0x30, 0xfc, 0x2b, 0x1e, 0xd2, 103 }; 104 105 static const unsigned char x509[742] = { 106 0x30, 0x82, 0x02, 0xe2, 0x30, 0x81, 0xcb, 0x02, 107 0x01, 0x01, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 108 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 109 0x00, 0x30, 0x1d, 0x31, 0x1b, 0x30, 0x19, 0x06, 110 0x03, 0x55, 0x04, 0x03, 0x13, 0x12, 0x59, 0x75, 111 0x62, 0x69, 0x63, 0x6f, 0x20, 0x55, 0x32, 0x46, 112 0x20, 0x54, 0x65, 0x73, 0x74, 0x20, 0x43, 0x41, 113 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x34, 0x30, 0x35, 114 0x31, 0x35, 0x31, 0x32, 0x35, 0x38, 0x35, 0x34, 115 0x5a, 0x17, 0x0d, 0x31, 0x34, 0x30, 0x36, 0x31, 116 0x34, 0x31, 0x32, 0x35, 0x38, 0x35, 0x34, 0x5a, 117 0x30, 0x1d, 0x31, 0x1b, 0x30, 0x19, 0x06, 0x03, 118 0x55, 0x04, 0x03, 0x13, 0x12, 0x59, 0x75, 0x62, 119 0x69, 0x63, 0x6f, 0x20, 0x55, 0x32, 0x46, 0x20, 120 0x54, 0x65, 0x73, 0x74, 0x20, 0x45, 0x45, 0x30, 121 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 122 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 123 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 124 0x00, 0x04, 0xdb, 0x0a, 0xdb, 0xf5, 0x21, 0xc7, 125 0x5c, 0xce, 0x63, 0xdc, 0xa6, 0xe1, 0xe8, 0x25, 126 0x06, 0x0d, 0x94, 0xe6, 0x27, 0x54, 0x19, 0x4f, 127 0x9d, 0x24, 0xaf, 0x26, 0x1a, 0xbe, 0xad, 0x99, 128 0x44, 0x1f, 0x95, 0xa3, 0x71, 0x91, 0x0a, 0x3a, 129 0x20, 0xe7, 0x3e, 0x91, 0x5e, 0x13, 0xe8, 0xbe, 130 0x38, 0x05, 0x7a, 0xd5, 0x7a, 0xa3, 0x7e, 0x76, 131 0x90, 0x8f, 0xaf, 0xe2, 0x8a, 0x94, 0xb6, 0x30, 132 0xeb, 0x9d, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 133 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 134 0x00, 0x03, 0x82, 0x02, 0x01, 0x00, 0x95, 0x40, 135 0x6b, 0x50, 0x61, 0x7d, 0xad, 0x84, 0xa3, 0xb4, 136 0xeb, 0x88, 0x0f, 0xe3, 0x30, 0x0f, 0x2d, 0xa2, 137 0x0a, 0x00, 0xd9, 0x25, 0x04, 0xee, 0x72, 0xfa, 138 0x67, 0xdf, 0x58, 0x51, 0x0f, 0x0b, 0x47, 0x02, 139 0x9c, 0x3e, 0x41, 0x29, 0x4a, 0x93, 0xac, 0x29, 140 0x85, 0x89, 0x2d, 0xa4, 0x7a, 0x81, 0x32, 0x28, 141 0x57, 0x71, 0x01, 0xef, 0xa8, 0x42, 0x88, 0x16, 142 0x96, 0x37, 0x91, 0xd5, 0xdf, 0xe0, 0x8f, 0xc9, 143 0x3c, 0x8d, 0xb0, 0xcd, 0x89, 0x70, 0x82, 0xec, 144 0x79, 0xd3, 0xc6, 0x78, 0x73, 0x29, 0x32, 0xe5, 145 0xab, 0x6c, 0xbd, 0x56, 0x9f, 0xd5, 0x45, 0x91, 146 0xce, 0xc1, 0xdd, 0x8d, 0x64, 0xdc, 0xe9, 0x9c, 147 0x1f, 0x5e, 0x3c, 0xd2, 0xaf, 0x51, 0xa5, 0x82, 148 0x18, 0xaf, 0xe0, 0x37, 0xe7, 0x32, 0x9e, 0x76, 149 0x05, 0x77, 0x02, 0x7b, 0xe6, 0x24, 0xa0, 0x31, 150 0x56, 0x1b, 0xfd, 0x19, 0xc5, 0x71, 0xd3, 0xf0, 151 0x9e, 0xc0, 0x73, 0x05, 0x4e, 0xbc, 0x85, 0xb8, 152 0x53, 0x9e, 0xef, 0xc5, 0xbc, 0x9c, 0x56, 0xa3, 153 0xba, 0xd9, 0x27, 0x6a, 0xbb, 0xa9, 0x7a, 0x40, 154 0xd7, 0x47, 0x8b, 0x55, 0x72, 0x6b, 0xe3, 0xfe, 155 0x28, 0x49, 0x71, 0x24, 0xf4, 0x8f, 0xf4, 0x20, 156 0x81, 0xea, 0x38, 0xff, 0x7c, 0x0a, 0x4f, 0xdf, 157 0x02, 0x82, 0x39, 0x81, 0x82, 0x3b, 0xca, 0x09, 158 0xdd, 0xca, 0xaa, 0x0f, 0x27, 0xf5, 0xa4, 0x83, 159 0x55, 0x6c, 0x9a, 0x39, 0x9b, 0x15, 0x3a, 0x16, 160 0x63, 0xdc, 0x5b, 0xf9, 0xac, 0x5b, 0xbc, 0xf7, 161 0x9f, 0xbe, 0x0f, 0x8a, 0xa2, 0x3c, 0x31, 0x13, 162 0xa3, 0x32, 0x48, 0xca, 0x58, 0x87, 0xf8, 0x7b, 163 0xa0, 0xa1, 0x0a, 0x6a, 0x60, 0x96, 0x93, 0x5f, 164 0x5d, 0x26, 0x9e, 0x63, 0x1d, 0x09, 0xae, 0x9a, 165 0x41, 0xe5, 0xbd, 0x08, 0x47, 0xfe, 0xe5, 0x09, 166 0x9b, 0x20, 0xfd, 0x12, 0xe2, 0xe6, 0x40, 0x7f, 167 0xba, 0x4a, 0x61, 0x33, 0x66, 0x0d, 0x0e, 0x73, 168 0xdb, 0xb0, 0xd5, 0xa2, 0x9a, 0x9a, 0x17, 0x0d, 169 0x34, 0x30, 0x85, 0x6a, 0x42, 0x46, 0x9e, 0xff, 170 0x34, 0x8f, 0x5f, 0x87, 0x6c, 0x35, 0xe7, 0xa8, 171 0x4d, 0x35, 0xeb, 0xc1, 0x41, 0xaa, 0x8a, 0xd2, 172 0xda, 0x19, 0xaa, 0x79, 0xa2, 0x5f, 0x35, 0x2c, 173 0xa0, 0xfd, 0x25, 0xd3, 0xf7, 0x9d, 0x25, 0x18, 174 0x2d, 0xfa, 0xb4, 0xbc, 0xbb, 0x07, 0x34, 0x3c, 175 0x8d, 0x81, 0xbd, 0xf4, 0xe9, 0x37, 0xdb, 0x39, 176 0xe9, 0xd1, 0x45, 0x5b, 0x20, 0x41, 0x2f, 0x2d, 177 0x27, 0x22, 0xdc, 0x92, 0x74, 0x8a, 0x92, 0xd5, 178 0x83, 0xfd, 0x09, 0xfb, 0x13, 0x9b, 0xe3, 0x39, 179 0x7a, 0x6b, 0x5c, 0xfa, 0xe6, 0x76, 0x9e, 0xe0, 180 0xe4, 0xe3, 0xef, 0xad, 0xbc, 0xfd, 0x42, 0x45, 181 0x9a, 0xd4, 0x94, 0xd1, 0x7e, 0x8d, 0xa7, 0xd8, 182 0x05, 0xd5, 0xd3, 0x62, 0xcf, 0x15, 0xcf, 0x94, 183 0x7d, 0x1f, 0x5b, 0x58, 0x20, 0x44, 0x20, 0x90, 184 0x71, 0xbe, 0x66, 0xe9, 0x9a, 0xab, 0x74, 0x32, 185 0x70, 0x53, 0x1d, 0x69, 0xed, 0x87, 0x66, 0xf4, 186 0x09, 0x4f, 0xca, 0x25, 0x30, 0xc2, 0x63, 0x79, 187 0x00, 0x3c, 0xb1, 0x9b, 0x39, 0x3f, 0x00, 0xe0, 188 0xa8, 0x88, 0xef, 0x7a, 0x51, 0x5b, 0xe7, 0xbd, 189 0x49, 0x64, 0xda, 0x41, 0x7b, 0x24, 0xc3, 0x71, 190 0x22, 0xfd, 0xd1, 0xd1, 0x20, 0xb3, 0x3f, 0x97, 191 0xd3, 0x97, 0xb2, 0xaa, 0x18, 0x1c, 0x9e, 0x03, 192 0x77, 0x7b, 0x5b, 0x7e, 0xf9, 0xa3, 0xa0, 0xd6, 193 0x20, 0x81, 0x2c, 0x38, 0x8f, 0x9d, 0x25, 0xde, 194 0xe9, 0xc8, 0xf5, 0xdd, 0x6a, 0x47, 0x9c, 0x65, 195 0x04, 0x5a, 0x56, 0xe6, 0xc2, 0xeb, 0xf2, 0x02, 196 0x97, 0xe1, 0xb9, 0xd8, 0xe1, 0x24, 0x76, 0x9f, 197 0x23, 0x62, 0x39, 0x03, 0x4b, 0xc8, 0xf7, 0x34, 198 0x07, 0x49, 0xd6, 0xe7, 0x4d, 0x9a, 199 }; 200 201 const unsigned char sig[70] = { 202 0x30, 0x44, 0x02, 0x20, 0x54, 0x92, 0x28, 0x3b, 203 0x83, 0x33, 0x47, 0x56, 0x68, 0x79, 0xb2, 0x0c, 204 0x84, 0x80, 0xcc, 0x67, 0x27, 0x8b, 0xfa, 0x48, 205 0x43, 0x0d, 0x3c, 0xb4, 0x02, 0x36, 0x87, 0x97, 206 0x3e, 0xdf, 0x2f, 0x65, 0x02, 0x20, 0x1b, 0x56, 207 0x17, 0x06, 0xe2, 0x26, 0x0f, 0x6a, 0xe9, 0xa9, 208 0x70, 0x99, 0x62, 0xeb, 0x3a, 0x04, 0x1a, 0xc4, 209 0xa7, 0x03, 0x28, 0x56, 0x7c, 0xed, 0x47, 0x08, 210 0x68, 0x73, 0x6a, 0xb6, 0x89, 0x0d, 211 }; 212 213 const unsigned char pubkey[64] = { 214 0x17, 0x5b, 0x27, 0xa6, 0x56, 0xb2, 0x26, 0x0c, 215 0x26, 0x0c, 0x55, 0x42, 0x78, 0x17, 0x5d, 0x4c, 216 0xf8, 0xa2, 0xfd, 0x1b, 0xb9, 0x54, 0xdf, 0xd5, 217 0xeb, 0xbf, 0x22, 0x64, 0xf5, 0x21, 0x9a, 0xc6, 218 0x87, 0x5f, 0x90, 0xe6, 0xfd, 0x71, 0x27, 0x9f, 219 0xeb, 0xe3, 0x03, 0x44, 0xbc, 0x8d, 0x49, 0xc6, 220 0x1c, 0x31, 0x3b, 0x72, 0xae, 0xd4, 0x53, 0xb1, 221 0xfe, 0x5d, 0xe1, 0x30, 0xfc, 0x2b, 0x1e, 0xd2, 222 }; 223 224 const unsigned char id[64] = { 225 0x53, 0xfb, 0xdf, 0xaa, 0xce, 0x63, 0xde, 0xc5, 226 0xfe, 0x47, 0xe6, 0x52, 0xeb, 0xf3, 0x5d, 0x53, 227 0xa8, 0xbf, 0x9d, 0xd6, 0x09, 0x6b, 0x5e, 0x7f, 228 0xe0, 0x0d, 0x51, 0x30, 0x85, 0x6a, 0xda, 0x68, 229 0x70, 0x85, 0xb0, 0xdb, 0x08, 0x0b, 0x83, 0x2c, 230 0xef, 0x44, 0xe2, 0x36, 0x88, 0xee, 0x76, 0x90, 231 0x6e, 0x7b, 0x50, 0x3e, 0x9a, 0xa0, 0xd6, 0x3c, 232 0x34, 0xe3, 0x83, 0xe7, 0xd1, 0xbd, 0x9f, 0x25, 233 }; 234 235 /* 236 * Security Key By Yubico 237 * 5.1.X 238 * f8a011f3-8c0a-4d15-8006-17111f9edc7d 239 */ 240 const unsigned char aaguid[16] = { 241 0xf8, 0xa0, 0x11, 0xf3, 0x8c, 0x0a, 0x4d, 0x15, 242 0x80, 0x06, 0x17, 0x11, 0x1f, 0x9e, 0xdc, 0x7d, 243 }; 244 245 const char rp_id[] = "localhost"; 246 const char rp_name[] = "sweet home localhost"; 247 248 static void * 249 dummy_open(const char *path) 250 { 251 (void)path; 252 253 return (FAKE_DEV_HANDLE); 254 } 255 256 static void 257 dummy_close(void *handle) 258 { 259 assert(handle == FAKE_DEV_HANDLE); 260 } 261 262 static int 263 dummy_read(void *handle, unsigned char *buf, size_t len, int ms) 264 { 265 (void)handle; 266 (void)buf; 267 (void)len; 268 (void)ms; 269 270 abort(); 271 /* NOTREACHED */ 272 } 273 274 static int 275 dummy_write(void *handle, const unsigned char *buf, size_t len) 276 { 277 (void)handle; 278 (void)buf; 279 (void)len; 280 281 abort(); 282 /* NOTREACHED */ 283 } 284 285 static fido_cred_t * 286 alloc_cred(void) 287 { 288 fido_cred_t *c; 289 290 c = fido_cred_new(); 291 assert(c != NULL); 292 293 return (c); 294 } 295 296 static void 297 free_cred(fido_cred_t *c) 298 { 299 fido_cred_free(&c); 300 assert(c == NULL); 301 } 302 303 static fido_dev_t * 304 alloc_dev(void) 305 { 306 fido_dev_t *d; 307 308 d = fido_dev_new(); 309 assert(d != NULL); 310 311 return (d); 312 } 313 314 static void 315 free_dev(fido_dev_t *d) 316 { 317 fido_dev_free(&d); 318 assert(d == NULL); 319 } 320 321 static void 322 empty_cred(void) 323 { 324 fido_cred_t *c; 325 fido_dev_t *d; 326 fido_dev_io_t io_f; 327 328 c = alloc_cred(); 329 assert(fido_cred_authdata_len(c) == 0); 330 assert(fido_cred_authdata_ptr(c) == NULL); 331 assert(fido_cred_authdata_raw_len(c) == 0); 332 assert(fido_cred_authdata_raw_ptr(c) == NULL); 333 assert(fido_cred_clientdata_hash_len(c) == 0); 334 assert(fido_cred_clientdata_hash_ptr(c) == NULL); 335 assert(fido_cred_flags(c) == 0); 336 assert(fido_cred_fmt(c) == NULL); 337 assert(fido_cred_id_len(c) == 0); 338 assert(fido_cred_id_ptr(c) == NULL); 339 assert(fido_cred_prot(c) == 0); 340 assert(fido_cred_pubkey_len(c) == 0); 341 assert(fido_cred_pubkey_ptr(c) == NULL); 342 assert(fido_cred_rp_id(c) == NULL); 343 assert(fido_cred_rp_name(c) == NULL); 344 assert(fido_cred_sig_len(c) == 0); 345 assert(fido_cred_sig_ptr(c) == NULL); 346 assert(fido_cred_x5c_len(c) == 0); 347 assert(fido_cred_x5c_ptr(c) == NULL); 348 assert(fido_cred_verify(c) == FIDO_ERR_INVALID_ARGUMENT); 349 350 memset(&io_f, 0, sizeof(io_f)); 351 352 io_f.open = dummy_open; 353 io_f.close = dummy_close; 354 io_f.read = dummy_read; 355 io_f.write = dummy_write; 356 357 d = alloc_dev(); 358 359 fido_dev_force_u2f(d); 360 assert(fido_dev_set_io_functions(d, &io_f) == FIDO_OK); 361 assert(fido_dev_make_cred(d, c, NULL) == FIDO_ERR_INVALID_ARGUMENT); 362 assert(fido_dev_make_cred(d, c, "") == FIDO_ERR_UNSUPPORTED_OPTION); 363 assert(fido_cred_verify(c) == FIDO_ERR_INVALID_ARGUMENT); 364 365 fido_dev_force_fido2(d); 366 assert(fido_dev_set_io_functions(d, &io_f) == FIDO_OK); 367 assert(fido_dev_make_cred(d, c, NULL) == FIDO_ERR_INVALID_ARGUMENT); 368 assert(fido_dev_make_cred(d, c, "") == FIDO_ERR_INVALID_ARGUMENT); 369 assert(fido_cred_verify(c) == FIDO_ERR_INVALID_ARGUMENT); 370 371 free_cred(c); 372 free_dev(d); 373 } 374 375 static void 376 valid_cred(void) 377 { 378 fido_cred_t *c; 379 380 c = alloc_cred(); 381 assert(fido_cred_set_type(c, COSE_ES256) == FIDO_OK); 382 assert(fido_cred_set_clientdata_hash(c, cdh, sizeof(cdh)) == FIDO_OK); 383 assert(fido_cred_set_rp(c, rp_id, rp_name) == FIDO_OK); 384 assert(fido_cred_set_authdata(c, authdata, sizeof(authdata)) == FIDO_OK); 385 assert(fido_cred_set_rk(c, FIDO_OPT_FALSE) == FIDO_OK); 386 assert(fido_cred_set_uv(c, FIDO_OPT_FALSE) == FIDO_OK); 387 assert(fido_cred_set_x509(c, x509, sizeof(x509)) == FIDO_OK); 388 assert(fido_cred_set_sig(c, sig, sizeof(sig)) == FIDO_OK); 389 assert(fido_cred_set_fmt(c, "packed") == FIDO_OK); 390 assert(fido_cred_verify(c) == FIDO_OK); 391 assert(fido_cred_prot(c) == 0); 392 assert(fido_cred_pubkey_len(c) == sizeof(pubkey)); 393 assert(memcmp(fido_cred_pubkey_ptr(c), pubkey, sizeof(pubkey)) == 0); 394 assert(fido_cred_id_len(c) == sizeof(id)); 395 assert(memcmp(fido_cred_id_ptr(c), id, sizeof(id)) == 0); 396 assert(fido_cred_aaguid_len(c) == sizeof(aaguid)); 397 assert(memcmp(fido_cred_aaguid_ptr(c), aaguid, sizeof(aaguid)) == 0); 398 free_cred(c); 399 } 400 401 static void 402 no_cdh(void) 403 { 404 fido_cred_t *c; 405 406 c = alloc_cred(); 407 assert(fido_cred_set_type(c, COSE_ES256) == FIDO_OK); 408 assert(fido_cred_set_rp(c, rp_id, rp_name) == FIDO_OK); 409 assert(fido_cred_set_authdata(c, authdata, sizeof(authdata)) == FIDO_OK); 410 assert(fido_cred_set_rk(c, FIDO_OPT_FALSE) == FIDO_OK); 411 assert(fido_cred_set_uv(c, FIDO_OPT_FALSE) == FIDO_OK); 412 assert(fido_cred_set_x509(c, x509, sizeof(x509)) == FIDO_OK); 413 assert(fido_cred_set_sig(c, sig, sizeof(sig)) == FIDO_OK); 414 assert(fido_cred_set_fmt(c, "packed") == FIDO_OK); 415 assert(fido_cred_verify(c) == FIDO_ERR_INVALID_ARGUMENT); 416 assert(fido_cred_pubkey_len(c) == sizeof(pubkey)); 417 assert(memcmp(fido_cred_pubkey_ptr(c), pubkey, sizeof(pubkey)) == 0); 418 assert(fido_cred_id_len(c) == sizeof(id)); 419 assert(memcmp(fido_cred_id_ptr(c), id, sizeof(id)) == 0); 420 assert(fido_cred_aaguid_len(c) == sizeof(aaguid)); 421 assert(memcmp(fido_cred_aaguid_ptr(c), aaguid, sizeof(aaguid)) == 0); 422 free_cred(c); 423 } 424 425 static void 426 no_rp_id(void) 427 { 428 fido_cred_t *c; 429 430 c = alloc_cred(); 431 assert(fido_cred_set_type(c, COSE_ES256) == FIDO_OK); 432 assert(fido_cred_set_clientdata_hash(c, cdh, sizeof(cdh)) == FIDO_OK); 433 assert(fido_cred_set_authdata(c, authdata, sizeof(authdata)) == FIDO_OK); 434 assert(fido_cred_set_rk(c, FIDO_OPT_FALSE) == FIDO_OK); 435 assert(fido_cred_set_uv(c, FIDO_OPT_FALSE) == FIDO_OK); 436 assert(fido_cred_set_x509(c, x509, sizeof(x509)) == FIDO_OK); 437 assert(fido_cred_set_sig(c, sig, sizeof(sig)) == FIDO_OK); 438 assert(fido_cred_set_fmt(c, "packed") == FIDO_OK); 439 assert(fido_cred_verify(c) == FIDO_ERR_INVALID_ARGUMENT); 440 assert(fido_cred_pubkey_len(c) == sizeof(pubkey)); 441 assert(memcmp(fido_cred_pubkey_ptr(c), pubkey, sizeof(pubkey)) == 0); 442 assert(fido_cred_id_len(c) == sizeof(id)); 443 assert(memcmp(fido_cred_id_ptr(c), id, sizeof(id)) == 0); 444 assert(fido_cred_aaguid_len(c) == sizeof(aaguid)); 445 assert(memcmp(fido_cred_aaguid_ptr(c), aaguid, sizeof(aaguid)) == 0); 446 free_cred(c); 447 } 448 449 static void 450 no_rp_name(void) 451 { 452 fido_cred_t *c; 453 454 c = alloc_cred(); 455 assert(fido_cred_set_type(c, COSE_ES256) == FIDO_OK); 456 assert(fido_cred_set_rp(c, rp_id, NULL) == FIDO_OK); 457 assert(fido_cred_set_clientdata_hash(c, cdh, sizeof(cdh)) == FIDO_OK); 458 assert(fido_cred_set_authdata(c, authdata, sizeof(authdata)) == FIDO_OK); 459 assert(fido_cred_set_rk(c, FIDO_OPT_FALSE) == FIDO_OK); 460 assert(fido_cred_set_uv(c, FIDO_OPT_FALSE) == FIDO_OK); 461 assert(fido_cred_set_x509(c, x509, sizeof(x509)) == FIDO_OK); 462 assert(fido_cred_set_sig(c, sig, sizeof(sig)) == FIDO_OK); 463 assert(fido_cred_set_fmt(c, "packed") == FIDO_OK); 464 assert(fido_cred_verify(c) == FIDO_OK); 465 assert(fido_cred_pubkey_len(c) == sizeof(pubkey)); 466 assert(memcmp(fido_cred_pubkey_ptr(c), pubkey, sizeof(pubkey)) == 0); 467 assert(fido_cred_id_len(c) == sizeof(id)); 468 assert(memcmp(fido_cred_id_ptr(c), id, sizeof(id)) == 0); 469 assert(fido_cred_aaguid_len(c) == sizeof(aaguid)); 470 assert(memcmp(fido_cred_aaguid_ptr(c), aaguid, sizeof(aaguid)) == 0); 471 free_cred(c); 472 } 473 474 static void 475 no_authdata(void) 476 { 477 fido_cred_t *c; 478 unsigned char *unset; 479 480 unset = calloc(1, sizeof(aaguid)); 481 assert(unset != NULL); 482 483 c = alloc_cred(); 484 assert(fido_cred_set_type(c, COSE_ES256) == FIDO_OK); 485 assert(fido_cred_set_clientdata_hash(c, cdh, sizeof(cdh)) == FIDO_OK); 486 assert(fido_cred_set_rp(c, rp_id, rp_name) == FIDO_OK); 487 assert(fido_cred_set_rk(c, FIDO_OPT_FALSE) == FIDO_OK); 488 assert(fido_cred_set_uv(c, FIDO_OPT_FALSE) == FIDO_OK); 489 assert(fido_cred_set_x509(c, x509, sizeof(x509)) == FIDO_OK); 490 assert(fido_cred_set_sig(c, sig, sizeof(sig)) == FIDO_OK); 491 assert(fido_cred_set_fmt(c, "packed") == FIDO_OK); 492 assert(fido_cred_verify(c) == FIDO_ERR_INVALID_ARGUMENT); 493 assert(fido_cred_pubkey_len(c) == 0); 494 assert(fido_cred_pubkey_ptr(c) == NULL); 495 assert(fido_cred_id_len(c) == 0); 496 assert(fido_cred_id_ptr(c) == NULL); 497 assert(fido_cred_aaguid_len(c) == sizeof(aaguid)); 498 assert(memcmp(fido_cred_aaguid_ptr(c), unset, sizeof(aaguid)) == 0); 499 free_cred(c); 500 free(unset); 501 } 502 503 static void 504 no_x509(void) 505 { 506 fido_cred_t *c; 507 508 c = alloc_cred(); 509 assert(fido_cred_set_type(c, COSE_ES256) == FIDO_OK); 510 assert(fido_cred_set_clientdata_hash(c, cdh, sizeof(cdh)) == FIDO_OK); 511 assert(fido_cred_set_rp(c, rp_id, rp_name) == FIDO_OK); 512 assert(fido_cred_set_authdata(c, authdata, sizeof(authdata)) == FIDO_OK); 513 assert(fido_cred_set_rk(c, FIDO_OPT_FALSE) == FIDO_OK); 514 assert(fido_cred_set_uv(c, FIDO_OPT_FALSE) == FIDO_OK); 515 assert(fido_cred_set_sig(c, sig, sizeof(sig)) == FIDO_OK); 516 assert(fido_cred_set_fmt(c, "packed") == FIDO_OK); 517 assert(fido_cred_verify(c) == FIDO_ERR_INVALID_ARGUMENT); 518 assert(fido_cred_pubkey_len(c) == sizeof(pubkey)); 519 assert(memcmp(fido_cred_pubkey_ptr(c), pubkey, sizeof(pubkey)) == 0); 520 assert(fido_cred_id_len(c) == sizeof(id)); 521 assert(memcmp(fido_cred_id_ptr(c), id, sizeof(id)) == 0); 522 assert(fido_cred_aaguid_len(c) == sizeof(aaguid)); 523 assert(memcmp(fido_cred_aaguid_ptr(c), aaguid, sizeof(aaguid)) == 0); 524 free_cred(c); 525 } 526 527 static void 528 no_sig(void) 529 { 530 fido_cred_t *c; 531 532 c = alloc_cred(); 533 assert(fido_cred_set_type(c, COSE_ES256) == FIDO_OK); 534 assert(fido_cred_set_clientdata_hash(c, cdh, sizeof(cdh)) == FIDO_OK); 535 assert(fido_cred_set_rp(c, rp_id, rp_name) == FIDO_OK); 536 assert(fido_cred_set_authdata(c, authdata, sizeof(authdata)) == FIDO_OK); 537 assert(fido_cred_set_rk(c, FIDO_OPT_FALSE) == FIDO_OK); 538 assert(fido_cred_set_uv(c, FIDO_OPT_FALSE) == FIDO_OK); 539 assert(fido_cred_set_x509(c, x509, sizeof(x509)) == FIDO_OK); 540 assert(fido_cred_set_fmt(c, "packed") == FIDO_OK); 541 assert(fido_cred_verify(c) == FIDO_ERR_INVALID_ARGUMENT); 542 assert(fido_cred_pubkey_len(c) == sizeof(pubkey)); 543 assert(memcmp(fido_cred_pubkey_ptr(c), pubkey, sizeof(pubkey)) == 0); 544 assert(fido_cred_id_len(c) == sizeof(id)); 545 assert(memcmp(fido_cred_id_ptr(c), id, sizeof(id)) == 0); 546 assert(fido_cred_aaguid_len(c) == sizeof(aaguid)); 547 assert(memcmp(fido_cred_aaguid_ptr(c), aaguid, sizeof(aaguid)) == 0); 548 free_cred(c); 549 } 550 551 static void 552 no_fmt(void) 553 { 554 fido_cred_t *c; 555 556 c = alloc_cred(); 557 assert(fido_cred_set_type(c, COSE_ES256) == FIDO_OK); 558 assert(fido_cred_set_clientdata_hash(c, cdh, sizeof(cdh)) == FIDO_OK); 559 assert(fido_cred_set_rp(c, rp_id, rp_name) == FIDO_OK); 560 assert(fido_cred_set_authdata(c, authdata, sizeof(authdata)) == FIDO_OK); 561 assert(fido_cred_set_rk(c, FIDO_OPT_FALSE) == FIDO_OK); 562 assert(fido_cred_set_uv(c, FIDO_OPT_FALSE) == FIDO_OK); 563 assert(fido_cred_set_x509(c, x509, sizeof(x509)) == FIDO_OK); 564 assert(fido_cred_set_sig(c, sig, sizeof(sig)) == FIDO_OK); 565 assert(fido_cred_verify(c) == FIDO_ERR_INVALID_ARGUMENT); 566 assert(fido_cred_pubkey_len(c) == sizeof(pubkey)); 567 assert(memcmp(fido_cred_pubkey_ptr(c), pubkey, sizeof(pubkey)) == 0); 568 assert(fido_cred_id_len(c) == sizeof(id)); 569 assert(memcmp(fido_cred_id_ptr(c), id, sizeof(id)) == 0); 570 assert(fido_cred_aaguid_len(c) == sizeof(aaguid)); 571 assert(memcmp(fido_cred_aaguid_ptr(c), aaguid, sizeof(aaguid)) == 0); 572 free_cred(c); 573 } 574 575 static void 576 wrong_options(void) 577 { 578 fido_cred_t *c; 579 580 c = alloc_cred(); 581 assert(fido_cred_set_type(c, COSE_ES256) == FIDO_OK); 582 assert(fido_cred_set_clientdata_hash(c, cdh, sizeof(cdh)) == FIDO_OK); 583 assert(fido_cred_set_rp(c, rp_id, rp_name) == FIDO_OK); 584 assert(fido_cred_set_authdata(c, authdata, sizeof(authdata)) == FIDO_OK); 585 assert(fido_cred_set_rk(c, FIDO_OPT_FALSE) == FIDO_OK); 586 assert(fido_cred_set_uv(c, FIDO_OPT_TRUE) == FIDO_OK); 587 assert(fido_cred_set_x509(c, x509, sizeof(x509)) == FIDO_OK); 588 assert(fido_cred_set_sig(c, sig, sizeof(sig)) == FIDO_OK); 589 assert(fido_cred_set_fmt(c, "packed") == FIDO_OK); 590 assert(fido_cred_verify(c) == FIDO_ERR_INVALID_PARAM); 591 assert(fido_cred_pubkey_len(c) == sizeof(pubkey)); 592 assert(memcmp(fido_cred_pubkey_ptr(c), pubkey, sizeof(pubkey)) == 0); 593 assert(fido_cred_id_len(c) == sizeof(id)); 594 assert(memcmp(fido_cred_id_ptr(c), id, sizeof(id)) == 0); 595 assert(fido_cred_aaguid_len(c) == sizeof(aaguid)); 596 assert(memcmp(fido_cred_aaguid_ptr(c), aaguid, sizeof(aaguid)) == 0); 597 free_cred(c); 598 } 599 600 static void 601 junk_cdh(void) 602 { 603 fido_cred_t *c; 604 unsigned char *junk; 605 606 junk = malloc(sizeof(cdh)); 607 assert(junk != NULL); 608 memcpy(junk, cdh, sizeof(cdh)); 609 junk[0] = ~junk[0]; 610 611 c = alloc_cred(); 612 assert(fido_cred_set_type(c, COSE_ES256) == FIDO_OK); 613 assert(fido_cred_set_clientdata_hash(c, junk, sizeof(cdh)) == FIDO_OK); 614 assert(fido_cred_set_rp(c, rp_id, rp_name) == FIDO_OK); 615 assert(fido_cred_set_authdata(c, authdata, sizeof(authdata)) == FIDO_OK); 616 assert(fido_cred_set_rk(c, FIDO_OPT_FALSE) == FIDO_OK); 617 assert(fido_cred_set_uv(c, FIDO_OPT_FALSE) == FIDO_OK); 618 assert(fido_cred_set_x509(c, x509, sizeof(x509)) == FIDO_OK); 619 assert(fido_cred_set_sig(c, sig, sizeof(sig)) == FIDO_OK); 620 assert(fido_cred_set_fmt(c, "packed") == FIDO_OK); 621 assert(fido_cred_verify(c) == FIDO_ERR_INVALID_SIG); 622 assert(fido_cred_pubkey_len(c) == sizeof(pubkey)); 623 assert(memcmp(fido_cred_pubkey_ptr(c), pubkey, sizeof(pubkey)) == 0); 624 assert(fido_cred_id_len(c) == sizeof(id)); 625 assert(memcmp(fido_cred_id_ptr(c), id, sizeof(id)) == 0); 626 assert(fido_cred_aaguid_len(c) == sizeof(aaguid)); 627 assert(memcmp(fido_cred_aaguid_ptr(c), aaguid, sizeof(aaguid)) == 0); 628 free_cred(c); 629 free(junk); 630 } 631 632 static void 633 junk_fmt(void) 634 { 635 fido_cred_t *c; 636 637 c = alloc_cred(); 638 assert(fido_cred_set_type(c, COSE_ES256) == FIDO_OK); 639 assert(fido_cred_set_clientdata_hash(c, cdh, sizeof(cdh)) == FIDO_OK); 640 assert(fido_cred_set_rp(c, rp_id, rp_name) == FIDO_OK); 641 assert(fido_cred_set_authdata(c, authdata, sizeof(authdata)) == FIDO_OK); 642 assert(fido_cred_set_rk(c, FIDO_OPT_FALSE) == FIDO_OK); 643 assert(fido_cred_set_uv(c, FIDO_OPT_FALSE) == FIDO_OK); 644 assert(fido_cred_set_x509(c, x509, sizeof(x509)) == FIDO_OK); 645 assert(fido_cred_set_sig(c, sig, sizeof(sig)) == FIDO_OK); 646 assert(fido_cred_set_fmt(c, "junk") == FIDO_ERR_INVALID_ARGUMENT); 647 assert(fido_cred_verify(c) == FIDO_ERR_INVALID_ARGUMENT); 648 free_cred(c); 649 } 650 651 static void 652 junk_rp_id(void) 653 { 654 fido_cred_t *c; 655 656 c = alloc_cred(); 657 assert(fido_cred_set_type(c, COSE_ES256) == FIDO_OK); 658 assert(fido_cred_set_clientdata_hash(c, cdh, sizeof(cdh)) == FIDO_OK); 659 assert(fido_cred_set_rp(c, "potato", rp_name) == FIDO_OK); 660 assert(fido_cred_set_authdata(c, authdata, sizeof(authdata)) == FIDO_OK); 661 assert(fido_cred_set_rk(c, FIDO_OPT_FALSE) == FIDO_OK); 662 assert(fido_cred_set_uv(c, FIDO_OPT_FALSE) == FIDO_OK); 663 assert(fido_cred_set_x509(c, x509, sizeof(x509)) == FIDO_OK); 664 assert(fido_cred_set_sig(c, sig, sizeof(sig)) == FIDO_OK); 665 assert(fido_cred_set_fmt(c, "packed") == FIDO_OK); 666 assert(fido_cred_verify(c) == FIDO_ERR_INVALID_PARAM); 667 assert(fido_cred_pubkey_len(c) == sizeof(pubkey)); 668 assert(memcmp(fido_cred_pubkey_ptr(c), pubkey, sizeof(pubkey)) == 0); 669 assert(fido_cred_id_len(c) == sizeof(id)); 670 assert(memcmp(fido_cred_id_ptr(c), id, sizeof(id)) == 0); 671 assert(fido_cred_aaguid_len(c) == sizeof(aaguid)); 672 assert(memcmp(fido_cred_aaguid_ptr(c), aaguid, sizeof(aaguid)) == 0); 673 free_cred(c); 674 } 675 676 static void 677 junk_rp_name(void) 678 { 679 fido_cred_t *c; 680 681 c = alloc_cred(); 682 assert(fido_cred_set_type(c, COSE_ES256) == FIDO_OK); 683 assert(fido_cred_set_clientdata_hash(c, cdh, sizeof(cdh)) == FIDO_OK); 684 assert(fido_cred_set_rp(c, rp_id, "potato") == FIDO_OK); 685 assert(fido_cred_set_authdata(c, authdata, sizeof(authdata)) == FIDO_OK); 686 assert(fido_cred_set_rk(c, FIDO_OPT_FALSE) == FIDO_OK); 687 assert(fido_cred_set_uv(c, FIDO_OPT_FALSE) == FIDO_OK); 688 assert(fido_cred_set_x509(c, x509, sizeof(x509)) == FIDO_OK); 689 assert(fido_cred_set_sig(c, sig, sizeof(sig)) == FIDO_OK); 690 assert(fido_cred_set_fmt(c, "packed") == FIDO_OK); 691 assert(fido_cred_verify(c) == FIDO_OK); 692 assert(fido_cred_pubkey_len(c) == sizeof(pubkey)); 693 assert(memcmp(fido_cred_pubkey_ptr(c), pubkey, sizeof(pubkey)) == 0); 694 assert(fido_cred_id_len(c) == sizeof(id)); 695 assert(memcmp(fido_cred_id_ptr(c), id, sizeof(id)) == 0); 696 assert(fido_cred_aaguid_len(c) == sizeof(aaguid)); 697 assert(memcmp(fido_cred_aaguid_ptr(c), aaguid, sizeof(aaguid)) == 0); 698 free_cred(c); 699 } 700 701 static void 702 junk_authdata(void) 703 { 704 fido_cred_t *c; 705 unsigned char *junk; 706 unsigned char *unset; 707 708 junk = malloc(sizeof(authdata)); 709 assert(junk != NULL); 710 memcpy(junk, authdata, sizeof(authdata)); 711 junk[0] = ~junk[0]; 712 713 unset = calloc(1, sizeof(aaguid)); 714 assert(unset != NULL); 715 716 c = alloc_cred(); 717 assert(fido_cred_set_authdata(c, junk, 718 sizeof(authdata)) == FIDO_ERR_INVALID_ARGUMENT); 719 assert(fido_cred_authdata_len(c) == 0); 720 assert(fido_cred_authdata_ptr(c) == NULL); 721 assert(fido_cred_authdata_raw_len(c) == 0); 722 assert(fido_cred_authdata_raw_ptr(c) == NULL); 723 assert(fido_cred_flags(c) == 0); 724 assert(fido_cred_fmt(c) == NULL); 725 assert(fido_cred_id_len(c) == 0); 726 assert(fido_cred_id_ptr(c) == NULL); 727 assert(fido_cred_pubkey_len(c) == 0); 728 assert(fido_cred_pubkey_ptr(c) == NULL); 729 assert(fido_cred_rp_id(c) == NULL); 730 assert(fido_cred_rp_name(c) == NULL); 731 assert(fido_cred_sig_len(c) == 0); 732 assert(fido_cred_sig_ptr(c) == NULL); 733 assert(fido_cred_x5c_len(c) == 0); 734 assert(fido_cred_x5c_ptr(c) == NULL); 735 assert(fido_cred_aaguid_len(c) == sizeof(aaguid)); 736 assert(memcmp(fido_cred_aaguid_ptr(c), unset, sizeof(aaguid)) == 0); 737 assert(fido_cred_verify(c) == FIDO_ERR_INVALID_ARGUMENT); 738 free_cred(c); 739 free(junk); 740 free(unset); 741 } 742 743 static void 744 junk_sig(void) 745 { 746 fido_cred_t *c; 747 unsigned char *junk; 748 749 junk = malloc(sizeof(sig)); 750 assert(junk != NULL); 751 memcpy(junk, sig, sizeof(sig)); 752 junk[0] = ~junk[0]; 753 754 c = alloc_cred(); 755 assert(fido_cred_set_type(c, COSE_ES256) == FIDO_OK); 756 assert(fido_cred_set_clientdata_hash(c, cdh, sizeof(cdh)) == FIDO_OK); 757 assert(fido_cred_set_rp(c, rp_id, rp_name) == FIDO_OK); 758 assert(fido_cred_set_authdata(c, authdata, sizeof(authdata)) == FIDO_OK); 759 assert(fido_cred_set_rk(c, FIDO_OPT_FALSE) == FIDO_OK); 760 assert(fido_cred_set_uv(c, FIDO_OPT_FALSE) == FIDO_OK); 761 assert(fido_cred_set_x509(c, x509, sizeof(x509)) == FIDO_OK); 762 assert(fido_cred_set_sig(c, junk, sizeof(sig)) == FIDO_OK); 763 assert(fido_cred_set_fmt(c, "packed") == FIDO_OK); 764 assert(fido_cred_verify(c) == FIDO_ERR_INVALID_SIG); 765 assert(fido_cred_pubkey_len(c) == sizeof(pubkey)); 766 assert(memcmp(fido_cred_pubkey_ptr(c), pubkey, sizeof(pubkey)) == 0); 767 assert(fido_cred_id_len(c) == sizeof(id)); 768 assert(memcmp(fido_cred_id_ptr(c), id, sizeof(id)) == 0); 769 assert(fido_cred_aaguid_len(c) == sizeof(aaguid)); 770 assert(memcmp(fido_cred_aaguid_ptr(c), aaguid, sizeof(aaguid)) == 0); 771 free_cred(c); 772 free(junk); 773 } 774 775 static void 776 junk_x509(void) 777 { 778 fido_cred_t *c; 779 unsigned char *junk; 780 781 junk = malloc(sizeof(x509)); 782 assert(junk != NULL); 783 memcpy(junk, x509, sizeof(x509)); 784 junk[0] = ~junk[0]; 785 786 c = alloc_cred(); 787 assert(fido_cred_set_type(c, COSE_ES256) == FIDO_OK); 788 assert(fido_cred_set_clientdata_hash(c, cdh, sizeof(cdh)) == FIDO_OK); 789 assert(fido_cred_set_rp(c, rp_id, rp_name) == FIDO_OK); 790 assert(fido_cred_set_authdata(c, authdata, sizeof(authdata)) == FIDO_OK); 791 assert(fido_cred_set_rk(c, FIDO_OPT_FALSE) == FIDO_OK); 792 assert(fido_cred_set_uv(c, FIDO_OPT_FALSE) == FIDO_OK); 793 assert(fido_cred_set_x509(c, junk, sizeof(x509)) == FIDO_OK); 794 assert(fido_cred_set_sig(c, sig, sizeof(sig)) == FIDO_OK); 795 assert(fido_cred_set_fmt(c, "packed") == FIDO_OK); 796 assert(fido_cred_verify(c) == FIDO_ERR_INVALID_SIG); 797 assert(fido_cred_pubkey_len(c) == sizeof(pubkey)); 798 assert(memcmp(fido_cred_pubkey_ptr(c), pubkey, sizeof(pubkey)) == 0); 799 assert(fido_cred_id_len(c) == sizeof(id)); 800 assert(memcmp(fido_cred_id_ptr(c), id, sizeof(id)) == 0); 801 assert(fido_cred_aaguid_len(c) == sizeof(aaguid)); 802 assert(memcmp(fido_cred_aaguid_ptr(c), aaguid, sizeof(aaguid)) == 0); 803 free_cred(c); 804 free(junk); 805 } 806 807 /* github issue #6 */ 808 static void 809 invalid_type(void) 810 { 811 fido_cred_t *c; 812 unsigned char *unset; 813 814 unset = calloc(1, sizeof(aaguid)); 815 assert(unset != NULL); 816 817 c = alloc_cred(); 818 assert(fido_cred_set_type(c, COSE_RS256) == FIDO_OK); 819 assert(fido_cred_set_clientdata_hash(c, cdh, sizeof(cdh)) == FIDO_OK); 820 assert(fido_cred_set_rp(c, rp_id, rp_name) == FIDO_OK); 821 assert(fido_cred_set_authdata(c, authdata, sizeof(authdata)) == FIDO_ERR_INVALID_ARGUMENT); 822 assert(fido_cred_set_rk(c, FIDO_OPT_FALSE) == FIDO_OK); 823 assert(fido_cred_set_uv(c, FIDO_OPT_FALSE) == FIDO_OK); 824 assert(fido_cred_set_x509(c, x509, sizeof(x509)) == FIDO_OK); 825 assert(fido_cred_set_sig(c, sig, sizeof(sig)) == FIDO_OK); 826 assert(fido_cred_set_fmt(c, "packed") == FIDO_OK); 827 assert(fido_cred_verify(c) == FIDO_ERR_INVALID_ARGUMENT); 828 assert(fido_cred_pubkey_len(c) == 0); 829 assert(fido_cred_pubkey_ptr(c) == NULL); 830 assert(fido_cred_id_len(c) == 0); 831 assert(fido_cred_id_ptr(c) == NULL); 832 assert(fido_cred_aaguid_len(c) == sizeof(aaguid)); 833 assert(memcmp(fido_cred_aaguid_ptr(c), unset, sizeof(aaguid)) == 0); 834 free_cred(c); 835 free(unset); 836 } 837 838 /* cbor_serialize_alloc misuse */ 839 static void 840 bad_cbor_serialize(void) 841 { 842 fido_cred_t *c; 843 844 c = alloc_cred(); 845 assert(fido_cred_set_type(c, COSE_ES256) == FIDO_OK); 846 assert(fido_cred_set_authdata(c, authdata, sizeof(authdata)) == FIDO_OK); 847 assert(fido_cred_authdata_len(c) == sizeof(authdata)); 848 free_cred(c); 849 } 850 851 static void 852 duplicate_keys(void) 853 { 854 fido_cred_t *c; 855 856 c = alloc_cred(); 857 assert(fido_cred_set_type(c, COSE_ES256) == FIDO_OK); 858 assert(fido_cred_set_authdata(c, authdata_dupkeys, 859 sizeof(authdata_dupkeys)) == FIDO_ERR_INVALID_ARGUMENT); 860 free_cred(c); 861 } 862 863 static void 864 unsorted_keys(void) 865 { 866 fido_cred_t *c; 867 868 c = alloc_cred(); 869 assert(fido_cred_set_type(c, COSE_ES256) == FIDO_OK); 870 assert(fido_cred_set_authdata(c, authdata_unsorted_keys, 871 sizeof(authdata_unsorted_keys)) == FIDO_ERR_INVALID_ARGUMENT); 872 free_cred(c); 873 } 874 875 static void 876 wrong_credprot(void) 877 { 878 fido_cred_t *c; 879 880 c = alloc_cred(); 881 assert(fido_cred_set_type(c, COSE_ES256) == FIDO_OK); 882 assert(fido_cred_set_clientdata_hash(c, cdh, sizeof(cdh)) == FIDO_OK); 883 assert(fido_cred_set_rp(c, rp_id, rp_name) == FIDO_OK); 884 assert(fido_cred_set_x509(c, x509, sizeof(x509)) == FIDO_OK); 885 assert(fido_cred_set_sig(c, sig, sizeof(sig)) == FIDO_OK); 886 assert(fido_cred_set_fmt(c, "packed") == FIDO_OK); 887 assert(fido_cred_set_prot(c, FIDO_CRED_PROT_UV_OPTIONAL_WITH_ID) == FIDO_OK); 888 assert(fido_cred_set_authdata(c, authdata, sizeof(authdata)) == FIDO_OK); 889 assert(fido_cred_verify(c) == FIDO_ERR_INVALID_PARAM); 890 free_cred(c); 891 } 892 893 static void 894 raw_authdata(void) 895 { 896 fido_cred_t *c; 897 cbor_item_t *item; 898 struct cbor_load_result cbor_result; 899 const unsigned char *ptr; 900 unsigned char *cbor; 901 size_t len; 902 size_t cbor_len; 903 size_t alloclen; 904 905 c = alloc_cred(); 906 assert(fido_cred_set_type(c, COSE_ES256) == FIDO_OK); 907 assert(fido_cred_set_authdata(c, authdata, sizeof(authdata)) == FIDO_OK); 908 assert((ptr = fido_cred_authdata_ptr(c)) != NULL); 909 assert((len = fido_cred_authdata_len(c)) != 0); 910 assert((item = cbor_load(ptr, len, &cbor_result)) != NULL); 911 assert(cbor_result.read == len); 912 assert(cbor_isa_bytestring(item)); 913 assert((ptr = fido_cred_authdata_raw_ptr(c)) != NULL); 914 assert((len = fido_cred_authdata_raw_len(c)) != 0); 915 assert(cbor_bytestring_length(item) == len); 916 assert(memcmp(ptr, cbor_bytestring_handle(item), len) == 0); 917 assert((len = fido_cred_authdata_len(c)) != 0); 918 assert((cbor_len = cbor_serialize_alloc(item, &cbor, &alloclen)) == len); 919 assert((ptr = cbor_bytestring_handle(item)) != NULL); 920 assert((len = cbor_bytestring_length(item)) != 0); 921 assert(fido_cred_set_authdata_raw(c, ptr, len) == FIDO_OK); 922 assert((ptr = fido_cred_authdata_ptr(c)) != NULL); 923 assert((len = fido_cred_authdata_len(c)) != 0); 924 assert(len == cbor_len); 925 assert(memcmp(cbor, ptr, len) == 0); 926 assert(cbor_len == sizeof(authdata)); 927 assert(memcmp(cbor, authdata, cbor_len) == 0); 928 cbor_decref(&item); 929 free(cbor); 930 free_cred(c); 931 } 932 933 static void 934 fmt_none(void) 935 { 936 fido_cred_t *c; 937 938 c = alloc_cred(); 939 assert(fido_cred_set_type(c, COSE_ES256) == FIDO_OK); 940 assert(fido_cred_set_clientdata_hash(c, cdh, sizeof(cdh)) == FIDO_OK); 941 assert(fido_cred_set_rp(c, rp_id, rp_name) == FIDO_OK); 942 assert(fido_cred_set_authdata(c, authdata, sizeof(authdata)) == FIDO_OK); 943 assert(fido_cred_set_rk(c, FIDO_OPT_FALSE) == FIDO_OK); 944 assert(fido_cred_set_uv(c, FIDO_OPT_FALSE) == FIDO_OK); 945 assert(fido_cred_set_fmt(c, "none") == FIDO_OK); 946 assert(fido_cred_verify(c) == FIDO_ERR_INVALID_ARGUMENT); 947 assert(fido_cred_prot(c) == 0); 948 assert(fido_cred_pubkey_len(c) == sizeof(pubkey)); 949 assert(memcmp(fido_cred_pubkey_ptr(c), pubkey, sizeof(pubkey)) == 0); 950 assert(fido_cred_id_len(c) == sizeof(id)); 951 assert(memcmp(fido_cred_id_ptr(c), id, sizeof(id)) == 0); 952 assert(fido_cred_aaguid_len(c) == sizeof(aaguid)); 953 assert(memcmp(fido_cred_aaguid_ptr(c), aaguid, sizeof(aaguid)) == 0); 954 free_cred(c); 955 } 956 957 int 958 main(void) 959 { 960 fido_init(0); 961 962 empty_cred(); 963 valid_cred(); 964 no_cdh(); 965 no_rp_id(); 966 no_rp_name(); 967 no_authdata(); 968 no_x509(); 969 no_sig(); 970 no_fmt(); 971 junk_cdh(); 972 junk_fmt(); 973 junk_rp_id(); 974 junk_rp_name(); 975 junk_authdata(); 976 junk_x509(); 977 junk_sig(); 978 wrong_options(); 979 invalid_type(); 980 bad_cbor_serialize(); 981 duplicate_keys(); 982 unsorted_keys(); 983 wrong_credprot(); 984 raw_authdata(); 985 fmt_none(); 986 987 exit(0); 988 } 989