-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinalQueryfinal.sql
More file actions
1259 lines (1050 loc) · 28 KB
/
Copy pathfinalQueryfinal.sql
File metadata and controls
1259 lines (1050 loc) · 28 KB
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Create database finalproject3;
--======================
--encrption
--======================
--master key
CREATE MASTER KEY
ENCRYPTION BY PASSWORD = 'Bavly@123';
GO
--certificate
CREATE CERTIFICATE SRMSCert
WITH SUBJECT = 'SRMS Encryption Certificate';
GO
CREATE SYMMETRIC KEY SRMSSymKey
WITH ALGORITHM = AES_256
ENCRYPTION BY CERTIFICATE SRMSCert;
GO
--======================
--create tables
--======================
--create student tabel
CREATE TABLE Student (
StudentID INT IDENTITY PRIMARY KEY,
EncryptedStudentID VARBINARY(MAX) NOT NULL, -- AES
FullName NVARCHAR(100),
Email NVARCHAR(100),
Phone VARBINARY(MAX), -- AES
DOB DATE,
Department NVARCHAR(50),
ClearanceLevel INT -- MLS
);
---INSERT
OPEN SYMMETRIC KEY SRMSSymKey
DECRYPTION BY CERTIFICATE SRMSCert;
GO
INSERT INTO Student (
EncryptedStudentID,
FullName,
Email,
Phone,
DOB,
Department,
ClearanceLevel
)
VALUES (
EncryptByKey(Key_GUID('SRMSSymKey'), CAST(1 AS NVARCHAR(20))), -- student logical ID
'Sara Amgad',
'sara@mail.com',
EncryptByKey(Key_GUID('SRMSSymKey'), '01012345678'),
'2005-08-03',
'CS',
1
);
GO
CLOSE SYMMETRIC KEY SRMSSymKey;
GO
EXEC Instructor_InsertGrade
@Username = 'instructor1',
@StudentID = 4,
@CourseID = 1,
@Grade = 95;
SELECT name
FROM sys.procedures
WHERE name LIKE '%Grade%';
--create instructor table
CREATE TABLE Instructor (
InstructorID INT IDENTITY PRIMARY KEY,
FullName NVARCHAR(100),
Email NVARCHAR(100),
ClearanceLevel INT
);
--INSERT
INSERT INTO Instructor (
FullName,
Email,
ClearanceLevel
)
VALUES
('Dr. Ahmed Hassan', 'ahmed@uni.edu', 3),
('Dr. Mona Ali', 'mona@uni.edu', 3);
GO
--create course table
CREATE TABLE Course (
CourseID INT IDENTITY PRIMARY KEY,
CourseName NVARCHAR(100),
Description NVARCHAR(MAX),
PublicInfo NVARCHAR(MAX)
);
--INSERT
INSERT INTO Course (CourseName, Description, PublicInfo)
VALUES
(
'Database Security',
'Advanced database security concepts including RBAC and MLS',
'Open for all students'
),
(
'Operating Systems',
'Processes, memory management, scheduling',
'Basic course info'
);
GO
--create grandes table
CREATE TABLE Grades (
GradeID INT IDENTITY PRIMARY KEY,
StudentID INT,
CourseID INT,
GradeValue VARBINARY(MAX), -- AES
DateEntered DATETIME,
FOREIGN KEY (StudentID) REFERENCES Student(StudentID),
FOREIGN KEY (CourseID) REFERENCES Course(CourseID)
);
--insert and use encrption and dycrption
OPEN SYMMETRIC KEY SRMSSymKey
DECRYPTION BY CERTIFICATE SRMSCert;
INSERT INTO Grades (StudentID, CourseID, GradeValue, DateEntered, ClassificationLevel)
VALUES (
1,
1,
EncryptByKey(Key_GUID('SRMSSymKey'), CAST(95 AS NVARCHAR)),
GETDATE(),
3
);
CLOSE SYMMETRIC KEY SRMSSymKey;
--create attendance table
CREATE TABLE Attendance (
AttendanceID INT IDENTITY PRIMARY KEY,
StudentID INT,
CourseID INT,
Status BIT,
DateRecorded DATETIME,
FOREIGN KEY (StudentID) REFERENCES Student(StudentID),
FOREIGN KEY (CourseID) REFERENCES Course(CourseID)
);
INSERT INTO Attendance (StudentID, CourseID, Status, DateRecorded)
VALUES (3,1, 1, GETDATE());
SELECT * FROM Attendance;
--create user table
CREATE TABLE Users (
Username NVARCHAR(50) PRIMARY KEY,
Password VARBINARY(MAX), -- Hashed / Encrypted
Role NVARCHAR(20),
ClearanceLevel INT
);
---INSERT
INSERT INTO Users (Username, Password, Role, ClearanceLevel)
VALUES
('AdminUser', HASHBYTES('SHA2_256','admin123'), 'Admin', 4),
('InstructorUser', HASHBYTES('SHA2_256','-'), 'Instructor', 3),
('TAUser', HASHBYTES('SHA2_256','ta123'), 'TA', 2),
('StudentUser', HASHBYTES('SHA2_256','stud123'), 'Student', 1),
('GuestUser', HASHBYTES('SHA2_256','guest123'), 'Guest', 0);
GO
-- Create new student user for demo
INSERT INTO Users (Username, Password, Role, ClearanceLevel)
VALUES (
'student_demo',
HASHBYTES('SHA2_256', 'demo123'),
'Student',
1
);
--======================
--create roles
--======================
CREATE ROLE AdminRole;
CREATE ROLE InstructorRole;
CREATE ROLE TARole;
CREATE ROLE StudentRole;
CREATE ROLE GuestRole;
GO
--login
CREATE LOGIN AdminUser WITH PASSWORD = 'Admin@123';
CREATE LOGIN InstructorUser WITH PASSWORD = 'Inst@123';
CREATE LOGIN TAUser WITH PASSWORD = 'TA@123';
CREATE LOGIN StudentUser WITH PASSWORD = 'Stud@123';
CREATE LOGIN GuestUser WITH PASSWORD = 'Guest@123';
GO
--users
CREATE USER AdminUser FOR LOGIN AdminUser;
CREATE USER InstructorUser FOR LOGIN InstructorUser;
CREATE USER TAUser FOR LOGIN TAUser;
CREATE USER StudentUser FOR LOGIN StudentUser;
CREATE USER GuestUser FOR LOGIN GuestUser;
GO
--exec
EXEC sp_addrolemember 'AdminRole', 'AdminUser';
EXEC sp_addrolemember 'InstructorRole', 'InstructorUser';
EXEC sp_addrolemember 'TARole', 'TAUser';
EXEC sp_addrolemember 'StudentRole', 'StudentUser';
EXEC sp_addrolemember 'GuestRole', 'GuestUser';
--======================
--access control
--======================
--deny for public
DENY SELECT,INSERT,UPDATE,DELETE ON Student TO PUBLIC;
DENY SELECT,INSERT,UPDATE,DELETE ON Grades TO PUBLIC;
DENY SELECT,INSERT,UPDATE,DELETE ON Attendance TO PUBLIC;
DENY SELECT,INSERT,UPDATE,DELETE ON Users TO PUBLIC;
--Admin
GRANT SELECT, INSERT, UPDATE, DELETE ON Student TO AdminRole;
GRANT SELECT, INSERT, UPDATE, DELETE ON Instructor TO AdminRole;
GRANT SELECT, INSERT, UPDATE, DELETE ON Course TO AdminRole;
GRANT SELECT, INSERT, UPDATE, DELETE ON Grades TO AdminRole;
GRANT SELECT, INSERT, UPDATE, DELETE ON Attendance TO AdminRole;
GRANT SELECT, INSERT, UPDATE, DELETE ON Users TO AdminRole;
--instructor
GRANT SELECT, INSERT, UPDATE ON Grades TO InstructorRole;
GRANT SELECT ON Attendance TO InstructorRole;
GRANT SELECT ON Course TO InstructorRole;
DENY DELETE ON Users TO InstructorRole;
DENY INSERT ON Users TO InstructorRole;
DENY UPDATE ON Users TO InstructorRole;
--TA
GRANT SELECT, INSERT, UPDATE ON Attendance TO TARole;
GRANT SELECT ON Student TO TARole;
GRANT SELECT ON Course TO TARole;
DENY SELECT, INSERT, UPDATE, DELETE ON Grades TO TARole;
--student
GRANT SELECT ON Student TO StudentRole;
GRANT SELECT ON Attendance TO StudentRole;
GRANT SELECT ON Grades TO StudentRole;
--guest
GRANT SELECT ON Course TO GuestRole;
DENY SELECT ON Student TO GuestRole;
DENY SELECT ON Grades TO GuestRole;
DENY SELECT ON Attendance TO GuestRole;
--=============================
--VIEWS
--=============================
--ADMIN
GO
CREATE VIEW vw_Admin_Students
AS
SELECT StudentID, FullName, Email, Department, ClearanceLevel
FROM Student;
GO
GO
CREATE VIEW vw_Admin_Grades
AS
SELECT *
FROM Grades;
GO
CREATE VIEW vw_Admin_Attendance
AS
SELECT *
FROM Attendance;
GO
CREATE VIEW vw_Admin_Users
AS
SELECT Username, Role, ClearanceLevel
FROM Users;
GO
--====================================================
--INTSRUCTOR
CREATE VIEW vw_Instructor_Grades
AS
SELECT g.GradeID, g.CourseID, g.GradeValue, g.DateEntered
FROM Grades g;
GO
CREATE VIEW vw_Instructor_Attendance
AS
SELECT a.AttendanceID, a.StudentID, a.CourseID, a.Status, a.DateRecorded
FROM Attendance a;
GO
CREATE VIEW vw_Instructor_Courses
AS
SELECT CourseID, CourseName, Description
FROM Course;
GO
--====================================================================
--TA
CREATE VIEW vw_TA_Students
AS
SELECT StudentID, FullName, Department
FROM Student;
GO
CREATE VIEW vw_TA_Attendance
AS
SELECT AttendanceID, StudentID, CourseID, Status, DateRecorded
FROM Attendance;
GO
--===========================================================
--STUDENT
CREATE VIEW vw_Student_OwnProfile
AS
SELECT
s.StudentID,
s.FullName,
s.Email,
s.Department
FROM Student s
JOIN Users u
ON u.ClearanceLevel = s.ClearanceLevel;
GO
CREATE VIEW vw_Student_OwnGrades
AS
SELECT g.GradeID, g.CourseID, g.GradeValue
FROM Grades g
JOIN Student s
ON g.StudentID = s.StudentID;
GO
CREATE VIEW vw_Student_OwnAttendance
AS
SELECT a.AttendanceID, a.CourseID, a.Status, a.DateRecorded
FROM Attendance a
JOIN Student s
ON a.StudentID = s.StudentID;
GO
--======================================================
--GUEST
CREATE VIEW vw_Guest_Courses
AS
SELECT CourseID, CourseName, PublicInfo
FROM Course;
GO
--============================
--ACCESS CONTROL TO VIEWS
--============================
GRANT SELECT ON vw_Admin_Users TO Adminrole;
GRANT SELECT ON vw_Admin_Attendance TO Adminrole;
GRANT SELECT ON vw_Admin_Grades TO Adminrole;
GRANT SELECT ON vw_Admin_Students TO Adminrole;
GRANT SELECT ON vw_Instructor_Grades TO InstructorRole;
GRANT SELECT ON vw_Instructor_Attendance TO InstructorRole;
GRANT SELECT ON vw_Instructor_Courses TO InstructorRole;
-- TA
GRANT SELECT ON vw_TA_Students TO TARole;
GRANT SELECT ON vw_TA_Attendance TO TARole;
-- Student
GRANT SELECT ON vw_Student_OwnProfile TO StudentRole;
GRANT SELECT ON vw_Student_OwnGrades TO StudentRole;
GRANT SELECT ON vw_Student_OwnAttendance TO StudentRole;
-- Guest
GRANT SELECT ON vw_Guest_Courses TO GuestRole;
--===================================================
--DENY
--===================================================
-- ADMIN VIEWS
REVOKE SELECT ON vw_Admin_Students TO PUBLIC;
REVOKE SELECT ON vw_Admin_Grades TO PUBLIC;
REVOKE SELECT ON vw_Admin_Attendance TO PUBLIC;
REVOKE SELECT ON vw_Admin_Users TO PUBLIC;
DENY SELECT ON vw_Admin_Students TO StudentRole, TARole, InstructorRole, GuestRole;
DENY SELECT ON vw_Admin_Grades TO StudentRole, TARole, InstructorRole, GuestRole;
DENY SELECT ON vw_Admin_Attendance TO StudentRole, TARole, InstructorRole, GuestRole;
-- INSTRUCTOR VIEWS
REVOKE SELECT ON vw_Instructor_Grades TO PUBLIC;
REVOKE SELECT ON vw_Instructor_Attendance TO PUBLIC;
REVOKE SELECT ON vw_Instructor_Courses TO PUBLIC;
-- TA VIEWS
REVOKE SELECT ON vw_TA_Students TO PUBLIC;
REVOKE SELECT ON vw_TA_Attendance TO PUBLIC;
-- STUDENT VIEWS
REVOKE SELECT ON vw_Student_OwnProfile TO PUBLIC;
REVOKE SELECT ON vw_Student_OwnGrades TO PUBLIC;
REVOKE SELECT ON vw_Student_OwnAttendance TO PUBLIC;
-- GUEST VIEW
REVOKE SELECT ON vw_Guest_Courses TO PUBLIC;
GO
--======================================================================================
--TEST ACCESS CONTROL
--========================================================
EXECUTE AS USER = 'AdminUser';
SELECT * FROM vw_Admin_Students; -- ✅
SELECT * FROM vw_Admin_Grades; -- ✅
SELECT * FROM vw_Guest_Courses;
REVERT;
EXECUTE AS USER = 'StudentUser';
SELECT * FROM vw_Admin_Students; -- Permission Denied
SELECT * FROM vw_Student_OwnProfile; --
REVERT;
EXECUTE AS USER = 'GuestUser';
SELECT * FROM vw_Guest_Courses; -- ✔
SELECT * FROM vw_Student_OwnGrades; -- Permission Denied
REVERT;
EXECUTE AS USER = 'InstructorUser';
-- Allowed
SELECT * FROM vw_Instructor_Grades;
SELECT * FROM vw_Instructor_Attendance;
SELECT * FROM vw_Instructor_Courses;
-- Not Allowed
SELECT * FROM vw_Admin_Students; -- Permission Denied
SELECT * FROM vw_TA_Students; -- Permission Denied
SELECT * FROM vw_Student_OwnProfile; -- Permission Denied
REVERT;
EXECUTE AS USER = 'TAUser';
-- Allowed
SELECT * FROM vw_TA_Students;
SELECT * FROM vw_TA_Attendance;
-- Not Allowed
SELECT * FROM vw_Instructor_Grades; -- Permission Denied
SELECT * FROM vw_Admin_Grades; -- Permission Denied
SELECT * FROM vw_Admin_Users; -- Permission Denied
REVERT;
--======================
--poredcure
--======================
--GetUserRole
GO
CREATE PROCEDURE GetUserRole
@Username NVARCHAR(50),
@Role NVARCHAR(20) OUTPUT,
@Clearance INT OUTPUT
AS
BEGIN
SELECT
@Role = Role,
@Clearance = ClearanceLevel
FROM Users
WHERE Username = @Username;
END;
GO
--View Own Profile (Admin / Instructor / TA / Student)
GO
CREATE PROCEDURE ViewOwnProfile
@Username NVARCHAR(50),
@StudentID INT
AS
BEGIN
DECLARE @Role NVARCHAR(20), @Clearance INT;
EXEC GetUserRole @Username, @Role OUTPUT, @Clearance OUTPUT;
IF @Role IN ('Admin', 'Instructor', 'TA', 'Student')
SELECT StudentID, FullName, Email, DOB, Department
FROM Student
WHERE StudentID = @StudentID;
ELSE
RAISERROR('Access Denied', 16, 1);
END;
GO
--View Grades (Admin / Instructor)
GO
CREATE OR ALTER PROCEDURE ViewGrades
@Username NVARCHAR(50)
AS
BEGIN
DECLARE @Role NVARCHAR(20), @Clearance INT;
EXEC GetUserRole @Username, @Role OUTPUT, @Clearance OUTPUT;
IF @Role IN ('Admin', 'Instructor')
BEGIN
OPEN SYMMETRIC KEY SRMSSymKey
DECRYPTION BY CERTIFICATE SRMSCert;
SELECT
GradeID,
StudentID,
CourseID,
CONVERT(DECIMAL, DecryptByKey(GradeValue)) AS GradeValue,
DateEntered
FROM Grades;
CLOSE SYMMETRIC KEY SRMSSymKey;
END
ELSE
RAISERROR('Access Denied: Cannot view grades', 16, 1);
END;
GO
EXEC ViewGrades @Username = 'AdminUser'; -- ✅
EXEC ViewGrades @Username = 'InstructorUser';-- ✅
EXEC ViewGrades @Username = 'TAUser'; -- ❌ Access Denied
EXEC ViewGrades @Username = 'StudentUser'; -- ❌ Access Denied
--view Own Grades (Student)
GO
CREATE PROCEDURE ViewMyGrades
@Username NVARCHAR(50),
@StudentID INT
AS
BEGIN
DECLARE @Role NVARCHAR(20), @Clearance INT;
EXEC GetUserRole @Username, @Role OUTPUT, @Clearance OUTPUT;
IF @Role = 'Student'
BEGIN
OPEN SYMMETRIC KEY SRMSSymKey
DECRYPTION BY CERTIFICATE SRMSCert;
SELECT
CourseID,
CONVERT(DECIMAL, DecryptByKey(GradeValue)) AS GradeValue
FROM Grades
WHERE StudentID = @StudentID;
CLOSE SYMMETRIC KEY SRMSSymKey;
END
ELSE
RAISERROR('Access Denied', 16, 1);
END;
GO
--Manage Attendance (Admin / Instructor / TA)
GO
CREATE PROCEDURE UpdateAttendance
@Username NVARCHAR(50),
@AttendanceID INT,
@Status BIT
AS
BEGIN
DECLARE @Role NVARCHAR(20), @Clearance INT;
EXEC GetUserRole @Username, @Role OUTPUT, @Clearance OUTPUT;
IF @Role IN ('Admin', 'Instructor', 'TA')
UPDATE Attendance
SET Status = @Status
WHERE AttendanceID = @AttendanceID;
ELSE
RAISERROR('Access Denied', 16, 1);
END;
GO
--View Public Course Info
GO
CREATE PROCEDURE ViewPublicCourses
AS
BEGIN
SELECT CourseID, CourseName, PublicInfo
FROM Course;
END;
GO
--instructor insert
CREATE PROCEDURE Instructor_InsertGrade
@Username NVARCHAR(50),
@StudentID INT,
@CourseID INT,
@Grade INT
AS
BEGIN
DECLARE @Clearance INT;
SELECT @Clearance = ClearanceLevel
FROM Users
WHERE Username = @Username;
-- Grades = Secret (3)
IF @Clearance < 3
BEGIN
RAISERROR('Access Denied: MLS No Write Down', 16, 1);
RETURN;
END
OPEN SYMMETRIC KEY SRMSSymKey
DECRYPTION BY CERTIFICATE SRMSCert;
INSERT INTO Grades (StudentID, CourseID, GradeValue, DateEntered, ClassificationLevel)
VALUES (
@StudentID,
@CourseID,
EncryptByKey(Key_GUID('SRMSSymKey'), CAST(@Grade AS NVARCHAR)),
GETDATE(),
3
);
CLOSE SYMMETRIC KEY SRMSSymKey;
END;
-----------------------------------------------------
--procedure for gui
CREATE PROCEDURE SubmitRoleRequest
AS
EXEC RequestRoleUpgrade
--istructor view
CREATE PROCEDURE Instructor_ViewGrades
@Username NVARCHAR(50)
AS
BEGIN
DECLARE @Clearance INT;
SELECT @Clearance = ClearanceLevel
FROM Users
WHERE Username = @Username;
IF @Clearance < 3
BEGIN
RAISERROR('Access Denied: No Read Up', 16, 1);
RETURN;
END
OPEN SYMMETRIC KEY SRMSSymKey
DECRYPTION BY CERTIFICATE SRMSCert;
SELECT
StudentID,
CourseID,
CONVERT(INT, DecryptByKey(GradeValue)) AS Grade,
DateEntered
FROM Grades;
CLOSE SYMMETRIC KEY SRMSSymKey;
END;
--istructor view attendance
CREATE PROCEDURE Instructor_ViewAttendance
AS
BEGIN
SELECT StudentID, CourseID, Status, DateRecorded
FROM Attendance;
END;
--student view grade
CREATE PROCEDURE Student_ViewGrades
@Username NVARCHAR(50)
AS
BEGIN
DECLARE @StudentID INT;
SELECT @StudentID = s.StudentID
FROM Student s
JOIN Users u ON u.ClearanceLevel = s.ClearanceLevel
WHERE u.Username = @Username;
OPEN SYMMETRIC KEY SRMSSymKey
DECRYPTION BY CERTIFICATE SRMSCert;
SELECT
CourseID,
CONVERT(INT, DecryptByKey(GradeValue)) AS Grade
FROM Grades
WHERE StudentID = @StudentID;
CLOSE SYMMETRIC KEY SRMSSymKey;
END;
----student view attendance
CREATE PROCEDURE Student_ViewAttendance
@Username NVARCHAR(50)
AS
BEGIN
DECLARE @StudentID INT;
SELECT @StudentID = s.StudentID
FROM Student s
JOIN Users u ON u.ClearanceLevel = s.ClearanceLevel
WHERE u.Username = @Username;
SELECT CourseID, Status, DateRecorded
FROM Attendance
WHERE StudentID = @StudentID;
END;
--virew profile
CREATE PROCEDURE Student_ViewProfile
@Username NVARCHAR(50)
AS
BEGIN
SELECT s.StudentID, s.FullName, s.Email, s.DOB, s.Department
FROM Student s
JOIN Users u ON u.ClearanceLevel = s.ClearanceLevel
WHERE u.Username = @Username;
END;
--ta
GO
CREATE PROCEDURE TA_ViewAttendance
@Username NVARCHAR(50)
AS
BEGIN
DECLARE @Role NVARCHAR(20)
SELECT @Role = Role
FROM Users
WHERE Username = @Username
IF @Role <> 'TA'
BEGIN
RAISERROR('Access Denied',16,1)
RETURN
END
SELECT StudentID, CourseID, Status, DateRecorded
FROM Attendance
END
GO
--
GO
CREATE PROCEDURE Guest_ViewCourses
AS
BEGIN
SELECT CourseID, CourseName, PublicInfo
FROM Course
END
GO
--login
CREATE OR ALTER PROCEDURE sp_Login
@Username NVARCHAR(50),
@hashedPassword VARBINARY(MAX)
AS
BEGIN
SET NOCOUNT ON;
SELECT
Role,
ClearanceLevel
FROM Users
WHERE Username = @Username
AND Password = @hashedPassword;
END;
GO
-------------------------------------------
UPDATE Users SET ClearanceLevel = 4 WHERE Role = 'Admin';
UPDATE Users SET ClearanceLevel = 3 WHERE Role = 'Instructor';
UPDATE Users SET ClearanceLevel = 2 WHERE Role = 'TA';
UPDATE Users SET ClearanceLevel = 1 WHERE Role = 'Student';
UPDATE Users SET ClearanceLevel = 0 WHERE Role = 'Guest';
--======================
--multilevel-security
--======================
--Grades = Secret
ALTER TABLE Grades
ADD ClassificationLevel INT DEFAULT 3;
--Attendance = Secret
ALTER TABLE Attendance
ADD ClassificationLevel INT DEFAULT 3;
--Student = Confidential
ALTER TABLE Student
ADD ClassificationLevel INT DEFAULT 2;
--Course = Unclassified
ALTER TABLE Course
ADD ClassificationLevel INT DEFAULT 1;
--no read up
GO
CREATE PROCEDURE MLS_ViewGrades
@Username NVARCHAR(50)
AS
BEGIN
DECLARE @UserClearance INT;
SELECT @UserClearance = ClearanceLevel
FROM Users
WHERE Username = @Username;
-- No Read Up
IF @UserClearance < 3
BEGIN
RAISERROR('MLS Violation: No Read Up', 16, 1);
RETURN;
END
OPEN SYMMETRIC KEY SRMSSymKey
DECRYPTION BY CERTIFICATE SRMSCert;
SELECT
StudentID,
CourseID,
CONVERT(DECIMAL, DecryptByKey(GradeValue)) AS Grade
FROM Grades;
CLOSE SYMMETRIC KEY SRMSSymKey;
END;
GO
--no write down
GO
CREATE PROCEDURE MLS_InsertGrade
@Username NVARCHAR(50),
@StudentID INT,
@CourseID INT,
@Grade DECIMAL
AS
BEGIN
DECLARE @UserClearance INT;
DECLARE @DataLevel INT = 3; -- Secret
SELECT @UserClearance = ClearanceLevel
FROM Users
WHERE Username = @Username;
-- No Write Down
IF @UserClearance > @DataLevel
BEGIN
RAISERROR('MLS Violation: No Write Down', 16, 1);
RETURN;
END
OPEN SYMMETRIC KEY SRMSSymKey
DECRYPTION BY CERTIFICATE SRMSCert;
INSERT INTO Grades (StudentID, CourseID, GradeValue, DateEntered, ClassificationLevel)
VALUES (
@StudentID,
@CourseID,
EncryptByKey(Key_GUID('SRMSSymKey'), CAST(@Grade AS NVARCHAR)),
GETDATE(),
3
);
CLOSE SYMMETRIC KEY SRMSSymKey;
END;
GO
--view
GO
CREATE VIEW V_MLS_StudentData
AS
SELECT *
FROM Student
WHERE ClassificationLevel <=
(SELECT ClearanceLevel FROM Users WHERE Username = USER_NAME());
--======================
--Inference Control
--======================
--Implement Query Set Size Control (minimum group size = 3)
GO
CREATE PROCEDURE IC_ViewAttendance
@Username NVARCHAR(50),
@CourseID INT
AS
BEGIN
DECLARE @Count INT;
SELECT @Count = COUNT(*)
FROM Attendance
WHERE CourseID = @CourseID;
IF @Count < 3
BEGIN
RAISERROR('Inference Control Violation: Query Set Size < 3', 16, 1);
RETURN;
END
SELECT StudentID, Status, DateRecorded
FROM Attendance
WHERE CourseID = @CourseID;
END;
GO
--Student Attendance View
GO
CREATE VIEW V_StudentAttendance
AS
SELECT
StudentID,
CourseID,
Status,
DateRecorded
FROM Attendance
WHERE StudentID =
(
SELECT StudentID
FROM Student
WHERE StudentID = CAST(SESSION_CONTEXT(N'StudentID') AS INT)
);
--in gui
EXEC sys.sp_set_session_context 'StudentID', @StudentID;
--Create restricted views for TA/Student
GO
CREATE VIEW V_TA_Students
AS
SELECT StudentID, FullName, Department
FROM Student;
--Block aggregate results that reveal identity
GO
CREATE PROCEDURE IC_AvgGrades
@Username NVARCHAR(50),
@CourseID INT
AS
BEGIN
RAISERROR('Inference Control: Aggregate queries are restricted', 16, 1);
END;
GO
--failur test
SELECT AVG(GradeValue)
FROM Grades
WHERE CourseID = 5 AND StudentID <> 101;
--======================
--flow control
--======================
--deny leak information
GO
CREATE PROCEDURE FC_ExportGrades
@Username NVARCHAR(50)
AS
BEGIN
DECLARE @UserClearance INT;
SELECT @UserClearance = ClearanceLevel
FROM Users
WHERE Username = @Username;
-- Grades = Secret (3)
IF @UserClearance < 3
BEGIN
RAISERROR('Flow Control Violation: Downward Flow Blocked', 16, 1);
RETURN;
END
RAISERROR('Export of Secret data is not allowed', 16, 1);
END;
GO
--======================
--test cases
--======================
--login
--======================
--Login Form
GO
CREATE PROCEDURE sp_Login
@username NVARCHAR(50),
@hashedPassword VARBINARY(MAX)
AS
BEGIN