summaryrefslogtreecommitdiff
path: root/verity/KeystoreSigner.java
blob: d57f328fe01d45ec544cd66ddbf0dc073a910381 (plain)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * 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 com.android.verity;

import java.io.IOException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.ASN1EncodableVector;
import org.bouncycastle.asn1.ASN1Integer;
import org.bouncycastle.asn1.ASN1Object;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.DEROctetString;
import org.bouncycastle.asn1.DERPrintableString;
import org.bouncycastle.asn1.DERSequence;
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
import org.bouncycastle.asn1.pkcs.RSAPublicKey;
import org.bouncycastle.asn1.util.ASN1Dump;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;

/**
 * AndroidVerifiedBootKeystore DEFINITIONS ::=
 * BEGIN
 *     FormatVersion ::= INTEGER
 *     KeyBag ::= SEQUENCE {
 *         Key  ::= SEQUENCE {
 *             AlgorithmIdentifier  ::=  SEQUENCE {
 *                 algorithm OBJECT IDENTIFIER,
 *                 parameters ANY DEFINED BY algorithm OPTIONAL
 *             }
 *             KeyMaterial ::= RSAPublicKey
 *         }
 *     }
 *     Signature ::= AndroidVerifiedBootSignature
 * END
 */

class BootKey extends ASN1Object
{
    private AlgorithmIdentifier algorithmIdentifier;
    private RSAPublicKey keyMaterial;

    public BootKey(PublicKey key) throws Exception {
        java.security.interfaces.RSAPublicKey k =
                (java.security.interfaces.RSAPublicKey) key;
        this.keyMaterial = new RSAPublicKey(
                k.getModulus(),
                k.getPublicExponent());
        this.algorithmIdentifier = new AlgorithmIdentifier(
                PKCSObjectIdentifiers.sha256WithRSAEncryption);
    }

    public ASN1Primitive toASN1Primitive() {
        ASN1EncodableVector v = new ASN1EncodableVector();
        v.add(algorithmIdentifier);
        v.add(keyMaterial);
        return new DERSequence(v);
    }

    public void dump() throws Exception {
        System.out.println(ASN1Dump.dumpAsString(toASN1Primitive()));
    }
}

class BootKeystore extends ASN1Object
{
    private ASN1Integer                     formatVersion;
    private ASN1EncodableVector             keyBag;
    private BootSignature    signature;

    public BootKeystore() {
        this.formatVersion = new ASN1Integer(0);
        this.keyBag = new ASN1EncodableVector();
    }

    public void addPublicKey(byte[] der) throws Exception {
        PublicKey pubkey = Utils.loadDERPublicKey(der);
        BootKey k = new BootKey(pubkey);
        keyBag.add(k);
    }

    public byte[] getInnerKeystore() throws Exception {
        ASN1EncodableVector v = new ASN1EncodableVector();
        v.add(formatVersion);
        v.add(new DERSequence(keyBag));
        return new DERSequence(v).getEncoded();
    }

    public ASN1Primitive toASN1Primitive() {
        ASN1EncodableVector v = new ASN1EncodableVector();
        v.add(formatVersion);
        v.add(new DERSequence(keyBag));
        v.add(signature);
        return new DERSequence(v);
    }

    public void sign(PrivateKey privateKey) throws Exception {
        byte[] innerKeystore = getInnerKeystore();
        byte[] rawSignature = Utils.sign(privateKey, innerKeystore);
        signature = new BootSignature("keystore", innerKeystore.length);
        signature.setSignature(rawSignature);
    }

    public void dump() throws Exception {
        System.out.println(ASN1Dump.dumpAsString(toASN1Primitive()));
    }

    // USAGE:
    //      AndroidVerifiedBootKeystoreSigner <privkeyFile> <outfile> <pubkeyFile0> ... <pubkeyFileN-1>
    // EG:
    //     java -cp ../../../out/host/common/obj/JAVA_LIBRARIES/AndroidVerifiedBootKeystoreSigner_intermediates/classes/ com.android.verity.AndroidVerifiedBootKeystoreSigner ../../../build/target/product/security/verity_private_dev_key /tmp/keystore.out /tmp/k
    public static void main(String[] args) throws Exception {
        String privkeyFname = args[0];
        String outfileFname = args[1];
        BootKeystore ks = new BootKeystore();
        for (int i=2; i < args.length; i++) {
            ks.addPublicKey(Utils.read(args[i]));
        }
        ks.sign(Utils.loadPEMPrivateKeyFromFile(privkeyFname));
        Utils.write(ks.getEncoded(), outfileFname);
    }
}