Skip to content

Commit 5981bbb

Browse files
committed
More correct omit
1 parent 5915554 commit 5981bbb

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-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[None, None]):
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[None, bytes]
694+
return None, s
695+
696+
def i2m(self, pkt, x):
697+
# type: (ASN1_Packet, Optional[bytes]) -> bytes
698+
return x
699+
700+
685701
_CHOICE_T = Union['ASN1_Packet', Type[ASN1F_field[Any, Any]], 'ASN1F_PACKET']
686702

687703

scapy/layers/x509.py

Lines changed: 30 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,33 @@ class X509_AlgorithmIdentifier(ASN1_Packet):
866867
ASN1F_OID("algorithm", "1.2.840.113549.1.1.11"),
867868
MultipleTypeField(
868869
[
870+
(
871+
# RFC4055:
872+
# "The correct encoding is to omit the parameters field"
873+
# "All implementations MUST accept both NULL and absent
874+
# parameters as legal and equivalent encodings."
875+
876+
# RFC8017:
877+
# "should generally be omitted, but if present, it shall have a
878+
# value of type NULL."
879+
ASN1F_optional(ASN1F_NULL("parameters", None)),
880+
lambda pkt: (
881+
pkt.algorithm.val[:19] == "1.2.840.113549.1.1." or
882+
pkt.algorithm.val[:21] == "2.16.840.1.101.3.4.2."
883+
)
884+
),
885+
(
886+
# RFC5758:
887+
# "the encoding MUST omit the parameters field"
888+
889+
# RFC8410:
890+
# "For all of the OIDs, the parameters MUST be absent."
891+
ASN1F_omit("parameters", None),
892+
lambda pkt: (
893+
pkt.algorithm.val[:16] == "1.2.840.10045.4." or
894+
pkt.algorithm.val in ["1.3.101.112", "1.3.101.113"]
895+
)
896+
),
869897
# RFC5480
870898
(
871899
ASN1F_PACKET(
@@ -893,10 +921,9 @@ class X509_AlgorithmIdentifier(ASN1_Packet):
893921
),
894922
lambda pkt: pkt.algorithm.val == "1.2.840.113549.1.3.1",
895923
),
896-
897924
],
898-
# RFC4055 (=1.2.840.113549.1.1.11) / Default
899-
ASN1F_optional(ASN1F_NULL("parameters", 0)),
925+
# Default: fail, probably. This is most likely unimplemented.
926+
ASN1F_NULL("parameters", 0),
900927
)
901928
)
902929

0 commit comments

Comments
 (0)