| 1 | // |
| 2 | // Copyright (c) SAS Institute Inc. |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | |
| 17 | package pkcs |
| 18 | |
| 19 | // Verify PKCS#7 SignedData structures. |
| 20 | |
| 21 | import ( |
| 22 | "io/ioutil" |
| 23 | "os" |
| 24 | |
| 25 | "github.com/sassoftware/relic/v7/lib/magic" |
| 26 | "github.com/sassoftware/relic/v7/lib/pkcs7" |
| 27 | "github.com/sassoftware/relic/v7/lib/pkcs9" |
| 28 | "github.com/sassoftware/relic/v7/lib/x509tools" |
| 29 | "github.com/sassoftware/relic/v7/signers" |
| 30 | ) |
| 31 | |
| 32 | var PkcsSigner = &signers.Signer{ |
| 33 | Name: "pkcs7", |
| 34 | Magic: magic.FileTypePKCS7, |
| 35 | CertTypes: signers.CertTypeX509, |
| 36 | Sign: nil, |
| 37 | Verify: Verify, |
| 38 | } |
| 39 | |
| 40 | func init() { |
| 41 | PkcsSigner.Flags().String("content", "", "Specify file containing contents for detached signatures") |
| 42 | signers.Register(PkcsSigner) |
| 43 | } |
| 44 | |
| 45 | func Verify(f *os.File, opts signers.VerifyOpts) ([]*signers.Signature, error) { |
| 46 | blob, err := ioutil.ReadAll(f) |
| 47 | if err != nil { |
| 48 | return nil, err |
| 49 | } |
| 50 | psd, err := pkcs7.Unmarshal(blob) |
| 51 | if err != nil { |
| 52 | return nil, err |
| 53 | } |
| 54 | var cblob []byte |
| 55 | if !opts.NoDigests && opts.Content != "" { |
| 56 | cblob, err = ioutil.ReadFile(opts.Content) |
| 57 | if err != nil { |
| 58 | return nil, err |
| 59 | } |
| 60 | } |
| 61 | sig, err := psd.Content.Verify(cblob, opts.NoDigests) |
| 62 | if err != nil { |
| 63 | return nil, err |
| 64 | } |
| 65 | ts, err := pkcs9.VerifyOptionalTimestamp(sig) |
| 66 | if err != nil { |
| 67 | return nil, err |
| 68 | } |
| 69 | hash, _ := x509tools.PkixDigestToHash(ts.SignerInfo.DigestAlgorithm) |
| 70 | return []*signers.Signature{&signers.Signature{ |
| 71 | Hash: hash, |
| 72 | X509Signature: &ts, |
| 73 | }}, nil |
| 74 | } |