| 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2011 by Dominik Seichter * |
| 3 | * [email protected] * |
| 4 | * by Petr Pytelka * |
| 5 | * * |
| 6 | * This program is free software; you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU Library General Public License as * |
| 8 | * published by the Free Software Foundation; either version 2 of the * |
| 9 | * License, or (at your option) any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU Library General Public * |
| 17 | * License along with this program; if not, write to the * |
| 18 | * Free Software Foundation, Inc., * |
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 20 | ***************************************************************************/ |
| 21 | |
| 22 | #include <podofo.h> |
| 23 | |
| 24 | #include "../PdfTest.h" |
| 25 | |
| 26 | #include "SignatureGenerator.h" |
| 27 | #include "SimpleSignatureGenerator.h" |
| 28 | //#include "NSSSignatureGenerator.h" |
| 29 | |
| 30 | //#include <cert.h> |
| 31 | //#include <certt.h> |
| 32 | |
| 33 | #include <iostream> |
| 34 | #include <cstdio> |
| 35 | #include <fstream> |
| 36 | |
| 37 | using namespace PoDoFo; |
| 38 | |
| 39 | #define CONVERSION_CONSTANT 0.002834645669291339 |
| 40 | /* |
| 41 | static CERTCertificate* read_cert() |
| 42 | { |
| 43 | CERTCertificate* pCert = NULL; |
| 44 | SECStatus status; |
| 45 | CERTCertDBHandle* pDbHandle; |
| 46 | const char* pszDbName = "/home/dominik/Desktop/Documents"; |
| 47 | / * |
| 48 | status = CERT_InitCertDB( pDbHandle ); |
| 49 | printf("Init database: %i\n", status ); |
| 50 | |
| 51 | status = CERT_OpenCertDBFilename( pDbHandle, |
| 52 | strdup(pszDbName), 0 ); |
| 53 | printf("Open database: %i\n", status ); |
| 54 | */ |
| 55 | /* |
| 56 | pCert = CERT_FindCertByName ( |
| 57 | pDbHandle, "[email protected]" ); |
| 58 | * / |
| 59 | return pCert; |
| 60 | } |
| 61 | */ |
| 62 | void CreateSimpleForm( PdfPage* pPage, PdfStreamedDocument* pDoc, const PdfData &signatureData ) |
| 63 | { |
| 64 | PdfPainter painter; |
| 65 | PdfFont* pFont = pDoc->CreateFont( "Courier" ); |
| 66 | |
| 67 | painter.SetPage( pPage ); |
| 68 | painter.SetFont( pFont ); |
| 69 | painter.DrawText( 10000 * CONVERSION_CONSTANT, 280000 * CONVERSION_CONSTANT, "PoDoFo Sign Test" ); |
| 70 | painter.FinishPage(); |
| 71 | |
| 72 | PdfSignatureField signField( pPage, PdfRect( 70000 * CONVERSION_CONSTANT, 10000 * CONVERSION_CONSTANT, |
| 73 | 50000 * CONVERSION_CONSTANT, 50000 * CONVERSION_CONSTANT ), pDoc ); |
| 74 | signField.SetFieldName("SignatureFieldName"); |
| 75 | signField.SetSignature(signatureData); |
| 76 | signField.SetSignatureReason("I agree"); |
| 77 | // Set time of signing |
| 78 | signField.SetSignatureDate( PdfDate() ); |
| 79 | } |
| 80 | |
| 81 | |
| 82 | int main( int argc, char* argv[] ) |
| 83 | { |
| 84 | PdfPage* pPage; |
| 85 | |
| 86 | if( argc != 2 ) |
| 87 | { |
| 88 | printf("Usage: SignTest [output_filename]\n"); |
| 89 | printf(" - Create a PDF ready to be signed\n"); |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | try { |
| 94 | PdfSignOutputDevice signer(argv[1]); |
| 95 | // Reserve space for signature |
| 96 | signer.SetSignatureSize(1024); |
| 97 | |
| 98 | PdfStreamedDocument writer( &signer, PoDoFo::ePdfVersion_1_5 ); |
| 99 | // Disable default appearance |
| 100 | writer.GetAcroForm(ePdfCreateObject, ePdfAcroFormDefaultAppearance_None); |
| 101 | |
| 102 | pPage = writer.CreatePage( PdfPage::CreateStandardPageSize( ePdfPageSize_A4 ) ); |
| 103 | TEST_SAFE_OP( CreateSimpleForm( pPage, &writer, *signer.GetSignatureBeacon() ) ); |
| 104 | |
| 105 | TEST_SAFE_OP( writer.Close() ); |
| 106 | |
| 107 | // Check if position of signature was found |
| 108 | if(signer.HasSignaturePosition()) { |
| 109 | // Adjust ByteRange for signature |
| 110 | signer.AdjustByteRange(); |
| 111 | |
| 112 | // Read data for signature and count it |
| 113 | // We have to seek at the beginning of the file |
| 114 | signer.Seek(0); |
| 115 | |
| 116 | // Generate digest and count signature |
| 117 | // use NSS, MS Crypto API or OpenSSL |
| 118 | // to generate signature in DER format |
| 119 | |
| 120 | // This is example of generation process |
| 121 | // with dummy generator. Check example for |
| 122 | // NSS generator |
| 123 | /* |
| 124 | SimpleSignatureGenerator sg; |
| 125 | |
| 126 | // Read data to be signed and send them to the |
| 127 | // signature generator |
| 128 | char buff[65536]; |
| 129 | size_t len; |
| 130 | while( (len = signer.ReadForSignature(buff, 65536))>0 ) |
| 131 | { |
| 132 | sg.appendData(buff, len); |
| 133 | } |
| 134 | sg.finishData(); |
| 135 | |
| 136 | // Paste signature to the file |
| 137 | const PdfData *pSignature = sg.getSignature(); |
| 138 | */ |
| 139 | /* |
| 140 | CERTCertificate* pCert = read_cert(); |
| 141 | NSSSignatureGenerator ng(pCert); |
| 142 | char buff[65536]; |
| 143 | size_t len; |
| 144 | while( (len = signer.ReadForSignature(buff, 65536))>0 ) |
| 145 | { |
| 146 | ng.appendData(buff, len); |
| 147 | } |
| 148 | ng.finishData(); |
| 149 | |
| 150 | // Paste signature to the file |
| 151 | const PdfData *pSignature = ng.getSignature(); |
| 152 | |
| 153 | CERT_DestroyCertificate(pCert); |
| 154 | |
| 155 | if(pSignature!=NULL) { |
| 156 | signer.SetSignature(*pSignature); |
| 157 | } |
| 158 | */ |
| 159 | } |
| 160 | |
| 161 | signer.Flush(); |
| 162 | } catch( PdfError & e ) { |
| 163 | std::cerr << "Error: An error " << e.GetError() << " ocurred." << std::endl; |
| 164 | e.PrintErrorMsg(); |
| 165 | return e.GetError(); |
| 166 | } |
| 167 | |
| 168 | return 0; |
| 169 | } |