[xmlsec] creating a ds:KeyInfo object

Smith Baylor sbaylor at gmail.com
Tue Dec 28 15:24:02 PST 2004


Hi,

I am interested in creating a ds:KeyInfo Object - without using
templates - as I want to just stick in my personal schema.  I am
having problems with this code.  Can someone tell me what I am doing
wrong.  I am using xmlsec1-1.2.6.


  xmlSecKeyInfoCtxPtr keyInfoCtx = NULL;
  xmlSecKeyPtr key = xmlSecKeyCreate();
  xmlSecKeysMngrPtr mngr;
 
  const char* cert_file = "rsacert.pem";

  xmlNodePtr cur = NULL;
  xmlNodePtr keyInfoNode = NULL;
  xmlNodePtr myKeyInfo = NULL;

  assert(cert_file);

   mngr = xmlSecKeysMngrCreate();
      if(mngr == NULL) {
	fprintf(stderr, "Error: failed to create keys manager.\n");
      }
      if(xmlSecCryptoAppDefaultKeysMngrInit(mngr) < 0) {
	fprintf(stderr, "Error: failed to initialize keys manager.\n");
	xmlSecKeysMngrDestroy(mngr);
      }    
      
      keyInfoCtx = xmlSecKeyInfoCtxCreate(NULL);
      if(keyInfoCtx == NULL) {
        printf("Error: failed to create KeyInfo context\n");
      }
      else {
	keyInfoCtx->mode = xmlSecKeyInfoModeWrite;
	printf("Success: KeyInfoCtx\n");
      }

      /* load key from a pem file, 
       * if key is not found then it's an error (is it?) 
       */
      
      if (xmlSecCryptoAppKeyCertLoad(key,
				     cert_file,
				     xmlSecKeyDataFormatPem) < 0)
	{
	  printf ("ERROR: loading cert from file %s\n", cert_file);
	}

      /* 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", 
		  cert_file);
	  xmlSecKeyDestroy(key);
	  xmlSecKeysMngrDestroy(mngr);
	}      

      if (key != NULL) {
	printf("Success: key is NOT Null\n");
	/* store the Key into the KeyInfoNode */
	myKeyInfo =  xmlNewNode(NULL, BAD_CAST "KeyInfo");
	
	/* <dsig:KeyInfo/> node */
	cur = xmlSecAddChild(myKeyInfo, 
			     xmlSecNodeKeyInfo, 
			     xmlSecDSigNs);

       xmlSecKeyDebugXmlDump(key, stdout);
	if ( xmlSecKeyInfoNodeWrite(cur, 
				    key, 
				    keyInfoCtx) < 0)
	  {
	    printf("ERROR: writing KeyInfo Node\n");
	  }
      }
      else {
	printf("ERROR: key is NULL!!\n");
      }
    }

When I run this code, I get the following error:
func=xmlSecKeyDebugXmlDump:file=keys.c:line=1004:obj=unknown:subj=xmlSecKeyIsValid(key):error=100:assertion:

And all I see for KeyInfo is <KeyInfo
xmlns="http://www.w3.org/2000/09/xmldsig#"/>

I am using the rsacert.pem from the xmlsec zip bundle.

Thanks in advance for the help

smith



More information about the xmlsec mailing list