-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathencrypt-smime.py
151 lines (108 loc) · 5.45 KB
/
encrypt-smime.py
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
#!/usr/bin/python
##############################################################################
# #
# Copyright 2015, Jakob Bieling & John Bieling #
# #
# https://github.com/jobisoft/encrypt-smime #
# #
# This program is free software; you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 2 of the License, or #
# any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the Free Software #
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #
# #
##############################################################################
import sys, string, uuid, re, os.path, socket
from M2Crypto import BIO, Rand, SMIME, X509
def parseHeader(hdr):
headerLines = hdr.split (lineFeedType)
header = []
for line in headerLines:
if line [0] != ' ' and line [0] != '\t':
header.append (line)
else:
header [-1] += lineFeedType + line
return header
def isContentTypeEncrypted(hdr):
isEncrypted = re.search ("""^Content-Type:.*smime-type[ ]*=[ "']*enveloped-data""", hdr, re.DOTALL) != None
return isEncrypted
# Read full message from stdin.
emailData = sys.stdin.read ()
lineFeedType = ""
# A missing pubKey is not an error, the user decided not to provide one.
pubKeyIsGiven = len (sys.argv) > 1 and os.path.isfile (sys.argv[1])
if not pubKeyIsGiven:
sys.stdout.write (emailData)
sys.exit (0)
# Split headers from body.
lineFeedType = "\r\n"
emailParts = emailData.split (lineFeedType + lineFeedType, 1)
if len(emailParts) == 1:
lineFeedType = "\n"
emailParts = emailData.split (lineFeedType + lineFeedType, 1)
unableToSplitHeaderFromBody = (len(emailParts) == 1)
if unableToSplitHeaderFromBody:
sys.stdout.write (emailData)
sys.exit (0)
emailHeader = emailParts [0]
emailBody = emailParts [1]
# Split header into individual header elements.
parsedHeader = parseHeader(emailHeader)
isAlreadyEncrypted = any(isContentTypeEncrypted(hdr) for hdr in parsedHeader)
if isAlreadyEncrypted:
sys.stdout.write (emailData)
sys.exit (0)
# Filter and sort header elements.
emailContentHeaders = lineFeedType.join (filter(lambda x: x.lower().startswith("content-"), parsedHeader))
emailNonContentHeaders = lineFeedType.join (filter(lambda x: not x.lower().startswith("content-") and not x.lower().startswith("mime-version:") and not x.lower().startswith("message-id:"), parsedHeader))
emailOriginalMessageID = lineFeedType.join (filter(lambda x: x.lower().startswith("message-id:"), parsedHeader))
# Generate new body, which will be encrypted.
emailUUID = str (uuid.uuid1 ())
emailBoundaryString = '==_Part_' + emailUUID
emailNewBody = 'Content-Type: multipart/mixed; boundary="' + emailBoundaryString + '"' + lineFeedType
emailNewBody += lineFeedType
emailNewBody += '--' + emailBoundaryString + lineFeedType
emailNewBody += emailContentHeaders + lineFeedType
emailNewBody += lineFeedType
emailNewBody += emailBody + lineFeedType
emailNewBody += '--' + emailBoundaryString + '--' + lineFeedType
# Seed the random number generator with 1024 random bytes (8192 bits).
Rand.rand_seed (os.urandom (1024))
# An invalid key is an error, tell user via X-Crypt-Header.
try:
pubKeyIsInvalid = False
cert = X509.load_cert (sys.argv[1])
x509Stack = X509.X509_Stack ()
x509Stack.push (cert)
except:
pubKeyIsInvalid = True
if pubKeyIsInvalid:
sys.stdout.write ("X-Crypt: Encryption by <" + socket.gethostname()+ "> failed due to bad key." + lineFeedType)
sys.stdout.write (emailData)
sys.exit (0)
# Encrypt message.
smimeObj = SMIME.SMIME ()
smimeObj.set_x509_stack (x509Stack)
smimeObj.set_cipher (SMIME.Cipher ('des_ede3_cbc'))
encryptedEmailBody = smimeObj.encrypt (BIO.MemoryBuffer (emailNewBody))
# Compose encrypted email out of prepared parts.
# According to RFC 822 and RFC 2822, every email must have a message
# field ID that "provides a unique message identifier that refers to a
# particular version of a particular message. The uniqueness of
# the message identifier is guaranteed by the host that generates it"
# Therefore, a new message ID must be genereated.
out = BIO.MemoryBuffer ()
out.write ("X-Crypt: This message has been encrypted by <" + socket.gethostname() + ">" + lineFeedType)
out.write ("Message-ID: " + emailUUID + "@" + socket.gethostname() + lineFeedType)
out.write ("X-Original-" + emailOriginalMessageID + lineFeedType)
out.write (emailNonContentHeaders + lineFeedType)
smimeObj.write (out, encryptedEmailBody)
print out.read ()