Skip to content

Commit adc9355

Browse files
committed
refactor(euicc): extract parse handle notification as function
1 parent 2768143 commit adc9355

File tree

4 files changed

+142
-98
lines changed

4 files changed

+142
-98
lines changed

euicc/CMakeLists.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ target_sources(
1616
es9p_errors.c
1717
es10a.c
1818
es10b.c
19+
es10b_ex.c
1920
es10c.c
2021
es10c_ex.c
2122
euicc.c
@@ -43,17 +44,18 @@ target_sources(
4344
BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/..
4445
FILES
4546
base64.h
46-
es8p.h
47-
es9p_errors.h
48-
es10b.h
49-
es10c_ex.h
5047
interface.h
5148
logger.h
5249
tostr.h
5350
derutil.h
51+
es8p.h
5452
es9p.h
53+
es9p_errors.h
5554
es10a.h
55+
es10b.h
56+
es10b_ex.h
5657
es10c.h
58+
es10c_ex.h
5759
euicc.h
5860
hexutil.h
5961
sha256.h

euicc/es10b.c

Lines changed: 6 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "base64.h"
55
#include "derutil.h"
6+
#include "es10b_ex.h"
67
#include "hexutil.h"
78
#include "sha256.h"
89

@@ -177,101 +178,12 @@ static int es10b_load_bound_profile_package_tx(struct euicc_ctx *ctx,
177178
goto err;
178179
}
179180

180-
if (resplen > 0) {
181-
struct euicc_derutil_node tmpnode, n_notificationMetadata, n_sequenceNumber, n_finalResult;
182-
183-
if (euicc_derutil_unpack_find_tag(&tmpnode, 0xBF37, respbuf, resplen) < 0) // ProfileInstallationResult
184-
{
185-
goto err;
186-
}
187-
188-
if (euicc_derutil_unpack_find_tag(&tmpnode, 0xBF27, tmpnode.value, tmpnode.length)
189-
< 0) // ProfileInstallationResultData
190-
{
191-
goto err;
192-
}
193-
194-
if (euicc_derutil_unpack_find_tag(&n_notificationMetadata, 0xBF2F, tmpnode.value,
195-
tmpnode.length)
196-
< 0) // NotificationMetadata
197-
{
198-
goto err;
199-
}
200-
201-
if (euicc_derutil_unpack_find_tag(&tmpnode, 0xA2, tmpnode.value, tmpnode.length) < 0) // finalResult
202-
{
203-
goto err;
204-
}
205-
206-
if (euicc_derutil_unpack_first(&n_finalResult, tmpnode.value, tmpnode.length) < 0) {
207-
goto err;
208-
}
209-
210-
if (euicc_derutil_unpack_find_tag(&n_sequenceNumber, 0x80, n_notificationMetadata.value,
211-
n_notificationMetadata.length)
212-
== 0) {
213-
result->seqNumber = euicc_derutil_convert_bin2long(n_sequenceNumber.value, n_sequenceNumber.length);
214-
}
181+
if (resplen == 0) {
182+
goto exit;
183+
}
215184

216-
switch (n_finalResult.tag) {
217-
case 0xA0: // SuccessResult
218-
break;
219-
case 0xA1: // ErrorResult
220-
tmpnode.self.ptr = n_finalResult.value;
221-
tmpnode.self.length = 0;
222-
while (euicc_derutil_unpack_next(&tmpnode, &tmpnode, n_finalResult.value, n_finalResult.length) == 0) {
223-
long tmpint;
224-
switch (tmpnode.tag) {
225-
case 0x80:
226-
tmpint = euicc_derutil_convert_bin2long(tmpnode.value, tmpnode.length);
227-
switch (tmpint) {
228-
case ES10B_BPP_COMMAND_ID_INITIALISE_SECURE_CHANNEL:
229-
case ES10B_BPP_COMMAND_ID_CONFIGURE_ISDP:
230-
case ES10B_BPP_COMMAND_ID_STORE_METADATA:
231-
case ES10B_BPP_COMMAND_ID_STORE_METADATA2:
232-
case ES10B_BPP_COMMAND_ID_REPLACE_SESSION_KEYS:
233-
case ES10B_BPP_COMMAND_ID_LOAD_PROFILE_ELEMENTS:
234-
result->bppCommandId = tmpint;
235-
break;
236-
default:
237-
result->bppCommandId = ES10B_BPP_COMMAND_ID_UNDEFINED;
238-
break;
239-
}
240-
break;
241-
case 0x81:
242-
tmpint = euicc_derutil_convert_bin2long(tmpnode.value, tmpnode.length);
243-
switch (tmpint) {
244-
case ES10B_ERROR_REASON_INCORRECT_INPUT_VALUES:
245-
case ES10B_ERROR_REASON_INVALID_SIGNATURE:
246-
case ES10B_ERROR_REASON_INVALID_TRANSACTION_ID:
247-
case ES10B_ERROR_REASON_UNSUPPORTED_CRT_VALUES:
248-
case ES10B_ERROR_REASON_UNSUPPORTED_REMOTE_OPERATION_TYPE:
249-
case ES10B_ERROR_REASON_UNSUPPORTED_PROFILE_CLASS:
250-
case ES10B_ERROR_REASON_SCP03T_STRUCTURE_ERROR:
251-
case ES10B_ERROR_REASON_SCP03T_SECURITY_ERROR:
252-
case ES10B_ERROR_REASON_INSTALL_FAILED_DUE_TO_ICCID_ALREADY_EXISTS_ON_EUICC:
253-
case ES10B_ERROR_REASON_INSTALL_FAILED_DUE_TO_INSUFFICIENT_MEMORY_FOR_PROFILE:
254-
case ES10B_ERROR_REASON_INSTALL_FAILED_DUE_TO_INTERRUPTION:
255-
case ES10B_ERROR_REASON_INSTALL_FAILED_DUE_TO_PE_PROCESSING_ERROR:
256-
case ES10B_ERROR_REASON_INSTALL_FAILED_DUE_TO_DATA_MISMATCH:
257-
case ES10B_ERROR_REASON_TEST_PROFILE_INSTALL_FAILED_DUE_TO_INVALID_NAA_KEY:
258-
case ES10B_ERROR_REASON_PPR_NOT_ALLOWED:
259-
case ES10B_ERROR_REASON_INSTALL_FAILED_DUE_TO_UNKNOWN_ERROR:
260-
result->errorReason = tmpint;
261-
break;
262-
default:
263-
result->errorReason = ES10B_ERROR_REASON_UNDEFINED;
264-
break;
265-
}
266-
break;
267-
default:
268-
break;
269-
}
270-
}
271-
goto err;
272-
default:
273-
goto err;
274-
}
185+
if (es10b_parse_handle_notification(respbuf, resplen, result) < 0) {
186+
goto err;
275187
}
276188

277189
fret = 0;

euicc/es10b_ex.c

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#include "es10b_ex.h"
2+
3+
#include "base64.h"
4+
#include "derutil.h"
5+
6+
#include <stdlib.h>
7+
8+
int es10b_parse_pending_notification(const struct es10b_pending_notification *notification,
9+
struct es10b_load_bound_profile_package_result *result) {
10+
int fret = 0;
11+
const int buf_len = euicc_base64_decode_len(notification->b64_PendingNotification);
12+
uint8_t *buf = calloc(sizeof(uint8_t), buf_len);
13+
if (euicc_base64_decode(buf, notification->b64_PendingNotification) < 0) {
14+
goto err;
15+
}
16+
if (es10b_parse_handle_notification(buf, buf_len, result) < 0) {
17+
goto err;
18+
}
19+
goto exit;
20+
err:
21+
fret = -1;
22+
exit:
23+
free(buf);
24+
buf = NULL;
25+
return fret;
26+
}
27+
28+
int es10b_parse_handle_notification(const uint8_t *buf, const unsigned int buf_len,
29+
struct es10b_load_bound_profile_package_result *result) {
30+
struct euicc_derutil_node tmpnode, n_notificationMetadata, n_sequenceNumber, n_finalResult;
31+
32+
if (euicc_derutil_unpack_find_tag(&tmpnode, 0xBF37, buf, buf_len) < 0) {
33+
goto err; // ProfileInstallationResult
34+
}
35+
36+
if (euicc_derutil_unpack_find_tag(&tmpnode, 0xBF27, tmpnode.value, tmpnode.length) < 0) {
37+
goto err; // ProfileInstallationResultData
38+
}
39+
40+
if (euicc_derutil_unpack_find_tag(&n_notificationMetadata, 0xBF2F, tmpnode.value, tmpnode.length) < 0) {
41+
goto err; // NotificationMetadata
42+
}
43+
44+
if (euicc_derutil_unpack_find_tag(&tmpnode, 0xA2, tmpnode.value, tmpnode.length) < 0) {
45+
goto err; // finalResult
46+
}
47+
48+
if (euicc_derutil_unpack_first(&n_finalResult, tmpnode.value, tmpnode.length) < 0) {
49+
goto err;
50+
}
51+
52+
if (euicc_derutil_unpack_find_tag(&n_sequenceNumber, 0x80, n_notificationMetadata.value,
53+
n_notificationMetadata.length)
54+
== 0) {
55+
result->seqNumber = euicc_derutil_convert_bin2long(n_sequenceNumber.value, n_sequenceNumber.length);
56+
}
57+
58+
switch (n_finalResult.tag) {
59+
case 0xA0: // SuccessResult
60+
break;
61+
case 0xA1: // ErrorResult
62+
tmpnode.self.ptr = n_finalResult.value;
63+
tmpnode.self.length = 0;
64+
while (euicc_derutil_unpack_next(&tmpnode, &tmpnode, n_finalResult.value, n_finalResult.length) == 0) {
65+
long tmpint;
66+
switch (tmpnode.tag) {
67+
case 0x80:
68+
tmpint = euicc_derutil_convert_bin2long(tmpnode.value, tmpnode.length);
69+
switch (tmpint) {
70+
case ES10B_BPP_COMMAND_ID_INITIALISE_SECURE_CHANNEL:
71+
case ES10B_BPP_COMMAND_ID_CONFIGURE_ISDP:
72+
case ES10B_BPP_COMMAND_ID_STORE_METADATA:
73+
case ES10B_BPP_COMMAND_ID_STORE_METADATA2:
74+
case ES10B_BPP_COMMAND_ID_REPLACE_SESSION_KEYS:
75+
case ES10B_BPP_COMMAND_ID_LOAD_PROFILE_ELEMENTS:
76+
result->bppCommandId = tmpint;
77+
break;
78+
default:
79+
result->bppCommandId = ES10B_BPP_COMMAND_ID_UNDEFINED;
80+
break;
81+
}
82+
break;
83+
case 0x81:
84+
tmpint = euicc_derutil_convert_bin2long(tmpnode.value, tmpnode.length);
85+
switch (tmpint) {
86+
case ES10B_ERROR_REASON_INCORRECT_INPUT_VALUES:
87+
case ES10B_ERROR_REASON_INVALID_SIGNATURE:
88+
case ES10B_ERROR_REASON_INVALID_TRANSACTION_ID:
89+
case ES10B_ERROR_REASON_UNSUPPORTED_CRT_VALUES:
90+
case ES10B_ERROR_REASON_UNSUPPORTED_REMOTE_OPERATION_TYPE:
91+
case ES10B_ERROR_REASON_UNSUPPORTED_PROFILE_CLASS:
92+
case ES10B_ERROR_REASON_SCP03T_STRUCTURE_ERROR:
93+
case ES10B_ERROR_REASON_SCP03T_SECURITY_ERROR:
94+
case ES10B_ERROR_REASON_INSTALL_FAILED_DUE_TO_ICCID_ALREADY_EXISTS_ON_EUICC:
95+
case ES10B_ERROR_REASON_INSTALL_FAILED_DUE_TO_INSUFFICIENT_MEMORY_FOR_PROFILE:
96+
case ES10B_ERROR_REASON_INSTALL_FAILED_DUE_TO_INTERRUPTION:
97+
case ES10B_ERROR_REASON_INSTALL_FAILED_DUE_TO_PE_PROCESSING_ERROR:
98+
case ES10B_ERROR_REASON_INSTALL_FAILED_DUE_TO_DATA_MISMATCH:
99+
case ES10B_ERROR_REASON_TEST_PROFILE_INSTALL_FAILED_DUE_TO_INVALID_NAA_KEY:
100+
case ES10B_ERROR_REASON_PPR_NOT_ALLOWED:
101+
case ES10B_ERROR_REASON_INSTALL_FAILED_DUE_TO_UNKNOWN_ERROR:
102+
result->errorReason = tmpint;
103+
break;
104+
default:
105+
result->errorReason = ES10B_ERROR_REASON_UNDEFINED;
106+
break;
107+
}
108+
break;
109+
default:
110+
break;
111+
}
112+
}
113+
goto err;
114+
default: // Unexpected tag
115+
goto err;
116+
}
117+
return 0;
118+
err:
119+
return -1;
120+
}

euicc/es10b_ex.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
3+
#include "es10b.h"
4+
#include <stdbool.h>
5+
6+
int es10b_parse_pending_notification(const struct es10b_pending_notification *,
7+
struct es10b_load_bound_profile_package_result *);
8+
9+
int es10b_parse_handle_notification(const uint8_t *buf, unsigned int buf_len,
10+
struct es10b_load_bound_profile_package_result *result);

0 commit comments

Comments
 (0)