Advertisement
AnthonyCagliano

Untitled

Apr 15th, 2023
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. // ===================================================================
  2. // header file
  3.  
  4. #define CRYPTX_ASN1_GETCLASS(flags) ((flag)>>1 & 0b11)
  5. #define CRYPTX_ASN1_ISCONSTRUCTED(flags) ((flags) & 1)
  6.  
  7. typedef enum {
  8. ASN1_OK,
  9. ASN1_EOF,
  10. ASN1_INVALID_ARG,
  11. ASN1_LEN_OVERFLOW,
  12. ASN1_SPEC_MISMATCH
  13. } asn1_error_t;
  14.  
  15. struct asn1_context {
  16. void *asn1_data_start;
  17. void *asn1_data_end;
  18. void *asn1_this;
  19. void *asn1_next;
  20. };
  21.  
  22. asn1_error_t cryptx_asn1_start(struct asn1_context *ctx, void *asn1_data, size_t len);
  23. asn1_error_t cryptx_asn1_decode(struct asn1_context *ctx, uint8_t **element, size_t *element_len, uint8_t *tag, uint8_t *flags);
  24. asn1_error_t cryptx_asn1_next(struct asn1_context *ctx);
  25.  
  26.  
  27. // ===================================================================
  28. // demo code
  29.  
  30. int main(void)
  31. {
  32. struct asn1_context asn1_state;
  33. asn1_error_t err;
  34. uint8_t *ptr;
  35. size_t elem_len;
  36. uint8_t tag;
  37. uint8_t flags;
  38.  
  39. sprintf(CEMU_CONSOLE, "\n\n----------------------------------\nENCODEX ASN.1 Decoder Demo\n");
  40.  
  41. err = cryptx_asn1_start(&asn1_state, asn1_demo, sizeof asn1_demo);
  42. sprintf(CEMU_CONSOLE, "\nASN.1 parser init complete. Exit code %u.\n", err);
  43. if(err != ASN1_OK) return 1;
  44.  
  45. while(err != ASN1_EOF){
  46. err = cryptx_asn1_decode(&asn1_state, &ptr, &elem_len, &tag, &flags);
  47. if(err)
  48. sprintf(CEMU_CONSOLE, "Decoder error %u\n", err);
  49. else
  50. sprintf(CEMU_CONSOLE, "Object Data, Tag Id: %u, Flags: %u, Size: %u, Addr: %p\n", tag, flags, elem_len, ptr);
  51. if(!CRYPTX_ASN1_ISCONSTRUCTED(flags)){
  52. err = cryptx_asn1_next(&asn1_state);
  53. }
  54. }
  55.  
  56.  
  57. return 0;
  58. }
  59.  
  60. // ===================================================================
  61. // output
  62.  
  63. ASN.1 parser init complete. Exit code 0.
  64. Object Data, Tag Id: 16, Flags: 1, Size: 159, Addr: d1ab80
  65. Object Data, Tag Id: 16, Flags: 1, Size: 13, Addr: d1ab82
  66. Object Data, Tag Id: 6, Flags: 0, Size: 9, Addr: d1ab84
  67. Object Data, Tag Id: 5, Flags: 0, Size: 0, Addr: d1ab8f
  68. Object Data, Tag Id: 3, Flags: 0, Size: 141, Addr: d1ab92
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement