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#if defined(PKCS11CRYPTO) && defined(HAVE_PKCS11_GOST)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * RU CryptoPro GOST keys:
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * mechanisms:
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * CKM_GOSTR3411
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * CKM_GOSTR3410_WITH_GOSTR3411
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * CKM_GOSTR3410_KEY_PAIR_GEN
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * domain parameters:
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * CKA_GOSTR3410_PARAMS (fixed BER OID 1.2.643.2.2.35.1)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * CKA_GOSTR3411_PARAMS (fixed BER OID 1.2.643.2.2.30.1)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * CKA_GOST28147_PARAMS (optional, don't use)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * public keys:
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * object class CKO_PUBLIC_KEY
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * key type CKK_GOSTR3410
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * attribute CKA_VALUE (point Q)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * attribute CKA_GOSTR3410_PARAMS
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * attribute CKA_GOSTR3411_PARAMS
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * attribute CKA_GOST28147_PARAMS
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * private keys:
e20788e1216ed720aefa84f3295f7899d9f28c22Mark Andrews * object class CKO_PRIVATE_KEY
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * key type CKK_GOSTR3410
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * attribute CKA_VALUE (big int d)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * attribute CKA_GOSTR3410_PARAMS
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * attribute CKA_GOSTR3411_PARAMS
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * attribute CKA_GOST28147_PARAMS
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * point format: <x> <y> (little endian)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt/* HASH methods */
acbb301e648b82fcc38b876a44403cf0fe539cc9Evan Hunt ret = pk11_get_session(ctx, OP_GOST, ISC_TRUE, ISC_FALSE,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt PK11_CALL(pkcs_C_DigestInit, (ctx->session, &mech), ISC_R_FAILURE);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt (void) pkcs_C_DigestFinal(ctx->session, garbage, &len);
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman isc_safe_memwipe(garbage, sizeof(garbage));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntisc_gost_update(isc_gost_t *ctx, const unsigned char *buf, unsigned int len) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntisc_gost_final(isc_gost_t *ctx, unsigned char *digest) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt/* DST methods */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntstatic isc_result_t pkcs11gost_todns(const dst_key_t *key, isc_buffer_t *data);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntpkcs11gost_createctx_sign(dst_key_t *key, dst_context_t *dctx) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt CK_MECHANISM mech = { CKM_GOSTR3410_WITH_GOSTR3411, NULL, 0 };
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;
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt pk11_ctx = (pk11_context_t *) isc_mem_get(dctx->mctx,
acbb301e648b82fcc38b876a44403cf0fe539cc9Evan Hunt ret = pk11_get_session(pk11_ctx, OP_GOST, ISC_TRUE, ISC_FALSE,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (gost->ontoken && (gost->object != CK_INVALID_HANDLE)) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (!pk11_ctx->ontoken && (pk11_ctx->object != CK_INVALID_HANDLE))
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt (void) pkcs_C_DestroyObject(pk11_ctx->session, pk11_ctx->object);
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman isc_safe_memwipe(pk11_ctx, sizeof(*pk11_ctx));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_mem_put(dctx->mctx, pk11_ctx, sizeof(*pk11_ctx));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntpkcs11gost_createctx_verify(dst_key_t *key, dst_context_t *dctx) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt CK_MECHANISM mech = { CKM_GOSTR3410_WITH_GOSTR3411, NULL, 0 };
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;
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt pk11_ctx = (pk11_context_t *) isc_mem_get(dctx->mctx,
acbb301e648b82fcc38b876a44403cf0fe539cc9Evan Hunt ret = pk11_get_session(pk11_ctx, OP_GOST, ISC_TRUE, ISC_FALSE,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (gost->ontoken && (gost->object != CK_INVALID_HANDLE)) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (!pk11_ctx->ontoken && (pk11_ctx->object != CK_INVALID_HANDLE))
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt (void) pkcs_C_DestroyObject(pk11_ctx->session, pk11_ctx->object);
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman isc_safe_memwipe(pk11_ctx, sizeof(*pk11_ctx));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_mem_put(dctx->mctx, pk11_ctx, sizeof(*pk11_ctx));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntpkcs11gost_createctx(dst_key_t *key, 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 Huntpkcs11gost_adddata(dst_context_t *dctx, const isc_region_t *data) {
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt pk11_context_t *pk11_ctx = dctx->ctxdata.pk11_ctx;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntpkcs11gost_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 Huntpkcs11gost_verify(dst_context_t *dctx, const isc_region_t *sig) {
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt pk11_context_t *pk11_ctx = dctx->ctxdata.pk11_ctx;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntpkcs11gost_compare(const dst_key_t *key1, const dst_key_t *key2) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntpkcs11gost_generate(dst_key_t *key, int unused, void (*callback)(int)) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt CK_MECHANISM mech = { CKM_GOSTR3410_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_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) },
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt pk11_ctx = (pk11_context_t *) isc_mem_get(key->mctx,
acbb301e648b82fcc38b876a44403cf0fe539cc9Evan Hunt ret = pk11_get_session(pk11_ctx, OP_GOST, ISC_TRUE, ISC_FALSE,
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt gost = (pk11_object_t *) isc_mem_get(key->mctx, sizeof(*gost));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt gost->repr = (CK_ATTRIBUTE *) isc_mem_get(key->mctx,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt attr->pValue = isc_mem_get(key->mctx, attr->ulValueLen);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt attr->pValue = isc_mem_get(key->mctx, attr->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 INSIST((gost->object == CK_INVALID_HANDLE) || gost->ontoken);
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman isc_safe_memwipe(gost->repr, gost->attrcnt * sizeof(*attr));
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman gost->repr, gost->attrcnt * sizeof(*attr));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntpkcs11gost_todns(const dst_key_t *key, isc_buffer_t *data) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if ((attr == NULL) || (attr->ulValueLen != ISC_GOST_PUBKEYLENGTH))
1b255a0c4eaccf0feff70328a8c108a22abfbf3cEvan Hunt memmove(r.base, (CK_BYTE_PTR) attr->pValue, ISC_GOST_PUBKEYLENGTH);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntpkcs11gost_fromdns(dst_key_t *key, isc_buffer_t *data) {
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt gost = (pk11_object_t *) isc_mem_get(key->mctx, sizeof(*gost));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt gost->repr = (CK_ATTRIBUTE *) isc_mem_get(key->mctx, sizeof(*attr));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt attr->pValue = isc_mem_get(key->mctx, ISC_GOST_PUBKEYLENGTH);
1b255a0c4eaccf0feff70328a8c108a22abfbf3cEvan Hunt memmove((CK_BYTE_PTR) attr->pValue, r.base, ISC_GOST_PUBKEYLENGTH);
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman isc_safe_memwipe(gost->repr, gost->attrcnt * sizeof(*attr));
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman gost->repr, gost->attrcnt * sizeof(*attr));
12bf5d4796505b4c20680531da96a31e6c2c1144Evan Huntpkcs11gost_tofile(const dst_key_t *key, const char *directory) {
12bf5d4796505b4c20680531da96a31e6c2c1144Evan Hunt unsigned int i = 0;
12bf5d4796505b4c20680531da96a31e6c2c1144Evan Hunt return (dst__privstruct_writefile(key, &priv, directory));
12bf5d4796505b4c20680531da96a31e6c2c1144Evan Hunt buf = isc_mem_get(key->mctx, attr->ulValueLen + 39);
3249da26fc28297265d444a1f3647f1e6700a2a0Evan Hunt memmove(buf + 39, attr->pValue, attr->ulValueLen);
12bf5d4796505b4c20680531da96a31e6c2c1144Evan Hunt if (adj != 0) {
12bf5d4796505b4c20680531da96a31e6c2c1144Evan Hunt ret = dst__privstruct_writefile(key, &priv, directory);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntpkcs11gost_tofile(const dst_key_t *key, const char *directory) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt unsigned int i = 0;
12bf5d4796505b4c20680531da96a31e6c2c1144Evan Hunt return (dst__privstruct_writefile(key, &priv, directory));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt priv.elements[i].length = (unsigned short) attr->ulValueLen;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ret = dst__privstruct_writefile(key, &priv, directory);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntpkcs11gost_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_ECDSA256, lexer, mctx, &priv);
12bf5d4796505b4c20680531da96a31e6c2c1144Evan Hunt int adj = (int) priv.elements[0].length - (39 + 32);
12bf5d4796505b4c20680531da96a31e6c2c1144Evan Hunt if (adj != 0) {
420a43c8d8028992a4e9c170022f97bfac689025Evan Hunt if (!isc_safe_memequal(priv.elements[0].data, buf, 39))
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt gost = (pk11_object_t *) isc_mem_get(key->mctx, sizeof(*gost));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt gost->repr = (CK_ATTRIBUTE *) isc_mem_get(key->mctx,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt pattr = pk11_attribute_bytype(pub->keydata.pkey, CKA_VALUE);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt attr->pValue = isc_mem_get(key->mctx, pattr->ulValueLen);
1b255a0c4eaccf0feff70328a8c108a22abfbf3cEvan Hunt memmove(attr->pValue, pattr->pValue, pattr->ulValueLen);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt attr->pValue = isc_mem_get(key->mctx, priv.elements[0].length);
1b255a0c4eaccf0feff70328a8c108a22abfbf3cEvan Hunt memmove(attr->pValue, priv.elements[0].data, priv.elements[0].length);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#else /* PKCS11CRYPTO && HAVE_PKCS11_GOST */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#endif /* PKCS11CRYPTO && HAVE_PKCS11_GOST */