summaryrefslogtreecommitdiff
path: root/SecurityPkg/UserIdentification/UserProfileManagerDxe/ModifyIdentityPolicy.c
blob: 9f157f816b28f3eb4bb72881f4c1d04ae3a4b842 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/** @file
  The functions for identification policy modification.
    
Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials 
are licensed and made available under the terms and conditions of the BSD License 
which accompanies this distribution.  The full text of the license may be found at 
http://opensource.org/licenses/bsd-license.php

THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, 
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/

#include "UserProfileManager.h"


/**
  Verify the new identity policy in the current implementation. The same credential
  provider can't appear twice in one identity policy.

  @param[in] NewGuid       Points to the credential provider guid.
  
  @retval TRUE     The NewGuid was found in the identity policy.
  @retval FALSE    The NewGuid was not found.

**/
BOOLEAN
ProviderAlreadyInPolicy (
  IN EFI_GUID                                      *NewGuid
  )
{
  UINTN                         Offset;
  EFI_USER_INFO_IDENTITY_POLICY *Identity;
  EFI_INPUT_KEY                 Key;

  Offset = 0;
  while (Offset < mUserInfo.NewIdentityPolicyLen) {
    Identity = (EFI_USER_INFO_IDENTITY_POLICY *) (mUserInfo.NewIdentityPolicy + Offset);
    if (Identity->Type == EFI_USER_INFO_IDENTITY_CREDENTIAL_PROVIDER) {
      if (CompareGuid (NewGuid, (EFI_GUID *) (Identity + 1))) {
        CreatePopUp (
          EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
          &Key,
          L"This Credential Provider Are Already Used!",
          L"",
          L"Press Any Key to Continue ...",
          NULL
          );
        return TRUE;
      }
    }
    Offset += Identity->Length;
  }
  
  return FALSE;
}


/**
  Add the user's credential record in the provider.

  @param[in]  Identity     Identity policy item including credential provider.
  @param[in]  User         Points to user profile.

  @retval EFI_SUCCESS      Add or delete record successfully.
  @retval Others           Fail to add or delete record.

**/
EFI_STATUS
EnrollUserOnProvider (
  IN  EFI_USER_INFO_IDENTITY_POLICY              *Identity,
  IN  EFI_USER_PROFILE_HANDLE                    User 
  )
{
  UINTN                          Index;
  EFI_USER_CREDENTIAL2_PROTOCOL  *UserCredential;
  
  //
  // Find the specified credential provider.
  //
  for (Index = 0; Index < mProviderInfo->Count; Index++) {
    UserCredential = mProviderInfo->Provider[Index];
    if (CompareGuid ((EFI_GUID *)(Identity + 1), &UserCredential->Identifier)) {
      return UserCredential->Enroll (UserCredential, User);
    }
  }

  return EFI_NOT_FOUND;  
}


/**
  Delete the User's credential record on the provider.

  @param[in]  Identity     Point to EFI_USER_INFO_IDENTITY_CREDENTIAL_PROVIDER user info.
  @param[in]  User         Points to user profile.

  @retval EFI_SUCCESS      Delete User's credential record successfully.
  @retval Others           Fail to add or delete record.

**/
EFI_STATUS
DeleteUserOnProvider (
  IN  EFI_USER_INFO_IDENTITY_POLICY              *Identity,
  IN  EFI_USER_PROFILE_HANDLE                    User 
  )
{
  UINTN                          Index;
  EFI_USER_CREDENTIAL2_PROTOCOL  *UserCredential;
  
  //
  // Find the specified credential provider.
  //
  for (Index = 0; Index < mProviderInfo->Count; Index++) {
    UserCredential = mProviderInfo->Provider[Index];
    if (CompareGuid ((EFI_GUID *)(Identity + 1), &UserCredential->Identifier)) {
      return UserCredential->Delete (UserCredential, User);
    }
  }

  return EFI_NOT_FOUND;  
}


