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 use crate::{unwrap, PanicReason};
16 use np_ffi_core::common::FixedSizeArray;
17 use np_ffi_core::deserialize::v1::*;
18 use np_ffi_core::deserialize::DecryptMetadataResult;
19 use np_ffi_core::utils::FfiEnum;
20 
21 /// Gets the number of legible sections on a deserialized V1 advertisement.
22 /// Suitable as an index bound for the second argument of
23 /// `np_ffi_DeserializedV1Advertisement#get_section`.
24 #[no_mangle]
np_ffi_DeserializedV1Advertisement_get_num_legible_sections( adv: DeserializedV1Advertisement, ) -> u825 pub extern "C" fn np_ffi_DeserializedV1Advertisement_get_num_legible_sections(
26     adv: DeserializedV1Advertisement,
27 ) -> u8 {
28     adv.num_legible_sections()
29 }
30 
31 /// Gets the number of sections on a deserialized V1 advertisement which
32 /// were unable to be decrypted with the credentials that the receiver possesses.
33 #[no_mangle]
np_ffi_DeserializedV1Advertisement_get_num_undecryptable_sections( adv: DeserializedV1Advertisement, ) -> u834 pub extern "C" fn np_ffi_DeserializedV1Advertisement_get_num_undecryptable_sections(
35     adv: DeserializedV1Advertisement,
36 ) -> u8 {
37     adv.num_undecryptable_sections()
38 }
39 
40 /// Gets the legible section with the given index in a deserialized V1 advertisement.
41 #[no_mangle]
np_ffi_DeserializedV1Advertisement_get_section( adv: DeserializedV1Advertisement, legible_section_index: u8, ) -> GetV1SectionResult42 pub extern "C" fn np_ffi_DeserializedV1Advertisement_get_section(
43     adv: DeserializedV1Advertisement,
44     legible_section_index: u8,
45 ) -> GetV1SectionResult {
46     adv.get_section(legible_section_index)
47 }
48 
49 /// Gets the tag of the `GetV1SectionResult` tagged-union.
50 #[no_mangle]
np_ffi_GetV1SectionResult_kind( result: GetV1SectionResult, ) -> GetV1SectionResultKind51 pub extern "C" fn np_ffi_GetV1SectionResult_kind(
52     result: GetV1SectionResult,
53 ) -> GetV1SectionResultKind {
54     result.kind()
55 }
56 
57 /// Casts a `GetV1SectionResult` to the `Success` variant, panicking in the
58 /// case where the passed value is of a different enum variant.
59 #[no_mangle]
np_ffi_GetV1SectionResult_into_SUCCESS( result: GetV1SectionResult, ) -> DeserializedV1Section60 pub extern "C" fn np_ffi_GetV1SectionResult_into_SUCCESS(
61     result: GetV1SectionResult,
62 ) -> DeserializedV1Section {
63     unwrap(result.into_success(), PanicReason::EnumCastFailed)
64 }
65 
66 /// Gets the number of data elements in a deserialized v1 section.
67 /// Suitable as an iteration bound for the second argument of
68 /// `np_ffi_DeserializedV1Section_get_de`.
69 #[no_mangle]
np_ffi_DeserializedV1Section_get_num_des(section: DeserializedV1Section) -> u870 pub extern "C" fn np_ffi_DeserializedV1Section_get_num_des(section: DeserializedV1Section) -> u8 {
71     section.num_des()
72 }
73 
74 /// Gets the tag of the identity tagged-union used for the passed section.
75 #[no_mangle]
np_ffi_DeserializedV1Section_get_identity_kind( section: DeserializedV1Section, ) -> DeserializedV1IdentityKind76 pub extern "C" fn np_ffi_DeserializedV1Section_get_identity_kind(
77     section: DeserializedV1Section,
78 ) -> DeserializedV1IdentityKind {
79     section.identity_kind()
80 }
81 
82 /// Gets the data-element with the given index in the passed section.
83 #[no_mangle]
np_ffi_DeserializedV1Section_get_de( section: DeserializedV1Section, de_index: u8, ) -> GetV1DEResult84 pub extern "C" fn np_ffi_DeserializedV1Section_get_de(
85     section: DeserializedV1Section,
86     de_index: u8,
87 ) -> GetV1DEResult {
88     section.get_de(de_index)
89 }
90 
91 /// Gets the identity details used to decrypt this V1 section, or returns an error if this payload
92 /// does not have any associated identity (public advertisement)
93 #[no_mangle]
np_ffi_DeserializedV1Section_get_identity_details( section: DeserializedV1Section, ) -> GetV1IdentityDetailsResult94 pub extern "C" fn np_ffi_DeserializedV1Section_get_identity_details(
95     section: DeserializedV1Section,
96 ) -> GetV1IdentityDetailsResult {
97     section.get_identity_details()
98 }
99 
100 /// Gets the tag of a `GetV1IdentityDetailsResult` tagged-union. On success the wrapped identity
101 /// details may be obtained via `GetV0IdentityDetailsResult#into_success`.
102 #[no_mangle]
np_ffi_GetV1IdentityDetailsResult_kind( result: GetV1IdentityDetailsResult, ) -> GetV1IdentityDetailsResultKind103 pub extern "C" fn np_ffi_GetV1IdentityDetailsResult_kind(
104     result: GetV1IdentityDetailsResult,
105 ) -> GetV1IdentityDetailsResultKind {
106     result.kind()
107 }
108 
109 /// Casts a `GetV1IdentityDetailsResult` to the `Success` variant, panicking in the
110 /// case where the passed value is of a different enum variant.
111 #[no_mangle]
np_ffi_GetV1IdentityDetailsResult_into_SUCCESS( result: GetV1IdentityDetailsResult, ) -> DeserializedV1IdentityDetails112 pub extern "C" fn np_ffi_GetV1IdentityDetailsResult_into_SUCCESS(
113     result: GetV1IdentityDetailsResult,
114 ) -> DeserializedV1IdentityDetails {
115     unwrap(result.into_success(), PanicReason::EnumCastFailed)
116 }
117 
118 /// Attempts to decrypt the metadata for the matched credential for this V0 payload (if any)
119 #[no_mangle]
np_ffi_DeserializedV1Section_decrypt_metadata( section: DeserializedV1Section, ) -> DecryptMetadataResult120 pub extern "C" fn np_ffi_DeserializedV1Section_decrypt_metadata(
121     section: DeserializedV1Section,
122 ) -> DecryptMetadataResult {
123     section.decrypt_metadata()
124 }
125 
126 /// Attempts to derive a 16-byte DE salt for a DE in this section with the given DE offset. This
127 /// operation may fail if the passed offset is 255 (causes overflow) or if the section
128 /// is leveraging a public identity, and hence, doesn't have an associated salt.
129 #[no_mangle]
np_ffi_DeserializedV1Section_derive_16_byte_salt_for_offset( section: DeserializedV1Section, offset: u8, ) -> GetV1DE16ByteSaltResult130 pub extern "C" fn np_ffi_DeserializedV1Section_derive_16_byte_salt_for_offset(
131     section: DeserializedV1Section,
132     offset: u8,
133 ) -> GetV1DE16ByteSaltResult {
134     section.derive_16_byte_salt_for_offset(offset)
135 }
136 
137 /// Gets the tag of a `GetV1DE16ByteSaltResult` tagged-union. On success the wrapped identity
138 /// details may be obtained via `GetV1DE16ByteSaltResult#into_success`.
139 #[no_mangle]
np_ffi_GetV1DE16ByteSaltResult_kind( result: GetV1DE16ByteSaltResult, ) -> GetV1DE16ByteSaltResultKind140 pub extern "C" fn np_ffi_GetV1DE16ByteSaltResult_kind(
141     result: GetV1DE16ByteSaltResult,
142 ) -> GetV1DE16ByteSaltResultKind {
143     result.kind()
144 }
145 
146 /// Casts a `GetV1DE16ByteSaltResult` to the `Success` variant, panicking in the
147 /// case where the passed value is of a different enum variant.
148 #[no_mangle]
np_ffi_GetV1DE16ByteSaltResult_into_SUCCESS( result: GetV1DE16ByteSaltResult, ) -> FixedSizeArray<16>149 pub extern "C" fn np_ffi_GetV1DE16ByteSaltResult_into_SUCCESS(
150     result: GetV1DE16ByteSaltResult,
151 ) -> FixedSizeArray<16> {
152     unwrap(result.into_success(), PanicReason::EnumCastFailed)
153 }
154 
155 /// Gets the tag of the `GetV1DEResult` tagged-union.
156 #[no_mangle]
np_ffi_GetV1DEResult_kind(result: GetV1DEResult) -> GetV1DEResultKind157 pub extern "C" fn np_ffi_GetV1DEResult_kind(result: GetV1DEResult) -> GetV1DEResultKind {
158     result.kind()
159 }
160 
161 /// Casts a `GetV1DEResult` to the `Success` variant, panicking in the
162 /// case where the passed value is of a different enum variant.
163 #[no_mangle]
np_ffi_GetV1DEResult_into_SUCCESS(result: GetV1DEResult) -> V1DataElement164 pub extern "C" fn np_ffi_GetV1DEResult_into_SUCCESS(result: GetV1DEResult) -> V1DataElement {
165     unwrap(result.into_success(), PanicReason::EnumCastFailed)
166 }
167