From 4be2e9ebb6c761365c99c1e9c962852dc9aafb0e Mon Sep 17 00:00:00 2001 From: Anas Nashif Date: Tue, 25 Jun 2019 15:51:04 -0400 Subject: [PATCH] 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 --- include/data/jwt.h | 96 +++++++++++++++++++++ include/zephyr/jwt.h | 99 ++-------------------- samples/net/google_iot_mqtt/src/protocol.c | 2 +- subsys/jwt/jwt.c | 2 +- tests/subsys/jwt/src/main.c | 2 +- 5 files changed, 108 insertions(+), 93 deletions(-) create mode 100644 include/data/jwt.h diff --git a/include/data/jwt.h b/include/data/jwt.h new file mode 100644 index 00000000000..bb8901eaf4d --- /dev/null +++ b/include/data/jwt.h @@ -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 +#include + +/** + * @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_ */ diff --git a/include/zephyr/jwt.h b/include/zephyr/jwt.h index db7712569e6..cbc87ef69ea 100644 --- a/include/zephyr/jwt.h +++ b/include/zephyr/jwt.h @@ -1,96 +1,15 @@ /* - * Copyright (c) 2018 Linaro Ltd + * Copyright (c) 2019 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ +#ifndef ZEPHYR_INCLUDE_ZEPHYR_JWT_H_ +#define ZEPHYR_INCLUDE_ZEPHYR_JWT_H_ -#ifndef ZEPHYR_INCLUDE_JWT_H_ -#define ZEPHYR_INCLUDE_JWT_H_ - -#include -#include - -/** - * @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); -} - -/** - * @} - */ - +#ifndef CONFIG_COMPAT_INCLUDES +#warning "This header file has moved, include instead." #endif + +#include + +#endif /* ZEPHYR_INCLUDE_ZEPHYR_JWT_H_ */ diff --git a/samples/net/google_iot_mqtt/src/protocol.c b/samples/net/google_iot_mqtt/src/protocol.c index c23bd53daeb..bbfb058a1ef 100644 --- a/samples/net/google_iot_mqtt/src/protocol.c +++ b/samples/net/google_iot_mqtt/src/protocol.c @@ -12,7 +12,7 @@ LOG_MODULE_DECLARE(net_google_iot_mqtt, LOG_LEVEL_DBG); #include #include -#include +#include #include #include diff --git a/subsys/jwt/jwt.c b/subsys/jwt/jwt.c index 89f1ef23082..fed7b53851e 100644 --- a/subsys/jwt/jwt.c +++ b/subsys/jwt/jwt.c @@ -8,7 +8,7 @@ #include #include -#include +#include #include #ifdef CONFIG_JWT_SIGN_RSA diff --git a/tests/subsys/jwt/src/main.c b/tests/subsys/jwt/src/main.c index 7b478df2c13..0a7d97fe4a6 100644 --- a/tests/subsys/jwt/src/main.c +++ b/tests/subsys/jwt/src/main.c @@ -21,7 +21,7 @@ #include #include #include -#include +#include #include #include