std::vector<uint8_t> Buf2() { unsigned int arcs1[] = {1, 3, 6, 1, 4, 1, 9363, 78458}; unsigned int arcs2[] = {1, 3, 6, 1, 4, 2, 9363}; unsigned int arcs3[] = {1, 3, 6, 1, 4, 3, 9363, 78458, 1}; unsigned int arcs4[] = {1, 3, 6, 1, 4, 4, 9363}; std::vector<uint8_t> buf(DEFAULT_BUF_SIZE); SequenceRecord_IS_OI_OIO_OIRef_OIRefO_t rec; memset(&rec, 0, sizeof(rec)); OBJECT_IDENTIFIER_set_arcs(&rec.seq.objectIdentifier1, arcs1, sizeof(arcs1[0]), sizeof(arcs1) / sizeof(arcs1[0])); rec.seq.objectIdentifier2 = (OBJECT_IDENTIFIER_t*)malloc(sizeof(OBJECT_IDENTIFIER_t)); OBJECT_IDENTIFIER_set_arcs(rec.seq.objectIdentifier2, arcs2, sizeof(arcs2[0]), sizeof(arcs2) / sizeof(arcs2[0])); OBJECT_IDENTIFIER_set_arcs(&rec.seq.objectIdentifier3, arcs3, sizeof(arcs3[0]), sizeof(arcs3) / sizeof(arcs3[0])); rec.seq.objectIdentifier4 = (ObjectIdentifierRef_t*)malloc(sizeof(ObjectIdentifierRef_t)); OBJECT_IDENTIFIER_set_arcs(rec.seq.objectIdentifier4, arcs4, sizeof(arcs4[0]), sizeof(arcs4) / sizeof(arcs4[0])); printf("std::vector<uint8_t> mustBe1 = "); PrintObjectIdent(rec.seq.objectIdentifier1); printf("std::vector<uint8_t> mustBe2 = "); PrintObjectIdent(*rec.seq.objectIdentifier2); printf("std::vector<uint8_t> mustBe3 = "); PrintObjectIdent(rec.seq.objectIdentifier3); printf("std::vector<uint8_t> mustBe4 = "); PrintObjectIdent(*rec.seq.objectIdentifier4); asn_enc_rval_t rval; rval = der_encode_to_buffer(&asn_DEF_SequenceRecord_IS_OI_OIO_OIRef_OIRefO, &rec, (void*)buf.data(), buf.size()); if (rval.encoded >= 0) { buf.resize(rval.encoded); } else { printf("cannot encode record in Buf2"); exit(0); } return std::move(buf); }
/*liczony jest strot z przekazywanych danych funkcja SHA-1, jesli countHashFlag=CCPD_COUNT_HASH */ long _add_to_Data__CCPD( GenBuf_t *data_for_hash, Data_t *data, long countHashFlag) { bmd_crypt_ctx_t *hash_context = NULL; GenBuf_t *hash = NULL; char oid_sha1_tab[] = { OID_HASH_FUNCTION_SHA1 }; long ret_val = 0; if(data_for_hash == NULL) { BMD_FOK(-1); } if(data_for_hash->buf == NULL) { BMD_FOK(-2); } if(data == NULL) { BMD_FOK(-3); } //w przypadku skrotu na wejsciu, dopuszczalna jest tylko funkcja SHA-1, a wiec 160 bitow (20 bajtow) if(countHashFlag == BMDDVCS_HASH_AT_INPUT && data_for_hash->size != 20) { { BMD_FOK(-6); } } data->present=Data_PR_messageImprint; //oid SHA-1 ret_val=OBJECT_IDENTIFIER_set_arcs(&(data->choice.messageImprint.digestAlgorithm.algorithm), oid_sha1_tab, sizeof(oid_sha1_tab[0]), sizeof(oid_sha1_tab)/sizeof(oid_sha1_tab[0])); if(ret_val != 0) { BMD_FOK(-4); } // serdecznie pierdole data->choice.messageImprint.digestAlgorithm.parameters=(ANY_t*)calloc(1, sizeof(ANY_t)); data->choice.messageImprint.digestAlgorithm.parameters->size=2; data->choice.messageImprint.digestAlgorithm.parameters->buf=(uint8_t*)calloc(2, sizeof(uint8_t)); data->choice.messageImprint.digestAlgorithm.parameters->buf[1]=0x00; data->choice.messageImprint.digestAlgorithm.parameters->buf[0]=0x05; if(countHashFlag == BMDDVCS_COUNT_HASH) { //liczenie skrotu z danych za pomoca SHA-1 bmd_set_ctx_hash(&hash_context,BMD_HASH_ALGO_SHA1); bmd_hash_data(data_for_hash, &hash_context, &hash, NULL); bmd_ctx_destroy(&hash_context); } else { hash=data_for_hash; } ret_val=OCTET_STRING_fromBuf(&(data->choice.messageImprint.digest), (char *)hash->buf, hash->size); if(countHashFlag == BMDDVCS_COUNT_HASH) { free_gen_buf(&hash); } else { hash = NULL; } if(ret_val != 0) { BMD_FOK(-5); } return 0; }
// Funkcja umieszcza w ContentInfo struktur�SignedData. Jeeli // w ContentInfo istniay ju jakie dane, zostan zwolnione, // a na ich miejsce umieszczona podana strukturka // Wykonywana jest kopia struktury SignedData long ContentInfo_set_SignedData(ContentInfo_t* content, SignedData_t* sigData) { long OID_id_signedData[] = {OID_CMS_ID_SIGNED_DATA_LONG}; long ret; if ((content == NULL) || (sigData == NULL)) return ERR_WRONG_ARGUMENT; // wyzeruj strukture ContentInfo asn_DEF_ContentInfo.free_struct( &asn_DEF_ContentInfo, content, 1); memset(content, 0, sizeof(ContentInfo_t)); // ustaw identyfikator OBJECT_IDENTIFIER_set_arcs( &(content->contentType), OID_id_signedData, sizeof(*OID_id_signedData), sizeof(OID_id_signedData)/sizeof(*OID_id_signedData)); // umiesc dane ret = ANY_fromType(&content->content, &asn_DEF_SignedData, sigData); if ( ret ) return ret; return 0; }
int main() { /* Initialize values which can't use static initializers. */ asn_long2INTEGER(&otp_format, 2); /* Alphanumeric */ asn_long2INTEGER(&kvno, 5); OBJECT_IDENTIFIER_set_arcs(&alg_sha256.algorithm, sha256_arcs, sizeof(*sha256_arcs), sizeof(sha256_arcs) / sizeof(*sha256_arcs)); OBJECT_IDENTIFIER_set_arcs(&alg_sha1.algorithm, sha1_arcs, sizeof(*sha1_arcs), sizeof(sha1_arcs) / sizeof(*sha1_arcs)); printf("Minimal OTP-TOKEN-INFO:\n"); der_encode(&asn_DEF_OTP_TOKENINFO, &token_info_1, consume, NULL); printbuf(); printf("\nMaximal OTP-TOKEN-INFO:\n"); der_encode(&asn_DEF_OTP_TOKENINFO, &token_info_2, consume, NULL); printbuf(); printf("\nMinimal PA-OTP-CHALLENGE:\n"); der_encode(&asn_DEF_PA_OTP_CHALLENGE, &challenge_1, consume, NULL); printbuf(); printf("\nMaximal PA-OTP-CHALLENGE:\n"); der_encode(&asn_DEF_PA_OTP_CHALLENGE, &challenge_2, consume, NULL); printbuf(); printf("\nMinimal PA-OTP-REQUEST:\n"); der_encode(&asn_DEF_PA_OTP_REQUEST, &request_1, consume, NULL); printbuf(); printf("\nMaximal PA-OTP-REQUEST:\n"); der_encode(&asn_DEF_PA_OTP_REQUEST, &request_2, consume, NULL); printbuf(); printf("\nPA-OTP-ENC-REQUEST:\n"); der_encode(&asn_DEF_PA_OTP_ENC_REQUEST, &enc_request, consume, NULL); printbuf(); printf("\n"); return 0; }
/* funkcja na podstawie struktury DVCSRequest tworzy strukture EncapsulatedContentInfo */ long _Create_encapContentInfo_DVCSReq(DVCSRequest_t *req, EncapsulatedContentInfo_t** encap) { OBJECT_IDENTIFIER_t *oid_dvcsReq=NULL; EncapsulatedContentInfo_t *nowy_encap=NULL; OCTET_STRING_t *econtent=NULL; /** @bug serwer DVCS nie rozpoznaje OID DVCSRequest w EncapsulatedData, jedynie data (ogólny typ) */ /*long oid_tab[]= { DVCS_REQUEST_OID };*/ long oid_tab[]= { 1, 2, 840, 113549, 1, 7, 1 }; /*CMS data*/ long ret_val=0; if(req == NULL) { return -1; } if(encap == NULL) { return -2; } if(*encap != NULL) { return -3; } if( (_encode_DVCSRequest_to_eContent(req, &econtent)) != 0 ) { return -4; } oid_dvcsReq=(OBJECT_IDENTIFIER_t*)calloc(1, sizeof(OBJECT_IDENTIFIER_t)); if(oid_dvcsReq == NULL) { free(econtent->buf); free(econtent); return -5; } ret_val=OBJECT_IDENTIFIER_set_arcs(oid_dvcsReq, oid_tab, sizeof(oid_tab[0]), sizeof(oid_tab)/sizeof(long)); if(ret_val != 0) { free(econtent->buf); free(econtent); free(oid_dvcsReq); return -6; } nowy_encap=(EncapsulatedContentInfo_t*)calloc(1, sizeof(EncapsulatedContentInfo_t)); if(nowy_encap == NULL) { free(econtent->buf); free(econtent); asn_DEF_OBJECT_IDENTIFIER.free_struct(&asn_DEF_OBJECT_IDENTIFIER, oid_dvcsReq, 1); free(oid_dvcsReq); return -7; } nowy_encap->eContent=econtent; nowy_encap->eContentType=*oid_dvcsReq; free(oid_dvcsReq); //opakowanie zwolnione *encap=nowy_encap; return 0; }
std::vector<uint8_t> Buf1() { unsigned long long arcs1[] = {1, 3, 6, 1, 4, 1, 9363, 78458}; unsigned int arcs3[] = {1, 3, 6, 1, 4, 3, 9363, 78458, 1}; std::vector<uint8_t> buf(DEFAULT_BUF_SIZE); SequenceRecord_IS_OI_OIO_OIRef_OIRefO_t rec; memset(&rec, 0, sizeof(rec)); OBJECT_IDENTIFIER_set_arcs(&rec.seq.objectIdentifier1, arcs1, sizeof(arcs1[0]), sizeof(arcs1) / sizeof(arcs1[0])); rec.seq.objectIdentifier2 = 0; OBJECT_IDENTIFIER_set_arcs(&rec.seq.objectIdentifier3, arcs3, sizeof(arcs3[0]), sizeof(arcs3) / sizeof(arcs3[0])); rec.seq.objectIdentifier4 = 0; asn_enc_rval_t rval; rval = der_encode_to_buffer(&asn_DEF_SequenceRecord_IS_OI_OIO_OIRef_OIRefO, &rec, (void*)buf.data(), buf.size()); if (rval.encoded >= 0) { buf.resize(rval.encoded); } else { printf("cannot encode record in Buf1"); exit(0); } return std::move(buf); }
// ustawienie typu zawratosci danych struktury SignedData long SignedData_set_ContentType( SignedData_t *sigData, long *oid_tab, long oid_size) { long err = 0; if ((sigData == NULL) || (oid_tab == NULL)) return ERR_WRONG_ARGUMENT; err = OBJECT_IDENTIFIER_set_arcs( &(sigData->encapContentInfo.eContentType), oid_tab, sizeof(oid_tab[0]), oid_size); if ( err != 0 ) return err; return err; }
// Umieszczenie danych zaszyfrowanych i informacji o algorytmie // szyfrujacym w strukturze // WYKONYWANE SA KOPIE iv oraz data long EnvelopedData_set_EncryptedData(EnvelopedData_t *envData,GenBuf_t *iv,GenBuf_t *data,bmd_encryption_params_t *options) { long OID_id_data[] = {OID_CMS_ID_DATA_LONG}; /*long OID_id_DES_CBC[] = {1,3,14,3,2,7};*/ long OID_id_DES_EDE3_CBC[] = {1,2,840,113549,3,7}; long *oid; long oidsize; long err = 0; OCTET_STRING_t *oiv = NULL; // set data OID err = OBJECT_IDENTIFIER_set_arcs(&(envData->encryptedContentInfo.contentType),OID_id_data, sizeof(OID_id_data[0]), sizeof(OID_id_data)/sizeof(OID_id_data[0])); if ( err ) return err; // set Algorithm OID if ( options->encryption_algo == BMD_CRYPT_ALGO_DES3 ) { oid = OID_id_DES_EDE3_CBC; oidsize = sizeof(OID_id_DES_EDE3_CBC)/sizeof(OID_id_DES_EDE3_CBC[0]); } else return BMD_ERR_UNIMPLEMENTED; err = AlgorithmIdentifier_set_OID(&(envData->encryptedContentInfo.contentEncryptionAlgorithm),oid, oidsize); if ( err ) return err; // set Algorithm IV oiv = mallocStructure(sizeof(OCTET_STRING_t)); OCTET_STRING_fromBuf(oiv,(char *)iv->buf, iv->size); envData->encryptedContentInfo.contentEncryptionAlgorithm.parameters = mallocStructure(sizeof(ANY_t)); err = ANY_fromType(envData->encryptedContentInfo.contentEncryptionAlgorithm.parameters, &asn_DEF_OCTET_STRING, oiv); if ( err ) return ERR_OPERATION_FAILED; asn_DEF_OCTET_STRING.free_struct(&asn_DEF_OCTET_STRING,oiv, 0); if( options->encryption_type==BMD_CMS_ENV_INTERNAL ) { // set Encrypted Data envData->encryptedContentInfo.encryptedContent = mallocStructure(sizeof(EncryptedContent_t)); OCTET_STRING_fromBuf(envData->encryptedContentInfo.encryptedContent,(char *)data->buf, data->size); } return err; }
// Funkcja umieszcza w ContentInfo struktur�EnvelopedData. Jeeli // w ContentInfo istniay ju jakie dane, zostan zwolnione, // a na ich miejsce umieszczona podana strukturka // Wykonywana jest kopia struktury EnvelopedData long ContentInfo_set_EnvelopedData(ContentInfo_t* content, EnvelopedData_t* envData) { long OID_id_envelopedData[] = {OID_CMS_ID_ENVELOPED_DATA_LONG}; long err = 0; if ((content == NULL) || (envData == NULL)) return ERR_WRONG_ARGUMENT; // wyzeruj strukture ContentInfo asn_DEF_ContentInfo.free_struct( &asn_DEF_ContentInfo, content, 1); memset(content, 0, sizeof(ContentInfo_t)); // ustaw identyfikator OBJECT_IDENTIFIER_set_arcs( &(content->contentType), OID_id_envelopedData, sizeof(*OID_id_envelopedData), sizeof(OID_id_envelopedData)/sizeof(*OID_id_envelopedData)); // umiesc dane err = ANY_fromType(&content->content, &asn_DEF_EnvelopedData, envData); if ( err ) return err; return err; }
int main() { /* Initialize values which can't use static initializers. */ asn_long2INTEGER(&otp_format, 2); /* Alphanumeric */ OBJECT_IDENTIFIER_set_arcs(&alg_sha256.algorithm, sha256_arcs, sizeof(*sha256_arcs), sizeof(sha256_arcs) / sizeof(*sha256_arcs)); OBJECT_IDENTIFIER_set_arcs(&alg_sha1.algorithm, sha1_arcs, sizeof(*sha1_arcs), sizeof(sha1_arcs) / sizeof(*sha1_arcs)); OBJECT_IDENTIFIER_set_arcs(&other_info.algorithmID.algorithm, krb5_arcs, sizeof(*krb5_arcs), sizeof(krb5_arcs) / sizeof(*krb5_arcs)); printf("PrincipalName:\n"); der_encode(&asn_DEF_PrincipalName, &princ, consume, NULL); printbuf(); /* Print this encoding and also use it to initialize two fields of * other_info. */ printf("\nKRB5PrincipalName:\n"); der_encode(&asn_DEF_KRB5PrincipalName, &krb5princ, consume, NULL); OCTET_STRING_fromBuf(&other_info.partyUInfo, buf, buf_pos); OCTET_STRING_fromBuf(&other_info.partyVInfo, buf, buf_pos); printbuf(); printf("\nOtherInfo:\n"); der_encode(&asn_DEF_OtherInfo, &other_info, consume, NULL); printbuf(); free(other_info.partyUInfo.buf); free(other_info.partyVInfo.buf); printf("\nPkinitSuppPubInfo:\n"); der_encode(&asn_DEF_PkinitSuppPubInfo, &supp_pub_info, consume, NULL); printbuf(); printf("\nMinimal OTP-TOKEN-INFO:\n"); der_encode(&asn_DEF_OTP_TOKENINFO, &token_info_1, consume, NULL); printbuf(); printf("\nMaximal OTP-TOKEN-INFO:\n"); der_encode(&asn_DEF_OTP_TOKENINFO, &token_info_2, consume, NULL); printbuf(); printf("\nMinimal PA-OTP-CHALLENGE:\n"); der_encode(&asn_DEF_PA_OTP_CHALLENGE, &challenge_1, consume, NULL); printbuf(); printf("\nMaximal PA-OTP-CHALLENGE:\n"); der_encode(&asn_DEF_PA_OTP_CHALLENGE, &challenge_2, consume, NULL); printbuf(); printf("\nMinimal PA-OTP-REQUEST:\n"); der_encode(&asn_DEF_PA_OTP_REQUEST, &request_1, consume, NULL); printbuf(); printf("\nMaximal PA-OTP-REQUEST:\n"); der_encode(&asn_DEF_PA_OTP_REQUEST, &request_2, consume, NULL); printbuf(); printf("\nPA-OTP-ENC-REQUEST:\n"); der_encode(&asn_DEF_PA_OTP_ENC_REQUEST, &enc_request, consume, NULL); printbuf(); printf("\n"); return 0; }
int main() { /* Define an OBJECT IDENTIFIER value */ int oid[] = { 1, 3, 6, 1, 4, 1, 9363, 1, 5, 0 }; /* or whatever */ /* Declare a pointer to a new instance of MyTypes type */ MyTypes_t *myType; /* Declare a pointer to a MyInt type */ MyInt_t *myInt; /* Temporary return value */ int ret; /* Allocate an instance of MyTypes */ myType = calloc(1, sizeof *myType); assert(myType); /* Assume infinite memory */ /* * Fill in myObjectId */ ret = OBJECT_IDENTIFIER_set_arcs(&myType->myObjectId, oid, sizeof(oid[0]), sizeof(oid) / sizeof(oid[0])); assert(ret == 0); /* * Fill in mySeqOf with a couple of integers. */ /* Prepare a certain INTEGER */ myInt = calloc(1, sizeof *myInt); assert(myInt); *myInt = 123; /* Set integer value */ /* Fill in mySeqOf with the prepared INTEGER */ ret = ASN_SEQUENCE_ADD(&myType->mySeqOf, myInt); assert(ret == 0); /* Prepare another integer */ myInt = calloc(1, sizeof *myInt); assert(myInt); *myInt = 111222333; /* Set integer value */ /* Append another INTEGER into mySeqOf */ ret = ASN_SEQUENCE_ADD(&myType->mySeqOf, myInt); assert(ret == 0); /* * Fill in myBitString */ /* Allocate some space for bitmask */ myType->myBitString.buf = calloc(1, 1); assert(myType->myBitString.buf); myType->myBitString.size = 1; /* 1 byte */ /* Set the value of muxToken */ myType->myBitString.buf[0] |= 1 << (7 - myBitString_muxToken); /* Also set the value of modemToken */ myType->myBitString.buf[0] |= 1 << (7 - myBitString_modemToken); /* Trim unused bits (optional) */ myType->myBitString.bits_unused = 6; /* * Print the resulting structure as XER (XML) */ xer_fprint(stdout, &asn_DEF_MyTypes, myType); return 0; }
long _add_to_Data_VSD_External(GenBuf_t *signedData, GenBuf_t *dataContent, long dataContentType, char* hashAlgorithmOid, Data_t *data) { long retVal=0; DigestInfo_t *digestInfo=NULL; long sha1_oid[] = { OID_SHA_1_LONG}; GenBuf_t *hash = NULL; bmd_crypt_ctx_t *hash_ctx=NULL; if(signedData == NULL) { return -1; } if(signedData->buf == NULL) { return -2; } if(dataContent == NULL) { return -3; } if(dataContent->buf == NULL) { return -4; } if(data == NULL) { return -5; } // gdy przekazany podpisany dokument, parametr hashAlgorithmOid jest ignorowany if(dataContentType == BMDDVCS_EXTERNAL_DOCUMENT_HASH && hashAlgorithmOid == NULL) { return -6; } data->present=Data_PR_messageInfo; //ustawienie podpisu zewnetrznego retVal=OCTET_STRING_fromBuf( &(data->choice.messageInfo.message), signedData->buf, signedData->size); if(retVal != 0) { return -10; } //ustawienie skrotu z podpisanych danych digestInfo=(DigestInfo_t*)calloc(1, sizeof(DigestInfo_t)); if(digestInfo == NULL) { asn_DEF_MessageInfo.free_struct(&asn_DEF_MessageInfo, &(data->choice.messageInfo), 1); return -11; } //ustawienie oidu funkcji skrotu (jesli przekazany byl skrot, a nie caly dokument) if(dataContentType == BMDDVCS_EXTERNAL_DOCUMENT_HASH) { retVal=String2OID( hashAlgorithmOid, &(digestInfo->digestAlgorithm.algorithm)); if(retVal < 0) { asn_DEF_MessageInfo.free_struct(&asn_DEF_MessageInfo, &(data->choice.messageInfo), 1); free(digestInfo); return -20; } } else { retVal=OBJECT_IDENTIFIER_set_arcs( &(digestInfo->digestAlgorithm.algorithm), sha1_oid, sizeof(long), sizeof(sha1_oid)/sizeof(long)); if(retVal != 0) { asn_DEF_MessageInfo.free_struct(&asn_DEF_MessageInfo, &(data->choice.messageInfo), 1); free(digestInfo); return -12; } } //ustawienie wartosci skrotu if(dataContentType == BMDDVCS_EXTERNAL_DOCUMENT_HASH) //gotowy skrot { retVal=OCTET_STRING_fromBuf( &(digestInfo->digest), dataContent->buf, dataContent->size); if(retVal != 0) { asn_DEF_MessageInfo.free_struct(&asn_DEF_MessageInfo, &(data->choice.messageInfo), 1); asn_DEF_DigestInfo.free_struct(&asn_DEF_DigestInfo, digestInfo, 1); free(digestInfo); return -13; } } else //wyliczanie skrotu { retVal=bmd_set_ctx_hash(&hash_ctx, BMD_HASH_ALGO_SHA1); if(retVal != BMD_OK) { asn_DEF_MessageInfo.free_struct(&asn_DEF_MessageInfo, &(data->choice.messageInfo), 1); asn_DEF_DigestInfo.free_struct(&asn_DEF_DigestInfo, digestInfo, 1); free(digestInfo); return -14; } retVal=bmd_hash_data(dataContent, &hash_ctx, &hash, NULL); bmd_ctx_destroy(&hash_ctx); if(retVal != BMD_OK) { asn_DEF_MessageInfo.free_struct(&asn_DEF_MessageInfo, &(data->choice.messageInfo), 1); asn_DEF_DigestInfo.free_struct(&asn_DEF_DigestInfo, digestInfo, 1); free(digestInfo); return -15; } retVal=OCTET_STRING_fromBuf( &(digestInfo->digest), hash->buf, hash->size); free_gen_buf(&hash); if(retVal != 0) { asn_DEF_MessageInfo.free_struct(&asn_DEF_MessageInfo, &(data->choice.messageInfo), 1); asn_DEF_DigestInfo.free_struct(&asn_DEF_DigestInfo, digestInfo, 1); free(digestInfo); return -16; } } //wstawienie digestInfo do zbioru externalData if( (asn_set_add((void*)&(data->choice.messageInfo.externalData.list), (void*)digestInfo)) != 0 ) { asn_DEF_MessageInfo.free_struct(&asn_DEF_MessageInfo, &(data->choice.messageInfo), 1); asn_DEF_DigestInfo.free_struct(&asn_DEF_DigestInfo, digestInfo, 1); free(digestInfo); return -17; } return 0; }
u8 Encode_DER_Packet(MyPacket * Pkt, u8 * buf) { u8 oid[] = { 1, 3, 6, 1, 5, 9, 0, 0, 0, 0 }; /* * MyPacket_t and MyInt_t declared in MyPacket.h and MyInt.h */ MyPacket_t *myPacket; MyInt_t *myInt; asn_enc_rval_t er; /* Encoder return value */ u8 ret, i; myPacket = calloc(1, sizeof *myPacket); /* * Fill in myObjectId so that the other end knows which * protocol is this */ ret = OBJECT_IDENTIFIER_set_arcs(&myPacket->myObjectId, oid, sizeof(oid[0]), sizeof(oid) / sizeof(oid[0])); if (ret != 0) goto fail; /* * Fill in myInts */ for (i = 0; i < INTARRAYSIZE; i++) { myInt = calloc(1, sizeof *myInt); *myInt = Pkt->myInts[i]; ret = ASN_SEQUENCE_ADD(&myPacket->myInts, myInt); if (ret != 0) goto fail; } /* * Fill in myName */ myPacket->myName.buf = calloc(1, strlen(Pkt->myName) + 1); strcpy(myPacket->myName.buf, Pkt->myName); myPacket->myName.size = strlen(Pkt->myName); /* * Fill in myFlags */ myPacket->myFlags.buf = calloc(1, 1); myPacket->myFlags.size = 1 /* 1 byte */ ; for (i = 0; i < 8; i++) if (Pkt->myFlags & (1 << i)) myPacket->myFlags.buf[0] |= (1 << (7 - i)); /* * Encode to DER and place result in buf */ er = der_encode_to_buffer(&asn_DEF_MyPacket, myPacket, buf, MAXPKTSIZE); if (er.encoded == -1) { fprintf(stderr, "Cannot encode %s: %s\n", er.failed_type->name, strerror(errno)); goto fail; } else { printf("Structure successfully encoded\n"); /*xer_fprint(stdout, &asn_DEF_MyPacket, myPacket);*/ asn_DEF_MyPacket.free_struct (&asn_DEF_MyPacket, myPacket, 0); return er.encoded; } fail: fprintf(stderr, "Cannot encode %s: %s\n", er.failed_type->name, strerror(errno)); asn_DEF_MyPacket.free_struct(&asn_DEF_MyPacket, myPacket, 0); return -1; }