Skip to content

Commit c808ff5

Browse files
committed
More correct omit
1 parent 5915554 commit c808ff5

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

scapy/asn1fields.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,6 @@ def m2i(self, pkt, s):
488488
obj.set_val(pkt, None)
489489
else:
490490
for obj in self.seq:
491-
# DEBUG: print(repr(obj), repr)
492491
try:
493492
s = obj.dissect(pkt, s)
494493
except ASN1F_badsequence:
@@ -642,6 +641,9 @@ class ASN1F_TIME_TICKS(ASN1F_INTEGER):
642641
#############################
643642

644643
class ASN1F_optional(ASN1F_element):
644+
"""
645+
ASN.1 field that is optional.
646+
"""
645647
def __init__(self, field):
646648
# type: (ASN1F_field[Any, Any]) -> None
647649
field.flexible_tag = False
@@ -682,6 +684,20 @@ def i2repr(self, pkt, x):
682684
return self._field.i2repr(pkt, x)
683685

684686

687+
class ASN1F_omit(ASN1F_field):
688+
"""
689+
ASN.1 field that is not specified. This is simply ommited on the network.
690+
This is different from ASN1F_NULL which has a network representation.
691+
"""
692+
def m2i(self, pkt, s):
693+
# type: (ASN1_Packet, bytes) -> Tuple[Any, bytes]
694+
return None, s
695+
696+
def dissect(self, pkt, s):
697+
# type: (ASN1_Packet, bytes) -> bytes
698+
return s
699+
700+
685701
_CHOICE_T = Union['ASN1_Packet', Type[ASN1F_field[Any, Any]], 'ASN1F_PACKET']
686702

687703

scapy/layers/x509.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
ASN1F_ISO646_STRING,
3737
ASN1F_NULL,
3838
ASN1F_OID,
39+
ASN1F_omit,
3940
ASN1F_optional,
4041
ASN1F_PACKET,
4142
ASN1F_PRINTABLE_STRING,
@@ -866,6 +867,27 @@ class X509_AlgorithmIdentifier(ASN1_Packet):
866867
ASN1F_OID("algorithm", "1.2.840.113549.1.1.11"),
867868
MultipleTypeField(
868869
[
870+
# RFC4055
871+
(
872+
# "The correct encoding is to omit the parameters field"
873+
# "All implementations MUST accept both NULL and absent parameters as
874+
# legal and equivalent encodings."
875+
ASN1F_optional(ASN1F_NULL("parameters", None)),
876+
lambda pkt: pkt.algorithm.val[:19] == "1.2.840.113549.1.1.",
877+
),
878+
# RFC8017
879+
(
880+
# "should generally be omitted, but if present, it shall have a
881+
# value of type NULL."
882+
ASN1F_optional(ASN1F_NULL("parameters", None)),
883+
lambda pkt: pkt.algorithm.val[:21] == "2.16.840.1.101.3.4.2.",
884+
),
885+
# RFC5758
886+
(
887+
# "the encoding MUST omit the parameters field"
888+
ASN1F_omit("parameters", None),
889+
lambda pkt: pkt.algorithm.val[:16] == "1.2.840.10045.4.",
890+
),
869891
# RFC5480
870892
(
871893
ASN1F_PACKET(
@@ -893,10 +915,9 @@ class X509_AlgorithmIdentifier(ASN1_Packet):
893915
),
894916
lambda pkt: pkt.algorithm.val == "1.2.840.113549.1.3.1",
895917
),
896-
897918
],
898-
# RFC4055 (=1.2.840.113549.1.1.11) / Default
899-
ASN1F_optional(ASN1F_NULL("parameters", 0)),
919+
# Default: fail, probably. This is most likely unimplemented.
920+
ASN1F_NULL("parameters", 0),
900921
)
901922
)
902923

0 commit comments

Comments
 (0)