mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <sha@pengutronix.de>
To: Ahmad Fatoum <a.fatoum@pengutronix.de>
Cc: barebox@lists.infradead.org
Subject: Re: [PATCH 2/3] crypto: add JSON Web Token (JWT) support
Date: Wed, 1 Nov 2023 10:13:23 +0100	[thread overview]
Message-ID: <20231101091323.GH3359458@pengutronix.de> (raw)
In-Reply-To: <20231023143122.1760217-3-a.fatoum@pengutronix.de>

On Mon, Oct 23, 2023 at 04:31:22PM +0200, Ahmad Fatoum wrote:
> diff --git a/crypto/jwt.c b/crypto/jwt.c
> new file mode 100644
> index 000000000000..146ddeff1e8b
> --- /dev/null
> +++ b/crypto/jwt.c
> +struct jwt *jwt_decode(const char *token, const struct jwt_key *key)
> +{
> +	const char *alg_name = jwt_alg_names[key->alg];
> +	enum hash_algo hash_algo;
> +	const char *payload, *signature, *end;
> +	u8 *sigbin;
> +	size_t sig_len, sigbin_len;
> +	struct jwt *jwt;
> +	u8 *hash;
> +	int ret;
> +
> +	token = jwt_split(token, &payload, &signature, &end);
> +	if (IS_ERR(token))
> +		return ERR_CAST(token);
> +
> +	sig_len = end - signature;
> +
> +	switch (key->alg) {
> +	case JWT_ALG_RS256:
> +	case JWT_ALG_RS384:
> +	case JWT_ALG_RS512:
> +		if (sig_len == 0)
> +			return ERR_PTR(-EILSEQ);
> +
> +		sigbin = xzalloc(sig_len);
> +		sigbin_len = decode_base64url(sigbin, sig_len, signature);
> +
> +		hash_algo = digest_algo_by_jwt_alg(key->alg);
> +		hash = do_hash(token, signature - token - 1, hash_algo);
> +		if (IS_ERR(hash)) {
> +			free(sigbin);
> +			return ERR_CAST(hash);
> +		}
> +
> +		ret = rsa_verify(key->material.rsa_pub, sigbin, sigbin_len, hash,
> +				 hash_algo);
> +		free(hash);
> +		free(sigbin);
> +		if (ret < 0) {
> +			pr_debug("%s signature does not match: %pe\n",
> +				 alg_name, ERR_PTR(ret));
> +			return ERR_PTR(ret);
> +		}
> +
> +		break;
> +	default:
> +		return ERR_PTR(-ENOSYS);
> +	}
> +
> +	pr_debug("verification for algo %s ok\n", alg_name);
> +
> +	jwt = xzalloc(sizeof(*jwt));
> +
> +	ret = jwt_part_parse(&jwt->header, token, payload - token - 1);
> +	if (ret || !jwt_header_ok(jwt, key->alg)) {
> +		ret = ret ?: -EINVAL;
> +		pr_debug("failed to parse header: %pe\n", ERR_PTR(ret));
> +		goto err;
> +	}
> +
> +	ret = jwt_part_parse(&jwt->payload, payload, signature - payload - 1);
> +	if (ret) {
> +		ret = ret ?: -EINVAL;

Dropped this superfluous line while applying.

Sascha

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



  reply	other threads:[~2023-11-01  9:14 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-23 14:31 [PATCH 0/3] " Ahmad Fatoum
2023-10-23 14:31 ` [PATCH 1/3] lib: base64: add support for base64url Ahmad Fatoum
2023-10-23 14:31 ` [PATCH 2/3] crypto: add JSON Web Token (JWT) support Ahmad Fatoum
2023-11-01  9:13   ` Sascha Hauer [this message]
2023-10-23 14:31 ` [PATCH 3/3] test: self: add JSON Web Token tests Ahmad Fatoum
2023-11-02  7:20   ` Sascha Hauer
2023-11-02  8:07     ` Ahmad Fatoum
2023-11-01  9:10 ` [PATCH 0/3] crypto: add JSON Web Token (JWT) support Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231101091323.GH3359458@pengutronix.de \
    --to=sha@pengutronix.de \
    --cc=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox