encryptionKey returns the certificate to encrypt to from the KeyDescriptors of an entity's metadata, and the EncryptionMethods listed with it: the first certificate whose KeyDescriptor has use="encryption", or else the first one whose KeyDescriptor has no use. It returns os.ErrNotExist if there is n
(keyDescriptors []KeyDescriptor)
| 159 | // or else the first one whose KeyDescriptor has no use. It returns |
| 160 | // os.ErrNotExist if there is none. |
| 161 | func encryptionKey(keyDescriptors []KeyDescriptor) (*x509.Certificate, []EncryptionMethod, error) { |
| 162 | for _, use := range []string{"encryption", ""} { |
| 163 | for _, keyDescriptor := range keyDescriptors { |
| 164 | if keyDescriptor.Use != use { |
| 165 | continue |
| 166 | } |
| 167 | for _, certificate := range keyDescriptor.KeyInfo.X509Data.X509Certificates { |
| 168 | certStr := whitespaceRegexp.ReplaceAllString(certificate.Data, "") |
| 169 | if certStr == "" { |
| 170 | continue |
| 171 | } |
| 172 | certBytes, err := base64.StdEncoding.DecodeString(certStr) |
| 173 | if err != nil { |
| 174 | return nil, nil, classify(fmt.Errorf("cannot decode certificate base64: %w", err), ErrInvalidCertificate) |
| 175 | } |
| 176 | cert, err := x509.ParseCertificate(certBytes) |
| 177 | if err != nil { |
| 178 | return nil, nil, classify(fmt.Errorf("cannot parse certificate: %w", err), ErrInvalidCertificate) |
| 179 | } |
| 180 | return cert, keyDescriptor.EncryptionMethods, nil |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | return nil, nil, os.ErrNotExist |
| 185 | } |
| 186 | |
| 187 | var whitespaceRegexp = regexp.MustCompile(`\s+`) |
| 188 |