1 /*
2  * userpref.h
3  * contains methods to access user specific certificates IDs and more.
4  *
5  * Copyright (c) 2013-2014 Martin Szulecki All Rights Reserved.
6  * Copyright (c) 2008 Jonathan Beck All Rights Reserved.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22 
23 #ifndef __USERPREF_H
24 #define __USERPREF_H
25 
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29 
30 #ifdef HAVE_OPENSSL
31 typedef struct {
32 	unsigned char *data;
33 	unsigned int size;
34 } key_data_t;
35 #else
36 #include <gnutls/gnutls.h>
37 typedef gnutls_datum_t key_data_t;
38 #endif
39 
40 #include <stdint.h>
41 #include <plist/plist.h>
42 
43 #define USERPREF_DEVICE_CERTIFICATE_KEY "DeviceCertificate"
44 #define USERPREF_ESCROW_BAG_KEY "EscrowBag"
45 #define USERPREF_HOST_CERTIFICATE_KEY "HostCertificate"
46 #define USERPREF_ROOT_CERTIFICATE_KEY "RootCertificate"
47 #define USERPREF_HOST_PRIVATE_KEY_KEY "HostPrivateKey"
48 #define USERPREF_ROOT_PRIVATE_KEY_KEY "RootPrivateKey"
49 #define USERPREF_HOST_ID_KEY "HostID"
50 #define USERPREF_SYSTEM_BUID_KEY "SystemBUID"
51 #define USERPREF_WIFI_MAC_ADDRESS_KEY "WiFiMACAddress"
52 
53 /** Error Codes */
54 typedef enum {
55 	USERPREF_E_SUCCESS       =  0,
56 	USERPREF_E_INVALID_ARG   = -1,
57 	USERPREF_E_INVALID_CONF  = -2,
58 	USERPREF_E_SSL_ERROR     = -3,
59 	USERPREF_E_READ_ERROR    = -4,
60 	USERPREF_E_WRITE_ERROR   = -5,
61 	USERPREF_E_UNKNOWN_ERROR = -256
62 } userpref_error_t;
63 
64 const char *userpref_get_config_dir(void);
65 int userpref_read_system_buid(char **system_buid);
66 userpref_error_t userpref_read_pair_record(const char *udid, plist_t *pair_record);
67 userpref_error_t userpref_save_pair_record(const char *udid, uint32_t device_id, plist_t pair_record);
68 userpref_error_t userpref_delete_pair_record(const char *udid);
69 
70 userpref_error_t pair_record_generate_keys_and_certs(plist_t pair_record, key_data_t public_key);
71 #ifdef HAVE_OPENSSL
72 userpref_error_t pair_record_import_key_with_name(plist_t pair_record, const char* name, key_data_t* key);
73 userpref_error_t pair_record_import_crt_with_name(plist_t pair_record, const char* name, key_data_t* cert);
74 #else
75 userpref_error_t pair_record_import_key_with_name(plist_t pair_record, const char* name, gnutls_x509_privkey_t key);
76 userpref_error_t pair_record_import_crt_with_name(plist_t pair_record, const char* name, gnutls_x509_crt_t cert);
77 #endif
78 
79 userpref_error_t pair_record_get_host_id(plist_t pair_record, char** host_id);
80 userpref_error_t pair_record_set_host_id(plist_t pair_record, const char* host_id);
81 userpref_error_t pair_record_get_item_as_key_data(plist_t pair_record, const char* name, key_data_t *value);
82 userpref_error_t pair_record_set_item_from_key_data(plist_t pair_record, const char* name, key_data_t *value);
83 
84 /* deprecated */
85 userpref_error_t userpref_get_paired_udids(char ***list, unsigned int *count);
86 int userpref_has_pair_record(const char *udid);
87 
88 #endif
89