Advertisement
AnthonyCagliano

Untitled

Apr 14th, 2023
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. struct asn1_specification {
  2. uint8_t len;
  3. uint8_t elem[1];
  4. };
  5.  
  6.  
  7.  
  8.  
  9. asn1_error_t asn1_decode(uint8_t *asn1_data, size_t asn1_len, asn1_obj_t *objs, size_t *objs_ret, struct asn1_specification *spec){
  10. uint8_t *asn1_current = asn1_data; // set current to start of data to decode
  11. uint8_t *asn1_end = asn1_current + asn1_len;
  12. if(*asn1_current == 0) asn1_current++;
  13. size_t i;
  14.  
  15. for(i = 0; asn1_current < asn1_end; i++){ // loop until iter count hit. Break manually if done.
  16. asn1_obj_t *node_o = &objs[i];
  17. uint8_t spec_tag_form = (spec == NULL) ? 0 : spec->elem[i];
  18. uint8_t tag = *asn1_current++;
  19. //node_o->tag = (*asn1_current++) & 0b11111; // get numeric tag id
  20. uint8_t byte_2nd = *asn1_current++; // get byte 2. Can be size or can be size of size
  21. node_o->len = 0;
  22. if((byte_2nd>>7) & 1){ // if bit 7 of byte is set, this is a size word length
  23. uint8_t size_len = byte_2nd & 0x7f;
  24. if(size_len > 3) return ASN1_ARCH_BAD_LEN; // due to device limits, seq len limited to u24.
  25. rmemcpy((uint8_t*)&node_o->len, asn1_current, size_len);
  26. asn1_current += size_len;
  27. }
  28. else // else, this is a size byte
  29. node_o->len = byte_2nd;
  30.  
  31. // pull out tag with associated flags
  32. node_o->tag = tag & 0b11111;
  33. node_o->f_constr = (tag>>5 & 1);
  34. node_o->f_class = (tag>>6 & 0b11);
  35.  
  36. // retrieve data addr and length
  37. node_o->data = asn1_current;
  38. asn1_current += node_o->len;
  39.  
  40. // if the constructed flag is set, this element may contain other encoded types
  41. if((node_o->f_constr) || (spec_tag_form == 1)){
  42. size_t node_interior_len;
  43. if(asn1_decode(node_o->data, node_o->len, node_o, &node_interior_len, NULL) == ASN1_OK){
  44. i--;
  45. i += node_interior_len;
  46. }
  47. }
  48.  
  49. }
  50. *objs_ret = i;
  51. if (spec != NULL) {if(i != spec->len) return ASN1_SPEC_MISMATCH;}
  52. return ASN1_OK;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement