~mcepl/m2crypto#108: 
how to use pkcs7_sign

Migrated from: https://gitlab.com/m2crypto/m2crypto/-/issues/108
Created by: vinsia (@vinsia)
Created at: 2016-03-13T09:38:54.210Z
Closed at: 2023-02-03T10:40:55.955Z
Milestone: 0.39

here is my code

from OpenSSL.crypto import load_pkcs12, FILETYPE_PEM, dump_privatekey, dump_certificate
from M2Crypto import BIO, SMIME, X509, EVP
from M2Crypto.SMIME import PKCS7_BINARY, PKCS7_NOATTR, PKCS7_NOSIGS

def sign(data):
    p12 = load_pkcs12(open(PFX_FILE, "r").read(), PASSWORD)
    certificate = dump_certificate(FILETYPE_PEM, p12.get_certificate())
    privatekey = dump_privatekey(FILETYPE_PEM, p12.get_privatekey())
    buf = BIO.MemoryBuffer(data)

    signer = SMIME.SMIME()
    signer.pkey = EVP.load_key_string(privatekey)
    signer.x509 = X509.load_cert_string(certificate)

    p7 = signer.sign(buf, flags=PKCS7_NOATTR | PKCS7_BINARY | PKCS7_NOSIGS)

    buf = BIO.MemoryBuffer(data)
    out = BIO.MemoryBuffer()
    signer.write(out, p7, buf)
    result = out.read()

    print result
    return result

it did not sign on the message

Status
RESOLVED FIXED
Submitter
~mcepl
Assigned to
No-one
Submitted
9 months ago
Updated
9 months ago
Labels
No labels applied.

~mcepl 9 months ago

On 2016-03-13T20:42:54.844Z, Matěj Cepl wrote:

OK, what did you expect to happen? What did actually happen? Any error messages?

~mcepl 9 months ago

Changed on 2016-03-13T20:43:02.780Z by Matěj Cepl:

Added needinfo label

~mcepl 9 months ago

On 2016-03-14T01:47:47.373Z, vinsia wrote:

no error message, but i found that it lacks of the base64 segement while i sign the data.

~mcepl 9 months ago

On 2016-03-14T01:47:58.654Z, vinsia wrote:

From: sender@example.dom To: recipient@example.dom Subject: M2Crypto S/MIME testing MIME-Version: 1.0 Content-Type: multipart/signed; protocol="application/x-pkcs7-signature"; micalg="sha1"; boundary="----832C81E0C7CCE010273CB2FEA8ACD965"

This is an S/MIME signed message

------832C81E0C7CCE010273CB2FEA8ACD965 a sign of our times

and that's my sign result

(Last edited at 2016-03-14T01:48:38.594Z.)

~mcepl 9 months ago

On 2016-03-14T02:10:28.531Z, vinsia wrote:

#!/usr/local/bin/python

from M2Crypto import BIO, Rand, SMIME

def makebuf(text):
    return BIO.MemoryBuffer(text)

buf = makebuf('a sign of our times')
Rand.load_file('randpool.dat', -1)

s = SMIME.SMIME()
s.load_key('signer_key.pem', 'signer.pem')
p7 = s.sign(buf)
buf = makebuf('a sign of our times')

out = BIO.MemoryBuffer()
out.write('From: sender@example.dom\n')
out.write('To: recipient@example.dom\n')
out.write('Subject: M2Crypto S/MIME testing\n')
s.write(out, p7, buf)
print out.read()

Rand.save_file('randpool.dat')

this'my sign code

(Last edited at 2016-03-14T10:55:27.469Z.)

~mcepl 9 months ago

Changed on 2016-03-14T10:51:33.579Z by Matěj Cepl:

Removed needinfo label

~mcepl 9 months ago

Changed on 2016-03-20T21:59:12.710Z by Matěj Cepl:

Milestone changed to 0.25.0

~mcepl 9 months ago

Changed on 2016-07-12T19:15:49.321Z by Matěj Cepl:

Milestone changed to 0.28

~mcepl 9 months ago

On 2017-03-08T06:12:36.866Z, Matheus Bratfisch wrote:

Hello @vinsia

I had the same problem to solve it I had to use:

s.sign(buf, SMIME.PKCS7_DETACHED)

Check #164 I hope this is useful :)

~mcepl 9 months ago

Changed on 2017-10-05T23:21:25.654Z by Matěj Cepl:

changed milestone to 0.29

~mcepl 9 months ago

Changed on 2018-02-23T21:19:20.605Z by Matěj Cepl:

removed milestone

~mcepl 9 months ago

Changed on 2023-02-03T10:40:55.829Z by Matěj Cepl:

marked this issue as a duplicate of #164

(Last edited at 2023-02-03T10:40:55.831Z.)

~mcepl 9 months ago

Changed on 2023-02-03T10:40:56.174Z by Matěj Cepl:

marked this issue as related to #164

(Last edited at 2023-02-03T10:40:56.176Z.)

~mcepl 9 months ago

Changed on 2023-02-03T10:40:56.351Z by Matěj Cepl:

changed the description

(Last edited at 2023-02-03T10:40:56.353Z.)

~mcepl referenced this from #164 9 months ago

~mcepl referenced this from #164 9 months ago

~mcepl referenced this from #164 9 months ago

~mcepl REPORTED FIXED 9 months ago

Register here or Log in to comment, or comment via email.