blob: 87b82ec9d7a8ed39f002ebf4103a2fcf92a8ac8e [file] [log] [blame]
andrewsbrownaada3932015-04-01 14:09:51 -07001/*
2 * jndn-mock
3 * Copyright (c) 2015, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU Lesser General Public License,
7 * version 3, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT ANY
10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
12 * more details.
13 */
14package com.intel.jndn.mock;
15
16import net.named_data.jndn.Name;
17import net.named_data.jndn.security.KeyChain;
18import net.named_data.jndn.security.identity.IdentityManager;
19import net.named_data.jndn.security.identity.IdentityStorage;
20import net.named_data.jndn.security.identity.MemoryIdentityStorage;
21import net.named_data.jndn.security.identity.MemoryPrivateKeyStorage;
22import net.named_data.jndn.security.identity.PrivateKeyStorage;
23import net.named_data.jndn.security.policy.SelfVerifyPolicyManager;
24
25/**
26 *
27 * @author Andrew Brown <andrew.brown@intel.com>
28 */
29public class MockKeyChain {
30
31 /**
32 * Build and configure an in-memory {@link KeyChain}.
33 *
34 * @param name the name of the default identity to create
35 * @return an in-memory {@link KeyChain} configured with the name as the
36 * default identity
37 * @throws net.named_data.jndn.security.SecurityException
38 */
39 public static KeyChain configure(Name name) throws net.named_data.jndn.security.SecurityException {
andrewsbrownaada3932015-04-01 14:09:51 -070040 PrivateKeyStorage keyStorage = new MemoryPrivateKeyStorage();
41 IdentityStorage identityStorage = new MemoryIdentityStorage();
42 KeyChain keyChain = new KeyChain(new IdentityManager(identityStorage, keyStorage),
43 new SelfVerifyPolicyManager(identityStorage));
44
45 // create keys, certs if necessary
46 if (!identityStorage.doesIdentityExist(name)) {
Alexander Afanasyev83a26d32016-01-26 01:04:32 -080047 keyChain.createIdentityAndCertificate(name);
andrewsbrownaada3932015-04-01 14:09:51 -070048 }
49
50 // set default identity
51 keyChain.getIdentityManager().setDefaultIdentity(name);
52
53 return keyChain;
54 }
55}