/**
  Delete User's credental from all the providers that exist in User's identity policy.
  
  @param[in]  IdentityPolicy     Point to User's identity policy.
  @param[in]  IdentityPolicyLen  The length of the identity policy.
  @param[in]  User               Points to user profile.

**/
VOID
DeleteCredentialFromProviders (
  IN     UINT8                                *IdentityPolicy,
  IN     UINTN                                 IdentityPolicyLen,
  IN     EFI_USER_PROFILE_HANDLE               User 
  )
{
  EFI_USER_INFO_IDENTITY_POLICY    *Identity;
  UINTN                            Offset;

  Offset = 0;
  while (Offset < IdentityPolicyLen) {
    Identity = (EFI_USER_INFO_IDENTITY_POLICY *) (IdentityPolicy + Offset);
    if (Identity->Type == EFI_USER_INFO_IDENTITY_CREDENTIAL_PROVIDER) {
      //
      // Delete the user on this provider.
      //
      DeleteUserOnProvider (Identity, User);
    }
    Offset += Identity->Length;
  }

}


/**
  Remove the provider specified by Offset from the new user identification record.
  
  @param[in]  IdentityPolicy    Point to user identity item in new identification policy.
  @param[in]  Offset            The item offset in the new identification policy.

**/
VOID
DeleteProviderFromPolicy (
  IN     EFI_USER_INFO_IDENTITY_POLICY         *IdentityPolicy,
  IN     UINTN                                 Offset
  )
{
  UINTN                         RemainingLen;
  UINTN                         DeleteLen;

  if (IdentityPolicy->Length == mUserInfo.NewIdentityPolicyLen) {
    //
    // Only one credential provider in the identification policy.
    // Set the new policy to be TRUE after removed the provider.
    //
    IdentityPolicy->Type           = EFI_USER_INFO_IDENTITY_TRUE;
    IdentityPolicy->Length         = sizeof (EFI_USER_INFO_IDENTITY_POLICY);
    mUserInfo.NewIdentityPolicyLen = IdentityPolicy->Length;
    return ;
  }

  DeleteLen = IdentityPolicy->Length + sizeof(EFI_USER_INFO_IDENTITY_POLICY);
  if ((Offset + IdentityPolicy->Length) != mUserInfo.NewIdentityPolicyLen) {
    //
    // This provider is not the last item in the identification policy, delete it and the connector.
    //    
    RemainingLen = mUserInfo.NewIdentityPolicyLen - Offset - DeleteLen;
    CopyMem ((UINT8 *) IdentityPolicy, (UINT8 *) IdentityPolicy + DeleteLen, RemainingLen);
  }
  mUserInfo.NewIdentityPolicyLen -= DeleteLen;  
}


/**
  Add a new provider to the mUserInfo.NewIdentityPolicy.

  It is invoked when 'add option' in UI is pressed.

  @param[in] NewGuid       Points to the credential provider guid.
  
**/
VOID
AddProviderToPolicy (
  IN  EFI_GUID                                  *NewGuid
  )
{
  UINT8                         *NewPolicyInfo;
  UINTN                         NewPolicyInfoLen;
  EFI_USER_INFO_IDENTITY_POLICY *Policy;

  //
  // Allocate memory for the new identity policy.
  //
  NewPolicyInfoLen = mUserInfo.NewIdentityPolicyLen + sizeof (EFI_USER_INFO_IDENTITY_POLICY) + sizeof (EFI_GUID);
  if (mUserInfo.NewIdentityPolicyLen > 0) {
    //
    // It is not the first provider in the policy. Add a connector before provider.
    //
    NewPolicyInfoLen += sizeof (EFI_USER_INFO_IDENTITY_POLICY);
  }
  NewPolicyInfo = AllocateZeroPool (NewPolicyInfoLen);
  if (NewPolicyInfo == NULL) {
    return ;
  }

  NewPolicyInfoLen = 0;
  if (mUserInfo.NewIdentityPolicyLen > 0) {
    //
    // Save orginal policy.
    //
    CopyMem (NewPolicyInfo, mUserInfo.NewIdentityPolicy, mUserInfo.NewIdentityPolicyLen);

    //
    // Save logical connector.
    //
    Policy = (EFI_USER_INFO_IDENTITY_POLICY *) (NewPolicyInfo + mUserInfo.NewIdentityPolicyLen);
    if (mConncetLogical == 0) {
      Policy->Type = EFI_USER_INFO_IDENTITY_AND;
    } else {
      Policy->Type = EFI_USER_INFO_IDENTITY_OR;
    }

    Policy->Length   = sizeof (EFI_USER_INFO_IDENTITY_POLICY);
    NewPolicyInfoLen = mUserInfo.NewIdentityPolicyLen + Policy->Length;
    FreePool (mUserInfo.NewIdentityPolicy);
  }
  
  //
  // Save credential provider.
  //
  Policy = (EFI_USER_INFO_IDENTITY_POLICY *) (NewPolicyInfo + NewPolicyInfoLen);
  Policy->Length = sizeof (EFI_USER_INFO_IDENTITY_POLICY) + sizeof (EFI_GUID);
  Policy->Type   = EFI_USER_INFO_IDENTITY_CREDENTIAL_PROVIDER;
  CopyGuid ((EFI_GUID *) (Policy + 1), NewGuid);
  NewPolicyInfoLen += Policy->Length;

  //
  // Update identity policy choice.
  //
  mUserInfo.NewIdentityPolicy         = NewPolicyInfo;
  mUserInfo.NewIdentityPolicyLen      = NewPolicyInfoLen;
  mUserInfo.NewIdentityPolicyModified = TRUE;
}


