accesscontrol.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 implements an access control authorization scheme
  20. // based on digital signatures.
  21. //
  22. // Authorizations for specified access types are issued by an entity that
  23. // digitally signs each authorization. The digital signature is verified
  24. // by service providers before granting the specified access type. Each
  25. // authorization includes an expiry date and a unique ID that may be used
  26. // to mitigate malicious reuse/sharing of authorizations.
  27. //
  28. // In a typical deployment, the signing keys will be present on issuing
  29. // entities which are distinct from service providers. Only verification
  30. // keys will be deployed to service providers.
  31. //
  32. // An authorization is represented in JSON, which is then base64-encoded
  33. // for transport:
  34. //
  35. // {
  36. // "Authorization" : {
  37. // "ID" : <derived unique ID>,
  38. // "AccessType" : <access type name; e.g., "my-access">,
  39. // "Expires" : <RFC3339-encoded UTC time value>
  40. // },
  41. // "SigningKeyID" : <unique key ID>,
  42. // "Signature" : <Ed25519 digital signature>
  43. // }
  44. //
  45. package accesscontrol
  46. import (
  47. "crypto/ed25519"
  48. "crypto/rand"
  49. "crypto/sha256"
  50. "crypto/subtle"
  51. "encoding/base64"
  52. "encoding/json"
  53. "io"
  54. "time"
  55. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  56. "golang.org/x/crypto/hkdf"
  57. )
  58. const (
  59. keyIDLength = 32
  60. authorizationIDKeyLength = 32
  61. authorizationIDLength = 32
  62. )
  63. // SigningKey is the private key used to sign newly issued
  64. // authorizations for the specified access type. The key ID
  65. // is included in authorizations and identifies the
  66. // corresponding verification keys.
  67. //
  68. // AuthorizationIDKey is used to produce a unique
  69. // authentication ID that cannot be mapped back to its seed
  70. // value.
  71. type SigningKey struct {
  72. ID []byte
  73. AccessType string
  74. AuthorizationIDKey []byte
  75. PrivateKey []byte
  76. }
  77. // VerificationKey is the public key used to verify signed
  78. // authentications issued for the specified access type. The
  79. // authorization references the expected public key by ID.
  80. type VerificationKey struct {
  81. ID []byte
  82. AccessType string
  83. PublicKey []byte
  84. }
  85. // NewKeyPair generates a new authorization signing key pair.
  86. func NewKeyPair(
  87. accessType string) (*SigningKey, *VerificationKey, error) {
  88. ID := make([]byte, keyIDLength)
  89. _, err := rand.Read(ID)
  90. if err != nil {
  91. return nil, nil, errors.Trace(err)
  92. }
  93. authorizationIDKey := make([]byte, authorizationIDKeyLength)
  94. _, err = rand.Read(authorizationIDKey)
  95. if err != nil {
  96. return nil, nil, errors.Trace(err)
  97. }
  98. publicKey, privateKey, err := ed25519.GenerateKey(rand.Reader)
  99. if err != nil {
  100. return nil, nil, errors.Trace(err)
  101. }
  102. signingKey := &SigningKey{
  103. ID: ID,
  104. AccessType: accessType,
  105. AuthorizationIDKey: authorizationIDKey,
  106. PrivateKey: privateKey,
  107. }
  108. verificationKey := &VerificationKey{
  109. ID: ID,
  110. AccessType: accessType,
  111. PublicKey: publicKey,
  112. }
  113. return signingKey, verificationKey, nil
  114. }
  115. // Authorization describes an authorization, with a unique ID,
  116. // granting access to a specified access type, and expiring at
  117. // the specified time.
  118. //
  119. // An Authorization is embedded within a digitally signed
  120. // object. This wrapping object adds a signature and a signing
  121. // key ID.
  122. type Authorization struct {
  123. ID []byte
  124. AccessType string
  125. Expires time.Time
  126. }
  127. type signedAuthorization struct {
  128. Authorization json.RawMessage
  129. SigningKeyID []byte
  130. Signature []byte
  131. }
  132. // ValidateSigningKey checks that a signing key is correctly configured.
  133. func ValidateSigningKey(signingKey *SigningKey) error {
  134. if len(signingKey.ID) != keyIDLength ||
  135. len(signingKey.AccessType) < 1 ||
  136. len(signingKey.AuthorizationIDKey) != authorizationIDKeyLength ||
  137. len(signingKey.PrivateKey) != ed25519.PrivateKeySize {
  138. return errors.TraceNew("invalid signing key")
  139. }
  140. return nil
  141. }
  142. // IssueAuthorization issues an authorization signed with the
  143. // specified signing key.
  144. //
  145. // seedAuthorizationID should be a value that uniquely identifies
  146. // the purchase, subscription, or transaction that backs the
  147. // authorization; a distinct unique authorization ID will be derived
  148. // from the seed without revealing the original value. The authorization
  149. // ID is to be used to mitigate malicious authorization reuse/sharing.
  150. //
  151. // The first return value is a base64-encoded, serialized JSON representation
  152. // of the signed authorization that can be passed to VerifyAuthorization. The
  153. // second return value is the unique ID of the signed authorization returned in
  154. // the first value.
  155. func IssueAuthorization(
  156. signingKey *SigningKey,
  157. seedAuthorizationID []byte,
  158. expires time.Time) (string, []byte, error) {
  159. err := ValidateSigningKey(signingKey)
  160. if err != nil {
  161. return "", nil, errors.Trace(err)
  162. }
  163. hkdf := hkdf.New(sha256.New, signingKey.AuthorizationIDKey, nil, seedAuthorizationID)
  164. ID := make([]byte, authorizationIDLength)
  165. _, err = io.ReadFull(hkdf, ID)
  166. if err != nil {
  167. return "", nil, errors.Trace(err)
  168. }
  169. auth := Authorization{
  170. ID: ID,
  171. AccessType: signingKey.AccessType,
  172. Expires: expires.UTC(),
  173. }
  174. authJSON, err := json.Marshal(auth)
  175. if err != nil {
  176. return "", nil, errors.Trace(err)
  177. }
  178. signature := ed25519.Sign(signingKey.PrivateKey, authJSON)
  179. signedAuth := signedAuthorization{
  180. Authorization: authJSON,
  181. SigningKeyID: signingKey.ID,
  182. Signature: signature,
  183. }
  184. signedAuthJSON, err := json.Marshal(signedAuth)
  185. if err != nil {
  186. return "", nil, errors.Trace(err)
  187. }
  188. encodedSignedAuth := base64.StdEncoding.EncodeToString(signedAuthJSON)
  189. return encodedSignedAuth, ID, nil
  190. }
  191. // VerificationKeyRing is a set of verification keys to be deployed
  192. // to a service provider for verifying access authorizations.
  193. type VerificationKeyRing struct {
  194. Keys []*VerificationKey
  195. }
  196. // ValidateVerificationKeyRing checks that a verification key ring is
  197. // correctly configured.
  198. func ValidateVerificationKeyRing(keyRing *VerificationKeyRing) error {
  199. for _, key := range keyRing.Keys {
  200. if len(key.ID) != keyIDLength ||
  201. len(key.AccessType) < 1 ||
  202. len(key.PublicKey) != ed25519.PublicKeySize {
  203. return errors.TraceNew("invalid verification key")
  204. }
  205. }
  206. return nil
  207. }
  208. // VerifyAuthorization verifies the signed authorization and, when
  209. // verified, returns the embedded Authorization struct with the
  210. // access control information.
  211. //
  212. // The key ID in the signed authorization is used to select the
  213. // appropriate verification key from the key ring.
  214. func VerifyAuthorization(
  215. keyRing *VerificationKeyRing,
  216. encodedSignedAuthorization string) (*Authorization, error) {
  217. err := ValidateVerificationKeyRing(keyRing)
  218. if err != nil {
  219. return nil, errors.Trace(err)
  220. }
  221. signedAuthorizationJSON, err := base64.StdEncoding.DecodeString(
  222. encodedSignedAuthorization)
  223. if err != nil {
  224. return nil, errors.Trace(err)
  225. }
  226. var signedAuth signedAuthorization
  227. err = json.Unmarshal(signedAuthorizationJSON, &signedAuth)
  228. if err != nil {
  229. return nil, errors.Trace(err)
  230. }
  231. if len(signedAuth.SigningKeyID) != keyIDLength {
  232. return nil, errors.TraceNew("invalid key ID length")
  233. }
  234. if len(signedAuth.Signature) != ed25519.SignatureSize {
  235. return nil, errors.TraceNew("invalid signature length")
  236. }
  237. var verificationKey *VerificationKey
  238. for _, key := range keyRing.Keys {
  239. if subtle.ConstantTimeCompare(signedAuth.SigningKeyID, key.ID) == 1 {
  240. verificationKey = key
  241. }
  242. }
  243. if verificationKey == nil {
  244. return nil, errors.TraceNew("invalid key ID")
  245. }
  246. if !ed25519.Verify(
  247. verificationKey.PublicKey, signedAuth.Authorization, signedAuth.Signature) {
  248. return nil, errors.TraceNew("invalid signature")
  249. }
  250. var auth Authorization
  251. err = json.Unmarshal(signedAuth.Authorization, &auth)
  252. if err != nil {
  253. return nil, errors.Trace(err)
  254. }
  255. if len(auth.ID) == 0 {
  256. return nil, errors.TraceNew("invalid authorization ID")
  257. }
  258. if auth.AccessType != verificationKey.AccessType {
  259. return nil, errors.TraceNew("invalid access type")
  260. }
  261. if auth.Expires.IsZero() {
  262. return nil, errors.TraceNew("invalid expiry")
  263. }
  264. if auth.Expires.Before(time.Now().UTC()) {
  265. return nil, errors.TraceNew("expired authorization")
  266. }
  267. return &auth, nil
  268. }