Skip to content

Instantly share code, notes, and snippets.

@Houdini
Last active September 17, 2015 23:46
Show Gist options
  • Save Houdini/8bd661d1d0c846293f3a to your computer and use it in GitHub Desktop.
Save Houdini/8bd661d1d0c846293f3a to your computer and use it in GitHub Desktop.
static int
cft_signature(char *in, struct MemoryStruct *out)
{
xmlDocPtr xmlDocIn;
xmlChar *xmlCharIn = xmlCharStrdup(in);
xmlDocIn = xmlParseDoc(xmlCharIn);
if (xmlDocIn == NULL)
return 1;
xmlNodePtr signNode = NULL;
xmlNodePtr refNode = NULL;
xmlNodePtr keyInfoNode = NULL;
xmlSecDSigCtxPtr dsigCtx = NULL;
xmlNodePtr root, node, object;
xmlDocPtr doc;
doc = xmlNewDoc(NULL);
root = xmlNewNode(NULL, BAD_CAST "Document");
char timeNow[10];
sprintf(timeNow, "%d", time(NULL));
xmlNewProp(root, BAD_CAST "stan", BAD_CAST timeNow);
object = xmlNewNode(NULL, BAD_CAST "Object");
xmlNewProp(object, BAD_CAST "Id", BAD_CAST "Res0");
xmlDocSetRootElement(doc, root);
xmlAddChild(object, xmlDocGetRootElement(xmlDocIn));
xmlAddChild(root, object);
// xmlNewChild(doc, NULL, BAD_CAST "Object", NULL);
signNode = xmlSecTmplSignatureCreate(xmlDocIn, xmlSecTransformExclC14NId, xmlSecTransformRsaSha1Id, NULL);
if (signNode == NULL) {
fprintf(stderr, "Error: failed to create signature template\n");
return 1;
}
/* add <dsig:Signature/> node to the doc */
xmlAddChild(root, signNode);
/* add reference */
refNode = xmlSecTmplSignatureAddReference(signNode, xmlSecTransformSha1Id, BAD_CAST "#Res0", NULL, BAD_CAST "http://www.w3.org/2000/09/xmldsig#Object");
// xmlNewProp(refNode, BAD_CAST "URI", BAD_CAST "#Res0");
if (refNode == NULL) {
fprintf(stderr, "Error: failed to add reference to signature template\n");
return 1;
}
//
// /* add enveloped transform */
// if (xmlSecTmplReferenceAddTransform(refNode, xmlSecTransformEnvelopedId) == NULL) {
if (xmlSecTmplReferenceAddTransform(refNode, xmlSecTransformExclC14NWithCommentsId) == NULL) {
fprintf(stderr, "Error: failed to add enveloped transform to reference\n");
return 1;
}
/* add <dsig:KeyInfo/> and <dsig:KeyName/> nodes to put key name in the signed document */
keyInfoNode = xmlSecTmplSignatureEnsureKeyInfo(signNode, NULL);
if (keyInfoNode == NULL) {
fprintf(stderr, "Error: failed to add key info\n");
return (1);
}
if (xmlSecTmplKeyInfoAddX509Data(keyInfoNode) == NULL) {
fprintf(stderr, "Error: failed to add X509Data node\n");
return (1);
}
////
//// if(xmlSecTmplKeyInfoAddKeyName(keyInfoNode, NULL) == NULL) {
//// fprintf(stderr, "Error: failed to add key name\n");
//// exit(-1);
//// }
//
// /* create signature context, we don't need keys manager in this example */
dsigCtx = xmlSecDSigCtxCreate(NULL);
if (dsigCtx == NULL) {
fprintf(stderr, "Error: failed to create signature context\n");
return 1;
}
//
// /* load private key, assuming that there is not password */
dsigCtx->signKey = xmlSecCryptoAppKeyLoad(PKCS12, xmlSecKeyDataFormatPkcs12, PKCS12_PASSWORD, NULL, NULL);
if (dsigCtx->signKey == NULL) {
fprintf(stderr, "Error: failed to load private pem key from \"%s\"\n", PKCS12);
return 1;
}
//
// /* set key name to the file name, this is just an example! */
if (xmlSecKeySetName(dsigCtx->signKey, (xmlChar *) PKCS12) < 0) {
fprintf(stderr, "Error: failed to set key name for key from \"%s\"\n", PKCS12);
return 1;
}
//
// /* sign the template */
if (xmlSecDSigCtxSign(dsigCtx, signNode) < 0) {
fprintf(stderr, "Error: signature failed\n");
return 1;
}
// if(xmlSecTmplObjectAddSignProperties())
/* print signed document to stdout */
xmlDocDump(stdout, doc);
// xmlChar *data_out;
// int data_out_len;
// xmlDocDumpMemory(doc, &data_out, &data_out_len);
xmlChar *xmlbuff;
int buffsize;
// xmlDocDumpFormatMemory(doc, (xmlChar **)&out->memory, buffsize, 1);
// out->size = buffsize;
xmlDocDumpFormatMemory(doc, &xmlbuff, &buffsize, 1);
out->memory = calloc(1, buffsize + 0);
memcpy(out->memory, xmlbuff, buffsize);
out->size = buffsize;
xmlFree(xmlbuff);
/* cleanup */
// if(dsigCtx != NULL) {
// xmlSecDSigCtxDestroy(dsigCtx);
// }
//
if (doc != NULL) {
xmlFreeDoc(doc);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment