root/WizardsToolkit/trunk/wizard/authenticate.c

Revision 1, 23.2 KB (checked in by cristy, 3 months ago)


Line 
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3%                                                                             %
4%                                                                             %
5%                                                                             %
6%    AAA  U   U TTTTT H   H EEEEE N   N TTTTT IIIII  CCCC  AAA  TTTTT EEEEE   %
7%   A   A U   U   T   H   H E     NN  N   T     I   C     A   A   T   E       %
8%   AAAAA U   U   T   HHHHH EEE   N N N   T     I   C     AAAAA   T   EEE     %
9%   A   A U   U   T   H   H E     N  NN   T     I   C     A   A   T   E       %
10%   A   A  UUU    T   H   H EEEEE N   N   T   IIIII  CCCC A   A   T   EEEEE   %
11%                                                                             %
12%                                                                             %
13%                   Wizard's Toolkit Authentication Methods                   %
14%                                                                             %
15%                             Software Design                                 %
16%                               John Cristy                                   %
17%                               March 2003                                    %
18%                                                                             %
19%                                                                             %
20%  Copyright 1999-2009 ImageMagick Studio LLC, a non-profit organization      %
21%  dedicated to making software imaging solutions freely available.           %
22%                                                                             %
23%  You may not use this file except in compliance with the License.  You may  %
24%  obtain a copy of the License at                                            %
25%                                                                             %
26%    http://www.wizards-toolkit.org/script/license.php                        %
27%                                                                             %
28%  Unless required by applicable law or agreed to in writing, software        %
29%  distributed under the License is distributed on an "AS IS" BASIS,          %
30%  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
31%  See the License for the specific language governing permissions and        %
32%  limitations under the License.                                             %
33%                                                                             %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37%
38*/
39
40/*
41  Include declarations.
42*/
43#include "wizard/studio.h"
44#include "wizard/authenticate.h"
45#include "wizard/exception.h"
46#include "wizard/exception-private.h"
47#include "wizard/memory_.h"
48#include "wizard/secret.h"
49
50/*
51  Define declarations.
52*/
53#define SecretKeyLength  1024
54
55/*
56  Typedef declarations.
57*/
58struct _AuthenticateInfo
59{
60  AuthenticateMethod
61    method;
62
63  void
64    *handle;
65
66  time_t
67    timestamp;
68
69  unsigned long
70    signature;
71};
72
73/*
74%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75%                                                                             %
76%                                                                             %
77%                                                                             %
78%   A c q u i r e A u t h e n t i c a t e I n f o                             %
79%                                                                             %
80%                                                                             %
81%                                                                             %
82%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83%
84%  AcquireAuthenticateInfo() allocates the AuthenticateInfo structure.
85%
86%  The format of the AcquireAuthenticateInfo method is:
87%
88%      AuthenticateInfo *AcquireAuthenticateInfo(
89%        const AuthenticateMethod method,const char *path,const HashType hash)
90%
91%  A description of each parameter follows:
92%
93%    o authenticate: The authenticate type.
94%
95%    o path: The keyring path.
96%
97%    o hash: The hash type.
98%
99*/
100WizardExport AuthenticateInfo *AcquireAuthenticateInfo(
101  const AuthenticateMethod method,const char *path,const HashType hash)
102{
103  AuthenticateInfo
104    *authenticate_info;
105
106  authenticate_info=(AuthenticateInfo *) AcquireWizardMemory(
107    sizeof(*authenticate_info));
108  if (authenticate_info == (AuthenticateInfo *) NULL)
109    ThrowWizardFatalError(AuthenticateDomain,MemoryError);
110  (void) ResetWizardMemory(authenticate_info,0,sizeof(*authenticate_info));
111  authenticate_info->method=method;
112  switch (method)
113  {
114    case SecretAuthenticateMethod:
115    {
116      authenticate_info->handle=(AuthenticateInfo *) AcquireSecretInfo(path,
117        hash,SecretKeyLength);
118      break;
119    }
120    default:
121      ThrowWizardFatalError(AuthenticateDomain,EnumerateError);
122  }
123  authenticate_info->timestamp=time((time_t *) NULL);
124  authenticate_info->signature=WizardSignature;
125  return(authenticate_info);
126}
127
128/*
129%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130%                                                                             %
131%                                                                             %
132%                                                                             %
133%   A u t h e n t i c a t e K e y                                             %
134%                                                                             %
135%                                                                             %
136%                                                                             %
137%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138%
139%  AuthenticateKey() returns WizardTrue if the caller is authenticated.
140%
141%  The format of the AuthenticateKey method is:
142%
143%      void AuthenticateKey(AuthenticateInfo *authenticate_info,
144%        ExceptionInfo *exception)
145%
146%  A description of each parameter follows:
147%
148%    o authenticate_info: The authenticate info.
149%
150%    o exception: Return any errors or warnings in this structure.
151%
152*/
153WizardExport WizardBooleanType AuthenticateKey(
154  AuthenticateInfo *authenticate_info,ExceptionInfo *exception)
155{
156  WizardBooleanType
157    status;
158
159  (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
160  WizardAssert(AuthenticateDomain,
161    authenticate_info != (AuthenticateInfo *) NULL);
162  WizardAssert(AuthenticateDomain,
163    authenticate_info->signature == WizardSignature);
164  WizardAssert(AuthenticateDomain,
165    authenticate_info->handle != (AuthenticateInfo *) NULL);
166  status=WizardFalse;
167  switch (authenticate_info->method)
168  {
169    case SecretAuthenticateMethod:
170    {
171      SecretInfo
172        *secret_info;
173
174      secret_info=(SecretInfo *) authenticate_info->handle;
175      status=AuthenticateSecretKey(secret_info,exception);
176      break;
177    }
178    default:
179      ThrowWizardFatalError(AuthenticateDomain,EnumerateError);
180  }
181  return(status);
182}
183
184/*
185%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
186%                                                                             %
187%                                                                             %
188%                                                                             %
189%   D e s t r o y A u t h e n t i c a t e I n f o                             %
190%                                                                             %
191%                                                                             %
192%                                                                             %
193%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
194%
195%  DestroyAuthenticateInfo() zeros memory associated with the AuthenticateInfo
196%  structure.
197%
198%  The format of the DestroyAuthenticateInfo method is:
199%
200%      AuthenticateInfo *DestroyAuthenticateInfo(
201%        AuthenticateInfo *authenticate_info)
202%
203%  A description of each parameter follows:
204%
205%    o authenticate_info: The authenticate info.
206%
207*/
208WizardExport AuthenticateInfo *DestroyAuthenticateInfo(
209  AuthenticateInfo *authenticate_info)
210{
211  (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
212  WizardAssert(AuthenticateDomain,
213    authenticate_info != (AuthenticateInfo *) NULL);
214  WizardAssert(AuthenticateDomain,
215    authenticate_info->signature == WizardSignature);
216  if (authenticate_info->handle != (AuthenticateInfo *) NULL)
217    switch (authenticate_info->method)
218    {
219      case SecretAuthenticateMethod:
220      {
221        authenticate_info->handle=DestroySecretInfo((SecretInfo *)
222          authenticate_info->handle);
223        break;
224      }
225      default:
226        ThrowWizardFatalError(AuthenticateDomain,EnumerateError);
227    }
228  authenticate_info->signature=(~WizardSignature);
229  authenticate_info=(AuthenticateInfo *)
230    RelinquishWizardMemory(authenticate_info);
231  return(authenticate_info);
232}
233
234/*
235%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
236%                                                                             %
237%                                                                             %
238%                                                                             %
239%   G e n e r a t e A u t h e n t i c a t e K e y                             %
240%                                                                             %
241%                                                                             %
242%                                                                             %
243%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
244%
245%  GenerateAuthenticateKey() returns WizardTrue if a secret key is generated and
246%  successfully added to the secret key ring.
247%
248%  The format of the GenerateAuthenticateKey method is:
249%
250%      WizardBooleanType GenerateAuthenticateKey(
251%        AuthenticateInfo *authenticate_info,ExceptionInfo *exception)
252%
253%  A description of each parameter follows:
254%
255%    o authenticate_info: The authenticate info.
256%
257%    o exception: Return any errors or warnings in this structure.
258%
259*/
260WizardExport WizardBooleanType GenerateAuthenticateKey(
261  AuthenticateInfo *authenticate_info,ExceptionInfo *exception)
262{
263  WizardBooleanType
264    status;
265
266  (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
267  WizardAssert(AuthenticateDomain,
268    authenticate_info != (AuthenticateInfo *) NULL);
269  WizardAssert(AuthenticateDomain,
270    authenticate_info->signature == WizardSignature);
271  WizardAssert(AuthenticateDomain,
272    authenticate_info->handle != (AuthenticateInfo *) NULL);
273  WizardAssert(AuthenticateDomain,exception != (ExceptionInfo *) NULL);
274  status=WizardFalse;
275  switch (authenticate_info->method)
276  {
277    case SecretAuthenticateMethod:
278    {
279      SecretInfo
280        *secret_info;
281
282      secret_info=(SecretInfo *) authenticate_info->handle;
283      status=GenerateSecretKey(secret_info,exception);
284      if (status == WizardFalse)
285        break;
286      break;
287    }
288    default:
289      ThrowWizardFatalError(AuthenticateDomain,EnumerateError);
290  }
291  return(status);
292}
293
294/*
295%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
296%                                                                             %
297%                                                                             %
298%                                                                             %
299%   G e t A u t h e n t i c a t e I d                                         %
300%                                                                             %
301%                                                                             %
302%                                                                             %
303%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
304%
305%  GetAuthenticateId() returns the authentication id.
306%
307%  The format of the GetAuthenticateId method is:
308%
309%      const StringInfo *GetAuthenticateId(
310%        const AuthenticateInfo *authenticate_info)
311%
312%  A description of each parameter follows:
313%
314%    o authenticate_info: The authenticate info.
315%
316*/
317WizardExport const StringInfo *GetAuthenticateId(
318  const AuthenticateInfo *authenticate_info)
319{
320  const StringInfo
321    *id;
322
323  (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
324  WizardAssert(AuthenticateDomain,
325    authenticate_info != (AuthenticateInfo *) NULL);
326  WizardAssert(AuthenticateDomain,
327    authenticate_info->signature == WizardSignature);
328  switch (authenticate_info->method)
329  {
330    case SecretAuthenticateMethod:
331    {
332      SecretInfo
333        *secret_info;
334
335      secret_info=(SecretInfo *) authenticate_info->handle;
336      id=GetSecretId(secret_info);
337      break;
338    }
339    default:
340      ThrowWizardFatalError(AuthenticateDomain,EnumerateError);
341  }
342  return(id);
343}
344
345/*
346%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
347%                                                                             %
348%                                                                             %
349%                                                                             %
350%   G e t A u t h e n t i c a t e K e y                                       %
351%                                                                             %
352%                                                                             %
353%                                                                             %
354%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
355%
356%  GetAuthenticateKey() returns the authenticate key.
357%
358%  The format of the GetAuthenticateKey method is:
359%
360%      const StringInfo *GetAuthenticateKey(
361%        const AuthenticateInfo *authenticate_info)
362%
363%  A description of each parameter follows:
364%
365%    o authenticate_info: The authenticate info.
366%
367*/
368WizardExport const StringInfo *GetAuthenticateKey(
369  const AuthenticateInfo *authenticate_info)
370{
371  const StringInfo
372    *key;
373
374  (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
375  WizardAssert(AuthenticateDomain,
376    authenticate_info != (AuthenticateInfo *) NULL);
377  WizardAssert(AuthenticateDomain,
378    authenticate_info->signature == WizardSignature);
379  switch (authenticate_info->method)
380  {
381    case SecretAuthenticateMethod:
382    {
383      SecretInfo
384        *secret_info;
385
386      secret_info=(SecretInfo *) authenticate_info->handle;
387      key=GetSecretKey(secret_info);
388      break;
389    }
390    default:
391      ThrowWizardFatalError(AuthenticateDomain,EnumerateError);
392  }
393  return(key);
394}
395
396/*
397%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
398%                                                                             %
399%                                                                             %
400%                                                                             %
401%   G e t A u t h e n t i c a t e K e y L e n g t h                           %
402%                                                                             %
403%                                                                             %
404%                                                                             %
405%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
406%
407%  GetAuthenticateKeyLength() returns the authenticate key length.
408%
409%  The format of the GetAuthenticateKeyLength method is:
410%
411%      size_t GetAuthenticateKeyLength(
412%        const AuthenticateInfo *authenticate_info)
413%
414%  A description of each parameter follows:
415%
416%    o authenticate_info: The authenticate info.
417%
418*/
419WizardExport size_t GetAuthenticateKeyLength(
420  const AuthenticateInfo *authenticate_info)
421{
422  size_t
423    key_length;
424
425  (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
426  WizardAssert(AuthenticateDomain,
427    authenticate_info != (AuthenticateInfo *) NULL);
428  WizardAssert(AuthenticateDomain,
429    authenticate_info->signature == WizardSignature);
430  switch (authenticate_info->method)
431  {
432    case SecretAuthenticateMethod:
433    {
434      SecretInfo
435        *secret_info;
436
437      secret_info=(SecretInfo *) authenticate_info->handle;
438      key_length=GetSecretKeyLength(secret_info);
439      break;
440    }
441    default:
442      ThrowWizardFatalError(AuthenticateDomain,EnumerateError);
443  }
444  return(key_length);
445}
446
447/*
448%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
449%                                                                             %
450%                                                                             %
451%                                                                             %
452%   G e t A u t h e n t i c a t e P a s s p h r a s e                         %
453%                                                                             %
454%                                                                             %
455%                                                                             %
456%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
457%
458%  GetAuthenticateId() returns the authentication passphrase.
459%
460%  The format of the GetAuthenticatePassphrase method is:
461%
462%      const char *GetAuthenticatePassphrase(
463%        const AuthenticateInfo *authenticate_info)
464%
465%  A description of each parameter follows:
466%
467%    o authenticate_info: The authenticate info.
468%
469*/
470WizardExport const char *GetAuthenticatePassphrase(
471  const AuthenticateInfo *authenticate_info)
472{
473  const char
474    *passphrase;
475
476  (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
477  WizardAssert(AuthenticateDomain,
478    authenticate_info != (AuthenticateInfo *) NULL);
479  WizardAssert(AuthenticateDomain,
480    authenticate_info->signature == WizardSignature);
481  switch (authenticate_info->method)
482  {
483    case SecretAuthenticateMethod:
484    {
485      SecretInfo
486        *secret_info;
487
488      secret_info=(SecretInfo *) authenticate_info->handle;
489      passphrase=GetSecretPassphrase(secret_info);
490      break;
491    }
492    default:
493      ThrowWizardFatalError(AuthenticateDomain,EnumerateError);
494  }
495  return(passphrase);
496}
497
498/*
499%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
500%                                                                             %
501%                                                                             %
502%                                                                             %
503%   S e t A u t h e n t i c a t e I d                                         %
504%                                                                             %
505%                                                                             %
506%                                                                             %
507%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
508%
509%  SetAuthenticateId() sets the authentication id.
510%
511%  The format of the SetAuthenticateId method is:
512%
513%      void SetAuthenticateId(AuthenticateInfo *authenticate_info,
514%        const StringInfo *id)
515%
516%  A description of each parameter follows:
517%
518%    o authenticate_info: The authentication info.
519%
520%    o id: The id.
521%
522*/
523WizardExport void SetAuthenticateId(AuthenticateInfo *authenticate_info,
524  const StringInfo *id)
525{
526  (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
527  WizardAssert(AuthenticateDomain,
528    authenticate_info != (AuthenticateInfo *) NULL);
529  WizardAssert(AuthenticateDomain,
530    authenticate_info->signature == WizardSignature);
531  WizardAssert(AuthenticateDomain,
532    authenticate_info->handle != (AuthenticateInfo *) NULL);
533  switch (authenticate_info->method)
534  {
535    case SecretAuthenticateMethod:
536    {
537      SecretInfo
538        *secret_info;
539
540      secret_info=(SecretInfo *) authenticate_info->handle;
541      SetSecretId(secret_info,id);
542      break;
543    }
544    default:
545      ThrowWizardFatalError(AuthenticateDomain,EnumerateError);
546  }
547}
548
549/*
550%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
551%                                                                             %
552%                                                                             %
553%                                                                             %
554%   S e t A u t h e n t i c a t e K e y L e n g t h                           %
555%                                                                             %
556%                                                                             %
557%                                                                             %
558%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
559%
560%  SetAuthenticateKeyLength() sets the authentication method key length.
561%
562%  The format of the SetAuthenticateKeyLength method is:
563%
564%      void SetAuthenticateKeyLength(AuthenticateInfo *authenticate_info,
565%        const unsigned long key_length)
566%
567%  A description of each parameter follows:
568%
569%    o authenticate_info: The authenticate info.
570%
571%    o key_length: The key length in bits.
572%
573*/
574WizardExport void SetAuthenticateKeyLength(AuthenticateInfo *authenticate_info,
575  const unsigned long key_length)
576{
577  (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
578  WizardAssert(AuthenticateDomain,
579    authenticate_info != (AuthenticateInfo *) NULL);
580  WizardAssert(AuthenticateDomain,
581    authenticate_info->signature == WizardSignature);
582  WizardAssert(AuthenticateDomain,
583    authenticate_info->handle != (AuthenticateInfo *) NULL);
584  switch (authenticate_info->method)
585  {
586    case SecretAuthenticateMethod:
587    {
588      SecretInfo
589        *secret_info;
590
591      secret_info=(SecretInfo *) authenticate_info->handle;
592      SetSecretKeyLength(secret_info,key_length);
593      break;
594    }
595    default:
596      break;
597  }
598}
599
600/*
601%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
602%                                                                             %
603%                                                                             %
604%                                                                             %
605%   S e t A u t h e n t i c a t e P a s s p h r a s e                         %
606%                                                                             %
607%                                                                             %
608%                                                                             %
609%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
610%
611%  SetAuthenticatePassphrase() sets the authentication method key file.
612%
613%  The format of the SetAuthenticatePassphrase method is:
614%
615%      void SetAuthenticatePassphrase(AuthenticateInfo *authenticate_info,
616%        const char *passphrase)
617%
618%  A description of each parameter follows:
619%
620%    o authenticate_info: The authenticate info.
621%
622%    o passphrase: The passphrase.
623%
624*/
625WizardExport void SetAuthenticatePassphrase(AuthenticateInfo *authenticate_info,
626  const char *passphrase)
627{
628  (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
629  WizardAssert(AuthenticateDomain,
630    authenticate_info != (AuthenticateInfo *) NULL);
631  WizardAssert(AuthenticateDomain,
632    authenticate_info->signature == WizardSignature);
633  WizardAssert(AuthenticateDomain,
634    authenticate_info->handle != (AuthenticateInfo *) NULL);
635  switch (authenticate_info->method)
636  {
637    case SecretAuthenticateMethod:
638    {
639      SecretInfo
640        *secret_info;
641
642      secret_info=(SecretInfo *) authenticate_info->handle;
643      SetSecretPassphrase(secret_info,passphrase);
644      break;
645    }
646    default:
647      break;
648  }
649}
Note: See TracBrowser for help on using the browser.