|
WizardsToolkit
1.0.7
|
00001 /* 00002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00003 % % 00004 % % 00005 % % 00006 % SSSSS EEEEE CCCC RRRR EEEEE TTTTT % 00007 % SS E C R R E T % 00008 % SSS EEE C RRRR EEE T % 00009 % SS E C R R E T % 00010 % SSSSS EEEEE CCCC R R EEEEE T % 00011 % % 00012 % % 00013 % Wizard's Toolkit Secret Authentication Methods % 00014 % % 00015 % Software Design % 00016 % John Cristy % 00017 % March 2003 % 00018 % % 00019 % % 00020 % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization % 00021 % dedicated to making software imaging solutions freely available. % 00022 % % 00023 % You may not use this file except in compliance with the License. You may % 00024 % obtain a copy of the License at % 00025 % % 00026 % http://www.wizards-toolkit.org/script/license.php % 00027 % % 00028 % Unless required by applicable law or agreed to in writing, software % 00029 % distributed under the License is distributed on an "AS IS" BASIS, % 00030 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. % 00031 % See the License for the specific language governing permissions and % 00032 % limitations under the License. % 00033 % % 00034 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00035 % 00036 % 00037 % 00038 */ 00039 00040 /* 00041 Include declarations. 00042 */ 00043 #include "wizard/studio.h" 00044 #include "wizard/cipher.h" 00045 #include "wizard/exception.h" 00046 #include "wizard/exception-private.h" 00047 #include "wizard/hmac.h" 00048 #include "wizard/keyring.h" 00049 #include "wizard/memory_.h" 00050 #include "wizard/passphrase.h" 00051 #include "wizard/random_.h" 00052 #include "wizard/secret.h" 00053 00054 /* 00055 Define declarations. 00056 */ 00057 #define SecretKeyCipher AESCipher 00058 #define SecretKeyMode CTRMode 00059 #define SecretKeyHash SHA256Hash 00060 #define SecretRandomHash SHA256Hash 00061 00062 /* 00063 Typedef declarations. 00064 */ 00065 struct _SecretInfo 00066 { 00067 char 00068 *passphrase; 00069 00070 StringInfo 00071 *id, 00072 *key, 00073 *digest, 00074 *nonce; 00075 00076 size_t 00077 key_length; 00078 00079 KeyringInfo 00080 *keyring_info; 00081 00082 CipherInfo 00083 *cipher_info; 00084 00085 HMACInfo 00086 *hmac_info; 00087 00088 RandomInfo 00089 *random_info; 00090 00091 time_t 00092 timestamp; 00093 00094 size_t 00095 signature; 00096 }; 00097 00098 /* 00099 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00100 % % 00101 % % 00102 % % 00103 % A c q u i r e S e c r e t I n f o % 00104 % % 00105 % % 00106 % % 00107 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00108 % 00109 % AcquireSecretInfo() allocates the SecretInfo structure. 00110 % 00111 % The format of the AcquireSecretInfo method is: 00112 % 00113 % SecretInfo *AcquireSecretInfo(const char *path,const HashType hash, 00114 % const size_t key_length) 00115 % 00116 % A description of each parameter follows: 00117 % 00118 % o path: The secret keyring path. 00119 % 00120 % o hash: The hash type. 00121 % 00122 % o key_length: The key length in bits. 00123 */ 00124 WizardExport SecretInfo *AcquireSecretInfo(const char *path,const HashType hash, 00125 const size_t key_length) 00126 { 00127 SecretInfo 00128 *secret_info; 00129 00130 secret_info=(SecretInfo *) AcquireWizardMemory(sizeof(*secret_info)); 00131 if (secret_info == (SecretInfo *) NULL) 00132 ThrowWizardFatalError(AuthenticateDomain,MemoryError); 00133 (void) ResetWizardMemory(secret_info,0,sizeof(*secret_info)); 00134 secret_info->key_length=key_length; 00135 secret_info->keyring_info=AcquireKeyringInfo(path); 00136 secret_info->cipher_info=AcquireCipherInfo(SecretKeyCipher,SecretKeyMode); 00137 secret_info->nonce=GenerateCipherNonce(secret_info->cipher_info); 00138 secret_info->hmac_info=AcquireHMACInfo(hash); 00139 secret_info->random_info=AcquireRandomInfo(SecretRandomHash); 00140 secret_info->timestamp=time((time_t *) NULL); 00141 secret_info->signature=WizardSignature; 00142 return(secret_info); 00143 } 00144 00145 /* 00146 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00147 % % 00148 % % 00149 % % 00150 % A u t h e n t i c a t e S e c r e t K e y % 00151 % % 00152 % % 00153 % % 00154 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00155 % 00156 % AuthenticateSecretKey() returns WizardTrue if the caller is authenticated. 00157 % 00158 % The format of the AuthenticateSecretKey method is: 00159 % 00160 % void AuthenticateSecretKey(SecretInfo *secret_info, 00161 % ExceptionInfo *exception) 00162 % 00163 % A description of each parameter follows: 00164 % 00165 % o secret_info: The secret info. 00166 % 00167 % o exception: Return any errors or warnings in this structure. 00168 % 00169 */ 00170 WizardExport WizardBooleanType AuthenticateSecretKey(SecretInfo *secret_info, 00171 ExceptionInfo *exception) 00172 { 00173 WizardBooleanType 00174 status; 00175 00176 StringInfo 00177 *digest, 00178 *phrase; 00179 00180 (void) LogWizardEvent(TraceEvent,GetWizardModule(),"..."); 00181 WizardAssert(AuthenticateDomain,secret_info != (SecretInfo *) NULL); 00182 WizardAssert(AuthenticateDomain,secret_info->signature == WizardSignature); 00183 SetKeyringId(secret_info->keyring_info,secret_info->id); 00184 status=ExportKeyringKey(secret_info->keyring_info,exception); 00185 if (status == WizardFalse) 00186 return(WizardFalse); 00187 if (secret_info->passphrase == (char *) NULL) 00188 phrase=GetPassphrase(exception); 00189 else 00190 phrase=FileToStringInfo(secret_info->passphrase,~0,exception); 00191 if (phrase == (StringInfo *) NULL) 00192 return(WizardFalse); 00193 SetCipherKey(secret_info->cipher_info,phrase); 00194 SetCipherNonce(secret_info->cipher_info,GetKeyringNonce( 00195 secret_info->keyring_info)); 00196 if (secret_info->key != (StringInfo *) NULL) 00197 secret_info->key=DestroyStringInfo(secret_info->key); 00198 secret_info->key=CloneStringInfo(GetKeyringKey(secret_info->keyring_info)); 00199 (void) DecipherCipher(secret_info->cipher_info,secret_info->key); 00200 ConstructHMAC(secret_info->hmac_info,phrase,secret_info->key); 00201 phrase=DestroyStringInfo(phrase); 00202 digest=CloneStringInfo(GetHMACDigest(secret_info->hmac_info)); 00203 if (CompareStringInfo(secret_info->id,digest) != 0) 00204 { 00205 digest=DestroyStringInfo(digest); 00206 return(WizardFalse); 00207 } 00208 digest=DestroyStringInfo(digest); 00209 return(WizardTrue); 00210 } 00211 00212 /* 00213 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00214 % % 00215 % % 00216 % % 00217 % D e s t r o y S e c r e t I n f o % 00218 % % 00219 % % 00220 % % 00221 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00222 % 00223 % DestroySecretInfo() zeros memory associated with the SecretInfo structure. 00224 % 00225 % The format of the DestroySecretInfo method is: 00226 % 00227 % SecretInfo *DestroySecretInfo(SecretInfo *secret_info) 00228 % 00229 % A description of each parameter follows: 00230 % 00231 % o secret_info: The secret info. 00232 % 00233 */ 00234 WizardExport SecretInfo *DestroySecretInfo(SecretInfo *secret_info) 00235 { 00236 (void) LogWizardEvent(TraceEvent,GetWizardModule(),"..."); 00237 WizardAssert(AuthenticateDomain,secret_info != (SecretInfo *) NULL); 00238 WizardAssert(AuthenticateDomain,secret_info->signature == WizardSignature); 00239 if (secret_info->random_info != (RandomInfo *) NULL) 00240 secret_info->random_info=DestroyRandomInfo(secret_info->random_info); 00241 if (secret_info->hmac_info != (HMACInfo *) NULL) 00242 secret_info->hmac_info=DestroyHMACInfo(secret_info->hmac_info); 00243 if (secret_info->cipher_info != (CipherInfo *) NULL) 00244 secret_info->cipher_info=DestroyCipherInfo(secret_info->cipher_info); 00245 if (secret_info->keyring_info != (KeyringInfo *) NULL) 00246 secret_info->keyring_info=DestroyKeyringInfo(secret_info->keyring_info); 00247 if (secret_info->nonce != (StringInfo *) NULL) 00248 secret_info->nonce=DestroyStringInfo(secret_info->nonce); 00249 if (secret_info->key != (StringInfo *) NULL) 00250 secret_info->key=DestroyStringInfo(secret_info->key); 00251 if (secret_info->id != (StringInfo *) NULL) 00252 secret_info->id=DestroyStringInfo(secret_info->id); 00253 if (secret_info->digest != (StringInfo *) NULL) 00254 secret_info->digest=DestroyStringInfo(secret_info->digest); 00255 secret_info->signature=(~WizardSignature); 00256 secret_info=(SecretInfo *) RelinquishWizardMemory(secret_info); 00257 return(secret_info); 00258 } 00259 00260 /* 00261 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00262 % % 00263 % % 00264 % % 00265 % G e n e r a t e S e c r e t K e y % 00266 % % 00267 % % 00268 % % 00269 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00270 % 00271 % GenerateSecretKey() returns WizardTrue if a key is generated and successfully 00272 % added to the secret key ring. 00273 % 00274 % The format of the GenerateSecretKey method is: 00275 % 00276 % WizardBooleanType GenerateSecretKey(SecretInfo *secret_info, 00277 % ExceptionInfo *exception) 00278 % 00279 % A description of each parameter follows: 00280 % 00281 % o secret_info: The secret info. 00282 % 00283 % o exception: Return any errors or warnings in this structure. 00284 % 00285 */ 00286 WizardExport WizardBooleanType GenerateSecretKey(SecretInfo *secret_info, 00287 ExceptionInfo *exception) 00288 { 00289 ExceptionInfo 00290 *sans; 00291 00292 WizardBooleanType 00293 status; 00294 00295 StringInfo 00296 *key, 00297 *phrase; 00298 00299 (void) LogWizardEvent(TraceEvent,GetWizardModule(),"..."); 00300 WizardAssert(AuthenticateDomain,secret_info != (SecretInfo *) NULL); 00301 WizardAssert(AuthenticateDomain,secret_info->signature == WizardSignature); 00302 WizardAssert(AuthenticateDomain,exception != (ExceptionInfo *) NULL); 00303 if (secret_info->passphrase == (char *) NULL) 00304 phrase=GetPassphrase(exception); 00305 else 00306 phrase=FileToStringInfo(secret_info->passphrase,~0,exception); 00307 if (phrase == (StringInfo *) NULL) 00308 return(WizardFalse); 00309 sans=AcquireExceptionInfo(); 00310 do 00311 { 00312 if (secret_info->key != (StringInfo *) NULL) 00313 secret_info->key=DestroyStringInfo(secret_info->key); 00314 secret_info->key=GetRandomKey(secret_info->random_info, 00315 secret_info->key_length/8); 00316 ConstructHMAC(secret_info->hmac_info,phrase,secret_info->key); 00317 if (secret_info->id != (StringInfo *) NULL) 00318 secret_info->id=DestroyStringInfo(secret_info->id); 00319 secret_info->id=CloneStringInfo(GetHMACDigest(secret_info->hmac_info)); 00320 SetKeyringId(secret_info->keyring_info,secret_info->id); 00321 status=ExportKeyringKey(secret_info->keyring_info,sans); 00322 } while (status != WizardFalse); 00323 sans=DestroyExceptionInfo(sans); 00324 SetKeyringKey(secret_info->keyring_info,secret_info->key); 00325 SetKeyringNonce(secret_info->keyring_info,secret_info->nonce); 00326 SetCipherKey(secret_info->cipher_info,phrase); 00327 SetCipherNonce(secret_info->cipher_info,GetKeyringNonce( 00328 secret_info->keyring_info)); 00329 key=CloneStringInfo(GetKeyringKey(secret_info->keyring_info)); 00330 (void) EncipherCipher(secret_info->cipher_info,key); 00331 SetKeyringKey(secret_info->keyring_info,key); 00332 status=ImportKeyringKey(secret_info->keyring_info,exception); 00333 phrase=DestroyStringInfo(phrase); 00334 key=DestroyStringInfo(key); 00335 return(WizardTrue); 00336 } 00337 00338 /* 00339 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00340 % % 00341 % % 00342 % % 00343 % G e t S e c r e t I d % 00344 % % 00345 % % 00346 % % 00347 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00348 % 00349 % GetSecretId() returns the secret id. 00350 % 00351 % The format of the GetSecretId method is: 00352 % 00353 % const StringInfo *GetSecretId(const SecretInfo *secret_info) 00354 % 00355 % A description of each parameter follows: 00356 % 00357 % o secret_info: The secret info. 00358 % 00359 */ 00360 WizardExport const StringInfo *GetSecretId(const SecretInfo *secret_info) 00361 { 00362 (void) LogWizardEvent(TraceEvent,GetWizardModule(),"..."); 00363 WizardAssert(AuthenticateDomain,secret_info != (SecretInfo *) NULL); 00364 WizardAssert(AuthenticateDomain,secret_info->signature == WizardSignature); 00365 return(secret_info->id); 00366 } 00367 00368 /* 00369 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00370 % % 00371 % % 00372 % % 00373 % G e t S e c r e t K e y % 00374 % % 00375 % % 00376 % % 00377 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00378 % 00379 % GetSecretKey() returns the secret key. 00380 % 00381 % The format of the GetSecretKey method is: 00382 % 00383 % const StringInfo *GetSecretKey(const SecretInfo *secret_info) 00384 % 00385 % A description of each parameter follows: 00386 % 00387 % o secret_info: The secret info. 00388 % 00389 */ 00390 WizardExport const StringInfo *GetSecretKey(const SecretInfo *secret_info) 00391 { 00392 (void) LogWizardEvent(TraceEvent,GetWizardModule(),"..."); 00393 WizardAssert(AuthenticateDomain,secret_info != (SecretInfo *) NULL); 00394 WizardAssert(AuthenticateDomain,secret_info->signature == WizardSignature); 00395 return(secret_info->key); 00396 } 00397 00398 /* 00399 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00400 % % 00401 % % 00402 % % 00403 % G e t S e c r e t K e y L e n g t h % 00404 % % 00405 % % 00406 % % 00407 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00408 % 00409 % GetSecretKeyLength() returns the secret key length. 00410 % 00411 % The format of the GetSecretKeyLength method is: 00412 % 00413 % size_t GetSecretKeyLength(const SecretInfo *secret_info) 00414 % 00415 % A description of each parameter follows: 00416 % 00417 % o secret_info: The secret info. 00418 % 00419 */ 00420 WizardExport size_t GetSecretKeyLength(const SecretInfo *secret_info) 00421 { 00422 (void) LogWizardEvent(TraceEvent,GetWizardModule(),"..."); 00423 WizardAssert(AuthenticateDomain,secret_info != (SecretInfo *) NULL); 00424 WizardAssert(AuthenticateDomain,secret_info->signature == WizardSignature); 00425 return(secret_info->key_length); 00426 } 00427 00428 /* 00429 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00430 % % 00431 % % 00432 % % 00433 % G e t S e c r e t P a s s p h r a s e % 00434 % % 00435 % % 00436 % % 00437 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00438 % 00439 % GetSecretPassphrase() returns the secret passphrase. 00440 % 00441 % The format of the GetSecretPassphrase method is: 00442 % 00443 % const char *GetSecretPassphrase(const SecretInfo *secret_info) 00444 % 00445 % A description of each parameter follows: 00446 % 00447 % o secret_info: The secret info. 00448 % 00449 */ 00450 WizardExport const char *GetSecretPassphrase(const SecretInfo *secret_info) 00451 { 00452 (void) LogWizardEvent(TraceEvent,GetWizardModule(),"..."); 00453 WizardAssert(AuthenticateDomain,secret_info != (SecretInfo *) NULL); 00454 WizardAssert(AuthenticateDomain,secret_info->signature == WizardSignature); 00455 return(secret_info->passphrase); 00456 } 00457 00458 /* 00459 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00460 % % 00461 % % 00462 % % 00463 % S e t S e c r e t I d % 00464 % % 00465 % % 00466 % % 00467 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00468 % 00469 % SetSecretId() sets the secret id. 00470 % 00471 % The format of the SetSecretId method is: 00472 % 00473 % void SetSecretId(SecretInfo *secret_info,const StringInfo *id) 00474 % 00475 % A description of each parameter follows: 00476 % 00477 % o secret_info: The ring info. 00478 % 00479 % o id: The id. 00480 % 00481 */ 00482 WizardExport void SetSecretId(SecretInfo *secret_info,const StringInfo *id) 00483 { 00484 if (secret_info->id != (StringInfo *) NULL) 00485 secret_info->id=DestroyStringInfo(secret_info->id); 00486 secret_info->id=CloneStringInfo(id); 00487 } 00488 00489 /* 00490 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00491 % % 00492 % % 00493 % % 00494 % S e t S e c r e t K e y L e n g t h % 00495 % % 00496 % % 00497 % % 00498 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00499 % 00500 % SetSecretKeyLength() sets the authentication method key length. 00501 % 00502 % The format of the SetSecretKeyLength method is: 00503 % 00504 % void SetSecretKeyLength(SecretInfo *secret_info, 00505 % const size_t key_length) 00506 % 00507 % A description of each parameter follows: 00508 % 00509 % o secret_info: The secret info. 00510 % 00511 % o key_length: The key length in bits. 00512 % 00513 */ 00514 WizardExport void SetSecretKeyLength(SecretInfo *secret_info, 00515 const size_t key_length) 00516 { 00517 (void) LogWizardEvent(TraceEvent,GetWizardModule(),"..."); 00518 WizardAssert(AuthenticateDomain,secret_info != (SecretInfo *) NULL); 00519 WizardAssert(AuthenticateDomain,secret_info->signature == WizardSignature); 00520 secret_info->key_length=key_length; 00521 } 00522 00523 /* 00524 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00525 % % 00526 % % 00527 % % 00528 % S e t S e c r e t P a s s p h r a s e % 00529 % % 00530 % % 00531 % % 00532 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 00533 % 00534 % SetSecretPassphrase() sets the secret passphrase. 00535 % 00536 % The format of the SetSecretPassphrase method is: 00537 % 00538 % void SetSecretPassphrase(SecretInfo *secret_info,const char *passphrase) 00539 % 00540 % A description of each parameter follows: 00541 % 00542 % o secret_info: The secret info. 00543 % 00544 % o passphrase: The passphrase. 00545 % 00546 */ 00547 WizardExport void SetSecretPassphrase(SecretInfo *secret_info, 00548 const char *passphrase) 00549 { 00550 if (secret_info->passphrase != (char *) NULL) 00551 secret_info->passphrase=DestroyString(secret_info->passphrase); 00552 secret_info->passphrase=ConstantString(passphrase); 00553 }