2728d0618e15ee3a2ecc5f6d15acd7898e6de85aTinderbox User * Copyright (C) 2014-2017 Internet Systems Consortium, Inc. ("ISC")
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * Limit the size of public exponents.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntstatic isc_result_t pkcs11rsa_todns(const dst_key_t *key, isc_buffer_t *data);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntstatic isc_result_t pkcs11rsa_fetch(dst_key_t *key, const char *engine,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntpkcs11rsa_createctx_sign(dst_key_t *key, dst_context_t *dctx) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt { CKA_CLASS, &keyClass, (CK_ULONG) sizeof(keyClass) },
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt { CKA_KEY_TYPE, &keyType, (CK_ULONG) sizeof(keyType) },
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt { CKA_TOKEN, &falsevalue, (CK_ULONG) sizeof(falsevalue) },
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt { CKA_PRIVATE, &falsevalue, (CK_ULONG) sizeof(falsevalue) },
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt { CKA_SENSITIVE, &falsevalue, (CK_ULONG) sizeof(falsevalue) },
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt { CKA_SIGN, &truevalue, (CK_ULONG) sizeof(truevalue) },
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt unsigned int i;
264e17e73941059877ccf3c96f26aac15a25500bMukund Sivaraman * Reject incorrect RSA key lengths.
264e17e73941059877ccf3c96f26aac15a25500bMukund Sivaraman /* From RFC 3110 */
264e17e73941059877ccf3c96f26aac15a25500bMukund Sivaraman /* From RFC 5702 */
264e17e73941059877ccf3c96f26aac15a25500bMukund Sivaraman /* From RFC 5702 */
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt pk11_ctx = (pk11_context_t *) isc_mem_get(dctx->mctx,
acbb301e648b82fcc38b876a44403cf0fe539cc9Evan Hunt ret = pk11_get_session(pk11_ctx, OP_RSA, ISC_TRUE, ISC_FALSE,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (rsa->ontoken && (rsa->object != CK_INVALID_HANDLE)) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (!pk11_ctx->ontoken && (pk11_ctx->object != CK_INVALID_HANDLE))
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman isc_safe_memwipe(pk11_ctx, sizeof(*pk11_ctx));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_mem_put(dctx->mctx, pk11_ctx, sizeof(*pk11_ctx));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntpkcs11rsa_createctx_verify(dst_key_t *key, unsigned int maxbits,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt { CKA_CLASS, &keyClass, (CK_ULONG) sizeof(keyClass) },
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt { CKA_KEY_TYPE, &keyType, (CK_ULONG) sizeof(keyType) },
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt { CKA_TOKEN, &falsevalue, (CK_ULONG) sizeof(falsevalue) },
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt { CKA_PRIVATE, &falsevalue, (CK_ULONG) sizeof(falsevalue) },
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt { CKA_VERIFY, &truevalue, (CK_ULONG) sizeof(truevalue) },
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt unsigned int i;
264e17e73941059877ccf3c96f26aac15a25500bMukund Sivaraman * Reject incorrect RSA key lengths.
264e17e73941059877ccf3c96f26aac15a25500bMukund Sivaraman /* From RFC 3110 */
264e17e73941059877ccf3c96f26aac15a25500bMukund Sivaraman /* From RFC 5702 */
264e17e73941059877ccf3c96f26aac15a25500bMukund Sivaraman /* From RFC 5702 */
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt pk11_ctx = (pk11_context_t *) isc_mem_get(dctx->mctx,
acbb301e648b82fcc38b876a44403cf0fe539cc9Evan Hunt ret = pk11_get_session(pk11_ctx, OP_RSA, ISC_TRUE, ISC_FALSE,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (!pk11_ctx->ontoken && (pk11_ctx->object != CK_INVALID_HANDLE))
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman isc_safe_memwipe(pk11_ctx, sizeof(*pk11_ctx));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_mem_put(dctx->mctx, pk11_ctx, sizeof(*pk11_ctx));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntpkcs11rsa_createctx(dst_key_t *key, dst_context_t *dctx) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt return (pkcs11rsa_createctx_verify(key, 0U, dctx));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntpkcs11rsa_createctx2(dst_key_t *key, int maxbits, dst_context_t *dctx) {
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt pk11_context_t *pk11_ctx = dctx->ctxdata.pk11_ctx;
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman isc_safe_memwipe(pk11_ctx, sizeof(*pk11_ctx));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_mem_put(dctx->mctx, pk11_ctx, sizeof(*pk11_ctx));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntpkcs11rsa_adddata(dst_context_t *dctx, const isc_region_t *data) {
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt pk11_context_t *pk11_ctx = dctx->ctxdata.pk11_ctx;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntpkcs11rsa_sign(dst_context_t *dctx, isc_buffer_t *sig) {
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt pk11_context_t *pk11_ctx = dctx->ctxdata.pk11_ctx;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt (pk11_ctx->session, (CK_BYTE_PTR) r.base, &siglen),
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntpkcs11rsa_verify(dst_context_t *dctx, const isc_region_t *sig) {
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt pk11_context_t *pk11_ctx = dctx->ctxdata.pk11_ctx;
f5c17a057fc5974bb51d7bc8c5827a7fd6dc9aeeEvan Hunt * CKM_<hash>_RSA_PKCS mechanisms are not available so fall back
f5c17a057fc5974bb51d7bc8c5827a7fd6dc9aeeEvan Hunt * to CKM_RSA_PKCS and do the EMSA-PKCS#1-v1.5 encapsulation by hand.
f5c17a057fc5974bb51d7bc8c5827a7fd6dc9aeeEvan Huntpkcs11rsa_createctx(dst_key_t *key, dst_context_t *dctx) {
264e17e73941059877ccf3c96f26aac15a25500bMukund Sivaraman * Reject incorrect RSA key lengths.
264e17e73941059877ccf3c96f26aac15a25500bMukund Sivaraman /* From RFC 3110 */
264e17e73941059877ccf3c96f26aac15a25500bMukund Sivaraman /* From RFC 5702 */
264e17e73941059877ccf3c96f26aac15a25500bMukund Sivaraman /* From RFC 5702 */
f5c17a057fc5974bb51d7bc8c5827a7fd6dc9aeeEvan Hunt pk11_ctx = (pk11_context_t *) isc_mem_get(dctx->mctx,
f5c17a057fc5974bb51d7bc8c5827a7fd6dc9aeeEvan Hunt ret = pk11_get_session(pk11_ctx, OP_RSA, ISC_TRUE, ISC_FALSE,
f5c17a057fc5974bb51d7bc8c5827a7fd6dc9aeeEvan Hunt PK11_RET(pkcs_C_DigestInit, (pk11_ctx->session, &mech), ISC_R_FAILURE);
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman isc_safe_memwipe(pk11_ctx, sizeof(*pk11_ctx));
f5c17a057fc5974bb51d7bc8c5827a7fd6dc9aeeEvan Hunt isc_mem_put(dctx->mctx, pk11_ctx, sizeof(*pk11_ctx));
f5c17a057fc5974bb51d7bc8c5827a7fd6dc9aeeEvan Hunt pk11_context_t *pk11_ctx = dctx->ctxdata.pk11_ctx;
f5c17a057fc5974bb51d7bc8c5827a7fd6dc9aeeEvan Hunt (void) pkcs_C_DigestFinal(pk11_ctx->session, garbage, &len);
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman isc_safe_memwipe(garbage, sizeof(garbage));
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman isc_safe_memwipe(pk11_ctx, sizeof(*pk11_ctx));
f5c17a057fc5974bb51d7bc8c5827a7fd6dc9aeeEvan Hunt isc_mem_put(dctx->mctx, pk11_ctx, sizeof(*pk11_ctx));
f5c17a057fc5974bb51d7bc8c5827a7fd6dc9aeeEvan Huntpkcs11rsa_adddata(dst_context_t *dctx, const isc_region_t *data) {
f5c17a057fc5974bb51d7bc8c5827a7fd6dc9aeeEvan Hunt pk11_context_t *pk11_ctx = dctx->ctxdata.pk11_ctx;
f5c17a057fc5974bb51d7bc8c5827a7fd6dc9aeeEvan Huntpkcs11rsa_sign(dst_context_t *dctx, isc_buffer_t *sig) {
f5c17a057fc5974bb51d7bc8c5827a7fd6dc9aeeEvan Hunt { CKA_CLASS, &keyClass, (CK_ULONG) sizeof(keyClass) },
f5c17a057fc5974bb51d7bc8c5827a7fd6dc9aeeEvan Hunt { CKA_KEY_TYPE, &keyType, (CK_ULONG) sizeof(keyType) },
f5c17a057fc5974bb51d7bc8c5827a7fd6dc9aeeEvan Hunt { CKA_TOKEN, &falsevalue, (CK_ULONG) sizeof(falsevalue) },
f5c17a057fc5974bb51d7bc8c5827a7fd6dc9aeeEvan Hunt { CKA_PRIVATE, &falsevalue, (CK_ULONG) sizeof(falsevalue) },
f5c17a057fc5974bb51d7bc8c5827a7fd6dc9aeeEvan Hunt { CKA_SENSITIVE, &falsevalue, (CK_ULONG) sizeof(falsevalue) },
f5c17a057fc5974bb51d7bc8c5827a7fd6dc9aeeEvan Hunt { CKA_SIGN, &truevalue, (CK_ULONG) sizeof(truevalue) },
f5c17a057fc5974bb51d7bc8c5827a7fd6dc9aeeEvan Hunt CK_BYTE digest[MAX_DER_SIZE + ISC_SHA512_DIGESTLENGTH];
f5c17a057fc5974bb51d7bc8c5827a7fd6dc9aeeEvan Hunt pk11_context_t *pk11_ctx = dctx->ctxdata.pk11_ctx;
f5c17a057fc5974bb51d7bc8c5827a7fd6dc9aeeEvan Hunt unsigned int i;
264e17e73941059877ccf3c96f26aac15a25500bMukund Sivaraman * Reject incorrect RSA key lengths.
264e17e73941059877ccf3c96f26aac15a25500bMukund Sivaraman /* From RFC 3110 */
264e17e73941059877ccf3c96f26aac15a25500bMukund Sivaraman /* From RFC 5702 */
264e17e73941059877ccf3c96f26aac15a25500bMukund Sivaraman /* From RFC 5702 */
f5c17a057fc5974bb51d7bc8c5827a7fd6dc9aeeEvan Hunt if (r.length < (unsigned int) dgstlen + MIN_PKCS1_PADLEN)
f5c17a057fc5974bb51d7bc8c5827a7fd6dc9aeeEvan Hunt if (rsa->ontoken && (rsa->object != CK_INVALID_HANDLE)) {
f5c17a057fc5974bb51d7bc8c5827a7fd6dc9aeeEvan Hunt (void) pkcs_C_DestroyObject(pk11_ctx->session, hKey);
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman isc_safe_memwipe(pk11_ctx, sizeof(*pk11_ctx));
f5c17a057fc5974bb51d7bc8c5827a7fd6dc9aeeEvan Hunt isc_mem_put(dctx->mctx, pk11_ctx, sizeof(*pk11_ctx));
f5c17a057fc5974bb51d7bc8c5827a7fd6dc9aeeEvan Huntpkcs11rsa_verify(dst_context_t *dctx, const isc_region_t *sig) {
f5c17a057fc5974bb51d7bc8c5827a7fd6dc9aeeEvan Hunt { CKA_CLASS, &keyClass, (CK_ULONG) sizeof(keyClass) },
f5c17a057fc5974bb51d7bc8c5827a7fd6dc9aeeEvan Hunt { CKA_KEY_TYPE, &keyType, (CK_ULONG) sizeof(keyType) },
f5c17a057fc5974bb51d7bc8c5827a7fd6dc9aeeEvan Hunt { CKA_TOKEN, &falsevalue, (CK_ULONG) sizeof(falsevalue) },
f5c17a057fc5974bb51d7bc8c5827a7fd6dc9aeeEvan Hunt { CKA_PRIVATE, &falsevalue, (CK_ULONG) sizeof(falsevalue) },
f5c17a057fc5974bb51d7bc8c5827a7fd6dc9aeeEvan Hunt { CKA_VERIFY, &truevalue, (CK_ULONG) sizeof(truevalue) },
f5c17a057fc5974bb51d7bc8c5827a7fd6dc9aeeEvan Hunt CK_BYTE digest[MAX_DER_SIZE + ISC_SHA512_DIGESTLENGTH];
f5c17a057fc5974bb51d7bc8c5827a7fd6dc9aeeEvan Hunt pk11_context_t *pk11_ctx = dctx->ctxdata.pk11_ctx;
f5c17a057fc5974bb51d7bc8c5827a7fd6dc9aeeEvan Hunt unsigned int i;
f5c17a057fc5974bb51d7bc8c5827a7fd6dc9aeeEvan Hunt (void) pkcs_C_DestroyObject(pk11_ctx->session, hKey);
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman isc_safe_memwipe(pk11_ctx, sizeof(*pk11_ctx));
f5c17a057fc5974bb51d7bc8c5827a7fd6dc9aeeEvan Hunt isc_mem_put(dctx->mctx, pk11_ctx, sizeof(*pk11_ctx));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntpkcs11rsa_compare(const dst_key_t *key1, const dst_key_t *key2) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt attr1 = pk11_attribute_bytype(rsa1, CKA_PUBLIC_EXPONENT);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt attr2 = pk11_attribute_bytype(rsa2, CKA_PUBLIC_EXPONENT);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt attr1 = pk11_attribute_bytype(rsa1, CKA_PRIVATE_EXPONENT);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt attr2 = pk11_attribute_bytype(rsa2, CKA_PRIVATE_EXPONENT);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntpkcs11rsa_generate(dst_key_t *key, int exp, void (*callback)(int)) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt CK_MECHANISM mech = { CKM_RSA_PKCS_KEY_PAIR_GEN, NULL, 0 };
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt { CKA_CLASS, &pubClass, (CK_ULONG) sizeof(pubClass) },
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt { CKA_KEY_TYPE, &keyType, (CK_ULONG) sizeof(keyType) },
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt { CKA_TOKEN, &falsevalue, (CK_ULONG) sizeof(falsevalue) },
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt { CKA_PRIVATE, &falsevalue, (CK_ULONG) sizeof(falsevalue) },
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt { CKA_VERIFY, &truevalue, (CK_ULONG) sizeof(truevalue) },
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt { CKA_MODULUS_BITS, &bits, (CK_ULONG) sizeof(bits) },
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt { CKA_PUBLIC_EXPONENT, &pubexp, (CK_ULONG) sizeof(pubexp) }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt { CKA_CLASS, &privClass, (CK_ULONG) sizeof(privClass) },
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt { CKA_KEY_TYPE, &keyType, (CK_ULONG) sizeof(keyType) },
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt { CKA_TOKEN, &falsevalue, (CK_ULONG) sizeof(falsevalue) },
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt { CKA_PRIVATE, &falsevalue, (CK_ULONG) sizeof(falsevalue) },
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt { CKA_SENSITIVE, &falsevalue, (CK_ULONG) sizeof(falsevalue) },
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt { CKA_EXTRACTABLE, &truevalue, (CK_ULONG) sizeof(truevalue) },
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt { CKA_SIGN, &truevalue, (CK_ULONG) sizeof(truevalue) },
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt unsigned int i;
264e17e73941059877ccf3c96f26aac15a25500bMukund Sivaraman * Reject incorrect RSA key lengths.
264e17e73941059877ccf3c96f26aac15a25500bMukund Sivaraman /* From RFC 3110 */
264e17e73941059877ccf3c96f26aac15a25500bMukund Sivaraman /* From RFC 5702 */
264e17e73941059877ccf3c96f26aac15a25500bMukund Sivaraman /* From RFC 5702 */
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt pk11_ctx = (pk11_context_t *) isc_mem_get(key->mctx,
acbb301e648b82fcc38b876a44403cf0fe539cc9Evan Hunt ret = pk11_get_session(pk11_ctx, OP_RSA, ISC_TRUE, ISC_FALSE,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (exp == 0) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /* RSA_F4 0x10001 */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /* F5 0x100000001 */
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt rsa = (pk11_object_t *) isc_mem_get(key->mctx, sizeof(*rsa));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt rsa->repr = (CK_ATTRIBUTE *) isc_mem_get(key->mctx, sizeof(*attr) * 8);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt for (i = 0; i <= 1; i++) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt attr[i].pValue = isc_mem_get(key->mctx, attr[i].ulValueLen);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt for (i = 0; i <= 5; i++) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt attr[i].pValue = isc_mem_get(key->mctx, attr[i].ulValueLen);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt (void) pkcs_C_DestroyObject(pk11_ctx->session, priv);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt (void) pkcs_C_DestroyObject(pk11_ctx->session, pub);
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman isc_safe_memwipe(pk11_ctx, sizeof(*pk11_ctx));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_mem_put(key->mctx, pk11_ctx, sizeof(*pk11_ctx));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt (void) pkcs_C_DestroyObject(pk11_ctx->session, priv);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt (void) pkcs_C_DestroyObject(pk11_ctx->session, pub);
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman isc_safe_memwipe(pk11_ctx, sizeof(*pk11_ctx));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_mem_put(key->mctx, pk11_ctx, sizeof(*pk11_ctx));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt attr = pk11_attribute_bytype(rsa, CKA_PRIVATE_EXPONENT);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt INSIST((rsa->object == CK_INVALID_HANDLE) || rsa->ontoken);
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman isc_safe_memwipe(rsa->repr, rsa->attrcnt * sizeof(*attr));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntpkcs11rsa_todns(const dst_key_t *key, isc_buffer_t *data) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (e_bytes < 256) { /*%< key exponent is <= 2040 bits */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_buffer_putuint16(data, (isc_uint16_t) e_bytes);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntpkcs11rsa_fromdns(dst_key_t *key, isc_buffer_t *data) {
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt rsa = (pk11_object_t *) isc_mem_get(key->mctx, sizeof(*rsa));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt rsa->repr = (CK_ATTRIBUTE *) isc_mem_get(key->mctx, sizeof(*attr) * 2);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt attr[0].pValue = isc_mem_get(key->mctx, mod_bytes);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntpkcs11rsa_tofile(const dst_key_t *key, const char *directory) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt CK_ATTRIBUTE *dmp1 = NULL, *dmq1 = NULL, *iqmp = NULL;
12bf5d4796505b4c20680531da96a31e6c2c1144Evan Hunt return (dst__privstruct_writefile(key, &priv, directory));
12bf5d4796505b4c20680531da96a31e6c2c1144Evan Hunt for (i = 0; i < 10; i++) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt bufs[i] = isc_mem_get(key->mctx, modulus->ulValueLen);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt priv.elements[i].length = (unsigned short) modulus->ulValueLen;
1b255a0c4eaccf0feff70328a8c108a22abfbf3cEvan Hunt memmove(bufs[i], modulus->pValue, modulus->ulValueLen);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt priv.elements[i].length = (unsigned short) exponent->ulValueLen;
1b255a0c4eaccf0feff70328a8c108a22abfbf3cEvan Hunt memmove(bufs[i], exponent->pValue, exponent->ulValueLen);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt priv.elements[i].length = (unsigned short) d->ulValueLen;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt priv.elements[i].length = (unsigned short) p->ulValueLen;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt priv.elements[i].length = (unsigned short) q->ulValueLen;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt priv.elements[i].length = (unsigned short) dmp1->ulValueLen;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt priv.elements[i].length = (unsigned short) dmq1->ulValueLen;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt priv.elements[i].length = (unsigned short) iqmp->ulValueLen;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt priv.elements[i].data = (unsigned char *)key->engine;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt priv.elements[i].data = (unsigned char *)key->label;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = dst__privstruct_writefile(key, &priv, directory);
12bf5d4796505b4c20680531da96a31e6c2c1144Evan Hunt for (i = 0; i < 10; i++) {
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman isc_safe_memwipe(bufs[i], modulus->ulValueLen);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_mem_put(key->mctx, bufs[i], modulus->ulValueLen);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntpkcs11rsa_fetch(dst_key_t *key, const char *engine, const char *label,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt { CKA_CLASS, &keyClass, (CK_ULONG) sizeof(keyClass) },
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt { CKA_KEY_TYPE, &keyType, (CK_ULONG) sizeof(keyType) },
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt { CKA_TOKEN, &truevalue, (CK_ULONG) sizeof(truevalue) },
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt rsa->repr = (CK_ATTRIBUTE *) isc_mem_get(key->mctx, sizeof(*attr) * 2);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt pubattr = pk11_attribute_bytype(pubrsa, CKA_MODULUS);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt attr->pValue = isc_mem_get(key->mctx, pubattr->ulValueLen);
1b255a0c4eaccf0feff70328a8c108a22abfbf3cEvan Hunt memmove(attr->pValue, pubattr->pValue, pubattr->ulValueLen);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt pubattr = pk11_attribute_bytype(pubrsa, CKA_PUBLIC_EXPONENT);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt attr->pValue = isc_mem_get(key->mctx, pubattr->ulValueLen);
1b255a0c4eaccf0feff70328a8c108a22abfbf3cEvan Hunt memmove(attr->pValue, pubattr->pValue, pubattr->ulValueLen);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ret = pk11_parse_uri(rsa, label, key->mctx, OP_RSA);
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt pk11_ctx = (pk11_context_t *) isc_mem_get(key->mctx,
acbb301e648b82fcc38b876a44403cf0fe539cc9Evan Hunt ret = pk11_get_session(pk11_ctx, OP_RSA, ISC_TRUE, ISC_FALSE,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt (pk11_ctx->session, searchTemplate, (CK_ULONG) 4),
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt (pk11_ctx->session, &rsa->object, (CK_ULONG) 1, &cnt),
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt (void) pkcs_C_FindObjectsFinal(pk11_ctx->session);
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman isc_safe_memwipe(pk11_ctx, sizeof(*pk11_ctx));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_mem_put(key->mctx, pk11_ctx, sizeof(*pk11_ctx));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt key->key_size = pk11_numbits(attr->pValue, attr->ulValueLen);
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman isc_safe_memwipe(pk11_ctx, sizeof(*pk11_ctx));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_mem_put(key->mctx, pk11_ctx, sizeof(*pk11_ctx));
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Huntrsa_check(pk11_object_t *rsa, pk11_object_t *pubrsa) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt privattr = pk11_attribute_bytype(rsa, CKA_PUBLIC_EXPONENT);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt pubattr = pk11_attribute_bytype(pubrsa, CKA_PUBLIC_EXPONENT);
420a43c8d8028992a4e9c170022f97bfac689025Evan Hunt if (!isc_safe_memequal(priv_exp, pub_exp, pub_explen))
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt privattr = pk11_attribute_bytype(rsa, CKA_MODULUS);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt pubattr = pk11_attribute_bytype(pubrsa, CKA_MODULUS);
420a43c8d8028992a4e9c170022f97bfac689025Evan Hunt if (!isc_safe_memequal(priv_mod, pub_mod, pub_modlen))
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntpkcs11rsa_parse(dst_key_t *key, isc_lex_t *lexer, dst_key_t *pub) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /* read private key file */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ret = dst__privstruct_parse(key, DST_ALG_RSA, lexer, mctx, &priv);
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt rsa = (pk11_object_t *) isc_mem_get(key->mctx, sizeof(*rsa));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /* Is this key is stored in a HSM? See if we can fetch it. */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt rsa->repr = (CK_ATTRIBUTE *) isc_mem_get(key->mctx, sizeof(*attr) * 8);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt bn = isc_mem_get(key->mctx, priv.elements[i].length);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (rsa_check(rsa, pub->keydata.pkey) != ISC_R_SUCCESS)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt key->key_size = pk11_numbits(attr->pValue, attr->ulValueLen);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt attr = pk11_attribute_bytype(rsa, CKA_PUBLIC_EXPONENT);
3249da26fc28297265d444a1f3647f1e6700a2a0Evan Hunt if (pk11_numbits(attr->pValue, attr->ulValueLen) > RSA_MAX_PUBEXP_BITS)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntpkcs11rsa_fromlabel(dst_key_t *key, const char *engine, const char *label,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt const char *pin)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt { CKA_CLASS, &keyClass, (CK_ULONG) sizeof(keyClass) },
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt { CKA_KEY_TYPE, &keyType, (CK_ULONG) sizeof(keyType) },
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt { CKA_TOKEN, &truevalue, (CK_ULONG) sizeof(truevalue) },
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt unsigned int i;
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt rsa = (pk11_object_t *) isc_mem_get(key->mctx, sizeof(*rsa));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt rsa->repr = (CK_ATTRIBUTE *) isc_mem_get(key->mctx, sizeof(*attr) * 2);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ret = pk11_parse_uri(rsa, label, key->mctx, OP_RSA);
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt pk11_ctx = (pk11_context_t *) isc_mem_get(key->mctx,
acbb301e648b82fcc38b876a44403cf0fe539cc9Evan Hunt ret = pk11_get_session(pk11_ctx, OP_RSA, ISC_TRUE, ISC_FALSE,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt (pk11_ctx->session, searchTemplate, (CK_ULONG) 4),
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt (void) pkcs_C_FindObjectsFinal(pk11_ctx->session);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt for (i = 0; i <= 1; i++) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt attr[i].pValue = isc_mem_get(key->mctx, attr[i].ulValueLen);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt (pk11_ctx->session, searchTemplate, (CK_ULONG) 4),
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt (pk11_ctx->session, &rsa->object, (CK_ULONG) 1, &cnt),
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt (void) pkcs_C_FindObjectsFinal(pk11_ctx->session);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt attr = pk11_attribute_bytype(rsa, CKA_PUBLIC_EXPONENT);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (pk11_numbits(attr->pValue, attr->ulValueLen) > RSA_MAX_PUBEXP_BITS)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt key->key_size = pk11_numbits(attr->pValue, attr->ulValueLen);
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman isc_safe_memwipe(pk11_ctx, sizeof(*pk11_ctx));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_mem_put(key->mctx, pk11_ctx, sizeof(*pk11_ctx));
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman isc_safe_memwipe(pk11_ctx, sizeof(*pk11_ctx));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_mem_put(key->mctx, pk11_ctx, sizeof(*pk11_ctx));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#else /* PKCS11CRYPTO */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#endif /* PKCS11CRYPTO */