summaryrefslogtreecommitdiff
path: root/src/libstrongswan/plugins/pkcs1/pkcs1_encoder.c
blob: 2c3bf6e7ce7ebb615353d3c75117b3e03fcd0649 (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
/*
 * Copyright (C) 2009 Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "pkcs1_encoder.h"

#include <utils/debug.h>
#include <asn1/asn1.h>
#include <asn1/oid.h>

/**
 * Encode a public key in PKCS#1/ASN.1 DER
 */
static bool build_pub(chunk_t *encoding, va_list args)
{
	chunk_t n, e;

	if (cred_encoding_args(args, CRED_PART_RSA_MODULUS, &n,
						   CRED_PART_RSA_PUB_EXP, &e, CRED_PART_END))
	{
		*encoding = asn1_wrap(ASN1_SEQUENCE, "mm",
						asn1_wrap(ASN1_INTEGER, "c", n),
						asn1_wrap(ASN1_INTEGER, "c", e));
		return TRUE;
	}
	return FALSE;
}

/**
 * Encode a public key in PKCS#1/ASN.1 DER, contained in subjectPublicKeyInfo
 */
static bool build_pub_info(chunk_t *encoding, va_list args)
{
	chunk_t n, e;

	if (cred_encoding_args(args, CRED_PART_RSA_MODULUS, &n,
						   CRED_PART_RSA_PUB_EXP, &e, CRED_PART_END))
	{
		*encoding = asn1_wrap(ASN1_SEQUENCE, "mm",
						asn1_algorithmIdentifier(OID_RSA_ENCRYPTION),
						asn1_bitstring("m",
							asn1_wrap(ASN1_SEQUENCE, "mm",
								asn1_wrap(ASN1_INTEGER, "c", n),
								asn1_wrap(ASN1_INTEGER, "c", e))));
		return TRUE;
	}
	return FALSE;
}

/**
 * Encode a private key in PKCS#1/ASN.1 DER
 */
static bool build_priv(chunk_t *encoding, va_list args)
{
	chunk_t n, e, d, p, q, exp1, exp2, coeff;

	if (cred_encoding_args(args, CRED_PART_RSA_MODULUS, &n,
					CRED_PART_RSA_PUB_EXP, &e, CRED_PART_RSA_PRIV_EXP, &d,
					CRED_PART_RSA_PRIME1, &p, CRED_PART_RSA_PRIME2, &q,
					CRED_PART_RSA_EXP1, &exp1, CRED_PART_RSA_EXP2, &exp2,
					CRED_PART_RSA_COEFF, &coeff, CRED_PART_END))
	{
		*encoding = asn1_wrap(ASN1_SEQUENCE, "cmmssssss",
						ASN1_INTEGER_0,
						asn1_wrap(ASN1_INTEGER, "c", n),
						asn1_wrap(ASN1_INTEGER, "c", e),
						asn1_wrap(ASN1_INTEGER, "c", d),
						asn1_wrap(ASN1_INTEGER, "c", p),
						asn1_wrap(ASN1_INTEGER, "c", q),
						asn1_wrap(ASN1_INTEGER, "c", exp1),
						asn1_wrap(ASN1_INTEGER, "c", exp2),
						asn1_wrap(ASN1_INTEGER, "c", coeff));
		return TRUE;
	}
	return FALSE;
}

/**
 * Build the SHA1 hash of pubkey(info) ASN.1 data
 */
static bool hash_pubkey(chunk_t pubkey, chunk_t *hash)
{
	hasher_t *hasher;

	hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
	if (!hasher || !hasher->allocate_hash(hasher, pubkey, hash))
	{
		DESTROY_IF(hasher);
		chunk_free(&pubkey);
		DBG1(DBG_LIB, "SHA1 hash algorithm not supported, "
			 "fingerprinting failed");
		return FALSE;
	}
	hasher->destroy(hasher);
	chunk_free(&pubkey);
	return TRUE;
}

/**
 * build the fingerprint of the subjectPublicKeyInfo object
 */
static bool build_info_sha1(chunk_t *encoding, va_list args)
{
	chunk_t pubkey;

	if (build_pub_info(&pubkey, args))
	{
		return hash_pubkey(pubkey, encoding);
	}
	return FALSE;
}

/**
 * build the fingerprint of the subjectPublicKey object
 */
static bool build_sha1(chunk_t *encoding, va_list args)
{
	chunk_t pubkey;

	if (build_pub(&pubkey, args))
	{
		return hash_pubkey(pubkey, encoding);
	}
	return FALSE;
}

/**
 * See header.
 */
bool pkcs1_encoder_encode(cred_encoding_type_t type, chunk_t *encoding,
						  va_list args)
{
	switch (type)
	{
		case KEYID_PUBKEY_INFO_SHA1:
			return build_info_sha1(encoding, args);
		case KEYID_PUBKEY_SHA1:
			return build_sha1(encoding, args);
		case PUBKEY_ASN1_DER:
			return build_pub(encoding, args);
		case PUBKEY_SPKI_ASN1_DER:
			return build_pub_info(encoding, args);
		case PRIVKEY_ASN1_DER:
			return build_priv(encoding, args);
		default:
			return FALSE;
	}
}