estkme-group / lpac

C-based eUICC LPA
GNU Affero General Public License v3.0
253 stars 62 forks source link

[Help wanted] Windows asn1c GeneralizedTime encode error #22

Closed estkme closed 8 months ago

estkme commented 9 months ago

When debug an issue reported by a chat group user, the notification generated by euicc can not pass es10b_retrieve_notification function.

Linux version lpac works well, but in Windows enviroment(mingw), GeneralizedTime thorw an error.

Here is a code snipt to reproduct this issue.

#include "../euicc/asn1c/asn1/RetrieveNotificationsListResponse.h"
#include <euicc/hexutil.h>

static void bugged_GeneralizedTime_on_Windows()
{
    const char *hex = "BF2B8205E5A08205E1308205DDBF2F2580010F810204100C107273702E74727570686F6E652E636F6D5A0A985803012280000076F65F37401F762F6AA6078B8E048CF5D0E6C57344423A7C69D735F53C8ED1AC986CDEE042C1A73E8AC5D6076BFE0634BC1F05BB479818FE36FA98415045C536D7669A13BC3082026E30820215A0030201020212008904903200500888260003645626352101300A06082A8648CE3D040302308185310B3009060355040613024445310F300D06035504070C064D756E696368312F302D060355040A0C264769657365636B65204465767269656E74204D6F62696C6520536563757269747920476D624831183016060355040B0C0F4D6F62696C65205365637572697479311A301806035504030C1153696C766572205253502056322045554D3022180F32303136313130313132303030305A180F39393939313233313233353935395A307A310B30090603550406130244453121301F060355040A0C184769657365636B652026204465767269656E7420476D6248311D301B060355040B0C1450726F6A65637420426C61636B20466F72657374312930270603550405132038393034393033323030353030383838323630303033363435363236333532313059301306072A8648CE3D020106082A8648CE3D0301070342000494138838528F4B1A61F3C0D97B7CF40E8B139BBC56298EC3880530AE64AFF830C3F474E048812FB7A681905B339DE47A41463F9F0F0D3F35DE69DFBC77CE576DA36B3069300E0603551D0F0101FF04040302078030170603551D200101FF040D300B3009060767811201020101301D0603551D0E04160414A7EBA4B511105D405EE62984F40C7A88A49A7A1F301F0603551D230418301680146D89DBF9D27F45E827B4432218B4B3759354E965300A06082A8648CE3D040302034700304402203CB4F40D5CCB3D10CDBF16D28C70DDA1203DF6EC7F25151E0537B003F0FBD1BE0220264ED4E2C39127AC8600054941EE0D07765C08404009830B21CB9F66CF0F05B6308202FC308202A3A00302010202106CFA33CFA8B8B55BB8467778BB9CE320300A06082A8648CE3D040302304431183016060355040A130F47534D204173736F63696174696F6E312830260603550403131F47534D204173736F63696174696F6E202D205253503220526F6F7420434931301E170D3138303330363030303030305A170D3438303330353233353935395A308185310B3009060355040613024445310F300D060355040713064D756E696368312F302D060355040A13264769657365636B65204465767269656E74204D6F62696C6520536563757269747920476D624831183016060355040B130F4D6F62696C65205365637572697479311A30180603550403131153696C766572205253502056322045554D3059301306072A8648CE3D020106082A8648CE3D0301070342000478A37220D0C0B6464348516FFBECF0F1A06D4651259E4F0B479B3EC05FD9E38B480E501019CDACD9FB4FB5B6E0C3E1F6AC3BA146EAA556BBD96B19CAA8320BC1A38201333082012F301D0603551D0E041604146D89DBF9D27F45E827B4432218B4B3759354E96530120603551D130101FF040830060101FF02010030170603551D200101FF040D300B3009060767811201020102304D0603551D1F044630443042A040A03E863C687474703A2F2F67736D612D63726C2E73796D617574682E636F6D2F6F66666C696E6563612F67736D612D727370322D726F6F742D6369312E63726C300E0603551D0F0101FF040403020106304A0603551D1E0101FF0440303EA03C303AA43830363121301F060355040A0C184769657365636B652026204465767269656E7420476D62483111300F06035504051308383930343930333230150603551D11040E300C880A2B06010401DC0F680101301F0603551D2304183016801481370F5125D0B1D408D4C3B232E6D25E795BEBFB300A06082A8648CE3D040302034700304402205DD1C330F1312AC95A9395D0EC601A0D042D3E17F6C82A9449BCB0B91627F7D20220677F0AD4A621BADC2FE3D69E192B18C31CA74CE1E74FF63825ECFEAC8D49D562";
    asn_enc_rval_t asn1erval;
    asn_dec_rval_t asn1drval;
    RetrieveNotificationsListResponse_t *asn1resp = NULL;
    PendingNotification_t *asn1notification;
    NotificationMetadata_t *asn1metadata;
    char *payload = NULL, *receiver = NULL, *b64_payload = NULL;
    unsigned payload_length = 0;

    uint8_t *respbuf = malloc((strlen(hex) - 1));

    size_t resplen = euicc_hexutil_hex2bin(respbuf, (strlen(hex) - 1), hex);

    printf("resplen: %ld\n", resplen);

    asn1drval = ber_decode(NULL, &asn_DEF_RetrieveNotificationsListResponse, (void **)&asn1resp, respbuf, resplen);

    if (asn1drval.code != RC_OK)
    {
        printf("BAD!: code: %d, consumed: %ld\n", asn1drval.code, asn1drval.consumed);
        return;
    }

    printf("GOOD!\n");

    if (asn1resp->present != RetrieveNotificationsListResponse_PR_notificationList)
    {
        printf("0\n");
        return;
    }

    if (asn1resp->choice.notificationList.list.count < 1)
    {
        printf("1\n");
        return;
    }

    asn1notification = asn1resp->choice.notificationList.list.array[0];

    switch (asn1notification->present)
    {
    case PendingNotification_PR_profileInstallationResult:
        asn1metadata = &asn1notification->choice.profileInstallationResult.profileInstallationResultData.notificationMetadata;
        break;
    case PendingNotification_PR_otherSignedNotification:
        asn1metadata = &asn1notification->choice.otherSignedNotification.tbsOtherNotification;
        break;
    default:
        printf("2\n");
        return;
    }

    receiver = malloc(asn1metadata->notificationAddress.size + 1);
    memset(receiver, 0, asn1metadata->notificationAddress.size + 1);
    if (!(receiver))
    {
        printf("3\n");
        return;
    }
    memcpy(receiver, asn1metadata->notificationAddress.buf, asn1metadata->notificationAddress.size);

    asn1erval = der_encode(&asn_DEF_PendingNotification, asn1notification, NULL, NULL);
    if (asn1erval.encoded == -1)
    {
        printf("4\n");
        printf("%p\n", asn1erval.failed_type);
        printf("failed_type->name: %s\n", asn1erval.failed_type->name);
        return;
    }

    payload_length = asn1erval.encoded;
    payload = malloc(payload_length);
    if (!payload)
    {
        printf("5\n");
        return;
    }

    asn1erval = der_encode_to_buffer(&asn_DEF_PendingNotification, asn1notification, payload, payload_length);
    if (asn1erval.encoded == -1)
    {
        printf("6\n");
        return;
    }

    b64_payload = malloc(euicc_base64_encode_len(payload_length));
    if (!(b64_payload))
    {
        printf("7\n");
        return;
    }
    if (euicc_base64_encode(b64_payload, payload, payload_length) < 0)
    {
        printf("8\n");
        return;
    }
    printf("Done\n");
}
septs commented 9 months ago

ASN.1 c-language definition, generate using asn1c code generator

https://github.com/estkme-group/lpac/blob/3755b498cec7d57af490ddc2d6ecabebdc0b05bb/euicc/asn1c/asn1/Iccid.h#L1-L6

i think can see upstream issues: https://github.com/vlm/asn1c/issues?q=is%3Aissue+Windows+GeneralizedTime


I DON'T USE WINDOWS PLATFORM DEVELOPEMENT

creamlike1024 commented 9 months ago

May relate to https://github.com/vlm/asn1c/issues/196