Compare commits

..

No commits in common. "43ca0e5035b6951297715a211b73d8db5f751f15" and "2d3fd3317bbb0f002ba484bf63814ef899d55ef5" have entirely different histories.

4 changed files with 15 additions and 1276 deletions

View File

@ -2,8 +2,8 @@ pdf-simple-sign
=============== ===============
:compact-option: :compact-option:
'pdf-simple-sign' is a simple PDF signer intended for documents produced by 'pdf-simple-sign' is a simple open source PDF signer intended for documents
the Cairo library. As such, it currently comes with some restrictions: generated by Cairo. As such, it currently comes with some restrictions:
* the document may not have any forms or signatures already, as they will be * the document may not have any forms or signatures already, as they will be
overwitten overwitten
@ -30,10 +30,6 @@ Runtime dependencies: libcrypto (OpenSSL 1.1 API)
$ cd builddir $ cd builddir
$ ninja $ ninja
In addition to the C++ version, also included is a native Go port:
$ go get janouch.name/pdf-simple-sign/cmd/pdf-simple-sign
Usage Usage
----- -----

View File

@ -1,72 +0,0 @@
//
// Copyright (c) 2018, Přemysl Janouch <p@janouch.name>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// pdf-simple-sign is a simple PDF signer.
package main
import (
"flag"
"fmt"
"io/ioutil"
"janouch.name/pdf-simple-sign/pdf"
"os"
)
// #include <unistd.h>
import "C"
func isatty(fd uintptr) bool { return C.isatty(C.int(fd)) != 0 }
func die(status int, format string, args ...interface{}) {
msg := fmt.Sprintf(format+"\n", args...)
if isatty(os.Stderr.Fd()) {
msg = "\x1b[0;31m" + msg + "\x1b[m"
}
os.Stderr.WriteString(msg)
os.Exit(status)
}
func usage() {
die(1, "Usage: %s [-h] INPUT-FILENAME OUTPUT-FILENAME "+
"PKCS12-PATH PKCS12-PASS", os.Args[0])
}
func main() {
flag.Usage = usage
flag.Parse()
if flag.NArg() != 4 {
usage()
}
inputPath, outputPath := flag.Arg(0), flag.Arg(1)
pdfDocument, err := ioutil.ReadFile(inputPath)
if err != nil {
die(1, "%s", err)
}
p12, err := ioutil.ReadFile(flag.Arg(2))
if err != nil {
die(2, "%s", err)
}
key, certs, err := pdf.PKCS12Parse(p12, flag.Arg(3))
if err != nil {
die(3, "%s", err)
}
if pdfDocument, err = pdf.Sign(pdfDocument, key, certs); err != nil {
die(2, "error: %s", err)
}
if err = ioutil.WriteFile(outputPath, pdfDocument, 0666); err != nil {
die(3, "%s", err)
}
}

View File

