2 * Copyright (C) 2009 Martin Willi
3 * Hochschule fuer Technik Rapperswil
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>.
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
16 #include "pkcs1_encoder.h"
18 #include <utils/debug.h>
19 #include <asn1/asn1.h>
23 * Encode a public key in PKCS#1/ASN.1 DER
25 static bool build_pub(chunk_t
*encoding
, va_list args
)
29 if (cred_encoding_args(args
, CRED_PART_RSA_MODULUS
, &n
,
30 CRED_PART_RSA_PUB_EXP
, &e
, CRED_PART_END
))
32 *encoding
= asn1_wrap(ASN1_SEQUENCE
, "mm",
34 asn1_integer("c", e
));
41 * Encode a public key in PKCS#1/ASN.1 DER, contained in subjectPublicKeyInfo
43 static bool build_pub_info(chunk_t
*encoding
, va_list args
)
47 if (cred_encoding_args(args
, CRED_PART_RSA_MODULUS
, &n
,
48 CRED_PART_RSA_PUB_EXP
, &e
, CRED_PART_END
))
50 *encoding
= asn1_wrap(ASN1_SEQUENCE
, "mm",
51 asn1_algorithmIdentifier(OID_RSA_ENCRYPTION
),
53 asn1_wrap(ASN1_SEQUENCE
, "mm",
55 asn1_integer("c", e
))));
62 * Encode the RSA modulus of a public key only
64 static bool build_pub_modulus(chunk_t
*encoding
, va_list args
)
68 if (cred_encoding_args(args
, CRED_PART_RSA_MODULUS
, &n
, CRED_PART_END
))
70 /* remove preceding zero bytes */
71 while (n
.len
> 0 && *n
.ptr
== 0x00)
76 *encoding
= chunk_clone(n
);
83 * Encode a private key in PKCS#1/ASN.1 DER
85 static bool build_priv(chunk_t
*encoding
, va_list args
)
87 chunk_t n
, e
, d
, p
, q
, exp1
, exp2
, coeff
;
89 if (cred_encoding_args(args
, CRED_PART_RSA_MODULUS
, &n
,
90 CRED_PART_RSA_PUB_EXP
, &e
, CRED_PART_RSA_PRIV_EXP
, &d
,
91 CRED_PART_RSA_PRIME1
, &p
, CRED_PART_RSA_PRIME2
, &q
,
92 CRED_PART_RSA_EXP1
, &exp1
, CRED_PART_RSA_EXP2
, &exp2
,
93 CRED_PART_RSA_COEFF
, &coeff
, CRED_PART_END
))
95 *encoding
= asn1_wrap(ASN1_SEQUENCE
, "cmmssssss",
100 asn1_integer("c", p
),
101 asn1_integer("c", q
),
102 asn1_integer("c", exp1
),
103 asn1_integer("c", exp2
),
104 asn1_integer("c", coeff
));
111 * Build the SHA1 hash of pubkey(info) ASN.1 data
113 static bool hash_pubkey(chunk_t pubkey
, chunk_t
*hash
)
117 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, HASH_SHA1
);
118 if (!hasher
|| !hasher
->allocate_hash(hasher
, pubkey
, hash
))
122 DBG1(DBG_LIB
, "SHA1 hash algorithm not supported, "
123 "fingerprinting failed");
126 hasher
->destroy(hasher
);
132 * build the fingerprint of the subjectPublicKeyInfo object
134 static bool build_info_sha1(chunk_t
*encoding
, va_list args
)
138 if (build_pub_info(&pubkey
, args
))
140 return hash_pubkey(pubkey
, encoding
);
146 * build the fingerprint of the subjectPublicKey object
148 static bool build_sha1(chunk_t
*encoding
, va_list args
)
152 if (build_pub(&pubkey
, args
))
154 return hash_pubkey(pubkey
, encoding
);
162 bool pkcs1_encoder_encode(cred_encoding_type_t type
, chunk_t
*encoding
,
167 case KEYID_PUBKEY_INFO_SHA1
:
168 return build_info_sha1(encoding
, args
);
169 case KEYID_PUBKEY_SHA1
:
170 return build_sha1(encoding
, args
);
171 case PUBKEY_ASN1_DER
:
172 return build_pub(encoding
, args
);
173 case PUBKEY_SPKI_ASN1_DER
:
174 return build_pub_info(encoding
, args
);
175 case PUBKEY_RSA_MODULUS
:
176 return build_pub_modulus(encoding
, args
);
177 case PRIVKEY_ASN1_DER
:
178 return build_priv(encoding
, args
);