1f7d24e3fSHanoh Haim## This file is part of Scapy
2f7d24e3fSHanoh Haim## See http://www.secdev.org/projects/scapy for more informations
3f7d24e3fSHanoh Haim## Copyright (C) Philippe Biondi <phil@secdev.org>
4f7d24e3fSHanoh Haim## This program is published under a GPLv2 license
5f7d24e3fSHanoh Haim
6f7d24e3fSHanoh Haim"""
7f7d24e3fSHanoh HaimX.509 certificates.
8f7d24e3fSHanoh Haim"""
9f7d24e3fSHanoh Haim
10f7d24e3fSHanoh Haimfrom scapy.asn1packet import *
11f7d24e3fSHanoh Haimfrom scapy.asn1fields import *
12f7d24e3fSHanoh Haim
13f7d24e3fSHanoh Haim##########
14f7d24e3fSHanoh Haim## X509 ##
15f7d24e3fSHanoh Haim##########
16f7d24e3fSHanoh Haim
17f7d24e3fSHanoh Haim######[ ASN1 class ]######
18f7d24e3fSHanoh Haim
19f7d24e3fSHanoh Haimclass ASN1_Class_X509(ASN1_Class_UNIVERSAL):
20f7d24e3fSHanoh Haim    name="X509"
21f7d24e3fSHanoh Haim    CONT0 = 0xa0
22f7d24e3fSHanoh Haim    CONT1 = 0xa1
23f7d24e3fSHanoh Haim    CONT2 = 0xa2
24f7d24e3fSHanoh Haim    CONT3 = 0xa3
25f7d24e3fSHanoh Haim
26f7d24e3fSHanoh Haimclass ASN1_X509_CONT0(ASN1_SEQUENCE):
27f7d24e3fSHanoh Haim    tag = ASN1_Class_X509.CONT0
28f7d24e3fSHanoh Haim
29f7d24e3fSHanoh Haimclass ASN1_X509_CONT1(ASN1_SEQUENCE):
30f7d24e3fSHanoh Haim    tag = ASN1_Class_X509.CONT1
31f7d24e3fSHanoh Haim
32f7d24e3fSHanoh Haimclass ASN1_X509_CONT2(ASN1_SEQUENCE):
33f7d24e3fSHanoh Haim    tag = ASN1_Class_X509.CONT2
34f7d24e3fSHanoh Haim
35f7d24e3fSHanoh Haimclass ASN1_X509_CONT3(ASN1_SEQUENCE):
36f7d24e3fSHanoh Haim    tag = ASN1_Class_X509.CONT3
37f7d24e3fSHanoh Haim
38f7d24e3fSHanoh Haim######[ BER codecs ]#######
39f7d24e3fSHanoh Haim
40f7d24e3fSHanoh Haimclass BERcodec_X509_CONT0(BERcodec_SEQUENCE):
41f7d24e3fSHanoh Haim    tag = ASN1_Class_X509.CONT0
42f7d24e3fSHanoh Haim
43f7d24e3fSHanoh Haimclass BERcodec_X509_CONT1(BERcodec_SEQUENCE):
44f7d24e3fSHanoh Haim    tag = ASN1_Class_X509.CONT1
45f7d24e3fSHanoh Haim
46f7d24e3fSHanoh Haimclass BERcodec_X509_CONT2(BERcodec_SEQUENCE):
47f7d24e3fSHanoh Haim    tag = ASN1_Class_X509.CONT2
48f7d24e3fSHanoh Haim
49f7d24e3fSHanoh Haimclass BERcodec_X509_CONT3(BERcodec_SEQUENCE):
50f7d24e3fSHanoh Haim    tag = ASN1_Class_X509.CONT3
51f7d24e3fSHanoh Haim
52f7d24e3fSHanoh Haim######[ ASN1 fields ]######
53f7d24e3fSHanoh Haim
54f7d24e3fSHanoh Haimclass ASN1F_X509_CONT0(ASN1F_SEQUENCE):
55f7d24e3fSHanoh Haim    ASN1_tag = ASN1_Class_X509.CONT0
56f7d24e3fSHanoh Haim
57f7d24e3fSHanoh Haimclass ASN1F_X509_CONT1(ASN1F_SEQUENCE):
58f7d24e3fSHanoh Haim    ASN1_tag = ASN1_Class_X509.CONT1
59f7d24e3fSHanoh Haim
60f7d24e3fSHanoh Haimclass ASN1F_X509_CONT2(ASN1F_SEQUENCE):
61f7d24e3fSHanoh Haim    ASN1_tag = ASN1_Class_X509.CONT2
62f7d24e3fSHanoh Haim
63f7d24e3fSHanoh Haimclass ASN1F_X509_CONT3(ASN1F_SEQUENCE):
64f7d24e3fSHanoh Haim    ASN1_tag = ASN1_Class_X509.CONT3
65f7d24e3fSHanoh Haim
66f7d24e3fSHanoh Haim######[ X509 packets ]######
67f7d24e3fSHanoh Haim
68f7d24e3fSHanoh Haimclass X509RDN(ASN1_Packet):
69f7d24e3fSHanoh Haim    ASN1_codec = ASN1_Codecs.BER
70f7d24e3fSHanoh Haim    ASN1_root = ASN1F_SET(
71f7d24e3fSHanoh Haim                  ASN1F_SEQUENCE( ASN1F_OID("oid","2.5.4.6"),
72f7d24e3fSHanoh Haim                                  ASN1F_PRINTABLE_STRING("value","")
73f7d24e3fSHanoh Haim                                  )
74f7d24e3fSHanoh Haim                  )
75f7d24e3fSHanoh Haim
76f7d24e3fSHanoh Haimclass X509v3Ext(ASN1_Packet):
77f7d24e3fSHanoh Haim    ASN1_codec = ASN1_Codecs.BER
78f7d24e3fSHanoh Haim    ASN1_root = ASN1F_field("val",ASN1_NULL(0))
79f7d24e3fSHanoh Haim
80f7d24e3fSHanoh Haim
81f7d24e3fSHanoh Haimclass X509Cert(ASN1_Packet):
82f7d24e3fSHanoh Haim    ASN1_codec = ASN1_Codecs.BER
83f7d24e3fSHanoh Haim    ASN1_root = ASN1F_SEQUENCE(
84f7d24e3fSHanoh Haim        ASN1F_SEQUENCE(
85f7d24e3fSHanoh Haim            ASN1F_optionnal(ASN1F_X509_CONT0(ASN1F_INTEGER("version",3))),
86f7d24e3fSHanoh Haim            ASN1F_INTEGER("sn",1),
87f7d24e3fSHanoh Haim            ASN1F_SEQUENCE(ASN1F_OID("sign_algo","1.2.840.113549.1.1.5"),
88f7d24e3fSHanoh Haim                           ASN1F_field("sa_value",ASN1_NULL(0))),
89f7d24e3fSHanoh Haim            ASN1F_SEQUENCE_OF("issuer",[],X509RDN),
90f7d24e3fSHanoh Haim            ASN1F_SEQUENCE(ASN1F_UTC_TIME("not_before",ZuluTime(-600)),  # ten minutes ago
91f7d24e3fSHanoh Haim                           ASN1F_UTC_TIME("not_after",ZuluTime(+86400))), # for 24h
92f7d24e3fSHanoh Haim            ASN1F_SEQUENCE_OF("subject",[],X509RDN),
93f7d24e3fSHanoh Haim            ASN1F_SEQUENCE(
94f7d24e3fSHanoh Haim                ASN1F_SEQUENCE(ASN1F_OID("pubkey_algo","1.2.840.113549.1.1.1"),
95f7d24e3fSHanoh Haim                               ASN1F_field("pk_value",ASN1_NULL(0))),
96f7d24e3fSHanoh Haim                ASN1F_BIT_STRING("pubkey","")
97f7d24e3fSHanoh Haim                ),
98f7d24e3fSHanoh Haim            ASN1F_optionnal(ASN1F_X509_CONT3(ASN1F_SEQUENCE_OF("x509v3ext",[],X509v3Ext))),
99f7d24e3fSHanoh Haim
100f7d24e3fSHanoh Haim        ),
101f7d24e3fSHanoh Haim        ASN1F_SEQUENCE(ASN1F_OID("sign_algo2","1.2.840.113549.1.1.5"),
102f7d24e3fSHanoh Haim                       ASN1F_field("sa2_value",ASN1_NULL(0))),
103f7d24e3fSHanoh Haim        ASN1F_BIT_STRING("signature","")
104f7d24e3fSHanoh Haim        )
105f7d24e3fSHanoh Haim
106f7d24e3fSHanoh Haim
107f7d24e3fSHanoh Haim
108f7d24e3fSHanoh Haim
109