Skip to content

Commit 48b683e

Browse files
maxd-nordicnordicjm
authored andcommitted
lib: nrf_cloud: fix non-nrf91 builds
The wifi compatibility didn't survive all of the change requests in the original PR. This is fixed here, a test build will also be added. Signed-off-by: Maximilian Deubel <[email protected]>
1 parent 1541856 commit 48b683e

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

subsys/net/lib/nrf_cloud/src/nrf_cloud_jwt.c

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,16 @@ static int get_key_from_cred(const int sec_tag, uint8_t *const der_out)
132132
return 0;
133133
}
134134

135-
static int custom_jwt_generate(struct jwt_data *const jwt)
135+
static int custom_jwt_generate(uint32_t exp_delta_s, char *const jwt_buf, size_t jwt_buf_sz,
136+
const char *subject, int sec_tag)
136137
{
137138
int err = 0;
138139
psa_key_id_t kid;
139140
psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
140141
uint8_t priv_key[PRV_KEY_SZ];
141142

142143
/* Load private key from storage */
143-
err = get_key_from_cred(jwt->sec_tag, priv_key);
144+
err = get_key_from_cred(sec_tag, priv_key);
144145
if (err) {
145146
LOG_ERR("Failed to get private key, error: %d", err);
146147
return err;
@@ -170,11 +171,10 @@ static int custom_jwt_generate(struct jwt_data *const jwt)
170171
.sec_tag = kid,
171172
.key_type = JWT_KEY_TYPE_CLIENT_PRIV,
172173
.alg = JWT_ALG_TYPE_ES256,
173-
.validity_s = jwt->exp_delta_s,
174-
.jwt_buf = jwt->jwt_buf,
175-
.jwt_sz = jwt->jwt_sz,
176-
.subject = jwt->subject,
177-
.audience = jwt->audience,
174+
.validity_s = exp_delta_s,
175+
.jwt_buf = jwt_buf,
176+
.jwt_sz = jwt_buf_sz,
177+
.subject = subject,
178178
};
179179

180180
return app_jwt_generate(&_jwt_internal);
@@ -189,16 +189,10 @@ int nrf_cloud_jwt_generate(uint32_t time_valid_s, char *const jwt_buf, size_t jw
189189

190190
int err;
191191
const char *id_ptr;
192-
struct jwt_data jwt = {
193-
.audience = NULL,
194-
.key = JWT_KEY_TYPE_CLIENT_PRIV,
195-
.alg = JWT_ALG_TYPE_ES256,
196-
.jwt_buf = jwt_buf,
197-
.jwt_sz = jwt_buf_sz
198-
};
199-
200-
jwt.sec_tag = IS_ENABLED(CONFIG_NRF_CLOUD_COAP) ?
192+
uint32_t exp_delta_s = time_valid_s;
193+
int sec_tag = IS_ENABLED(CONFIG_NRF_CLOUD_COAP) ?
201194
nrf_cloud_sec_tag_coap_jwt_get() : nrf_cloud_sec_tag_get();
195+
const char *subject;
202196

203197
#if defined(CONFIG_MODEM_JWT)
204198
/* Check if modem time is valid */
@@ -211,30 +205,38 @@ int nrf_cloud_jwt_generate(uint32_t time_valid_s, char *const jwt_buf, size_t jw
211205
}
212206
#endif
213207
if (time_valid_s > NRF_CLOUD_JWT_VALID_TIME_S_MAX) {
214-
jwt.exp_delta_s = NRF_CLOUD_JWT_VALID_TIME_S_MAX;
208+
exp_delta_s = NRF_CLOUD_JWT_VALID_TIME_S_MAX;
215209
} else if (time_valid_s == 0) {
216-
jwt.exp_delta_s = NRF_CLOUD_JWT_VALID_TIME_S_DEF;
217-
} else {
218-
jwt.exp_delta_s = time_valid_s;
210+
exp_delta_s = NRF_CLOUD_JWT_VALID_TIME_S_DEF;
219211
}
220212

221213
if (IS_ENABLED(CONFIG_NRF_CLOUD_CLIENT_ID_SRC_INTERNAL_UUID)) {
222214
/* The UUID is present in the iss claim, so there is no need
223215
* to also include it in the sub claim.
224216
*/
225-
jwt.subject = NULL;
217+
subject = NULL;
226218
} else {
227219
err = nrf_cloud_client_id_ptr_get(&id_ptr);
228220
if (err) {
229221
LOG_ERR("Failed to obtain client ID, error: %d", err);
230222
return err;
231223
}
232-
jwt.subject = id_ptr;
224+
subject = id_ptr;
233225
}
234226

235227
#if defined(CONFIG_NRF_CLOUD_JWT_SOURCE_CUSTOM)
236-
return custom_jwt_generate(&jwt);
228+
return custom_jwt_generate(exp_delta_s, jwt_buf, jwt_buf_sz, subject, sec_tag);
237229
#elif defined(CONFIG_MODEM_JWT)
230+
struct jwt_data jwt = {
231+
.audience = NULL,
232+
.key = JWT_KEY_TYPE_CLIENT_PRIV,
233+
.alg = JWT_ALG_TYPE_ES256,
234+
.jwt_buf = jwt_buf,
235+
.jwt_sz = jwt_buf_sz,
236+
.exp_delta_s = exp_delta_s,
237+
.sec_tag = sec_tag,
238+
.subject = subject,
239+
};
238240
err = modem_jwt_generate(&jwt);
239241
if (err) {
240242
LOG_ERR("Failed to generate JWT, error: %d", err);

0 commit comments

Comments
 (0)