1 /* $NetBSD: crypto.c,v 1.1.1.2 2014/04/24 12:45:41 pettai Exp $ */
4 * Copyright (c) 2004 - 2007 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 struct hx509_generate_private_context
{
43 const heim_oid
*key_oid
;
45 unsigned long num_bits
;
48 struct hx509_private_key_ops
{
50 const heim_oid
*key_oid
;
51 int (*available
)(const hx509_private_key
,
52 const AlgorithmIdentifier
*);
53 int (*get_spki
)(hx509_context
,
54 const hx509_private_key
,
55 SubjectPublicKeyInfo
*);
56 int (*export
)(hx509_context context
,
57 const hx509_private_key
,
60 int (*import
)(hx509_context
, const AlgorithmIdentifier
*,
61 const void *, size_t, hx509_key_format_t
,
63 int (*generate_private_key
)(hx509_context
,
64 struct hx509_generate_private_context
*,
66 BIGNUM
*(*get_internal
)(hx509_context
, hx509_private_key
, const char *);
69 struct hx509_private_key
{
71 const struct signature_alg
*md
;
72 const heim_oid
*signature_alg
;
80 hx509_private_key_ops
*ops
;
87 struct signature_alg
{
89 const heim_oid
*sig_oid
;
90 const AlgorithmIdentifier
*sig_alg
;
91 const heim_oid
*key_oid
;
92 const AlgorithmIdentifier
*digest_alg
;
94 #define PROVIDE_CONF 0x1
95 #define REQUIRE_SIGNER 0x2
96 #define SELF_SIGNED_OK 0x4
98 #define SIG_DIGEST 0x100
99 #define SIG_PUBLIC_SIG 0x200
100 #define SIG_SECRET 0x400
102 #define RA_RSA_USES_DIGEST_INFO 0x1000000
104 time_t best_before
; /* refuse signature made after best before date */
105 const EVP_MD
*(*evp_md
)(void);
106 int (*verify_signature
)(hx509_context context
,
107 const struct signature_alg
*,
109 const AlgorithmIdentifier
*,
110 const heim_octet_string
*,
111 const heim_octet_string
*);
112 int (*create_signature
)(hx509_context
,
113 const struct signature_alg
*,
114 const hx509_private_key
,
115 const AlgorithmIdentifier
*,
116 const heim_octet_string
*,
117 AlgorithmIdentifier
*,
118 heim_octet_string
*);
122 static const struct signature_alg
*
123 find_sig_alg(const heim_oid
*oid
);
129 static const heim_octet_string null_entry_oid
= { 2, rk_UNCONST("\x05\x00") };
131 static const unsigned sha512_oid_tree
[] = { 2, 16, 840, 1, 101, 3, 4, 2, 3 };
132 const AlgorithmIdentifier _hx509_signature_sha512_data
= {
133 { 9, rk_UNCONST(sha512_oid_tree
) }, rk_UNCONST(&null_entry_oid
)
136 static const unsigned sha384_oid_tree
[] = { 2, 16, 840, 1, 101, 3, 4, 2, 2 };
137 const AlgorithmIdentifier _hx509_signature_sha384_data
= {
138 { 9, rk_UNCONST(sha384_oid_tree
) }, rk_UNCONST(&null_entry_oid
)
141 static const unsigned sha256_oid_tree
[] = { 2, 16, 840, 1, 101, 3, 4, 2, 1 };
142 const AlgorithmIdentifier _hx509_signature_sha256_data
= {
143 { 9, rk_UNCONST(sha256_oid_tree
) }, rk_UNCONST(&null_entry_oid
)
146 static const unsigned sha1_oid_tree
[] = { 1, 3, 14, 3, 2, 26 };
147 const AlgorithmIdentifier _hx509_signature_sha1_data
= {
148 { 6, rk_UNCONST(sha1_oid_tree
) }, rk_UNCONST(&null_entry_oid
)
151 static const unsigned md5_oid_tree
[] = { 1, 2, 840, 113549, 2, 5 };
152 const AlgorithmIdentifier _hx509_signature_md5_data
= {
153 { 6, rk_UNCONST(md5_oid_tree
) }, rk_UNCONST(&null_entry_oid
)
156 static const unsigned ecPublicKey
[] ={ 1, 2, 840, 10045, 2, 1 };
157 const AlgorithmIdentifier _hx509_signature_ecPublicKey
= {
158 { 6, rk_UNCONST(ecPublicKey
) }, NULL
161 static const unsigned ecdsa_with_sha256_oid
[] ={ 1, 2, 840, 10045, 4, 3, 2 };
162 const AlgorithmIdentifier _hx509_signature_ecdsa_with_sha256_data
= {
163 { 7, rk_UNCONST(ecdsa_with_sha256_oid
) }, NULL
166 static const unsigned ecdsa_with_sha1_oid
[] ={ 1, 2, 840, 10045, 4, 1 };
167 const AlgorithmIdentifier _hx509_signature_ecdsa_with_sha1_data
= {
168 { 6, rk_UNCONST(ecdsa_with_sha1_oid
) }, NULL
171 static const unsigned rsa_with_sha512_oid
[] ={ 1, 2, 840, 113549, 1, 1, 13 };
172 const AlgorithmIdentifier _hx509_signature_rsa_with_sha512_data
= {
173 { 7, rk_UNCONST(rsa_with_sha512_oid
) }, NULL
176 static const unsigned rsa_with_sha384_oid
[] ={ 1, 2, 840, 113549, 1, 1, 12 };
177 const AlgorithmIdentifier _hx509_signature_rsa_with_sha384_data
= {
178 { 7, rk_UNCONST(rsa_with_sha384_oid
) }, NULL
181 static const unsigned rsa_with_sha256_oid
[] ={ 1, 2, 840, 113549, 1, 1, 11 };
182 const AlgorithmIdentifier _hx509_signature_rsa_with_sha256_data
= {
183 { 7, rk_UNCONST(rsa_with_sha256_oid
) }, NULL
186 static const unsigned rsa_with_sha1_oid
[] ={ 1, 2, 840, 113549, 1, 1, 5 };
187 const AlgorithmIdentifier _hx509_signature_rsa_with_sha1_data
= {
188 { 7, rk_UNCONST(rsa_with_sha1_oid
) }, NULL
191 static const unsigned rsa_with_md5_oid
[] ={ 1, 2, 840, 113549, 1, 1, 4 };
192 const AlgorithmIdentifier _hx509_signature_rsa_with_md5_data
= {
193 { 7, rk_UNCONST(rsa_with_md5_oid
) }, NULL
196 static const unsigned rsa_oid
[] ={ 1, 2, 840, 113549, 1, 1, 1 };
197 const AlgorithmIdentifier _hx509_signature_rsa_data
= {
198 { 7, rk_UNCONST(rsa_oid
) }, NULL
201 static const unsigned rsa_pkcs1_x509_oid
[] ={ 1, 2, 752, 43, 16, 1 };
202 const AlgorithmIdentifier _hx509_signature_rsa_pkcs1_x509_data
= {
203 { 6, rk_UNCONST(rsa_pkcs1_x509_oid
) }, NULL
206 static const unsigned des_rsdi_ede3_cbc_oid
[] ={ 1, 2, 840, 113549, 3, 7 };
207 const AlgorithmIdentifier _hx509_des_rsdi_ede3_cbc_oid
= {
208 { 6, rk_UNCONST(des_rsdi_ede3_cbc_oid
) }, NULL
211 static const unsigned aes128_cbc_oid
[] ={ 2, 16, 840, 1, 101, 3, 4, 1, 2 };
212 const AlgorithmIdentifier _hx509_crypto_aes128_cbc_data
= {
213 { 9, rk_UNCONST(aes128_cbc_oid
) }, NULL
216 static const unsigned aes256_cbc_oid
[] ={ 2, 16, 840, 1, 101, 3, 4, 1, 42 };
217 const AlgorithmIdentifier _hx509_crypto_aes256_cbc_data
= {
218 { 9, rk_UNCONST(aes256_cbc_oid
) }, NULL
226 heim_int2BN(const heim_integer
*i
)
230 bn
= BN_bin2bn(i
->data
, i
->length
, NULL
);
231 BN_set_negative(bn
, i
->negative
);
240 set_digest_alg(DigestAlgorithmIdentifier
*id
,
242 const void *param
, size_t length
)
246 id
->parameters
= malloc(sizeof(*id
->parameters
));
247 if (id
->parameters
== NULL
)
249 id
->parameters
->data
= malloc(length
);
250 if (id
->parameters
->data
== NULL
) {
251 free(id
->parameters
);
252 id
->parameters
= NULL
;
255 memcpy(id
->parameters
->data
, param
, length
);
256 id
->parameters
->length
= length
;
258 id
->parameters
= NULL
;
259 ret
= der_copy_oid(oid
, &id
->algorithm
);
261 if (id
->parameters
) {
262 free(id
->parameters
->data
);
263 free(id
->parameters
);
264 id
->parameters
= NULL
;
274 heim_oid2ecnid(heim_oid
*oid
)
277 * Now map to openssl OID fun
280 if (der_heim_oid_cmp(oid
, ASN1_OID_ID_EC_GROUP_SECP256R1
) == 0)
281 return NID_X9_62_prime256v1
;
282 else if (der_heim_oid_cmp(oid
, ASN1_OID_ID_EC_GROUP_SECP160R1
) == 0)
283 return NID_secp160r1
;
284 else if (der_heim_oid_cmp(oid
, ASN1_OID_ID_EC_GROUP_SECP160R2
) == 0)
285 return NID_secp160r2
;
291 parse_ECParameters(hx509_context context
,
292 heim_octet_string
*parameters
, int *nid
)
294 ECParameters ecparam
;
298 if (parameters
== NULL
) {
299 ret
= HX509_PARSING_KEY_FAILED
;
300 hx509_set_error_string(context
, 0, ret
,
301 "EC parameters missing");
305 ret
= decode_ECParameters(parameters
->data
, parameters
->length
,
308 hx509_set_error_string(context
, 0, ret
,
309 "Failed to decode EC parameters");
313 if (ecparam
.element
!= choice_ECParameters_namedCurve
) {
314 free_ECParameters(&ecparam
);
315 hx509_set_error_string(context
, 0, ret
,
316 "EC parameters is not a named curve");
317 return HX509_CRYPTO_SIG_INVALID_FORMAT
;
320 *nid
= heim_oid2ecnid(&ecparam
.u
.namedCurve
);
321 free_ECParameters(&ecparam
);
323 hx509_set_error_string(context
, 0, ret
,
324 "Failed to find matcing NID for EC curve");
325 return HX509_CRYPTO_SIG_INVALID_FORMAT
;
336 ecdsa_verify_signature(hx509_context context
,
337 const struct signature_alg
*sig_alg
,
338 const Certificate
*signer
,
339 const AlgorithmIdentifier
*alg
,
340 const heim_octet_string
*data
,
341 const heim_octet_string
*sig
)
343 const AlgorithmIdentifier
*digest_alg
;
344 const SubjectPublicKeyInfo
*spi
;
345 heim_octet_string digest
;
350 const unsigned char *p
;
353 digest_alg
= sig_alg
->digest_alg
;
355 ret
= _hx509_create_signature(context
,
365 spi
= &signer
->tbsCertificate
.subjectPublicKeyInfo
;
367 if (der_heim_oid_cmp(&spi
->algorithm
.algorithm
, ASN1_OID_ID_ECPUBLICKEY
) != 0)
368 return HX509_CRYPTO_SIG_INVALID_FORMAT
;
375 ret
= parse_ECParameters(context
, spi
->algorithm
.parameters
, &groupnid
);
377 der_free_octet_string(&digest
);
382 * Create group, key, parse key
386 group
= EC_GROUP_new_by_curve_name(groupnid
);
387 EC_KEY_set_group(key
, group
);
388 EC_GROUP_free(group
);
390 p
= spi
->subjectPublicKey
.data
;
391 len
= spi
->subjectPublicKey
.length
/ 8;
393 if (o2i_ECPublicKey(&key
, &p
, len
) == NULL
) {
395 return HX509_CRYPTO_SIG_INVALID_FORMAT
;
398 key
= SubjectPublicKeyInfo2EC_KEY(spi
);
401 ret
= ECDSA_verify(-1, digest
.data
, digest
.length
,
402 sig
->data
, sig
->length
, key
);
403 der_free_octet_string(&digest
);
406 ret
= HX509_CRYPTO_SIG_INVALID_FORMAT
;
414 ecdsa_create_signature(hx509_context context
,
415 const struct signature_alg
*sig_alg
,
416 const hx509_private_key signer
,
417 const AlgorithmIdentifier
*alg
,
418 const heim_octet_string
*data
,
419 AlgorithmIdentifier
*signatureAlgorithm
,
420 heim_octet_string
*sig
)
422 const AlgorithmIdentifier
*digest_alg
;
423 heim_octet_string indata
;
424 const heim_oid
*sig_oid
;
428 if (signer
->ops
&& der_heim_oid_cmp(signer
->ops
->key_oid
, ASN1_OID_ID_ECPUBLICKEY
) != 0)
429 _hx509_abort("internal error passing private key to wrong ops");
431 sig_oid
= sig_alg
->sig_oid
;
432 digest_alg
= sig_alg
->digest_alg
;
434 if (signatureAlgorithm
) {
435 ret
= set_digest_alg(signatureAlgorithm
, sig_oid
, "\x05\x00", 2);
437 hx509_clear_error_string(context
);
442 ret
= _hx509_create_signature(context
,
449 if (signatureAlgorithm
)
450 free_AlgorithmIdentifier(signatureAlgorithm
);
454 sig
->length
= ECDSA_size(signer
->private_key
.ecdsa
);
455 sig
->data
= malloc(sig
->length
);
456 if (sig
->data
== NULL
) {
457 der_free_octet_string(&indata
);
459 hx509_set_error_string(context
, 0, ret
, "out of memory");
463 siglen
= sig
->length
;
465 ret
= ECDSA_sign(-1, indata
.data
, indata
.length
,
466 sig
->data
, &siglen
, signer
->private_key
.ecdsa
);
467 der_free_octet_string(&indata
);
469 ret
= HX509_CMS_FAILED_CREATE_SIGATURE
;
470 hx509_set_error_string(context
, 0, ret
,
471 "ECDSA sign failed: %d", ret
);
474 if (siglen
> sig
->length
)
475 _hx509_abort("ECDSA signature prelen longer the output len");
477 sig
->length
= siglen
;
481 if (signatureAlgorithm
)
482 free_AlgorithmIdentifier(signatureAlgorithm
);
487 ecdsa_available(const hx509_private_key signer
,
488 const AlgorithmIdentifier
*sig_alg
)
490 const struct signature_alg
*sig
;
491 const EC_GROUP
*group
;
492 BN_CTX
*bnctx
= NULL
;
493 BIGNUM
*order
= NULL
;
496 if (der_heim_oid_cmp(signer
->ops
->key_oid
, &asn1_oid_id_ecPublicKey
) != 0)
497 _hx509_abort("internal error passing private key to wrong ops");
499 sig
= find_sig_alg(&sig_alg
->algorithm
);
501 if (sig
== NULL
|| sig
->digest_size
== 0)
504 group
= EC_KEY_get0_group(signer
->private_key
.ecdsa
);
508 bnctx
= BN_CTX_new();
513 if (EC_GROUP_get_order(group
, order
, bnctx
) != 1)
516 if (BN_num_bytes(order
) > sig
->digest_size
)
522 BN_clear_free(order
);
528 #endif /* HAVE_OPENSSL */
535 rsa_verify_signature(hx509_context context
,
536 const struct signature_alg
*sig_alg
,
537 const Certificate
*signer
,
538 const AlgorithmIdentifier
*alg
,
539 const heim_octet_string
*data
,
540 const heim_octet_string
*sig
)
542 const SubjectPublicKeyInfo
*spi
;
549 const unsigned char *p
;
551 memset(&di
, 0, sizeof(di
));
553 spi
= &signer
->tbsCertificate
.subjectPublicKeyInfo
;
555 p
= spi
->subjectPublicKey
.data
;
556 size
= spi
->subjectPublicKey
.length
/ 8;
558 rsa
= d2i_RSAPublicKey(NULL
, &p
, size
);
561 hx509_set_error_string(context
, 0, ret
, "out of memory");
565 tosize
= RSA_size(rsa
);
569 hx509_set_error_string(context
, 0, ret
, "out of memory");
573 retsize
= RSA_public_decrypt(sig
->length
, (unsigned char *)sig
->data
,
574 to
, rsa
, RSA_PKCS1_PADDING
);
576 ret
= HX509_CRYPTO_SIG_INVALID_FORMAT
;
577 hx509_set_error_string(context
, 0, ret
,
578 "RSA public decrypt failed: %d", retsize
);
582 if (retsize
> tosize
)
583 _hx509_abort("internal rsa decryption failure: ret > tosize");
585 if (sig_alg
->flags
& RA_RSA_USES_DIGEST_INFO
) {
587 ret
= decode_DigestInfo(to
, retsize
, &di
, &size
);
593 /* Check for extra data inside the sigature */
594 if (size
!= (size_t)retsize
) {
595 ret
= HX509_CRYPTO_SIG_INVALID_FORMAT
;
596 hx509_set_error_string(context
, 0, ret
, "size from decryption mismatch");
600 if (sig_alg
->digest_alg
&&
601 der_heim_oid_cmp(&di
.digestAlgorithm
.algorithm
,
602 &sig_alg
->digest_alg
->algorithm
) != 0)
604 ret
= HX509_CRYPTO_OID_MISMATCH
;
605 hx509_set_error_string(context
, 0, ret
, "object identifier in RSA sig mismatch");
609 /* verify that the parameters are NULL or the NULL-type */
610 if (di
.digestAlgorithm
.parameters
!= NULL
&&
611 (di
.digestAlgorithm
.parameters
->length
!= 2 ||
612 memcmp(di
.digestAlgorithm
.parameters
->data
, "\x05\x00", 2) != 0))
614 ret
= HX509_CRYPTO_SIG_INVALID_FORMAT
;
615 hx509_set_error_string(context
, 0, ret
, "Extra parameters inside RSA signature");
619 ret
= _hx509_verify_signature(context
,
625 if ((size_t)retsize
!= data
->length
||
626 ct_memcmp(to
, data
->data
, retsize
) != 0)
628 ret
= HX509_CRYPTO_SIG_INVALID_FORMAT
;
629 hx509_set_error_string(context
, 0, ret
, "RSA Signature incorrect");
637 free_DigestInfo(&di
);
644 rsa_create_signature(hx509_context context
,
645 const struct signature_alg
*sig_alg
,
646 const hx509_private_key signer
,
647 const AlgorithmIdentifier
*alg
,
648 const heim_octet_string
*data
,
649 AlgorithmIdentifier
*signatureAlgorithm
,
650 heim_octet_string
*sig
)
652 const AlgorithmIdentifier
*digest_alg
;
653 heim_octet_string indata
;
654 const heim_oid
*sig_oid
;
658 if (signer
->ops
&& der_heim_oid_cmp(signer
->ops
->key_oid
, ASN1_OID_ID_PKCS1_RSAENCRYPTION
) != 0)
659 return HX509_ALG_NOT_SUPP
;
662 sig_oid
= &alg
->algorithm
;
664 sig_oid
= signer
->signature_alg
;
666 if (der_heim_oid_cmp(sig_oid
, ASN1_OID_ID_PKCS1_SHA512WITHRSAENCRYPTION
) == 0) {
667 digest_alg
= hx509_signature_sha512();
668 } else if (der_heim_oid_cmp(sig_oid
, ASN1_OID_ID_PKCS1_SHA384WITHRSAENCRYPTION
) == 0) {
669 digest_alg
= hx509_signature_sha384();
670 } else if (der_heim_oid_cmp(sig_oid
, ASN1_OID_ID_PKCS1_SHA256WITHRSAENCRYPTION
) == 0) {
671 digest_alg
= hx509_signature_sha256();
672 } else if (der_heim_oid_cmp(sig_oid
, ASN1_OID_ID_PKCS1_SHA1WITHRSAENCRYPTION
) == 0) {
673 digest_alg
= hx509_signature_sha1();
674 } else if (der_heim_oid_cmp(sig_oid
, ASN1_OID_ID_PKCS1_MD5WITHRSAENCRYPTION
) == 0) {
675 digest_alg
= hx509_signature_md5();
676 } else if (der_heim_oid_cmp(sig_oid
, ASN1_OID_ID_PKCS1_MD5WITHRSAENCRYPTION
) == 0) {
677 digest_alg
= hx509_signature_md5();
678 } else if (der_heim_oid_cmp(sig_oid
, ASN1_OID_ID_DSA_WITH_SHA1
) == 0) {
679 digest_alg
= hx509_signature_sha1();
680 } else if (der_heim_oid_cmp(sig_oid
, ASN1_OID_ID_PKCS1_RSAENCRYPTION
) == 0) {
681 digest_alg
= hx509_signature_sha1();
682 } else if (der_heim_oid_cmp(sig_oid
, ASN1_OID_ID_HEIM_RSA_PKCS1_X509
) == 0) {
685 return HX509_ALG_NOT_SUPP
;
687 if (signatureAlgorithm
) {
688 ret
= set_digest_alg(signatureAlgorithm
, sig_oid
, "\x05\x00", 2);
690 hx509_clear_error_string(context
);
697 memset(&di
, 0, sizeof(di
));
699 ret
= _hx509_create_signature(context
,
707 ASN1_MALLOC_ENCODE(DigestInfo
,
713 free_DigestInfo(&di
);
715 hx509_set_error_string(context
, 0, ret
, "out of memory");
718 if (indata
.length
!= size
)
719 _hx509_abort("internal ASN.1 encoder error");
724 sig
->length
= RSA_size(signer
->private_key
.rsa
);
725 sig
->data
= malloc(sig
->length
);
726 if (sig
->data
== NULL
) {
727 der_free_octet_string(&indata
);
728 hx509_set_error_string(context
, 0, ENOMEM
, "out of memory");
732 ret
= RSA_private_encrypt(indata
.length
, indata
.data
,
734 signer
->private_key
.rsa
,
736 if (indata
.data
!= data
->data
)
737 der_free_octet_string(&indata
);
739 ret
= HX509_CMS_FAILED_CREATE_SIGATURE
;
740 hx509_set_error_string(context
, 0, ret
,
741 "RSA private encrypt failed: %d", ret
);
744 if ((size_t)ret
> sig
->length
)
745 _hx509_abort("RSA signature prelen longer the output len");
753 rsa_private_key_import(hx509_context context
,
754 const AlgorithmIdentifier
*keyai
,
757 hx509_key_format_t format
,
758 hx509_private_key private_key
)
761 case HX509_KEY_FORMAT_DER
: {
762 const unsigned char *p
= data
;
764 private_key
->private_key
.rsa
=
765 d2i_RSAPrivateKey(NULL
, &p
, len
);
766 if (private_key
->private_key
.rsa
== NULL
) {
767 hx509_set_error_string(context
, 0, HX509_PARSING_KEY_FAILED
,
768 "Failed to parse RSA key");
769 return HX509_PARSING_KEY_FAILED
;
771 private_key
->signature_alg
= ASN1_OID_ID_PKCS1_SHA1WITHRSAENCRYPTION
;
776 return HX509_CRYPTO_KEY_FORMAT_UNSUPPORTED
;
783 rsa_private_key2SPKI(hx509_context context
,
784 hx509_private_key private_key
,
785 SubjectPublicKeyInfo
*spki
)
789 memset(spki
, 0, sizeof(*spki
));
791 len
= i2d_RSAPublicKey(private_key
->private_key
.rsa
, NULL
);
793 spki
->subjectPublicKey
.data
= malloc(len
);
794 if (spki
->subjectPublicKey
.data
== NULL
) {
795 hx509_set_error_string(context
, 0, ENOMEM
, "malloc - out of memory");
798 spki
->subjectPublicKey
.length
= len
* 8;
800 ret
= set_digest_alg(&spki
->algorithm
, ASN1_OID_ID_PKCS1_RSAENCRYPTION
,
803 hx509_set_error_string(context
, 0, ret
, "malloc - out of memory");
804 free(spki
->subjectPublicKey
.data
);
805 spki
->subjectPublicKey
.data
= NULL
;
806 spki
->subjectPublicKey
.length
= 0;
811 unsigned char *pp
= spki
->subjectPublicKey
.data
;
812 i2d_RSAPublicKey(private_key
->private_key
.rsa
, &pp
);
819 rsa_generate_private_key(hx509_context context
,
820 struct hx509_generate_private_context
*ctx
,
821 hx509_private_key private_key
)
827 static const int default_rsa_e
= 65537;
828 static const int default_rsa_bits
= 2048;
830 private_key
->private_key
.rsa
= RSA_new();
831 if (private_key
->private_key
.rsa
== NULL
) {
832 hx509_set_error_string(context
, 0, HX509_PARSING_KEY_FAILED
,
833 "Failed to generate RSA key");
834 return HX509_PARSING_KEY_FAILED
;
838 BN_set_word(e
, default_rsa_e
);
840 bits
= default_rsa_bits
;
843 bits
= ctx
->num_bits
;
845 ret
= RSA_generate_key_ex(private_key
->private_key
.rsa
, bits
, e
, NULL
);
848 hx509_set_error_string(context
, 0, HX509_PARSING_KEY_FAILED
,
849 "Failed to generate RSA key");
850 return HX509_PARSING_KEY_FAILED
;
852 private_key
->signature_alg
= ASN1_OID_ID_PKCS1_SHA1WITHRSAENCRYPTION
;
858 rsa_private_key_export(hx509_context context
,
859 const hx509_private_key key
,
860 hx509_key_format_t format
,
861 heim_octet_string
*data
)
869 case HX509_KEY_FORMAT_DER
:
871 ret
= i2d_RSAPrivateKey(key
->private_key
.rsa
, NULL
);
874 hx509_set_error_string(context
, 0, ret
,
875 "Private key is not exportable");
879 data
->data
= malloc(ret
);
880 if (data
->data
== NULL
) {
882 hx509_set_error_string(context
, 0, ret
, "malloc out of memory");
888 unsigned char *p
= data
->data
;
889 i2d_RSAPrivateKey(key
->private_key
.rsa
, &p
);
893 return HX509_CRYPTO_KEY_FORMAT_UNSUPPORTED
;
900 rsa_get_internal(hx509_context context
,
901 hx509_private_key key
,
904 if (strcasecmp(type
, "rsa-modulus") == 0) {
905 return BN_dup(key
->private_key
.rsa
->n
);
906 } else if (strcasecmp(type
, "rsa-exponent") == 0) {
907 return BN_dup(key
->private_key
.rsa
->e
);
914 static hx509_private_key_ops rsa_private_key_ops
= {
916 ASN1_OID_ID_PKCS1_RSAENCRYPTION
,
918 rsa_private_key2SPKI
,
919 rsa_private_key_export
,
920 rsa_private_key_import
,
921 rsa_generate_private_key
,
928 ecdsa_private_key2SPKI(hx509_context context
,
929 hx509_private_key private_key
,
930 SubjectPublicKeyInfo
*spki
)
932 memset(spki
, 0, sizeof(*spki
));
937 ecdsa_private_key_export(hx509_context context
,
938 const hx509_private_key key
,
939 hx509_key_format_t format
,
940 heim_octet_string
*data
)
942 return HX509_CRYPTO_KEY_FORMAT_UNSUPPORTED
;
946 ecdsa_private_key_import(hx509_context context
,
947 const AlgorithmIdentifier
*keyai
,
950 hx509_key_format_t format
,
951 hx509_private_key private_key
)
953 const unsigned char *p
= data
;
954 EC_KEY
**pkey
= NULL
;
956 if (keyai
->parameters
) {
962 ret
= parse_ECParameters(context
, keyai
->parameters
, &groupnid
);
970 group
= EC_GROUP_new_by_curve_name(groupnid
);
975 EC_GROUP_set_asn1_flag(group
, OPENSSL_EC_NAMED_CURVE
);
976 if (EC_KEY_set_group(key
, group
) == 0) {
978 EC_GROUP_free(group
);
981 EC_GROUP_free(group
);
986 case HX509_KEY_FORMAT_DER
:
988 private_key
->private_key
.ecdsa
= d2i_ECPrivateKey(pkey
, &p
, len
);
989 if (private_key
->private_key
.ecdsa
== NULL
) {
990 hx509_set_error_string(context
, 0, HX509_PARSING_KEY_FAILED
,
991 "Failed to parse EC private key");
992 return HX509_PARSING_KEY_FAILED
;
994 private_key
->signature_alg
= ASN1_OID_ID_ECDSA_WITH_SHA256
;
998 return HX509_CRYPTO_KEY_FORMAT_UNSUPPORTED
;
1005 ecdsa_generate_private_key(hx509_context context
,
1006 struct hx509_generate_private_context
*ctx
,
1007 hx509_private_key private_key
)
1013 ecdsa_get_internal(hx509_context context
,
1014 hx509_private_key key
,
1021 static hx509_private_key_ops ecdsa_private_key_ops
= {
1023 ASN1_OID_ID_ECPUBLICKEY
,
1025 ecdsa_private_key2SPKI
,
1026 ecdsa_private_key_export
,
1027 ecdsa_private_key_import
,
1028 ecdsa_generate_private_key
,
1032 #endif /* HAVE_OPENSSL */
1039 dsa_verify_signature(hx509_context context
,
1040 const struct signature_alg
*sig_alg
,
1041 const Certificate
*signer
,
1042 const AlgorithmIdentifier
*alg
,
1043 const heim_octet_string
*data
,
1044 const heim_octet_string
*sig
)
1046 const SubjectPublicKeyInfo
*spi
;
1053 spi
= &signer
->tbsCertificate
.subjectPublicKeyInfo
;
1057 hx509_set_error_string(context
, 0, ENOMEM
, "out of memory");
1061 ret
= decode_DSAPublicKey(spi
->subjectPublicKey
.data
,
1062 spi
->subjectPublicKey
.length
/ 8,
1067 dsa
->pub_key
= heim_int2BN(&pk
);
1069 free_DSAPublicKey(&pk
);
1071 if (dsa
->pub_key
== NULL
) {
1073 hx509_set_error_string(context
, 0, ret
, "out of memory");
1077 if (spi
->algorithm
.parameters
== NULL
) {
1078 ret
= HX509_CRYPTO_SIG_INVALID_FORMAT
;
1079 hx509_set_error_string(context
, 0, ret
, "DSA parameters missing");
1083 ret
= decode_DSAParams(spi
->algorithm
.parameters
->data
,
1084 spi
->algorithm
.parameters
->length
,
1088 hx509_set_error_string(context
, 0, ret
, "DSA parameters failed to decode");
1092 dsa
->p
= heim_int2BN(¶m
.p
);
1093 dsa
->q
= heim_int2BN(¶m
.q
);
1094 dsa
->g
= heim_int2BN(¶m
.g
);
1096 free_DSAParams(¶m
);
1098 if (dsa
->p
== NULL
|| dsa
->q
== NULL
|| dsa
->g
== NULL
) {
1100 hx509_set_error_string(context
, 0, ret
, "out of memory");
1104 ret
= DSA_verify(-1, data
->data
, data
->length
,
1105 (unsigned char*)sig
->data
, sig
->length
,
1109 else if (ret
== 0 || ret
== -1) {
1110 ret
= HX509_CRYPTO_BAD_SIGNATURE
;
1111 hx509_set_error_string(context
, 0, ret
, "BAD DSA sigature");
1113 ret
= HX509_CRYPTO_SIG_INVALID_FORMAT
;
1114 hx509_set_error_string(context
, 0, ret
, "Invalid format of DSA sigature");
1125 dsa_parse_private_key(hx509_context context
,
1128 hx509_private_key private_key
)
1130 const unsigned char *p
= data
;
1132 private_key
->private_key
.dsa
=
1133 d2i_DSAPrivateKey(NULL
, &p
, len
);
1134 if (private_key
->private_key
.dsa
== NULL
)
1136 private_key
->signature_alg
= ASN1_OID_ID_DSA_WITH_SHA1
;
1140 hx509_set_error_string(context
, 0, HX509_PARSING_KEY_FAILED
,
1141 "No support to parse DSA keys");
1142 return HX509_PARSING_KEY_FAILED
;
1147 evp_md_create_signature(hx509_context context
,
1148 const struct signature_alg
*sig_alg
,
1149 const hx509_private_key signer
,
1150 const AlgorithmIdentifier
*alg
,
1151 const heim_octet_string
*data
,
1152 AlgorithmIdentifier
*signatureAlgorithm
,
1153 heim_octet_string
*sig
)
1155 size_t sigsize
= EVP_MD_size(sig_alg
->evp_md());
1158 memset(sig
, 0, sizeof(*sig
));
1160 if (signatureAlgorithm
) {
1162 ret
= set_digest_alg(signatureAlgorithm
, sig_alg
->sig_oid
,
1169 sig
->data
= malloc(sigsize
);
1170 if (sig
->data
== NULL
) {
1174 sig
->length
= sigsize
;
1176 ctx
= EVP_MD_CTX_create();
1177 EVP_DigestInit_ex(ctx
, sig_alg
->evp_md(), NULL
);
1178 EVP_DigestUpdate(ctx
, data
->data
, data
->length
);
1179 EVP_DigestFinal_ex(ctx
, sig
->data
, NULL
);
1180 EVP_MD_CTX_destroy(ctx
);
1187 evp_md_verify_signature(hx509_context context
,
1188 const struct signature_alg
*sig_alg
,
1189 const Certificate
*signer
,
1190 const AlgorithmIdentifier
*alg
,
1191 const heim_octet_string
*data
,
1192 const heim_octet_string
*sig
)
1194 unsigned char digest
[EVP_MAX_MD_SIZE
];
1196 size_t sigsize
= EVP_MD_size(sig_alg
->evp_md());
1198 if (sig
->length
!= sigsize
|| sigsize
> sizeof(digest
)) {
1199 hx509_set_error_string(context
, 0, HX509_CRYPTO_SIG_INVALID_FORMAT
,
1200 "SHA256 sigature have wrong length");
1201 return HX509_CRYPTO_SIG_INVALID_FORMAT
;
1204 ctx
= EVP_MD_CTX_create();
1205 EVP_DigestInit_ex(ctx
, sig_alg
->evp_md(), NULL
);
1206 EVP_DigestUpdate(ctx
, data
->data
, data
->length
);
1207 EVP_DigestFinal_ex(ctx
, digest
, NULL
);
1208 EVP_MD_CTX_destroy(ctx
);
1210 if (ct_memcmp(digest
, sig
->data
, sigsize
) != 0) {
1211 hx509_set_error_string(context
, 0, HX509_CRYPTO_BAD_SIGNATURE
,
1212 "Bad %s sigature", sig_alg
->name
);
1213 return HX509_CRYPTO_BAD_SIGNATURE
;
1221 static const struct signature_alg ecdsa_with_sha256_alg
= {
1222 "ecdsa-with-sha256",
1223 ASN1_OID_ID_ECDSA_WITH_SHA256
,
1224 &_hx509_signature_ecdsa_with_sha256_data
,
1225 ASN1_OID_ID_ECPUBLICKEY
,
1226 &_hx509_signature_sha256_data
,
1227 PROVIDE_CONF
|REQUIRE_SIGNER
|RA_RSA_USES_DIGEST_INFO
|SIG_PUBLIC_SIG
|SELF_SIGNED_OK
,
1230 ecdsa_verify_signature
,
1231 ecdsa_create_signature
,
1235 static const struct signature_alg ecdsa_with_sha1_alg
= {
1237 ASN1_OID_ID_ECDSA_WITH_SHA1
,
1238 &_hx509_signature_ecdsa_with_sha1_data
,
1239 ASN1_OID_ID_ECPUBLICKEY
,
1240 &_hx509_signature_sha1_data
,
1241 PROVIDE_CONF
|REQUIRE_SIGNER
|RA_RSA_USES_DIGEST_INFO
|SIG_PUBLIC_SIG
|SELF_SIGNED_OK
,
1244 ecdsa_verify_signature
,
1245 ecdsa_create_signature
,
1251 static const struct signature_alg heim_rsa_pkcs1_x509
= {
1253 ASN1_OID_ID_HEIM_RSA_PKCS1_X509
,
1254 &_hx509_signature_rsa_pkcs1_x509_data
,
1255 ASN1_OID_ID_PKCS1_RSAENCRYPTION
,
1257 PROVIDE_CONF
|REQUIRE_SIGNER
|SIG_PUBLIC_SIG
,
1260 rsa_verify_signature
,
1261 rsa_create_signature
,
1265 static const struct signature_alg pkcs1_rsa_sha1_alg
= {
1267 ASN1_OID_ID_PKCS1_RSAENCRYPTION
,
1268 &_hx509_signature_rsa_with_sha1_data
,
1269 ASN1_OID_ID_PKCS1_RSAENCRYPTION
,
1271 PROVIDE_CONF
|REQUIRE_SIGNER
|RA_RSA_USES_DIGEST_INFO
|SIG_PUBLIC_SIG
|SELF_SIGNED_OK
,
1274 rsa_verify_signature
,
1275 rsa_create_signature
,
1279 static const struct signature_alg rsa_with_sha512_alg
= {
1281 ASN1_OID_ID_PKCS1_SHA512WITHRSAENCRYPTION
,
1282 &_hx509_signature_rsa_with_sha512_data
,
1283 ASN1_OID_ID_PKCS1_RSAENCRYPTION
,
1284 &_hx509_signature_sha512_data
,
1285 PROVIDE_CONF
|REQUIRE_SIGNER
|RA_RSA_USES_DIGEST_INFO
|SIG_PUBLIC_SIG
|SELF_SIGNED_OK
,
1288 rsa_verify_signature
,
1289 rsa_create_signature
,
1293 static const struct signature_alg rsa_with_sha384_alg
= {
1295 ASN1_OID_ID_PKCS1_SHA384WITHRSAENCRYPTION
,
1296 &_hx509_signature_rsa_with_sha384_data
,
1297 ASN1_OID_ID_PKCS1_RSAENCRYPTION
,
1298 &_hx509_signature_sha384_data
,
1299 PROVIDE_CONF
|REQUIRE_SIGNER
|RA_RSA_USES_DIGEST_INFO
|SIG_PUBLIC_SIG
|SELF_SIGNED_OK
,
1302 rsa_verify_signature
,
1303 rsa_create_signature
,
1307 static const struct signature_alg rsa_with_sha256_alg
= {
1309 ASN1_OID_ID_PKCS1_SHA256WITHRSAENCRYPTION
,
1310 &_hx509_signature_rsa_with_sha256_data
,
1311 ASN1_OID_ID_PKCS1_RSAENCRYPTION
,
1312 &_hx509_signature_sha256_data
,
1313 PROVIDE_CONF
|REQUIRE_SIGNER
|RA_RSA_USES_DIGEST_INFO
|SIG_PUBLIC_SIG
|SELF_SIGNED_OK
,
1316 rsa_verify_signature
,
1317 rsa_create_signature
,
1321 static const struct signature_alg rsa_with_sha1_alg
= {
1323 ASN1_OID_ID_PKCS1_SHA1WITHRSAENCRYPTION
,
1324 &_hx509_signature_rsa_with_sha1_data
,
1325 ASN1_OID_ID_PKCS1_RSAENCRYPTION
,
1326 &_hx509_signature_sha1_data
,
1327 PROVIDE_CONF
|REQUIRE_SIGNER
|RA_RSA_USES_DIGEST_INFO
|SIG_PUBLIC_SIG
|SELF_SIGNED_OK
,
1330 rsa_verify_signature
,
1331 rsa_create_signature
,
1335 static const struct signature_alg rsa_with_sha1_alg_secsig
= {
1337 ASN1_OID_ID_SECSIG_SHA_1WITHRSAENCRYPTION
,
1338 &_hx509_signature_rsa_with_sha1_data
,
1339 ASN1_OID_ID_PKCS1_RSAENCRYPTION
,
1340 &_hx509_signature_sha1_data
,
1341 PROVIDE_CONF
|REQUIRE_SIGNER
|RA_RSA_USES_DIGEST_INFO
|SIG_PUBLIC_SIG
|SELF_SIGNED_OK
,
1344 rsa_verify_signature
,
1345 rsa_create_signature
,
1349 static const struct signature_alg rsa_with_md5_alg
= {
1351 ASN1_OID_ID_PKCS1_MD5WITHRSAENCRYPTION
,
1352 &_hx509_signature_rsa_with_md5_data
,
1353 ASN1_OID_ID_PKCS1_RSAENCRYPTION
,
1354 &_hx509_signature_md5_data
,
1355 PROVIDE_CONF
|REQUIRE_SIGNER
|RA_RSA_USES_DIGEST_INFO
|SIG_PUBLIC_SIG
,
1358 rsa_verify_signature
,
1359 rsa_create_signature
,
1363 static const struct signature_alg dsa_sha1_alg
= {
1365 ASN1_OID_ID_DSA_WITH_SHA1
,
1368 &_hx509_signature_sha1_data
,
1369 PROVIDE_CONF
|REQUIRE_SIGNER
|SIG_PUBLIC_SIG
,
1372 dsa_verify_signature
,
1373 /* create_signature */ NULL
,
1377 static const struct signature_alg sha512_alg
= {
1380 &_hx509_signature_sha512_data
,
1386 evp_md_verify_signature
,
1387 evp_md_create_signature
,
1391 static const struct signature_alg sha384_alg
= {
1394 &_hx509_signature_sha384_data
,
1400 evp_md_verify_signature
,
1401 evp_md_create_signature
,
1405 static const struct signature_alg sha256_alg
= {
1408 &_hx509_signature_sha256_data
,
1414 evp_md_verify_signature
,
1415 evp_md_create_signature
,
1419 static const struct signature_alg sha1_alg
= {
1421 ASN1_OID_ID_SECSIG_SHA_1
,
1422 &_hx509_signature_sha1_data
,
1428 evp_md_verify_signature
,
1429 evp_md_create_signature
,
1433 static const struct signature_alg md5_alg
= {
1435 ASN1_OID_ID_RSA_DIGEST_MD5
,
1436 &_hx509_signature_md5_data
,
1442 evp_md_verify_signature
,
1448 * Order matter in this structure, "best" first for each "key
1449 * compatible" type (type is ECDSA, RSA, DSA, none, etc)
1452 static const struct signature_alg
*sig_algs
[] = {
1454 &ecdsa_with_sha256_alg
,
1455 &ecdsa_with_sha1_alg
,
1457 &rsa_with_sha512_alg
,
1458 &rsa_with_sha384_alg
,
1459 &rsa_with_sha256_alg
,
1461 &rsa_with_sha1_alg_secsig
,
1462 &pkcs1_rsa_sha1_alg
,
1464 &heim_rsa_pkcs1_x509
,
1474 static const struct signature_alg
*
1475 find_sig_alg(const heim_oid
*oid
)
1478 for (i
= 0; sig_algs
[i
]; i
++)
1479 if (der_heim_oid_cmp(sig_algs
[i
]->sig_oid
, oid
) == 0)
1484 static const AlgorithmIdentifier
*
1485 alg_for_privatekey(const hx509_private_key pk
, int type
)
1487 const heim_oid
*keytype
;
1490 if (pk
->ops
== NULL
)
1493 keytype
= pk
->ops
->key_oid
;
1495 for (i
= 0; sig_algs
[i
]; i
++) {
1496 if (sig_algs
[i
]->key_oid
== NULL
)
1498 if (der_heim_oid_cmp(sig_algs
[i
]->key_oid
, keytype
) != 0)
1500 if (pk
->ops
->available
&&
1501 pk
->ops
->available(pk
, sig_algs
[i
]->sig_alg
) == 0)
1503 if (type
== HX509_SELECT_PUBLIC_SIG
)
1504 return sig_algs
[i
]->sig_alg
;
1505 if (type
== HX509_SELECT_DIGEST
)
1506 return sig_algs
[i
]->digest_alg
;
1517 static struct hx509_private_key_ops
*private_algs
[] = {
1518 &rsa_private_key_ops
,
1520 &ecdsa_private_key_ops
,
1525 hx509_private_key_ops
*
1526 hx509_find_private_alg(const heim_oid
*oid
)
1529 for (i
= 0; private_algs
[i
]; i
++) {
1530 if (private_algs
[i
]->key_oid
== NULL
)
1532 if (der_heim_oid_cmp(private_algs
[i
]->key_oid
, oid
) == 0)
1533 return private_algs
[i
];
1539 * Check if the algorithm `alg' have a best before date, and if it
1540 * des, make sure the its before the time `t'.
1544 _hx509_signature_best_before(hx509_context context
,
1545 const AlgorithmIdentifier
*alg
,
1548 const struct signature_alg
*md
;
1550 md
= find_sig_alg(&alg
->algorithm
);
1552 hx509_clear_error_string(context
);
1553 return HX509_SIG_ALG_NO_SUPPORTED
;
1555 if (md
->best_before
&& md
->best_before
< t
) {
1556 hx509_set_error_string(context
, 0, HX509_CRYPTO_ALGORITHM_BEST_BEFORE
,
1557 "Algorithm %s has passed it best before date",
1559 return HX509_CRYPTO_ALGORITHM_BEST_BEFORE
;
1565 _hx509_self_signed_valid(hx509_context context
,
1566 const AlgorithmIdentifier
*alg
)
1568 const struct signature_alg
*md
;
1570 md
= find_sig_alg(&alg
->algorithm
);
1572 hx509_clear_error_string(context
);
1573 return HX509_SIG_ALG_NO_SUPPORTED
;
1575 if ((md
->flags
& SELF_SIGNED_OK
) == 0) {
1576 hx509_set_error_string(context
, 0, HX509_CRYPTO_ALGORITHM_BEST_BEFORE
,
1577 "Algorithm %s not trusted for self signatures",
1579 return HX509_CRYPTO_ALGORITHM_BEST_BEFORE
;
1586 _hx509_verify_signature(hx509_context context
,
1587 const hx509_cert cert
,
1588 const AlgorithmIdentifier
*alg
,
1589 const heim_octet_string
*data
,
1590 const heim_octet_string
*sig
)
1592 const struct signature_alg
*md
;
1593 const Certificate
*signer
= NULL
;
1596 signer
= _hx509_get_cert(cert
);
1598 md
= find_sig_alg(&alg
->algorithm
);
1600 hx509_clear_error_string(context
);
1601 return HX509_SIG_ALG_NO_SUPPORTED
;
1603 if (signer
&& (md
->flags
& PROVIDE_CONF
) == 0) {
1604 hx509_clear_error_string(context
);
1605 return HX509_CRYPTO_SIG_NO_CONF
;
1607 if (signer
== NULL
&& (md
->flags
& REQUIRE_SIGNER
)) {
1608 hx509_clear_error_string(context
);
1609 return HX509_CRYPTO_SIGNATURE_WITHOUT_SIGNER
;
1611 if (md
->key_oid
&& signer
) {
1612 const SubjectPublicKeyInfo
*spi
;
1613 spi
= &signer
->tbsCertificate
.subjectPublicKeyInfo
;
1615 if (der_heim_oid_cmp(&spi
->algorithm
.algorithm
, md
->key_oid
) != 0) {
1616 hx509_clear_error_string(context
);
1617 return HX509_SIG_ALG_DONT_MATCH_KEY_ALG
;
1620 return (*md
->verify_signature
)(context
, md
, signer
, alg
, data
, sig
);
1624 _hx509_create_signature(hx509_context context
,
1625 const hx509_private_key signer
,
1626 const AlgorithmIdentifier
*alg
,
1627 const heim_octet_string
*data
,
1628 AlgorithmIdentifier
*signatureAlgorithm
,
1629 heim_octet_string
*sig
)
1631 const struct signature_alg
*md
;
1633 md
= find_sig_alg(&alg
->algorithm
);
1635 hx509_set_error_string(context
, 0, HX509_SIG_ALG_NO_SUPPORTED
,
1636 "algorithm no supported");
1637 return HX509_SIG_ALG_NO_SUPPORTED
;
1640 if (signer
&& (md
->flags
& PROVIDE_CONF
) == 0) {
1641 hx509_set_error_string(context
, 0, HX509_SIG_ALG_NO_SUPPORTED
,
1642 "algorithm provides no conf");
1643 return HX509_CRYPTO_SIG_NO_CONF
;
1646 return (*md
->create_signature
)(context
, md
, signer
, alg
, data
,
1647 signatureAlgorithm
, sig
);
1651 _hx509_create_signature_bitstring(hx509_context context
,
1652 const hx509_private_key signer
,
1653 const AlgorithmIdentifier
*alg
,
1654 const heim_octet_string
*data
,
1655 AlgorithmIdentifier
*signatureAlgorithm
,
1656 heim_bit_string
*sig
)
1658 heim_octet_string os
;
1661 ret
= _hx509_create_signature(context
, signer
, alg
,
1662 data
, signatureAlgorithm
, &os
);
1665 sig
->data
= os
.data
;
1666 sig
->length
= os
.length
* 8;
1671 _hx509_public_encrypt(hx509_context context
,
1672 const heim_octet_string
*cleartext
,
1673 const Certificate
*cert
,
1674 heim_oid
*encryption_oid
,
1675 heim_octet_string
*ciphertext
)
1677 const SubjectPublicKeyInfo
*spi
;
1683 const unsigned char *p
;
1685 ciphertext
->data
= NULL
;
1686 ciphertext
->length
= 0;
1688 spi
= &cert
->tbsCertificate
.subjectPublicKeyInfo
;
1690 p
= spi
->subjectPublicKey
.data
;
1691 size
= spi
->subjectPublicKey
.length
/ 8;
1693 rsa
= d2i_RSAPublicKey(NULL
, &p
, size
);
1695 hx509_set_error_string(context
, 0, ENOMEM
, "out of memory");
1699 tosize
= RSA_size(rsa
);
1700 to
= malloc(tosize
);
1703 hx509_set_error_string(context
, 0, ENOMEM
, "out of memory");
1707 ret
= RSA_public_encrypt(cleartext
->length
,
1708 (unsigned char *)cleartext
->data
,
1709 to
, rsa
, RSA_PKCS1_PADDING
);
1713 hx509_set_error_string(context
, 0, HX509_CRYPTO_RSA_PUBLIC_ENCRYPT
,
1714 "RSA public encrypt failed with %d", ret
);
1715 return HX509_CRYPTO_RSA_PUBLIC_ENCRYPT
;
1718 _hx509_abort("internal rsa decryption failure: ret > tosize");
1720 ciphertext
->length
= ret
;
1721 ciphertext
->data
= to
;
1723 ret
= der_copy_oid(ASN1_OID_ID_PKCS1_RSAENCRYPTION
, encryption_oid
);
1725 der_free_octet_string(ciphertext
);
1726 hx509_set_error_string(context
, 0, ENOMEM
, "out of memory");
1734 hx509_private_key_private_decrypt(hx509_context context
,
1735 const heim_octet_string
*ciphertext
,
1736 const heim_oid
*encryption_oid
,
1737 hx509_private_key p
,
1738 heim_octet_string
*cleartext
)
1742 cleartext
->data
= NULL
;
1743 cleartext
->length
= 0;
1745 if (p
->private_key
.rsa
== NULL
) {
1746 hx509_set_error_string(context
, 0, HX509_PRIVATE_KEY_MISSING
,
1747 "Private RSA key missing");
1748 return HX509_PRIVATE_KEY_MISSING
;
1751 cleartext
->length
= RSA_size(p
->private_key
.rsa
);
1752 cleartext
->data
= malloc(cleartext
->length
);
1753 if (cleartext
->data
== NULL
) {
1754 hx509_set_error_string(context
, 0, ENOMEM
, "out of memory");
1757 ret
= RSA_private_decrypt(ciphertext
->length
, ciphertext
->data
,
1762 der_free_octet_string(cleartext
);
1763 hx509_set_error_string(context
, 0, HX509_CRYPTO_RSA_PRIVATE_DECRYPT
,
1764 "Failed to decrypt using private key: %d", ret
);
1765 return HX509_CRYPTO_RSA_PRIVATE_DECRYPT
;
1767 if (cleartext
->length
< (size_t)ret
)
1768 _hx509_abort("internal rsa decryption failure: ret > tosize");
1770 cleartext
->length
= ret
;
1777 hx509_parse_private_key(hx509_context context
,
1778 const AlgorithmIdentifier
*keyai
,
1781 hx509_key_format_t format
,
1782 hx509_private_key
*private_key
)
1784 struct hx509_private_key_ops
*ops
;
1787 *private_key
= NULL
;
1789 ops
= hx509_find_private_alg(&keyai
->algorithm
);
1791 hx509_clear_error_string(context
);
1792 return HX509_SIG_ALG_NO_SUPPORTED
;
1795 ret
= hx509_private_key_init(private_key
, ops
, NULL
);
1797 hx509_set_error_string(context
, 0, ret
, "out of memory");
1801 ret
= (*ops
->import
)(context
, keyai
, data
, len
, format
, *private_key
);
1803 hx509_private_key_free(private_key
);
1813 hx509_private_key2SPKI(hx509_context context
,
1814 hx509_private_key private_key
,
1815 SubjectPublicKeyInfo
*spki
)
1817 const struct hx509_private_key_ops
*ops
= private_key
->ops
;
1818 if (ops
== NULL
|| ops
->get_spki
== NULL
) {
1819 hx509_set_error_string(context
, 0, HX509_UNIMPLEMENTED_OPERATION
,
1820 "Private key have no key2SPKI function");
1821 return HX509_UNIMPLEMENTED_OPERATION
;
1823 return (*ops
->get_spki
)(context
, private_key
, spki
);
1827 _hx509_generate_private_key_init(hx509_context context
,
1828 const heim_oid
*oid
,
1829 struct hx509_generate_private_context
**ctx
)
1833 if (der_heim_oid_cmp(oid
, ASN1_OID_ID_PKCS1_RSAENCRYPTION
) != 0) {
1834 hx509_set_error_string(context
, 0, EINVAL
,
1835 "private key not an RSA key");
1839 *ctx
= calloc(1, sizeof(**ctx
));
1841 hx509_set_error_string(context
, 0, ENOMEM
, "out of memory");
1844 (*ctx
)->key_oid
= oid
;
1850 _hx509_generate_private_key_is_ca(hx509_context context
,
1851 struct hx509_generate_private_context
*ctx
)
1858 _hx509_generate_private_key_bits(hx509_context context
,
1859 struct hx509_generate_private_context
*ctx
,
1862 ctx
->num_bits
= bits
;
1868 _hx509_generate_private_key_free(struct hx509_generate_private_context
**ctx
)
1875 _hx509_generate_private_key(hx509_context context
,
1876 struct hx509_generate_private_context
*ctx
,
1877 hx509_private_key
*private_key
)
1879 struct hx509_private_key_ops
*ops
;
1882 *private_key
= NULL
;
1884 ops
= hx509_find_private_alg(ctx
->key_oid
);
1886 hx509_clear_error_string(context
);
1887 return HX509_SIG_ALG_NO_SUPPORTED
;
1890 ret
= hx509_private_key_init(private_key
, ops
, NULL
);
1892 hx509_set_error_string(context
, 0, ret
, "out of memory");
1896 ret
= (*ops
->generate_private_key
)(context
, ctx
, *private_key
);
1898 hx509_private_key_free(private_key
);
1907 const AlgorithmIdentifier
*
1908 hx509_signature_sha512(void)
1909 { return &_hx509_signature_sha512_data
; }
1911 const AlgorithmIdentifier
*
1912 hx509_signature_sha384(void)
1913 { return &_hx509_signature_sha384_data
; }
1915 const AlgorithmIdentifier
*
1916 hx509_signature_sha256(void)
1917 { return &_hx509_signature_sha256_data
; }
1919 const AlgorithmIdentifier
*
1920 hx509_signature_sha1(void)
1921 { return &_hx509_signature_sha1_data
; }
1923 const AlgorithmIdentifier
*
1924 hx509_signature_md5(void)
1925 { return &_hx509_signature_md5_data
; }
1927 const AlgorithmIdentifier
*
1928 hx509_signature_ecPublicKey(void)
1929 { return &_hx509_signature_ecPublicKey
; }
1931 const AlgorithmIdentifier
*
1932 hx509_signature_ecdsa_with_sha256(void)
1933 { return &_hx509_signature_ecdsa_with_sha256_data
; }
1935 const AlgorithmIdentifier
*
1936 hx509_signature_ecdsa_with_sha1(void)
1937 { return &_hx509_signature_ecdsa_with_sha1_data
; }
1939 const AlgorithmIdentifier
*
1940 hx509_signature_rsa_with_sha512(void)
1941 { return &_hx509_signature_rsa_with_sha512_data
; }
1943 const AlgorithmIdentifier
*
1944 hx509_signature_rsa_with_sha384(void)
1945 { return &_hx509_signature_rsa_with_sha384_data
; }
1947 const AlgorithmIdentifier
*
1948 hx509_signature_rsa_with_sha256(void)
1949 { return &_hx509_signature_rsa_with_sha256_data
; }
1951 const AlgorithmIdentifier
*
1952 hx509_signature_rsa_with_sha1(void)
1953 { return &_hx509_signature_rsa_with_sha1_data
; }
1955 const AlgorithmIdentifier
*
1956 hx509_signature_rsa_with_md5(void)
1957 { return &_hx509_signature_rsa_with_md5_data
; }
1959 const AlgorithmIdentifier
*
1960 hx509_signature_rsa(void)
1961 { return &_hx509_signature_rsa_data
; }
1963 const AlgorithmIdentifier
*
1964 hx509_signature_rsa_pkcs1_x509(void)
1965 { return &_hx509_signature_rsa_pkcs1_x509_data
; }
1967 const AlgorithmIdentifier
*
1968 hx509_crypto_des_rsdi_ede3_cbc(void)
1969 { return &_hx509_des_rsdi_ede3_cbc_oid
; }
1971 const AlgorithmIdentifier
*
1972 hx509_crypto_aes128_cbc(void)
1973 { return &_hx509_crypto_aes128_cbc_data
; }
1975 const AlgorithmIdentifier
*
1976 hx509_crypto_aes256_cbc(void)
1977 { return &_hx509_crypto_aes256_cbc_data
; }
1983 const AlgorithmIdentifier
* _hx509_crypto_default_sig_alg
=
1984 &_hx509_signature_rsa_with_sha256_data
;
1985 const AlgorithmIdentifier
* _hx509_crypto_default_digest_alg
=
1986 &_hx509_signature_sha256_data
;
1987 const AlgorithmIdentifier
* _hx509_crypto_default_secret_alg
=
1988 &_hx509_crypto_aes128_cbc_data
;
1995 hx509_private_key_init(hx509_private_key
*key
,
1996 hx509_private_key_ops
*ops
,
1999 *key
= calloc(1, sizeof(**key
));
2004 (*key
)->private_key
.keydata
= keydata
;
2009 _hx509_private_key_ref(hx509_private_key key
)
2012 _hx509_abort("key refcount <= 0 on ref");
2014 if (key
->ref
== UINT_MAX
)
2015 _hx509_abort("key refcount == UINT_MAX on ref");
2020 _hx509_private_pem_name(hx509_private_key key
)
2022 return key
->ops
->pemtype
;
2026 hx509_private_key_free(hx509_private_key
*key
)
2028 if (key
== NULL
|| *key
== NULL
)
2031 if ((*key
)->ref
== 0)
2032 _hx509_abort("key refcount == 0 on free");
2033 if (--(*key
)->ref
> 0)
2036 if ((*key
)->ops
&& der_heim_oid_cmp((*key
)->ops
->key_oid
, ASN1_OID_ID_PKCS1_RSAENCRYPTION
) == 0) {
2037 if ((*key
)->private_key
.rsa
)
2038 RSA_free((*key
)->private_key
.rsa
);
2040 } else if ((*key
)->ops
&& der_heim_oid_cmp((*key
)->ops
->key_oid
, ASN1_OID_ID_ECPUBLICKEY
) == 0) {
2041 if ((*key
)->private_key
.ecdsa
)
2042 EC_KEY_free((*key
)->private_key
.ecdsa
);
2045 (*key
)->private_key
.rsa
= NULL
;
2052 hx509_private_key_assign_rsa(hx509_private_key key
, void *ptr
)
2054 if (key
->private_key
.rsa
)
2055 RSA_free(key
->private_key
.rsa
);
2056 key
->private_key
.rsa
= ptr
;
2057 key
->signature_alg
= ASN1_OID_ID_PKCS1_SHA1WITHRSAENCRYPTION
;
2058 key
->md
= &pkcs1_rsa_sha1_alg
;
2062 _hx509_private_key_oid(hx509_context context
,
2063 const hx509_private_key key
,
2067 ret
= der_copy_oid(key
->ops
->key_oid
, data
);
2069 hx509_set_error_string(context
, 0, ret
, "malloc out of memory");
2074 _hx509_private_key_exportable(hx509_private_key key
)
2076 if (key
->ops
->export
== NULL
)
2082 _hx509_private_key_get_internal(hx509_context context
,
2083 hx509_private_key key
,
2086 if (key
->ops
->get_internal
== NULL
)
2088 return (*key
->ops
->get_internal
)(context
, key
, type
);
2092 _hx509_private_key_export(hx509_context context
,
2093 const hx509_private_key key
,
2094 hx509_key_format_t format
,
2095 heim_octet_string
*data
)
2097 if (key
->ops
->export
== NULL
) {
2098 hx509_clear_error_string(context
);
2099 return HX509_UNIMPLEMENTED_OPERATION
;
2101 return (*key
->ops
->export
)(context
, key
, format
, data
);
2108 struct hx509cipher
{
2111 #define CIPHER_WEAK 1
2112 const heim_oid
*oid
;
2113 const AlgorithmIdentifier
*(*ai_func
)(void);
2114 const EVP_CIPHER
*(*evp_func
)(void);
2115 int (*get_params
)(hx509_context
, const hx509_crypto
,
2116 const heim_octet_string
*, heim_octet_string
*);
2117 int (*set_params
)(hx509_context
, const heim_octet_string
*,
2118 hx509_crypto
, heim_octet_string
*);
2121 struct hx509_crypto_data
{
2124 #define ALLOW_WEAK 1
2126 #define PADDING_NONE 2
2127 #define PADDING_PKCS7 4
2128 #define PADDING_FLAGS (2|4)
2129 const struct hx509cipher
*cipher
;
2130 const EVP_CIPHER
*c
;
2131 heim_octet_string key
;
2140 static unsigned private_rc2_40_oid_data
[] = { 127, 1 };
2142 static heim_oid asn1_oid_private_rc2_40
=
2143 { 2, private_rc2_40_oid_data
};
2150 CMSCBCParam_get(hx509_context context
, const hx509_crypto crypto
,
2151 const heim_octet_string
*ivec
, heim_octet_string
*param
)
2156 assert(crypto
->param
== NULL
);
2160 ASN1_MALLOC_ENCODE(CMSCBCParameter
, param
->data
, param
->length
,
2162 if (ret
== 0 && size
!= param
->length
)
2163 _hx509_abort("Internal asn1 encoder failure");
2165 hx509_clear_error_string(context
);
2170 CMSCBCParam_set(hx509_context context
, const heim_octet_string
*param
,
2171 hx509_crypto crypto
, heim_octet_string
*ivec
)
2177 ret
= decode_CMSCBCParameter(param
->data
, param
->length
, ivec
, NULL
);
2179 hx509_clear_error_string(context
);
2184 struct _RC2_params
{
2185 int maximum_effective_key
;
2189 CMSRC2CBCParam_get(hx509_context context
, const hx509_crypto crypto
,
2190 const heim_octet_string
*ivec
, heim_octet_string
*param
)
2192 CMSRC2CBCParameter rc2params
;
2193 const struct _RC2_params
*p
= crypto
->param
;
2194 int maximum_effective_key
= 128;
2198 memset(&rc2params
, 0, sizeof(rc2params
));
2201 maximum_effective_key
= p
->maximum_effective_key
;
2203 switch(maximum_effective_key
) {
2205 rc2params
.rc2ParameterVersion
= 160;
2208 rc2params
.rc2ParameterVersion
= 120;
2211 rc2params
.rc2ParameterVersion
= 58;
2214 rc2params
.iv
= *ivec
;
2216 ASN1_MALLOC_ENCODE(CMSRC2CBCParameter
, param
->data
, param
->length
,
2217 &rc2params
, &size
, ret
);
2218 if (ret
== 0 && size
!= param
->length
)
2219 _hx509_abort("Internal asn1 encoder failure");
2225 CMSRC2CBCParam_set(hx509_context context
, const heim_octet_string
*param
,
2226 hx509_crypto crypto
, heim_octet_string
*ivec
)
2228 CMSRC2CBCParameter rc2param
;
2229 struct _RC2_params
*p
;
2233 ret
= decode_CMSRC2CBCParameter(param
->data
, param
->length
,
2236 hx509_clear_error_string(context
);
2240 p
= calloc(1, sizeof(*p
));
2242 free_CMSRC2CBCParameter(&rc2param
);
2243 hx509_clear_error_string(context
);
2246 switch(rc2param
.rc2ParameterVersion
) {
2248 crypto
->c
= EVP_rc2_40_cbc();
2249 p
->maximum_effective_key
= 40;
2252 crypto
->c
= EVP_rc2_64_cbc();
2253 p
->maximum_effective_key
= 64;
2256 crypto
->c
= EVP_rc2_cbc();
2257 p
->maximum_effective_key
= 128;
2261 free_CMSRC2CBCParameter(&rc2param
);
2262 return HX509_CRYPTO_SIG_INVALID_FORMAT
;
2265 ret
= der_copy_octet_string(&rc2param
.iv
, ivec
);
2266 free_CMSRC2CBCParameter(&rc2param
);
2269 hx509_clear_error_string(context
);
2280 static const struct hx509cipher ciphers
[] = {
2284 ASN1_OID_ID_PKCS3_RC2_CBC
,
2293 ASN1_OID_ID_RSADSI_RC2_CBC
,
2302 &asn1_oid_private_rc2_40
,
2311 ASN1_OID_ID_PKCS3_DES_EDE3_CBC
,
2320 ASN1_OID_ID_RSADSI_DES_EDE3_CBC
,
2321 hx509_crypto_des_rsdi_ede3_cbc
,
2329 ASN1_OID_ID_AES_128_CBC
,
2330 hx509_crypto_aes128_cbc
,
2338 ASN1_OID_ID_AES_192_CBC
,
2347 ASN1_OID_ID_AES_256_CBC
,
2348 hx509_crypto_aes256_cbc
,
2355 static const struct hx509cipher
*
2356 find_cipher_by_oid(const heim_oid
*oid
)
2360 for (i
= 0; i
< sizeof(ciphers
)/sizeof(ciphers
[0]); i
++)
2361 if (der_heim_oid_cmp(oid
, ciphers
[i
].oid
) == 0)
2367 static const struct hx509cipher
*
2368 find_cipher_by_name(const char *name
)
2372 for (i
= 0; i
< sizeof(ciphers
)/sizeof(ciphers
[0]); i
++)
2373 if (strcasecmp(name
, ciphers
[i
].name
) == 0)
2381 hx509_crypto_enctype_by_name(const char *name
)
2383 const struct hx509cipher
*cipher
;
2385 cipher
= find_cipher_by_name(name
);
2392 hx509_crypto_init(hx509_context context
,
2393 const char *provider
,
2394 const heim_oid
*enctype
,
2395 hx509_crypto
*crypto
)
2397 const struct hx509cipher
*cipher
;
2401 cipher
= find_cipher_by_oid(enctype
);
2402 if (cipher
== NULL
) {
2403 hx509_set_error_string(context
, 0, HX509_ALG_NOT_SUPP
,
2404 "Algorithm not supported");
2405 return HX509_ALG_NOT_SUPP
;
2408 *crypto
= calloc(1, sizeof(**crypto
));
2409 if (*crypto
== NULL
) {
2410 hx509_clear_error_string(context
);
2414 (*crypto
)->flags
= PADDING_PKCS7
;
2415 (*crypto
)->cipher
= cipher
;
2416 (*crypto
)->c
= (*cipher
->evp_func
)();
2418 if (der_copy_oid(enctype
, &(*crypto
)->oid
)) {
2419 hx509_crypto_destroy(*crypto
);
2421 hx509_clear_error_string(context
);
2429 hx509_crypto_provider(hx509_crypto crypto
)
2435 hx509_crypto_destroy(hx509_crypto crypto
)
2439 if (crypto
->key
.data
)
2440 free(crypto
->key
.data
);
2442 free(crypto
->param
);
2443 der_free_oid(&crypto
->oid
);
2444 memset(crypto
, 0, sizeof(*crypto
));
2449 hx509_crypto_set_key_name(hx509_crypto crypto
, const char *name
)
2455 hx509_crypto_allow_weak(hx509_crypto crypto
)
2457 crypto
->flags
|= ALLOW_WEAK
;
2461 hx509_crypto_set_padding(hx509_crypto crypto
, int padding_type
)
2463 switch (padding_type
) {
2464 case HX509_CRYPTO_PADDING_PKCS7
:
2465 crypto
->flags
&= ~PADDING_FLAGS
;
2466 crypto
->flags
|= PADDING_PKCS7
;
2468 case HX509_CRYPTO_PADDING_NONE
:
2469 crypto
->flags
&= ~PADDING_FLAGS
;
2470 crypto
->flags
|= PADDING_NONE
;
2473 _hx509_abort("Invalid padding");
2478 hx509_crypto_set_key_data(hx509_crypto crypto
, const void *data
, size_t length
)
2480 if (EVP_CIPHER_key_length(crypto
->c
) > (int)length
)
2481 return HX509_CRYPTO_INTERNAL_ERROR
;
2483 if (crypto
->key
.data
) {
2484 free(crypto
->key
.data
);
2485 crypto
->key
.data
= NULL
;
2486 crypto
->key
.length
= 0;
2488 crypto
->key
.data
= malloc(length
);
2489 if (crypto
->key
.data
== NULL
)
2491 memcpy(crypto
->key
.data
, data
, length
);
2492 crypto
->key
.length
= length
;
2498 hx509_crypto_set_random_key(hx509_crypto crypto
, heim_octet_string
*key
)
2500 if (crypto
->key
.data
) {
2501 free(crypto
->key
.data
);
2502 crypto
->key
.length
= 0;
2505 crypto
->key
.length
= EVP_CIPHER_key_length(crypto
->c
);
2506 crypto
->key
.data
= malloc(crypto
->key
.length
);
2507 if (crypto
->key
.data
== NULL
) {
2508 crypto
->key
.length
= 0;
2511 if (RAND_bytes(crypto
->key
.data
, crypto
->key
.length
) <= 0) {
2512 free(crypto
->key
.data
);
2513 crypto
->key
.data
= NULL
;
2514 crypto
->key
.length
= 0;
2515 return HX509_CRYPTO_INTERNAL_ERROR
;
2518 return der_copy_octet_string(&crypto
->key
, key
);
2524 hx509_crypto_set_params(hx509_context context
,
2525 hx509_crypto crypto
,
2526 const heim_octet_string
*param
,
2527 heim_octet_string
*ivec
)
2529 return (*crypto
->cipher
->set_params
)(context
, param
, crypto
, ivec
);
2533 hx509_crypto_get_params(hx509_context context
,
2534 hx509_crypto crypto
,
2535 const heim_octet_string
*ivec
,
2536 heim_octet_string
*param
)
2538 return (*crypto
->cipher
->get_params
)(context
, crypto
, ivec
, param
);
2542 hx509_crypto_random_iv(hx509_crypto crypto
, heim_octet_string
*ivec
)
2544 ivec
->length
= EVP_CIPHER_iv_length(crypto
->c
);
2545 ivec
->data
= malloc(ivec
->length
);
2546 if (ivec
->data
== NULL
) {
2551 if (RAND_bytes(ivec
->data
, ivec
->length
) <= 0) {
2555 return HX509_CRYPTO_INTERNAL_ERROR
;
2561 hx509_crypto_encrypt(hx509_crypto crypto
,
2563 const size_t length
,
2564 const heim_octet_string
*ivec
,
2565 heim_octet_string
**ciphertext
)
2568 size_t padsize
, bsize
;
2573 if ((crypto
->cipher
->flags
& CIPHER_WEAK
) &&
2574 (crypto
->flags
& ALLOW_WEAK
) == 0)
2575 return HX509_CRYPTO_ALGORITHM_BEST_BEFORE
;
2577 assert(EVP_CIPHER_iv_length(crypto
->c
) == (int)ivec
->length
);
2579 EVP_CIPHER_CTX_init(&evp
);
2581 ret
= EVP_CipherInit_ex(&evp
, crypto
->c
, NULL
,
2582 crypto
->key
.data
, ivec
->data
, 1);
2584 EVP_CIPHER_CTX_cleanup(&evp
);
2585 ret
= HX509_CRYPTO_INTERNAL_ERROR
;
2589 *ciphertext
= calloc(1, sizeof(**ciphertext
));
2590 if (*ciphertext
== NULL
) {
2595 assert(crypto
->flags
& PADDING_FLAGS
);
2597 bsize
= EVP_CIPHER_block_size(crypto
->c
);
2600 if (crypto
->flags
& PADDING_NONE
) {
2601 if (bsize
!= 1 && (length
% bsize
) != 0)
2602 return HX509_CMS_PADDING_ERROR
;
2603 } else if (crypto
->flags
& PADDING_PKCS7
) {
2605 padsize
= bsize
- (length
% bsize
);
2608 (*ciphertext
)->length
= length
+ padsize
;
2609 (*ciphertext
)->data
= malloc(length
+ padsize
);
2610 if ((*ciphertext
)->data
== NULL
) {
2615 memcpy((*ciphertext
)->data
, data
, length
);
2618 unsigned char *p
= (*ciphertext
)->data
;
2620 for (i
= 0; i
< padsize
; i
++)
2624 ret
= EVP_Cipher(&evp
, (*ciphertext
)->data
,
2625 (*ciphertext
)->data
,
2628 ret
= HX509_CRYPTO_INTERNAL_ERROR
;
2636 if ((*ciphertext
)->data
) {
2637 free((*ciphertext
)->data
);
2643 EVP_CIPHER_CTX_cleanup(&evp
);
2649 hx509_crypto_decrypt(hx509_crypto crypto
,
2651 const size_t length
,
2652 heim_octet_string
*ivec
,
2653 heim_octet_string
*clear
)
2662 if ((crypto
->cipher
->flags
& CIPHER_WEAK
) &&
2663 (crypto
->flags
& ALLOW_WEAK
) == 0)
2664 return HX509_CRYPTO_ALGORITHM_BEST_BEFORE
;
2666 if (ivec
&& EVP_CIPHER_iv_length(crypto
->c
) < (int)ivec
->length
)
2667 return HX509_CRYPTO_INTERNAL_ERROR
;
2669 if (crypto
->key
.data
== NULL
)
2670 return HX509_CRYPTO_INTERNAL_ERROR
;
2675 EVP_CIPHER_CTX_init(&evp
);
2677 ret
= EVP_CipherInit_ex(&evp
, crypto
->c
, NULL
,
2678 crypto
->key
.data
, idata
, 0);
2680 EVP_CIPHER_CTX_cleanup(&evp
);
2681 return HX509_CRYPTO_INTERNAL_ERROR
;
2684 clear
->length
= length
;
2685 clear
->data
= malloc(length
);
2686 if (clear
->data
== NULL
) {
2687 EVP_CIPHER_CTX_cleanup(&evp
);
2692 if (EVP_Cipher(&evp
, clear
->data
, data
, length
) != 1) {
2693 return HX509_CRYPTO_INTERNAL_ERROR
;
2695 EVP_CIPHER_CTX_cleanup(&evp
);
2697 if ((crypto
->flags
& PADDING_PKCS7
) && EVP_CIPHER_block_size(crypto
->c
) > 1) {
2700 int j
, bsize
= EVP_CIPHER_block_size(crypto
->c
);
2702 if ((int)clear
->length
< bsize
) {
2703 ret
= HX509_CMS_PADDING_ERROR
;
2708 p
+= clear
->length
- 1;
2710 if (padsize
> bsize
) {
2711 ret
= HX509_CMS_PADDING_ERROR
;
2714 clear
->length
-= padsize
;
2715 for (j
= 0; j
< padsize
; j
++) {
2716 if (*p
-- != padsize
) {
2717 ret
= HX509_CMS_PADDING_ERROR
;
2733 typedef int (*PBE_string2key_func
)(hx509_context
,
2735 const heim_octet_string
*,
2736 hx509_crypto
*, heim_octet_string
*,
2737 heim_octet_string
*,
2738 const heim_oid
*, const EVP_MD
*);
2741 PBE_string2key(hx509_context context
,
2742 const char *password
,
2743 const heim_octet_string
*parameters
,
2744 hx509_crypto
*crypto
,
2745 heim_octet_string
*key
, heim_octet_string
*iv
,
2746 const heim_oid
*enc_oid
,
2749 PKCS12_PBEParams p12params
;
2752 int iter
, saltlen
, ret
;
2753 unsigned char *salt
;
2755 passwordlen
= password
? strlen(password
) : 0;
2757 if (parameters
== NULL
)
2758 return HX509_ALG_NOT_SUPP
;
2760 ret
= decode_PKCS12_PBEParams(parameters
->data
,
2766 if (p12params
.iterations
)
2767 iter
= *p12params
.iterations
;
2770 salt
= p12params
.salt
.data
;
2771 saltlen
= p12params
.salt
.length
;
2773 if (!PKCS12_key_gen (password
, passwordlen
, salt
, saltlen
,
2774 PKCS12_KEY_ID
, iter
, key
->length
, key
->data
, md
)) {
2775 ret
= HX509_CRYPTO_INTERNAL_ERROR
;
2779 if (!PKCS12_key_gen (password
, passwordlen
, salt
, saltlen
,
2780 PKCS12_IV_ID
, iter
, iv
->length
, iv
->data
, md
)) {
2781 ret
= HX509_CRYPTO_INTERNAL_ERROR
;
2785 ret
= hx509_crypto_init(context
, NULL
, enc_oid
, &c
);
2789 hx509_crypto_allow_weak(c
);
2791 ret
= hx509_crypto_set_key_data(c
, key
->data
, key
->length
);
2793 hx509_crypto_destroy(c
);
2799 free_PKCS12_PBEParams(&p12params
);
2803 static const heim_oid
*
2804 find_string2key(const heim_oid
*oid
,
2805 const EVP_CIPHER
**c
,
2807 PBE_string2key_func
*s2k
)
2809 if (der_heim_oid_cmp(oid
, ASN1_OID_ID_PBEWITHSHAAND40BITRC2_CBC
) == 0) {
2810 *c
= EVP_rc2_40_cbc();
2812 *s2k
= PBE_string2key
;
2813 return &asn1_oid_private_rc2_40
;
2814 } else if (der_heim_oid_cmp(oid
, ASN1_OID_ID_PBEWITHSHAAND128BITRC2_CBC
) == 0) {
2817 *s2k
= PBE_string2key
;
2818 return ASN1_OID_ID_PKCS3_RC2_CBC
;
2820 } else if (der_heim_oid_cmp(oid
, ASN1_OID_ID_PBEWITHSHAAND40BITRC4
) == 0) {
2823 *s2k
= PBE_string2key
;
2825 } else if (der_heim_oid_cmp(oid
, ASN1_OID_ID_PBEWITHSHAAND128BITRC4
) == 0) {
2828 *s2k
= PBE_string2key
;
2829 return ASN1_OID_ID_PKCS3_RC4
;
2831 } else if (der_heim_oid_cmp(oid
, ASN1_OID_ID_PBEWITHSHAAND3_KEYTRIPLEDES_CBC
) == 0) {
2832 *c
= EVP_des_ede3_cbc();
2834 *s2k
= PBE_string2key
;
2835 return ASN1_OID_ID_PKCS3_DES_EDE3_CBC
;
2846 _hx509_pbe_encrypt(hx509_context context
,
2848 const AlgorithmIdentifier
*ai
,
2849 const heim_octet_string
*content
,
2850 heim_octet_string
*econtent
)
2852 hx509_clear_error_string(context
);
2861 _hx509_pbe_decrypt(hx509_context context
,
2863 const AlgorithmIdentifier
*ai
,
2864 const heim_octet_string
*econtent
,
2865 heim_octet_string
*content
)
2867 const struct _hx509_password
*pw
;
2868 heim_octet_string key
, iv
;
2869 const heim_oid
*enc_oid
;
2870 const EVP_CIPHER
*c
;
2872 PBE_string2key_func s2k
;
2876 memset(&key
, 0, sizeof(key
));
2877 memset(&iv
, 0, sizeof(iv
));
2879 memset(content
, 0, sizeof(*content
));
2881 enc_oid
= find_string2key(&ai
->algorithm
, &c
, &md
, &s2k
);
2882 if (enc_oid
== NULL
) {
2883 hx509_set_error_string(context
, 0, HX509_ALG_NOT_SUPP
,
2884 "String to key algorithm not supported");
2885 ret
= HX509_ALG_NOT_SUPP
;
2889 key
.length
= EVP_CIPHER_key_length(c
);
2890 key
.data
= malloc(key
.length
);
2891 if (key
.data
== NULL
) {
2893 hx509_clear_error_string(context
);
2897 iv
.length
= EVP_CIPHER_iv_length(c
);
2898 iv
.data
= malloc(iv
.length
);
2899 if (iv
.data
== NULL
) {
2901 hx509_clear_error_string(context
);
2905 pw
= _hx509_lock_get_passwords(lock
);
2907 ret
= HX509_CRYPTO_INTERNAL_ERROR
;
2908 for (i
= 0; i
< pw
->len
+ 1; i
++) {
2909 hx509_crypto crypto
;
2910 const char *password
;
2913 password
= pw
->val
[i
];
2914 else if (i
< pw
->len
+ 1)
2919 ret
= (*s2k
)(context
, password
, ai
->parameters
, &crypto
,
2920 &key
, &iv
, enc_oid
, md
);
2924 ret
= hx509_crypto_decrypt(crypto
,
2929 hx509_crypto_destroy(crypto
);
2936 der_free_octet_string(&key
);
2938 der_free_octet_string(&iv
);
2948 match_keys_rsa(hx509_cert c
, hx509_private_key private_key
)
2950 const Certificate
*cert
;
2951 const SubjectPublicKeyInfo
*spi
;
2957 if (private_key
->private_key
.rsa
== NULL
)
2960 rsa
= private_key
->private_key
.rsa
;
2961 if (rsa
->d
== NULL
|| rsa
->p
== NULL
|| rsa
->q
== NULL
)
2964 cert
= _hx509_get_cert(c
);
2965 spi
= &cert
->tbsCertificate
.subjectPublicKeyInfo
;
2971 ret
= decode_RSAPublicKey(spi
->subjectPublicKey
.data
,
2972 spi
->subjectPublicKey
.length
/ 8,
2978 rsa
->n
= heim_int2BN(&pk
.modulus
);
2979 rsa
->e
= heim_int2BN(&pk
.publicExponent
);
2981 free_RSAPublicKey(&pk
);
2983 rsa
->d
= BN_dup(private_key
->private_key
.rsa
->d
);
2984 rsa
->p
= BN_dup(private_key
->private_key
.rsa
->p
);
2985 rsa
->q
= BN_dup(private_key
->private_key
.rsa
->q
);
2986 rsa
->dmp1
= BN_dup(private_key
->private_key
.rsa
->dmp1
);
2987 rsa
->dmq1
= BN_dup(private_key
->private_key
.rsa
->dmq1
);
2988 rsa
->iqmp
= BN_dup(private_key
->private_key
.rsa
->iqmp
);
2990 if (rsa
->n
== NULL
|| rsa
->e
== NULL
||
2991 rsa
->d
== NULL
|| rsa
->p
== NULL
|| rsa
->q
== NULL
||
2992 rsa
->dmp1
== NULL
|| rsa
->dmq1
== NULL
) {
2997 ret
= RSA_check_key(rsa
);
3004 match_keys_ec(hx509_cert c
, hx509_private_key private_key
)
3006 return 1; /* XXX use EC_KEY_check_key */
3011 _hx509_match_keys(hx509_cert c
, hx509_private_key key
)
3013 if (der_heim_oid_cmp(key
->ops
->key_oid
, ASN1_OID_ID_PKCS1_RSAENCRYPTION
) == 0)
3014 return match_keys_rsa(c
, key
);
3015 if (der_heim_oid_cmp(key
->ops
->key_oid
, ASN1_OID_ID_ECPUBLICKEY
) == 0)
3016 return match_keys_ec(c
, key
);
3022 static const heim_oid
*
3023 find_keytype(const hx509_private_key key
)
3025 const struct signature_alg
*md
;
3030 md
= find_sig_alg(key
->signature_alg
);
3037 hx509_crypto_select(const hx509_context context
,
3039 const hx509_private_key source
,
3040 hx509_peer_info peer
,
3041 AlgorithmIdentifier
*selected
)
3043 const AlgorithmIdentifier
*def
= NULL
;
3047 memset(selected
, 0, sizeof(*selected
));
3049 if (type
== HX509_SELECT_DIGEST
) {
3052 def
= alg_for_privatekey(source
, type
);
3054 def
= _hx509_crypto_default_digest_alg
;
3055 } else if (type
== HX509_SELECT_PUBLIC_SIG
) {
3056 bits
= SIG_PUBLIC_SIG
;
3057 /* XXX depend on `source´ and `peer´ */
3059 def
= alg_for_privatekey(source
, type
);
3061 def
= _hx509_crypto_default_sig_alg
;
3062 } else if (type
== HX509_SELECT_SECRET_ENC
) {
3064 def
= _hx509_crypto_default_secret_alg
;
3066 hx509_set_error_string(context
, 0, EINVAL
,
3067 "Unknown type %d of selection", type
);
3072 const heim_oid
*keytype
= NULL
;
3074 keytype
= find_keytype(source
);
3076 for (i
= 0; i
< peer
->len
; i
++) {
3077 for (j
= 0; sig_algs
[j
]; j
++) {
3078 if ((sig_algs
[j
]->flags
& bits
) != bits
)
3080 if (der_heim_oid_cmp(sig_algs
[j
]->sig_oid
,
3081 &peer
->val
[i
].algorithm
) != 0)
3083 if (keytype
&& sig_algs
[j
]->key_oid
&&
3084 der_heim_oid_cmp(keytype
, sig_algs
[j
]->key_oid
))
3087 /* found one, use that */
3088 ret
= copy_AlgorithmIdentifier(&peer
->val
[i
], selected
);
3090 hx509_clear_error_string(context
);
3093 if (bits
& SIG_SECRET
) {
3094 const struct hx509cipher
*cipher
;
3096 cipher
= find_cipher_by_oid(&peer
->val
[i
].algorithm
);
3099 if (cipher
->ai_func
== NULL
)
3101 ret
= copy_AlgorithmIdentifier(cipher
->ai_func(), selected
);
3103 hx509_clear_error_string(context
);
3110 ret
= copy_AlgorithmIdentifier(def
, selected
);
3112 hx509_clear_error_string(context
);
3117 hx509_crypto_available(hx509_context context
,
3120 AlgorithmIdentifier
**val
,
3123 const heim_oid
*keytype
= NULL
;
3124 unsigned int len
, i
;
3130 if (type
== HX509_SELECT_ALL
) {
3131 bits
= SIG_DIGEST
| SIG_PUBLIC_SIG
| SIG_SECRET
;
3132 } else if (type
== HX509_SELECT_DIGEST
) {
3134 } else if (type
== HX509_SELECT_PUBLIC_SIG
) {
3135 bits
= SIG_PUBLIC_SIG
;
3137 hx509_set_error_string(context
, 0, EINVAL
,
3138 "Unknown type %d of available", type
);
3143 keytype
= find_keytype(_hx509_cert_private_key(source
));
3146 for (i
= 0; sig_algs
[i
]; i
++) {
3147 if ((sig_algs
[i
]->flags
& bits
) == 0)
3149 if (sig_algs
[i
]->sig_alg
== NULL
)
3151 if (keytype
&& sig_algs
[i
]->key_oid
&&
3152 der_heim_oid_cmp(sig_algs
[i
]->key_oid
, keytype
))
3155 /* found one, add that to the list */
3156 ptr
= realloc(*val
, sizeof(**val
) * (len
+ 1));
3161 ret
= copy_AlgorithmIdentifier(sig_algs
[i
]->sig_alg
, &(*val
)[len
]);
3168 if (bits
& SIG_SECRET
) {
3170 for (i
= 0; i
< sizeof(ciphers
)/sizeof(ciphers
[0]); i
++) {
3172 if (ciphers
[i
].flags
& CIPHER_WEAK
)
3174 if (ciphers
[i
].ai_func
== NULL
)
3177 ptr
= realloc(*val
, sizeof(**val
) * (len
+ 1));
3182 ret
= copy_AlgorithmIdentifier((ciphers
[i
].ai_func
)(), &(*val
)[len
]);
3193 for (i
= 0; i
< len
; i
++)
3194 free_AlgorithmIdentifier(&(*val
)[i
]);
3197 hx509_set_error_string(context
, 0, ENOMEM
, "out of memory");
3202 hx509_crypto_free_algs(AlgorithmIdentifier
*val
,
3206 for (i
= 0; i
< len
; i
++)
3207 free_AlgorithmIdentifier(&val
[i
]);