-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclkeys
More file actions
executable file
·927 lines (818 loc) · 37.7 KB
/
clkeys
File metadata and controls
executable file
·927 lines (818 loc) · 37.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
#!/usr/bin/python3
"""
* File: clkeys
* Version : 1.2
* License : BSD-3-Clause
*
* Copyright (c) 2023 - 2026
* Ralf Senderek, Ireland. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Ralf Senderek.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
"""
import sys, os, re
from binascii import *
ERR_CL = -1
OK = 0
ERR_USE = 1
ERR_PERM = 2
ERR_PASSWORD = 3
ERR_INSTALL = 4
ERR_BITS = 5
ERR_SIZE = 7
ERR_CORRUPT = 10
ERR_INCOMPLETE = 11
Mode = "generate"
MinPasswordLength = 8
MaxPasswordLength = 64
Version = "1.2"
KeyFileName = "./rsakeyfile.p15"
CertFileName = "./certificate.pem"
CSRFileName = "./CSR"
NewCertFileName = "./newcertificate.pem"
status = 0
RSABits = 2048
MinRSABits = 1536
MaxRSABits = 4096
ALGO = "RSA"
ImportCert = bytearray()
KeyFileExists = False
Name = bytearray()
KeyFile = bytearray()
Password = bytearray()
CertDN = bytearray()
CertCOUNTRY = "."
CertSP = "."
CertLOCALITY = "."
CertORG = "."
CertOU = "."
CertNAME = "."
CertEMAIL = "."
BINARY = False
regex = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,7}\b'
try:
from cryptlib_py import *
except:
ERR_IMPORT = """
The python3 library is not installed. You need to install the packages cryptlib-python3 and cryptlib.
You will find them for a variety of operating systems here:
https://senderek.ie/cryptlib
or in the Fedora repository.
"""
print( ERR_IMPORT )
exit( ERR_INSTALL )
#-----------------------------------------------------------#
def unix (command) :
Result = ""
if os.name == "posix" :
try:
Pipe = os.popen(sanitize(command), "r")
Result = Pipe.read()
Pipe.close()
return Result
except:
print("\nA Unix command failed.\n")
return Result
#-----------------------------------------------#
ASKPASS = "/bin/systemd-ask-password"
if not os.path.isfile(ASKPASS) :
print ("Error: Please install " + ASKPASS + " to ensure safe password input")
exit(ERR_INSTALL)
#-----------------------------------------------------------#
def get_random_bytes ( num ):
# this function does not need to produce cryptographically secure random numbers
try:
from random import randbytes
return randbytes( num )
except:
RandomBuffer = bytearray(b' '*num)
RandomContext_object = cryptCreateContext( cryptUser, CRYPT_ALGO_AES )
RandomContext = int( RandomContext_object )
cryptSetAttribute( RandomContext, CRYPT_CTXINFO_MODE, CRYPT_MODE_CFB )
cryptGenerateKey( RandomContext )
cryptEncrypt( RandomContext, RandomBuffer )
cryptDestroyContext( RandomContext )
return RandomBuffer
#-----------------------------------------------------------#
def clean():
global privKeyContext
global cryptKeyset
try:
cryptKeysetClose( cryptKeyset )
if not (Mode == "import"):
cryptDestroyContext( privKeyContext )
cryptEnd()
except:
cryptEnd()
#-----------------------------------------------------------#
def write_CSR(buff, pathname):
# writes a bytearray into a file
try:
F = open(pathname,'w')
F.write("-----BEGIN CERTIFICATE REQUEST-----\n")
ASCII = b2a_base64(buff)
ASCII = ASCII[:-1]
i = 0
while i < len(ASCII) :
line = ASCII[i:i+64]
i = i + 64
F.write(line.decode())
if i < len(ASCII) :
F.write("\n")
F.write("\n")
F.write("-----END CERTIFICATE REQUEST-----\n")
unix("chmod 600 '" + pathname + "'")
except:
print ("Error: cannot write to " + pathname)
#-----------------------------------------------------------#
def collect_attributes():
global CertCOUNTRY, CertSP, CertLOCALITY, CertORG, CertOU, CertNAME, CertEMAIL
intro = """
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name.
There are quite a few fields but you can leave some blank.
If you enter '.', the field will be left blank.
A country name, a common name and an email address are mandatory.
"""
print (intro)
CertCOUNTRY = sanitize( input( "Country Name (2 letter code) " ))
CertSP = sanitize( input( "State or Province Name (full name) " ))
CertLOCALITY = sanitize( input( "Locality Name (eg, city) " ))
CertORG = sanitize( input( "Organization Name (eg, company) " ))
CertOU = sanitize( input( "Organizational Unit Name " ))
CertNAME = Name.decode() + sanitize( input( "Common Name (enter YOUR name) " + Name.decode() ))
CertEMAIL = sanitize( input( "SAN: Email Address " ))
#-----------------------------------------------------------#
def sanitize(data):
forbidden = "!\"§$%()[]{}=?*+~,<>\\"
good = ""
for i in range(len(data)) :
if (data[i] not in forbidden) and (ord(data[i]) < 128):
good += data[i]
return good
#-----------------------------------------------------------#
def readCertificateFromFile( FileName, Header ):
global BINARY
Header = Header + "-----"
F = open( FileName, "rb" )
data = F.read()
F.close()
start = end = 0
BEGIN = bytearray(b'-----BEGIN ')
BEGIN.extend(Header.encode())
END = bytearray(b'-----END ')
END.extend(Header.encode())
begin = False
Length = len( data )
ASCII = bytearray()
i = j = 0
while ( (not begin) and (i < Length) ) :
while ((i < Length) and (data[i] != 45)) :
i = i + 1
if (i < Length) :
begin = True
# hit first -
j = 0
while ((j < (len(BEGIN) -1)) and begin and (i < Length)) :
if (data[i] != BEGIN[j]) :
begin = False
i = i + 1
j = j + 1
if (begin) :
start = i - len(BEGIN) + 1
# find -----END CERTIFICATE----- or -----END CERTIFICATE REQUEST-----
begin = False
while ( (not begin) and (i < Length) ) :
while ((i < Length) and (data[i] != 45)) :
i = i + 1
if (i < Length) :
begin = True
# hit first -
j = 0
while ((j < (len(END) )) and begin and (i < Length)) :
if (data[i] != END[j]) :
begin = False
i = i + 1
j = j + 1
if (begin) :
end = i
# copy start to end to ASCII block
i = start
j = 0
while (i < end) :
ASCII.append( data[i] )
j = j + 1
i = i + 1
BINARY = False
if ( (start == 0) and (end == 0) ) :
# data is probably binary input string
BINARY = True
i = start
j = 0
while (i < len(data)) :
ASCII.append( data[i] )
j = j + 1
i = i + 1
return ASCII
#-----------------------------------------------------------#
def get_asn1 (FileName):
# returns a string
L = ""
try:
L = unix( "/bin/dumpasn1 -z " + FileName )
except:
print("Cannot read keyset file " + FileName)
exit ( ERR_PERM )
return L
#-----------------------------------------------------------#
def get_DN_from_keyset (FileName):
# The file must contain an ASN1 structure of a Cryptlib-generated keyset
# returns String
DN = ""
try:
L = unix( "/bin/dumpasn1 -z " + FileName ).split('\n')
if "pkcs15content" in L[1]:
DNLine = L[10]
if "UTF8String" in DNLine:
pos = DNLine.find("UTF8String ")
DN = DNLine[pos+12:-1]
if DN:
return DN
except:
print("Cannot read keyset file " + FileName)
return DN
#-----------------------------------------------------------#
if ( len(sys.argv) >= 2 ):
# legitimate options are in the parameter list
if "generate" in sys.argv :
Mode = "generate"
sys.argv.remove( "generate" )
elif "request" in sys.argv :
Mode = "request"
sys.argv.remove( "request" )
elif "import" in sys.argv :
Mode = "import"
sys.argv.remove( "import" )
elif "casign" in sys.argv :
Mode = "casign"
sys.argv.remove( "casign" )
elif "list" in sys.argv :
Mode = "list"
sys.argv.remove( "list" )
else:
print("You need to specify an operation: generate or request or import or casign or list")
exit( ERR_USE )
if "-DSA" in sys.argv :
ALGO = "DSA"
sys.argv.remove( "-DSA" )
else:
print( "usage: clkeys generate [-DSA] KeysetName [-SIZE RSABits] [-CN YourName]" )
print( " clkeys request KeysetName [-CN YourName]")
print( " clkeys import KeysetName CertFile")
print( " clkeys casign CAKeysetName RequestFile")
print( " clkeys list KeysetName")
exit(ERR_USE)
# all modes are processed
if Mode == "generate":
if ALGO == "RSA":
print ("Generating a RSA key pair.")
if ALGO == "DSA":
print ("Generating a DSA key pair.")
if Mode == "request":
print ("Writing a CertificateSigningRequest for a RSA key pair.")
if Mode == "import":
print ("Importing a CA-signed certificate from file.")
if Mode == "casign":
print ("Signing a request with a CA-key and write the user's certificate to a file.")
if len(sys.argv) >= 2 :
SafeKeysetName = sanitize( sys.argv[1] )
# allow the Keyset name to end in ".p15" on the command line
if len( SafeKeysetName) < 2 :
print("The keyset name is invalid.")
exit( ERR_USE )
if SafeKeysetName[-4:] == ".p15" :
KeyFileName = SafeKeysetName
KeyBaseName = SafeKeysetName[:-4] + ".base.p15"
else:
KeyFileName = SafeKeysetName + ".p15"
KeyBaseName = SafeKeysetName + ".base.p15"
# check if keyset file exists
if os.path.isfile (KeyFileName):
KeyFileExists = True
if Mode == "generate":
print( "The keyfile " + KeyFileName + " exists. Please rename or remove it." )
exit( ERR_USE )
else:
if os.path.getsize(KeyFileName) < 100 :
print( "There is a keyset named " + KeyFileName +" but it is not a valid keyset file." )
print( "Please use generate to create a new keyset or use a different name." )
exit( ERR_CORRUPT )
else:
# request, import, casign and list need an existing keyset
if (Mode == "request") or (Mode == "import") or (Mode == "casign") or (Mode == "list"):
print( "There is no keyset named " + KeyFileName )
exit( ERR_USE )
if SafeKeysetName[-4:] == ".p15" :
Name.extend( SafeKeysetName[:-4].encode() )
else:
Name.extend( SafeKeysetName.encode() )
KeyFile.extend( KeyFileName.encode() )
if Mode == "generate":
if SafeKeysetName[-4:] == ".p15" :
CertFileName = SafeKeysetName[:-4] + ".cert.pem"
else:
CertFileName = SafeKeysetName + ".cert.pem"
print( "Storing keys in the file " + KeyFileName + " and " + CertFileName )
if Mode == "request":
if SafeKeysetName[-4:] == ".p15" :
CSRFileName = SafeKeysetName[:-4] + ".CSR"
else:
CSRFileName = SafeKeysetName + ".CSR"
print( "Using existing keys in the keyset file " + KeyFileName + " and storing the new request in " + CSRFileName + ".pem" )
if Mode == "import":
print( "Importing a certificate into keyset " + KeyFileName )
if Mode == "casign":
print( "Using the keyset " + KeyFileName + " with the CA private key and CA certificate")
if Mode == "list":
print ("Listing the ASN1 structure of the keyset " + KeyFileName + ".")
if (len (sys.argv) >= 3) and ((Mode == "generate") or (Mode == "request")) :
# check -SIZE
if ((sys.argv[2] == "-SIZE") or (sys.argv[2] == "-size")) and (Mode == "generate") :
# read RSA bits
try:
RSABits = int( sys.argv[3] )
sys.argv.pop(3)
sys.argv.pop(2)
print("RSABits is set to : " + str(RSABits))
if ( (RSABits < MinRSABits) or (RSABits > MaxRSABits) ) :
print( "RSABits must be greater than " + str(MinRSABits) + " but not exceed " + str(MaxRSABits) )
exit( ERR_SIZE )
except ValueError:
print( "Illegal RSABits " + str(sys.argv[3]) )
exit( ERR_BITS )
if len(sys.argv) >= 3 and ((sys.argv[2] != "-CN") and (sys.argv[2] != "-cn")) :
# illegal parameter at pos 2, remove it silently
sys.argv.pop(2)
if len(sys.argv) >= 3 and (sys.argv[2] == "-CN" or sys.argv[2] == "-cn"):
# overwrite Name with the new Common Name
if len(sys.argv) > 3 :
i = 3
Name = bytearray()
Name.extend( sanitize(sys.argv[i]).encode() )
while i < len(sys.argv)-1 :
i += 1
Name.extend( " ".encode() )
Name.extend( sanitize(sys.argv[i]).encode() )
print("Using the KEYID \'" + Name.decode() + "\'")
if (Mode == "import") or (Mode == "casign") :
if (len(sys.argv) >= 3) :
# process a cert file name
try:
# read the certificate file
if (Mode == "import") :
ImportCert = readCertificateFromFile(sys.argv[2], "CERTIFICATE")
if (Mode == "casign") :
ImportCert = readCertificateFromFile(sys.argv[2], "CERTIFICATE REQUEST")
if (sys.argv[2][-8:] == ".CSR.pem") or (sys.argv[2][-8:] == ".CSR.der") :
NewCertFileName = sys.argv[2][:-8] + ".newcert.pem"
except:
print( "Cannot read the certificate or request " + sys.argv[2] + "." )
exit( ERR_PERM )
else:
# a required file name is missing
print( "You need to use a file name for the certificate or request." )
exit( ERR_USE )
else:
print( "usage: clkeys generate [-DSA] KeysetName [-SIZE RSABits] [-CN YourName]")
print( " clkeys request KeysetName [-CN YourName]")
print( " clkeys import KeysetName CertFile")
print( " clkeys casign CAKeysetName RequestFile")
print( " clkeys list KeysetName")
exit(ERR_USE)
##### Begin Cryptlib code #####
try:
cryptUser = CRYPT_UNUSED
cryptInit()
# collect randomness information
cryptAddRandom( CRYPT_RANDOM_SLOWPOLL )
# get Cryptlib Version
Major = cryptGetAttribute(CRYPT_UNUSED, CRYPT_OPTION_INFO_MAJORVERSION)
Minor = cryptGetAttribute(CRYPT_UNUSED, CRYPT_OPTION_INFO_MINORVERSION)
Step = cryptGetAttribute(CRYPT_UNUSED, CRYPT_OPTION_INFO_STEPPING)
CryptlibVersion = str(Major)+"."+str(Minor)+"."+str(Step)
print( "clkeys " + Version + " uses Cryptlib version " + CryptlibVersion + "\n")
if (Mode == "list") :
ASN1 = get_asn1 (KeyFileName)
print(ASN1)
# nothing to clean !
cryptEnd()
exit(OK)
if ( (Mode == "generate") ) :
# create a new FILE keyset
cryptKeyset_Object = cryptKeysetOpen( cryptUser, CRYPT_KEYSET_FILE, KeyFile, CRYPT_KEYOPT_CREATE )
cryptKeyset = int( cryptKeyset_Object )
# generate a key pair into the keyset
if ALGO == "RSA" :
privKeyContext_Object = cryptCreateContext( cryptUser, CRYPT_ALGO_RSA )
privKeyContext = int ( privKeyContext_Object )
if ALGO == "DSA":
privKeyContext_Object = cryptCreateContext( cryptUser, CRYPT_ALGO_DSA )
privKeyContext = int ( privKeyContext_Object )
cryptSetAttributeString( privKeyContext, CRYPT_CTXINFO_LABEL, Name )
if ALGO == "RSA" :
cryptSetAttribute( privKeyContext, CRYPT_CTXINFO_KEYSIZE, int( RSABits/8 ) )
cryptGenerateKey( privKeyContext )
# get a password to encrypt the private key
print("Please enter the password used to protect the private key: ")
Password.extend( unix(ASKPASS).encode() )
if len( Password ) >= MinPasswordLength and len( Password ) <= MaxPasswordLength :
Password = Password[:-1]
cryptAddPrivateKey( cryptKeyset, privKeyContext, Password )
# randomize password buffer as it is no longer needed
Password = get_random_bytes( len(Password) )
del Password
else:
print ("Error: Your password must have at least " + str(MinPasswordLength) + " characters. Nothing done.")
unix("/bin/stty sane")
clean()
exit (ERR_PASSWORD)
# selfsign a simplified Certificate
cryptCertificate_Object = cryptCreateCert( cryptUser, CRYPT_CERTTYPE_CERTIFICATE )
cryptCertificate = int (cryptCertificate_Object)
cryptSetAttribute( cryptCertificate, CRYPT_CERTINFO_SUBJECTPUBLICKEYINFO, privKeyContext )
cryptSetAttribute( cryptCertificate, CRYPT_CERTINFO_XYZZY, 1)
cryptSetAttributeString( cryptCertificate, CRYPT_CERTINFO_COMMONNAME, Name )
cryptSignCert( cryptCertificate, privKeyContext )
# do not add the selfsiged certificate, use import to add it to the keyset
# export the selfsigned certificate
certFormatType = CRYPT_CERTFORMAT_TEXT_CERTIFICATE
certMaxLength = cryptExportCert( None, 0, certFormatType, cryptCertificate )
EncodedCert = bytearray(b' '*certMaxLength)
print ("\nCertLength: " + str(certMaxLength) + " bytes")
cryptExportCert( EncodedCert, certMaxLength, certFormatType, cryptCertificate )
print(EncodedCert.decode())
try:
# write cert to file
F = open (CertFileName,"bw")
F.write(EncodedCert)
F.close()
unix("chmod 600 '" + CertFileName + "'")
# write KEYID to file
F = open (KeyFileName + ".KEYID","bw")
F.write( Name )
F.close()
unix("chmod 600 '" + KeyFileName + ".KEYID'")
# the keyset must be closed before it can be copied
cryptKeysetClose( cryptKeyset )
cryptDestroyContext( privKeyContext )
cryptDestroyContext( cryptCertificate )
unix("/bin/cp " + KeyFileName + " " + KeyBaseName )
cryptEnd()
except:
print("Cannot write files.")
cryptDestroyContext( cryptCertificate )
clean()
exit( ERR_PERM )
exit(OK)
if (Mode == "request") :
try:
cryptKeyset_Object = cryptKeysetOpen( cryptUser, CRYPT_KEYSET_FILE, KeyFile, CRYPT_KEYOPT_READONLY )
cryptKeyset = int( cryptKeyset_Object )
except CryptException as e :
status, message = e.args
if status == CRYPT_ERROR_BADDATA :
print( "Error: The keyset file " + KeyFile.decode() + " cannot be used. It may be corrupted." )
cryptEnd( )
exit( ERR_CORRUPT )
# get the public key
try:
pubKey_Object = cryptGetPublicKey( cryptKeyset, CRYPT_KEYID_NAME, Name )
pubKey = int (pubKey_Object)
except CryptException as e :
status, message = e.args
if status == CRYPT_ERROR_NOTFOUND :
print( "Error: You are using the wrong KEYID, please use -DN YourName instead." )
Owner = get_DN_from_keyset (KeyFile.decode())
if Owner :
print( " Found the key-owner name " + Owner + " in the keyset file" )
clean()
exit( ERR_CORRUPT )
# get the private key
print("Please enter the password that you use to protect the private key: ")
Password.extend( unix(ASKPASS).encode() )
if len( Password ) >= MinPasswordLength and len( Password ) <= MaxPasswordLength :
Password = Password[:-1]
try:
privKeyContext_Object = cryptGetPrivateKey(cryptKeyset, CRYPT_KEYID_NAME, Name, Password)
privKeyContext = int( privKeyContext_Object )
except CryptException as e :
status, message = e.args
# randomize password buffer as it is no longer needed
Password = get_random_bytes( len(Password) )
del Password
if status == CRYPT_ERROR_WRONGKEY :
print( "Error: You have entered the wrong password. Private key is unavailable." )
cryptDestroyContext( pubKey )
clean()
exit( ERR_PASSWORD )
elif status == CRYPT_ERROR_NOTFOUND :
print( "Error: You are using the wrong KEYID, please use -DN YourName instead." )
Owner = get_DN_from_keyset (KeyFile.decode())
if Owner :
print( " Found the key-owner name " + Owner + " in the keyset file" )
cryptDestroyContext( pubKey )
clean()
exit( ERR_PASSWORD )
# randomize password buffer as it is no longer needed
Password = get_random_bytes( len(Password) )
del Password
else:
print ("Error: Your password must have at least " + str(MinPasswordLength) + " characters. Nothing done.")
Password = get_random_bytes( len(Password) )
cryptDestroyContext( pubKey )
unix("/bin/stty sane")
clean()
exit (ERR_PASSWORD)
# create a CertificateSigningRequest
CertRequest_Object = cryptCreateCert( cryptUser, CRYPT_CERTTYPE_CERTREQUEST )
CertRequest = int( CertRequest_Object )
cryptSetAttribute( CertRequest, CRYPT_CERTINFO_SUBJECTPUBLICKEYINFO, pubKey )
# add DN info
collect_attributes()
if ( not re.fullmatch(regex, CertEMAIL) ) :
print( "The email address \"" + CertEMAIL + "\" is invalid!" )
cryptDestroyContext( pubKey )
cryptDestroyContext( CertRequest )
clean()
exit ( ERR_INCOMPLETE )
if CertNAME == "." or CertEMAIL == "." or len(CertNAME) < 3 or len(CertEMAIL) == 0 :
print( "You need to enter a common name and a valid email address!" )
cryptDestroyContext( pubKey )
cryptDestroyContext( CertRequest )
clean()
exit ( ERR_INCOMPLETE )
if CertCOUNTRY == "." or len(CertCOUNTRY) != 2 :
print("Please provide a correct country code")
cryptDestroyContext( pubKey )
cryptDestroyContext( CertRequest )
clean()
exit(ERR_INCOMPLETE)
# set the minimal required fields
cryptSetAttributeString( CertRequest, CRYPT_CERTINFO_COUNTRYNAME, bytearray(CertCOUNTRY.encode()) )
cryptSetAttributeString( CertRequest, CRYPT_CERTINFO_COMMONNAME, bytearray(CertNAME.encode()) )
# additional information
if CertOU != "." and CertOU :
cryptSetAttributeString( CertRequest, CRYPT_CERTINFO_ORGANISATIONALUNITNAME, bytearray(CertOU.encode()) )
if CertORG != "." and CertORG :
cryptSetAttributeString( CertRequest, CRYPT_CERTINFO_ORGANISATIONNAME, bytearray(CertORG.encode()) )
if CertLOCALITY != "." and CertLOCALITY :
cryptSetAttributeString( CertRequest, CRYPT_CERTINFO_LOCALITYNAME, bytearray(CertLOCALITY.encode()) )
if CertSP != "." and CertSP :
cryptSetAttributeString( CertRequest, CRYPT_CERTINFO_STATEORPROVINCENAME, bytearray(CertSP.encode()) )
try:
cryptSetAttributeString( CertRequest, CRYPT_CERTINFO_EMAIL, bytearray(CertEMAIL.encode()) )
except CryptException as e :
status, message = e.args
if status == CRYPT_ERROR_PARAM4 :
print( "Error: You need to enter a valid email address!" )
cryptDestroyContext( pubKey )
cryptDestroyContext( CertRequest )
clean()
exit( ERR_INCOMPLETE )
try:
cryptSignCert( CertRequest, privKeyContext )
except CryptException as e :
status, message = e.args
if status == CRYPT_ERROR_NOTINITED :
print( "Error: You need to enter at least a country name, a common name." )
cryptDestroyContext( pubKey )
cryptDestroyContext( CertRequest )
clean()
exit( ERR_INCOMPLETE )
requestMaxLength = cryptExportCert( None, 0, CRYPT_CERTFORMAT_CERTIFICATE, CertRequest )
EncodedCSR = bytearray(b' '*requestMaxLength)
print ("\nGenerating a CSR of length: " + str(requestMaxLength) + " bytes.")
cryptExportCert( EncodedCSR, requestMaxLength, CRYPT_CERTFORMAT_CERTIFICATE, CertRequest )
try:
# write CSR to file
print( "Writing the certificate signing request to the file " + CSRFileName+".pem ." )
write_CSR(EncodedCSR, CSRFileName+".pem")
# also write the binary content to
F = open (CSRFileName+".der","bw")
F.write(EncodedCSR)
F.close()
unix("chmod 600 '" + CSRFileName + ".der'")
except:
print("Cannot write files.")
cryptDestroyContext( pubKey )
cryptDestroyContext( CertRequest )
clean()
exit( ERR_PERM )
if (Mode == "import") :
cryptCertificate_Object = cryptImportCert( ImportCert, cryptUser )
cryptCertificate = int( cryptCertificate_Object )
try:
cryptKeyset_Object = cryptKeysetOpen( cryptUser, CRYPT_KEYSET_FILE, KeyFile, CRYPT_KEYOPT_NONE )
cryptKeyset = int( cryptKeyset_Object )
except CryptException as e :
status, message = e.args
if status == CRYPT_ERROR_BADDATA :
print( "Error: The keyset file " + KeyFile.decode() + " cannot be used. It may be corrupted." )
cryptDestroyContext( cryptCertificate )
cryptEnd()
exit( ERR_CORRUPT )
# Update the keyset with the certificate
try:
cryptAddPublicKey( cryptKeyset, cryptCertificate )
print ("The certificate has been imported successfully.")
except CryptException as e :
status, message = e.args
if status == CRYPT_ERROR_PARAM2 :
print( "Error: there isn’t already a matching private key present in the keyset" )
elif status == CRYPT_ERROR_DUPLICATE :
print( "This certificate is already imported in the keyset " + KeyFileName )
else:
print( "Error ... " + message )
if (Mode == "casign") :
print ("Processing " + str(len(ImportCert)) + " bytes." )
# turn the request into a certificate object
try:
cryptCertRequest_Object = cryptImportCert( ImportCert, cryptUser )
CertRequest = int( cryptCertRequest_Object )
except CryptException as e :
status, message = e.args
if status == CRYPT_ERROR_BADDATA :
print( "Error: The request is probably corrupt." )
cryptEnd()
exit( ERR_CORRUPT )
try:
cryptCheckCert( CertRequest, cryptUser )
except CryptException as e :
status, message = e.args
if status == CRYPT_ERROR_BADDATA :
print( "Error: The request is probably corrupt." )
cryptDestroyContext( CertRequest )
cryptEnd()
exit( ERR_CORRUPT )
# make sure, that the correct KEYID is being used
KEYIDFileName = KeyFile.decode() + ".KEYID"
if os.path.isfile(KEYIDFileName) :
KeyIDContent = ""
F = open (KEYIDFileName, "r")
KeyIDContent = F.read().strip()
if len(KeyIDContent) != 0 :
Name.clear()
Name.extend ( KeyIDContent.encode() )
print( "Using the KEYID \'" + Name.decode() + "\' from file " + KEYIDFileName )
F.close()
cryptCertificate_Object = cryptCreateCert( cryptUser, CRYPT_CERTTYPE_CERTIFICATE )
cryptCertificate = int( cryptCertificate_Object )
cryptSetAttribute( cryptCertificate, CRYPT_CERTINFO_CERTREQUEST, CertRequest )
try:
cryptKeyset_Object = cryptKeysetOpen( cryptUser, CRYPT_KEYSET_FILE, KeyFile, CRYPT_KEYOPT_READONLY )
cryptKeyset = int( cryptKeyset_Object )
except CryptException as e :
status, message = e.args
if status == CRYPT_ERROR_BADDATA :
print( "Error: The keyset file " + KeyFile.decode() + " cannot be used. It may be corrupted." )
cryptDestroyContext( CertRequest )
cryptDestroyContext( cryptCertificate )
cryptEnd()
exit( ERR_CORRUPT )
# read the CA signing key from the keyset
print("Please enter the password that you use to protect the private key: ")
Password.extend( unix(ASKPASS).encode() )
if len( Password ) >= MinPasswordLength and len( Password ) <= MaxPasswordLength :
Password = Password[:-1]
try:
privKeyContext_Object = cryptGetPrivateKey(cryptKeyset, CRYPT_KEYID_NAME, Name, Password)
privKeyContext = int( privKeyContext_Object )
except CryptException as e :
status, message = e.args
# randomize password buffer as it is no longer needed
Password = get_random_bytes( len(Password) )
del Password
if status == CRYPT_ERROR_WRONGKEY :
print( "Error: You have entered the wrong password. The CA private key is unavailable." )
cryptDestroyContext( CertRequest )
cryptDestroyContext( cryptCertificate )
clean()
exit( ERR_PASSWORD )
elif status == CRYPT_ERROR_NOTFOUND :
print( "Error: The KEYID " + Name.decode() + " is incorrect! The CA private key is unavailable." )
Owner = get_DN_from_keyset (KeyFile.decode())
if Owner :
print( " Found the CA key-owner name " + Owner + " in the keyset file" )
print( " Please make sure that a proper KEYID file is present for this CA key." )
cryptDestroyContext( CertRequest )
cryptDestroyContext( cryptCertificate )
clean()
exit( ERR_USE )
# randomize password buffer as it is no longer needed
Password = get_random_bytes( len(Password) )
del Password
else:
print ("Error: Your password must have at least " + str(MinPasswordLength) + " characters. Nothing done.")
Password = get_random_bytes( len(Password) )
cryptDestroyContext( CertRequest )
cryptDestroyContext( cryptCertificate )
unix("/bin/stty sane")
clean()
exit (ERR_PASSWORD)
# show the DN to be signed ...
print()
DNList = [CRYPT_CERTINFO_COMMONNAME, CRYPT_CERTINFO_EMAIL, CRYPT_CERTINFO_COUNTRYNAME, CRYPT_CERTINFO_STATEORPROVINCENAME, CRYPT_CERTINFO_LOCALITYNAME, CRYPT_CERTINFO_ORGANISATIONNAME, CRYPT_CERTINFO_ORGANISATIONALUNITNAME]
DNLabel = ["CN = ", "emailAddress = ", "C = ", "SP = ", "L = ", "O = ", "OU = "]
i = 0
while i < len(DNList) :
try:
Info = bytearray(b' ')
cryptGetAttributeString( cryptCertificate, DNList[i], Info )
print( DNLabel[i] + Info.decode() )
except:
print( DNLabel[i] )
i = i + 1
print()
# ask for confirmation
REPLY = input("SIGN THIS KEY ? : ")
if REPLY == "yes" or REPLY == "y" :
# now sign the certificate
try:
cryptSignCert( cryptCertificate, privKeyContext )
except CryptException as e :
status, message = e.args
if status == CRYPT_ERROR_PARAM2 :
print( "Error: The keyset file " + KeyFile.decode() + " may not contain the selfsigend certificate." )
print( " Please make sure that the CA certificate is imported! " )
cryptDestroyContext( privKeyContext )
cryptDestroyContext( cryptCertificate )
cryptDestroyContext( CertRequest )
clean()
exit( ERR_USE )
# export the new certificate
CertMaxLength = cryptExportCert( None, 0, CRYPT_CERTFORMAT_CERTIFICATE, cryptCertificate )
EncodedCert = bytearray(b' '*CertMaxLength)
print ("\nGenerating a Certificate of length: " + str(CertMaxLength) + " bytes.")
cryptExportCert( EncodedCert, CertMaxLength, CRYPT_CERTFORMAT_CERTIFICATE, cryptCertificate )
# write the certificate to the file "newcertificate.pem"
certFormatType = CRYPT_CERTFORMAT_TEXT_CERTIFICATE
certMaxLength = cryptExportCert( None, 0, certFormatType, cryptCertificate)
EncodedCert = bytearray(b' '*certMaxLength)
print ("\nText Length: " + str(certMaxLength) + " bytes:\n")
cryptExportCert( EncodedCert, certMaxLength, certFormatType, cryptCertificate )
print(EncodedCert.decode())
try:
# write cert to file
F = open (NewCertFileName,"bw")
F.write(EncodedCert)
F.close()
unix("chmod 600 '" + NewCertFileName + "'")
except:
print("Cannot write file " + NewCertFileName)
cryptDestroyContext( privKeyContext )
cryptDestroyContext( cryptCertificate )
cryptDestroyContext( CertRequest )
clean()
exit( ERR_PERM )
else:
print("Nothing done.")
# destroy all contexts
if not (Mode == "request") :
cryptDestroyContext( cryptCertificate )
if (Mode == "request") :
cryptDestroyContext( pubKey )
if (Mode == "casign") or (Mode == "request"):
cryptDestroyContext( CertRequest )
# normal clean up
# destroy cryptKeyset and privKeyContext
# followed by cryptEnd()
clean()
except CryptException as e :
status, message = e.args
print( "Error ... " + message )
clean()
exit( OK )