2c3bf6e7ce7ebb615353d3c75117b3e03fcd0649
[strongswan.git] / src / libstrongswan / plugins / pkcs1 / pkcs1_encoder.c
1 /*
2 * Copyright (C) 2009 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "pkcs1_encoder.h"
17
18 #include <utils/debug.h>
19 #include <asn1/asn1.h>
20 #include <asn1/oid.h>
21
22 /**
23 * Encode a public key in PKCS#1/ASN.1 DER
24 */
25 static bool build_pub(chunk_t *encoding, va_list args)
26 {
27 chunk_t n, e;
28
29 if (cred_encoding_args(args, CRED_PART_RSA_MODULUS, &n,
30 CRED_PART_RSA_PUB_EXP, &e, CRED_PART_END))
31 {
32 *encoding = asn1_wrap(ASN1_SEQUENCE, "mm",
33 asn1_wrap(ASN1_INTEGER, "c", n),
34 asn1_wrap(ASN1_INTEGER, "c", e));
35 return TRUE;
36 }
37 return FALSE;
38 }
39
40 /**
41 * Encode a public key in PKCS#1/ASN.1 DER, contained in subjectPublicKeyInfo
42 */
43 static bool build_pub_info(chunk_t *encoding, va_list args)
44 {
45 chunk_t n, e;
46
47 if (cred_encoding_args(args, CRED_PART_RSA_MODULUS, &n,
48 CRED_PART_RSA_PUB_EXP, &e, CRED_PART_END))
49 {
50 *encoding = asn1_wrap(ASN1_SEQUENCE, "mm",
51 asn1_algorithmIdentifier(OID_RSA_ENCRYPTION),
52 asn1_bitstring("m",
53 asn1_wrap(ASN1_SEQUENCE, "mm",
54 asn1_wrap(ASN1_INTEGER, "c", n),
55 asn1_wrap(ASN1_INTEGER, "c", e))));
56 return TRUE;
57 }
58 return FALSE;
59 }
60
61 /**
62 * Encode a private key in PKCS#1/ASN.1 DER
63 */
64 static bool build_priv(chunk_t *encoding, va_list args)
65 {
66 chunk_t n, e, d, p, q, exp1, exp2, coeff;
67
68 if (cred_encoding_args(args, CRED_PART_RSA_MODULUS, &n,
69 CRED_PART_RSA_PUB_EXP, &e, CRED_PART_RSA_PRIV_EXP, &d,
70 CRED_PART_RSA_PRIME1, &p, CRED_PART_RSA_PRIME2, &q,
71 CRED_PART_RSA_EXP1, &exp1, CRED_PART_RSA_EXP2, &exp2,
72 CRED_PART_RSA_COEFF, &coeff, CRED_PART_END))
73 {
74 *encoding = asn1_wrap(ASN1_SEQUENCE, "cmmssssss",
75 ASN1_INTEGER_0,
76 asn1_wrap(ASN1_INTEGER, "c", n),
77 asn1_wrap(ASN1_INTEGER, "c", e),
78 asn1_wrap(ASN1_INTEGER, "c", d),
79 asn1_wrap(ASN1_INTEGER, "c", p),
80 asn1_wrap(ASN1_INTEGER, "c", q),
81 asn1_wrap(ASN1_INTEGER, "c", exp1),
82 asn1_wrap(ASN1_INTEGER, "c", exp2),
83 asn1_wrap(ASN1_INTEGER, "c", coeff));
84 return TRUE;
85 }
86 return FALSE;
87 }
88
89 /**
90 * Build the SHA1 hash of pubkey(info) ASN.1 data
91 */
92 static bool hash_pubkey(chunk_t pubkey, chunk_t *hash)
93 {
94 hasher_t *hasher;
95
96 hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
97 if (!hasher || !hasher->allocate_hash(hasher, pubkey, hash))
98 {
99 DESTROY_IF(hasher);
100 chunk_free(&pubkey);
101 DBG1(DBG_LIB, "SHA1 hash algorithm not supported, "
102 "fingerprinting failed");
103 return FALSE;
104 }
105 hasher->destroy(hasher);
106 chunk_free(&pubkey);
107 return TRUE;
108 }
109
110 /**
111 * build the fingerprint of the subjectPublicKeyInfo object
112 */
113 static bool build_info_sha1(chunk_t *encoding, va_list args)
114 {
115 chunk_t pubkey;
116
117 if (build_pub_info(&pubkey, args))
118 {
119 return hash_pubkey(pubkey, encoding);
120 }
121 return FALSE;
122 }
123
124 /**
125 * build the fingerprint of the subjectPublicKey object
126 */
127 static bool build_sha1(chunk_t *encoding, va_list args)
128 {
129 chunk_t pubkey;
130
131 if (build_pub(&pubkey, args))
132 {
133 return hash_pubkey(pubkey, encoding);
134 }
135 return FALSE;
136 }
137
138 /**
139 * See header.
140 */
141 bool pkcs1_encoder_encode(cred_encoding_type_t type, chunk_t *encoding,
142 va_list args)
143 {
144 switch (type)
145 {
146 case KEYID_PUBKEY_INFO_SHA1:
147 return build_info_sha1(encoding, args);
148 case KEYID_PUBKEY_SHA1:
149 return build_sha1(encoding, args);
150 case PUBKEY_ASN1_DER:
151 return build_pub(encoding, args);
152 case PUBKEY_SPKI_ASN1_DER:
153 return build_pub_info(encoding, args);
154 case PRIVKEY_ASN1_DER:
155 return build_priv(encoding, args);
156 default:
157 return FALSE;
158 }
159 }
160
161