iso14229 0.9.0
ISO14229-1 (UDS) C Library
Loading...
Searching...
No Matches
server.c
Go to the documentation of this file.
1/**
2 * @file examples/linux_server_0x27/server.c
3 * @brief UDS server demonstrating Security Access (0x27)
4 */
5#include "iso14229.h"
6#include <stdint.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <errno.h>
10#include <signal.h>
11#include <sys/time.h>
12#include <sys/types.h>
13#include <time.h>
14#include <mbedtls/config.h>
15#include <mbedtls/platform.h>
16#include <mbedtls/pk.h>
17#include <mbedtls/rsa.h>
18#include <mbedtls/sha256.h>
19#include <mbedtls/error.h>
20
21static UDSServer_t srv;
22#if defined(UDS_TP_ISOTP_SOCK)
23static UDSTpIsoTpSock_t tp;
24#elif defined(UDS_TP_ISOTP_C_SOCKETCAN)
25static UDSTpISOTpC_t tp;
26#else
27#error "no transport defined"
28#endif
29static bool done = false;
30static uint8_t seed[32] = {0};
31
32void sigint_handler(int signum) {
33 printf("SIGINT received\n");
34 done = true;
35}
36
37int rsa_verify(const uint8_t *key, size_t key_len, bool *valid) {
38 int ret = 0;
39 mbedtls_pk_context pk;
40 const char *pubkey = "public_key.pem";
41 mbedtls_pk_init(&pk);
42
43 if ((ret = mbedtls_pk_parse_public_keyfile(&pk, pubkey) != 0)) {
44 mbedtls_printf(" failed\n ! Could not read key from '%s'\n", pubkey);
45 mbedtls_printf(" ! mbedtls_pk_parse_public_keyfile returned %d\n\n", ret);
46 goto exit;
47 }
48
49 if ((ret = mbedtls_pk_verify(&pk, MBEDTLS_MD_SHA256, seed, sizeof(seed), key, key_len)) != 0) {
50 mbedtls_printf(" failed\n ! mbedtls_pk_verify returned %d\n\n", ret);
51 goto exit;
52 }
53
54exit:
55 *valid = (ret == 0);
56 // print the mbedtls error code
57 if (ret != 0) {
58 char buf[128];
59 mbedtls_strerror(ret, buf, sizeof(buf));
60 printf("mbedtls error: %s\n", buf);
61 }
62 return ret;
63}
64
65static UDSErr_t fn(UDSServer_t *srv, UDSEvent_t ev, void *arg) {
66 UDS_LOGI(__FILE__, "Server event: %s (%d)", UDSEventToStr(ev), ev);
67 switch (ev) {
70 UDS_LOGI(__FILE__, "Generating seed for level %d", req->level);
71 // use urandom to generate a random seed
72 FILE *f = fopen("/dev/urandom", "r");
73 if (!f) {
74 UDS_LOGE(__FILE__, "Failed to open /dev/urandom");
75 return UDS_NRC_GeneralReject;
76 }
77 fread(seed, sizeof(seed), 1, f);
78 fclose(f);
79 return req->copySeed(srv, seed, sizeof(seed));
80 }
83 bool valid = false;
84
85 UDS_LOGI(__FILE__, "Validating key, level=%d, len=%u", req->level, req->len);
86
87 if (0 != rsa_verify(req->key, req->len, &valid)) {
88 UDS_LOGE(__FILE__, "rsa_verify failed");
89 return UDS_NRC_GeneralReject;
90 } else {
91 if (valid) {
92 UDS_LOGI(__FILE__, "Security level %d unlocked", req->level);
93 return UDS_PositiveResponse;
94 } else {
95 UDS_LOGE(__FILE__, "Security access denied");
96 return UDS_NRC_SecurityAccessDenied;
97 }
98 }
99 }
100 default:
101 UDS_LOGW(__FILE__, "Unhandled event: %d", ev);
102 return UDS_OK;
103 }
104}
105
106static int sleep_ms(uint32_t tms) {
107 struct timespec ts;
108 int ret;
109 ts.tv_sec = tms / 1000;
110 ts.tv_nsec = (tms % 1000) * 1000000;
111 do {
112 ret = nanosleep(&ts, &ts);
113 } while (ret && errno == EINTR);
114 return ret;
115}
116
117int main(int ac, char **av) {
118 struct sigaction sa;
119 memset(&sa, 0, sizeof(sa));
120 sa.sa_handler = sigint_handler;
121 sigaction(SIGINT, &sa, NULL);
122
123#if defined(UDS_TP_ISOTP_SOCK)
124 if (UDSTpIsoTpSockInitServer(&tp, "vcan0", 0x7E0, 0x7E8, 0x7DF)) {
125 fprintf(stderr, "UDSTpIsoTpSockInitServer failed\n");
126 exit(-1);
127 }
128#elif defined(UDS_TP_ISOTP_C_SOCKETCAN)
129 if (UDSTpISOTpCInit((UDSTpISOTpC_t *)&tp, "vcan0", 0x7E0, 0x7E8, 0x7DF, 0x7FF)) {
130 fprintf(stderr, "UDSTpISOTpCInit failed\n");
131 exit(-1);
132 }
133#else
134#error "no transport defined"
135#endif
136
137 if (UDSServerInit(&srv)) {
138 fprintf(stderr, "UDSServerInit failed\n");
139 }
140
141 srv.tp = (UDSTp_t *)&tp;
142 srv.fn = fn;
143
144 printf("server up, polling . . .\n");
145 while (!done) {
146 UDSServerPoll(&srv);
147 sleep_ms(1);
148 }
149 printf("server exiting\n");
150 return 0;
151}
ISO14229-1 (UDS) library.
UDSEvent_t
UDS events.
Definition iso14229.h:298
@ UDS_EVT_SecAccessRequestSeed
Definition iso14229.h:308
@ UDS_EVT_SecAccessValidateKey
Definition iso14229.h:309
Security access request seed arguments.
Definition iso14229.h:1006
uint8_t(* copySeed)(UDSServer_t *srv, const void *src, uint16_t len)
Definition iso14229.h:1010
Security access validate key arguments.
Definition iso14229.h:1017
const uint8_t *const key
Definition iso14229.h:1019
UDS server structure.
Definition iso14229.h:850
UDSErr_t(* fn)(struct UDSServer *srv, UDSEvent_t event, void *arg)
Definition iso14229.h:852
UDSTp_t * tp
Definition iso14229.h:851
UDS Transport layer.
Definition iso14229.h:254