Skip to content

Commit 5fa1cd7

Browse files
Refactor ec key import (#229)
Co-authored-by: Dengke Tang <[email protected]>
1 parent d4d51d1 commit 5fa1cd7

File tree

8 files changed

+558
-121
lines changed

8 files changed

+558
-121
lines changed

.github/workflows/codecov.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Code coverage check
2+
3+
on:
4+
push:
5+
6+
env:
7+
BUILDER_VERSION: v0.9.74
8+
BUILDER_SOURCE: releases
9+
BUILDER_HOST: https://d19elf31gohf1l.cloudfront.net
10+
PACKAGE_NAME: aws-c-cal
11+
RUN: ${{ github.run_id }}-${{ github.run_number }}
12+
CRT_CI_ROLE: ${{ secrets.CRT_CI_ROLE_ARN }}
13+
AWS_DEFAULT_REGION: us-east-1
14+
15+
permissions:
16+
id-token: write # This is required for requesting the JWT
17+
18+
jobs:
19+
codecov-linux:
20+
runs-on: ubuntu-24.04
21+
steps:
22+
- uses: aws-actions/configure-aws-credentials@v4
23+
with:
24+
role-to-assume: ${{ env.CRT_CI_ROLE }}
25+
aws-region: ${{ env.AWS_DEFAULT_REGION }}
26+
- name: Checkout Sources
27+
uses: actions/checkout@v4
28+
- name: Build ${{ env.PACKAGE_NAME }} + consumers
29+
run: |
30+
python3 -c "from urllib.request import urlretrieve; urlretrieve('${{ env.BUILDER_HOST }}/${{ env.BUILDER_SOURCE }}/${{ env.BUILDER_VERSION }}/builder.pyz?run=${{ env.RUN }}', 'builder')"
31+
chmod a+x builder
32+
./builder build -p ${{ env.PACKAGE_NAME }} --compiler=gcc --cmake-extra=-DASSERT_LOCK_HELD=ON --coverage

include/aws/cal/private/der.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ enum aws_der_type {
6060
/* forms */
6161
AWS_DER_FORM_CONSTRUCTED = 0x20,
6262
AWS_DER_FORM_PRIMITIVE = 0x00,
63+
64+
/* context specific */
65+
/* TODO: we should probably handle tags more generically, but for now first 2 tags cover all cases. */
66+
AWS_DER_CONTEXT_SPECIFIC_TAG0 = 0xa0,
67+
AWS_DER_CONTEXT_SPECIFIC_TAG1 = 0xa1,
6368
};
6469

6570
AWS_EXTERN_C_BEGIN
@@ -164,6 +169,14 @@ AWS_CAL_API int aws_der_encoder_get_contents(struct aws_der_encoder *encoder, st
164169
*/
165170
AWS_CAL_API struct aws_der_decoder *aws_der_decoder_new(struct aws_allocator *allocator, struct aws_byte_cursor input);
166171

172+
/**
173+
* Initializes new decoder from string at the current location.
174+
* Useful for cases where asn1 structure is nested inside another one, ex. ec pkcs8.
175+
* @param decoder Current decoder
176+
* @return Initialized decoder, or NULL
177+
*/
178+
AWS_CAL_API struct aws_der_decoder *aws_der_decoder_nested_tlv_decoder(struct aws_der_decoder *decoder);
179+
167180
/**
168181
* Cleans up a DER encoder
169182
* @param decoder The encoder to clean up
@@ -177,6 +190,12 @@ AWS_CAL_API void aws_der_decoder_destroy(struct aws_der_decoder *decoder);
177190
*/
178191
AWS_CAL_API bool aws_der_decoder_next(struct aws_der_decoder *decoder);
179192

193+
/**
194+
* Resets der decoder to the start.
195+
* @param decoder The decoder to reset
196+
*/
197+
AWS_CAL_API void aws_der_decoder_reset(struct aws_der_decoder *decoder);
198+
180199
/**
181200
* The type of the current TLV
182201
* @param decoder The decoder to inspect

include/aws/cal/private/ecc.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,16 @@ struct aws_der_decoder;
1313

1414
AWS_EXTERN_C_BEGIN
1515

16+
/*
17+
* Helper to load keypair from various ASN1 format.
18+
* Note: there are several formats in the wild: Sec1 and PKCS8 for private key and X509 for public key.
19+
* This function attempts to automatically recognize the format and load from it.
20+
* Depending on data available in the asn, either private or public key might be empty (zeroed out).
21+
*/
1622
AWS_CAL_API int aws_der_decoder_load_ecc_key_pair(
1723
struct aws_der_decoder *decoder,
18-
struct aws_byte_cursor *out_public_x_coor,
19-
struct aws_byte_cursor *out_public_y_coor,
24+
struct aws_byte_cursor *out_public_x_coord,
25+
struct aws_byte_cursor *out_public_y_coord,
2026
struct aws_byte_cursor *out_private_d,
2127
enum aws_ecc_curve_name *out_curve_name);
2228

source/der.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,15 @@ struct aws_der_decoder *aws_der_decoder_new(struct aws_allocator *allocator, str
400400
return NULL;
401401
}
402402

403+
struct aws_der_decoder *aws_der_decoder_nested_tlv_decoder(struct aws_der_decoder *decoder) {
404+
struct aws_byte_cursor cursor;
405+
AWS_ZERO_STRUCT(cursor);
406+
if (aws_der_decoder_tlv_string(decoder, &cursor)) {
407+
return NULL;
408+
}
409+
return aws_der_decoder_new(decoder->allocator, cursor);
410+
}
411+
403412
void aws_der_decoder_destroy(struct aws_der_decoder *decoder) {
404413
if (!decoder) {
405414
return;
@@ -467,6 +476,10 @@ bool aws_der_decoder_next(struct aws_der_decoder *decoder) {
467476
return (++decoder->tlv_idx < (int)decoder->tlvs.length);
468477
}
469478

479+
void aws_der_decoder_reset(struct aws_der_decoder *decoder) {
480+
decoder->tlv_idx = -1;
481+
}
482+
470483
static struct der_tlv s_decoder_tlv(struct aws_der_decoder *decoder) {
471484
AWS_FATAL_ASSERT(decoder->tlv_idx < (int)decoder->tlvs.length);
472485
struct der_tlv tlv = {0};

0 commit comments

Comments
 (0)