xref: /trunk/test/odf-validate/fixtures/signed/verify.py (revision 0e16a31d78a652c56c5c0fc79c752f48b9048df9)
1#**************************************************************
2#
3#  Licensed to the Apache Software Foundation (ASF) under one
4#  or more contributor license agreements.  See the NOTICE file
5#  distributed with this work for additional information
6#  regarding copyright ownership.  The ASF licenses this file
7#  to you under the Apache License, Version 2.0 (the
8#  "License"); you may not use this file except in compliance
9#  with the License.  You may obtain a copy of the License at
10#
11#    http://www.apache.org/licenses/LICENSE-2.0
12#
13#  Unless required by applicable law or agreed to in writing,
14#  software distributed under the License is distributed on an
15#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16#  KIND, either express or implied.  See the License for the
17#  specific language governing permissions and limitations
18#  under the License.
19#
20
21# usage: runpy.sh fixtures/signed/verify.py <port> <dir> -- loads every package in <dir> and prints what the office's
22# DocumentDigitalSignatures service says about its document signature. Needs an office with the throw-away NSS
23# database in MOZILLA_CERTIFICATE_FOLDER (start-ui.sh) to resolve the signer certificate.
24import os, sys, uno, unohelper
25from com.sun.star.frame import XStatusListener
26from unohelp import connect, desktop, load, pv
27
28port, folder = int(sys.argv[1]), sys.argv[2]
29ctx = connect(port)
30sm = ctx.ServiceManager
31d = desktop(ctx)
32for name in sorted(os.listdir(folder)):
33    path = os.path.join(folder, name)
34    if not os.path.isfile(path):
35        continue
36    url = uno.systemPathToFileUrl(path)
37    doc = load(d, path)
38    loaded = doc.getURL() == url
39    state = []
40
41    class L(unohelper.Base, XStatusListener):
42        def statusChanged(self, ev):
43            state.append(ev.State)
44
45        def disposing(self, ev):
46            pass
47    u = uno.createUnoStruct("com.sun.star.util.URL")
48    u.Complete = ".uno:Signature"
49    u = sm.createInstanceWithContext("com.sun.star.util.URLTransformer", ctx).parseStrict(u)[1]
50    dsp = doc.CurrentController.Frame.queryDispatch(u, "", 0)
51    dsp.addStatusListener(L(), u)
52    doc.close(True)
53    sf = sm.createInstanceWithContext("com.sun.star.embed.StorageFactory", ctx)
54    props = uno.Any("[]com.sun.star.beans.PropertyValue", (pv("StorageFormat", "ZipFormat"),))
55    st = uno.invoke(sf, "createInstanceWithArguments", ((url, 3, props),))
56    ds = sm.createInstanceWithArgumentsAndContext("com.sun.star.security.DocumentDigitalSignatures", ("1.2", False), ctx)
57    meta = st.openStorageElement("META-INF", 3)
58    name_ = ds.getDocumentContentSignatureDefaultStreamName()
59    infos = ds.verifyDocumentContentSignatures(st, meta.openStreamElement(name_, 3).getInputStream()) \
60        if meta.hasByName(name_) else ()
61    print("%s: loaded=%s signatures=%d .uno:Signature state=%s (0 none, 1 ok, 2 broken, 3 invalid, 4 not validated, 5 partial ok)" % (
62        name, loaded, len(infos), state[-1] if state else None))
63    for i in infos:
64        print("  signer=%s SignatureIsValid=%s CertificateStatus=%s" % (
65            i.Signer.SubjectName if i.Signer else None, i.SignatureIsValid, i.CertificateStatus))
66