@ -136,11 +136,12 @@ struct pdf_lexer {
if (eat_newline(ch)) if (eat_newline(ch))
continue; continue;
std::string octal; std::string octal;
if (ch && strchr(oct_alphabet, ch)) { if (*p && strchr(oct_alphabet, *p)) octal += *p++;
octal += ch; if (*p && strchr(oct_alphabet, *p)) octal += *p++;
if (*p && strchr(oct_alphabet, *p)) octal += *p++; if (*p && strchr(oct_alphabet, *p)) octal += *p++;
if (*p && strchr(oct_alphabet, *p)) octal += *p++; if (!octal.empty()) {
ch = std::stoi(octal, nullptr, 8); value += char(std::stoi(octal, nullptr, 8));
continue;
} }
} }
} }
@ -161,7 +162,6 @@ struct pdf_lexer {
buf.clear(); buf.clear();
} }
} }
p++;
if (!buf.empty()) value += char(std::stoi(buf + '0', nullptr, 16)); if (!buf.empty()) value += char(std::stoi(buf + '0', nullptr, 16));
return {pdf_object::STRING, value}; return {pdf_object::STRING, value};
} }
@ -257,7 +257,7 @@ struct pdf_lexer {
static std::string pdf_serialize(const pdf_object& o) { static std::string pdf_serialize(const pdf_object& o) {
switch (o.type) { switch (o.type) {
case pdf_object::NL: return "\n"; case pdf_object::NL: return "\n";
case pdf_object::NIL: return "null"; case pdf_object::NIL: return "nil";
case pdf_object::BOOL: return o.number ? "true" : "false"; case pdf_object::BOOL: return o.number ? "true" : "false";
case pdf_object::NUMERIC: case pdf_object::NUMERIC:
{ {
@ -301,7 +301,6 @@ static std::string pdf_serialize(const pdf_object& o) {
{ {
std::string s; std::string s;
for (const auto i : o.dict) for (const auto i : o.dict)
// FIXME the key is also supposed to be escaped by pdf_serialize()
s += " /" + i.first + " " + pdf_serialize(i.second); s += " /" + i.first + " " + pdf_serialize(i.second);
return "<<" + s + " >>"; return "<<" + s + " >>";
} }
@ -354,12 +353,6 @@ public:
// ------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------
/// If the object is an error, forward its message, otherwise return err.
static std::string pdf_error(const pdf_object& o, const char* err) {
if (o.type != pdf_object::END || o.string.empty()) return err;
return o.string;
}
pdf_object pdf_updater::parse_obj(pdf_lexer& lex, std::vector<pdf_object>& stack) const { pdf_object pdf_updater::parse_obj(pdf_lexer& lex, std::vector<pdf_object>& stack) const {
if (stack.size() < 2) if (stack.size() < 2)
return {pdf_object::END, "missing object ID pair"}; return {pdf_object::END, "missing object ID pair"};
@ -377,7 +370,7 @@ pdf_object pdf_updater::parse_obj(pdf_lexer& lex, std::vector<pdf_object>& stack
while (1) { while (1) {
auto object = parse(lex, obj.array); auto object = parse(lex, obj.array);
if (object.type == pdf_object::END) if (object.type == pdf_object::END)
return {pdf_object::END, pdf_error(object, "object doesn't end")}; return {pdf_object::END, "object doesn't end"};
if (object.type == pdf_object::KEYWORD && object.string == "endobj") if (object.type == pdf_object::KEYWORD && object.string == "endobj")
break; break;
obj.array.push_back(std::move(object)); obj.array.push_back(std::move(object));
@ -415,7 +408,7 @@ pdf_object pdf_updater::parse(pdf_lexer& lex, std::vector<pdf_object>& stack) co
while (1) { while (1) {
auto object = parse(lex, array); auto object = parse(lex, array);
if (object.type == pdf_object::END) if (object.type == pdf_object::END)
return {pdf_object::END, pdf_error(object, "array doesn't end")}; return {pdf_object::END, "array doesn't end"};
if (object.type == pdf_object::E_ARRAY) if (object.type == pdf_object::E_ARRAY)
break; break;
array.push_back(std::move(object)); array.push_back(std::move(object));
@ -428,7 +421,7 @@ pdf_object pdf_updater::parse(pdf_lexer& lex, std::vector<pdf_object>& stack) co
while (1) { while (1) {
auto object = parse(lex, array); auto object = parse(lex, array);
if (object.type == pdf_object::END) if (object.type == pdf_object::END)
return {pdf_object::END, pdf_error(object, "dictionary doesn't end")}; return {pdf_object::END, "dictionary doesn't end"};
if (object.type == pdf_object::E_DICT) if (object.type == pdf_object::E_DICT)
break; break;
array.push_back(std::move(object)); array.push_back(std::move(object));
@ -466,7 +459,7 @@ std::string pdf_updater::load_xref(pdf_lexer& lex, std::set<uint>& loaded_entrie
while (1) { while (1) {
auto object = parse(lex, throwaway_stack); auto object = parse(lex, throwaway_stack);
if (object.type == pdf_object::END) if (object.type == pdf_object::END)
return pdf_error(object, "unexpected EOF while looking for the trailer"); return "unexpected EOF while looking for the trailer";
if (object.type == pdf_object::KEYWORD && object.string == "trailer") if (object.type == pdf_object::KEYWORD && object.string == "trailer")
break; break;
@ -536,7 +529,7 @@ std::string pdf_updater::initialize() {
auto trailer = parse(lex, throwaway_stack); auto trailer = parse(lex, throwaway_stack);
if (trailer.type != pdf_object::DICT) if (trailer.type != pdf_object::DICT)
return pdf_error(trailer, "invalid trailer dictionary"); return "invalid trailer dictionary";
if (loaded_xrefs.empty()) if (loaded_xrefs.empty())
this->trailer = trailer.dict; this->trailer = trailer.dict;
loaded_xrefs.insert(xref_offset); loaded_xrefs.insert(xref_offset);
@ -544,7 +537,6 @@ std::string pdf_updater::initialize() {
const auto prev_offset = trailer.dict.find("Prev"); const auto prev_offset = trailer.dict.find("Prev");
if (prev_offset == trailer.dict.end()) if (prev_offset == trailer.dict.end())
break; break;
// FIXME we don't check for size_t over or underflow
if (!prev_offset->second.is_integer()) if (!prev_offset->second.is_integer())
return "invalid Prev offset"; return "invalid Prev offset";
xref_offset = prev_offset->second.number; xref_offset = prev_offset->second.number;
@ -564,7 +556,7 @@ pdf_object pdf_updater::get(uint n, uint generation) const {
if (n >= xref_size) if (n >= xref_size)
return {pdf_object::NIL}; return {pdf_object::NIL};
const auto& ref = xref[n]; auto& ref = xref[n];
if (ref.free || ref.generation != generation || ref.offset >= document.length()) if (ref.free || ref.generation != generation || ref.offset >= document.length())
return {pdf_object::NIL}; return {pdf_object::NIL};

1177
pdf/pdf.go

File diff suppressed because it is too large Load Diff