forked from mozilla-services/pkcs7
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
318f32c
commit c3bf0ff
Showing
4 changed files
with
98 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package pkcs7 | ||
|
||
import "encoding/asn1" | ||
|
||
func (p7 *PKCS7) Marshal() ([]byte, error) { | ||
var contentType asn1.ObjectIdentifier | ||
switch p7.raw.(type) { | ||
case signedData: | ||
contentType = OIDSignedData | ||
case envelopedData: | ||
contentType = OIDEnvelopedData | ||
case encryptedData: | ||
contentType = OIDEncryptedData | ||
default: | ||
return nil, ErrUnsupportedContentType | ||
} | ||
inner, err := asn1.Marshal(p7.raw) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return asn1.Marshal(contentInfo{ | ||
ContentType: contentType, | ||
Content: asn1.RawValue{Class: 2, Tag: 0, Bytes: inner, IsCompound: true}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package pkcs7 | ||
|
||
import ( | ||
"bytes" | ||
"crypto/x509" | ||
"encoding/pem" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestPKCS7_Marshal(t *testing.T) { | ||
content := []byte("Hello World") | ||
rootCert, err := createTestCertificateByIssuer("PKCS7 Test Root CA", nil, x509.SHA256WithRSA, true) | ||
if err != nil { | ||
t.Fatalf("cannot generate root cert: %s", err) | ||
} | ||
truststore := x509.NewCertPool() | ||
truststore.AddCert(rootCert.Certificate) | ||
signerCert, err := createTestCertificateByIssuer("PKCS7 Test Signer Cert", rootCert, x509.SHA256WithRSA, false) | ||
if err != nil { | ||
t.Fatalf("cannot generate signer cert: %s", err) | ||
} | ||
toBeSigned, err := NewSignedData(content) | ||
if err != nil { | ||
t.Fatalf("cannot initialize signed data: %s", err) | ||
} | ||
|
||
// Set the digest to match the end entity cert | ||
signerDigest, _ := getDigestOIDForSignatureAlgorithm(signerCert.Certificate.SignatureAlgorithm) | ||
toBeSigned.SetDigestAlgorithm(signerDigest) | ||
|
||
if err := toBeSigned.AddSignerChain(signerCert.Certificate, *signerCert.PrivateKey, nil, SignerInfoConfig{}); err != nil { | ||
t.Fatalf("cannot add signer: %s", err) | ||
} | ||
signed, err := toBeSigned.Finish() | ||
if err != nil { | ||
t.Fatalf("cannot finish signing data: %s", err) | ||
} | ||
pem.Encode(os.Stdout, &pem.Block{Type: "PKCS7", Bytes: signed}) | ||
|
||
p7, err := Parse(signed) | ||
if err != nil { | ||
t.Fatalf("cannot parse signed data: %s", err) | ||
} | ||
|
||
marshaled, err := p7.Marshal() | ||
if err != nil { | ||
t.Fatalf("cannot marshal signed data: %s", err) | ||
} | ||
p7Reparsed, err := Parse(marshaled) | ||
if err != nil { | ||
t.Fatalf("cannot reparse signed data: %s", err) | ||
} | ||
if !bytes.Equal(p7.Content, p7Reparsed.Content) { | ||
t.Errorf("content was not found in the reparsed data:\n\tExpected: %s\n\tActual: %s", p7.Content, p7Reparsed.Content) | ||
} | ||
if err := p7Reparsed.VerifyWithChain(truststore); err != nil { | ||
t.Errorf("cannot verify reparsed data: %s", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters