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 #ifndef _EXTERN_H 8 #define _EXTERN_H 9 10 #ifdef __MINGW32__ 11 #include <sys/types.h> 12 #endif 13 14 #ifdef HAVE_SIGNAL_H 15 #include <signal.h> 16 #endif 17 18 #include <stdint.h> 19 20 #include "fido/types.h" 21 #include "blob.h" 22 23 #ifdef __cplusplus 24 extern "C" { 25 #endif /* __cplusplus */ 26 27 /* aes256 */ 28 int aes256_cbc_dec(const fido_dev_t *dev, const fido_blob_t *, 29 const fido_blob_t *, fido_blob_t *); 30 int aes256_cbc_enc(const fido_dev_t *dev, const fido_blob_t *, 31 const fido_blob_t *, fido_blob_t *); 32 int aes256_gcm_dec(const fido_blob_t *, const fido_blob_t *, 33 const fido_blob_t *, const fido_blob_t *, fido_blob_t *); 34 int aes256_gcm_enc(const fido_blob_t *, const fido_blob_t *, 35 const fido_blob_t *, const fido_blob_t *, fido_blob_t *); 36 37 /* cbor encoding functions */ 38 cbor_item_t *cbor_build_uint(const uint64_t); 39 cbor_item_t *cbor_flatten_vector(cbor_item_t **, size_t); 40 cbor_item_t *cbor_encode_assert_opt(fido_opt_t, fido_opt_t); 41 cbor_item_t *cbor_encode_change_pin_auth(const fido_dev_t *, 42 const fido_blob_t *, const fido_blob_t *, const fido_blob_t *); 43 cbor_item_t *cbor_encode_cred_ext(const fido_cred_ext_t *, const fido_blob_t *); 44 cbor_item_t *cbor_encode_assert_ext(fido_dev_t *, 45 const fido_assert_ext_t *, const fido_blob_t *, const es256_pk_t *); 46 cbor_item_t *cbor_encode_cred_opt(fido_opt_t, fido_opt_t); 47 cbor_item_t *cbor_encode_pin_auth(const fido_dev_t *, const fido_blob_t *, 48 const fido_blob_t *); 49 cbor_item_t *cbor_encode_pin_opt(const fido_dev_t *); 50 cbor_item_t *cbor_encode_pubkey(const fido_blob_t *); 51 cbor_item_t *cbor_encode_pubkey_list(const fido_blob_array_t *); 52 cbor_item_t *cbor_encode_pubkey_param(int); 53 cbor_item_t *cbor_encode_rp_entity(const fido_rp_t *); 54 cbor_item_t *cbor_encode_user_entity(const fido_user_t *); 55 cbor_item_t *es256_pk_encode(const es256_pk_t *, int); 56 57 /* cbor decoding functions */ 58 int cbor_decode_attstmt(const cbor_item_t *, fido_attstmt_t *); 59 int cbor_decode_cred_authdata(const cbor_item_t *, int, fido_blob_t *, 60 fido_authdata_t *, fido_attcred_t *, fido_cred_ext_t *); 61 int cbor_decode_assert_authdata(const cbor_item_t *, fido_blob_t *, 62 fido_authdata_t *, fido_assert_extattr_t *); 63 int cbor_decode_cred_id(const cbor_item_t *, fido_blob_t *); 64 int cbor_decode_fmt(const cbor_item_t *, char **); 65 int cbor_decode_pubkey(const cbor_item_t *, int *, void *); 66 int cbor_decode_rp_entity(const cbor_item_t *, fido_rp_t *); 67 int cbor_decode_uint64(const cbor_item_t *, uint64_t *); 68 int cbor_decode_user(const cbor_item_t *, fido_user_t *); 69 int es256_pk_decode(const cbor_item_t *, es256_pk_t *); 70 int rs256_pk_decode(const cbor_item_t *, rs256_pk_t *); 71 int eddsa_pk_decode(const cbor_item_t *, eddsa_pk_t *); 72 73 /* auxiliary cbor routines */ 74 int cbor_add_bool(cbor_item_t *, const char *, fido_opt_t); 75 int cbor_add_bytestring(cbor_item_t *, const char *, const unsigned char *, 76 size_t); 77 int cbor_add_string(cbor_item_t *, const char *, const char *); 78 int cbor_array_iter(const cbor_item_t *, void *, int(*)(const cbor_item_t *, 79 void *)); 80 int cbor_build_frame(uint8_t, cbor_item_t *[], size_t, fido_blob_t *); 81 int cbor_bytestring_copy(const cbor_item_t *, unsigned char **, size_t *); 82 int cbor_map_iter(const cbor_item_t *, void *, int(*)(const cbor_item_t *, 83 const cbor_item_t *, void *)); 84 int cbor_string_copy(const cbor_item_t *, char **); 85 int cbor_parse_reply(const unsigned char *, size_t, void *, 86 int(*)(const cbor_item_t *, const cbor_item_t *, void *)); 87 int cbor_add_uv_params(fido_dev_t *, uint8_t, const fido_blob_t *, 88 const es256_pk_t *, const fido_blob_t *, const char *, const char *, 89 cbor_item_t **, cbor_item_t **); 90 void cbor_vector_free(cbor_item_t **, size_t); 91 int cbor_array_append(cbor_item_t **, cbor_item_t *); 92 int cbor_array_drop(cbor_item_t **, size_t); 93 94 /* deflate */ 95 int fido_compress(fido_blob_t *, const fido_blob_t *); 96 int fido_uncompress(fido_blob_t *, const fido_blob_t *, size_t); 97 98 #ifndef nitems 99 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0])) 100 #endif 101 102 /* buf */ 103 int fido_buf_read(const unsigned char **, size_t *, void *, size_t); 104 int fido_buf_write(unsigned char **, size_t *, const void *, size_t); 105 106 /* hid i/o */ 107 void *fido_hid_open(const char *); 108 void fido_hid_close(void *); 109 int fido_hid_read(void *, unsigned char *, size_t, int); 110 int fido_hid_write(void *, const unsigned char *, size_t); 111 int fido_hid_get_usage(const uint8_t *, size_t, uint32_t *); 112 int fido_hid_get_report_len(const uint8_t *, size_t, size_t *, size_t *); 113 int fido_hid_unix_open(const char *); 114 int fido_hid_unix_wait(int, int, const fido_sigset_t *); 115 int fido_hid_set_sigmask(void *, const fido_sigset_t *); 116 size_t fido_hid_report_in_len(void *); 117 size_t fido_hid_report_out_len(void *); 118 119 /* nfc i/o */ 120 void *fido_nfc_open(const char *); 121 void fido_nfc_close(void *); 122 int fido_nfc_read(void *, unsigned char *, size_t, int); 123 int fido_nfc_write(void *, const unsigned char *, size_t); 124 int fido_nfc_rx(fido_dev_t *, uint8_t, unsigned char *, size_t, int); 125 int fido_nfc_tx(fido_dev_t *, uint8_t, const unsigned char *, size_t); 126 int fido_nfc_set_sigmask(void *, const fido_sigset_t *); 127 128 /* windows hello */ 129 int fido_winhello_manifest(fido_dev_info_t *, size_t, size_t *); 130 int fido_winhello_open(fido_dev_t *); 131 int fido_winhello_close(fido_dev_t *); 132 int fido_winhello_cancel(fido_dev_t *); 133 int fido_winhello_get_assert(fido_dev_t *, fido_assert_t *, const char *); 134 int fido_winhello_get_cbor_info(fido_dev_t *, fido_cbor_info_t *); 135 int fido_winhello_make_cred(fido_dev_t *, fido_cred_t *, const char *); 136 137 /* generic i/o */ 138 int fido_rx_cbor_status(fido_dev_t *, int); 139 int fido_rx(fido_dev_t *, uint8_t, void *, size_t, int); 140 int fido_tx(fido_dev_t *, uint8_t, const void *, size_t); 141 142 /* log */ 143 #ifdef FIDO_NO_DIAGNOSTIC 144 #define fido_log_init(...) do { /* nothing */ } while (0) 145 #define fido_log_debug(...) do { /* nothing */ } while (0) 146 #define fido_log_xxd(...) do { /* nothing */ } while (0) 147 #define fido_log_error(...) do { /* nothing */ } while (0) 148 #else 149 #ifdef __GNUC__ 150 void fido_log_init(void); 151 void fido_log_debug(const char *, ...) 152 __attribute__((__format__ (printf, 1, 2))); 153 void fido_log_xxd(const void *, size_t, const char *, ...) 154 __attribute__((__format__ (printf, 3, 4))); 155 void fido_log_error(int, const char *, ...) 156 __attribute__((__format__ (printf, 2, 3))); 157 #else 158 void fido_log_init(void); 159 void fido_log_debug(const char *, ...); 160 void fido_log_xxd(const void *, size_t, const char *, ...); 161 void fido_log_error(int, const char *, ...); 162 #endif /* __GNUC__ */ 163 #endif /* FIDO_NO_DIAGNOSTIC */ 164 165 /* u2f */ 166 int u2f_register(fido_dev_t *, fido_cred_t *, int); 167 int u2f_authenticate(fido_dev_t *, fido_assert_t *, int); 168 int u2f_get_touch_begin(fido_dev_t *); 169 int u2f_get_touch_status(fido_dev_t *, int *, int); 170 171 /* unexposed fido ops */ 172 uint8_t fido_dev_get_pin_protocol(const fido_dev_t *); 173 int fido_dev_authkey(fido_dev_t *, es256_pk_t *); 174 int fido_dev_get_cbor_info_wait(fido_dev_t *, fido_cbor_info_t *, int); 175 int fido_dev_get_uv_token(fido_dev_t *, uint8_t, const char *, 176 const fido_blob_t *, const es256_pk_t *, const char *, fido_blob_t *); 177 uint64_t fido_dev_maxmsgsize(const fido_dev_t *); 178 int fido_do_ecdh(fido_dev_t *, es256_pk_t **, fido_blob_t **); 179 bool fido_dev_supports_permissions(const fido_dev_t *); 180 181 /* misc */ 182 void fido_assert_reset_rx(fido_assert_t *); 183 void fido_assert_reset_tx(fido_assert_t *); 184 void fido_cred_reset_rx(fido_cred_t *); 185 void fido_cred_reset_tx(fido_cred_t *); 186 void fido_cbor_info_reset(fido_cbor_info_t *); 187 int fido_blob_serialise(fido_blob_t *, const cbor_item_t *); 188 int fido_check_flags(uint8_t, fido_opt_t, fido_opt_t); 189 int fido_check_rp_id(const char *, const unsigned char *); 190 int fido_get_random(void *, size_t); 191 int fido_sha256(fido_blob_t *, const u_char *, size_t); 192 193 /* crypto */ 194 int fido_verify_sig_es256(const fido_blob_t *, const es256_pk_t *, 195 const fido_blob_t *); 196 int fido_verify_sig_rs256(const fido_blob_t *, const rs256_pk_t *, 197 const fido_blob_t *); 198 int fido_verify_sig_eddsa(const fido_blob_t *, const eddsa_pk_t *, 199 const fido_blob_t *); 200 int fido_get_signed_hash(int, fido_blob_t *, const fido_blob_t *, 201 const fido_blob_t *); 202 203 /* device manifest functions */ 204 int fido_hid_manifest(fido_dev_info_t *, size_t, size_t *); 205 int fido_nfc_manifest(fido_dev_info_t *, size_t, size_t *); 206 207 /* device manifest registration */ 208 typedef int (*dev_manifest_func_t)(fido_dev_info_t *, size_t, size_t *); 209 int fido_dev_register_manifest_func(const dev_manifest_func_t); 210 void fido_dev_unregister_manifest_func(const dev_manifest_func_t); 211 212 /* fuzzing instrumentation */ 213 #ifdef FIDO_FUZZ 214 uint32_t uniform_random(uint32_t); 215 #endif 216 217 /* internal device capability flags */ 218 #define FIDO_DEV_PIN_SET 0x001 219 #define FIDO_DEV_PIN_UNSET 0x002 220 #define FIDO_DEV_CRED_PROT 0x004 221 #define FIDO_DEV_CREDMAN 0x008 222 #define FIDO_DEV_PIN_PROTOCOL1 0x010 223 #define FIDO_DEV_PIN_PROTOCOL2 0x020 224 #define FIDO_DEV_UV_SET 0x040 225 #define FIDO_DEV_UV_UNSET 0x080 226 #define FIDO_DEV_TOKEN_PERMS 0x100 227 #define FIDO_DEV_WINHELLO 0x200 228 229 /* miscellanea */ 230 #define FIDO_DUMMY_CLIENTDATA "" 231 #define FIDO_DUMMY_RP_ID "localhost" 232 #define FIDO_DUMMY_USER_NAME "dummy" 233 #define FIDO_DUMMY_USER_ID 1 234 #define FIDO_WINHELLO_PATH "windows://hello" 235 236 #ifdef __cplusplus 237 } /* extern "C" */ 238 #endif /* __cplusplus */ 239 240 #endif /* !_EXTERN_H */ 241