1
+ /*****************************************************************************
2
+ * MIT License
3
+ *
4
+ * Copyright (c) 2023 coderofstuff
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ *****************************************************************************/
24
+ #include <stdint.h> // uint*_t
25
+ #include <string.h> // memset, explicit_bzero
26
+
27
+ #include "types.h"
28
+ #include "buffer.h"
29
+ #include "./globals.h"
30
+ #include "./sign_msg.h"
31
+ #include "../sw.h"
32
+ #include "../ui/display.h"
33
+ #include "../helper/send_response.h"
34
+
35
+ /**
36
+ * Handler for SIGN_MESSAGE command. If successfully parse BIP32 path
37
+ * and message, sign the message and send APDU response.
38
+ *
39
+ * @see G_context.bip32_path, G_context.msg_info
40
+ *
41
+ * @param[in,out] cdata
42
+ * Command data with BIP32 path and raw message.
43
+ *
44
+ * @return zero or positive integer if success, negative integer otherwise.
45
+ *
46
+ */
47
+ int handler_sign_msg (buffer_t * cdata ) {
48
+ explicit_bzero (& G_context , sizeof (G_context ));
49
+ G_context .req_type = CONFIRM_MESSAGE ;
50
+ G_context .state = STATE_NONE ;
51
+
52
+ if (!buffer_read_u8 (cdata , & G_context .msg_info .address_type )) {
53
+ return io_send_sw (SW_WRONG_DATA_LENGTH );
54
+ }
55
+
56
+ if (!buffer_read_u32 (cdata , & G_context .msg_info .address_index , BE )) {
57
+ return io_send_sw (SW_WRONG_DATA_LENGTH );
58
+ }
59
+
60
+ uint8_t message_len = 0 ;
61
+ if (!buffer_read_u8 (cdata , & message_len )) {
62
+ return io_send_sw (SW_WRONG_DATA_LENGTH );
63
+ }
64
+
65
+ if (message_len > MAX_MESSAGE_LEN ) {
66
+ return io_send_sw (SW_MESSAGE_TOO_LONG );
67
+ }
68
+
69
+ G_context .msg_info .message_len = (size_t ) message_len ;
70
+
71
+ if (!buffer_can_read (cdata , G_context .msg_info .message_len )) {
72
+ return io_send_sw (SW_WRONG_DATA_LENGTH );
73
+ }
74
+
75
+ memcpy (G_context .msg_info .message , cdata -> ptr + cdata -> offset , G_context .msg_info .message_len );
76
+
77
+ if (!buffer_seek_cur (cdata , G_context .msg_info .message_len )) {
78
+ return io_send_sw (SW_WRONG_DATA_LENGTH );
79
+ }
80
+
81
+ G_context .bip32_path [0 ] = 0x8000002C ;
82
+ G_context .bip32_path [1 ] = 0x8001b207 ;
83
+ G_context .bip32_path [2 ] = 0x80000000 ;
84
+ G_context .bip32_path [3 ] = (uint32_t )(G_context .msg_info .address_type );
85
+ G_context .bip32_path [4 ] = G_context .msg_info .address_index ;
86
+
87
+ G_context .bip32_path_len = 5 ;
88
+
89
+ return ui_display_message ();
90
+ }
0 commit comments