Documentation
¶
Overview ¶
Package cmsdetector provides functions for detecting and identifying various CMS (Cryptographic Message Syntax) and PKCS (Public Key Cryptography Standards) formats.
Example (Detect) ¶
ExampleDetect demonstrates the basic usage of the Detect function
package main
import (
"fmt"
"github.com/lEx0/cmsdetector"
)
func main() {
// In a real application, this would be data from a file
// Here we'll just use a placeholder to demonstrate the API
data := []byte("This would be binary CMS data")
// Try to detect the CMS/PKCS format
result, err := cmsdetector.Detect(data)
if err != nil {
fmt.Printf("Error detecting format: %v\n", err)
return
}
// Use the detection results
fmt.Printf("Detected format: %s\n", result.Type)
fmt.Printf("Content type OID: %s\n", result.ContentType.String())
}
Output: Error detecting format: failed to parse ASN.1 structure: asn1: structure error: tags don't match (16 vs {class:1 tag:20 length:104 isCompound:false}) {optional:false explicit:false application:false private:false defaultValue:<nil> tag:<nil> stringType:0 timeType:0 set:false omitEmpty:false} ContentInfo @2
Example (FileDetection) ¶
ExampleFileDetection demonstrates how to detect the format of a file
package main
import (
"fmt"
"os"
"github.com/lEx0/cmsdetector"
)
func main() {
// Skip this example when running tests
if os.Getenv("SKIP_FILE_EXAMPLES") != "" {
fmt.Println("Skipping file example")
return
}
// The path to your CMS/PKCS file
filePath := "тест.jpg.cms"
// filePath := "тест.pdf"
// Read the file
data, err := os.ReadFile(filePath)
if err != nil {
fmt.Printf("Error reading file: %v\n", err)
return
}
// Detect the format
result, err := cmsdetector.Detect(data)
if err != nil {
fmt.Printf("Error detecting format: %v\n", err)
return
}
// Process based on the detected format
switch {
case cmsdetector.IsPKCS7SignedData(data):
fmt.Println("This is a signed PKCS#7 file")
case cmsdetector.IsPKCS7EnvelopedData(data):
fmt.Println("This is an enveloped PKCS#7 file")
case cmsdetector.IsPKCS12(data):
fmt.Println("This is a PKCS#12 certificate store")
default:
fmt.Printf("This is a %s file\n", result.Type)
}
}
Output: Error reading file: open тест.jpg.cms: no such file or directory
Example (UsingHelperFunctions) ¶
ExampleUsingHelperFunctions demonstrates how to use the helper functions
package main
import (
"fmt"
"github.com/lEx0/cmsdetector"
)
func main() {
// In a real application, this would be data from a file
// Here we'll just use a placeholder to demonstrate the API
data := []byte("This would be binary CMS data")
// Use the specific format detection functions
if cmsdetector.IsPKCS7SignedData(data) {
fmt.Println("This contains PKCS#7 signed data")
} else {
fmt.Println("This is not PKCS#7 signed data")
}
if cmsdetector.IsPKCS7EnvelopedData(data) {
fmt.Println("This contains PKCS#7 enveloped data")
} else {
fmt.Println("This is not PKCS#7 enveloped data")
}
if cmsdetector.IsPKCS12(data) {
fmt.Println("This is a PKCS#12 container")
} else {
fmt.Println("This is not a PKCS#12 container")
}
}
Output: This is not PKCS#7 signed data This is not PKCS#7 enveloped data This is not a PKCS#12 container
Index ¶
- Constants
- Variables
- func GetOIDDescription(oid asn1.ObjectIdentifier) string
- func IsPKCS12(data []byte) bool
- func IsPKCS7Data(data []byte) bool
- func IsPKCS7EnvelopedData(data []byte) bool
- func IsPKCS7SignedData(data []byte) bool
- func IsUserKeyPKCS12(data []byte) bool
- type ContentInfo
- type DetectionResult
Examples ¶
Constants ¶
const (
TypeEncryptedPKCS12 = "Encrypted PKCS#12"
)
Additional type constants for formats that can't be detected via OID
Variables ¶
var ( // PKCS#7 OIDs PKCS7DataOID = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 1} PKCS7SignedDataOID = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 2} PKCS7EnvelopedDataOID = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 3} PKCS7SignedAndEnvelopedOID = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 4} PKCS7DigestedDataOID = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 5} PKCS7EncryptedDataOID = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 6} // Other common OIDs for CMS/PKCS PKCS12OID = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 12, 10, 1} )
OIDs for various types of CMS/PKCS messages
Functions ¶
func GetOIDDescription ¶
func GetOIDDescription(oid asn1.ObjectIdentifier) string
GetOIDDescription returns a human-readable description of the OID
func IsPKCS7EnvelopedData ¶
IsPKCS7EnvelopedData checks if the data is PKCS#7 enveloped data
func IsPKCS7SignedData ¶
IsPKCS7SignedData checks if the data is PKCS#7 signed data
func IsUserKeyPKCS12 ¶
IsUserKeyPKCS12 checks if the data appears to be a user PKCS#12 key container
Types ¶
type ContentInfo ¶
type ContentInfo struct {
ContentType asn1.ObjectIdentifier
Content asn1.RawValue `asn1:"explicit,optional,tag:0"`
}
ContentInfo provides the ASN.1 structure for the main CMS/PKCS container
type DetectionResult ¶
type DetectionResult struct {
Type string
ContentType asn1.ObjectIdentifier
IsEncrypted bool // Indicates if the content is encrypted
}
DetectionResult contains the result of CMS/PKCS type detection
func Detect ¶
func Detect(data []byte) (DetectionResult, error)
Detect tries to determine the type of CMS/PKCS data