| 1 | # CMS [](https://pkg.go.dev/github.com/github/smimesign/ietf-cms?tab=doc) |
| 2 | |
| 3 | [CMS (Cryptographic Message Syntax)](https://tools.ietf.org/html/rfc5652) is a syntax for signing, digesting, and encrypting arbitrary messages. It evolved from PKCS#7 and is the basis for higher level protocols such as S/MIME. This package implements the SignedData CMS content-type, allowing users to digitally sign data as well as verify data signed by others. |
| 4 | |
| 5 | ## Signing and Verifying Data |
| 6 | |
| 7 | High level APIs are provided for signing a message with a certificate and key: |
| 8 | |
| 9 | ```go |
| 10 | msg := []byte("some data") |
| 11 | cert, _ := x509.ParseCertificate(someCertificateData) |
| 12 | key, _ := x509.ParseECPrivateKey(somePrivateKeyData) |
| 13 | |
| 14 | der, _ := cms.Sign(msg, []*x509.Certificate{cert}, key) |
| 15 | |
| 16 | //// |
| 17 | /// At another time, in another place... |
| 18 | // |
| 19 | |
| 20 | sd, _ := ParseSignedData(der) |
| 21 | if err, _ := sd.Verify(x509.VerifyOptions{}); err != nil { |
| 22 | panic(err) |
| 23 | } |
| 24 | ``` |
| 25 | |
| 26 | By default, CMS SignedData includes the original message. High level APIs are also available for creating and verifying detached signatures: |
| 27 | |
| 28 | ```go |
| 29 | msg := []byte("some data") |
| 30 | cert, _ := x509.ParseCertificate(someCertificateData) |
| 31 | key, _ := x509.ParseECPrivateKey(somePrivateKeyData) |
| 32 | |
| 33 | der, _ := cms.SignDetached(msg, cert, key) |
| 34 | |
| 35 | //// |
| 36 | /// At another time, in another place... |
| 37 | // |
| 38 | |
| 39 | sd, _ := ParseSignedData(der) |
| 40 | if err, _ := sd.VerifyDetached(msg, x509.VerifyOptions{}); err != nil { |
| 41 | panic(err) |
| 42 | } |
| 43 | ``` |
| 44 | |
| 45 | ## Timestamping |
| 46 | |
| 47 | Because certificates expire and can be revoked, it is may be helpful to attach certified timestamps to signatures, proving that they existed at a given time. RFC3161 timestamps can be added to signatures like so: |
| 48 | |
| 49 | ```go |
| 50 | signedData, _ := NewSignedData([]byte("Hello, world!")) |
| 51 | signedData.Sign(identity.Chain(), identity.PrivateKey) |
| 52 | signedData.AddTimestamps("http://timestamp.digicert.com") |
| 53 | |
| 54 | derEncoded, _ := signedData.ToDER() |
| 55 | io.Copy(os.Stdout, bytes.NewReader(derEncoded)) |
| 56 | ``` |
| 57 | |
| 58 | Verification functions implicitly verify timestamps as well. Without a timestamp, verification will fail if the certificate is no longer valid. |