@@ -8,6 +8,7 @@ import json
88import os
99import sys
1010from contextlib import suppress
11+ from pathlib import Path
1112from tempfile import NamedTemporaryFile
1213
1314# The signing config in this trust_config is not used: it's just here
@@ -97,6 +98,52 @@ with NamedTemporaryFile(mode="wt") as temp_file:
9798 # `sigstore` requires `verify identity` for identity based verifications.
9899 subcommand , * fixed_args = fixed_args
99100 if subcommand == "sign" :
101+ if "--in-toto" in fixed_args :
102+ # Handle DSSE signing via library call
103+ fixed_args .remove ("--in-toto" )
104+
105+ from sigstore .dsse import Statement
106+ from sigstore .models import ClientTrustConfig
107+ from sigstore .oidc import IdentityToken
108+ from sigstore .sign import SigningContext
109+
110+ identity_token = None
111+ bundle_path = None
112+ try :
113+ i = fixed_args .index ("--identity-token" )
114+ identity_token = fixed_args [i + 1 ]
115+ i = fixed_args .index ("--bundle" )
116+ bundle_path = fixed_args [i + 1 ]
117+ except (ValueError , IndexError ):
118+ raise ValueError ("Missing required arguments for DSSE signing" )
119+
120+ # The statement is always the last argument in the protocol
121+ input_file = fixed_args [- 1 ]
122+
123+ # Direct library call
124+ with open (input_file , "rb" ) as f :
125+ statement_bytes = f .read ()
126+
127+ statement = Statement (statement_bytes )
128+
129+ if trusted_root_path is not None :
130+ trust_config_obj = ClientTrustConfig .from_json (Path (temp_file .name ).read_text ())
131+ else :
132+ trust_config_obj = ClientTrustConfig .production ()
133+ if "--staging" in sys .argv :
134+ trust_config_obj = ClientTrustConfig .staging ()
135+
136+ context = SigningContext .from_trust_config (trust_config_obj )
137+ token = IdentityToken (identity_token )
138+
139+ with context .signer (token ) as signer :
140+ bundle = signer .sign_dsse (statement )
141+
142+ with open (bundle_path , "w" ) as f :
143+ f .write (bundle .to_json ())
144+
145+ # Exit successfully after handling DSSE
146+ sys .exit (0 )
100147 command .append ("sign" )
101148 elif subcommand == "verify" :
102149 command .extend (["verify" , "identity" ])
0 commit comments