bf471dc5360252a88401480a3541eed9eda18cf6
[pyi2ncommon] / src / cnfline / build_key.py
1 # The software in this package is distributed under the GNU General
2 # Public License version 2 (with a special exception described below).
3 #
4 # A copy of GNU General Public License (GPL) is included in this distribution,
5 # in the file COPYING.GPL.
6 #
7 # As a special exception, if other files instantiate templates or use macros
8 # or inline functions from this file, or you compile this file and link it
9 # with other works to produce a work based on this file, this file
10 # does not by itself cause the resulting work to be covered
11 # by the GNU General Public License.
12 #
13 # However the source code for this file must still be made available
14 # in accordance with section (3) of the GNU General Public License.
15 #
16 # This exception does not invalidate any other reasons why a work based
17 # on this file might be covered by the GNU General Public License.
18 #
19 # Copyright (c) 2016-2018 Intra2net AG <info@intra2net.com>
20
21 from .build_cnfvar import BuildCnfVar
22 """Class to create own keys cnfvar on the fly """
23
24
25 class BuildKey(BuildCnfVar):
26     def __init__(self, data='sample key', instance=0, line_no=1):
27         BuildCnfVar.__init__(self, 'KEY_OWN', instance, data, line_no)
28
29         # set some dummy data for cnf_check
30         defaults = {
31             'KEY_OWN_FINGERPRINT_MD5': '76:3B:CF:8E:CB:BF:A5:7D:CC:87:39:FA:CE:99:2E:96',
32             'KEY_OWN_FINGERPRINT_SHA1': 'ED:5A:C6:D9:5B:BE:47:1F:B9:4F:CF:A3:80:3B:42:08:F4:00:16:96',
33             'KEY_OWN_ID_X509': 'CN=some.net.lan',
34             'KEY_OWN_ISSUER': 'CN=ab, C=fd, L=ab, ST=ab, O=ab, OU=ab/emailAddress=ab@ab.com',
35             'KEY_OWN_KEYSIZE': '2048',
36             'KEY_OWN_HASH_ALGO': 'SHA2_256',
37             'KEY_OWN_PRIVATE_KEY': '-----BEGIN PRIVATE KEY-----\\nMIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKk' +
38                                    'ZTzqHXg41RZMiY+ywRZ037pBq8J3BkH\\n-----END PRIVATE KEY-----\\n',
39             'KEY_OWN_PUBLIC_KEY': '-----BEGIN CERTIFICATE-----\\nMIIFCTCCAvGgAwIBAgIEVBGDuTANBgkqhkiG' +
40                                   '9w0BAQsFADCBgTEPMA0GA1UEAwwG\\n-----END CERTIFICATE-----\\n',
41             'KEY_OWN_REQUEST': '-----BEGIN CERTIFICATE REQUEST-----\\nMIIDCzCCAfMCAQAwIjEgMB4GA1UEAww' +
42                                'XaW50cmFkZXYtYWllc\\n-----END CERTIFICATE REQUEST-----\\n',
43             'KEY_OWN_SUBJECT': 'CN=some.net.lan',
44             'KEY_OWN_VALIDFROM': '20140911T111257',
45             'KEY_OWN_VALIDTILL': '20160731T134608',
46             'KEY_OWN_TYPE': 'SELF',
47
48             # the ones bellow should be set when using 'generate' to create the key
49             'KEY_OWN_CREATE_CN': 'somehost',
50             'KEY_OWN_CREATE_EMAIL': 'default@intra2net.com'
51         }
52
53         self.add_defaults(defaults)
54
55     def country(self, country):
56         self.update_cnf('KEY_OWN_CREATE_C', 0, country)
57         return self
58
59     def state(self, state):
60         self.update_cnf('KEY_OWN_CREATE_ST', 0, state)
61         return self
62
63     def city(self, city):
64         self.update_cnf('KEY_OWN_CREATE_L', 0, city)
65         return self
66
67     def company(self, company):
68         self.update_cnf('KEY_OWN_CREATE_O', 0, company)
69         return self
70
71     def department(self, department):
72         self.update_cnf('KEY_OWN_CREATE_OU', 0, department)
73         return self
74
75     def computer_name(self, computer_name):
76         self.update_cnf('KEY_OWN_CREATE_CN', 0, computer_name)
77         return self
78
79     def email(self, email):
80         self.update_cnf('KEY_OWN_CREATE_EMAIL', 0, email)
81         return self
82
83     def days(self, days):
84         self.update_cnf('KEY_OWN_CREATE_DAYS', 0, days)
85         return self
86
87     def keysize(self, keysize):
88         self.update_cnf('KEY_OWN_KEYSIZE', 0, keysize)
89         return self
90
91     def hash_algo(self, hash_algo):
92         self.update_cnf('KEY_OWN_HASH_ALGO', 0, hash_algo)
93         return self
94
95     def certchain(self, certchain):
96         self.update_cnf('KEY_OWN_CERTCHAIN', 0, certchain)
97         return self
98
99     def cerchain_count(self, cerchain_count):
100         self.update_cnf('KEY_OWN_CERTCHAIN_CERTCOUNT', 0, cerchain_count)
101         return self
102
103     def create_subjalt(self, create_subjalt):
104         self.update_cnf('KEY_OWN_CREATE_SUBJALT', 0, create_subjalt)
105         return self
106
107     def create_subjalt_type(self, create_subjalt_type):
108         self.update_cnf('KEY_OWN_CREATE_SUBJALT_TYPE', 0, create_subjalt_type)
109         return self
110
111     def fingerprint_md5(self, fingerprint_md5):
112         self.update_cnf('KEY_OWN_FINGERPRINT_MD5', 0, fingerprint_md5)
113         return self
114
115     def fingerprint_sha1(self, fingerprint_sha1):
116         self.update_cnf('KEY_OWN_FINGERPRINT_SHA1', 0, fingerprint_sha1)
117         return self
118
119     def id_x509(self, id_x509):
120         self.update_cnf('KEY_OWN_ID_X509', 0, id_x509)
121         return self
122
123     def issuer(self, issuer):
124         self.update_cnf('KEY_OWN_ISSUER', 0, issuer)
125         return self
126
127     def private_key(self, private_key):
128         self.update_cnf('KEY_OWN_PRIVATE_KEY', 0, private_key)
129         return self
130
131     def public_key(self, public_key):
132         self.update_cnf('KEY_OWN_PUBLIC_KEY', 0, public_key)
133         return self
134
135     def request(self, request):
136         self.update_cnf('KEY_OWN_REQUEST', 0, request)
137         return self
138
139     def subject(self, subject):
140         self.update_cnf('KEY_OWN_SUBJECT', 0, subject)
141         return self
142
143     def subject_alt(self, subject_alt):
144         self.update_cnf('KEY_OWN_SUBJECT_ALT', 0, subject_alt)
145         return self
146
147     def key_type(self, key_type):
148         self.update_cnf('KEY_OWN_TYPE', 0, key_type)
149         return self
150
151     def valid_from(self, valid_from):
152         self.update_cnf('KEY_OWN_VALIDFROM', 0, valid_from)
153         return self
154
155     def valid_till(self, valid_till):
156         self.update_cnf('KEY_OWN_VALIDTILL', 0, valid_till)
157         return self