curve25519: Support loading Ed25519 public keys from simple blobs

This commit is contained in:
Tobias Brunner
2018-10-26 11:01:10 +02:00
parent b982473a86
commit 20f74adbae
3 changed files with 61 additions and 39 deletions
+1
View File
@@ -73,6 +73,7 @@ ENUM(builder_part_names, BUILD_FROM_FILE, BUILD_END,
"BUILD_SAFE_PRIMES",
"BUILD_SHARES",
"BUILD_THRESHOLD",
"BUILD_EDDSA_PUB",
"BUILD_EDDSA_PRIV_ASN1_DER",
"BUILD_END",
);
+2
View File
@@ -156,6 +156,8 @@ enum builder_part_t {
BUILD_SHARES,
/** minimum number of participating private key shares */
BUILD_THRESHOLD,
/** EdDSA public key blob */
BUILD_EDDSA_PUB,
/** DER encoded ASN.1 EdDSA private key */
BUILD_EDDSA_PRIV_ASN1_DER,
/** end of variable argument builder list */
@@ -1,4 +1,5 @@
/*
* Copyright (C) 2018 Tobias Brunner
* Copyright (C) 2016 Andreas Steffen
* HSR Hochschule fuer Technik Rapperswil
*
@@ -200,50 +201,16 @@ static const asn1Object_t pubkeyObjects[] = {
#define ED25519_SUBJECT_PUBLIC_KEY 2
/**
* See header.
* Parse the ASN.1-encoded subjectPublicKeyInfo
*/
curve25519_public_key_t *curve25519_public_key_load(key_type_t type,
va_list args)
static bool parse_public_key_info(private_curve25519_public_key_t *this,
chunk_t blob)
{
private_curve25519_public_key_t *this;
chunk_t blob = chunk_empty, object;
asn1_parser_t *parser;
chunk_t object;
bool success = FALSE;
int objectID, oid;
while (TRUE)
{
switch (va_arg(args, builder_part_t))
{
case BUILD_BLOB_ASN1_DER:
blob = va_arg(args, chunk_t);
continue;
case BUILD_END:
break;
default:
return NULL;
}
break;
}
INIT(this,
.public = {
.key = {
.get_type = _get_type,
.verify = _verify,
.encrypt = _encrypt_,
.equals = public_key_equals,
.get_keysize = _get_keysize,
.get_fingerprint = _get_fingerprint,
.has_fingerprint = public_key_has_fingerprint,
.get_encoding = _get_encoding,
.get_ref = _get_ref,
.destroy = _destroy,
},
},
.ref = 1,
);
parser = asn1_parser_create(pubkeyObjects, blob);
while (parser->iterate(parser, &objectID, &object))
@@ -276,7 +243,59 @@ curve25519_public_key_t *curve25519_public_key_load(key_type_t type,
end:
parser->destroy(parser);
if (!success)
return success;
}
/**
* See header.
*/
curve25519_public_key_t *curve25519_public_key_load(key_type_t type,
va_list args)
{
private_curve25519_public_key_t *this;
chunk_t asn1 = chunk_empty, blob = chunk_empty;
while (TRUE)
{
switch (va_arg(args, builder_part_t))
{
case BUILD_BLOB_ASN1_DER:
asn1 = va_arg(args, chunk_t);
continue;
case BUILD_EDDSA_PUB:
blob = va_arg(args, chunk_t);
continue;
case BUILD_END:
break;
default:
return NULL;
}
break;
}
INIT(this,
.public = {
.key = {
.get_type = _get_type,
.verify = _verify,
.encrypt = _encrypt_,
.equals = public_key_equals,
.get_keysize = _get_keysize,
.get_fingerprint = _get_fingerprint,
.has_fingerprint = public_key_has_fingerprint,
.get_encoding = _get_encoding,
.get_ref = _get_ref,
.destroy = _destroy,
},
},
.ref = 1,
);
if (blob.len == ED25519_KEY_LEN)
{
this->pubkey = chunk_clone(blob);
}
else if (!asn1.len || !parse_public_key_info(this, asn1))
{
destroy(this);
return NULL;