1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2017-2020, Linaro Limited
4  */
5 
6 #ifndef PKCS11_TA_SERIALIZER_H
7 #define PKCS11_TA_SERIALIZER_H
8 
9 #include <pkcs11_ta.h>
10 #include <stdbool.h>
11 #include <stdint.h>
12 
13 struct pkcs11_client;
14 struct pkcs11_session;
15 
16 /*
17  * Util routines for serializes unformated arguments in a client memref
18  */
19 struct serialargs {
20 	char *start;
21 	char *next;
22 	size_t size;
23 };
24 
25 struct pkcs11_client;
26 struct pkcs11_session;
27 
28 /*
29  * serialargs_init() - Initialize with a new input buffer
30  * @args:	serializing state
31  * @in:		input buffer
32  * @size:	size of the input buffer
33  */
34 void serialargs_init(struct serialargs *args, void *in, size_t size);
35 
36 /*
37  * serialargs_get() - copy out a chunk of data and advance
38  * @args:	serializing state
39  * @out:	output buffer
40  * @sz:		number of bytes to copy to output buffer
41  *
42  * Returns PKCS11_CKR_OK on success or PKCS11_CKR_ARGUMENTS_BAD on failure.
43  */
44 enum pkcs11_rc serialargs_get(struct serialargs *args, void *out, size_t sz);
45 
46 /*
47  * serialargs_get_u32() - copy out a uint32_t and advance
48  * @args:	serializing state
49  * @out:	output buffer
50  *
51  * Returns PKCS11_CKR_OK on success or PKCS11_CKR_ARGUMENTS_BAD on failure.
52  */
serialargs_get_u32(struct serialargs * args,uint32_t * out)53 static inline enum pkcs11_rc serialargs_get_u32(struct serialargs *args,
54 						uint32_t *out)
55 {
56 	return serialargs_get(args, out, sizeof(*out));
57 }
58 
59 /*
60  * serialargs_get_ptr() - get a pointer to a chunk of data and advance
61  * @args:	serializing state
62  * @out:	Pointer to the data retrieved in *@out
63  * @size:	Number of bytes to advance
64  *
65  * Returns PKCS11_CKR_OK on success or PKCS11_CKR_ARGUMENTS_BAD on failure.
66  */
67 enum pkcs11_rc serialargs_get_ptr(struct serialargs *args, void **out,
68 				  size_t size);
69 
70 /*
71  * serialargs_alloc_get_one_attribute() - allocate and extract one attribute
72  * @args:	serializing state
73  * @out:	Pointer to the allocated and extracted attribute in *@out
74  *
75  * Returns PKCS11_CKR_OK on success or an error code from enum pkcs11_rc on
76  * failure.
77  */
78 enum pkcs11_rc
79 serialargs_alloc_get_one_attribute(struct serialargs *args,
80 				   struct pkcs11_attribute_head **out);
81 
82 /*
83  * serialargs_alloc_get_attributes() - allocate and extract an object
84  * @args:	serializing state
85  * @out:	Pointer to the allocated and extracted object in *@out
86  *
87  * Returns PKCS11_CKR_OK on success or an error code from enum pkcs11_rc on
88  * failure.
89  */
90 enum pkcs11_rc serialargs_alloc_get_attributes(struct serialargs *args,
91 					       struct pkcs11_object_head **out);
92 
93 /*
94  * serialargs_alloc_and_get() - allocate and extract data
95  * @args:	serializing state
96  * @out:	Pointer to the allocated and extracted data in *@out
97  * @size:	Number of bytes to extract
98  *
99  * Returns PKCS11_CKR_OK on success or an error code from enum pkcs11_rc on
100  * failure.
101  */
102 enum pkcs11_rc serialargs_alloc_and_get(struct serialargs *args,
103 					void **out, size_t size);
104 
105 /*
106  * serialargs_remaining_bytes() - check for remaining bytes
107  * @args:	serializing state
108  *
109  * Returns true if there are remaining bytes in @args or false if all bytes
110  * are consumed.
111  */
112 bool serialargs_remaining_bytes(struct serialargs *args);
113 
114 /*
115  * serialargs_get_session_from_handle() - extract and verify session
116  * @args:	serializing state
117  * @client:	client state
118  * @sess:	The retrieved session handle is available in *@sess
119  *
120  * Returns PKCS11_CKR_OK on success or an error code from enum pkcs11_rc on
121  * failure.
122  */
123 enum pkcs11_rc serialargs_get_session_from_handle(struct serialargs *args,
124 						  struct pkcs11_client *client,
125 						  struct pkcs11_session **sess);
126 
127 /*
128  * serialize() - append data into a serialized buffer
129  * @bstart:	points to start of a buffer or NULL, *@bstart is updated
130  *		with the new buffer if changed
131  * @blen:	size of the *@bstart buffer, updated when data is added
132  * @data:	data to appen to the buffer
133  * @len:	size of the @data buffer
134  *
135  * Returns PKCS11_CKR_OK on success or an error code from enum pkcs11_rc on
136  * failure.
137  */
138 enum pkcs11_rc serialize(char **bstart, size_t *blen, void *data, size_t len);
139 
140 #endif /*PKCS11_TA_SERIALIZER_H*/
141