Exemplo n.º 1
0
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%   G e n e r a t e S e c r e t K e y                                         %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  GenerateSecretKey() returns WizardTrue if a key is generated and successfully
%  added to the secret key ring.
%
%  The format of the GenerateSecretKey method is:
%
%      WizardBooleanType GenerateSecretKey(SecretInfo *secret_info,
%        ExceptionInfo *exception)
%
%  A description of each parameter follows:
%
%    o secret_info: The secret info.
%
%    o exception: Return any errors or warnings in this structure.
%
*/
WizardExport WizardBooleanType GenerateSecretKey(SecretInfo *secret_info,
  ExceptionInfo *exception)
{
  ExceptionInfo
    *sans;

  WizardBooleanType
    status;

  StringInfo
    *key,
    *phrase;

  (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
  WizardAssert(AuthenticateDomain,secret_info != (SecretInfo *) NULL);
  WizardAssert(AuthenticateDomain,secret_info->signature == WizardSignature);
  WizardAssert(AuthenticateDomain,exception != (ExceptionInfo *) NULL);
  if (secret_info->passphrase == (char *) NULL)
    phrase=GetPassphrase(exception);
  else
    phrase=FileToStringInfo(secret_info->passphrase,~0,exception);
  if (phrase == (StringInfo *) NULL)
    return(WizardFalse);
  sans=AcquireExceptionInfo();
  do
  {
    if (secret_info->key != (StringInfo *) NULL)
      secret_info->key=DestroyStringInfo(secret_info->key);
    secret_info->key=GetRandomKey(secret_info->random_info,
      secret_info->key_length/8);
    ConstructHMAC(secret_info->hmac_info,phrase,secret_info->key);
    if (secret_info->id != (StringInfo *) NULL)
      secret_info->id=DestroyStringInfo(secret_info->id);
    secret_info->id=CloneStringInfo(GetHMACDigest(secret_info->hmac_info));
    SetKeyringId(secret_info->keyring_info,secret_info->id);
    status=ExportKeyringKey(secret_info->keyring_info,sans);
  } while (status != WizardFalse);
  sans=DestroyExceptionInfo(sans);
  SetKeyringKey(secret_info->keyring_info,secret_info->key);
  SetKeyringNonce(secret_info->keyring_info,secret_info->nonce);
  SetCipherKey(secret_info->cipher_info,phrase);
  SetCipherNonce(secret_info->cipher_info,GetKeyringNonce(
    secret_info->keyring_info));
  key=CloneStringInfo(GetKeyringKey(secret_info->keyring_info));
  (void) EncipherCipher(secret_info->cipher_info,key);
  SetKeyringKey(secret_info->keyring_info,key);
  status=ImportKeyringKey(secret_info->keyring_info,exception);
  phrase=DestroyStringInfo(phrase);
  key=DestroyStringInfo(key);
  return(WizardTrue);
}
Exemplo n.º 2
0
MagickExport int AcquireUniqueFileResource(char *path)
{
#if !defined(O_NOFOLLOW)
#define O_NOFOLLOW 0
#endif
#if !defined(TMP_MAX)
# define TMP_MAX  238328
#endif

  int
    c,
    file;

  register char
    *p;

  register ssize_t
    i;

  static const char
    portable_filename[65] =
      "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";

  StringInfo
    *key;

  unsigned char
    *datum;

  assert(path != (char *) NULL);
  (void) LogMagickEvent(ResourceEvent,GetMagickModule(),"%s",path);
  if (random_info == (RandomInfo *) NULL)
    random_info=AcquireRandomInfo();
  file=(-1);
  for (i=0; i < (ssize_t) TMP_MAX; i++)
  {
    /*
      Get temporary pathname.
    */
    (void) GetPathTemplate(path);
    key=GetRandomKey(random_info,2);
    p=path+strlen(path)-8;
    datum=GetStringInfoDatum(key);
    for (i=0; i < (ssize_t) GetStringInfoLength(key); i++)
    {
      c=(int) (datum[i] & 0x3f);
      *p++=portable_filename[c];
    }
    key=DestroyStringInfo(key);
#if defined(MAGICKCORE_HAVE_MKSTEMP)
    file=mkstemp(path);
#if defined(__OS2__)
    setmode(file,O_BINARY);
#endif
    if (file != -1)
      break;
#endif
    key=GetRandomKey(random_info,6);
    p=path+strlen(path)-6;
    datum=GetStringInfoDatum(key);
    for (i=0; i < (ssize_t) GetStringInfoLength(key); i++)
    {
      c=(int) (datum[i] & 0x3f);
      *p++=portable_filename[c];
    }
    key=DestroyStringInfo(key);
    file=open_utf8(path,O_RDWR | O_CREAT | O_EXCL | O_BINARY | O_NOFOLLOW,S_MODE);
    if ((file >= 0) || (errno != EEXIST))
      break;
  }
  (void) LogMagickEvent(ResourceEvent,GetMagickModule(),"%s",path);
  if (file == -1)
    return(file);
  if (resource_semaphore == (SemaphoreInfo *) NULL)
    AcquireSemaphoreInfo(&resource_semaphore);
  LockSemaphoreInfo(resource_semaphore);
  if (temporary_resources == (SplayTreeInfo *) NULL)
    temporary_resources=NewSplayTree(CompareSplayTreeString,
      DestroyTemporaryResources,(void *(*)(void *)) NULL);
  UnlockSemaphoreInfo(resource_semaphore);
  (void) AddValueToSplayTree(temporary_resources,ConstantString(path),
    (const void *) NULL);
  return(file);
}
Exemplo n.º 3
0
    void LruCacheTest::PerfTestHelper(
        int operationCount,
        size_t capacity,
        bool enableTrim)
    {
        //SetVerifyOutput localVerifySettings(VerifyOutputSettings::LogOnlyFailures);

        size_t bucketCount = 10240;

        LruCache<wstring, TestCacheEntry> cache(enableTrim ? capacity : 0, bucketCount);

        Random rand(static_cast<int>(DateTime::Now().Ticks));
        vector<TestCacheEntrySPtr> entries;
        for (auto ix=0; ix<operationCount; ++ix)
        {
            entries.push_back(make_shared<TestCacheEntry>(GetRandomKey(rand)));
        }

        Stopwatch stopwatch;

        // Write
        
        stopwatch.Restart();
        for (auto it=entries.begin(); it!=entries.end(); ++it)
        {
            auto entry = *it;
            bool success = cache.TryPutOrGet(entry);
            VERIFY_IS_TRUE(success);
        }
        stopwatch.Stop();

        Trace.WriteInfo(
            TraceComponent, 
            "LruCache-put: operations={0} elapsed={1} throughput={2} ops/s", 
            cache.Size, 
            stopwatch.Elapsed,
            (double)cache.Size / stopwatch.ElapsedMilliseconds * 1000);

        // Read

        stopwatch.Restart();
        for (auto it=entries.begin(); it!=entries.end(); ++it)
        {
            shared_ptr<TestCacheEntry> result;
            bool success = cache.TryGet((*it)->GetKey(), result);
            VERIFY_IS_TRUE(success);
        }
        stopwatch.Stop();

        Trace.WriteInfo(
            TraceComponent, 
            "LruCache-get: operations={0} elapsed={1} throughput={2} ops/s", 
            cache.Size, 
            stopwatch.Elapsed,
            (double)cache.Size / stopwatch.ElapsedMilliseconds * 1000);

        Trace.WriteInfo(
            TraceComponent, 
            "CacheSize: {0}",
            cache.Size);
    }
Exemplo n.º 4
0
    void LruCacheTest::SyncAddRemoveHelper(TestCache & cache, int keyCount)
    {
        Trace.WriteInfo(
            TraceComponent, 
            "Add/Remove {0} keys",
            keyCount);

        VERIFY_IS_TRUE_FMT(cache.Size == 0, "cache size = {0}", cache.Size);
        VERIFY_IS_TRUE_FMT(cache.CacheLimit == 0 || cache.EvictionListSize == cache.Size, "limit = {0} eviction = {1}", cache.CacheLimit, cache.EvictionListSize);

        Random rand(static_cast<int>(DateTime::Now().Ticks));

        vector<wstring> keys;
        for (auto ix=0; ix<keyCount; ++ix)
        {
            keys.push_back(GetRandomKey(rand));
        }

        for (auto ix=0; ix<keyCount; ++ix) 
        {
            SyncTryGet(cache, keys[ix], false);
        }

        Trace.WriteInfo(
            TraceComponent, 
            "Add {0} keys",
            keyCount);

        for (auto ix=0; ix<keyCount; ++ix) 
        {
            SyncTryPut(cache, keys[ix], true);

            VERIFY_IS_TRUE_FMT(cache.Size == ix + 1, "cache size = {0}", cache.Size);
            VERIFY_IS_TRUE_FMT(cache.CacheLimit == 0 || cache.EvictionListSize == cache.Size, "limit = {0} eviction = {1}", cache.CacheLimit, cache.EvictionListSize);

            SyncTryGet(cache, keys[ix], true);
        }

        for (auto ix=0; ix<keyCount; ++ix) 
        {
            SyncTryGet(cache, keys[ix], true);
        }

        Trace.WriteInfo(
            TraceComponent, 
            "Remove {0} keys",
            keyCount);

        for (auto ix=0; ix<keyCount; ++ix) 
        {
            SyncTryRemove(cache, keys[ix], true);

            VERIFY_IS_TRUE_FMT(cache.Size == keyCount - ix - 1, "cache size = {0}", cache.Size);
            VERIFY_IS_TRUE_FMT(cache.CacheLimit == 0 || cache.EvictionListSize == cache.Size, "limit = {0} eviction = {1}", cache.CacheLimit, cache.EvictionListSize);

            SyncTryGet(cache, keys[ix], false);
        }

        for (auto ix=0; ix<keyCount; ++ix) SyncTryGet(cache, keys[ix], false);
        for (auto ix=0; ix<keyCount; ++ix) SyncTryRemove(cache, keys[ix], false);

        VERIFY_IS_TRUE_FMT(cache.Size == 0, "cache size = {0}", cache.Size);
        VERIFY_IS_TRUE_FMT(cache.CacheLimit == 0 || cache.EvictionListSize == cache.Size, "limit = {0} eviction = {1}", cache.CacheLimit, cache.EvictionListSize);
    }
Exemplo n.º 5
0
MagickExport int AcquireUniqueFileResource(char *path)
{
#if !defined(O_NOFOLLOW)
#define O_NOFOLLOW 0
#endif
#if !defined(TMP_MAX)
# define TMP_MAX  238328
#endif

  char
    *resource;

  int
    c,
    file;

  register char
    *p;

  register long
    i;

  static const char
    portable_filename[65] =
      "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._";

  unsigned char
    key[8];

  assert(path != (char *) NULL);
  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
  file=(-1);
  for (i=0; i < TMP_MAX; i++)
  {
    /*
      Get temporary pathname.
    */
    (void) GetPathTemplate(path);
#if defined(HAVE_MKSTEMP)
    file=mkstemp(path);
    if (file != -1)
      break;
#endif
    GetRandomKey(key,8);
    p=path+strlen(path)-8;
    for (i=0; i < 8; i++)
    {
      c=(int) (key[i] & 0x3f);
      *p++=portable_filename[c];
    }
    file=open(path,O_RDWR | O_CREAT | O_EXCL | O_BINARY | O_NOFOLLOW,S_MODE);
    if ((file > 0) || (errno != EEXIST))
      break;
  }
  (void) LogMagickEvent(ResourceEvent,GetMagickModule(),"%s",path);
  if (file == -1)
    return(file);
  AcquireSemaphoreInfo(&resource_semaphore);
  if (temporary_resources == (SplayTreeInfo *) NULL)
    temporary_resources=NewSplayTree(CompareSplayTreeString,
      RelinquishMagickMemory,DestroyTemporaryResources);
  RelinquishSemaphoreInfo(resource_semaphore);
  resource=ConstantString(path);
  (void) AddValueToSplayTree(temporary_resources,resource,resource);
  return(file);
}
Exemplo n.º 6
0
MagickExport int AcquireUniqueFileResource(char *path)
{
#if !defined(O_NOFOLLOW)
#define O_NOFOLLOW 0
#endif
#if !defined(TMP_MAX)
# define TMP_MAX  238328
#endif

  char
    *resource;

  int
    c,
    file;

  register char
    *p;

  register long
    i;

  unsigned char
    key[8];

  assert(path != (char *) NULL);
  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
  file=(-1);
  for (i=0; i < TMP_MAX; i++)
  {
    /*
      Get temporary pathname.
    */
    (void) GetPathTemplate(path);
#if defined(HAVE_MKSTEMP)
    file=mkstemp(path);
    if (file != -1)
      break;
#endif
    GetRandomKey(key,8);
    p=path+strlen(path)-8;
    for (i=0; i < 8; i++)
    {
      c=(int) (key[i] & 0x1f);
      *p++=(char) (c > 9 ? c+(int) 'a'-10 : c+(int) '0');
    }
    file=open(path,O_RDWR | O_CREAT | O_EXCL | O_BINARY | O_NOFOLLOW,S_MODE);
    if ((file > 0) || (errno != EEXIST))
      break;
  }
  (void) LogMagickEvent(ResourceEvent,GetMagickModule(),"%s",path);
  if (file == -1)
    return(file);
  (void) AcquireMagickResource(FileResource,1);
  if (temporary_resources == (SplayTreeInfo *) NULL)
    temporary_resources=NewSplayTree(CompareSplayTreeString,
      RelinquishMagickMemory,DestroyTemporaryResources);
  resource=ConstantString(AcquireString(path));
  (void) AddValueToSplayTree(temporary_resources,resource,resource);
  return(file);
}