Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/scapy/layers/x509.py: 88%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# SPDX-License-Identifier: GPL-2.0-only
2# This file is part of Scapy
3# See https://scapy.net/ for more information
4# Copyright (C) Philippe Biondi <phil@secdev.org>
5# Acknowledgment: Arnaud Ebalard & Maxence Tury
7# Cool history about this file: http://natisbad.org/scapy/index.html
9"""
10X.509 certificates, OCSP, CRL, CMS and other crypto-related ASN.1 structures
11"""
13from scapy.asn1.ber import BER_Decoding_Error
14from scapy.asn1.mib import conf # loads conf.mib
15from scapy.asn1.asn1 import (
16 ASN1_Codecs,
17 ASN1_IA5_STRING,
18 ASN1_OID,
19 ASN1_PRINTABLE_STRING,
20 ASN1_UTC_TIME,
21 ASN1_UTF8_STRING,
22)
23from scapy.asn1packet import ASN1_Packet
24from scapy.asn1fields import (
25 ASN1F_BIT_STRING_ENCAPS,
26 ASN1F_BIT_STRING,
27 ASN1F_BMP_STRING,
28 ASN1F_BOOLEAN,
29 ASN1F_CHOICE,
30 ASN1F_enum_INTEGER,
31 ASN1F_ENUMERATED,
32 ASN1F_field,
33 ASN1F_FLAGS,
34 ASN1F_GENERALIZED_TIME,
35 ASN1F_IA5_STRING,
36 ASN1F_INTEGER,
37 ASN1F_ISO646_STRING,
38 ASN1F_NULL,
39 ASN1F_OID,
40 ASN1F_omit,
41 ASN1F_optional,
42 ASN1F_PACKET,
43 ASN1F_PRINTABLE_STRING,
44 ASN1F_SEQUENCE_OF,
45 ASN1F_SEQUENCE,
46 ASN1F_SET_OF,
47 ASN1F_STRING_ENCAPS,
48 ASN1F_STRING_PacketField,
49 ASN1F_STRING,
50 ASN1F_T61_STRING,
51 ASN1F_UNIVERSAL_STRING,
52 ASN1F_UTC_TIME,
53 ASN1F_UTF8_STRING,
54)
55from scapy.packet import Packet
56from scapy.fields import (
57 MultipleTypeField,
58 PacketField,
59)
60from scapy.volatile import ZuluTime, GeneralizedTime
61from scapy.compat import plain_str
63from scapy.layers.tpm import KeyAttestationStatement
66class ASN1P_OID(ASN1_Packet):
67 ASN1_codec = ASN1_Codecs.BER
68 ASN1_root = ASN1F_OID("oid", "0")
71class ASN1P_INTEGER(ASN1_Packet):
72 ASN1_codec = ASN1_Codecs.BER
73 ASN1_root = ASN1F_INTEGER("number", 0)
76class ASN1P_PRIVSEQ(ASN1_Packet):
77 # This class gets used in x509.uts
78 # It showcases the private high-tag decoding capacities of scapy.
79 ASN1_codec = ASN1_Codecs.BER
80 ASN1_root = ASN1F_SEQUENCE(
81 ASN1F_IA5_STRING("str", ""),
82 ASN1F_STRING("int", 0),
83 explicit_tag=0,
84 flexible_tag=True,
85 )
88#######################
89# RSA packets #
90#######################
91# based on RFC 3447
94# It could be interesting to use os.urandom and try to generate
95# a new modulus each time RSAPublicKey is called with default values.
96# (We might have to dig into scapy field initialization mechanisms...)
97# NEVER rely on the key below, which is provided only for debugging purposes.
98class RSAPublicKey(ASN1_Packet):
99 ASN1_codec = ASN1_Codecs.BER
100 ASN1_root = ASN1F_SEQUENCE(
101 ASN1F_INTEGER("modulus", 10),
102 ASN1F_INTEGER("publicExponent", 3),
103 )
106class RSAOtherPrimeInfo(ASN1_Packet):
107 ASN1_codec = ASN1_Codecs.BER
108 ASN1_root = ASN1F_SEQUENCE(
109 ASN1F_INTEGER("prime", 0),
110 ASN1F_INTEGER("exponent", 0),
111 ASN1F_INTEGER("coefficient", 0),
112 )
115class RSAPrivateKey(ASN1_Packet):
116 ASN1_codec = ASN1_Codecs.BER
117 ASN1_root = ASN1F_SEQUENCE(
118 ASN1F_enum_INTEGER("version", 0, ["two-prime", "multi"]),
119 ASN1F_INTEGER("modulus", 10),
120 ASN1F_INTEGER("publicExponent", 3),
121 ASN1F_INTEGER("privateExponent", 3),
122 ASN1F_INTEGER("prime1", 2),
123 ASN1F_INTEGER("prime2", 5),
124 ASN1F_INTEGER("exponent1", 0),
125 ASN1F_INTEGER("exponent2", 3),
126 ASN1F_INTEGER("coefficient", 1),
127 ASN1F_optional(ASN1F_SEQUENCE_OF("otherPrimeInfos", None, RSAOtherPrimeInfo)),
128 )
131####################################
132# Diffie Hellman Packets #
133####################################
134# From X9.42 (or RFC3279)
137class ValidationParms(ASN1_Packet):
138 ASN1_codec = ASN1_Codecs.BER
139 ASN1_root = ASN1F_SEQUENCE(
140 ASN1F_BIT_STRING("seed", b""),
141 ASN1F_INTEGER("pgenCounter", 0),
142 )
145class DomainParameters(ASN1_Packet):
146 ASN1_codec = ASN1_Codecs.BER
147 ASN1_root = ASN1F_SEQUENCE(
148 ASN1F_INTEGER("p", 0),
149 ASN1F_INTEGER("g", 0),
150 ASN1F_INTEGER("q", 0),
151 ASN1F_optional(ASN1F_INTEGER("j", 0)),
152 ASN1F_optional(
153 ASN1F_PACKET("validationParms", None, ValidationParms),
154 ),
155 )
158class DHPublicKey(ASN1_Packet):
159 ASN1_codec = ASN1_Codecs.BER
160 ASN1_root = ASN1F_INTEGER("y", 0)
163####################################
164# ECDSA packets #
165####################################
166# based on RFC 3279 & 5480 & 5915
169class ECFieldID(ASN1_Packet):
170 # No characteristic-two-field support for now.
171 ASN1_codec = ASN1_Codecs.BER
172 ASN1_root = ASN1F_SEQUENCE(
173 ASN1F_OID("fieldType", "prime-field"), ASN1F_INTEGER("prime", 0)
174 )
177class ECCurve(ASN1_Packet):
178 ASN1_codec = ASN1_Codecs.BER
179 ASN1_root = ASN1F_SEQUENCE(
180 ASN1F_STRING("a", ""),
181 ASN1F_STRING("b", ""),
182 ASN1F_optional(ASN1F_BIT_STRING("seed", None)),
183 )
186class ECSpecifiedDomain(ASN1_Packet):
187 ASN1_codec = ASN1_Codecs.BER
188 ASN1_root = ASN1F_SEQUENCE(
189 ASN1F_enum_INTEGER("version", 1, {1: "ecpVer1"}),
190 ASN1F_PACKET("fieldID", ECFieldID(), ECFieldID),
191 ASN1F_PACKET("curve", ECCurve(), ECCurve),
192 ASN1F_STRING("base", ""),
193 ASN1F_INTEGER("order", 0),
194 ASN1F_optional(ASN1F_INTEGER("cofactor", None)),
195 )
198class ECParameters(ASN1_Packet):
199 ASN1_codec = ASN1_Codecs.BER
200 ASN1_root = ASN1F_CHOICE(
201 "curve",
202 ASN1_OID("ansip384r1"),
203 ASN1F_OID, # for named curves
204 ASN1F_NULL, # for implicit curves
205 ECSpecifiedDomain,
206 )
209class ECDSAPublicKey(ASN1_Packet):
210 ASN1_codec = ASN1_Codecs.BER
211 ASN1_root = ASN1F_BIT_STRING("ecPoint", b"")
214class ECDSAPrivateKey(ASN1_Packet):
215 ASN1_codec = ASN1_Codecs.BER
216 ASN1_root = ASN1F_SEQUENCE(
217 ASN1F_enum_INTEGER("version", 1, {1: "ecPrivkeyVer1"}),
218 ASN1F_STRING("privateKey", ""),
219 ASN1F_optional(
220 ASN1F_PACKET("parameters", None, ECParameters, explicit_tag=0xA0)
221 ),
222 ASN1F_optional(
223 ASN1F_PACKET("publicKey", None, ECDSAPublicKey, explicit_tag=0xA1)
224 ),
225 )
228class ECDSASignature(ASN1_Packet):
229 ASN1_codec = ASN1_Codecs.BER
230 ASN1_root = ASN1F_SEQUENCE(ASN1F_INTEGER("r", 0), ASN1F_INTEGER("s", 0))
233####################################
234# Diffie Hellman Exchange Packets #
235####################################
236# based on PKCS#3
238# PKCS#3 sect 9
241class DHParameter(ASN1_Packet):
242 ASN1_codec = ASN1_Codecs.BER
243 ASN1_root = ASN1F_SEQUENCE(
244 ASN1F_INTEGER("p", 0),
245 ASN1F_INTEGER("g", 0),
246 ASN1F_optional(ASN1F_INTEGER("l", 0)), # aka. 'privateValueLength'
247 )
250####################################
251# x25519/x448 packets #
252####################################
253# based on RFC 8410
256class EdDSAPublicKey(ASN1_Packet):
257 ASN1_codec = ASN1_Codecs.BER
258 ASN1_root = ASN1F_BIT_STRING("edKey", b"")
261class CurvePrivateKey(ASN1_Packet):
262 ASN1_codec = ASN1_Codecs.BER
263 ASN1_root = ASN1F_STRING("privateKey", "")
266EdDSAPrivateKey = CurvePrivateKey
269####################################
270# ML-DSA packets #
271####################################
272# based on RFC 9881
275class MLDSAPublicKey(ASN1_Packet):
276 ASN1_codec = ASN1_Codecs.BER
277 ASN1_root = ASN1F_BIT_STRING("mlKey", b"")
280class MLDSAPrivateKeySeed(ASN1_Packet):
281 ASN1_codec = ASN1_Codecs.BER
282 ASN1_root = ASN1F_STRING("seed", "", implicit_tag=0x80)
285class MLDSAPrivateKeyExpandedKey(ASN1_Packet):
286 ASN1_codec = ASN1_Codecs.BER
287 ASN1_root = ASN1F_STRING("expandedKey", "")
290class MLDSAPrivateKeyBoth(ASN1_Packet):
291 ASN1_codec = ASN1_Codecs.BER
292 ASN1_root = ASN1F_SEQUENCE(
293 ASN1F_STRING("seed", ""),
294 ASN1F_STRING("expandedKey", ""),
295 )
298class MLDSAPrivateKey(ASN1_Packet):
299 ASN1_codec = ASN1_Codecs.BER
300 ASN1_root = ASN1F_CHOICE(
301 "privateKey",
302 MLDSAPrivateKeyExpandedKey(),
303 MLDSAPrivateKeySeed,
304 MLDSAPrivateKeyExpandedKey,
305 MLDSAPrivateKeyBoth,
306 )
309######################
310# X509 packets #
311######################
312# based on RFC 5280
315# Names #
318class ASN1F_X509_DirectoryString(ASN1F_CHOICE):
319 # we include ASN1 bit strings and bmp strings for rare instances of x500 addresses
320 def __init__(self, name, default, **kwargs):
321 ASN1F_CHOICE.__init__(
322 self,
323 name,
324 default,
325 ASN1F_PRINTABLE_STRING,
326 ASN1F_UTF8_STRING,
327 ASN1F_IA5_STRING,
328 ASN1F_T61_STRING,
329 ASN1F_UNIVERSAL_STRING,
330 ASN1F_BIT_STRING,
331 ASN1F_BMP_STRING,
332 **kwargs,
333 )
336# More details on attributes in PKCS#9
337_X509_ATTRIBUTE_TYPE = {}
340class _AttributeValue_Field(ASN1F_field):
341 def m2i(self, pkt, s):
342 # Some types have special structures
343 if pkt.underlayer:
344 attrType = pkt.underlayer.type.val
345 if attrType in _X509_ATTRIBUTE_TYPE:
346 return self.extract_packet(
347 _X509_ATTRIBUTE_TYPE[attrType],
348 s,
349 _underlayer=pkt,
350 )
351 try:
352 return super(_AttributeValue_Field, self).m2i(pkt, s)
353 except BER_Decoding_Error:
354 # Do not fail on special attributes
355 return s, b""
357 def i2m(self, pkt, x):
358 # The special structures should be just bytes()
359 if pkt.underlayer and pkt.underlayer.type.val in _X509_ATTRIBUTE_TYPE:
360 return bytes(x)
361 return super(_AttributeValue_Field, self).i2m(pkt, x)
364class X509_AttributeValue(ASN1_Packet):
365 ASN1_codec = ASN1_Codecs.BER
366 ASN1_root = _AttributeValue_Field("value", ASN1_PRINTABLE_STRING("FR"))
369class X509_Attribute(ASN1_Packet):
370 ASN1_codec = ASN1_Codecs.BER
371 ASN1_root = ASN1F_SEQUENCE(
372 ASN1F_OID("type", "2.5.4.6"),
373 ASN1F_SET_OF("values", [X509_AttributeValue()], X509_AttributeValue),
374 )
377class X509_AttributeTypeAndValue(ASN1_Packet):
378 ASN1_codec = ASN1_Codecs.BER
379 ASN1_root = ASN1F_SEQUENCE(
380 ASN1F_OID("type", "2.5.4.6"),
381 ASN1F_X509_DirectoryString("value", ASN1_PRINTABLE_STRING("FR")),
382 )
385class X509_RDN(ASN1_Packet):
386 ASN1_codec = ASN1_Codecs.BER
387 ASN1_root = ASN1F_SET_OF(
388 "rdn", [X509_AttributeTypeAndValue()], X509_AttributeTypeAndValue
389 )
392class X509_OtherName(ASN1_Packet):
393 ASN1_codec = ASN1_Codecs.BER
394 ASN1_root = ASN1F_SEQUENCE(
395 ASN1F_OID("type_id", "0"),
396 ASN1F_CHOICE(
397 "value",
398 None,
399 ASN1F_IA5_STRING,
400 ASN1F_ISO646_STRING,
401 ASN1F_BMP_STRING,
402 ASN1F_UTF8_STRING,
403 ASN1F_STRING,
404 explicit_tag=0xA0,
405 ),
406 )
409class ASN1F_X509_otherName(ASN1F_SEQUENCE):
410 # field version of X509_OtherName, for usage in [MS-WCCE]
411 def __init__(self, **kargs):
412 seq = [ASN1F_SEQUENCE(*X509_OtherName.ASN1_root.seq, implicit_tag=0xA0)]
413 ASN1F_SEQUENCE.__init__(self, *seq, **kargs)
416class X509_RFC822Name(ASN1_Packet):
417 ASN1_codec = ASN1_Codecs.BER
418 ASN1_root = ASN1F_IA5_STRING("rfc822Name", "")
421class X509_DNSName(ASN1_Packet):
422 ASN1_codec = ASN1_Codecs.BER
423 ASN1_root = ASN1F_IA5_STRING("dNSName", "")
426# XXX write me
429class X509_X400Address(ASN1_Packet):
430 ASN1_codec = ASN1_Codecs.BER
431 ASN1_root = ASN1F_field("x400Address", "")
434_default_directoryName = [
435 X509_RDN(),
436 X509_RDN(
437 rdn=[
438 X509_AttributeTypeAndValue(
439 type=ASN1_OID("2.5.4.10"), value=ASN1_PRINTABLE_STRING("Scapy, Inc.")
440 )
441 ]
442 ),
443 X509_RDN(
444 rdn=[
445 X509_AttributeTypeAndValue(
446 type=ASN1_OID("2.5.4.3"),
447 value=ASN1_PRINTABLE_STRING("Scapy Default Name"),
448 )
449 ]
450 ),
451]
454class X509_DirectoryName(ASN1_Packet):
455 ASN1_codec = ASN1_Codecs.BER
456 ASN1_root = ASN1F_SEQUENCE_OF("directoryName", _default_directoryName, X509_RDN)
459class X509_EDIPartyName(ASN1_Packet):
460 ASN1_codec = ASN1_Codecs.BER
461 ASN1_root = ASN1F_SEQUENCE(
462 ASN1F_optional(
463 ASN1F_X509_DirectoryString("nameAssigner", None, explicit_tag=0xA0)
464 ),
465 ASN1F_X509_DirectoryString("partyName", None, explicit_tag=0xA1),
466 )
469class X509_URI(ASN1_Packet):
470 ASN1_codec = ASN1_Codecs.BER
471 ASN1_root = ASN1F_IA5_STRING("uniformResourceIdentifier", "")
474class X509_IPAddress(ASN1_Packet):
475 ASN1_codec = ASN1_Codecs.BER
476 ASN1_root = ASN1F_STRING("iPAddress", "")
479class X509_RegisteredID(ASN1_Packet):
480 ASN1_codec = ASN1_Codecs.BER
481 ASN1_root = ASN1F_OID("registeredID", "")
484class X509_GeneralName(ASN1_Packet):
485 ASN1_codec = ASN1_Codecs.BER
486 ASN1_root = ASN1F_CHOICE(
487 "generalName",
488 X509_DirectoryName(),
489 ASN1F_PACKET("otherName", None, X509_OtherName, implicit_tag=0xA0),
490 ASN1F_PACKET("rfc822Name", None, X509_RFC822Name, implicit_tag=0x81),
491 ASN1F_PACKET("dNSName", None, X509_DNSName, implicit_tag=0x82),
492 ASN1F_PACKET(
493 "x400Address", None, X509_X400Address, explicit_tag=0xA3
494 ),
495 ASN1F_PACKET(
496 "directoryName", None, X509_DirectoryName, explicit_tag=0xA4
497 ),
498 ASN1F_PACKET(
499 "ediPartyName", None, X509_EDIPartyName, explicit_tag=0xA5
500 ),
501 ASN1F_PACKET(
502 "uniformResourceIdentifier", None, X509_URI, implicit_tag=0x86
503 ),
504 ASN1F_PACKET("ipAddress", None, X509_IPAddress, implicit_tag=0x87),
505 ASN1F_PACKET(
506 "registeredID", None, X509_RegisteredID, implicit_tag=0x88
507 ),
508 )
511# Extensions #
514class X509_ExtAuthorityKeyIdentifier(ASN1_Packet):
515 ASN1_codec = ASN1_Codecs.BER
516 ASN1_root = ASN1F_SEQUENCE(
517 ASN1F_optional(ASN1F_STRING("keyIdentifier", b"\xff" * 20, implicit_tag=0x80)),
518 ASN1F_optional(
519 ASN1F_SEQUENCE_OF(
520 "authorityCertIssuer", None, X509_GeneralName, implicit_tag=0xA1
521 )
522 ),
523 ASN1F_optional(
524 ASN1F_INTEGER("authorityCertSerialNumber", None, implicit_tag=0x82)
525 ),
526 )
529class X509_ExtSubjectDirectoryAttributes(ASN1_Packet):
530 ASN1_codec = ASN1_Codecs.BER
531 ASN1_root = ASN1F_SEQUENCE_OF(
532 "subjectDirectoryAttributes", [X509_Attribute()], X509_Attribute
533 )
536class X509_ExtSubjectKeyIdentifier(ASN1_Packet):
537 ASN1_codec = ASN1_Codecs.BER
538 ASN1_root = ASN1F_STRING("keyIdentifier", "xff" * 20)
541class X509_ExtFullName(ASN1_Packet):
542 ASN1_codec = ASN1_Codecs.BER
543 ASN1_root = ASN1F_SEQUENCE_OF(
544 "fullName", [X509_GeneralName()], X509_GeneralName, implicit_tag=0xA0
545 )
548class X509_ExtNameRelativeToCRLIssuer(ASN1_Packet):
549 ASN1_codec = ASN1_Codecs.BER
550 ASN1_root = ASN1F_PACKET(
551 "nameRelativeToCRLIssuer", X509_RDN(), X509_RDN, implicit_tag=0xA1
552 )
555class X509_ExtDistributionPointName(ASN1_Packet):
556 ASN1_codec = ASN1_Codecs.BER
557 ASN1_root = ASN1F_CHOICE(
558 "distributionPointName", None, X509_ExtFullName, X509_ExtNameRelativeToCRLIssuer
559 )
562_reasons_mapping = [
563 "unused",
564 "keyCompromise",
565 "cACompromise",
566 "affiliationChanged",
567 "superseded",
568 "cessationOfOperation",
569 "certificateHold",
570 "privilegeWithdrawn",
571 "aACompromise",
572]
575class X509_ExtDistributionPoint(ASN1_Packet):
576 ASN1_codec = ASN1_Codecs.BER
577 ASN1_root = ASN1F_SEQUENCE(
578 ASN1F_optional(
579 ASN1F_PACKET(
580 "distributionPoint",
581 X509_ExtDistributionPointName(),
582 X509_ExtDistributionPointName,
583 explicit_tag=0xA0,
584 )
585 ),
586 ASN1F_optional(
587 ASN1F_FLAGS("reasons", None, _reasons_mapping, implicit_tag=0x81)
588 ),
589 ASN1F_optional(
590 ASN1F_SEQUENCE_OF("cRLIssuer", None, X509_GeneralName, implicit_tag=0xA2)
591 ),
592 )
595_ku_mapping = [
596 "digitalSignature",
597 "nonRepudiation",
598 "keyEncipherment",
599 "dataEncipherment",
600 "keyAgreement",
601 "keyCertSign",
602 "cRLSign",
603 "encipherOnly",
604 "decipherOnly",
605]
608class X509_ExtKeyUsage(ASN1_Packet):
609 ASN1_codec = ASN1_Codecs.BER
610 ASN1_root = ASN1F_FLAGS("keyUsage", "101", _ku_mapping)
612 def get_keyUsage(self):
613 return self.ASN1_root.get_flags(self)
616class X509_ExtPrivateKeyUsagePeriod(ASN1_Packet):
617 ASN1_codec = ASN1_Codecs.BER
618 ASN1_root = ASN1F_SEQUENCE(
619 ASN1F_optional(
620 ASN1F_GENERALIZED_TIME(
621 "notBefore", str(GeneralizedTime(-600)), implicit_tag=0x80
622 )
623 ),
624 ASN1F_optional(
625 ASN1F_GENERALIZED_TIME(
626 "notAfter", str(GeneralizedTime(+86400)), implicit_tag=0x81
627 )
628 ),
629 )
632class X509_PolicyMapping(ASN1_Packet):
633 ASN1_codec = ASN1_Codecs.BER
634 ASN1_root = ASN1F_SEQUENCE(
635 ASN1F_OID("issuerDomainPolicy", None), ASN1F_OID("subjectDomainPolicy", None)
636 )
639class X509_ExtPolicyMappings(ASN1_Packet):
640 ASN1_codec = ASN1_Codecs.BER
641 ASN1_root = ASN1F_SEQUENCE_OF("policyMappings", [], X509_PolicyMapping)
644class X509_ExtBasicConstraints(ASN1_Packet):
645 # The cA field should not be optional, but some certs omit it for False.
646 ASN1_codec = ASN1_Codecs.BER
647 ASN1_root = ASN1F_SEQUENCE(
648 ASN1F_optional(ASN1F_BOOLEAN("cA", False)),
649 ASN1F_optional(ASN1F_INTEGER("pathLenConstraint", None)),
650 )
653class X509_ExtCRLNumber(ASN1_Packet):
654 ASN1_codec = ASN1_Codecs.BER
655 ASN1_root = ASN1F_INTEGER("cRLNumber", 0)
658_cRL_reasons = [
659 "unspecified",
660 "keyCompromise",
661 "cACompromise",
662 "affiliationChanged",
663 "superseded",
664 "cessationOfOperation",
665 "certificateHold",
666 "unused_reasonCode",
667 "removeFromCRL",
668 "privilegeWithdrawn",
669 "aACompromise",
670]
673class X509_ExtReasonCode(ASN1_Packet):
674 ASN1_codec = ASN1_Codecs.BER
675 ASN1_root = ASN1F_ENUMERATED("cRLReason", 0, _cRL_reasons)
678class X509_ExtDeltaCRLIndicator(ASN1_Packet):
679 ASN1_codec = ASN1_Codecs.BER
680 ASN1_root = ASN1F_INTEGER("deltaCRLIndicator", 0)
683class X509_ExtIssuingDistributionPoint(ASN1_Packet):
684 ASN1_codec = ASN1_Codecs.BER
685 ASN1_root = ASN1F_SEQUENCE(
686 ASN1F_optional(
687 ASN1F_PACKET(
688 "distributionPoint",
689 X509_ExtDistributionPointName(),
690 X509_ExtDistributionPointName,
691 explicit_tag=0xA0,
692 )
693 ),
694 ASN1F_BOOLEAN("onlyContainsUserCerts", False, implicit_tag=0x81),
695 ASN1F_BOOLEAN("onlyContainsCACerts", False, implicit_tag=0x82),
696 ASN1F_optional(
697 ASN1F_FLAGS("onlySomeReasons", None, _reasons_mapping, implicit_tag=0x83)
698 ),
699 ASN1F_BOOLEAN("indirectCRL", False, implicit_tag=0x84),
700 ASN1F_BOOLEAN("onlyContainsAttributeCerts", False, implicit_tag=0x85),
701 )
704class X509_ExtCertificateIssuer(ASN1_Packet):
705 ASN1_codec = ASN1_Codecs.BER
706 ASN1_root = ASN1F_SEQUENCE_OF("certificateIssuer", [], X509_GeneralName)
709class X509_ExtInvalidityDate(ASN1_Packet):
710 ASN1_codec = ASN1_Codecs.BER
711 ASN1_root = ASN1F_GENERALIZED_TIME("invalidityDate", str(ZuluTime(+86400)))
714class X509_ExtSubjectAltName(ASN1_Packet):
715 ASN1_codec = ASN1_Codecs.BER
716 ASN1_root = ASN1F_SEQUENCE_OF("subjectAltName", [], X509_GeneralName)
719class X509_ExtIssuerAltName(ASN1_Packet):
720 ASN1_codec = ASN1_Codecs.BER
721 ASN1_root = ASN1F_SEQUENCE_OF("issuerAltName", [], X509_GeneralName)
724class X509_ExtGeneralSubtree(ASN1_Packet):
725 # 'minimum' is not optional in RFC 5280, yet it is in some implementations.
726 ASN1_codec = ASN1_Codecs.BER
727 ASN1_root = ASN1F_SEQUENCE(
728 ASN1F_PACKET("base", X509_GeneralName(), X509_GeneralName),
729 ASN1F_optional(ASN1F_INTEGER("minimum", None, implicit_tag=0x80)),
730 ASN1F_optional(ASN1F_INTEGER("maximum", None, implicit_tag=0x81)),
731 )
734class X509_ExtNameConstraints(ASN1_Packet):
735 ASN1_codec = ASN1_Codecs.BER
736 ASN1_root = ASN1F_SEQUENCE(
737 ASN1F_optional(
738 ASN1F_SEQUENCE_OF(
739 "permittedSubtrees", None, X509_ExtGeneralSubtree, implicit_tag=0xA0
740 )
741 ),
742 ASN1F_optional(
743 ASN1F_SEQUENCE_OF(
744 "excludedSubtrees", None, X509_ExtGeneralSubtree, implicit_tag=0xA1
745 )
746 ),
747 )
750class X509_ExtPolicyConstraints(ASN1_Packet):
751 ASN1_codec = ASN1_Codecs.BER
752 ASN1_root = ASN1F_SEQUENCE(
753 ASN1F_optional(ASN1F_INTEGER("requireExplicitPolicy", None, implicit_tag=0x80)),
754 ASN1F_optional(ASN1F_INTEGER("inhibitPolicyMapping", None, implicit_tag=0x81)),
755 )
758class X509_ExtExtendedKeyUsage(ASN1_Packet):
759 ASN1_codec = ASN1_Codecs.BER
760 ASN1_root = ASN1F_SEQUENCE_OF("extendedKeyUsage", [], ASN1P_OID)
762 def get_extendedKeyUsage(self):
763 return [eku.oid for eku in self.extendedKeyUsage]
766class X509_ExtNoticeReference(ASN1_Packet):
767 ASN1_codec = ASN1_Codecs.BER
768 ASN1_root = ASN1F_SEQUENCE(
769 ASN1F_CHOICE(
770 "organization",
771 ASN1_UTF8_STRING("Dummy Organization"),
772 ASN1F_IA5_STRING,
773 ASN1F_ISO646_STRING,
774 ASN1F_BMP_STRING,
775 ASN1F_UTF8_STRING,
776 ),
777 ASN1F_SEQUENCE_OF("noticeNumbers", [], ASN1P_INTEGER),
778 )
781class X509_ExtUserNotice(ASN1_Packet):
782 ASN1_codec = ASN1_Codecs.BER
783 ASN1_root = ASN1F_SEQUENCE(
784 ASN1F_optional(ASN1F_PACKET("noticeRef", None, X509_ExtNoticeReference)),
785 ASN1F_optional(
786 ASN1F_CHOICE(
787 "explicitText",
788 ASN1_UTF8_STRING("Dummy ExplicitText"),
789 ASN1F_IA5_STRING,
790 ASN1F_ISO646_STRING,
791 ASN1F_BMP_STRING,
792 ASN1F_UTF8_STRING,
793 )
794 ),
795 )
798class X509_ExtPolicyQualifierInfo(ASN1_Packet):
799 ASN1_codec = ASN1_Codecs.BER
800 ASN1_root = ASN1F_SEQUENCE(
801 ASN1F_OID("policyQualifierId", "1.3.6.1.5.5.7.2.1"),
802 ASN1F_CHOICE(
803 "qualifier",
804 ASN1_IA5_STRING("cps_str"),
805 ASN1F_IA5_STRING,
806 X509_ExtUserNotice,
807 ),
808 )
811class X509_ExtPolicyInformation(ASN1_Packet):
812 ASN1_codec = ASN1_Codecs.BER
813 ASN1_root = ASN1F_SEQUENCE(
814 ASN1F_OID("policyIdentifier", "2.5.29.32.0"),
815 ASN1F_optional(
816 ASN1F_SEQUENCE_OF("policyQualifiers", None, X509_ExtPolicyQualifierInfo)
817 ),
818 )
821class X509_ExtCertificatePolicies(ASN1_Packet):
822 ASN1_codec = ASN1_Codecs.BER
823 ASN1_root = ASN1F_SEQUENCE_OF(
824 "certificatePolicies", [X509_ExtPolicyInformation()], X509_ExtPolicyInformation
825 )
828class X509_ExtCRLDistributionPoints(ASN1_Packet):
829 ASN1_codec = ASN1_Codecs.BER
830 ASN1_root = ASN1F_SEQUENCE_OF(
831 "cRLDistributionPoints",
832 [X509_ExtDistributionPoint()],
833 X509_ExtDistributionPoint,
834 )
837class X509_ExtInhibitAnyPolicy(ASN1_Packet):
838 ASN1_codec = ASN1_Codecs.BER
839 ASN1_root = ASN1F_INTEGER("skipCerts", 0)
842class X509_ExtFreshestCRL(ASN1_Packet):
843 ASN1_codec = ASN1_Codecs.BER
844 ASN1_root = ASN1F_SEQUENCE_OF(
845 "cRLDistributionPoints",
846 [X509_ExtDistributionPoint()],
847 X509_ExtDistributionPoint,
848 )
851class X509_AccessDescription(ASN1_Packet):
852 ASN1_codec = ASN1_Codecs.BER
853 ASN1_root = ASN1F_SEQUENCE(
854 ASN1F_OID("accessMethod", "0"),
855 ASN1F_PACKET("accessLocation", X509_GeneralName(), X509_GeneralName),
856 )
859class X509_ExtAuthInfoAccess(ASN1_Packet):
860 ASN1_codec = ASN1_Codecs.BER
861 ASN1_root = ASN1F_SEQUENCE_OF(
862 "authorityInfoAccess", [X509_AccessDescription()], X509_AccessDescription
863 )
866class X509_ExtQcStatement(ASN1_Packet):
867 ASN1_codec = ASN1_Codecs.BER
868 ASN1_root = ASN1F_SEQUENCE(
869 ASN1F_OID("statementId", "0.4.0.1862.1.1"),
870 ASN1F_optional(ASN1F_field("statementInfo", None)),
871 )
874class X509_ExtQcStatements(ASN1_Packet):
875 ASN1_codec = ASN1_Codecs.BER
876 ASN1_root = ASN1F_SEQUENCE_OF(
877 "qcStatements", [X509_ExtQcStatement()], X509_ExtQcStatement
878 )
881class X509_ExtSubjInfoAccess(ASN1_Packet):
882 ASN1_codec = ASN1_Codecs.BER
883 ASN1_root = ASN1F_SEQUENCE_OF(
884 "subjectInfoAccess", [X509_AccessDescription()], X509_AccessDescription
885 )
888class X509_ExtNetscapeCertType(ASN1_Packet):
889 ASN1_codec = ASN1_Codecs.BER
890 ASN1_root = ASN1F_BIT_STRING("netscapeCertType", b"")
893class X509_ExtComment(ASN1_Packet):
894 ASN1_codec = ASN1_Codecs.BER
895 ASN1_root = ASN1F_CHOICE(
896 "comment",
897 ASN1_UTF8_STRING("Dummy comment."),
898 ASN1F_IA5_STRING,
899 ASN1F_ISO646_STRING,
900 ASN1F_BMP_STRING,
901 ASN1F_UTF8_STRING,
902 )
905class X509_ExtCertificateTemplateName(ASN1_Packet):
906 ASN1_codec = ASN1_Codecs.BER
907 ASN1_root = ASN1F_BMP_STRING("Name", b"")
910class X509_ExtOidNTDSCaSecurity(ASN1_Packet):
911 ASN1_codec = ASN1_Codecs.BER
912 ASN1_root = ASN1F_X509_otherName()
913 type_id = ASN1_OID("1.3.6.1.4.1.311.25.2.1")
914 value = ASN1_UTF8_STRING("")
917# [MS-WCCE] sect 2.2.2.7.7.2
920class X509_ExtCertificateTemplateOID(ASN1_Packet):
921 ASN1_codec = ASN1_Codecs.BER
922 ASN1_root = ASN1F_SEQUENCE(
923 ASN1F_OID("templateID", "0"),
924 ASN1F_optional(
925 ASN1F_INTEGER("templateMajorVersion", 0),
926 ),
927 ASN1F_optional(
928 ASN1F_INTEGER("templateMinorVersion", 0),
929 ),
930 )
933# oid-info.com shows that some extensions share multiple OIDs.
934# Here we only reproduce those written in RFC5280.
935_ext_mapping = {
936 "2.5.29.9": X509_ExtSubjectDirectoryAttributes,
937 "2.5.29.14": X509_ExtSubjectKeyIdentifier,
938 "2.5.29.15": X509_ExtKeyUsage,
939 "2.5.29.16": X509_ExtPrivateKeyUsagePeriod,
940 "2.5.29.17": X509_ExtSubjectAltName,
941 "2.5.29.18": X509_ExtIssuerAltName,
942 "2.5.29.19": X509_ExtBasicConstraints,
943 "2.5.29.20": X509_ExtCRLNumber,
944 "2.5.29.21": X509_ExtReasonCode,
945 "2.5.29.24": X509_ExtInvalidityDate,
946 "2.5.29.27": X509_ExtDeltaCRLIndicator,
947 "2.5.29.28": X509_ExtIssuingDistributionPoint,
948 "2.5.29.29": X509_ExtCertificateIssuer,
949 "2.5.29.30": X509_ExtNameConstraints,
950 "2.5.29.31": X509_ExtCRLDistributionPoints,
951 "2.5.29.32": X509_ExtCertificatePolicies,
952 "2.5.29.33": X509_ExtPolicyMappings,
953 "2.5.29.35": X509_ExtAuthorityKeyIdentifier,
954 "2.5.29.36": X509_ExtPolicyConstraints,
955 "2.5.29.37": X509_ExtExtendedKeyUsage,
956 "2.5.29.46": X509_ExtFreshestCRL,
957 "2.5.29.54": X509_ExtInhibitAnyPolicy,
958 "2.16.840.1.113730.1.1": X509_ExtNetscapeCertType,
959 "2.16.840.1.113730.1.13": X509_ExtComment,
960 "1.3.6.1.4.1.311.20.2": X509_ExtCertificateTemplateName,
961 "1.3.6.1.4.1.311.21.7": X509_ExtCertificateTemplateOID,
962 "1.3.6.1.4.1.311.21.10": X509_ExtCertificatePolicies,
963 "1.3.6.1.4.1.311.25.2": X509_ExtOidNTDSCaSecurity,
964 "1.3.6.1.5.5.7.1.1": X509_ExtAuthInfoAccess,
965 "1.3.6.1.5.5.7.1.3": X509_ExtQcStatements,
966 "1.3.6.1.5.5.7.1.11": X509_ExtSubjInfoAccess,
967}
970class _X509_ExtField(ASN1F_STRING_PacketField):
971 def m2i(self, pkt, s):
972 val = super(_X509_ExtField, self).m2i(pkt, s)
973 if not val[0].val:
974 return val
975 if pkt.extnID.val in _ext_mapping:
976 return (
977 _ext_mapping[pkt.extnID.val](val[0].val, _underlayer=pkt),
978 val[1],
979 )
980 return val
983class ASN1F_EXT_SEQUENCE(ASN1F_SEQUENCE):
984 def __init__(self, **kargs):
985 seq = [
986 ASN1F_OID("extnID", "2.5.29.19"),
987 ASN1F_optional(ASN1F_BOOLEAN("critical", False)),
988 _X509_ExtField("extnValue", X509_ExtBasicConstraints()),
989 ]
990 ASN1F_SEQUENCE.__init__(self, *seq, **kargs)
993class X509_Extension(ASN1_Packet):
994 ASN1_codec = ASN1_Codecs.BER
995 ASN1_root = ASN1F_EXT_SEQUENCE()
998class X509_Extensions(ASN1_Packet):
999 # we use this in OCSP status requests, in tls/handshake.py
1000 ASN1_codec = ASN1_Codecs.BER
1001 ASN1_root = ASN1F_optional(ASN1F_SEQUENCE_OF("extensions", None, X509_Extension))
1004# Aka 'ExtensionReq' in CMS
1005_X509_ATTRIBUTE_TYPE["1.2.840.113549.1.9.14"] = X509_Extensions
1008# Public key wrapper #
1011class X509_AlgorithmIdentifier(ASN1_Packet):
1012 ASN1_codec = ASN1_Codecs.BER
1013 ASN1_root = ASN1F_SEQUENCE(
1014 ASN1F_OID("algorithm", "1.2.840.113549.1.1.11"),
1015 MultipleTypeField(
1016 [
1017 (
1018 # RFC4055:
1019 # "The correct encoding is to omit the parameters field"
1020 # "All implementations MUST accept both NULL and absent
1021 # parameters as legal and equivalent encodings."
1022 # RFC8017:
1023 # "should generally be omitted, but if present, it shall have a
1024 # value of type NULL."
1025 ASN1F_optional(ASN1F_NULL("parameters", None)),
1026 lambda pkt: (
1027 pkt.algorithm.val[:19] == "1.2.840.113549.1.1."
1028 or pkt.algorithm.val[:21] == "2.16.840.1.101.3.4.2."
1029 or pkt.algorithm.val[:11] == "1.3.14.3.2."
1030 ),
1031 ),
1032 (
1033 # RFC5758:
1034 # "the encoding MUST omit the parameters field"
1035 # RFC8410:
1036 # "For all of the OIDs, the parameters MUST be absent."
1037 # RFC9881:
1038 # "The parameters of these signature algorithms MUST be absent"
1039 ASN1F_omit("parameters", None),
1040 lambda pkt: (
1041 pkt.algorithm.val[:16] == "1.2.840.10045.4."
1042 or pkt.algorithm.val in ["1.3.101.112", "1.3.101.113"]
1043 or pkt.algorithm.val[:21] == "2.16.840.1.101.3.4.3."
1044 ),
1045 ),
1046 # RFC5480
1047 (
1048 ASN1F_PACKET(
1049 "parameters",
1050 ECParameters(),
1051 ECParameters,
1052 ),
1053 lambda pkt: pkt.algorithm.val == "1.2.840.10045.2.1",
1054 ),
1055 # RFC3279
1056 (
1057 ASN1F_PACKET(
1058 "parameters",
1059 DomainParameters(),
1060 DomainParameters,
1061 ),
1062 lambda pkt: pkt.algorithm.val == "1.2.840.10046.2.1",
1063 ),
1064 # PKCS#3
1065 (
1066 ASN1F_PACKET(
1067 "parameters",
1068 DHParameter(),
1069 DHParameter,
1070 ),
1071 lambda pkt: pkt.algorithm.val == "1.2.840.113549.1.3.1",
1072 ),
1073 # TripleDES
1074 (
1075 ASN1F_STRING(
1076 "parameters",
1077 "",
1078 ),
1079 lambda pkt: pkt.algorithm.val == "1.2.840.113549.3.7",
1080 ),
1081 ],
1082 # Default: fail, probably. This is most likely unimplemented.
1083 ASN1F_NULL("parameters", 0),
1084 ),
1085 )
1088class ASN1F_X509_PublicKeyField(MultipleTypeField):
1089 def __init__(self, name, getoid, optional=False, **kwargs):
1090 super(ASN1F_X509_PublicKeyField, self).__init__(
1091 [
1092 (
1093 ASN1F_BIT_STRING_ENCAPS(
1094 name,
1095 None if optional else RSAPublicKey(),
1096 RSAPublicKey,
1097 **kwargs,
1098 ),
1099 lambda pkt: "rsa" in getoid(pkt).lower(),
1100 ),
1101 (
1102 ASN1F_BIT_STRING_ENCAPS(
1103 name,
1104 None if optional else DHPublicKey(),
1105 DHPublicKey,
1106 **kwargs,
1107 ),
1108 lambda pkt: "dhpublicnumber" == getoid(pkt),
1109 ),
1110 (
1111 ASN1F_PACKET(
1112 name,
1113 None if optional else ECDSAPublicKey(),
1114 ECDSAPublicKey,
1115 **kwargs,
1116 ),
1117 lambda pkt: "ecPublicKey" == getoid(pkt),
1118 ),
1119 (
1120 ASN1F_PACKET(
1121 name,
1122 None if optional else EdDSAPublicKey(),
1123 EdDSAPublicKey,
1124 **kwargs,
1125 ),
1126 lambda pkt: getoid(pkt) in ["Ed25519", "Ed448"],
1127 ),
1128 (
1129 ASN1F_PACKET(
1130 name,
1131 None if optional else MLDSAPublicKey(),
1132 MLDSAPublicKey,
1133 **kwargs,
1134 ),
1135 lambda pkt: "ml-dsa" in getoid(pkt),
1136 ),
1137 ],
1138 ASN1F_BIT_STRING(name, b""),
1139 )
1142class X509_SubjectPublicKeyInfo(ASN1_Packet):
1143 ASN1_codec = ASN1_Codecs.BER
1144 ASN1_root = ASN1F_SEQUENCE(
1145 ASN1F_PACKET(
1146 "signatureAlgorithm",
1147 X509_AlgorithmIdentifier(),
1148 X509_AlgorithmIdentifier,
1149 ),
1150 ASN1F_X509_PublicKeyField(
1151 "subjectPublicKey",
1152 getoid=lambda pkt: pkt.signatureAlgorithm.algorithm.oidname,
1153 ),
1154 )
1157# RFC 5958 private key wrapper #
1160class X509_OneAsymmetricKey(ASN1_Packet):
1161 ASN1_codec = ASN1_Codecs.BER
1162 ASN1_root = ASN1F_SEQUENCE(
1163 ASN1F_INTEGER("version", 0),
1164 ASN1F_PACKET(
1165 "privateKeyAlgorithm",
1166 X509_AlgorithmIdentifier(),
1167 X509_AlgorithmIdentifier,
1168 ),
1169 MultipleTypeField(
1170 [
1171 (
1172 ASN1F_STRING_ENCAPS(
1173 "privateKey",
1174 CurvePrivateKey(),
1175 CurvePrivateKey,
1176 ),
1177 lambda pkt: pkt.privateKeyAlgorithm.algorithm.oidname
1178 in ["Ed25519", "Ed448"],
1179 ),
1180 (
1181 ASN1F_STRING_ENCAPS(
1182 "privateKey",
1183 MLDSAPrivateKey(),
1184 MLDSAPrivateKey,
1185 ),
1186 lambda pkt: "ml-dsa" in pkt.privateKeyAlgorithm.algorithm.oidname,
1187 ),
1188 ],
1189 ASN1F_STRING("privateKey", b""),
1190 ),
1191 ASN1F_optional(
1192 ASN1F_X509_PublicKeyField(
1193 "publicKey",
1194 getoid=lambda pkt: pkt.privateKeyAlgorithm.algorithm.oidname,
1195 optional=True,
1196 explicit_tag=0xA1,
1197 )
1198 ),
1199 )
1202# OpenSSL compatibility wrappers #
1205# XXX As ECDSAPrivateKey already uses the structure from RFC 5958,
1206# and as we would prefer encapsulated RSA private keys to be parsed,
1207# this lazy implementation actually supports RSA encoding only.
1208# We'd rather call it RSAPrivateKey_OpenSSL than X509_PrivateKeyInfo.
1209class RSAPrivateKey_OpenSSL(ASN1_Packet):
1210 ASN1_codec = ASN1_Codecs.BER
1211 ASN1_root = ASN1F_SEQUENCE(
1212 ASN1F_enum_INTEGER("version", 0, ["v1", "v2"]),
1213 ASN1F_PACKET(
1214 "privateKeyAlgorithm", X509_AlgorithmIdentifier(), X509_AlgorithmIdentifier
1215 ),
1216 ASN1F_PACKET("privateKey", RSAPrivateKey(), RSAPrivateKey, explicit_tag=0x04),
1217 ASN1F_optional(
1218 ASN1F_PACKET("parameters", None, ECParameters, explicit_tag=0xA0)
1219 ),
1220 ASN1F_optional(
1221 ASN1F_PACKET("publicKey", None, ECDSAPublicKey, explicit_tag=0xA1)
1222 ),
1223 )
1226# We need this hack because ECParameters parsing below must return
1227# a Padding payload, and making the ASN1_Packet class have Padding
1228# instead of Raw payload would break things...
1231class _PacketFieldRaw(PacketField):
1232 def getfield(self, pkt, s):
1233 i = self.m2i(pkt, s)
1234 remain = ""
1235 if conf.raw_layer in i:
1236 r = i[conf.raw_layer]
1237 del r.underlayer.payload
1238 remain = r.load
1239 return remain, i
1242class ECDSAPrivateKey_OpenSSL(Packet):
1243 name = "ECDSA Params + Private Key"
1244 fields_desc = [
1245 _PacketFieldRaw("ecparam", ECParameters(), ECParameters),
1246 PacketField("privateKey", ECDSAPrivateKey(), ECDSAPrivateKey),
1247 ]
1250# TBSCertificate & Certificate #
1252_default_issuer = [
1253 X509_RDN(),
1254 X509_RDN(
1255 rdn=[
1256 X509_AttributeTypeAndValue(
1257 type=ASN1_OID("2.5.4.10"), value=ASN1_PRINTABLE_STRING("Scapy, Inc.")
1258 )
1259 ]
1260 ),
1261 X509_RDN(
1262 rdn=[
1263 X509_AttributeTypeAndValue(
1264 type=ASN1_OID("2.5.4.3"),
1265 value=ASN1_PRINTABLE_STRING("Scapy Default Issuer"),
1266 )
1267 ]
1268 ),
1269]
1271_default_subject = [
1272 X509_RDN(),
1273 X509_RDN(
1274 rdn=[
1275 X509_AttributeTypeAndValue(
1276 type=ASN1_OID("2.5.4.10"), value=ASN1_PRINTABLE_STRING("Scapy, Inc.")
1277 )
1278 ]
1279 ),
1280 X509_RDN(
1281 rdn=[
1282 X509_AttributeTypeAndValue(
1283 type=ASN1_OID("2.5.4.3"),
1284 value=ASN1_PRINTABLE_STRING("Scapy Default Subject"),
1285 )
1286 ]
1287 ),
1288]
1291class _IssuerUtils:
1292 def get_issuer(self):
1293 attrs = self.issuer
1294 attrsDict = {}
1295 for attr in attrs:
1296 # we assume there is only one name in each rdn ASN1_SET
1297 attrsDict[attr.rdn[0].type.oidname] = plain_str(
1298 attr.rdn[0].value.val
1299 )
1300 return attrsDict
1302 def get_issuer_str(self):
1303 """
1304 Returns a one-line string containing every type/value
1305 in a rather specific order. sorted() built-in ensures unicity.
1306 """
1307 name_str = ""
1308 attrsDict = self.get_issuer()
1309 for attrType, attrSymbol in _attrName_mapping:
1310 if attrType in attrsDict:
1311 name_str += "/" + attrSymbol + "="
1312 name_str += attrsDict[attrType]
1313 for attrType in sorted(attrsDict):
1314 if attrType not in _attrName_specials:
1315 name_str += "/" + attrType + "="
1316 name_str += attrsDict[attrType]
1317 return name_str
1320class X509_Validity(ASN1_Packet):
1321 ASN1_codec = ASN1_Codecs.BER
1322 ASN1_root = ASN1F_SEQUENCE(
1323 ASN1F_CHOICE(
1324 "not_before",
1325 ASN1_UTC_TIME(str(ZuluTime(-600))),
1326 ASN1F_UTC_TIME,
1327 ASN1F_GENERALIZED_TIME,
1328 ),
1329 ASN1F_CHOICE(
1330 "not_after",
1331 ASN1_UTC_TIME(str(ZuluTime(+86400))),
1332 ASN1F_UTC_TIME,
1333 ASN1F_GENERALIZED_TIME,
1334 ),
1335 )
1338_attrName_mapping = [
1339 ("countryName", "C"),
1340 ("stateOrProvinceName", "ST"),
1341 ("localityName", "L"),
1342 ("organizationName", "O"),
1343 ("organizationUnitName", "OU"),
1344 ("commonName", "CN"),
1345]
1346_attrName_specials = [name for name, symbol in _attrName_mapping]
1349class X509_TBSCertificate(ASN1_Packet, _IssuerUtils):
1350 ASN1_codec = ASN1_Codecs.BER
1351 ASN1_root = ASN1F_SEQUENCE(
1352 ASN1F_optional(
1353 ASN1F_enum_INTEGER("version", 0x2, ["v1", "v2", "v3"], explicit_tag=0xA0)
1354 ),
1355 ASN1F_INTEGER("serialNumber", 1),
1356 ASN1F_PACKET("signature", X509_AlgorithmIdentifier(), X509_AlgorithmIdentifier),
1357 ASN1F_SEQUENCE_OF("issuer", _default_issuer, X509_RDN),
1358 ASN1F_PACKET("validity", X509_Validity(), X509_Validity),
1359 ASN1F_SEQUENCE_OF("subject", _default_subject, X509_RDN),
1360 ASN1F_PACKET(
1361 "subjectPublicKeyInfo",
1362 X509_SubjectPublicKeyInfo(),
1363 X509_SubjectPublicKeyInfo,
1364 ),
1365 ASN1F_optional(ASN1F_BIT_STRING("issuerUniqueID", None, implicit_tag=0x81)),
1366 ASN1F_optional(ASN1F_BIT_STRING("subjectUniqueID", None, implicit_tag=0x82)),
1367 ASN1F_optional(
1368 ASN1F_SEQUENCE_OF(
1369 "extensions", [X509_Extension()], X509_Extension, explicit_tag=0xA3
1370 )
1371 ),
1372 )
1374 def get_subject(self):
1375 attrs = self.subject
1376 attrsDict = {}
1377 for attr in attrs:
1378 # we assume there is only one name in each rdn ASN1_SET
1379 attrsDict[attr.rdn[0].type.oidname] = plain_str(
1380 attr.rdn[0].value.val
1381 )
1382 return attrsDict
1384 def get_subject_str(self):
1385 name_str = ""
1386 attrsDict = self.get_subject()
1387 for attrType, attrSymbol in _attrName_mapping:
1388 if attrType in attrsDict:
1389 name_str += "/" + attrSymbol + "="
1390 name_str += attrsDict[attrType]
1391 for attrType in sorted(attrsDict):
1392 if attrType not in _attrName_specials:
1393 name_str += "/" + attrType + "="
1394 name_str += attrsDict[attrType]
1395 return name_str
1398class ASN1F_X509_Cert(ASN1F_SEQUENCE):
1399 def __init__(self, **kargs):
1400 seq = [
1401 ASN1F_PACKET("tbsCertificate", X509_TBSCertificate(), X509_TBSCertificate),
1402 ASN1F_PACKET(
1403 "signatureAlgorithm",
1404 X509_AlgorithmIdentifier(),
1405 X509_AlgorithmIdentifier,
1406 ),
1407 MultipleTypeField(
1408 [
1409 (
1410 ASN1F_BIT_STRING_ENCAPS(
1411 "signatureValue", ECDSASignature(), ECDSASignature
1412 ),
1413 lambda pkt: "ecdsa"
1414 in pkt.signatureAlgorithm.algorithm.oidname.lower(),
1415 ),
1416 ],
1417 ASN1F_BIT_STRING("signatureValue", b"defaultsignature" * 2),
1418 ),
1419 ]
1420 ASN1F_SEQUENCE.__init__(self, *seq, **kargs)
1423class X509_Cert(ASN1_Packet):
1424 ASN1_codec = ASN1_Codecs.BER
1425 ASN1_root = ASN1F_X509_Cert()
1428# TBSCertList & CRL #
1431class X509_RevokedCertificate(ASN1_Packet):
1432 ASN1_codec = ASN1_Codecs.BER
1433 ASN1_root = ASN1F_SEQUENCE(
1434 ASN1F_INTEGER("serialNumber", 1),
1435 ASN1F_UTC_TIME("revocationDate", str(ZuluTime(+86400))),
1436 ASN1F_optional(ASN1F_SEQUENCE_OF("crlEntryExtensions", None, X509_Extension)),
1437 )
1440class X509_TBSCertList(ASN1_Packet, _IssuerUtils):
1441 ASN1_codec = ASN1_Codecs.BER
1442 ASN1_root = ASN1F_SEQUENCE(
1443 ASN1F_optional(ASN1F_enum_INTEGER("version", 1, ["v1", "v2"])),
1444 ASN1F_PACKET("signature", X509_AlgorithmIdentifier(), X509_AlgorithmIdentifier),
1445 ASN1F_SEQUENCE_OF("issuer", _default_issuer, X509_RDN),
1446 ASN1F_UTC_TIME("this_update", str(ZuluTime(-1))),
1447 ASN1F_optional(ASN1F_UTC_TIME("next_update", None)),
1448 ASN1F_optional(
1449 ASN1F_SEQUENCE_OF("revokedCertificates", None, X509_RevokedCertificate)
1450 ),
1451 ASN1F_optional(
1452 ASN1F_SEQUENCE_OF("crlExtensions", None, X509_Extension, explicit_tag=0xA0)
1453 ),
1454 )
1457class ASN1F_X509_CRL(ASN1F_SEQUENCE):
1458 def __init__(self, **kargs):
1459 seq = [
1460 ASN1F_PACKET("tbsCertList", X509_TBSCertList(), X509_TBSCertList),
1461 ASN1F_PACKET(
1462 "signatureAlgorithm",
1463 X509_AlgorithmIdentifier(),
1464 X509_AlgorithmIdentifier,
1465 ),
1466 MultipleTypeField(
1467 [
1468 (
1469 ASN1F_BIT_STRING_ENCAPS(
1470 "signatureValue", ECDSASignature(), ECDSASignature
1471 ),
1472 lambda pkt: "ecdsa"
1473 in pkt.signatureAlgorithm.algorithm.oidname.lower(),
1474 ),
1475 ],
1476 ASN1F_BIT_STRING("signatureValue", b"defaultsignature" * 2),
1477 ),
1478 ]
1479 ASN1F_SEQUENCE.__init__(self, *seq, **kargs)
1482class X509_CRL(ASN1_Packet):
1483 ASN1_codec = ASN1_Codecs.BER
1484 ASN1_root = ASN1F_X509_CRL()
1487#####################
1488# CMS packets #
1489#####################
1490# based on RFC 3852
1492CMSVersion = ASN1F_INTEGER
1494# RFC3852 sect 5.2
1496# Other layers should store the structures that can be encapsulated
1497# by CMS here, referred by their OIDs.
1498_CMS_ENCAPSULATED = {}
1501class _EncapsulatedContent_Field(ASN1F_STRING_PacketField):
1502 def m2i(self, pkt, s):
1503 val = super(_EncapsulatedContent_Field, self).m2i(pkt, s)
1504 if not val[0].val:
1505 return val
1507 # Get encapsulated value from its type
1508 if pkt.eContentType.val in _CMS_ENCAPSULATED:
1509 return (
1510 _CMS_ENCAPSULATED[pkt.eContentType.val](val[0].val, _underlayer=pkt),
1511 val[1],
1512 )
1514 return val
1517class CMS_EncapsulatedContentInfo(ASN1_Packet):
1518 ASN1_codec = ASN1_Codecs.BER
1519 ASN1_root = ASN1F_SEQUENCE(
1520 ASN1F_OID("eContentType", "0"),
1521 ASN1F_optional(
1522 _EncapsulatedContent_Field("eContent", None, explicit_tag=0xA0),
1523 ),
1524 )
1527# RFC3852 sect 10.2.1
1530class CMS_RevocationInfoChoice(ASN1_Packet):
1531 ASN1_codec = ASN1_Codecs.BER
1532 ASN1_root = ASN1F_CHOICE(
1533 "crl",
1534 None,
1535 ASN1F_PACKET("crl", X509_CRL(), X509_Cert),
1536 # -- TODO: 1
1537 )
1540# RFC3852 sect 10.2.2
1543class CMS_CertificateChoices(ASN1_Packet):
1544 ASN1_codec = ASN1_Codecs.BER
1545 ASN1_root = ASN1F_CHOICE(
1546 "certificate",
1547 None,
1548 ASN1F_PACKET("certificate", X509_Cert(), X509_Cert),
1549 # -- TODO: 0, 1, 2
1550 )
1553# RFC3852 sect 10.2.4
1556class CMS_IssuerAndSerialNumber(ASN1_Packet, _IssuerUtils):
1557 ASN1_codec = ASN1_Codecs.BER
1558 ASN1_root = ASN1F_SEQUENCE(
1559 ASN1F_SEQUENCE_OF("issuer", _default_issuer, X509_RDN),
1560 ASN1F_INTEGER("serialNumber", 0),
1561 )
1564# RFC3852 sect 10.2.7
1567class CMS_OtherKeyAttribute(ASN1_Packet):
1568 ASN1_codec = ASN1_Codecs.BER
1569 ASN1_root = ASN1F_SEQUENCE(
1570 ASN1F_OID("keyAttrId", "0"),
1571 ASN1F_field("keyAttr", 0),
1572 )
1575# RFC3852 sect 5.3
1578class CMS_SubjectKeyIdentifier(ASN1_Packet):
1579 ASN1_codec = ASN1_Codecs.BER
1580 ASN1_root = ASN1F_STRING("sid", "")
1583class CMS_SignerInfo(ASN1_Packet):
1584 ASN1_codec = ASN1_Codecs.BER
1585 ASN1_root = ASN1F_SEQUENCE(
1586 CMSVersion("version", 1),
1587 ASN1F_CHOICE(
1588 "sid",
1589 CMS_IssuerAndSerialNumber(),
1590 ASN1F_PACKET("sid", CMS_IssuerAndSerialNumber(), CMS_IssuerAndSerialNumber),
1591 ASN1F_PACKET(
1592 "sid",
1593 CMS_SubjectKeyIdentifier(),
1594 CMS_SubjectKeyIdentifier,
1595 implicit_tag=0x80,
1596 ),
1597 ),
1598 ASN1F_PACKET(
1599 "digestAlgorithm", X509_AlgorithmIdentifier(), X509_AlgorithmIdentifier
1600 ),
1601 ASN1F_optional(
1602 ASN1F_SET_OF(
1603 "signedAttrs",
1604 None,
1605 X509_Attribute,
1606 implicit_tag=0xA0,
1607 )
1608 ),
1609 ASN1F_PACKET(
1610 "signatureAlgorithm", X509_AlgorithmIdentifier(), X509_AlgorithmIdentifier
1611 ),
1612 ASN1F_STRING("signature", ASN1_UTF8_STRING("")),
1613 ASN1F_optional(
1614 ASN1F_SET_OF(
1615 "unsignedAttrs",
1616 None,
1617 X509_Attribute,
1618 implicit_tag=0xA1,
1619 )
1620 ),
1621 )
1624# RFC3852 sect 5.4
1627class CMS_SignedAttrsForSignature(ASN1_Packet):
1628 ASN1_codec = ASN1_Codecs.BER
1629 ASN1_root = ASN1F_SET_OF(
1630 "signedAttrs",
1631 None,
1632 X509_Attribute,
1633 )
1636# RFC3852 sect 5.1
1639class CMS_SignedData(ASN1_Packet):
1640 ASN1_codec = ASN1_Codecs.BER
1641 ASN1_root = ASN1F_SEQUENCE(
1642 CMSVersion("version", 1),
1643 ASN1F_SET_OF("digestAlgorithms", [], X509_AlgorithmIdentifier),
1644 ASN1F_PACKET(
1645 "encapContentInfo",
1646 CMS_EncapsulatedContentInfo(),
1647 CMS_EncapsulatedContentInfo,
1648 ),
1649 ASN1F_optional(
1650 ASN1F_SET_OF(
1651 "certificates",
1652 None,
1653 CMS_CertificateChoices,
1654 implicit_tag=0xA0,
1655 )
1656 ),
1657 ASN1F_optional(
1658 ASN1F_SET_OF(
1659 "crls",
1660 None,
1661 CMS_RevocationInfoChoice,
1662 implicit_tag=0xA1,
1663 )
1664 ),
1665 ASN1F_SET_OF(
1666 "signerInfos",
1667 [],
1668 CMS_SignerInfo,
1669 ),
1670 )
1673# RFC3852 sect 6.2.1
1676class CMS_KeyTransRecipientInfo(ASN1_Packet):
1677 ASN1_codec = ASN1_Codecs.BER
1678 ASN1_root = ASN1F_SEQUENCE(
1679 CMSVersion("version", 0),
1680 ASN1F_CHOICE(
1681 "rid",
1682 CMS_IssuerAndSerialNumber(),
1683 ASN1F_PACKET("rid", CMS_IssuerAndSerialNumber(), CMS_IssuerAndSerialNumber),
1684 ASN1F_PACKET(
1685 "rid",
1686 CMS_SubjectKeyIdentifier(),
1687 CMS_SubjectKeyIdentifier,
1688 implicit_tag=0x80,
1689 ),
1690 ),
1691 ASN1F_PACKET(
1692 "keyEncryptionAlgorithm",
1693 X509_AlgorithmIdentifier(),
1694 X509_AlgorithmIdentifier,
1695 ),
1696 ASN1F_STRING("encryptedKey", ""),
1697 )
1700# RFC3852 sect 6.2.2
1703class CMS_OriginatorPublicKey(ASN1_Packet):
1704 ASN1_codec = ASN1_Codecs.BER
1705 ASN1_root = ASN1F_SEQUENCE(
1706 ASN1F_PACKET("algorithm", X509_AlgorithmIdentifier(), X509_AlgorithmIdentifier),
1707 ASN1F_BIT_STRING("publicKey", b""),
1708 )
1711class CMS_OriginatorIdentifierOrKey(ASN1_Packet):
1712 ASN1_codec = ASN1_Codecs.BER
1713 ASN1_root = ASN1F_CHOICE(
1714 "originator",
1715 CMS_IssuerAndSerialNumber(),
1716 ASN1F_PACKET(
1717 "issuerAndSerialNumber",
1718 CMS_IssuerAndSerialNumber(),
1719 CMS_IssuerAndSerialNumber,
1720 ),
1721 ASN1F_PACKET(
1722 "subjectKeyIdentifier",
1723 CMS_SubjectKeyIdentifier(),
1724 CMS_SubjectKeyIdentifier,
1725 implicit_tag=0x80,
1726 ),
1727 ASN1F_PACKET(
1728 "originatorKey",
1729 CMS_OriginatorPublicKey(),
1730 CMS_OriginatorPublicKey,
1731 implicit_tag=0xA1,
1732 ),
1733 )
1736class CMS_RecipientEncryptedKey(ASN1_Packet):
1737 ASN1_codec = ASN1_Codecs.BER
1738 ASN1_root = ASN1F_SEQUENCE(
1739 ASN1F_PACKET(
1740 "subjectKeyIdentifier", CMS_SubjectKeyIdentifier(), CMS_SubjectKeyIdentifier
1741 ),
1742 ASN1F_optional(
1743 ASN1F_GENERALIZED_TIME("date", ""),
1744 ),
1745 ASN1F_optional(
1746 ASN1F_PACKET("other", CMS_OtherKeyAttribute(), CMS_OtherKeyAttribute),
1747 ),
1748 )
1751class CMS_KeyAgreeRecipientInfo(ASN1_Packet):
1752 ASN1_codec = ASN1_Codecs.BER
1753 ASN1_root = ASN1F_SEQUENCE(
1754 CMSVersion("version", 3),
1755 ASN1F_PACKET(
1756 "originator",
1757 CMS_OriginatorIdentifierOrKey(),
1758 CMS_OriginatorIdentifierOrKey,
1759 explicit_tag=0xA0,
1760 ),
1761 ASN1F_optional(
1762 ASN1F_STRING("ukm", None, "", explicit_tag=0x81),
1763 ),
1764 ASN1F_PACKET(
1765 "keyEncryptionAlgorithm",
1766 X509_AlgorithmIdentifier(),
1767 X509_AlgorithmIdentifier,
1768 ),
1769 ASN1F_SEQUENCE_OF("recipientEncryptedKeys", [], CMS_RecipientEncryptedKey),
1770 )
1773# RFC3852 sect 6.2
1776class CMS_RecipientInfo(ASN1_Packet):
1777 ASN1_codec = ASN1_Codecs.BER
1778 ASN1_root = ASN1F_CHOICE(
1779 "recipientInfo",
1780 CMS_KeyTransRecipientInfo(),
1781 ASN1F_PACKET("ktri", CMS_KeyTransRecipientInfo(), CMS_KeyTransRecipientInfo),
1782 ASN1F_PACKET(
1783 "kari",
1784 CMS_KeyAgreeRecipientInfo(),
1785 CMS_KeyAgreeRecipientInfo,
1786 implicit_tag=0xA1,
1787 ),
1788 )
1791# RFC3852 sect 6.1
1794class CMS_OriginatorInfo(ASN1_Packet):
1795 ASN1_codec = ASN1_Codecs.BER
1796 ASN1_root = ASN1F_SEQUENCE(
1797 ASN1F_optional(
1798 ASN1F_SET_OF(
1799 "certs",
1800 None,
1801 CMS_CertificateChoices,
1802 implicit_tag=0xA0,
1803 )
1804 ),
1805 ASN1F_optional(
1806 ASN1F_SET_OF(
1807 "crls",
1808 None,
1809 CMS_RevocationInfoChoice,
1810 implicit_tag=0xA1,
1811 )
1812 ),
1813 )
1816class CMS_EncryptedContentInfo(ASN1_Packet):
1817 ASN1_codec = ASN1_Codecs.BER
1818 ASN1_root = ASN1F_SEQUENCE(
1819 ASN1F_OID("contentType", "1.2.840.113549.1.7.2"),
1820 ASN1F_PACKET(
1821 "contentEncryptionAlgorithm",
1822 X509_AlgorithmIdentifier(),
1823 X509_AlgorithmIdentifier,
1824 ),
1825 ASN1F_optional(
1826 ASN1F_STRING("encryptedContent", "", implicit_tag=0x80),
1827 ),
1828 )
1831class CMS_EnvelopedData(ASN1_Packet):
1832 ASN1_codec = ASN1_Codecs.BER
1833 ASN1_root = ASN1F_SEQUENCE(
1834 CMSVersion("version", 1),
1835 ASN1F_optional(
1836 ASN1F_PACKET("originatorInfo", None, CMS_OriginatorInfo, implicit_tag=0xA0),
1837 ),
1838 ASN1F_SET_OF("recipientInfos", CMS_RecipientInfo(), CMS_RecipientInfo),
1839 ASN1F_PACKET(
1840 "encryptedContentInfo", CMS_EncryptedContentInfo(), CMS_EncryptedContentInfo
1841 ),
1842 ASN1F_optional(
1843 ASN1F_SET_OF("unprotectedAttrs", [], X509_Attribute, implicit_tag=0xA1),
1844 ),
1845 )
1848# RFC3852 sect 3
1851class CMS_ContentInfo(ASN1_Packet):
1852 ASN1_codec = ASN1_Codecs.BER
1853 ASN1_root = ASN1F_SEQUENCE(
1854 ASN1F_OID("contentType", "1.2.840.113549.1.7.2"),
1855 MultipleTypeField(
1856 [
1857 (
1858 ASN1F_PACKET("content", None, CMS_SignedData, explicit_tag=0xA0),
1859 lambda pkt: pkt.contentType.oidname == "id-signedData",
1860 ),
1861 (
1862 ASN1F_PACKET("content", None, CMS_EnvelopedData, explicit_tag=0xA0),
1863 lambda pkt: pkt.contentType.oidname == "id-envelopedData",
1864 ),
1865 ],
1866 ASN1F_BIT_STRING("content", b"", explicit_tag=0xA0),
1867 ),
1868 )
1871#####################
1872# CSR packets #
1873#####################
1875# based on PKCS#10 #
1878class PKCS10_CertificationRequestInfo(ASN1_Packet):
1879 ASN1_codec = ASN1_Codecs.BER
1880 ASN1_root = ASN1F_SEQUENCE(
1881 ASN1F_INTEGER("version", 0),
1882 ASN1F_SEQUENCE_OF("subject", _default_subject, X509_RDN),
1883 ASN1F_PACKET(
1884 "subjectPublicKeyInfo",
1885 X509_SubjectPublicKeyInfo(),
1886 X509_SubjectPublicKeyInfo,
1887 ),
1888 ASN1F_SET_OF("attributes", [], X509_Attribute, implicit_tag=0xA0),
1889 )
1891 get_subject = X509_TBSCertificate.get_subject
1892 get_subject_str = X509_TBSCertificate.get_subject_str
1895class PKCS10_CertificationRequest(ASN1_Packet):
1896 ASN1_codec = ASN1_Codecs.BER
1897 ASN1_root = ASN1F_SEQUENCE(
1898 ASN1F_PACKET(
1899 "certificationRequestInfo",
1900 PKCS10_CertificationRequestInfo(),
1901 PKCS10_CertificationRequestInfo,
1902 ),
1903 ASN1F_PACKET(
1904 "signatureAlgorithm", X509_AlgorithmIdentifier(), X509_AlgorithmIdentifier
1905 ),
1906 ASN1F_BIT_STRING("signature", b""),
1907 )
1910# based on CMC #
1912# RFC 5272 sect 3.2.1.1
1915class CMC_TaggedAttribute(ASN1_Packet):
1916 ASN1_codec = ASN1_Codecs.BER
1917 ASN1_root = ASN1F_SEQUENCE(
1918 ASN1F_INTEGER("bodyPartID", 0),
1919 ASN1F_OID("type", "0"), # attrType for compat
1920 ASN1F_SET_OF("attrValues", [], X509_AttributeValue),
1921 )
1924# RFC 5272 sect 3.2.1.2.1
1927class CMC_TaggedCertificationRequest(ASN1_Packet):
1928 ASN1_codec = ASN1_Codecs.BER
1929 ASN1_root = ASN1F_SEQUENCE(
1930 ASN1F_INTEGER("bodyPartID", 0),
1931 ASN1F_PACKET(
1932 "certificationRequest",
1933 PKCS10_CertificationRequest(),
1934 PKCS10_CertificationRequest,
1935 ),
1936 )
1939# RFC 5272 sect 3.2.1.2
1942class CMC_TaggedRequest(ASN1_Packet):
1943 ASN1_codec = ASN1_Codecs.BER
1944 ASN1_root = ASN1F_CHOICE(
1945 "request",
1946 CMC_TaggedCertificationRequest(),
1947 ASN1F_PACKET(
1948 "tcr",
1949 CMC_TaggedCertificationRequest(),
1950 CMC_TaggedCertificationRequest,
1951 implicit_tag=0xA0,
1952 ),
1953 # XXX there are others
1954 )
1957# RFC 5272 sect 3.2.1.3
1960class CMC_TaggedContentInfo(ASN1_Packet):
1961 ASN1_codec = ASN1_Codecs.BER
1962 ASN1_root = ASN1F_SEQUENCE(
1963 ASN1F_INTEGER("bodyPartID", 0),
1964 ASN1F_PACKET("contentInfo", CMS_ContentInfo(), CMS_ContentInfo),
1965 )
1968# RFC 5272 sect 3.2.1.4
1971class CMC_OtherMsg(ASN1_Packet):
1972 ASN1_codec = ASN1_Codecs.BER
1973 ASN1_root = ASN1F_SEQUENCE(
1974 ASN1F_INTEGER("bodyPartID", 0),
1975 ASN1F_OID("otherMsgType", "0"),
1976 ASN1F_field("otherMsgValue", ""),
1977 )
1980# RFC 5272 sect 3.2.1
1983class CMC_PKIData(ASN1_Packet):
1984 ASN1_codec = ASN1_Codecs.BER
1985 ASN1_root = ASN1F_SEQUENCE(
1986 ASN1F_SEQUENCE_OF("controlSequence", [], CMC_TaggedAttribute),
1987 ASN1F_SEQUENCE_OF("reqSequence", [], CMC_TaggedRequest),
1988 ASN1F_SEQUENCE_OF("cmsSequence", [], CMC_TaggedContentInfo),
1989 ASN1F_SEQUENCE_OF("otherMsgSequence", [], CMC_OtherMsg),
1990 )
1993_CMS_ENCAPSULATED["1.3.6.1.5.5.7.12.2"] = CMC_PKIData
1996# Windows extensions #
1998# https://learn.microsoft.com/en-us/windows/win32/seccertenroll/cmc-extensions
2001class CMC_AddExtensions(ASN1_Packet):
2002 ASN1_codec = ASN1_Codecs.BER
2003 ASN1_root = ASN1F_SEQUENCE(
2004 ASN1F_INTEGER("pkiDataReference", 0),
2005 ASN1F_SEQUENCE_OF("certReferences", [], ASN1F_INTEGER),
2006 ASN1F_PACKET("extensions", X509_Extensions(), X509_Extensions),
2007 )
2010_X509_ATTRIBUTE_TYPE["1.3.6.1.5.5.7.7.8"] = CMC_AddExtensions
2013# https://learn.microsoft.com/en-us/windows/win32/seccertenroll/cmc-attributes
2016class CMC_AddAttributes(ASN1_Packet):
2017 ASN1_codec = ASN1_Codecs.BER
2018 ASN1_root = ASN1F_SEQUENCE(
2019 ASN1F_INTEGER("pkiDataReference", 0),
2020 ASN1F_SEQUENCE_OF("certReferences", [], ASN1F_INTEGER),
2021 ASN1F_SET_OF("attributes", X509_Attribute(), X509_Attribute),
2022 )
2025_X509_ATTRIBUTE_TYPE["1.3.6.1.4.1.311.10.10.1"] = CMC_AddAttributes
2028# [MS-WCCE] sect 2.2.2.7.2
2031class CMC_ENROLLMENT_CSP_PROVIDER(ASN1_Packet):
2032 ASN1_codec = ASN1_Codecs.BER
2033 ASN1_root = ASN1F_SEQUENCE(
2034 ASN1F_INTEGER("KeySpec", 0),
2035 ASN1F_BMP_STRING("ProviderName", ""),
2036 ASN1F_BIT_STRING("Signature", b""),
2037 )
2040_X509_ATTRIBUTE_TYPE["1.3.6.1.4.1.311.13.2.2"] = CMC_ENROLLMENT_CSP_PROVIDER
2043# [MS-WCCE] sect 2.2.2.7.4
2046class CMC_REQUEST_CLIENT_INFO(ASN1_Packet):
2047 ASN1_codec = ASN1_Codecs.BER
2048 ASN1_root = ASN1F_SEQUENCE(
2049 ASN1F_INTEGER("clientId", 0),
2050 ASN1F_UTF8_STRING("MachineName", ""),
2051 ASN1F_UTF8_STRING("UserName", ""),
2052 ASN1F_UTF8_STRING("ProcessName", ""),
2053 )
2056_X509_ATTRIBUTE_TYPE["1.3.6.1.4.1.311.21.20"] = CMC_REQUEST_CLIENT_INFO
2059# [MS-WCCE] sect 2.2.2.7.10
2062class CMC_EnrollmentNameValuePair(ASN1_Packet):
2063 ASN1_codec = ASN1_Codecs.BER
2064 ASN1_root = ASN1F_SEQUENCE(
2065 ASN1F_BMP_STRING("Name", ""),
2066 ASN1F_BMP_STRING("Value", ""),
2067 )
2070_X509_ATTRIBUTE_TYPE["1.3.6.1.4.1.311.13.2.1"] = CMC_EnrollmentNameValuePair
2073# [MS-WCCE] sect 2.2.2.7.12
2076class CMC_ENROLL_ATTESTATION_STATEMENT(ASN1_Packet):
2077 ASN1_codec = ASN1_Codecs.BER
2078 ASN1_root = ASN1F_STRING_ENCAPS(
2079 "kas", KeyAttestationStatement(), KeyAttestationStatement
2080 )
2083_X509_ATTRIBUTE_TYPE["1.3.6.1.4.1.311.21.24"] = CMC_ENROLL_ATTESTATION_STATEMENT
2086# [MS-WCCE] sect 2.2.2.7.13
2088_X509_ATTRIBUTE_TYPE["1.3.6.1.4.1.311.21.23"] = CMS_ContentInfo
2091#############################
2092# OCSP Status packets #
2093#############################
2094# based on RFC 6960
2097class OCSP_CertID(ASN1_Packet):
2098 ASN1_codec = ASN1_Codecs.BER
2099 ASN1_root = ASN1F_SEQUENCE(
2100 ASN1F_PACKET(
2101 "hashAlgorithm", X509_AlgorithmIdentifier(), X509_AlgorithmIdentifier
2102 ),
2103 ASN1F_STRING("issuerNameHash", ""),
2104 ASN1F_STRING("issuerKeyHash", ""),
2105 ASN1F_INTEGER("serialNumber", 0),
2106 )
2109class OCSP_GoodInfo(ASN1_Packet):
2110 ASN1_codec = ASN1_Codecs.BER
2111 ASN1_root = ASN1F_NULL("info", 0)
2114class OCSP_RevokedInfo(ASN1_Packet):
2115 ASN1_codec = ASN1_Codecs.BER
2116 ASN1_root = ASN1F_SEQUENCE(
2117 ASN1F_GENERALIZED_TIME("revocationTime", ""),
2118 ASN1F_optional(
2119 ASN1F_PACKET(
2120 "revocationReason", None, X509_ExtReasonCode, explicit_tag=0xA0
2121 )
2122 ),
2123 )
2126class OCSP_UnknownInfo(ASN1_Packet):
2127 ASN1_codec = ASN1_Codecs.BER
2128 ASN1_root = ASN1F_NULL("info", 0)
2131class OCSP_CertStatus(ASN1_Packet):
2132 ASN1_codec = ASN1_Codecs.BER
2133 ASN1_root = ASN1F_CHOICE(
2134 "certStatus",
2135 None,
2136 ASN1F_PACKET("good", OCSP_GoodInfo(), OCSP_GoodInfo, implicit_tag=0x80),
2137 ASN1F_PACKET(
2138 "revoked", OCSP_RevokedInfo(), OCSP_RevokedInfo, implicit_tag=0xA1
2139 ),
2140 ASN1F_PACKET(
2141 "unknown", OCSP_UnknownInfo(), OCSP_UnknownInfo, implicit_tag=0x82
2142 ),
2143 )
2146class OCSP_SingleResponse(ASN1_Packet):
2147 ASN1_codec = ASN1_Codecs.BER
2148 ASN1_root = ASN1F_SEQUENCE(
2149 ASN1F_PACKET("certID", OCSP_CertID(), OCSP_CertID),
2150 ASN1F_PACKET(
2151 "certStatus", OCSP_CertStatus(certStatus=OCSP_GoodInfo()), OCSP_CertStatus
2152 ),
2153 ASN1F_GENERALIZED_TIME("thisUpdate", ""),
2154 ASN1F_optional(ASN1F_GENERALIZED_TIME("nextUpdate", "", explicit_tag=0xA0)),
2155 ASN1F_optional(
2156 ASN1F_SEQUENCE_OF(
2157 "singleExtensions", None, X509_Extension, explicit_tag=0xA1
2158 )
2159 ),
2160 )
2163class OCSP_ByName(ASN1_Packet):
2164 ASN1_codec = ASN1_Codecs.BER
2165 ASN1_root = ASN1F_SEQUENCE_OF("byName", [], X509_RDN)
2168class OCSP_ByKey(ASN1_Packet):
2169 ASN1_codec = ASN1_Codecs.BER
2170 ASN1_root = ASN1F_STRING("byKey", "")
2173class OCSP_ResponderID(ASN1_Packet):
2174 ASN1_codec = ASN1_Codecs.BER
2175 ASN1_root = ASN1F_CHOICE(
2176 "responderID",
2177 None,
2178 ASN1F_PACKET("byName", OCSP_ByName(), OCSP_ByName, explicit_tag=0xA1),
2179 ASN1F_PACKET("byKey", OCSP_ByKey(), OCSP_ByKey, explicit_tag=0xA2),
2180 )
2183class OCSP_ResponseData(ASN1_Packet):
2184 ASN1_codec = ASN1_Codecs.BER
2185 ASN1_root = ASN1F_SEQUENCE(
2186 ASN1F_optional(ASN1F_enum_INTEGER("version", 0, {0: "v1"}, explicit_tag=0x80)),
2187 ASN1F_PACKET(
2188 "responderID", OCSP_ResponderID(responderID=OCSP_ByName()), OCSP_ResponderID
2189 ),
2190 ASN1F_GENERALIZED_TIME("producedAt", str(GeneralizedTime())),
2191 ASN1F_SEQUENCE_OF("responses", [], OCSP_SingleResponse),
2192 ASN1F_optional(
2193 ASN1F_SEQUENCE_OF(
2194 "responseExtensions", None, X509_Extension, explicit_tag=0xA1
2195 )
2196 ),
2197 )
2200class ASN1F_OCSP_BasicResponse(ASN1F_SEQUENCE):
2201 def __init__(self, **kargs):
2202 seq = [
2203 ASN1F_PACKET("tbsResponseData", OCSP_ResponseData(), OCSP_ResponseData),
2204 ASN1F_PACKET(
2205 "signatureAlgorithm",
2206 X509_AlgorithmIdentifier(),
2207 X509_AlgorithmIdentifier,
2208 ),
2209 MultipleTypeField(
2210 [
2211 (
2212 ASN1F_BIT_STRING_ENCAPS(
2213 "signature", ECDSASignature(), ECDSASignature
2214 ),
2215 lambda pkt: "ecdsa"
2216 in pkt.signatureAlgorithm.algorithm.oidname.lower(),
2217 ),
2218 ],
2219 ASN1F_BIT_STRING("signature", b"defaultsignature" * 2),
2220 ),
2221 ASN1F_optional(
2222 ASN1F_SEQUENCE_OF("certs", None, X509_Cert, explicit_tag=0xA0)
2223 ),
2224 ]
2225 ASN1F_SEQUENCE.__init__(self, *seq, **kargs)
2228class OCSP_ResponseBytes(ASN1_Packet):
2229 ASN1_codec = ASN1_Codecs.BER
2230 ASN1_root = ASN1F_SEQUENCE(
2231 ASN1F_OID("responseType", "1.3.6.1.5.5.7.48.1.1"),
2232 ASN1F_OCSP_BasicResponse(explicit_tag=0x04),
2233 )
2236_responseStatus_mapping = [
2237 "successful",
2238 "malformedRequest",
2239 "internalError",
2240 "tryLater",
2241 "notUsed",
2242 "sigRequired",
2243 "unauthorized",
2244]
2247class OCSP_Response(ASN1_Packet):
2248 ASN1_codec = ASN1_Codecs.BER
2249 ASN1_root = ASN1F_SEQUENCE(
2250 ASN1F_ENUMERATED("responseStatus", 0, _responseStatus_mapping),
2251 ASN1F_optional(
2252 ASN1F_PACKET("responseBytes", None, OCSP_ResponseBytes, explicit_tag=0xA0)
2253 ),
2254 )