/**
  This function replaces the old identity policy with a new identity policy.

  This function delete the user identity policy information.
  If enroll new credential failed, recover the old identity policy.

  @retval EFI_SUCCESS      Modify user identity policy successfully.
  @retval Others           Fail to modify user identity policy.

**/
EFI_STATUS
UpdateCredentialProvider (
  )
{
  EFI_STATUS                    Status;
  EFI_USER_INFO_IDENTITY_POLICY *Identity;
  UINTN                         Offset;

  //
  // Delete the old identification policy.
  //
  DeleteCredentialFromProviders (mUserInfo.IdentityPolicy, mUserInfo.IdentityPolicyLen, mModifyUser);

  //
  // Add the new identification policy.
  //
  Offset  = 0;
  while (Offset < mUserInfo.NewIdentityPolicyLen) {
    Identity = (EFI_USER_INFO_IDENTITY_POLICY *) (mUserInfo.NewIdentityPolicy + Offset);
    if (Identity->Type == EFI_USER_INFO_IDENTITY_CREDENTIAL_PROVIDER) {
      //
      // Enroll the user on this provider
      //
      Status = EnrollUserOnProvider (Identity, mModifyUser);
      if (EFI_ERROR (Status)) {
        //
        // Failed to enroll the user by new identification policy.
        // So removed the credential provider from the identification policy
        //
        DeleteProviderFromPolicy (Identity, Offset);
        continue;
      }
    }
    Offset += Identity->Length;
  }

  return EFI_SUCCESS;
}


/**
  Check whether the identity policy is valid.

  @param[in]  PolicyInfo          Point to the identity policy.
  @param[in]  PolicyInfoLen       The policy length.

  @retval TRUE     The policy is a valid identity policy.
  @retval FALSE    The policy is not a valid identity policy.
  
**/
BOOLEAN
CheckNewIdentityPolicy (
  IN  UINT8                                     *PolicyInfo,
  IN  UINTN                                     PolicyInfoLen
  )
{
  EFI_USER_INFO_IDENTITY_POLICY *Identity;
  EFI_INPUT_KEY                 Key;
  UINTN                         Offset;
  UINT32                        OpCode;
  
  //
  // Check policy expression.
  //
  OpCode  = EFI_USER_INFO_IDENTITY_FALSE;
  Offset  = 0;
  while (Offset < PolicyInfoLen) {
    //
    // Check identification policy according to type
    //
    Identity = (EFI_USER_INFO_IDENTITY_POLICY *) (PolicyInfo + Offset);
    switch (Identity->Type) {
      
    case EFI_USER_INFO_IDENTITY_TRUE:
      break;

    case EFI_USER_INFO_IDENTITY_OR:
      if (OpCode == EFI_USER_INFO_IDENTITY_AND) {
        CreatePopUp (
          EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
          &Key,
          L"Invalid Identity Policy, Mixed Connector Unsupport!",
          L"",
          L"Press Any Key to Continue ...",
          NULL
          );
        return FALSE;
      }

      OpCode = EFI_USER_INFO_IDENTITY_OR;
      break;

    case EFI_USER_INFO_IDENTITY_AND:
      if (OpCode == EFI_USER_INFO_IDENTITY_OR) {
        CreatePopUp (
          EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
          &Key,
          L"Invalid Identity Policy, Mixed Connector Unsupport!",
          L"",
          L"Press Any Key to Continue ...",
          NULL
          );
        return FALSE;
      }

      OpCode = EFI_USER_INFO_IDENTITY_AND;
      break;

    case EFI_USER_INFO_IDENTITY_CREDENTIAL_PROVIDER:
      break;

    default:
      CreatePopUp (
        EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
        &Key,
        L"Unsupport parameter",
        L"",
        L"Press Any Key to Continue ...",
        NULL
        );
      return FALSE;
    }
    Offset += Identity->Length;
  }

  return TRUE;
}


