1 // Copyright 2018 Google LLC. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package ctfe 16 17 import ( 18 "crypto/sha256" 19 "encoding/hex" 20 "fmt" 21 "strings" 22 23 "github.com/google/certificate-transparency-go/x509" 24 ) 25 26 // CertificateQuotaUserPrefix is prepended to all User quota ids association 27 // with intermediate certificates. 28 const CertificateQuotaUserPrefix = "@intermediate" 29 30 // QuotaUserForCert returns a User quota id string for the passed in 31 // certificate. 32 // This is intended to be used for quota limiting by intermediate certificates, 33 // but the function does not enforce anything about the passed in cert. 34 // 35 // Format returned is: 36 // 37 // "CertificateQuotaUserPrefix Subject hex(SHA256(SubjectPublicKeyInfo)[0:5])" 38 // 39 // See tests for examples. 40 func QuotaUserForCert(c *x509.Certificate) string { 41 spkiHash := sha256.Sum256(c.RawSubjectPublicKeyInfo) 42 return fmt.Sprintf("%s %s %s", CertificateQuotaUserPrefix, strings.ReplaceAll(c.Subject.String(), "/", "%2F"), hex.EncodeToString(spkiHash[0:5])) 43 } 44