accesscontrol_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. // Test: re-issuing authorization with the same seedAuthorizationID yields
  76. // the same value
  77. reauth, _, err := IssueAuthorization(correctSigningKey, id, expires)
  78. if err != nil {
  79. t.Fatalf("IssueAuthorization failed: %s", err)
  80. }
  81. if auth != reauth {
  82. t.Fatalf("unexpected difference in authorizations")
  83. }
  84. // Decode the signed authorization and check that the auth ID in the JSON
  85. // matches the one returned by IssueAuthorization.
  86. decodedAuthorization, err := base64.StdEncoding.DecodeString(auth)
  87. if err != nil {
  88. t.Fatalf("DecodeString failed: %s", err)
  89. }
  90. type partialSignedAuthorization struct {
  91. Authorization json.RawMessage
  92. }
  93. var partialSignedAuth partialSignedAuthorization
  94. err = json.Unmarshal(decodedAuthorization, &partialSignedAuth)
  95. if err != nil {
  96. t.Fatalf("Unmarshal failed: %s", err)
  97. }
  98. var unmarshaledAuth map[string]interface{}
  99. err = json.Unmarshal(partialSignedAuth.Authorization, &unmarshaledAuth)
  100. if err != nil {
  101. t.Fatalf("Unmarshal failed: %s", err)
  102. }
  103. authID, ok := unmarshaledAuth["ID"].(string)
  104. if !ok {
  105. t.Fatalf("Failed to find auth ID in unmarshaled auth: %s", unmarshaledAuth)
  106. }
  107. if string(authID) != base64.StdEncoding.EncodeToString(issuedID) {
  108. t.Fatalf("Expected auth ID in signed auth (%s) to match that returned by IssueAuthorization (%s)", string(authID), base64.StdEncoding.EncodeToString(issuedID))
  109. }
  110. fmt.Printf("encoded authorization length: %d\n", len(auth))
  111. verifiedAuth, err := VerifyAuthorization(keyRing, auth)
  112. if err != nil {
  113. t.Fatalf("VerifyAuthorization failed: %s", err)
  114. }
  115. if verifiedAuth.AccessType != correctAccess {
  116. t.Fatalf("unexpected access type: %s", verifiedAuth.AccessType)
  117. }
  118. // Test: expired authorization
  119. expires = time.Now().Add(-10 * time.Second)
  120. auth, _, err = IssueAuthorization(correctSigningKey, id, expires)
  121. if err != nil {
  122. t.Fatalf("IssueAuthorization failed: %s", err)
  123. }
  124. _, err = VerifyAuthorization(keyRing, auth)
  125. // TODO: check error message?
  126. if err == nil {
  127. t.Fatalf("VerifyAuthorization unexpected success")
  128. }
  129. // Test: authorization signed with key not in key ring
  130. expires = time.Now().Add(10 * time.Second)
  131. auth, _, err = IssueAuthorization(invalidSigningKey, id, expires)
  132. if err != nil {
  133. t.Fatalf("IssueAuthorization failed: %s", err)
  134. }
  135. _, err = VerifyAuthorization(keyRing, auth)
  136. // TODO: check error message?
  137. if err == nil {
  138. t.Fatalf("VerifyAuthorization unexpected success")
  139. }
  140. // Test: authorization signed with valid key, but hacked access type
  141. expires = time.Now().Add(10 * time.Second)
  142. auth, _, err = IssueAuthorization(otherSigningKey, id, expires)
  143. if err != nil {
  144. t.Fatalf("IssueAuthorization failed: %s", err)
  145. }
  146. decodedAuth, err := base64.StdEncoding.DecodeString(auth)
  147. if err != nil {
  148. t.Fatalf("DecodeString failed: %s", err)
  149. }
  150. var hackSignedAuth signedAuthorization
  151. err = json.Unmarshal(decodedAuth, &hackSignedAuth)
  152. if err != nil {
  153. t.Fatalf("Unmarshal failed: %s", err)
  154. }
  155. var hackAuth Authorization
  156. err = json.Unmarshal(hackSignedAuth.Authorization, &hackAuth)
  157. if err != nil {
  158. t.Fatalf("Unmarshal failed: %s", err)
  159. }
  160. hackAuth.AccessType = correctAccess
  161. marshaledAuth, err := json.Marshal(hackAuth)
  162. if err != nil {
  163. t.Fatalf("Marshall failed: %s", err)
  164. }
  165. hackSignedAuth.Authorization = marshaledAuth
  166. marshaledSignedAuth, err := json.Marshal(hackSignedAuth)
  167. if err != nil {
  168. t.Fatalf("Marshall failed: %s", err)
  169. }
  170. encodedSignedAuth := base64.StdEncoding.EncodeToString(marshaledSignedAuth)
  171. _, err = VerifyAuthorization(keyRing, encodedSignedAuth)
  172. // TODO: check error message?
  173. if err == nil {
  174. t.Fatalf("VerifyAuthorization unexpected success")
  175. }
  176. }