/**
  Save the identity policy and update UI with it.
  
  This funciton will verify the new identity policy, in current implementation, 
  the identity policy can be:  T, P & P & P & ..., P | P | P | ...
  Here, "T" means "True", "P" means "Credential Provider", "&" means "and", "|" means "or".
  Other identity policies are not supported.  

**/
VOID
SaveIdentityPolicy (
  VOID
  )
{
  EFI_STATUS                    Status;
  EFI_USER_INFO_HANDLE          UserInfo;
  EFI_USER_INFO                 *Info;

  if (!mUserInfo.NewIdentityPolicyModified || (mUserInfo.NewIdentityPolicyLen == 0)) {
    return;
  }

  //
  // Check policy expression.
  //
  if (!CheckNewIdentityPolicy (mUserInfo.NewIdentityPolicy, mUserInfo.NewIdentityPolicyLen)) {
    return;
  }

  Status = FindInfoByType (mModifyUser, EFI_USER_INFO_IDENTITY_POLICY_RECORD, &UserInfo);
  if (EFI_ERROR (Status)) {
    return ;
  }
  
  //
  // Update the informantion on credential provider.
  //
  Status = UpdateCredentialProvider ();
  if (EFI_ERROR (Status)) {
    return ;
  }
  
  //
  // Save new identification policy.
  //
  Info = AllocateZeroPool (sizeof (EFI_USER_INFO) + mUserInfo.NewIdentityPolicyLen);
  ASSERT (Info != NULL);

  Info->InfoType    = EFI_USER_INFO_IDENTITY_POLICY_RECORD;
  Info->InfoAttribs = EFI_USER_INFO_STORAGE_PLATFORM_NV | EFI_USER_INFO_PUBLIC | EFI_USER_INFO_EXCLUSIVE;
  Info->InfoSize    = (UINT32) (sizeof (EFI_USER_INFO) + mUserInfo.NewIdentityPolicyLen);
  CopyMem ((UINT8 *) (Info + 1), mUserInfo.NewIdentityPolicy, mUserInfo.NewIdentityPolicyLen);

  Status = mUserManager->SetInfo (mUserManager, mModifyUser, &UserInfo, Info, Info->InfoSize);
  FreePool (Info);
   
  //
  // Update the mUserInfo.IdentityPolicy by mUserInfo.NewIdentityPolicy
  //
  if (mUserInfo.IdentityPolicy != NULL) {
    FreePool (mUserInfo.IdentityPolicy);
  }
  mUserInfo.IdentityPolicy    = mUserInfo.NewIdentityPolicy;
  mUserInfo.IdentityPolicyLen = mUserInfo.NewIdentityPolicyLen;

  mUserInfo.NewIdentityPolicy         = NULL;
  mUserInfo.NewIdentityPolicyLen      = 0;
  mUserInfo.NewIdentityPolicyModified = FALSE;   

  //
  // Update identity policy choice.
  //
  ResolveIdentityPolicy (mUserInfo.IdentityPolicy, mUserInfo.IdentityPolicyLen, STRING_TOKEN (STR_IDENTIFY_POLICY_VAL));
}


/**
  Update the mUserInfo.NewIdentityPolicy, and UI when 'add option' is pressed.

**/
VOID
AddIdentityPolicyItem (
  VOID
  )
{
  if (mProviderInfo->Count == 0) {
    return ;
  }
  
  //
  // Check the identity policy.
  //
  if (ProviderAlreadyInPolicy (&mProviderInfo->Provider[mProviderChoice]->Identifier)) {
    return;
  }

  //
  // Add it to identification policy
  //
  AddProviderToPolicy (&mProviderInfo->Provider[mProviderChoice]->Identifier);

  //
  // Update identity policy choice.
  //
  ResolveIdentityPolicy (mUserInfo.NewIdentityPolicy, mUserInfo.NewIdentityPolicyLen, STRING_TOKEN (STR_IDENTIFY_POLICY_VALUE));
}