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",
33 asn1_wrap(ASN1_INTEGER
, "c", n
),
34 asn1_wrap(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",
54 asn1_wrap(ASN1_INTEGER
, "c", n
),
55 asn1_wrap(ASN1_INTEGER
, "c", e
))));
62 * Encode a private key in PKCS#1/ASN.1 DER
64 static bool build_priv(chunk_t
*encoding
, va_list args
)
66 chunk_t n
, e
, d
, p
, q
, exp1
, exp2
, coeff
;
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
))
74 *encoding
= asn1_wrap(ASN1_SEQUENCE
, "cmmssssss",
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
));
90 * Build the SHA1 hash of pubkey(info) ASN.1 data
92 static bool hash_pubkey(chunk_t pubkey
, chunk_t
*hash
)
96 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, HASH_SHA1
);
97 if (!hasher
|| !hasher
->allocate_hash(hasher
, pubkey
, hash
))
101 DBG1(DBG_LIB
, "SHA1 hash algorithm not supported, "
102 "fingerprinting failed");
105 hasher
->destroy(hasher
);
111 * build the fingerprint of the subjectPublicKeyInfo object
113 static bool build_info_sha1(chunk_t
*encoding
, va_list args
)
117 if (build_pub_info(&pubkey
, args
))
119 return hash_pubkey(pubkey
, encoding
);
125 * build the fingerprint of the subjectPublicKey object
127 static bool build_sha1(chunk_t
*encoding
, va_list args
)
131 if (build_pub(&pubkey
, args
))
133 return hash_pubkey(pubkey
, encoding
);
141 bool pkcs1_encoder_encode(cred_encoding_type_t type
, chunk_t
*encoding
,
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
);