1/*
2 * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * Neither the name of ARM nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific
16 * prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include <getopt.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35
36#include <openssl/conf.h>
37#include <openssl/evp.h>
38#include <openssl/pem.h>
39
40#include "cert.h"
41#include "debug.h"
42#include "key.h"
43#include "platform_oid.h"
44#include "sha.h"
45
46#define MAX_FILENAME_LEN		1024
47
48/*
49 * Create a new key
50 */
51int key_new(key_t *key)
52{
53	RSA *rsa = NULL;
54	EVP_PKEY *k = NULL;
55
56	/* Create key pair container */
57	k = EVP_PKEY_new();
58	if (k == NULL) {
59		return 0;
60	}
61
62	/* Generate a new RSA key */
63	rsa = RSA_generate_key(RSA_KEY_BITS, RSA_F4, NULL, NULL);
64	if (EVP_PKEY_assign_RSA(k, rsa)) {
65		key->key = k;
66		return 1;
67	} else {
68		printf("Cannot assign RSA key\n");
69	}
70
71	if (k)
72		EVP_PKEY_free(k);
73	return 0;
74}
75
76int key_load(key_t *key)
77{
78	FILE *fp = NULL;
79	EVP_PKEY *k = NULL;
80
81	/* Create key pair container */
82	k = EVP_PKEY_new();
83	if (k == NULL) {
84		return 0;
85	}
86
87	if (key->fn) {
88		/* Load key from file */
89		fp = fopen(key->fn, "r");
90		if (fp) {
91			k = PEM_read_PrivateKey(fp, &k, NULL, NULL);
92			fclose(fp);
93			if (k) {
94				key->key = k;
95				return 1;
96			} else {
97				ERROR("Cannot read key from %s\n", key->fn);
98			}
99		} else {
100			ERROR("Cannot open file %s\n", key->fn);
101		}
102	} else {
103		ERROR("Key filename not specified\n");
104	}
105
106	if (k)
107		EVP_PKEY_free(k);
108
109	return 0;
110}
111
112int key_store(key_t *key)
113{
114	FILE *fp = NULL;
115
116	if (key->fn) {
117		fp = fopen(key->fn, "w");
118		if (fp) {
119			PEM_write_PrivateKey(fp, key->key,
120					NULL, NULL, 0, NULL, NULL);
121			fclose(fp);
122			return 1;
123		} else {
124			ERROR("Cannot create file %s\n", key->fn);
125		}
126	} else {
127		ERROR("Key filename not specified\n");
128	}
129
130	return 0;
131}
132