unidoc / unipdf

Golang PDF library for creating and processing PDF files (pure go)
https://unidoc.io
Other
2.48k stars 250 forks source link

External Signature #423

Closed RangoW closed 3 years ago

RangoW commented 3 years ago

Hi, I need help regarding the external signature NewAdobePKCS7Detached I want to use the external signature interface pkcs11 for signing, but the formal parameter of this function is *rsa.Private instead of the general interface crypto.Signer?

// Public docs
func (s *Signer) Public() crypto.PublicKey {
    return s.Pub
}

// Sign use the pkcs11 interface
func (s *Signer) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) {
    signature, err := core.SignData(s.CertHash, digest, x509.SHA1WithRSA, false)
    if err != nil {
        return nil, err
    }
    signature = core.InvertSig(signature)
    return signature, nil
}

var (
    pdfSigner crypto.Signer
)

// getExternalSignature simulates an external service which signs the specified
// PDF file and returns its signature.
// Use the pkcs11 API for signing digest
func getExternalSignature(inputPath string, rule *config.Rule) ([]byte, error) {
    err := pdfSigner.(*Signer).Init()
    if err != nil {
        return nil, err
    }

    cert, err := pdfSigner.(*Signer).GetCert()
    if err != nil {
        return nil, err
    }

    // Sign input file.
    handler, err := sighandler.NewAdobePKCS7Detached(pdfSigner.(*rsa.PrivateKey), cert)
    if err != nil {
        return nil, err
    }

    _, signature, err := generateSignedFile(inputPath, handler, rule)
    if err != nil {
        return nil, err
    }

    return signature.Contents.Bytes(), nil
}

When running, it will report an error, the type conversion error "pdfSigner.(*rsa.PrivateKey)" ...