summaryrefslogtreecommitdiffstats
path: root/test/integration/samplecaservicecontainer/applicationfiles/CaSign.java
blob: a43e262240cf926250ed2f49c2069f57a3d8054d (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
 * Copyright 2018 Intel Corporation, 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.
 */
import java.security.*;
import sun.security.pkcs11.*;
import javax.crypto.spec.SecretKeySpec;
import java.util.*;
import sun.security.pkcs11.*;
import java.security.interfaces.*;
import javax.net.ssl.*;
import javax.crypto.spec.*;
import java.io.*;
import java.security.cert.*;
import java.security.spec.*;
import sun.security.x509.*;
import java.security.KeyStore.PrivateKeyEntry;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyManagementException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Security;
import java.security.SignatureException;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Date;
import java.util.Random;

import org.bouncycastle.pkcs.PKCS10CertificationRequest;
import org.bouncycastle.asn1.ASN1EncodableVector;
import org.bouncycastle.asn1.ASN1InputStream;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.DERSequence;
import org.bouncycastle.asn1.x500.X500Name;
import org.bouncycastle.asn1.x500.X500NameBuilder;
import org.bouncycastle.asn1.x500.style.BCStyle;
import org.bouncycastle.asn1.x509.BasicConstraints;
import org.bouncycastle.asn1.x509.Extension;
import org.bouncycastle.asn1.x509.KeyPurposeId;
import org.bouncycastle.asn1.x509.KeyUsage;
import org.bouncycastle.asn1.x509.SubjectKeyIdentifier;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.cert.CertIOException;
import org.bouncycastle.cert.X509CertificateHolder;
import org.bouncycastle.cert.X509v3CertificateBuilder;
import org.bouncycastle.cert.bc.BcX509ExtensionUtils;
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
import org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.operator.ContentSigner;
import org.bouncycastle.operator.OperatorCreationException;
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.util.io.pem.PemReader;
import org.bouncycastle.util.io.pem.PemObject;
import org.bouncycastle.asn1.pkcs.CertificationRequest;
import org.bouncycastle.asn1.x509.ExtendedKeyUsage;
import org.bouncycastle.asn1.x509.Extension;
import org.bouncycastle.asn1.x509.GeneralName;
import org.bouncycastle.asn1.x509.GeneralNames;
import org.bouncycastle.cert.jcajce.JcaX509ExtensionUtils;



import javax.xml.bind.DatatypeConverter;
import java.security.cert.X509Certificate;
import java.io.StringWriter;

public class CaSign {

	private static final KeyPurposeId[] ASN_WebUsage = new KeyPurposeId[] {
				KeyPurposeId.id_kp_serverAuth,

				KeyPurposeId.id_kp_clientAuth};

	public static void main(String[] args) throws Exception {
        //System.setProperty("javax.net.debug","all");
        //System.setProperty("javax.security.debug","all");
        if ( args.length < 2 )
            System.out.println("Arguments mismatch, Usage: CaSign  <pin> <alias>");
        for(int i = 0; i < args.length; i++) {
            System.out.println(args[i]);
        }
        // Set up the Sun PKCS 11 provider
        String configName = "/tmp/pkcs11.cfg";
        Provider p = new SunPKCS11(configName);
        //Provider p = Security.getProvider("SunPKCS11-pkcs11Test");
        if (p==null) {
           throw new RuntimeException("could not get security provider for");
        }
        Security.addProvider(p);
        // Load the key store
        char[] pin = args[0].toCharArray();
        KeyStore keyStore = KeyStore.getInstance("PKCS11", p);
        keyStore.load(null, pin);


        Enumeration<String> aliases = keyStore.aliases();
        while(aliases.hasMoreElements()){
            String alias = aliases.nextElement();
            System.out.println(alias + ": " + keyStore.getKey(alias,args[0].toCharArray()));
        }
        PrivateKeyEntry privateKeyEntry = (PrivateKeyEntry) keyStore.getEntry(args[1], null);
        PrivateKey privateKey = privateKeyEntry.getPrivateKey();

	File csrf = new File("/tmp/test.csr");
        if ( csrf == null )
            System.out.println("Make sure to copy the test.csr file to /tmp");
	Reader pemcsr = new FileReader(csrf);
	PemReader reader = new PemReader(pemcsr);
	PemObject pem = reader.readPemObject();
	PKCS10CertificationRequest csr = new PKCS10CertificationRequest(pem.getContent());

        X509Certificate caCert = (X509Certificate) privateKeyEntry.getCertificate();
        RSAPublicKey publicKey = (RSAPublicKey) caCert.getPublicKey();

        Security.addProvider(new BouncyCastleProvider());

        X500NameBuilder x500NameBld = new X500NameBuilder(BCStyle.INSTANCE);
        x500NameBld.addRDN(BCStyle.C, "US");
        x500NameBld.addRDN(BCStyle.ST, "CA");
        x500NameBld.addRDN(BCStyle.L, "local");
        x500NameBld.addRDN(BCStyle.O, "onap");
        x500NameBld.addRDN(BCStyle.CN, "test.onap.ca");
	X500Name issuer = x500NameBld.build();

	GregorianCalendar gc = new GregorianCalendar();
        Date start = gc.getTime();
	gc.add(GregorianCalendar.DAY_OF_MONTH, 1000);
	Date end = gc.getTime();

	X509Certificate x509;
        byte[] serialish = new byte[24];
	SecureRandom random = new SecureRandom();
	BigInteger bi;
        synchronized(serialish) {
		random.nextBytes(serialish);
		bi = new BigInteger(serialish);
        }
        X509v3CertificateBuilder xcb = new X509v3CertificateBuilder(issuer, bi,
		start, end, csr.getSubject(), csr.getSubjectPublicKeyInfo());



	JcaX509ExtensionUtils extUtils = new JcaX509ExtensionUtils();
		    xcb		.addExtension(Extension.basicConstraints,
                    	false, new BasicConstraints(false))
		            .addExtension(Extension.keyUsage,
		                true, new KeyUsage(KeyUsage.digitalSignature
		                                 | KeyUsage.keyEncipherment))
		            .addExtension(Extension.extendedKeyUsage,
		                          true, new ExtendedKeyUsage(ASN_WebUsage))

                    .addExtension(Extension.authorityKeyIdentifier,
		                          false, extUtils.createAuthorityKeyIdentifier(caCert))
		            .addExtension(Extension.subjectKeyIdentifier,
		                          false, extUtils.createSubjectKeyIdentifier(caCert.getPublicKey()));
		            //.addExtension(Extension.subjectAlternativeName,
		            //		false, new GeneralNames(sans));



	ContentSigner sigGen = new JcaContentSignerBuilder("SHA256WithRSA").build(privateKey);
	x509 = new JcaX509CertificateConverter().getCertificate(xcb.build(sigGen));

	StringWriter sw = new StringWriter();
        sw.write("-----BEGIN CERTIFICATE-----\n");
        sw.write(DatatypeConverter.printBase64Binary(x509.getEncoded()).replaceAll("(.{64})", "$1\n"));
        sw.write("\n-----END CERTIFICATE-----\n");
        FileWriter fw = new FileWriter("/tmp/test.cert");
        fw.write(sw.toString());
        fw.close();
        System.out.println("Done - Signed certificate at /tmp/test.cert");

   }
}