//Load public key from a certificate file into key manager xmlSecKeysMngrPtr load_key_from_certfile(xmlSecKeysMngrPtr* keys_manager, const char* certfile) { xmlSecKeysMngrPtr keys_mngr; if((keys_manager != NULL) && (*keys_manager != NULL)) keys_mngr = *keys_manager; else { keys_mngr = xmlSecKeysMngrCreate(); //initialize keys manager if (xmlSecCryptoAppDefaultKeysMngrInit(keys_mngr)<0) { std::cerr<<"Can not initialize xmlSecKeysMngr object"<<std::endl; xmlSecKeysMngrDestroy(keys_mngr); return NULL; } } if(keys_mngr == NULL) { std::cerr<<"Can not create xmlSecKeysMngr object"<<std::endl; return NULL;} std::string cert_str; cert_str = get_cert_str(certfile); xmlSecKeyPtr key = get_key_from_certstr(cert_str); if(xmlSecCryptoAppDefaultKeysMngrAdoptKey(keys_mngr, key) < 0) { std::cerr<<"Failed to add key from "<<certfile<<" to keys manager"<<std::endl; xmlSecKeyDestroy(key); xmlSecKeysMngrDestroy(keys_mngr); return NULL; } if(keys_manager != NULL) keys_manager = &keys_mngr; return keys_mngr; }
/** * load_des_keys: * @files: the list of filenames. * @files_size: the number of filenames in #files. * * Creates simple keys manager and load DES keys from #files in it. * The caller is responsible for destroing returned keys manager using * @xmlSecKeysMngrDestroy. * * Returns the pointer to newly created keys manager or NULL if an error * occurs. */ xmlSecKeysMngrPtr load_des_keys(char** files, int files_size) { xmlSecKeysMngrPtr mngr; xmlSecKeyPtr key; int i; assert(files); assert(files_size > 0); /* create and initialize keys manager, we use a simple list based * keys manager, implement your own xmlSecKeysStore klass if you need * something more sophisticated */ mngr = xmlSecKeysMngrCreate(); if(mngr == NULL) { fprintf(stderr, "Error: failed to create keys manager.\n"); return(NULL); } if(xmlSecCryptoAppDefaultKeysMngrInit(mngr) < 0) { fprintf(stderr, "Error: failed to initialize keys manager.\n"); xmlSecKeysMngrDestroy(mngr); return(NULL); } for(i = 0; i < files_size; ++i) { assert(files[i]); /* load DES key */ key = xmlSecKeyReadBinaryFile(xmlSecKeyDataDesId, files[i]); if(key == NULL) { fprintf(stderr,"Error: failed to load des key from binary file \"%s\"\n", files[i]); xmlSecKeysMngrDestroy(mngr); return(NULL); } /* set key name to the file name, this is just an example! */ if(xmlSecKeySetName(key, BAD_CAST files[i]) < 0) { fprintf(stderr,"Error: failed to set key name for key from \"%s\"\n", files[i]); xmlSecKeyDestroy(key); xmlSecKeysMngrDestroy(mngr); return(NULL); } /* add key to keys manager, from now on keys manager is responsible * for destroying key */ if(xmlSecCryptoAppDefaultKeysMngrAdoptKey(mngr, key) < 0) { fprintf(stderr,"Error: failed to add key from \"%s\" to keys manager\n", files[i]); xmlSecKeyDestroy(key); xmlSecKeysMngrDestroy(mngr); return(NULL); } } return(mngr); }
// --------------------------------------------------------------------------- // Destructor // --------------------------------------------------------------------------- // CXmlSecKeyManager::~CXmlSecKeyManager() { if(iKeyManager) { xmlSecKeysMngrDestroy(iKeyManager); } }
static xmlSecKeysMngrPtr getKeyManager(VALUE rb_certs) { int i, numCerts = RARRAY_LEN(rb_certs); xmlSecKeysMngrPtr keyManager = xmlSecKeysMngrCreate(); VALUE rb_cert; char *cert; unsigned int certLength; int numSuccessful = 0; if (keyManager == NULL) return NULL; if (xmlSecCryptoAppDefaultKeysMngrInit(keyManager) < 0) { rb_raise(rb_eKeystoreError, "could not initialize key manager"); xmlSecKeysMngrDestroy(keyManager); return NULL; } for (i = 0; i < numCerts; i++) { rb_cert = RARRAY_PTR(rb_certs)[i]; Check_Type(rb_cert, T_STRING); cert = RSTRING_PTR(rb_cert); certLength = RSTRING_LEN(rb_cert); if(xmlSecCryptoAppKeysMngrCertLoadMemory(keyManager, (xmlSecByte *)cert, certLength, xmlSecKeyDataFormatPem, xmlSecKeyDataTypeTrusted) < 0) { rb_warn("failed to load certificate at index %d", i); } else { numSuccessful++; } } // note, numCerts could be zero, meaning that we should use system SSL certs if (numSuccessful == 0 && numCerts != 0) { rb_raise(rb_eKeystoreError, "Could not load any of the specified certificates for signature verification"); xmlSecKeysMngrDestroy(keyManager); return NULL; } return keyManager; }
//Load trusted certificate from file xmlSecKeysMngrPtr load_trusted_cert_file(xmlSecKeysMngrPtr* keys_manager, const char* cert_file) { xmlSecKeysMngrPtr keys_mngr; if((keys_manager != NULL) && (*keys_manager != NULL)) keys_mngr = *keys_manager; else { keys_mngr = xmlSecKeysMngrCreate(); //initialize keys manager if (xmlSecCryptoAppDefaultKeysMngrInit(keys_mngr)<0) { std::cerr<<"Can not initialize xmlSecKeysMngr object"<<std::endl; xmlSecKeysMngrDestroy(keys_mngr); return NULL; } } if(keys_mngr == NULL) { std::cerr<<"Can not create xmlSecKeysMngr object"<<std::endl; return NULL;} //load cert from file if(cert_file && (strlen(cert_file) != 0)) if(xmlSecCryptoAppKeysMngrCertLoad(keys_mngr, cert_file, xmlSecKeyDataFormatPem, xmlSecKeyDataTypeTrusted) < 0) { xmlSecKeysMngrDestroy(keys_mngr); return NULL; } if(keys_manager != NULL) keys_manager = &keys_mngr; return keys_mngr; }
/** * create_files_keys_mngr: * * Creates a files based keys manager: we assume that key name is * the key file name, * * Returns pointer to newly created keys manager or NULL if an error occurs. */ xmlSecKeysMngrPtr create_files_keys_mngr(void) { xmlSecKeyStorePtr keysStore; xmlSecKeysMngrPtr mngr; /* create files based keys store */ keysStore = xmlSecKeyStoreCreate(files_keys_store_get_klass()); if(keysStore == NULL) { fprintf(stderr, "Error: failed to create keys store.\n"); return(NULL); } /* create keys manager */ mngr = xmlSecKeysMngrCreate(); if(mngr == NULL) { fprintf(stderr, "Error: failed to create keys manager.\n"); xmlSecKeyStoreDestroy(keysStore); return(NULL); } /* add store to keys manager, from now on keys manager destroys the store if needed */ if(xmlSecKeysMngrAdoptKeysStore(mngr, keysStore) < 0) { fprintf(stderr, "Error: failed to add keys store to keys manager.\n"); xmlSecKeyStoreDestroy(keysStore); xmlSecKeysMngrDestroy(mngr); return(NULL); } /* initialize crypto library specific data in keys manager */ if(xmlSecCryptoKeysMngrInit(mngr) < 0) { fprintf(stderr, "Error: failed to initialize crypto data in keys manager.\n"); xmlSecKeysMngrDestroy(mngr); return(NULL); } /* set the get key callback */ mngr->getKey = xmlSecKeysMngrGetKey; return(mngr); }
static xmlSecKeysMngrPtr getKeyManager(VALUE rb_hash) { xmlSecKeysMngrPtr keyManager = xmlSecKeysMngrCreate(); if (keyManager == NULL) return NULL; if (xmlSecCryptoAppDefaultKeysMngrInit(keyManager) < 0) { rb_raise(rb_eKeystoreError, "could not initialize key manager"); xmlSecKeysMngrDestroy(keyManager); return NULL; } rb_hash_foreach(rb_hash, addRubyKeyToManager, (VALUE)keyManager); return keyManager; }
VALUE verify_signature_with_certificates(VALUE self, VALUE rb_certs) { xmlDocPtr doc; xmlNodePtr node = NULL; xmlSecDSigCtxPtr dsigCtx = NULL; xmlSecKeysMngrPtr keyManager = NULL; VALUE result = Qfalse; Check_Type(rb_certs, T_ARRAY); Data_Get_Struct(self, xmlDoc, doc); // find start node node = xmlSecFindNode(xmlDocGetRootElement(doc), xmlSecNodeSignature, xmlSecDSigNs); if(node == NULL) { rb_raise(rb_eVerificationError, "start node not found"); goto done; } keyManager = getKeyManager(rb_certs); if (keyManager == NULL) { rb_raise(rb_eKeystoreError, "failed to create key manager"); goto done; } // create signature context, we don't need keys manager in this example dsigCtx = xmlSecDSigCtxCreate(keyManager); if(dsigCtx == NULL) { rb_raise(rb_eVerificationError, "failed to create signature context"); goto done; } // verify signature if(xmlSecDSigCtxVerify(dsigCtx, node) < 0) { rb_raise(rb_eVerificationError, "error occurred during signature verification"); goto done; } if(dsigCtx->status == xmlSecDSigStatusSucceeded) { result = Qtrue; } done: if(dsigCtx != NULL) { xmlSecDSigCtxDestroy(dsigCtx); } if (keyManager != NULL) { xmlSecKeysMngrDestroy(keyManager); } return result; }
/**Funkcja weryfikuje poprawność podpisu xades. Weryfikacja obejmuje obliczenia matematyczne * oraz weryfikację poszczególnych pól formualarza i certyfikatu. * \retval -109 Certyfikat poza terminem ważności. * */ long xades_verify(const char * buffer, const long buffer_len, GenBuf_t *timestamp) { xmlSecKeysMngrPtr mngr = NULL; long ret = 0; long status = 0; if (buffer == NULL) { PRINT_DEBUG("Wrong argument 1!\n"); return ERR_arg+1; } if (timestamp == NULL) { PRINT_DEBUG("Wrong argument 3!\n"); return ERR_arg+3; } /* Init libxml and libxslt libraries */ xades_init(); /* create keys manager and load trusted certificates */ mngr = xmlSecKeysMngrCreate(); if(mngr == NULL) { PRINT_DEBUG( "Error: failed to create keys manager.\n"); ret = -1; } if (mngr != NULL) { status = xmlSecCryptoAppDefaultKeysMngrInit(mngr); if(status < 0) { PRINT_DEBUG( "Error: failed to initialize keys manager: %li\n",status); ret = -2; } else { status = verify_memory(mngr,buffer, buffer_len); if (status != 0) { PRINT_ERROR("XAdES memory verification result: %li\n",status); ret = -4; } status = verify_fields(buffer, buffer_len, timestamp); if (status < 0) { PRINT_ERROR("XAdES tags verification result: %li\n", status); ret = -100+status; } } } xmlSecKeysMngrDestroy(mngr); xades_destroy(); return ret; }
//Load trusted cetificates into key manager xmlSecKeysMngrPtr load_trusted_certs(xmlSecKeysMngrPtr* keys_manager, const char* cafile, const char* capath) { xmlSecKeysMngrPtr keys_mngr; if((keys_manager != NULL) && (*keys_manager != NULL)) keys_mngr = *keys_manager; else { keys_mngr = xmlSecKeysMngrCreate(); //initialize keys manager if (xmlSecCryptoAppDefaultKeysMngrInit(keys_mngr)<0) { std::cerr<<"Can not initialize xmlSecKeysMngr object"<<std::endl; xmlSecKeysMngrDestroy(keys_mngr); return NULL; } } if(keys_mngr == NULL) { std::cerr<<"Can not create xmlSecKeysMngr object"<<std::endl; return NULL;} //load ca certs into keys manager, the two method used here could not work in some old xmlsec verion, //because of some bug about X509_FILETYPE_DEFAULT and X509_FILETYPE_PEM //load a ca path if(capath && (strlen(capath) != 0)) if(xmlSecOpenSSLAppKeysMngrAddCertsPath(keys_mngr, capath) < 0) { xmlSecKeysMngrDestroy(keys_mngr); return NULL; } #if 0 //load a ca file TODO: can only be used in some new version of xmlsec if(cafile && (strlen(cafile) != 0)) if(xmlSecOpenSSLAppKeysMngrAddCertsFile(keys_mngr, cafile) < 0) { xmlSecKeysMngrDestroy(keys_mngr); return NULL; } #endif if(cafile && (strlen(cafile) != 0)) if(xmlSecCryptoAppKeysMngrCertLoad(keys_mngr, cafile, xmlSecKeyDataFormatPem, xmlSecKeyDataTypeTrusted) < 0) { xmlSecKeysMngrDestroy(keys_mngr); return NULL; } if(keys_manager != NULL) keys_manager = &keys_mngr; return keys_mngr; }
//Could be used for many trusted certificates in string xmlSecKeysMngrPtr load_trusted_cert_str(xmlSecKeysMngrPtr* keys_manager, const std::string& cert_str) { xmlSecKeysMngrPtr keys_mngr; if((keys_manager != NULL) && (*keys_manager != NULL)) keys_mngr = *keys_manager; else { keys_mngr = xmlSecKeysMngrCreate(); //initialize keys manager if (xmlSecCryptoAppDefaultKeysMngrInit(keys_mngr)<0) { std::cerr<<"Can not initialize xmlSecKeysMngr object"<<std::endl; xmlSecKeysMngrDestroy(keys_mngr); return NULL; } } if(keys_mngr == NULL) { std::cerr<<"Can not create xmlSecKeysMngr object"<<std::endl; return NULL;} //load cert from memory if(!cert_str.empty()) if(xmlSecCryptoAppKeysMngrCertLoadMemory(keys_mngr, (const xmlSecByte*)(cert_str.c_str()), (xmlSecSize)(cert_str.size()), xmlSecKeyDataFormatPem, xmlSecKeyDataTypeTrusted) < 0) { xmlSecKeysMngrDestroy(keys_mngr); return NULL; } if(keys_manager != NULL) keys_manager = &keys_mngr; return keys_mngr; }
/** * load_trusted_certs: * @files: the list of filenames. * @files_size: the number of filenames in #files. * * Creates simple keys manager and load trusted certificates from PEM #files. * The caller is responsible for destroing returned keys manager using * @xmlSecKeysMngrDestroy. * * Returns the pointer to newly created keys manager or NULL if an error * occurs. */ xmlSecKeysMngrPtr load_trusted_certs(char** files, int files_size) { xmlSecKeysMngrPtr mngr; int i; assert(files); assert(files_size > 0); /* create and initialize keys manager, we use a simple list based * keys manager, implement your own xmlSecKeysStore klass if you need * something more sophisticated */ mngr = xmlSecKeysMngrCreate(); if(mngr == NULL) { fprintf(stderr, "Error: failed to create keys manager.\n"); return(NULL); } if(xmlSecCryptoAppDefaultKeysMngrInit(mngr) < 0) { fprintf(stderr, "Error: failed to initialize keys manager.\n"); xmlSecKeysMngrDestroy(mngr); return(NULL); } for(i = 0; i < files_size; ++i) { assert(files[i]); /* load trusted cert */ if(xmlSecCryptoAppKeysMngrCertLoad(mngr, files[i], xmlSecKeyDataFormatPem, xmlSecKeyDataTypeTrusted) < 0) { fprintf(stderr,"Error: failed to load pem certificate from \"%s\"\n", files[i]); xmlSecKeysMngrDestroy(mngr); return(NULL); } } return(mngr); }
PyObject *xmlsec_KeysMngrDestroy(PyObject *self, PyObject *args) { PyObject *mngr_obj; xmlSecKeysMngrPtr mngr; if (CheckArgs(args, "O:keysMngrDestroy")) { if (!PyArg_ParseTuple(args, "O:keysMngrDestroy", &mngr_obj)) return NULL; } else return NULL; mngr = xmlSecKeysMngrPtr_get(mngr_obj); xmlSecKeysMngrDestroy(mngr); Py_INCREF(Py_None); return (Py_None); }
xmlSecKeysMngrPtr load_certificates_verify() { xmlSecKeysMngrPtr key_manager; /* create and initialize keys manager */ key_manager = xmlSecKeysMngrCreate(); if (key_manager == NULL) { return(NULL); } if (xmlSecCryptoAppDefaultKeysMngrInit(key_manager) < 0) { xmlSecKeysMngrDestroy(key_manager); return(NULL); } return(key_manager); }
// --------------------------------------------------------------------------- // Second phase constructor // --------------------------------------------------------------------------- // void CXmlSecKeyManager::ConstructL() { iKeyManager = xmlSecKeysMngrCreate(); if(!iKeyManager) { XmlEngOOMTestL(); XmlSecErrorFlagTestL(); User::Leave(KErrKeyManagerInit); } if(xmlSecCryptoAppDefaultKeysMngrInit(iKeyManager) < 0) { xmlSecKeysMngrDestroy(iKeyManager); iKeyManager = NULL; XmlEngOOMTestL(); XmlSecErrorFlagTestL(); User::Leave(KErrKeyManagerInit); } }
VALUE xmlsec_is_valid_by_x509_file(VALUE self, xmlDocPtr doc, VALUE x509_file ) { xmlSecKeysMngrPtr mngr; VALUE v; xmlNodePtr node = NULL; xmlSecDSigCtxPtr dsigCtx = NULL; long i; mngr = xmlSecKeysMngrCreate(); if(mngr == NULL) { if(doc != NULL) xmlFreeDoc(doc); rb_raise(rb_eRuntimeError, "Error: failed to create keys manager.\n"); return Qnil; } if(xmlSecCryptoAppDefaultKeysMngrInit(mngr) < 0) { if(doc != NULL) xmlFreeDoc(doc); if(mngr != NULL) xmlSecKeysMngrDestroy(mngr); rb_raise(rb_eRuntimeError, "Error: failed to initialize keys manager.\n"); return Qnil; } if (TYPE(x509_file) == T_STRING){ /* load trusted cert */ if(xmlSecCryptoAppKeysMngrCertLoad(mngr, StringValuePtr(x509_file), xmlSecKeyDataFormatPem, xmlSecKeyDataTypeTrusted) < 0) { if(doc != NULL) xmlFreeDoc(doc); if(mngr != NULL) xmlSecKeysMngrDestroy(mngr); rb_raise(rb_eRuntimeError, "Error: failed to load pem certificate from \"%s\"\n", StringValuePtr(x509_file)); return Qnil; } } if (TYPE(x509_file) == T_ARRAY) { for (i =0; i < RARRAY_LEN(x509_file); i++) { v = rb_ary_entry(x509_file, i); StringValue(v); if(xmlSecCryptoAppKeysMngrCertLoad(mngr, RSTRING_PTR(v), xmlSecKeyDataFormatPem, xmlSecKeyDataTypeTrusted) < 0) { if(doc != NULL) xmlFreeDoc(doc); if(mngr != NULL) xmlSecKeysMngrDestroy(mngr); rb_raise(rb_eRuntimeError, "Error: failed to load pem certificate from \"%s\"\n", RSTRING_PTR(v)); return Qnil; } } //rb_ary_entry } /* find start node */ node = xmlSecFindNode(xmlDocGetRootElement(doc), xmlSecNodeSignature, xmlSecDSigNs); if(node == NULL) { if(doc != NULL) xmlFreeDoc(doc); rb_raise(rb_eRuntimeError, "Error: start node not found\n"); return Qnil; } /* create signature context*/ dsigCtx = xmlSecDSigCtxCreate(mngr); if(dsigCtx == NULL) { if(doc != NULL) xmlFreeDoc(doc); if(mngr != NULL) xmlSecKeysMngrDestroy(mngr); rb_raise(rb_eRuntimeError, "Error: failed to create signature context\n"); return Qnil; } /* limit the Reference URI attributes to empty or NULL */ dsigCtx->enabledReferenceUris = xmlSecTransformUriTypeEmpty; /* Verify signature */ if(xmlSecDSigCtxVerify(dsigCtx, node) < 0) { if(dsigCtx != NULL) xmlSecDSigCtxDestroy(dsigCtx); if(doc != NULL) xmlFreeDoc(doc); if(mngr != NULL) xmlSecKeysMngrDestroy(mngr); rb_raise(rb_eRuntimeError, "Error: signature verify \"%s\"\n"); return Qnil; } /* verification result*/ if(dsigCtx->status == xmlSecDSigStatusSucceeded) { if(dsigCtx != NULL) xmlSecDSigCtxDestroy(dsigCtx); if(doc != NULL) xmlFreeDoc(doc); if(mngr != NULL) xmlSecKeysMngrDestroy(mngr); return Qtrue; } else { if(dsigCtx != NULL) xmlSecDSigCtxDestroy(dsigCtx); if(doc != NULL) xmlFreeDoc(doc); if(mngr != NULL) xmlSecKeysMngrDestroy(mngr); return Qfalse; } }
int main(int argc, char **argv) { xmlSecKeysMngrPtr mngr; #ifndef XMLSEC_NO_XSLT xsltSecurityPrefsPtr xsltSecPrefs = NULL; #endif /* XMLSEC_NO_XSLT */ /* start response */ fprintf(stdout, "Content-type: text/plain\n"); fprintf(stdout, "\n"); /* Init libxml and libxslt libraries */ xmlInitParser(); LIBXML_TEST_VERSION xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS; xmlSubstituteEntitiesDefault(1); #ifndef XMLSEC_NO_XSLT xmlIndentTreeOutput = 1; #endif /* XMLSEC_NO_XSLT */ /* make sure that we print out everything to stdout */ xmlGenericErrorContext = stdout; /* Init libxslt */ #ifndef XMLSEC_NO_XSLT /* disable everything */ xsltSecPrefs = xsltNewSecurityPrefs(); xsltSetSecurityPrefs(xsltSecPrefs, XSLT_SECPREF_READ_FILE, xsltSecurityForbid); xsltSetSecurityPrefs(xsltSecPrefs, XSLT_SECPREF_WRITE_FILE, xsltSecurityForbid); xsltSetSecurityPrefs(xsltSecPrefs, XSLT_SECPREF_CREATE_DIRECTORY, xsltSecurityForbid); xsltSetSecurityPrefs(xsltSecPrefs, XSLT_SECPREF_READ_NETWORK, xsltSecurityForbid); xsltSetSecurityPrefs(xsltSecPrefs, XSLT_SECPREF_WRITE_NETWORK, xsltSecurityForbid); xsltSetDefaultSecurityPrefs(xsltSecPrefs); #endif /* XMLSEC_NO_XSLT */ /* Init xmlsec library */ if(xmlSecInit() < 0) { fprintf(stdout, "Error: xmlsec initialization failed.\n"); return(-1); } /* Check loaded library version */ if(xmlSecCheckVersion() != 1) { fprintf(stdout, "Error: loaded xmlsec library version is not compatible.\n"); return(-1); } /* Load default crypto engine if we are supporting dynamic * loading for xmlsec-crypto libraries. Use the crypto library * name ("openssl", "nss", etc.) to load corresponding * xmlsec-crypto library. */ #ifdef XMLSEC_CRYPTO_DYNAMIC_LOADING if(xmlSecCryptoDLLoadLibrary(NULL) < 0) { fprintf(stdout, "Error: unable to load default xmlsec-crypto library. Make sure\n" "that you have it installed and check shared libraries path\n" "(LD_LIBRARY_PATH) envornment variable.\n"); return(-1); } #endif /* XMLSEC_CRYPTO_DYNAMIC_LOADING */ /* Init crypto library */ if(xmlSecCryptoAppInit(XMLDSIGVERIFY_DEFAULT_TRUSTED_CERTS_FOLDER) < 0) { fprintf(stdout, "Error: crypto initialization failed.\n"); return(-1); } /* Init xmlsec-crypto library */ if(xmlSecCryptoInit() < 0) { fprintf(stdout, "Error: xmlsec-crypto initialization failed.\n"); return(-1); } /* create keys manager */ mngr = xmlSecKeysMngrCreate(); if(mngr == NULL) { fprintf(stdout, "Error: failed to create keys manager.\n"); return(-1); } if(xmlSecCryptoAppDefaultKeysMngrInit(mngr) < 0) { fprintf(stdout, "Error: failed to initialize keys manager.\n"); return(-1); } if(load_keys(mngr, XMLDSIGVERIFY_KEY_AND_CERTS_FOLDER, 0) < 0) { xmlSecKeysMngrDestroy(mngr); return(-1); } if(load_trusted_certs(mngr, XMLDSIGVERIFY_KEY_AND_CERTS_FOLDER, 0) < 0) { xmlSecKeysMngrDestroy(mngr); return(-1); } if(verify_request(mngr) < 0) { xmlSecKeysMngrDestroy(mngr); return(-1); } /* Destroy keys manager */ xmlSecKeysMngrDestroy(mngr); /* Shutdown xmlsec-crypto library */ xmlSecCryptoShutdown(); /* Shutdown crypto library */ xmlSecCryptoAppShutdown(); /* Shutdown xmlsec library */ xmlSecShutdown(); /* Shutdown libxslt/libxml */ #ifndef XMLSEC_NO_XSLT xsltFreeSecurityPrefs(xsltSecPrefs); xsltCleanupGlobals(); #endif /* XMLSEC_NO_XSLT */ xmlCleanupParser(); return(0); }
CAMLprim value xmlsecml_xmlSecKeysMngrDestroy(value camlKeyManager) { CAMLparam1(camlKeyManager); xmlSecKeysMngrDestroy(Keymngr_val(camlKeyManager)); CAMLreturn(Val_unit); }
xmlSecKeysMngrPtr load_trusted_certs(const char** files) { const char **f; xmlSecKeysMngrPtr mngr, mngr_test; char valid_cert; FILE *file; char *pi, *pf, *data; size_t size; char **a, *all_files[10000]; struct stat stat_struct; DIR *dir; struct dirent *dir_file; char name[10000]; assert(files); /* Create keys manager */ mngr = xmlSecKeysMngrCreate(); if(mngr == NULL) { fprintf(stderr, "XmlSecError: failed to create keys manager.\n"); return(NULL); } if(xmlSecCryptoAppDefaultKeysMngrInit(mngr) < 0) { fprintf(stderr, "XmlSecError: failed to initialize keys manager.\n"); xmlSecKeysMngrDestroy(mngr); return(NULL); } /* Create a list of files scanning directories */ for (f = files, a = all_files; *f; f++) { if (stat(*f, &stat_struct)) perror(*f); else if (S_ISDIR(stat_struct.st_mode)) { dir = opendir(*f); while ((dir_file = readdir(dir))) { sprintf(name, "%s/%s", *f, dir_file->d_name); if (stat(name, &stat_struct)) perror(name); else if (!(S_ISDIR(stat_struct.st_mode))) *(a++) = strdup(name); } closedir(dir); } else *(a++) = strdup(*f); } *a = NULL; /* Open each file, read data and add the certificates to keys manager, because * when load from file directly, just add the first certificate, then we need * read data and add each certificate in it. Other problem is when I have a invalid * certificate, the manager brokes, then I test with a other manager. */ for (a = all_files; *a; a++) { /* Open a file and read content data */ file = fopen(*a, "r"); if (file == NULL){ perror(*a); free(*a); continue; } fseek(file, 0, SEEK_END); size = ftell(file); fseek(file, 0, SEEK_SET); data = (char*)malloc(size + 1); fread(data, size, 1, file); fclose(file); data[size] = '\0'; /* Set pi to start of certificate and pf to end */ pf = pi = data; pf = strstr(pi, "-----END CERTIFICATE-----"); if (!pf) pf = pi + size; while (pf) { /* Test manager for test the certificate */ mngr_test = xmlSecKeysMngrCreate(); valid_cert = 1; if(mngr_test == NULL) { fprintf(stderr, "XmlSecError: failed to create keys manager.\n"); valid_cert = 0; } else if(xmlSecCryptoAppDefaultKeysMngrInit(mngr_test) < 0) { fprintf(stderr, "XmlSecError: failed to initialize keys manager.\n"); valid_cert = 0; } else if (xmlSecCryptoAppKeysMngrCertLoadMemory(mngr_test, (xmlSecByte*)pi, pf - pi + 25, xmlSecKeyDataFormatPem, xmlSecKeyDataTypeTrusted) < 0) { fprintf(stderr,"XmlSecError: failed to load pem certificate from \"%s\" at %i.\n", *a, pi - data + 1); valid_cert = 0; } xmlSecKeysMngrDestroy(mngr_test); /* If certificate is valid, add this to keys manager for future use */ if (valid_cert && xmlSecCryptoAppKeysMngrCertLoadMemory( mngr, (xmlSecByte*)pi, pf - pi + 25, xmlSecKeyDataFormatPem, xmlSecKeyDataTypeTrusted) < 0) fprintf(stderr,"XmlSecError: maybe duplicate pem certificate from \"%s\" at %i.\n", *a, pi - data + 1); pi = pf + 25; pf = strstr(pi, "-----END CERTIFICATE-----"); } /* Free allocated memory */ free(*a); free(data); } return(mngr); }
int xml_verify(char *filename) { xmlSecDSigCtxPtr dsig_ctx = NULL; xmlDocPtr doc = NULL; xmlNodePtr root_node; xmlNodePtr sign_node; xmlNodePtr cert_node; xmlNodePtr x509d_node; xmlNodePtr cur_node; int result = DCP_FATAL; xmlSecKeysMngrPtr key_manager; char cert[5000]; int cert_l; xmlsec_verify_init(); /* load doc file */ doc = xmlParseFile(filename); if (doc == NULL) { dcp_log(LOG_ERROR, "unable to parse file %s", filename); goto done; } /* find root node */ root_node = xmlDocGetRootElement(doc); if (root_node == NULL){ dcp_log(LOG_ERROR, "unable to find root node"); goto done; } /* find signature node */ sign_node = xmlSecFindNode(root_node, xmlSecNodeSignature, xmlSecDSigNs); if(sign_node == NULL) { dcp_log(LOG_ERROR, "signature node not found"); goto done; } /* create keys manager */ key_manager = load_certificates(); if (key_manager == NULL) { dcp_log(LOG_ERROR,"create key manager failed"); goto done; } /* find certificates */ cur_node = sign_node; while (x509d_node = xmlSecFindNode(cur_node, xmlSecNodeX509Data, xmlSecDSigNs)) { cert_node = xmlSecFindNode(x509d_node, xmlSecNodeX509Certificate, xmlSecDSigNs); if(cert_node == NULL) { dcp_log(LOG_ERROR, "X509certficate node not found"); goto done; } sprintf(cert,"-----BEGIN CERTIFICATE-----\n%s\n-----END CERTIFICATE-----\n",xmlNodeGetContent(cert_node)); cert_l = strlen(cert); if (xmlSecCryptoAppKeysMngrCertLoadMemory(key_manager, cert, cert_l, xmlSecKeyDataFormatPem, xmlSecKeyDataTypeTrusted) < 0) { dcp_log(LOG_ERROR, "could read X509certificate node value"); goto done; } cur_node = xmlNextElementSibling(x509d_node); } /* create signature context */ dsig_ctx = xmlSecDSigCtxCreate(key_manager); if (dsig_ctx == NULL) { dcp_log(LOG_ERROR,"create signature opendcp failed"); goto done; } /* sign the template */ if (xmlSecDSigCtxVerify(dsig_ctx, sign_node) < 0) { dcp_log(LOG_ERROR,"signature verify failed"); goto done; } if (dsig_ctx->status != xmlSecDSigStatusSucceeded) { dcp_log(LOG_ERROR,"signature validation failed"); goto done; } /* success */ result = 0; done: /* destroy keys manager */ xmlSecKeysMngrDestroy(key_manager); /* destroy signature context */ if(dsig_ctx != NULL) { xmlSecDSigCtxDestroy(dsig_ctx); } /* destroy xml doc */ if(doc != NULL) { xmlFreeDoc(doc); } xmlsec_close(); return(result); }
static PyObject * PoleXmlSec_verify(PyObject *self, PyObject *args) { const char *xml, *id_attr_name, *id_node_name, *certifiers; int verified = 0; PyObject* result; xmlSecKeysMngrPtr mngr; const char **f, *files[1000000]; char *c, *certs; if (!PyArg_ParseTuple(args, "ssss", &xml, &id_attr_name, &id_node_name, &certifiers)) return NULL; result = Py_BuildValue("b", verified); /* Init libxml and libxslt libraries */ xmlInitParser(); LIBXML_TEST_VERSION xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS; xmlSubstituteEntitiesDefault(1); #ifndef XMLSEC_NO_XSLT xmlIndentTreeOutput = 1; #endif /* XMLSEC_NO_XSLT */ /* Init xmlsec library */ if(xmlSecInit() < 0) { fprintf(stderr, "XmlSecError: xmlsec initialization failed.\n"); return result; } /* Check loaded library version */ if(xmlSecCheckVersion() != 1) { fprintf(stderr, "XmlSecError: loaded xmlsec library version is not compatible.\n"); return result; } /* Load default crypto engine if we are supporting dynamic * loading for xmlsec-crypto libraries. Use the crypto library * name ("openssl", "nss", etc.) to load corresponding * xmlsec-crypto library. */ #ifdef XMLSEC_CRYPTO_DYNAMIC_LOADING if(xmlSecCryptoDLLoadLibrary(BAD_CAST XMLSEC_CRYPTO) < 0) { fprintf(stderr, "XmlSecError: unable to load default xmlsec-crypto library. Make sure\n" "that you have it installed and check shared libraries path\n" "(LD_LIBRARY_PATH) envornment variable.\n"); return result; } #endif /* XMLSEC_CRYPTO_DYNAMIC_LOADING */ /* Init crypto library */ if(xmlSecCryptoAppInit(NULL) < 0) { fprintf(stderr, "XmlSecError: crypto initialization failed.\n"); return result; } /* Init xmlsec-crypto library */ if(xmlSecCryptoInit() < 0) { fprintf(stderr, "XmlSecError: xmlsec-crypto initialization failed.\n"); return result; } /* Create a list of trusted certificates files/directories */ certs = strdup(certifiers); for (c = certs, f = files, *(f++) = c; *c; c++) if (*c == ';') { *c = '\0'; *(f++) = c + 1; } *f = NULL; /* create keys manager and load trusted certificates */ mngr = load_trusted_certs(files); free(certs); if(mngr == NULL) { return result; } verified = verify_xml(mngr, xml, id_attr_name, id_node_name); xmlSecKeysMngrDestroy(mngr); result = Py_BuildValue("b", verified == 1); /* Shutdown xmlsec-crypto library */ xmlSecCryptoShutdown(); /* Shutdown xmlsec library */ xmlSecShutdown(); /* Shutdown crypto library */ /* Se executar essa linha dá erro na conexão HTTPS */ xmlSecCryptoAppShutdown(); /* Shutdown libxslt/libxml/libxmlsec1 */ #ifndef XMLSEC_NO_XSLT xsltCleanupGlobals(); #endif /* XMLSEC_NO_XSLT */ xmlCleanupParser(); return result; }
/** * xmlSecNssAppliedKeysMngrCreate: * @slot: array of pointers to NSS PKCS#11 slot information. * @cSlots: number of slots in the array * @handler: the pointer to NSS certificate database. * * Create and load NSS crypto slot and certificate database into keys manager * * Returns keys manager pointer on success or NULL otherwise. */ xmlSecKeysMngrPtr xmlSecNssAppliedKeysMngrCreate( PK11SlotInfo** slots, int cSlots, CERTCertDBHandle* handler ) { xmlSecKeyDataStorePtr certStore = NULL ; xmlSecKeysMngrPtr keyMngr = NULL ; xmlSecKeyStorePtr keyStore = NULL ; int islot = 0; keyStore = xmlSecKeyStoreCreate( xmlSecNssKeysStoreId ) ; if( keyStore == NULL ) { xmlSecError( XMLSEC_ERRORS_HERE , NULL , "xmlSecKeyStoreCreate" , XMLSEC_ERRORS_R_XMLSEC_FAILED , XMLSEC_ERRORS_NO_MESSAGE ) ; return NULL ; } for (islot = 0; islot < cSlots; islot++) { xmlSecNssKeySlotPtr keySlot ; /* Create a key slot */ keySlot = xmlSecNssKeySlotCreate() ; if( keySlot == NULL ) { xmlSecError( XMLSEC_ERRORS_HERE , xmlSecErrorsSafeString( xmlSecKeyStoreGetName( keyStore ) ) , "xmlSecNssKeySlotCreate" , XMLSEC_ERRORS_R_XMLSEC_FAILED , XMLSEC_ERRORS_NO_MESSAGE ) ; xmlSecKeyStoreDestroy( keyStore ) ; return NULL ; } /* Set slot */ if( xmlSecNssKeySlotSetSlot( keySlot , slots[islot] ) < 0 ) { xmlSecError( XMLSEC_ERRORS_HERE , xmlSecErrorsSafeString( xmlSecKeyStoreGetName( keyStore ) ) , "xmlSecNssKeySlotSetSlot" , XMLSEC_ERRORS_R_XMLSEC_FAILED , XMLSEC_ERRORS_NO_MESSAGE ) ; xmlSecKeyStoreDestroy( keyStore ) ; xmlSecNssKeySlotDestroy( keySlot ) ; return NULL ; } /* Adopt keySlot */ if( xmlSecNssKeysStoreAdoptKeySlot( keyStore , keySlot ) < 0 ) { xmlSecError( XMLSEC_ERRORS_HERE , xmlSecErrorsSafeString( xmlSecKeyStoreGetName( keyStore ) ) , "xmlSecNssKeysStoreAdoptKeySlot" , XMLSEC_ERRORS_R_XMLSEC_FAILED , XMLSEC_ERRORS_NO_MESSAGE ) ; xmlSecKeyStoreDestroy( keyStore ) ; xmlSecNssKeySlotDestroy( keySlot ) ; return NULL ; } } keyMngr = xmlSecKeysMngrCreate() ; if( keyMngr == NULL ) { xmlSecError( XMLSEC_ERRORS_HERE , NULL , "xmlSecKeysMngrCreate" , XMLSEC_ERRORS_R_XMLSEC_FAILED , XMLSEC_ERRORS_NO_MESSAGE ) ; xmlSecKeyStoreDestroy( keyStore ) ; return NULL ; } /*- * Add key store to manager, from now on keys manager destroys the store if * needed */ if( xmlSecKeysMngrAdoptKeysStore( keyMngr, keyStore ) < 0 ) { xmlSecError( XMLSEC_ERRORS_HERE , xmlSecErrorsSafeString( xmlSecKeyStoreGetName( keyStore ) ) , "xmlSecKeysMngrAdoptKeyStore" , XMLSEC_ERRORS_R_XMLSEC_FAILED , XMLSEC_ERRORS_NO_MESSAGE ) ; xmlSecKeyStoreDestroy( keyStore ) ; xmlSecKeysMngrDestroy( keyMngr ) ; return NULL ; } /*- * Initialize crypto library specific data in keys manager */ if( xmlSecNssKeysMngrInit( keyMngr ) < 0 ) { xmlSecError( XMLSEC_ERRORS_HERE , NULL , "xmlSecKeysMngrCreate" , XMLSEC_ERRORS_R_XMLSEC_FAILED , XMLSEC_ERRORS_NO_MESSAGE ) ; xmlSecKeysMngrDestroy( keyMngr ) ; return NULL ; } /*- * Set certificate databse to X509 key data store */ /** * Because Tej's implementation of certDB use the default DB, so I ignore * the certDB handler at present. I'll modify the cert store sources to * accept particular certDB instead of default ones. certStore = xmlSecKeysMngrGetDataStore( keyMngr , xmlSecNssKeyDataStoreX509Id ) ; if( certStore == NULL ) { xmlSecError( XMLSEC_ERRORS_HERE , xmlSecErrorsSafeString( xmlSecKeyStoreGetName( keyStore ) ) , "xmlSecKeysMngrGetDataStore" , XMLSEC_ERRORS_R_XMLSEC_FAILED , XMLSEC_ERRORS_NO_MESSAGE ) ; xmlSecKeysMngrDestroy( keyMngr ) ; return NULL ; } if( xmlSecNssKeyDataStoreX509SetCertDb( certStore , handler ) < 0 ) { xmlSecError( XMLSEC_ERRORS_HERE , xmlSecErrorsSafeString( xmlSecKeyStoreGetName( keyStore ) ) , "xmlSecNssKeyDataStoreX509SetCertDb" , XMLSEC_ERRORS_R_XMLSEC_FAILED , XMLSEC_ERRORS_NO_MESSAGE ) ; xmlSecKeysMngrDestroy( keyMngr ) ; return NULL ; } */ /*- * Set the getKey callback */ keyMngr->getKey = xmlSecKeysMngrGetKey ; return keyMngr ; }
int main(int argc, char **argv) { xmlSecKeysMngrPtr mngr; #ifndef XMLSEC_NO_XSLT xsltSecurityPrefsPtr xsltSecPrefs = NULL; #endif /* XMLSEC_NO_XSLT */ assert(argv); if(argc != 3) { fprintf(stderr, "Error: wrong number of arguments.\n"); fprintf(stderr, "Usage: %s <enc-file> <key-file1> [<key-file2> [...]]\n", argv[0]); return(1); } /* Init libxml and libxslt libraries */ xmlInitParser(); LIBXML_TEST_VERSION xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS; xmlSubstituteEntitiesDefault(1); #ifndef XMLSEC_NO_XSLT xmlIndentTreeOutput = 1; #endif /* XMLSEC_NO_XSLT */ /* Init libxslt */ #ifndef XMLSEC_NO_XSLT /* disable everything */ xsltSecPrefs = xsltNewSecurityPrefs(); xsltSetSecurityPrefs(xsltSecPrefs, XSLT_SECPREF_READ_FILE, xsltSecurityForbid); xsltSetSecurityPrefs(xsltSecPrefs, XSLT_SECPREF_WRITE_FILE, xsltSecurityForbid); xsltSetSecurityPrefs(xsltSecPrefs, XSLT_SECPREF_CREATE_DIRECTORY, xsltSecurityForbid); xsltSetSecurityPrefs(xsltSecPrefs, XSLT_SECPREF_READ_NETWORK, xsltSecurityForbid); xsltSetSecurityPrefs(xsltSecPrefs, XSLT_SECPREF_WRITE_NETWORK, xsltSecurityForbid); xsltSetDefaultSecurityPrefs(xsltSecPrefs); #endif /* XMLSEC_NO_XSLT */ /* Init xmlsec library */ if(xmlSecInit() < 0) { fprintf(stderr, "Error: xmlsec initialization failed.\n"); return(-1); } /* Check loaded library version */ if(xmlSecCheckVersion() != 1) { fprintf(stderr, "Error: loaded xmlsec library version is not compatible.\n"); return(-1); } /* Load default crypto engine if we are supporting dynamic * loading for xmlsec-crypto libraries. Use the crypto library * name ("openssl", "nss", etc.) to load corresponding * xmlsec-crypto library. */ #ifdef XMLSEC_CRYPTO_DYNAMIC_LOADING if(xmlSecCryptoDLLoadLibrary(BAD_CAST XMLSEC_CRYPTO) < 0) { fprintf(stderr, "Error: unable to load default xmlsec-crypto library. Make sure\n" "that you have it installed and check shared libraries path\n" "(LD_LIBRARY_PATH) envornment variable.\n"); return(-1); } #endif /* XMLSEC_CRYPTO_DYNAMIC_LOADING */ /* Init crypto library */ if(xmlSecCryptoAppInit(NULL) < 0) { fprintf(stderr, "Error: crypto initialization failed.\n"); return(-1); } /* Init xmlsec-crypto library */ if(xmlSecCryptoInit() < 0) { fprintf(stderr, "Error: xmlsec-crypto initialization failed.\n"); return(-1); } /* create keys manager and load keys */ mngr = load_des_keys(&(argv[2]), argc - 2); if(mngr == NULL) { return(-1); } if(decrypt_file(mngr, argv[1]) < 0) { xmlSecKeysMngrDestroy(mngr); return(-1); } /* destroy keys manager */ xmlSecKeysMngrDestroy(mngr); /* Shutdown xmlsec-crypto library */ xmlSecCryptoShutdown(); /* Shutdown crypto library */ xmlSecCryptoAppShutdown(); /* Shutdown xmlsec library */ xmlSecShutdown(); /* Shutdown libxslt/libxml */ #ifndef XMLSEC_NO_XSLT xsltFreeSecurityPrefs(xsltSecPrefs); xsltCleanupGlobals(); #endif /* XMLSEC_NO_XSLT */ xmlCleanupParser(); return(0); }
int main(int argc, char **argv) { xmlSecKeysMngrPtr mngr; assert(argv); if(argc != 2) { printf_a_ignorar3(stderr, "Error: wrong number of arguments.\n"); printf_a_ignorar3(stderr, "Usage: %s <enc-file>\n", argv[0]); return(1); } /* Init libxml and libxslt libraries */ xmlInitParser(); LIBXML_TEST_VERSION xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS; xmlSubstituteEntitiesDefault(1); #ifndef XMLSEC_NO_XSLT xmlIndentTreeOutput = 1; #endif /* XMLSEC_NO_XSLT */ /* Init xmlsec library */ if(xmlSecInit() < 0) { printf_a_ignorar3(stderr, "Error: xmlsec initialization failed.\n"); return(-1); } /* Check loaded library version */ if(xmlSecCheckVersion() != 1) { printf_a_ignorar3(stderr, "Error: loaded xmlsec library version is not compatible.\n"); return(-1); } /* Load default crypto engine if we are supporting dynamic * loading for xmlsec-crypto libraries. Use the crypto library * name ("openssl", "nss", etc.) to load corresponding * xmlsec-crypto library. */ #ifdef XMLSEC_CRYPTO_DYNAMIC_LOADING if(xmlSecCryptoDLLoadLibrary(BAD_CAST XMLSEC_CRYPTO) < 0) { printf_a_ignorar3(stderr, "Error: unable to load default xmlsec-crypto library. Make sure\n" "that you have it installed and check shared libraries path\n" "(LD_LIBRARY_PATH) envornment variable.\n"); return(-1); } #endif /* XMLSEC_CRYPTO_DYNAMIC_LOADING */ /* Init crypto library */ if(xmlSecCryptoAppInit(NULL) < 0) { printf_a_ignorar3(stderr, "Error: crypto initialization failed.\n"); return(-1); } /* Init xmlsec-crypto library */ if(xmlSecCryptoInit() < 0) { printf_a_ignorar3(stderr, "Error: xmlsec-crypto initialization failed.\n"); return(-1); } /* create keys manager and load keys */ mngr = create_files_keys_mngr(); if(mngr == NULL) { return(-1); } if(decrypt_file(mngr, argv[1]) < 0) { xmlSecKeysMngrDestroy(mngr); return(-1); } /* destroy keys manager */ xmlSecKeysMngrDestroy(mngr); /* Shutdown xmlsec-crypto library */ xmlSecCryptoShutdown(); /* Shutdown crypto library */ xmlSecCryptoAppShutdown(); /* Shutdown xmlsec library */ xmlSecShutdown(); /* Shutdown libxslt/libxml */ #ifndef XMLSEC_NO_XSLT xsltCleanupGlobals(); #endif /* XMLSEC_NO_XSLT */ xmlCleanupParser(); return(0); }
int xml_sign(opendcp_t *opendcp, char *filename) { xmlSecDSigCtxPtr dsig_ctx = NULL; xmlDocPtr doc = NULL; xmlNodePtr root_node; xmlNodePtr sign_node; FILE *fp; int result = OPENDCP_ERROR; xmlSecKeysMngrPtr key_manager = NULL; dcp_log(LOG_DEBUG, "xml_sign: xmlsec_init"); xmlsec_init(); /* load doc file */ dcp_log(LOG_DEBUG, "xml_sign: parse file"); doc = xmlParseFile(filename); if (doc == NULL) { dcp_log(OPENDCP_ERROR, "Error: unable to parse file %s", filename); goto done; } /* find root node */ root_node = xmlDocGetRootElement(doc); if (root_node == NULL){ dcp_log(OPENDCP_ERROR, "Error: unable to find root node"); goto done; } /* find signature node */ sign_node = xmlSecFindNode(root_node, xmlSecNodeSignature, xmlSecDSigNs); if(sign_node == NULL) { fprintf(stderr, "Error: start node not found"); goto done; } /* create keys manager */ key_manager = load_certificates_sign(opendcp); if (key_manager == NULL) { fprintf(stderr,"Error: failed to create key manager\n"); goto done; } /* create signature opendcp */ dsig_ctx = xmlSecDSigCtxCreate(key_manager); if(dsig_ctx == NULL) { fprintf(stderr,"Error: failed to create signature opendcp\n"); goto done; } /* sign the template */ if(xmlSecDSigCtxSign(dsig_ctx, sign_node) < 0) { fprintf(stderr,"Error: signature failed\n"); goto done; } /* open xml file */ fp = fopen(filename,"wb"); if (fp == NULL) { fprintf(stderr,"Error: could not open output file\n"); goto done; } /* write the xml file */ if (xmlDocDump(fp, doc) < 0) { fprintf(stderr,"Error: writing XML document failed\n"); goto done; } /* close the file */ fclose(fp); /* success */ result = 0; done: /* destroy keys manager */ xmlSecKeysMngrDestroy(key_manager); /* destroy signature context */ if(dsig_ctx != NULL) { xmlSecDSigCtxDestroy(dsig_ctx); } /* destroy xml doc */ if(doc != NULL) { xmlFreeDoc(doc); } xmlsec_close(); return(result); }
xmlSecKeysMngrPtr load_certificates_sign(opendcp_t *opendcp) { xmlSecKeysMngrPtr key_manager; xmlSecKeyPtr key; /* create and initialize keys manager */ key_manager = xmlSecKeysMngrCreate(); if (key_manager == NULL) { fprintf(stderr, "Error: failed to create keys manager.\n"); return(NULL); } if (xmlSecCryptoAppDefaultKeysMngrInit(key_manager) < 0) { fprintf(stderr, "Error: failed to initialize keys manager.\n"); xmlSecKeysMngrDestroy(key_manager); return(NULL); } /* read key file */ if (opendcp->xml_signature.private_key) { key = xmlSecCryptoAppKeyLoad(opendcp->xml_signature.private_key, xmlSecKeyDataFormatPem, NULL, NULL, NULL); } else { key = xmlSecCryptoAppKeyLoadMemory(opendcp_private_key, strlen((char *)opendcp_private_key),xmlSecKeyDataFormatPem, NULL, NULL, NULL); } if (xmlSecCryptoAppDefaultKeysMngrAdoptKey(key_manager, key) < 0) { fprintf(stderr, "Error: failed to initialize keys manager.\n"); xmlSecKeysMngrDestroy(key_manager); return(NULL); } /* load root certificate */ if (opendcp->xml_signature.root) { if (xmlSecCryptoAppKeysMngrCertLoad(key_manager, opendcp->xml_signature.root, xmlSecKeyDataFormatPem, xmlSecKeyDataTypeTrusted) < 0) { fprintf(stderr,"Error: failed to load pem certificate \"%s\"\n", opendcp->xml_signature.root); xmlSecKeysMngrDestroy(key_manager); return(NULL); } } else { if (xmlSecCryptoAppKeysMngrCertLoadMemory(key_manager, opendcp_root_cert, strlen((char* )opendcp_root_cert), xmlSecKeyDataFormatPem, xmlSecKeyDataTypeTrusted) < 0) { fprintf(stderr,"Error: failed to load pem certificate from memory\n"); xmlSecKeysMngrDestroy(key_manager); return(NULL); } } /* load ca (intermediate) certificate */ if (opendcp->xml_signature.ca) { if (xmlSecCryptoAppKeysMngrCertLoad(key_manager, opendcp->xml_signature.ca, xmlSecKeyDataFormatPem, xmlSecKeyDataTypeTrusted) < 0) { fprintf(stderr,"Error: failed to load pem certificate \"%s\"\n", opendcp->xml_signature.ca); xmlSecKeysMngrDestroy(key_manager); return(NULL); } } else { if (xmlSecCryptoAppKeysMngrCertLoadMemory(key_manager, opendcp_ca_cert, strlen((char *)opendcp_ca_cert), xmlSecKeyDataFormatPem, xmlSecKeyDataTypeTrusted) < 0) { fprintf(stderr,"Error: failed to load pem certificate from memory\n"); xmlSecKeysMngrDestroy(key_manager); return(NULL); } } return(key_manager); }