int main (int argc, char *argv[]) { asn1_retCode ec = 0; const char *errstr; do { errstr = asn1_strerror (ec); asn1_perror (ec); #ifndef ASN1_DISABLE_DEPRECATED errstr = libtasn1_strerror (ec); libtasn1_perror (ec); #endif ec++; } while (errstr); return 0; }
int main (int argc, char *argv[]) { int ec = 0; const char *errstr; int verbose = 0; if (argc > 1) verbose = 1; do { errstr = asn1_strerror (ec); if (verbose != 0) asn1_perror (ec); ec++; } while (errstr); return 0; }
int main (int argc, char *argv[]) { int result, der_len; unsigned char der[1024]; ASN1_TYPE PKIX1Implicit88 = ASN1_TYPE_EMPTY; char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE]; if (1) result = asn1_array2tree (pkix_asn1_tab, &PKIX1Implicit88, errorDescription); else result = asn1_parser2tree ("pkix.asn", &PKIX1Implicit88, errorDescription); if (result != ASN1_SUCCESS) { asn1_perror (result); printf ("%s", errorDescription); exit (1); } /* Use the following 3 lines to visit the PKIX1Implicit structures */ /* printf("-----------------\n"); asn1_visit_tree(PKIX1Implicit88,"PKIX1Implicit88"); printf("-----------------\n"); */ der_len = 1024; create_certificate (PKIX1Implicit88, der, &der_len); get_certificate (PKIX1Implicit88, der, der_len); /* Clear the "PKIX1Implicit88" structures */ asn1_delete_structure (&PKIX1Implicit88); return 0; }
int main (int argc, char *argv[]) { FILE *fpin, *fpout; const char *fileNameIn; const char *fileNameOut; unsigned char buffer[MAX_LENGTH_INT]; unsigned char newBuffer[2*MAX_LENGTH_INT]; char params[MAX_LENGTH_CMD]; char str[MAX_LENGTH_VAR]; // Number of polynomial or integer of a polynomial char n[MAX_LENGTH_VAR]; // Number of variables as a string char o[MAX_LENGTH_VAR]; // Number of polynomials as a string int nn; // Number of variables as an integer int oo; // Number of polynomials as an integer int i, j; int result, der_len; unsigned char der[MAX_DER_SIZE], ch[1]; /******* Define input and output files *******/ if (argc < 3) { printf("\nusage: %s ASN1BinaryFile PublicKeyIntFile\n\n", argv[0]); exit(1); } else { fileNameIn = argv[1]; fileNameOut = argv[2]; fpin = fopen(fileNameIn, "rb"); if (!fpin) { printf("\nError openning file: %s\n\n", fileNameIn); exit(1); } fpout = fopen(fileNameOut, "w"); if (!fpout) { fclose(fpin); printf("\nError openning file: %s\n\n", fileNameOut); exit(1); } } printf("\nDecoding an ASN.1 file (%s), into a text file (%s)\n", fileNameIn, fileNameOut); /******* ASN.1 initialization *******/ ASN1_TYPE PK_def = ASN1_TYPE_EMPTY; ASN1_TYPE PK_dec = ASN1_TYPE_EMPTY; char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE]; // Creates definition structures needed to manage the ASN.1 definitions result = asn1_array2tree(pkInt_asn1_tab, &PK_def, errorDescription); if (result != ASN1_SUCCESS) { asn1_perror(result); printf ("%s\n", errorDescription); exit (1); } // Creates a structure of type "PKInt" (from *.asn definition file) result = asn1_create_element(PK_def, "PKInt.PublicKey", &PK_dec); if ( result != ASN1_SUCCESS ) { printf("\nCould not create a new element to DECODE: %d\n", result); exit(1); } /******* Read data from input file *******/ der_len = i = 0; while ( 1 == fread(ch, 1, 1, fpin) ) { der[i] = ch[0]; i++; der_len++; } result = asn1_der_decoding(&PK_dec, der, der_len, errorDescription); if ( result != ASN1_SUCCESS ) { printf("\nCould not make a DER decoding: %d\n", result); printf("Error description: %s\n", errorDescription); asn1_perror(result); exit(2); } /*printf("------------DEC-----------------\n"); asn1_print_structure(stdout, PK_dec, "", ASN1_PRINT_ALL); printf("--------------------------------\n");*/ clrBuff(buffer, MAX_LENGTH_INT); // Necesary because asn1 functions just put data in buffer // without terminating it with 0x0!!! der_len = MAX_LENGTH_INT; result = asn1_read_value(PK_dec, "noVars", buffer, &der_len); if ( result != ASN1_SUCCESS ) { printf("\nCould not make a DER decoding (noVars): %d\n", result); printf("Error description: %s\n", errorDescription); exit(1); } nn = atoi(buffer); clrBuff(buffer, MAX_LENGTH_INT); der_len = MAX_LENGTH_INT; result = asn1_read_value(PK_dec, "noPoly", buffer, &der_len); if ( result != ASN1_SUCCESS ) { printf("\nCould not make a DER decoding (noPoly): %d\n", result); printf("Error description: %s\n", errorDescription); exit(1); } oo = atoi(buffer); printf("Number of variables: %d\nNumber of polynomials: %d\n", nn, oo); /******* Write 'n' and 'o' to output file ******/ sprintf(str, "%d", nn); fprintf(fpout, "%s\n", str); sprintf(str, "%d", oo); fprintf(fpout, "%s\n", str); // ******* Read all integers representing the polynomials ******* nn = nn + 2; // +2 because of linear terms vector and constant term for ( i=0; i<oo; i++ ) { for ( j=0; j<nn; j++) { der_len = MAX_LENGTH_INT; sprintf(o, "%d", i+1); // Number of polynomial as string in 'o' sprintf(n, "%d", j+1); // Number of integer as string in 'n' strcpy(params, "polys.?"); strcat(params, o); strcat(params, ".intData.?"); strcat(params, n); clrBuff(buffer, MAX_LENGTH_INT); result = asn1_read_value(PK_dec, params, buffer, &der_len); /******* Write that integer (as a string) to output file *******/ fprintf(fpout, "%s", buffer); fprintf(fpout, " "); if ( j == nn - 1 ) fprintf(fpout, "\n"); if ( result != ASN1_SUCCESS ) { asn1_perror(result); printf("Could not write value in 'intData', i: %d\n", i); exit(4); } } } printf("...Done!\n\n"); /******* Closing program *******/ asn1_delete_structure(&PK_dec); asn1_delete_structure(&PK_def); fclose(fpin); fclose(fpout); return 0; }
int main (int argc, char *argv[]) { int result; char buffer[5 * 1024]; char buffer2[5 * 1024]; asn1_node definitions = NULL; asn1_node asn1_element = NULL, cpy_node = NULL; char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE]; FILE *out, *fd; int start, end; ssize_t size; int size2; const char *treefile = getenv ("ASN1PKIX"); const char *derfile = getenv ("ASN1CRLDER"); int verbose = 0; if (argc > 1) verbose = 1; if (!treefile) treefile = "pkix.asn"; if (!derfile) derfile = "crl.der"; if (verbose) { printf ("\n\n/****************************************/\n"); printf ("/* Test sequence : Test_indefinite */\n"); printf ("/****************************************/\n\n"); printf ("ASN1TREE: %s\n", treefile); } /* Check version */ if (asn1_check_version ("0.3.3") == NULL) printf ("\nLibrary version check ERROR:\n actual version: %s\n\n", asn1_check_version (NULL)); result = asn1_parser2tree (treefile, &definitions, errorDescription); if (result != ASN1_SUCCESS) { asn1_perror (result); printf ("ErrorDescription = %s\n\n", errorDescription); exit (1); } out = stdout; fd = fopen (derfile, "rb"); if (fd == NULL) { printf ("Cannot read file %s\n", derfile); exit (1); } size = fread (buffer, 1, sizeof (buffer), fd); if (size <= 0) { printf ("Cannot read from file %s\n", derfile); exit (1); } fclose (fd); result = asn1_create_element (definitions, "PKIX1.CertificateList", &asn1_element); if (result != ASN1_SUCCESS) { asn1_perror (result); printf ("Cannot create CRL element\n"); exit (1); } result = asn1_der_decoding (&asn1_element, buffer, size, errorDescription); if (result != ASN1_SUCCESS) { asn1_perror (result); printf ("Cannot decode DER data (size %ld)\n", (long) size); exit (1); } /* test asn1_copy_node */ result = asn1_create_element (definitions, "PKIX1.CertificateList", &cpy_node); if (result != ASN1_SUCCESS) { asn1_perror (result); printf ("Cannot create CRL element\n"); exit (1); } result = asn1_copy_node(cpy_node, "", asn1_element, ""); if (result != ASN1_SUCCESS) { asn1_perror (result); printf ("Cannot copy node\n"); exit (1); } /* test whether the copied node encodes the same */ size2 = sizeof(buffer2); result = asn1_der_coding (cpy_node, "", buffer2, &size2, NULL); if (result != ASN1_SUCCESS) { asn1_perror (result); printf ("Cannot encode data (size %ld)\n", (long) size); exit (1); } if (size2 != size || memcmp(buffer, buffer2, size) != 0) { printf("DER encoded data differ!\n"); exit(1); } asn1_delete_structure (&cpy_node); /* Test asn1_dup_node */ cpy_node = asn1_dup_node(asn1_element, ""); if (cpy_node == NULL) { printf ("Cannot copy node (dup_node)\n"); exit (1); } /* test whether the copied node encodes the same */ size2 = sizeof(buffer2); result = asn1_der_coding (cpy_node, "", buffer2, &size2, NULL); if (result != ASN1_SUCCESS) { asn1_perror (result); printf ("Cannot encode data (size %ld)\n", (long) size); exit (1); } if (size2 != size || memcmp(buffer, buffer2, size) != 0) { printf("DER encoded data differ!\n"); exit(1); } result = asn1_der_decoding_startEnd (asn1_element, buffer, size, "tbsCertList.issuer", &start, &end); if (result != ASN1_SUCCESS) { asn1_perror (result); printf ("Cannot find start End\n"); exit (1); } if (start != 24 && end != 291) { printf("Error in start and end values for issuer. Have: %d..%d\n", start, end); exit(1); } result = asn1_der_decoding_startEnd (asn1_element, buffer, size, "signature", &start, &end); if (result != ASN1_SUCCESS) { asn1_perror (result); printf ("Cannot find start End\n"); exit (1); } if (start != 372 && end != 503) { printf("Error in start and end values for signature. Have: %d..%d\n", start, end); exit(1); } /* Clear the definition structures */ asn1_delete_structure (&asn1_element); asn1_delete_structure (&cpy_node); asn1_delete_structure (&definitions); if (out != stdout) fclose (out); exit (0); }
/** * libtasn1_perror - prints a string to stderr with a description of an error * @error: is an error returned by a libtasn1 function. * * This function is like perror(). The only difference is that it * accepts an error returned by a libtasn1 function. * * Deprecated: Use asn1_perror() instead. **/ void libtasn1_perror (asn1_retCode error) { asn1_perror (error); }
int main (int argc, char *argv[]) { int result, verbose = 0; asn1_node definitions = NULL; asn1_node asn1_element = NULL; char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE]; const char *treefile = getenv ("ASN1SETOF"); unsigned i; if (argc > 1) verbose = 1; if (!treefile) treefile = "setof.asn"; if (verbose != 0) { printf ("\n\n/****************************************/\n"); printf ("/* Test sequence : coding-decoding */\n"); printf ("/****************************************/\n\n"); } /* Check version */ if (asn1_check_version ("0.3.3") == NULL) printf ("\nLibrary version check ERROR:\n actual version: %s\n\n", asn1_check_version (NULL)); result = asn1_parser2tree (treefile, &definitions, errorDescription); if (result != ASN1_SUCCESS) { asn1_perror (result); printf ("ErrorDescription = %s\n\n", errorDescription); exit (1); } result = asn1_create_element (definitions, "TEST.Set", &asn1_element); if (result != ASN1_SUCCESS) { fprintf (stderr, "asn1_create_element(): "); asn1_perror (result); exit (1); } result = asn1_write_value (asn1_element, "", "NEW", 1); assert(result == ASN1_SUCCESS); result = asn1_write_value (asn1_element, "?LAST.val", "\x00\x02", 2); assert(result == ASN1_SUCCESS); result = asn1_write_value (asn1_element, "", "NEW", 1); assert(result == ASN1_SUCCESS); result = asn1_write_value (asn1_element, "?LAST.val", "\x00\x01\x00\x00", 4); assert(result == ASN1_SUCCESS); result = asn1_write_value (asn1_element, "", "NEW", 1); assert(result == ASN1_SUCCESS); result = asn1_write_value (asn1_element, "?LAST.val", "\x00\x00\x00\x00", 4); assert(result == ASN1_SUCCESS); result = asn1_write_value (asn1_element, "", "NEW", 1); assert(result == ASN1_SUCCESS); result = asn1_write_value (asn1_element, "?LAST.val", "\x00\x00\x00\x02", 4); assert(result == ASN1_SUCCESS); result = asn1_write_value (asn1_element, "", "NEW", 1); assert(result == ASN1_SUCCESS); result = asn1_write_value (asn1_element, "?LAST.val", "\x00\x00\x00\x01", 4); assert(result == ASN1_SUCCESS); result = asn1_write_value (asn1_element, "", "NEW", 1); assert(result == ASN1_SUCCESS); result = asn1_write_value (asn1_element, "?LAST.val", "\x01\x00\x00\x00", 4); assert(result == ASN1_SUCCESS); result = asn1_write_value (asn1_element, "", "NEW", 1); assert(result == ASN1_SUCCESS); result = asn1_write_value (asn1_element, "?LAST.val", "\x01\x01\x00\x00", 4); assert(result == ASN1_SUCCESS); result = asn1_write_value (asn1_element, "", "NEW", 1); assert(result == ASN1_SUCCESS); result = asn1_write_value (asn1_element, "?LAST.val", "\x05", 1); assert(result == ASN1_SUCCESS); result = asn1_write_value (asn1_element, "", "NEW", 1); assert(result == ASN1_SUCCESS); result = asn1_write_value (asn1_element, "?LAST.val", "\x01", 1); assert(result == ASN1_SUCCESS); /* Clear the definition structures */ result = asn1_der_coding (asn1_element, "", data, &data_size, NULL); if (result != ASN1_SUCCESS) { fprintf (stderr, "Encoding error.\n"); asn1_perror (result); exit (1); } asn1_delete_structure (&asn1_element); asn1_delete_structure (&definitions); if (data_size != sizeof(expected_der) || memcmp(data, expected_der, data_size) != 0) { fprintf(stderr, "encoded data differ to expected [%d - %d]!\n", data_size, (int)sizeof(expected_der)); printf("static unsigned char got[] = {\n"); for (i=0;i<(unsigned)data_size;i++) { printf("0x%.2x, ", (unsigned)data[i]); if ((i+1) % 8 == 0) printf("\n"); } printf("};\n"); printf("static unsigned char expected[] = {\n"); for (i=0;i<(unsigned)sizeof(expected_der);i++) { printf("0x%.2x, ", (unsigned)expected_der[i]); if ((i+1) % 8 == 0) printf("\n"); } printf("};\n"); exit(1); } if (verbose) printf ("Success\n"); exit (0); }
int main (int argc, char *argv[]) { asn1_retCode result; ASN1_TYPE definitions = ASN1_TYPE_EMPTY; ASN1_TYPE asn1_element = ASN1_TYPE_EMPTY; char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE]; const char *treefile = getenv ("ASN1ENCODING"); if (!treefile) treefile = "Test_encoding.asn"; printf ("\n\n/****************************************/\n"); printf ("/* Test sequence : coding-decoding */\n"); printf ("/****************************************/\n\n"); /* Check version */ if (asn1_check_version ("0.3.3") == NULL) printf ("\nLibrary version check ERROR:\n actual version: %s\n\n", asn1_check_version (NULL)); result = asn1_parser2tree (treefile, &definitions, errorDescription); if (result != ASN1_SUCCESS) { asn1_perror (result); printf ("ErrorDescription = %s\n\n", errorDescription); exit (1); } result = asn1_create_element (definitions, "TEST_TREE.Koko", &asn1_element); if (result != ASN1_SUCCESS) { fprintf (stderr, "asn1_create_element(): "); asn1_perror (result); exit (1); } result = asn1_write_value (asn1_element, "seqint", "NEW", 1); if (result != ASN1_SUCCESS) { fprintf (stderr, "asn1_write_value(): seqint "); asn1_perror (result); exit (1); } result = asn1_write_value (asn1_element, "seqint.?LAST", "1234", 0); if (result != ASN1_SUCCESS) { fprintf (stderr, "asn1_write_value(): seqint.?LAST "); asn1_perror (result); exit (1); } result = asn1_write_value (asn1_element, "int", "\x0f\xff\x01", 3); if (result != ASN1_SUCCESS) { fprintf (stderr, "asn1_write_value(): int "); asn1_perror (result); exit (1); } result = asn1_write_value (asn1_element, "str", "string", 6); if (result != ASN1_SUCCESS) { fprintf (stderr, "asn1_write_value(): str "); asn1_perror (result); exit (1); } /* Clear the definition structures */ asn1_delete_structure (&definitions); result = asn1_der_coding (asn1_element, "", data, &data_size, NULL); if (result != ASN1_SUCCESS) { fprintf (stderr, "Encoding error.\n"); asn1_perror (result); exit (1); } result = asn1_der_decoding (&asn1_element, data, data_size, NULL); if (result != ASN1_SUCCESS) { fprintf (stderr, "Decoding error.\n"); asn1_perror (result); exit (1); } asn1_delete_structure (&asn1_element); printf ("Success\n"); exit (0); }
int main (int argc, char *argv[]) { asn1_retCode result; char buffer[10 * 1024]; ASN1_TYPE definitions = ASN1_TYPE_EMPTY; ASN1_TYPE asn1_element = ASN1_TYPE_EMPTY; char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE]; FILE *out, *fd; ssize_t size; const char *treefile = getenv ("ASN1PKIX"); const char *indeffile = getenv ("ASN1INDEF"); if (!treefile) treefile = "pkix.asn"; if (!indeffile) indeffile = "TestIndef.p12"; printf ("\n\n/****************************************/\n"); printf ("/* Test sequence : Test_indefinite */\n"); printf ("/****************************************/\n\n"); printf ("ASN1TREE: %s\n", treefile); /* Check version */ if (asn1_check_version ("0.2.11") == NULL) printf ("\nLibrary version check ERROR:\n actual version: %s\n\n", asn1_check_version (NULL)); result = asn1_parser2tree (treefile, &definitions, errorDescription); if (result != ASN1_SUCCESS) { asn1_perror (result); printf ("ErrorDescription = %s\n\n", errorDescription); exit (1); } out = stdout; fd = fopen (indeffile, "rb"); if (fd == NULL) { printf ("Cannot read file %s\n", indeffile); exit (1); } size = fread (buffer, 1, sizeof (buffer), fd); if (size <= 0) { printf ("Cannot read from file %s\n", indeffile); exit (1); } fclose (fd); result = asn1_create_element (definitions, "PKIX1.pkcs-12-PFX", &asn1_element); if (result != ASN1_SUCCESS) { asn1_perror (result); printf ("Cannot create PKCS12 element\n"); exit (1); } result = asn1_der_decoding (&asn1_element, buffer, size, errorDescription); if (result != ASN1_SUCCESS) { asn1_perror (result); printf ("Cannot decode BER data (size %d)\n", size); exit (1); } /* Clear the definition structures */ asn1_delete_structure (&definitions); asn1_delete_structure (&asn1_element); if (out != stdout) fclose (out); exit (0); }
int main (int argc, char *argv[]) { asn1_retCode result; ASN1_TYPE definitions = ASN1_TYPE_EMPTY; char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE]; test_type *test; int errorCounter = 0, testCounter = 0; fileCorrectName = getenv ("ASN1PARSER"); if (!fileCorrectName) fileCorrectName = "Test_parser.asn"; printf ("\n\n/****************************************/\n"); printf ("/* Test sequence : Test_parser */\n"); printf ("/****************************************/\n\n"); printf ("ASN1PARSER: %s\n", fileCorrectName); result = asn1_parser2tree (fileCorrectName, &definitions, errorDescription); if (result != ASN1_SUCCESS) { printf ("File '%s' not correct\n", fileCorrectName); asn1_perror (result); printf ("ErrorDescription = %s\n\n", errorDescription); exit (1); } /* Only for Test */ /* asn1_visit_tree(stdout,definitions,"TEST_PARSER",ASN1_PRINT_ALL); */ /* Clear the definitions structures */ asn1_delete_structure (&definitions); test = test_array; while (test->lineNumber != 0) { testCounter++; createFile (test->lineNumber, test->line); result = asn1_parser2tree (fileErroredName, &definitions, errorDescription); asn1_delete_structure (&definitions); if ((result != test->errorNumber) || (strcmp (errorDescription, test->errorDescription))) { errorCounter++; printf ("ERROR N. %d:\n", errorCounter); printf (" Line %d - %s\n", test->lineNumber, test->line); printf (" Error expected: %s - %s\n", asn1_strerror (test->errorNumber), test->errorDescription); printf (" Error detected: %s - %s\n\n", asn1_strerror (result), errorDescription); } test++; } printf ("Total tests : %d\n", testCounter); printf ("Total errors: %d\n", errorCounter); if (errorCounter > 0) return 1; exit (0); }