Feature #6184

Find Python library to do PGP/MIME

Added by intrigeri 2013-07-23 07:31:45 . Updated 2015-01-02 01:22:39 .

Status:
Resolved
Priority:
Low
Assignee:
alant
Category:
Target version:
Start date:
2013-07-23
Due date:
% Done:

100%

Feature Branch:
Type of work:
Research
Blueprint:

Starter:
1
Affected tool:
Deliverable for:

Description

We need multiple PGP/MIME attachments for the parent ticket.


Subtasks


History

#1 Updated by alant 2013-09-24 04:03:59

  • Assignee set to alant

https://pypi.python.org/pypi/pgp-mime/ seems the only candidate. It is not in Debian yet. Next step is to review it.

#2 Updated by alant 2014-08-16 21:15:28

> https://pypi.python.org/pypi/pgp-mime/ seems the only candidate. It is not in Debian yet. Next step is to review it.

Looks unmaintained, but doing PGP/MIME with email.mime which is in the standard library seems easy (http://www.physics.drexel.edu/~wking/code/python/send_pgp_mime):


        enc = MIMEApplication(_data=encrypted, _subtype='octet-stream',
                              _encoder=encode_7or8bit)
        enc.set_charset('us-ascii')

        control = MIMEApplication(_data='Version: 1\n',
                                  _subtype='pgp-encrypted',
                                  _encoder=encode_7or8bit)

        msg = MIMEMultipart('encrypted', micalg='pgp-sha1',
                            protocol='application/pgp-encrypted')
        msg.attach(control)
        msg.attach(enc)

        msg['Content-Disposition'] = 'inline'

#3 Updated by alant 2015-01-02 00:01:43

  • % Done changed from 0 to 20

The following works:

#!/usr/bin/python

import email.mime.application
import email.mime.multipart
import email.mime.text
import email.encoders

import gnupg

text="Test"
keyid="alan@boum.org"

def pgp_mime(message, recepients):
    encrypted_content = gpg.encrypt(message.as_string(), recepients)

    enc = email.mime.application.MIMEApplication(
            _data=str(encrypted_content),
            _subtype='octet-stream; name="encrypted.asc"',
            _encoder=email.encoders.encode_7or8bit)
    enc['Content-Description'] = 'OpenPGP encrypted message'
    enc.set_charset('us-ascii')

    control = email.mime.application.MIMEApplication(
            _data='Version: 1\n',
            _subtype='pgp-encrypted',
            _encoder=email.encoders.encode_7or8bit)
    control.set_charset('us-ascii')

    encmsg = email.mime.multipart.MIMEMultipart(
            'encrypted',
            protocol='application/pgp-encrypted')
    encmsg.attach(control)
    encmsg.attach(enc)
    encmsg['Content-Disposition'] = 'inline'

    return encmsg

gpg = gnupg.GPG()
message = email.mime.text.MIMEText(_text=text)
encrypted_message = pgp_mime(message, keyid)
print(encrypted_message.as_string())

#4 Updated by alant 2015-01-02 00:03:34

The result should conform to https://tools.ietf.org/html/rfc3156.

#5 Updated by alant 2015-01-02 01:22:39

  • Status changed from Confirmed to Resolved
  • % Done changed from 20 to 100

The above code works as looks simple enough to maintain ourselves.