cleanup: include/: move zephyr/jwt.h to data/jwt.h

move zephyr/jwt.h to data/jwt.h and
create a shim for backward-compatibility.

No functional changes to the headers.
A warning in the shim can be controlled with CONFIG_COMPAT_INCLUDES.

Related to #16539

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
This commit is contained in:
Anas Nashif 2019-06-25 15:51:04 -04:00
commit 4be2e9ebb6
5 changed files with 108 additions and 93 deletions

96
include/data/jwt.h Normal file
View file

@ -0,0 +1,96 @@
/*
* Copyright (c) 2018 Linaro Ltd
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_INCLUDE_DATA_JWT_H_
#define ZEPHYR_INCLUDE_DATA_JWT_H_
#include <zephyr/types.h>
#include <stdbool.h>
/**
* @brief JSON Web Token (JWT)
* @defgroup jwt JSON Web Token (JWT)
* @ingroup structured_data
* @{
*/
/**
* @brief JWT data tracking.
*
* JSON Web Tokens contain several sections, each encoded in base-64.
* This structure tracks the token as it is being built, including
* limits on the amount of available space. It should be initialized
* with jwt_init().
*/
struct jwt_builder {
/** The base of the buffer we are writing to. */
char *base;
/** The place in this buffer where we are currently writing.
*/
char *buf;
/** The length remaining to write. */
size_t len;
/**
* Flag that is set if we try to write past the end of the
* buffer. If set, the token is not valid.
*/
bool overflowed;
/* Pending bytes yet to be converted to base64. */
unsigned char wip[3];
/* Number of pending bytes. */
int pending;
};
/**
* @brief Initialize the JWT builder.
*
* Initialize the given JWT builder for the creation of a fresh token.
* The buffer size should at least be as long as JWT_BUILDER_MAX_SIZE
* returns.
*
* @param builder The builder to initialize.
* @param buffer The buffer to write the token to.
* @param buffer_size The size of this buffer. The token will be NULL
* terminated, which needs to be allowed for in this size.
*
* @retval 0 Success
* @retval -ENOSPC Buffer is insufficient to initialize
*/
int jwt_init_builder(struct jwt_builder *builder,
char *buffer,
size_t buffer_size);
/**
* @brief add JWT primary payload.
*/
int jwt_add_payload(struct jwt_builder *builder,
s32_t exp,
s32_t iat,
const char *aud);
/**
* @brief Sign the JWT token.
*/
int jwt_sign(struct jwt_builder *builder,
const char *der_key,
size_t der_key_len);
static inline size_t jwt_payload_len(struct jwt_builder *builder)
{
return (builder->buf - builder->base);
}
/**
* @}
*/
#endif /* ZEPHYR_INCLUDE_DATA_JWT_H_ */

View file

@ -1,96 +1,15 @@
/* /*
* Copyright (c) 2018 Linaro Ltd * Copyright (c) 2019 Intel Corporation
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
#ifndef ZEPHYR_INCLUDE_ZEPHYR_JWT_H_
#define ZEPHYR_INCLUDE_ZEPHYR_JWT_H_
#ifndef ZEPHYR_INCLUDE_JWT_H_ #ifndef CONFIG_COMPAT_INCLUDES
#define ZEPHYR_INCLUDE_JWT_H_ #warning "This header file has moved, include <data/jwt.h> instead."
#include <zephyr/types.h>
#include <stdbool.h>
/**
* @brief JSON Web Token (JWT)
* @defgroup jwt JSON Web Token (JWT)
* @ingroup structured_data
* @{
*/
/**
* @brief JWT data tracking.
*
* JSON Web Tokens contain several sections, each encoded in base-64.
* This structure tracks the token as it is being built, including
* limits on the amount of available space. It should be initialized
* with jwt_init().
*/
struct jwt_builder {
/** The base of the buffer we are writing to. */
char *base;
/** The place in this buffer where we are currently writing.
*/
char *buf;
/** The length remaining to write. */
size_t len;
/**
* Flag that is set if we try to write past the end of the
* buffer. If set, the token is not valid.
*/
bool overflowed;
/* Pending bytes yet to be converted to base64. */
unsigned char wip[3];
/* Number of pending bytes. */
int pending;
};
/**
* @brief Initialize the JWT builder.
*
* Initialize the given JWT builder for the creation of a fresh token.
* The buffer size should at least be as long as JWT_BUILDER_MAX_SIZE
* returns.
*
* @param builder The builder to initialize.
* @param buffer The buffer to write the token to.
* @param buffer_size The size of this buffer. The token will be NULL
* terminated, which needs to be allowed for in this size.
*
* @retval 0 Success
* @retval -ENOSPC Buffer is insufficient to initialize
*/
int jwt_init_builder(struct jwt_builder *builder,
char *buffer,
size_t buffer_size);
/**
* @brief add JWT primary payload.
*/
int jwt_add_payload(struct jwt_builder *builder,
s32_t exp,
s32_t iat,
const char *aud);
/**
* @brief Sign the JWT token.
*/
int jwt_sign(struct jwt_builder *builder,
const char *der_key,
size_t der_key_len);
static inline size_t jwt_payload_len(struct jwt_builder *builder)
{
return (builder->buf - builder->base);
}
/**
* @}
*/
#endif #endif
#include <data/jwt.h>
#endif /* ZEPHYR_INCLUDE_ZEPHYR_JWT_H_ */

View file

@ -12,7 +12,7 @@ LOG_MODULE_DECLARE(net_google_iot_mqtt, LOG_LEVEL_DBG);
#include <zephyr.h> #include <zephyr.h>
#include <string.h> #include <string.h>
#include <zephyr/jwt.h> #include <data/jwt.h>
#include <entropy.h> #include <entropy.h>
#include <net/tls_credentials.h> #include <net/tls_credentials.h>

View file

@ -8,7 +8,7 @@
#include <zephyr/types.h> #include <zephyr/types.h>
#include <errno.h> #include <errno.h>
#include <zephyr/jwt.h> #include <data/jwt.h>
#include <data/json.h> #include <data/json.h>
#ifdef CONFIG_JWT_SIGN_RSA #ifdef CONFIG_JWT_SIGN_RSA

View file

@ -21,7 +21,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <ztest.h> #include <ztest.h>
#include <data/json.h> #include <data/json.h>
#include <zephyr/jwt.h> #include <data/jwt.h>
#include <mbedtls/pk.h> #include <mbedtls/pk.h>
#include <mbedtls/rsa.h> #include <mbedtls/rsa.h>