Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added coseKid to the CoseResult. #11

Merged
merged 3 commits into from
Sep 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 21 additions & 54 deletions lib/src/cose.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,46 +29,23 @@ class Cose {
inst.decodeFromList(cose);
List<dynamic>? data = inst.getDecodedData();

if (null == data) {
return CoseResult(
payload: {},
verified: false,
errorCode: CoseErrorCode.cbor_decoding_error,
certificate: null,
publicKey: null);
}

if (data.isEmpty) {
return CoseResult(
payload: {},
verified: false,
errorCode: CoseErrorCode.cbor_decoding_error,
certificate: null,
publicKey: null);
//check if the data is not there
if ((null == data) || (data.isEmpty)) {
return CoseResult.withErrorCode(CoseErrorCode.cbor_decoding_error);
}

// take the first element
final element = data.first;

// check if it is of type List
if (!(element is List)) {
return CoseResult(
payload: {},
verified: false,
errorCode: CoseErrorCode.unsupported_format,
certificate: null,
publicKey: null);
return CoseResult.withErrorCode(CoseErrorCode.unsupported_format);
}

List items = element;
// check if it has exactly 4 items
if (items.length != _CBOR_DATA_LENGTH) {
return CoseResult(
payload: {},
verified: false,
errorCode: CoseErrorCode.invalid_format,
certificate: null,
publicKey: null);
return CoseResult.withErrorCode(CoseErrorCode.invalid_format);
}

// extract the useful information.
Expand All @@ -84,21 +61,12 @@ class Cose {
var header = <dynamic, dynamic>{};
if (headerList != null) {
if (!(headerList is List)) {
return CoseResult(
payload: {},
verified: false,
errorCode: CoseErrorCode.unsupported_header_format,
certificate: null,
publicKey: null);
return CoseResult.withErrorCode(
CoseErrorCode.unsupported_header_format);
}

if (headerList.isEmpty) {
return CoseResult(
payload: {},
verified: false,
errorCode: CoseErrorCode.cbor_decoding_error,
certificate: null,
publicKey: null);
return CoseResult.withErrorCode(CoseErrorCode.cbor_decoding_error);
}
header = headerList.first;
}
Expand All @@ -118,35 +86,28 @@ class Cose {
try {
var data = payloadCbor.getDecodedData();
if (null == data) {
return CoseResult(
payload: {},
verified: false,
errorCode: CoseErrorCode.payload_format_error,
certificate: null,
publicKey: null);
return CoseResult.withErrorCodeAndKid(
CoseErrorCode.payload_format_error, bKid);
}
payload = data.first;
} on Exception catch (e) {
CoseLogger.printError(e);
return CoseResult(
payload: {},
verified: false,
errorCode: CoseErrorCode.payload_format_error,
certificate: null,
publicKey: null);
return CoseResult.withErrorCodeAndKid(
CoseErrorCode.payload_format_error, bKid);
}
if (!certs.containsKey(bKid)) {
return CoseResult(
payload: payload,
verified: false,
errorCode: CoseErrorCode.key_not_found,
coseKid: bKid,
certificate: null,
publicKey: null);
}

// Get the public key to verify the signature.
// This can be either a x509 certificate (EU) or only the public key structure (UK)
// First we try to parse a x509, when that fails we try to treat is as a public key sturcture
// First we try to parse a x509, when that fails we try to treat is as a public key structure
PublicKey publicKey;
X509Certificate? x509cert;
try {
Expand All @@ -158,9 +119,11 @@ class Cose {
payload: payload,
verified: false,
errorCode: CoseErrorCode.kid_mismatch,
certificate: null,
coseKid: bKid,
certificate: x509cert,
publicKey: null);
}

publicKey = x509cert.publicKey;
} on Error {
final key = certs[bKid]!;
Expand Down Expand Up @@ -201,6 +164,7 @@ class Cose {
payload: payload,
verified: false,
errorCode: CoseErrorCode.unsupported_algorithm,
coseKid: bKid,
certificate: x509cert,
publicKey: publicKey);
}
Expand Down Expand Up @@ -241,6 +205,7 @@ class Cose {
payload: payload,
verified: false,
errorCode: CoseErrorCode.unsupported_algorithm,
coseKid: bKid,
certificate: x509cert,
publicKey: publicKey);
}
Expand All @@ -249,6 +214,7 @@ class Cose {
payload: payload,
verified: false,
errorCode: CoseErrorCode.unsupported_algorithm,
coseKid: bKid,
certificate: x509cert,
publicKey: publicKey);
}
Expand All @@ -262,6 +228,7 @@ class Cose {
payload: payload,
verified: verified,
errorCode: CoseErrorCode.none,
coseKid: bKid,
certificate: x509cert,
publicKey: publicKey);
}
Expand Down
23 changes: 23 additions & 0 deletions lib/src/model/cose_result.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,36 @@ class CoseResult {
final Map payload;
final bool verified;
final CoseErrorCode errorCode;
final String? coseKid; //the kid found inside the COSE header
final X509Certificate? certificate;
final PublicKey? publicKey;

CoseResult(
{required this.payload,
required this.verified,
required this.errorCode,
required this.coseKid,
required this.certificate,
required this.publicKey});

factory CoseResult.withErrorCode(CoseErrorCode errorCode) {
return new CoseResult(
payload: {},
verified: false,
errorCode: errorCode,
coseKid: null,
certificate: null,
publicKey: null);
}

factory CoseResult.withErrorCodeAndKid(
CoseErrorCode errorCode, String coseKid) {
return new CoseResult(
payload: {},
verified: false,
errorCode: errorCode,
coseKid: coseKid,
certificate: null,
publicKey: null);
}
}