def test_mail(mails, bilateral): import email.mime.text from kryptomime import create_mail, protect_mail from kryptomime.pgp import find_gnupg_key server, imapsend, imaprecv = mails id1, id2 = bilateral['id1'], bilateral['id2'] key1 = find_gnupg_key(bilateral['gpg1'], sender) key2 = find_gnupg_key(bilateral['gpg2'], receiver) assert not len(imapsend) and not len(imaprecv) attachment = email.mime.text.MIMEText('some\nattachment') msg = create_mail(sender, receiver, 'subject', 'body\nmessage', attach=[attachment]) #msg = protect_mail(msg,linesep='\r\n') enc, _ = id1.encrypt(msg, sign=True, inline=False) assert enc and id2.analyze(enc) == (True, None) assert server.send(enc) assert not len(imapsend) and len(imaprecv) == 1 for mail, flags in imaprecv: mail, verified, result = id2.decrypt(mail) assert mail and verified and result assert result['encrypted'] and result['signed'] and result[ 'fingerprints'] == [key1] compare_mail(msg, mail) assert not len(imapsend) and not len(imaprecv) msg = create_mail(receiver, sender, 'subject', 'body\nmessage', attach=[attachment]) msg = protect_mail(msg, linesep='\r\n') sgn, _ = id2.sign(msg, inline=True, verify=True) assert sgn and id1.analyze(sgn) == (False, True) dec, verified, result = id1.decrypt(sgn, strict=True) assert dec and verified and result assert not result['encrypted'] and result['signed'] and result[ 'fingerprints'] == [key2] compare_mail(msg, dec) assert server.send(sgn) assert len(imapsend) == 1 and not len(imaprecv) for mail, flags in imapsend: mail = protect_mail(mail, linesep='\r\n') dec, verified, result = id1.decrypt(mail, strict=True) assert dec and verified and result assert not result['encrypted'] and result['signed'] and result[ 'fingerprints'] == [key2] compare_mail(msg, dec) assert not len(imapsend) and not len(imaprecv)
def create_mail(self, status, inv, sender, email, code=None): from kryptomime import create_mail if status == StatusType.uploaded: link = self.invite_url % inv.code subject, body = self.invite_subject, self.invite_body % link elif status == StatusType.registered: subject, body = self.registered_subject, self.registered_body else: # status == StatusType.failed: subject, body = self.failed_subject, self.failed_body return create_mail(sender, email, subject, body)
def create_mail(self,status,inv,sender,email,code=None): "create mail using the status dependent template" from kryptomime import create_mail from six import PY2 if status == IStatusType.uploaded: link = self.invite_url % inv.code subject, body = self.invite_subject,self.invite_body elif status == IStatusType.registered: subject, body = self.registered_subject, self.registered_body else: # status == IStatusType.failed: subject, body = self.failed_subject, self.failed_body if PY2: body = body.decode('string_escape') else: body = body.encode().decode('unicode_escape') if status == IStatusType.uploaded: link = self.invite_url % inv.code body = body % link return create_mail(sender,email,subject,body)
def test_registerkey(request,accounts,mails,bilateral,defect): from accounts.models import Account from idapi.models import Message, PublicKey from idapi.mails import gnupg_init, get_mails, send_mails, update_keyrings, process_register import email.mime.text from kryptomime import create_mail from kryptomime.pgp import find_gnupg_key import tempfile home = tempfile.mkdtemp() gpg = gnupg_init(home) update_keyrings(debug_gpg=gpg,debug_import=bilateral['gpg1']) user = accounts['member1'] id1, id2 = bilateral['id1'], bilateral['id2'] key1 = find_gnupg_key(bilateral['gpg1'],sender) key2 = find_gnupg_key(bilateral['gpg2'],receiver) server, imapsend, imaprecv = mails # id1=register, id2=member1 if defect=='unknown': # unknown sender, should be rejected unknown = 'unknown@localhost' imapbad = server.add_account(unknown,keep=False) msg = create_mail(unknown,sender,'register','register') elif defect=='nokey': # known sender, but forget key/signing msg = create_mail(receiver,sender,'register','register') else: # known sender, return confirmation request attach = id2.pubkey_attachment(key2) msg = create_mail(receiver,sender,'register','register',attach=[attach]) assert server.send(msg) assert len(imapsend)==1 get_mails(joint=True,debug=server,debug_gpg=gpg,keep=False) process_register(debug_gpg=gpg) send_mails(joint=True,debug=server,debug_gpg=gpg) if defect=='unknown': assert len(imapbad)==1 for mail, flags in imapbad: mail, verified, result = id2.decrypt(mail,strict=False) assert mail and result assert not result['encrypted'] and result['signed'] and result['fingerprints']==[key1] #print mail # assert key1 attached return return #FIXME assert len(imaprecv)==1 msg = None for mail, flags in imaprecv: mail, verified, result = id2.decrypt(mail,strict=False) assert mail and result assert result['signed'] and result['fingerprints']==[key1] assert bool(result['encrypted']) == (defect!='nokey') #print mail msg = mail # assert key1 attached if defect=='nokey': return if defect=='badcode': reply = create_mail(receiver,sender,'confirmation','bad code') else: body = msg.get_payload() reply = '> '+'> '.join(body.splitlines(True)) reply = create_mail(receiver,sender,'Re: '+mail['subject'],reply) reply = id2.encrypt(reply,sign=True) assert server.send(reply) assert len(imapsend)==1 get_mails(joint=True,debug=server,debug_gpg=gpg,keep=False) process_register(debug_gpg=gpg) send_mails(joint=True,debug=server,debug_gpg=gpg) assert len(imaprecv)==1 for mail, flags in imaprecv: mail, verified, result = id2.decrypt(mail,strict=False) assert mail and result assert result['encrypted'] and result['signed'] and result['fingerprints']==[key1] #print mail msg = mail # assert key1 attached if defect=='badcode': return if defect!='unverified': key = user.publickeys.get(active=True) key.trust=PublicKey.TRUSTED key.save() msg = create_mail(receiver,sender,'test','test') msg = id2.encrypt(msg,sign=True) assert server.send(reply) assert len(imapsend)==1 get_mails(joint=True,debug=server,debug_gpg=gpg,keep=False) # check verified = CONFIRMED/TRUST/... # verifiyed key, receive return
def test_receive(request, accounts, tokens, mails, bilateral, client, defect): from accounts.models import Account from idapi.models import Message, PublicKey from idapi.mails import get_mails, update_keyrings import email.mime.text, six from kryptomime import create_mail, protect_mail from kryptomime.pgp import find_gnupg_key user = accounts['member1'] token = tokens['member1'] #update_keyrings() id1, id2 = bilateral['id1'], bilateral['id2'] key1 = find_gnupg_key(bilateral['gpg1'], sender) key2 = find_gnupg_key(bilateral['gpg2'], receiver) livemail = request.config.getoption('livemail') if livemail: server = mails else: server, imapsend, imaprecv = mails if not livemail: assert not len(imapsend) and not len(imaprecv) user.publickeys.create(active=True, keytype=PublicKey.PGP, trust=PublicKey.TRUSTED) key1 = find_gnupg_key(bilateral['gpg1'], sender) key2 = find_gnupg_key(bilateral['gpg2'], receiver) attachment = email.mime.text.MIMEText('some\nattachment') msg = create_mail(receiver, sender, 'subject', 'body\nmessage', attach=[attachment]) msg = protect_mail(msg, linesep='\r\n') assert server.send(msg) if not livemail: assert len(imapsend) == 1 and not len(imaprecv) sgn, _ = id2.sign(msg, inline=True, verify=True) assert sgn and id1.analyze(sgn) == (False, True) assert server.send(sgn) if not livemail: assert len(imapsend) == 2 and not len(imaprecv) enc, _ = id2.encrypt(msg, sign=True, inline=False) assert enc and id1.analyze(enc) == (True, None) assert server.send(enc) if not livemail: assert len(imapsend) == 3 and not len(imaprecv) """ for mail, flags in imapsend: mtype = id1.analyze(mail) if mtype != (False,False): if mtype == (False,True): verified, result = id1.verify(mail) mail = id1.strip_signature(mail)[0] mail = protect_mail(mail,linesep='\r\n') else: #if mtype == (True,None): mail, verified, result = id1.decrypt(mail) assert mail and verified and result assert result['signed'] and result['fingerprints']==[key2] assert result['encrypted']==(mtype != (False,True)) else: mail = protect_mail(mail,linesep='\r\n') compare_mail(msg,mail) assert not len(imapsend) and not len(imaprecv) """ get_mails(joint=True, debug=None if livemail else server, debug_gpg=bilateral['gpg1'], keep=False) if not livemail: assert not len(imapsend) and not len(imaprecv) response, out = api(client, 'user/mails/', token=token) assert response.status_code == 200 todo = [(False, False), (True, False), (True, True)] for mid in out['items']: response, out = api(client, 'user/mails/%i/' % mid, token=token) assert response.status_code == 200 parts = out['parts'] assert out['subject'] == 'subject' and len(parts) == 2 signed, encrypted = out['signed'], out['encrypted'] contents = ['body\r\nmessage', 'some\r\nattachment'] assert parts[0] == { 'content': contents[0], 'content-charset': 'us-ascii', 'content-type': 'text/plain', 'content-encoding': '7bit' } assert parts[1] == { 'content': contents[1], 'content-charset': 'us-ascii', 'content-type': 'text/plain', 'content-encoding': '7bit' } todo.remove((signed, encrypted)) if signed: assert out['verified'] == 'trusted' #print out assert not todo if livemail: server.close()