accesscontrol_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright (c) 2018, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package accesscontrol
  20. import (
  21. "encoding/base64"
  22. "encoding/json"
  23. "fmt"
  24. "testing"
  25. "time"
  26. )
  27. func TestAuthorization(t *testing.T) {
  28. correctAccess := "access1"
  29. otherAccess := "access2"
  30. correctSigningKey, correctVerificationKey, err := NewKeyPair(correctAccess)
  31. if err != nil {
  32. t.Fatalf("NewKeyPair failed: %s", err)
  33. }
  34. otherSigningKey, otherVerificationKey, err := NewKeyPair(otherAccess)
  35. if err != nil {
  36. t.Fatalf("NewKeyPair failed: %s", err)
  37. }
  38. invalidSigningKey, _, err := NewKeyPair(correctAccess)
  39. if err != nil {
  40. t.Fatalf("NewKeyPair failed: %s", err)
  41. }
  42. keyRing := &VerificationKeyRing{
  43. Keys: []*VerificationKey{correctVerificationKey, otherVerificationKey},
  44. }
  45. // Test: valid key
  46. err = ValidateSigningKey(correctSigningKey)
  47. if err != nil {
  48. t.Fatalf("ValidateSigningKey failed: %s", err)
  49. }
  50. // Test: invalid key
  51. err = ValidateSigningKey(&SigningKey{})
  52. if err == nil {
  53. t.Fatalf("ValidateSigningKey unexpected success")
  54. }
  55. // Test: valid key ring
  56. err = ValidateVerificationKeyRing(keyRing)
  57. if err != nil {
  58. t.Fatalf("ValidateVerificationKeyRing failed: %s", err)
  59. }
  60. // Test: invalid key ring
  61. invalidKeyRing := &VerificationKeyRing{
  62. Keys: []*VerificationKey{&VerificationKey{}},
  63. }
  64. err = ValidateVerificationKeyRing(invalidKeyRing)
  65. if err == nil {
  66. t.Fatalf("ValidateVerificationKeyRing unexpected success")
  67. }
  68. // Test: valid authorization
  69. id := []byte("0000000000000001")
  70. expires := time.Now().Add(10 * time.Second)
  71. auth, issuedID, err := IssueAuthorization(correctSigningKey, id, expires)
  72. if err != nil {
  73. t.Fatalf("IssueAuthorization failed: %s", err)
  74. }
  75. // Decode the signed authorization and check that the auth ID in the JSON
  76. // matches the one returned by IssueAuthorization.
  77. decodedAuthorization, err := base64.StdEncoding.DecodeString(auth)
  78. if err != nil {
  79. t.Fatalf("DecodeString failed: %s", err)
  80. }
  81. type partialSignedAuthorization struct {
  82. Authorization json.RawMessage
  83. }
  84. var partialSignedAuth partialSignedAuthorization
  85. err = json.Unmarshal(decodedAuthorization, &partialSignedAuth)
  86. if err != nil {
  87. t.Fatalf("Unmarshal failed: %s", err)
  88. }
  89. var unmarshaledAuth map[string]interface{}
  90. err = json.Unmarshal(partialSignedAuth.Authorization, &unmarshaledAuth)
  91. if err != nil {
  92. t.Fatalf("Unmarshal failed: %s", err)
  93. }
  94. authID, ok := unmarshaledAuth["ID"].(string)
  95. if !ok {
  96. t.Fatalf("Failed to find auth ID in unmarshaled auth: %s", unmarshaledAuth)
  97. }
  98. if string(authID) != base64.StdEncoding.EncodeToString(issuedID) {
  99. t.Fatalf("Expected auth ID in signed auth (%s) to match that returned by IssueAuthorization (%s)", string(authID), base64.StdEncoding.EncodeToString(issuedID))
  100. }
  101. fmt.Printf("encoded authorization length: %d\n", len(auth))
  102. verifiedAuth, err := VerifyAuthorization(keyRing, auth)
  103. if err != nil {
  104. t.Fatalf("VerifyAuthorization failed: %s", err)
  105. }
  106. if verifiedAuth.AccessType != correctAccess {
  107. t.Fatalf("unexpected access type: %s", verifiedAuth.AccessType)
  108. }
  109. // Test: expired authorization
  110. expires = time.Now().Add(-10 * time.Second)
  111. auth, _, err = IssueAuthorization(correctSigningKey, id, expires)
  112. if err != nil {
  113. t.Fatalf("IssueAuthorization failed: %s", err)
  114. }
  115. _, err = VerifyAuthorization(keyRing, auth)
  116. // TODO: check error message?
  117. if err == nil {
  118. t.Fatalf("VerifyAuthorization unexpected success")
  119. }
  120. // Test: authorization signed with key not in key ring
  121. expires = time.Now().Add(10 * time.Second)
  122. auth, _, err = IssueAuthorization(invalidSigningKey, id, expires)
  123. if err != nil {
  124. t.Fatalf("IssueAuthorization failed: %s", err)
  125. }
  126. _, err = VerifyAuthorization(keyRing, auth)
  127. // TODO: check error message?
  128. if err == nil {
  129. t.Fatalf("VerifyAuthorization unexpected success")
  130. }
  131. // Test: authorization signed with valid key, but hacked access type
  132. expires = time.Now().Add(10 * time.Second)
  133. auth, _, err = IssueAuthorization(otherSigningKey, id, expires)
  134. if err != nil {
  135. t.Fatalf("IssueAuthorization failed: %s", err)
  136. }
  137. decodedAuth, err := base64.StdEncoding.DecodeString(auth)
  138. if err != nil {
  139. t.Fatalf("DecodeString failed: %s", err)
  140. }
  141. var hackSignedAuth signedAuthorization
  142. err = json.Unmarshal(decodedAuth, &hackSignedAuth)
  143. if err != nil {
  144. t.Fatalf("Unmarshal failed: %s", err)
  145. }
  146. var hackAuth Authorization
  147. err = json.Unmarshal(hackSignedAuth.Authorization, &hackAuth)
  148. if err != nil {
  149. t.Fatalf("Unmarshal failed: %s", err)
  150. }
  151. hackAuth.AccessType = correctAccess
  152. marshaledAuth, err := json.Marshal(hackAuth)
  153. if err != nil {
  154. t.Fatalf("Marshall failed: %s", err)
  155. }
  156. hackSignedAuth.Authorization = marshaledAuth
  157. marshaledSignedAuth, err := json.Marshal(hackSignedAuth)
  158. if err != nil {
  159. t.Fatalf("Marshall failed: %s", err)
  160. }
  161. encodedSignedAuth := base64.StdEncoding.EncodeToString(marshaledSignedAuth)
  162. _, err = VerifyAuthorization(keyRing, encodedSignedAuth)
  163. // TODO: check error message?
  164. if err == nil {
  165. t.Fatalf("VerifyAuthorization unexpected success")
  166. }
  167. }