22 #include "BDocException.h"
35 #include "crypto/signer/SignException.h"
36 #include "SignatureException.h"
37 #include "io/IOException.h"
40 #include "xml/OpenDocument_manifest.hxx"
42 #include <openssl/objects.h>
43 #include <openssl/pem.h>
44 #include <openssl/ssl.h>
48 #include <xercesc/parsers/XercesDOMParser.hpp>
49 #include <xercesc/dom/DOM.hpp>
50 #include <xercesc/util/XMLString.hpp>
51 #include <xercesc/util/PlatformUtils.hpp>
58 #include <xsec/canon/XSECC14n20010315.hpp>
59 #include <xsec/utils/XSECPlatformUtils.hpp>
61 using namespace digidoc;
65 int testUtil(
int argc,
char* argv[]);
67 int testBDOC(
int argc,
char* argv[]);
68 int testOCSP(
int argc,
char* argv[]);
74 int testC14N(
int argc,
char* argv[]);
109 int main(
int argc,
char* argv[])
168 ERR(
"Caught BDocException: %s", e.
getMsg().c_str());
172 ERR(
"Caught SignException: %s", e.
getMsg().c_str());
176 ERR(
"Caught OCSPException: %s", e.
getMsg().c_str());
180 ERR(
"Caught IOException: %s", e.
getMsg().c_str());
184 ERR(
"Caught Exception: %s", e.
getMsg().c_str());
188 ERR(
"Caught unknown exception");
223 ERR(
"Caught IOException: %s", e.
getMsg().c_str());
396 #include <openssl/ssl.h>
397 #include <openssl/conf.h>
411 const char *
pass, ENGINE *
e,
const char *cert_descrip)
416 STACK_OF(X509_INFO) *allcerts = NULL;
423 if((certs = BIO_new(BIO_s_file())) == NULL)
433 if (BIO_read_filename(certs,file) <= 0)
435 BIO_printf(err,
"Error opening %s %s\n",
444 othercerts = sk_X509_new_null();
447 sk_X509_free(othercerts);
451 allcerts = PEM_X509_INFO_read_bio(certs, NULL,
453 for(i = 0; i < sk_X509_INFO_num(allcerts); i++)
455 xi = sk_X509_INFO_value (allcerts, i);
458 sk_X509_push(othercerts, xi->x509);
465 BIO_printf(err,
"bad input format specified for %s\n",
470 if (othercerts == NULL)
472 BIO_printf(err,
"unable to load certificates\n");
475 if (allcerts) sk_X509_INFO_pop_free(allcerts, X509_INFO_free);
476 if (certs != NULL) BIO_free(certs);
500 FILE* pCertFile = fopen(
"etc/certs/mari-liis.mannik.pem",
"r");
501 X509* cert = PEM_read_X509(pCertFile, NULL, NULL, NULL);
505 FILE* pIssuerFile = fopen(
"etc/certs/sk-test.pem",
"r");
506 X509* issuer = PEM_read_X509(pIssuerFile, NULL, NULL, NULL);
517 digidoc::OCSP ocsp(
"http://www.openxades.org/cgi-bin/ocsp.cgi");
519 std::vector<unsigned char> nonce(20);
521 std::vector<unsigned char> ocspResponseDER;
531 ERR(
"Caught OCSPException: %s", e.
getMsg().c_str());
535 ERR(
"Caught IOException: %s", e.
getMsg().c_str());
539 ERR(
"Caught Exception: %s", e.
getMsg().c_str());
543 ERR(
"Caught unknown exception");
552 INFO(
"selectSignCertificate(certificates.size() = %d)", certificates.size());
554 for(std::vector<digidoc::PKCS11Signer::PKCS11Cert>::const_iterator iter = certificates.begin(); iter != certificates.end(); iter++)
556 DEBUG(
"token label: %s", iter->token.label.c_str());
557 DEBUG(
"token manufacturer: %s", iter->token.manufacturer.c_str());
558 DEBUG(
"token model: %s", iter->token.model.c_str());
559 DEBUG(
"token serial Nr: %s", iter->token.serialNr.c_str());
560 DEBUG(
"label: %s", iter->label.c_str());
561 DEBUG(
"cert: 0x%X", (
unsigned int)(iter->cert));
562 DEBUG(
"--------------------------------------------------------------------");
565 return certificates[0].cert;
570 INFO(
"getPin(certificate = { token = {label = '%s'}})", certificate.
token.
label.c_str());
598 memset(signature.signature, 0, signature.length);
602 DEBUG(
"----------------------------------------------------------------------------------------------------");
603 DEBUG(
"Digest: type = %d, digest = %s, length = %d", digest.
type, (
char*)digest.
digest, digest.
length);
604 DEBUG(
"Signature: signature = 0x%X, length = %d", (
unsigned int)signature.signature, signature.length);
607 DEBUG(
"signature.signature[100] = %u", (signature.signature)[100]);
608 DEBUG(
"signature.signature[127] = %u", (signature.signature)[127]);
609 DEBUG(
"signature.signature[128] = %u", (signature.signature)[128]);
613 ERR(
"Caught SignException: %s", e.
getMsg().c_str());
617 ERR(
"Caught Exception: %s", e.
getMsg().c_str());
621 ERR(
"Caught unknown exception");
633 int testSignBDoc(digidoc::Signature::Type signingType,
int argc,
char* argv[])
658 DEBUG(
"args: %s %s", argv[1], argv[2]);
668 bdoc.
sign(&signer, signingType);
680 ERR(
"Caught SignException: %s", e.
getMsg().c_str());
684 ERR(
"Caught Exception: %s", e.
getMsg().c_str());
688 ERR(
"Caught unknown exception");
696 return testSignBDoc(digidoc::Signature::BES, argc, argv);
701 return testSignBDoc(digidoc::Signature::TM, argc, argv);
712 const std::string fileName(
"/home/janari/projects/smartlink/digidoc/test/data/bdoc/BES.bdoc");
713 DEBUG(fileName.c_str());
720 std::auto_ptr<digidoc::BDoc> bdoc(
new digidoc::BDoc(serializer));
723 for (
size_t sig = 0; sig < sigCount; sig++ )
726 std::cout <<
"Signature[" << sig <<
"]:" << std::endl;
731 std::cout <<
"\tofflineValid[true]" << std::endl;
739 std::cout <<
"\tSignatureProductionPlace:" << std::endl;
740 std::cout <<
"\t\tcity[" << productionPlace.
city <<
"]" << std::endl;
741 std::cout <<
"\t\tstateOrProvince[" << productionPlace.
stateOrProvince <<
"]" << std::endl;
742 std::cout <<
"\t\tpostalCode[" << productionPlace.
postalCode <<
"]" << std::endl;
743 std::cout <<
"\t\tcountryName[" << productionPlace.
countryName <<
"]" << std::endl;
746 std::cout <<
"\tClaimedRoles:" << std::endl;
747 for ( digidoc::SignerRole::TRoles::const_iterator it = signerRole.
claimedRoles.begin()
751 std::cout <<
"\t\tClaimedRole[" << (*it) <<
"]" << std::endl;
754 std::cout <<
"\tSigningTime:" << std::endl;
755 std::cout <<
"\t\ttime[" << signature->
getSigningTime() <<
"]" << std::endl;
784 bdoc.
sign(&signer, digidoc::Signature::TM);
794 ERR(
"Caught SignException: %s", e.
getMsg().c_str());
798 ERR(
"Caught Exception: %s", e.
getMsg().c_str());
802 ERR(
"Caught unknown exception");
813 xercesc::XMLPlatformUtils::Initialize();
815 catch (
const xercesc::XMLException& toCatch) {
816 char* message = xercesc::XMLString::transcode(toCatch.getMessage());
817 std::cout <<
"Error during initialization! :\n"
819 xercesc::XMLString::release(&message);
823 xercesc::XercesDOMParser* parser =
new xercesc::XercesDOMParser();
824 parser->setValidationScheme(xercesc::XercesDOMParser::Val_Always);
825 parser->setDoNamespaces(
true);
832 char* xmlFile =
"tmp/javalib07/META-INF/signature1.xml";
834 xercesc::DOMDocument *doc = 0;
837 parser->parse(xmlFile);
838 std::cout <<
"Parsed" << std::endl;
839 doc = parser->getDocument();
840 std::cout <<
"Got document" << std::endl;
847 xercesc::DOMNodeList* list =doc->getElementsByTagNameNS(xercesc::XMLString::transcode(
"http://www.w3.org/2000/09/xmldsig#"),
848 xercesc::XMLString::transcode(
"SignedInfo"));
850 xercesc::DOMNode* node = list->item(0);
852 std::cout <<
"Got node" << std::endl;
855 XSECC14n20010315 canonicalizer(doc, node);
857 canonicalizer.setUseNamespaceStack(
true);
858 canonicalizer.setExclusive();
861 std::cout <<
"Got canon" << std::endl;
865 std::vector<unsigned char> c14n;
866 unsigned char buffer[1024];
868 std::cout <<
"Getting data" << std::endl;
869 while((bytes = canonicalizer.outputBuffer(buffer, 1024)) > 0)
872 for(
int i = 0; i < bytes; i++)
874 c14n.push_back(buffer[i]);
878 INFO(
"c14n = '%s'", std::string(reinterpret_cast<char*>(&c14n[0]), c14n.size()).c_str() );
884 std::vector<unsigned char> hash = sha1->getDigest();
886 DEBUGMEM(
"Digest", &hash[0], hash.size());
889 catch (
const xercesc::XMLException& toCatch) {
890 char* message = xercesc::XMLString::transcode(toCatch.getMessage());
891 std::cout <<
"Exception message is: \n"
893 xercesc::XMLString::release(&message);
896 catch (
const xercesc::DOMException& toCatch) {
897 char* message = xercesc::XMLString::transcode(toCatch.msg);
898 std::cout <<
"Exception message is: \n"
900 xercesc::XMLString::release(&message);
904 std::cout <<
"Unexpected Exception \n" ;
928 std::string fileName =
"/home/janari/projects/smartlink/digidoc/tmp/example_container/META-INF/manifest.xml";
929 xml_schema::Properties properties;
930 properties.schema_location(
"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0",
digidoc::Conf::getInstance()->getManifestXsdPath());
931 std::auto_ptr<digidoc::manifest::Manifest> manifest(digidoc::manifest::manifest(fileName, xml_schema::Flags::keep_dom, properties));
935 xercesc::DOMNode* pNode = manifest->_node();
959 XSECC14n20010315 canon(pNode->getOwnerDocument() );
960 canon.setCommentsProcessing(
true);
961 canon.setUseNamespaceStack(
true);
963 canon.setStartNode(manifest->file_entry()[0]._node());
967 int res = canon.outputBuffer((
unsigned char *) buffer, 128);
968 DEBUG(
"POS[41] :: res = %d", res);
973 res = canon.outputBuffer((
unsigned char *) buffer, 128);
976 std::cout << std::endl;
983 catch(
const xercesc::DOMException& e)
985 ERR(
"xercesc::DOMException: %s", XMLString::transcode(e.getMessage()));
987 catch(
const xercesc::XMLException& e)
989 ERR(
"xercesc::DOMException: %s", XMLString::transcode(e.getMessage()));
991 catch(
const xml_schema::Exception& e)
993 std::ostringstream oss;
995 ERR(
"xml_schema::Exception: %s", oss.str().c_str());
1010 std::string keyPath =
"/home/janari/projects/smartlink/digidoc/test/data/cert/cert+priv_key.pem";
1011 X509* signingCert =
X509Cert::loadX509(keyPath); X509_scope signingCertScope(&signingCert);
1014 RSASigner signer(signingCert, privateKey);
1021 memset(signature.signature, 0, signature.length);
1024 DEBUG(
"----------------------------------------------------------------------------------------------------");
1025 DEBUG(
"Digest: type = %d, digest = %s, length = %d", digest.
type, (
char*)digest.
digest, digest.
length);
1026 DEBUG(
"Signature: signature = 0x%X, length = %d", (
unsigned int)signature.signature, signature.length);
1028 DEBUGMEM(
"", signature.signature, signature.length);
1032 ERR(
"Caught SignException: %s", e.
getMsg().c_str());
1036 ERR(
"Caught IOException: %s", e.
getMsg().c_str());
1040 ERR(
"Caught Exception: %s", e.
getMsg().c_str());
1044 ERR(
"Caught unknown exception");
1055 std::cout <<
"\tCertificateInfo:" << std::endl;
1056 std::cout <<
"\t\tserial[" << cert.
getSerial() <<
"]" << std::endl;
1057 std::cout <<
"\t\tissuerName[" << cert.getIssuerName() <<
"]" << std::endl;
1062 std::string
indent(
size_t level,
char character =
' ')
1065 str.resize(level, character);
1070 , std::stringstream& addTo
1075 <<
"Exception[" << e.
getMsg() <<
"]"
1082 addTo <<
indent(depth + 1) <<
"Caused by:" << std::endl;
1083 for ( digidoc::Exception::Causes::const_iterator it = causedBy.begin()
1084 ; it != causedBy.end()
1095 std::stringstream
stream;
1097 std::cout << stream.str() << std::endl;
1118 std::vector<PKCS11Signer::PKCS11Cert> certificates)
throw(
SignException)
1121 DEBUG(
"Available certificates:\n");
1122 for(std::vector<PKCS11Signer::PKCS11Cert>::const_iterator iter = certificates.begin(); iter != certificates.end(); iter++)
1124 printPKCS11Cert(*iter);
1148 printf(
"Please enter PIN for token '%s' or <enter> to cancel: ", certificate.token.label.c_str());
1151 while(i < pinMax && (c = getch()) !=
'\r')
1158 snprintf(prompt,
sizeof(prompt),
"Please enter PIN for token '%s' or <enter> to cancel: ", certificate.token.label.c_str());
1159 char* p = getpass(prompt);
1160 strncpy(pin, p, pinMax);
1163 pin[pinMax-1] =
'\0';
1165 std::string result(pin);
1183 DEBUG(
"-------------------------------------------------------");