PdfDraw: developing PdfSigning

git-svn-id: svn://ultimatepp.org/upp/trunk@10351 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
cxl 2016-10-22 18:06:08 +00:00
parent d13877b76a
commit 40e42edd96
2 changed files with 87 additions and 6 deletions

View file

@ -2,6 +2,7 @@
namespace Upp {
#if 1
String GetP7Signature_imp(const void *data, int length, const String& cert_pem, const String& pkey_pem)
{
SslCertificate x509;
@ -14,7 +15,7 @@ String GetP7Signature_imp(const void *data, int length, const String& cert_pem,
SslStream in;
in.OpenBuffer((const char *)data, length);
PKCS7 *p7 = PKCS7_sign(x509, pkey, NULL, in, PKCS7_DETACHED|PKCS7_BINARY);
PKCS7 *p7 = PKCS7_sign(x509, pkey, NULL, in, PKCS7_DETACHED|PKCS7_BINARY|PKCS7_STREAM);
if (!p7)
return Null;
@ -23,7 +24,7 @@ String GetP7Signature_imp(const void *data, int length, const String& cert_pem,
out.CreateBuffer();
String r;
if (SMIME_write_PKCS7(out, p7, in, PKCS7_DETACHED|PKCS7_BINARY)) {
if (SMIME_write_PKCS7(out, p7, in, PKCS7_DETACHED|PKCS7_BINARY|PKCS7_STREAM)) {
SslStream out1;
out1.CreateBuffer();
i2d_PKCS7_bio(out1, p7);
@ -35,6 +36,64 @@ String GetP7Signature_imp(const void *data, int length, const String& cert_pem,
return r;
}
#else
String GetP7Signature_imp(const void *data, int length, const String& cert_pem, const String& pkey_pem)
{
SslCertificate x509;
SslKey pkey;
if(!x509.Load(cert_pem))
return Null;
if(!pkey.Load(pkey_pem))
return Null;
DDUMP(length);
String infilename = GetHomeDirFile("p7.in");
String outfilename = GetHomeDirFile("p7.out");
{ FileOut fo(infilename); fo.Put(data, length); }
BIO *infile = BIO_new_file(infilename, "r");
BIO *outfile = BIO_new_file(outfilename, "w");
int flags = PKCS7_DETACHED|PKCS7_BINARY|PKCS7_STREAM;
PKCS7 *p7 = PKCS7_sign(x509, pkey, NULL, infile, flags);
if (!p7)
return Null;
SslStream out;
out.CreateBuffer();
String r;
if(SMIME_write_PKCS7(outfile, p7, infile, flags)) {
BIO_free(outfile);
String s = LoadFile(outfilename);
DDUMP(s);
int p1 = s.FindAfter("Content-Disposition: attachment; filename=\"smime.p7s\"");
if(p1 >= 0) {
int p2 = s.Find("----", p1);
if(p2 >= 0) {
DDUMP(p1);
DDUMP(p2);
DDUMP(s.Mid(p1, p2 - p1));
r = Base64Decode(s.Mid(p1, p2 - p1));
DDUMP(r.GetCount());
}
}
}
else
BIO_free(outfile);
PKCS7_free(p7);
BIO_free(infile);
return r;
}
#endif
extern String (*GetP7Signature__)(const void *data, int length, const String& cert_pem, const String& pkey_pem);
INITBLOCK {

View file

@ -36,7 +36,7 @@ void PdfDraw::Clear()
out.Clear();
page.Clear();
offset.Clear();
out << "%PDF-1.3\n";
out << "%PDF-1.7\n";
out << "%\xf1\xf2\xf3\xf4\n\n";
empty = true;
}
@ -976,20 +976,30 @@ String PdfDraw::Finish(PdfSignatureInfo *sign)
}
static String byterange_filler = "[0 ********** ********** **********]";
int signature = -1;
int signature_widget = -1;
int p7s_start, p7s_end, byterange_pos;
int sign_page = 0;
if(sign) {
int signature = BeginObj();
signature = BeginObj();
out << "<< /Type /Sig\n";
out << "/ByteRange ";
byterange_pos = out.GetCount();
out << byterange_filler << "\n";
/*
out << "/Contents <";
p7s_start = out.GetCount();
out << String('0', 10000);
p7s_end = out.GetCount();
out << ">\n";
*/
out << "/Contents ";
p7s_start = out.GetCount();
out << "<" + String('0', 10000) + ">";
p7s_end = out.GetCount();
out << "\n";
// out << "/ByteRange [0 " << p7s_start << ' ' << p7s_end << ' ';
// pdf_length_pos = out.GetCount();
// //1234567890 - %10d
@ -1144,7 +1154,19 @@ String PdfDraw::Finish(PdfSignatureInfo *sign)
out << "/Metadata " << pdfa_metadata << " 0 R\n";
if(sign)
out << " /AcroForm << /Fields [" << signature_widget << " 0 R] /SigFlags 3 >>";
out << " /AcroForm << /Fields [" << signature_widget << " 0 R] /SigFlags 3 "
" /Perms << /DocMDP " << signature << " 0 R >>>>";
/*
17 0 obj
<< /Type /Catalog /Version /1.7 /Pages 1 0 R /Names << >>
/ViewerPreferences << /Direction /L2R >> /PageLayout /SinglePage /PageMode /UseNone
/OpenAction [12 0 R /FitH null] /Metadata 16 0 R /Lang (\Uffffffff\Uffffffffen)
/AcroForm << /Fields [4 0 R 10 0 R] /NeedAppearances false /SigFlags 3 /DR << /Font << /F1 3 0 R >> >> /DA (/F1 0 Tf 0 g)
/Q 0 >> /Perms << /DocMDP 5 0 R >> >>
endobj
*/
out << ">>\n";
EndObj();
@ -1182,7 +1204,7 @@ String PdfDraw::Finish(PdfSignatureInfo *sign)
DDUMP(byterange);
DDUMP(out.GetCount());
DDUMP(data.GetCount());
String sgn = HexString(GetP7Signature(data, sign->cert, sign->pkey));
String sgn = "<" + HexString(GetP7Signature(data, sign->cert, sign->pkey));
memcpy(~out + p7s_start, sgn, sgn.GetCount());
}