1 // Copyright 2023 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 //! NP Rust C FFI functionality for V1 advertisement serialization.
16
17 use crate::{unwrap, PanicReason};
18 use np_ffi_core::common::{ByteBuffer, FixedSizeArray};
19 use np_ffi_core::credentials::V1BroadcastCredential;
20 use np_ffi_core::serialize::v1::*;
21 use np_ffi_core::serialize::AdvertisementBuilderKind;
22 use np_ffi_core::utils::FfiEnum;
23 use np_ffi_core::v1::V1VerificationMode;
24
25 /// Attempts to create a builder for a new public section within
26 /// the advertisement builder behind this handle,
27 /// returning a handle to the newly-created section builder if successful.
28 ///
29 /// This method may fail if there is another currently-active
30 /// section builder for the same advertisement builder, if the
31 /// kind of section being added does not match the advertisement
32 /// type (public/encrypted), or if the section would not manage
33 /// to fit within the enclosing advertisement.
34 #[no_mangle]
np_ffi_V1AdvertisementBuilder_public_section_builder( adv_builder: V1AdvertisementBuilder, ) -> CreateV1SectionBuilderResult35 pub extern "C" fn np_ffi_V1AdvertisementBuilder_public_section_builder(
36 adv_builder: V1AdvertisementBuilder,
37 ) -> CreateV1SectionBuilderResult {
38 adv_builder.public_section_builder()
39 }
40
41 /// Attempts to create a builder for a new encrypted section within
42 /// the advertisement builder behind the given handle,
43 /// returning a handle to the newly-created section builder if successful.
44 ///
45 /// The identity details for the new section builder may be specified
46 /// via providing the broadcast credential data, the kind of encrypted
47 /// identity being broadcast (private/trusted/provisioned), and the
48 /// verification mode (MIC/Signature) to be used for the encrypted section.
49 ///
50 /// This method may fail if there is another currently-active
51 /// section builder for the same advertisement builder, if the
52 /// kind of section being added does not match the advertisement
53 /// type (public/encrypted), or if the section would not manage
54 /// to fit within the enclosing advertisement.
55 #[no_mangle]
np_ffi_V1AdvertisementBuilder_encrypted_section_builder( adv_builder: V1AdvertisementBuilder, broadcast_cred: V1BroadcastCredential, verification_mode: V1VerificationMode, ) -> CreateV1SectionBuilderResult56 pub extern "C" fn np_ffi_V1AdvertisementBuilder_encrypted_section_builder(
57 adv_builder: V1AdvertisementBuilder,
58 broadcast_cred: V1BroadcastCredential,
59 verification_mode: V1VerificationMode,
60 ) -> CreateV1SectionBuilderResult {
61 adv_builder.encrypted_section_builder(broadcast_cred, verification_mode)
62 }
63
64 /// Attempts to serialize the contents of the advertisement builder
65 /// behind this handle to bytes. Assuming that the handle is valid,
66 /// this operation will always result in the contents behind the
67 /// advertisement builder handle being deallocated.
68 #[no_mangle]
np_ffi_V1AdvertisementBuilder_into_advertisement( adv_builder: V1AdvertisementBuilder, ) -> SerializeV1AdvertisementResult69 pub extern "C" fn np_ffi_V1AdvertisementBuilder_into_advertisement(
70 adv_builder: V1AdvertisementBuilder,
71 ) -> SerializeV1AdvertisementResult {
72 adv_builder.into_advertisement()
73 }
74
75 /// Creates a new V1 advertisement builder for the given advertisement
76 /// kind (public/encrypted).
77 #[no_mangle]
np_ffi_create_v1_advertisement_builder( kind: AdvertisementBuilderKind, ) -> V1AdvertisementBuilder78 pub extern "C" fn np_ffi_create_v1_advertisement_builder(
79 kind: AdvertisementBuilderKind,
80 ) -> V1AdvertisementBuilder {
81 unwrap(
82 create_v1_advertisement_builder(kind).into_success(),
83 PanicReason::ExceededMaxHandleAllocations,
84 )
85 }
86
87 /// Gets the tag of a `SerializeV1AdvertisementResult` tagged-union.
88 #[no_mangle]
np_ffi_SerializeV1AdvertisementResult_kind( result: SerializeV1AdvertisementResult, ) -> SerializeV1AdvertisementResultKind89 pub extern "C" fn np_ffi_SerializeV1AdvertisementResult_kind(
90 result: SerializeV1AdvertisementResult,
91 ) -> SerializeV1AdvertisementResultKind {
92 result.kind()
93 }
94
95 /// Casts a `SerializeV1AdvertisementResult` to the `Success` variant,
96 /// panicking in the case where the passed value is of a different enum variant.
97 #[no_mangle]
np_ffi_SerializeV1AdvertisementResult_into_SUCCESS( result: SerializeV1AdvertisementResult, ) -> ByteBuffer<250>98 pub extern "C" fn np_ffi_SerializeV1AdvertisementResult_into_SUCCESS(
99 result: SerializeV1AdvertisementResult,
100 ) -> ByteBuffer<250> {
101 unwrap(result.into_success(), PanicReason::EnumCastFailed)
102 }
103
104 /// Gets the tag of a `CreateV1SectionBuilderResult` tagged-union.
105 #[no_mangle]
np_ffi_CreateV1SectionBuilderResult_kind( result: CreateV1SectionBuilderResult, ) -> CreateV1SectionBuilderResultKind106 pub extern "C" fn np_ffi_CreateV1SectionBuilderResult_kind(
107 result: CreateV1SectionBuilderResult,
108 ) -> CreateV1SectionBuilderResultKind {
109 result.kind()
110 }
111
112 /// Casts a `CreateV1SectionBuilderResult` to the `Success` variant,
113 /// panicking in the case where the passed value is of a different enum variant.
114 #[no_mangle]
np_ffi_CreateV1SectionBuilderResult_into_SUCCESS( result: CreateV1SectionBuilderResult, ) -> V1SectionBuilder115 pub extern "C" fn np_ffi_CreateV1SectionBuilderResult_into_SUCCESS(
116 result: CreateV1SectionBuilderResult,
117 ) -> V1SectionBuilder {
118 unwrap(result.into_success(), PanicReason::EnumCastFailed)
119 }
120
121 /// Gets the tag of a `NextV1DE16ByteSaltResult` tagged-union.
122 #[no_mangle]
np_ffi_NextV1DE16ByteSaltResult_kind( result: NextV1DE16ByteSaltResult, ) -> NextV1DE16ByteSaltResultKind123 pub extern "C" fn np_ffi_NextV1DE16ByteSaltResult_kind(
124 result: NextV1DE16ByteSaltResult,
125 ) -> NextV1DE16ByteSaltResultKind {
126 result.kind()
127 }
128
129 /// Casts a `NextV1DE16ByteSaltResult` to the `Success` variant,
130 /// panicking in the case where the passed value is of a different enum variant.
131 #[no_mangle]
np_ffi_NextV1DE16ByteSaltResult_into_SUCCESS( result: NextV1DE16ByteSaltResult, ) -> FixedSizeArray<16>132 pub extern "C" fn np_ffi_NextV1DE16ByteSaltResult_into_SUCCESS(
133 result: NextV1DE16ByteSaltResult,
134 ) -> FixedSizeArray<16> {
135 unwrap(result.into_success(), PanicReason::EnumCastFailed)
136 }
137
138 /// Adds the section constructed behind the given handle to
139 /// a section builder to the containing advertisement it originated from.
140 /// After this call, the section builder handle will become invalid.
141 #[no_mangle]
np_ffi_V1SectionBuilder_add_to_advertisement( section_builder: V1SectionBuilder, ) -> AddV1SectionToAdvertisementResult142 pub extern "C" fn np_ffi_V1SectionBuilder_add_to_advertisement(
143 section_builder: V1SectionBuilder,
144 ) -> AddV1SectionToAdvertisementResult {
145 section_builder.add_to_advertisement()
146 }
147
148 /// Attempts to get the derived 16-byte V1 DE salt for the next
149 /// DE to be added to the passed section builder. May fail if this
150 /// section builder handle is invalid, or if the section
151 /// is a public section.
152 #[no_mangle]
np_ffi_V1SectionBuilder_next_de_salt( section_builder: V1SectionBuilder, ) -> NextV1DE16ByteSaltResult153 pub extern "C" fn np_ffi_V1SectionBuilder_next_de_salt(
154 section_builder: V1SectionBuilder,
155 ) -> NextV1DE16ByteSaltResult {
156 section_builder.next_de_salt()
157 }
158
159 /// Attempts to add the given DE to the section builder behind
160 /// this handle. The passed DE may have a payload of up to 127
161 /// bytes, the maximum for a V1 DE.
162 #[no_mangle]
np_ffi_V1SectionBuilder_add_127_byte_buffer_de( section_builder: V1SectionBuilder, de: V1DE127ByteBuffer, ) -> AddV1DEResult163 pub extern "C" fn np_ffi_V1SectionBuilder_add_127_byte_buffer_de(
164 section_builder: V1SectionBuilder,
165 de: V1DE127ByteBuffer,
166 ) -> AddV1DEResult {
167 section_builder.add_127_byte_buffer_de(de)
168 }
169