Commit d1a303e8 authored by Stefan Berger's avatar Stefan Berger Committed by Herbert Xu

x509: Detect sm2 keys by their parameters OID

Detect whether a key is an sm2 type of key by its OID in the parameters
array rather than assuming that everything under OID_id_ecPublicKey
is sm2, which is not the case.

Cc: David Howells <dhowells@redhat.com>
Cc: keyrings@vger.kernel.org
Signed-off-by: default avatarStefan Berger <stefanb@linux.ibm.com>
Reviewed-by: default avatarTianjia Zhang <tianjia.zhang@linux.alibaba.com>
Tested-by: default avatarTianjia Zhang <tianjia.zhang@linux.alibaba.com>
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent c12d448b
...@@ -459,6 +459,7 @@ int x509_extract_key_data(void *context, size_t hdrlen, ...@@ -459,6 +459,7 @@ int x509_extract_key_data(void *context, size_t hdrlen,
const void *value, size_t vlen) const void *value, size_t vlen)
{ {
struct x509_parse_context *ctx = context; struct x509_parse_context *ctx = context;
enum OID oid;
ctx->key_algo = ctx->last_oid; ctx->key_algo = ctx->last_oid;
switch (ctx->last_oid) { switch (ctx->last_oid) {
...@@ -470,11 +471,20 @@ int x509_extract_key_data(void *context, size_t hdrlen, ...@@ -470,11 +471,20 @@ int x509_extract_key_data(void *context, size_t hdrlen,
ctx->cert->pub->pkey_algo = "ecrdsa"; ctx->cert->pub->pkey_algo = "ecrdsa";
break; break;
case OID_id_ecPublicKey: case OID_id_ecPublicKey:
if (parse_OID(ctx->params, ctx->params_size, &oid) != 0)
return -EBADMSG;
switch (oid) {
case OID_sm2:
ctx->cert->pub->pkey_algo = "sm2"; ctx->cert->pub->pkey_algo = "sm2";
break; break;
default: default:
return -ENOPKG; return -ENOPKG;
} }
break;
default:
return -ENOPKG;
}
/* Discard the BIT STRING metadata */ /* Discard the BIT STRING metadata */
if (vlen < 1 || *(const u8 *)value != 0) if (vlen < 1 || *(const u8 *)value != 0)
......
...@@ -121,6 +121,7 @@ enum OID { ...@@ -121,6 +121,7 @@ enum OID {
}; };
extern enum OID look_up_OID(const void *data, size_t datasize); extern enum OID look_up_OID(const void *data, size_t datasize);
extern int parse_OID(const void *data, size_t datasize, enum OID *oid);
extern int sprint_oid(const void *, size_t, char *, size_t); extern int sprint_oid(const void *, size_t, char *, size_t);
extern int sprint_OID(enum OID, char *, size_t); extern int sprint_OID(enum OID, char *, size_t);
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/errno.h> #include <linux/errno.h>
#include <linux/bug.h> #include <linux/bug.h>
#include <linux/asn1.h>
#include "oid_registry_data.c" #include "oid_registry_data.c"
MODULE_DESCRIPTION("OID Registry"); MODULE_DESCRIPTION("OID Registry");
...@@ -92,6 +93,29 @@ enum OID look_up_OID(const void *data, size_t datasize) ...@@ -92,6 +93,29 @@ enum OID look_up_OID(const void *data, size_t datasize)
} }
EXPORT_SYMBOL_GPL(look_up_OID); EXPORT_SYMBOL_GPL(look_up_OID);
/**
* parse_OID - Parse an OID from a bytestream
* @data: Binary representation of the header + OID
* @datasize: Size of the binary representation
* @oid: Pointer to oid to return result
*
* Parse an OID from a bytestream that holds the OID in the format
* ASN1_OID | length | oid. The length indicator must equal to datasize - 2.
* -EBADMSG is returned if the bytestream is too short.
*/
int parse_OID(const void *data, size_t datasize, enum OID *oid)
{
const unsigned char *v = data;
/* we need 2 bytes of header and at least 1 byte for oid */
if (datasize < 3 || v[0] != ASN1_OID || v[1] != datasize - 2)
return -EBADMSG;
*oid = look_up_OID(data + 2, datasize - 2);
return 0;
}
EXPORT_SYMBOL_GPL(parse_OID);
/* /*
* sprint_OID - Print an Object Identifier into a buffer * sprint_OID - Print an Object Identifier into a buffer
* @data: The encoded OID to print * @data: The encoded OID to print
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment