File: authenticode.go

package info (click to toggle)
relic 7.6.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,108 kB
  • sloc: sh: 230; makefile: 10
file content (94 lines) | stat: -rw-r--r-- 2,972 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//
// Copyright (c) SAS Institute Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package authenticode

import (
	"context"
	"crypto"
	"encoding/asn1"
	"errors"

	"github.com/sassoftware/relic/v7/lib/certloader"
	"github.com/sassoftware/relic/v7/lib/pkcs7"
	"github.com/sassoftware/relic/v7/lib/pkcs9"
	"github.com/sassoftware/relic/v7/lib/x509tools"
)

type OpusParams struct {
	Description string
	URL         string
}

func makePeIndirect(imprint []byte, hash crypto.Hash, oid asn1.ObjectIdentifier) (indirect SpcIndirectDataContentPe, err error) {
	alg, ok := x509tools.PkixDigestAlgorithm(hash)
	if !ok {
		err = errors.New("unsupported digest algorithm")
		return
	}
	indirect.Data.Type = oid
	indirect.MessageDigest.Digest = imprint
	indirect.MessageDigest.DigestAlgorithm = alg
	indirect.Data.Value.File.File = NewSpcString("")
	return
}

func signIndirect(ctx context.Context, indirect interface{}, hash crypto.Hash, cert *certloader.Certificate, params *OpusParams) (*pkcs9.TimestampedSignature, error) {
	sig := pkcs7.NewBuilder(cert.Signer(), cert.Chain(), hash)
	if err := sig.SetContent(OidSpcIndirectDataContent, indirect); err != nil {
		return nil, err
	}
	if err := addOpusAttrs(sig, params); err != nil {
		return nil, err
	}
	psd, err := sig.Sign()
	if err != nil {
		return nil, err
	}
	return pkcs9.TimestampAndMarshal(ctx, psd, cert.Timestamper, true)
}

func addOpusAttrs(sig *pkcs7.SignatureBuilder, params *OpusParams) error {
	if err := sig.AddAuthenticatedAttribute(OidSpcStatementType, SpcSpStatementType{Type: OidSpcIndividualPurpose}); err != nil {
		return err
	}
	var info SpcSpOpusInfo
	if params != nil {
		if params.Description != "" {
			info.ProgramName = NewSpcString(params.Description)
		}
		if params.URL != "" {
			info.MoreInfo.URL = params.URL
		}
	}
	if err := sig.AddAuthenticatedAttribute(OidSpcSpOpusInfo, info); err != nil {
		return err
	}
	return nil
}

func SignSip(ctx context.Context, imprint []byte, hash crypto.Hash, sipInfo SpcSipInfo, cert *certloader.Certificate, params *OpusParams) (*pkcs9.TimestampedSignature, error) {
	alg, ok := x509tools.PkixDigestAlgorithm(hash)
	if !ok {
		return nil, errors.New("unsupported digest algorithm")
	}
	var indirect SpcIndirectDataContentMsi
	indirect.Data.Type = OidSpcSipInfo
	indirect.Data.Value = sipInfo
	indirect.MessageDigest.Digest = imprint
	indirect.MessageDigest.DigestAlgorithm = alg
	return signIndirect(ctx, indirect, hash, cert, params)
}