// Create the OpenSSL representation of the key void OSSLDSAPublicKey::createOSSLKey() { if (dsa != NULL) return; dsa = DSA_new(); if (dsa == NULL) { ERROR_MSG("Could not create DSA object"); return; } // Use the OpenSSL implementation and not any engine #if OPENSSL_VERSION_NUMBER < 0x10100000L #ifdef WITH_FIPS if (FIPS_mode()) DSA_set_method(dsa, FIPS_dsa_openssl()); else DSA_set_method(dsa, DSA_OpenSSL()); #else DSA_set_method(dsa, DSA_OpenSSL()); #endif #else DSA_set_method(dsa, DSA_OpenSSL()); #endif BIGNUM* bn_p = OSSL::byteString2bn(p); BIGNUM* bn_q = OSSL::byteString2bn(q); BIGNUM* bn_g = OSSL::byteString2bn(g); BIGNUM* bn_pub_key = OSSL::byteString2bn(y); DSA_set0_pqg(dsa, bn_p, bn_q, bn_g); DSA_set0_key(dsa, bn_pub_key, NULL); }
static int constructDSASigningKey(struct pgpDigKeyDSA_s *key) { int rc; if (key->dsa_key) { /* We've already constructed it, so just reuse it */ return 1; } /* Create the DSA key */ DSA *dsa = DSA_new(); if (!dsa) return 0; if (!DSA_set0_pqg(dsa, key->p, key->q, key->g)) { rc = 0; goto done; } if (!DSA_set0_key(dsa, key->y, NULL)) { rc = 0; goto done; } key->dsa_key = dsa; rc = 1; done: if (rc == 0) { DSA_free(dsa); } return rc; }
int32_t CryptoNative_DsaKeyCreateByExplicitParameters( DSA** outDsa, uint8_t* p, int32_t pLength, uint8_t* q, int32_t qLength, uint8_t* g, int32_t gLength, uint8_t* y, int32_t yLength, uint8_t* x, int32_t xLength) { if (!outDsa) { assert(false); return 0; } *outDsa = DSA_new(); if (!*outDsa) { return 0; } DSA* dsa = *outDsa; BIGNUM* bnP = MakeBignum(p, pLength); BIGNUM* bnQ = MakeBignum(q, qLength); BIGNUM* bnG = MakeBignum(g, gLength); if (!DSA_set0_pqg(dsa, bnP, bnQ, bnG)) { // BN_free handles NULL input BN_free(bnP); BN_free(bnQ); BN_free(bnG); return 0; } // Control was transferred, do not free. bnP = NULL; bnQ = NULL; bnG = NULL; BIGNUM* bnY = MakeBignum(y, yLength); BIGNUM* bnX = MakeBignum(x, xLength); if (!DSA_set0_key(dsa, bnY, bnX)) { BN_free(bnY); BN_free(bnX); return 0; } return 1; }
int _libssh2_dsa_new(libssh2_dsa_ctx ** dsactx, const unsigned char *p, unsigned long p_len, const unsigned char *q, unsigned long q_len, const unsigned char *g, unsigned long g_len, const unsigned char *y, unsigned long y_len, const unsigned char *x, unsigned long x_len) { BIGNUM * p_bn; BIGNUM * q_bn; BIGNUM * g_bn; BIGNUM * pub_key; BIGNUM * priv_key = NULL; p_bn = BN_new(); BN_bin2bn(p, p_len, p_bn); q_bn = BN_new(); BN_bin2bn(q, q_len, q_bn); g_bn = BN_new(); BN_bin2bn(g, g_len, g_bn); pub_key = BN_new(); BN_bin2bn(y, y_len, pub_key); if(x_len) { priv_key = BN_new(); BN_bin2bn(x, x_len, priv_key); } *dsactx = DSA_new(); #ifdef HAVE_OPAQUE_STRUCTS DSA_set0_pqg(*dsactx, p_bn, q_bn, g_bn); #else (*dsactx)->p = p_bn; (*dsactx)->g = g_bn; (*dsactx)->q = q_bn; #endif #ifdef HAVE_OPAQUE_STRUCTS DSA_set0_key(*dsactx, pub_key, priv_key); #else (*dsactx)->pub_key = pub_key; (*dsactx)->priv_key = priv_key; #endif return 0; }
/* * Returns VAL_NO_ERROR on success, other values on failure */ static int dsasha1_parse_public_key(const u_char *buf, size_t buflen, DSA * dsa) { u_char T; int index = 0; BIGNUM *bn_p, *bn_q, *bn_g, *bn_y; if (!dsa || buflen == 0) { return VAL_BAD_ARGUMENT; } T = (u_char) (buf[index]); index++; if (index+20 > buflen) return VAL_BAD_ARGUMENT; bn_q = BN_bin2bn(buf + index, 20, NULL); index += 20; if (index+64 + (T * 8) > buflen) return VAL_BAD_ARGUMENT; bn_p = BN_bin2bn(buf + index, 64 + (T * 8), NULL); index += (64 + (T * 8)); if (index+64 + (T * 8) > buflen) return VAL_BAD_ARGUMENT; bn_g = BN_bin2bn(buf + index, 64 + (T * 8), NULL); index += (64 + (T * 8)); if (index+64 + (T * 8) > buflen) return VAL_BAD_ARGUMENT; bn_y = BN_bin2bn(buf + index, 64 + (T * 8), NULL); index += (64 + (T * 8)); DSA_set0_pqg(dsa, bn_p, bn_q, bn_g); DSA_set0_key(dsa, bn_y, NULL); return VAL_NO_ERROR; /* success */ }
static EVP_PKEY *b2i_dss(const unsigned char **in, unsigned int bitlen, int ispub) { const unsigned char *p = *in; EVP_PKEY *ret = NULL; DSA *dsa = NULL; BN_CTX *ctx = NULL; unsigned int nbyte; BIGNUM *pbn = NULL, *qbn = NULL, *gbn = NULL, *priv_key = NULL; BIGNUM *pub_key = NULL; nbyte = (bitlen + 7) >> 3; dsa = DSA_new(); ret = EVP_PKEY_new(); if (dsa == NULL || ret == NULL) goto memerr; if (!read_lebn(&p, nbyte, &pbn)) goto memerr; if (!read_lebn(&p, 20, &qbn)) goto memerr; if (!read_lebn(&p, nbyte, &gbn)) goto memerr; if (ispub) { if (!read_lebn(&p, nbyte, &pub_key)) goto memerr; } else { if (!read_lebn(&p, 20, &priv_key)) goto memerr; /* Calculate public key */ pub_key = BN_new(); if (pub_key == NULL) goto memerr; if ((ctx = BN_CTX_new()) == NULL) goto memerr; if (!BN_mod_exp(pub_key, gbn, priv_key, pbn, ctx)) goto memerr; BN_CTX_free(ctx); } if (!DSA_set0_pqg(dsa, pbn, qbn, gbn)) goto memerr; pbn = qbn = gbn = NULL; if (!DSA_set0_key(dsa, pub_key, priv_key)) goto memerr; EVP_PKEY_set1_DSA(ret, dsa); DSA_free(dsa); *in = p; return ret; memerr: PEMerr(PEM_F_B2I_DSS, ERR_R_MALLOC_FAILURE); DSA_free(dsa); BN_free(pbn); BN_free(qbn); BN_free(gbn); BN_free(pub_key); BN_free(priv_key); EVP_PKEY_free(ret); BN_CTX_free(ctx); return NULL; }
DSA * sldns_key_buf2dsa_raw(unsigned char* key, size_t len) { uint8_t T; uint16_t length; uint16_t offset; DSA *dsa; BIGNUM *Q; BIGNUM *P; BIGNUM *G; BIGNUM *Y; if(len == 0) return NULL; T = (uint8_t)key[0]; length = (64 + T * 8); offset = 1; if (T > 8) { return NULL; } if(len < (size_t)1 + SHA_DIGEST_LENGTH + 3*length) return NULL; Q = BN_bin2bn(key+offset, SHA_DIGEST_LENGTH, NULL); offset += SHA_DIGEST_LENGTH; P = BN_bin2bn(key+offset, (int)length, NULL); offset += length; G = BN_bin2bn(key+offset, (int)length, NULL); offset += length; Y = BN_bin2bn(key+offset, (int)length, NULL); /* create the key and set its properties */ if(!Q || !P || !G || !Y || !(dsa = DSA_new())) { BN_free(Q); BN_free(P); BN_free(G); BN_free(Y); return NULL; } #if OPENSSL_VERSION_NUMBER < 0x10100000 || defined(HAVE_LIBRESSL) #ifndef S_SPLINT_S dsa->p = P; dsa->q = Q; dsa->g = G; dsa->pub_key = Y; #endif /* splint */ #else /* OPENSSL_VERSION_NUMBER */ if (!DSA_set0_pqg(dsa, P, Q, G)) { /* QPG not yet attached, need to free */ BN_free(Q); BN_free(P); BN_free(G); DSA_free(dsa); BN_free(Y); return NULL; } if (!DSA_set0_key(dsa, Y, NULL)) { /* QPG attached, cleaned up by DSA_fre() */ DSA_free(dsa); BN_free(Y); return NULL; } #endif return dsa; }
/* * These parameters are from test/recipes/04-test_pem_data/dsaparam.pem, * converted using dsaparam -C */ static DSA *load_dsa_params(void) { static unsigned char dsap_2048[] = { 0xAE, 0x35, 0x7D, 0x4E, 0x1D, 0x96, 0xE2, 0x9F, 0x00, 0x96, 0x60, 0x5A, 0x6E, 0x4D, 0x07, 0x8D, 0xA5, 0x7C, 0xBC, 0xF9, 0xAD, 0xD7, 0x9F, 0xD5, 0xE9, 0xEE, 0xA6, 0x33, 0x51, 0xDE, 0x7B, 0x72, 0xD2, 0x75, 0xAA, 0x71, 0x77, 0xF1, 0x63, 0xFB, 0xB6, 0xEC, 0x5A, 0xBA, 0x0D, 0x72, 0xA2, 0x1A, 0x1C, 0x64, 0xB8, 0xE5, 0x89,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50, 0xC9, 0xAE, 0xEB, 0xAE, 0xB6, 0x22, 0x32, 0x16, 0x6B, 0x8C, 0x59, 0xDA, 0xEE, 0x1D, 0x33, 0xDF, 0x4C, 0xA2, 0x3D }; static unsigned char dsaq_2048[] = { 0xAD, 0x2D, 0x6E, 0x17, 0xB0, 0xF3, 0xEB, 0xC7, 0xB8, 0xEE, 0x95, 0x78, 0xF2, 0x17, 0xF5, 0x33, 0x01, 0x67, 0xBC, 0xDE, 0x93, 0xFF, 0xEE, 0x40, 0xE8, 0x7F, 0xF1, 0x93, 0x6D, 0x4B, 0x87, 0x13 }; static unsigned char dsag_2048[] = { 0x66, 0x6F, 0xDA, 0x63, 0xA5, 0x8E, 0xD2, 0x4C, 0xD5, 0x45, 0x2D, 0x76, 0x5D, 0x5F, 0xCD, 0x4A, 0xB4, 0x1A, 0x42, 0x35, 0x86, 0x3A, 0x6F, 0xA9, 0xFA, 0x27, 0xAB, 0xDE, 0x03, 0x21, 0x36, 0x0A, 0x07, 0x29, 0xC9, 0x2F, 0x6D, 0x49, 0xA8, 0xF7, 0xC6, 0xF4, 0x92, 0xD7, 0x73, 0xC1, 0xD8, 0x76, 0x0E, 0x61, 0xA7, 0x0B, 0x6E, 0x96, 0xB8, 0xC8, 0xCB, 0x38, 0x35, 0x12, 0x20, 0x79, 0xA5, 0x08, 0x28, 0x35, 0x5C, 0xBC, 0x52, 0x16, 0xAF, 0x52, 0xBA, 0x0F, 0xC3, 0xB1, 0x63, 0x12, 0x27, 0x0B, 0x74, 0xA4, 0x47, 0x43, 0xD6, 0x30, 0xB8, 0x9C, 0x2E, 0x40, 0x14, 0xCD, 0x99, 0x7F, 0xE8, 0x8E, 0x37, 0xB0, 0xA9, 0x3F, 0x54, 0xE9, 0x66, 0x22, 0x61, 0x4C, 0xF8, 0x49, 0x03, 0x57, 0x14, 0x32, 0x1D, 0x37, 0x3D, 0xE2, 0x92, 0xF8, 0x8E, 0xA0, 0x6A, 0x66, 0x63, 0xF0, 0xB0, 0x6E, 0x07, 0x2B, 0x3D, 0xBF, 0xD0, 0x84, 0x6A, 0xAA, 0x1F, 0x30, 0x77, 0x65, 0xE5, 0xFC, 0xF5, 0xEC, 0x55, 0xCE, 0x73, 0xDB, 0xBE, 0xA7, 0x8D, 0x3A, 0x9F, 0x7A, 0xED, 0x4F, 0xAF, 0xA2, 0x80, 0x4C, 0x30, 0x9E, 0x28, 0x49, 0x65, 0x40, 0xF0, 0x03, 0x45, 0x56, 0x99, 0xA2, 0x93, 0x1B, 0x9C, 0x46, 0xDE, 0xBD, 0xA8, 0xAB, 0x5F, 0x90, 0x3F, 0xB7, 0x3F, 0xD4, 0x6F, 0x8D, 0x5A, 0x30, 0xE1, 0xD4, 0x63, 0x3A, 0x6A, 0x7C, 0x8F, 0x24, 0xFC, 0xD9, 0x14, 0x28, 0x09, 0xE4, 0x84, 0x4E, 0x17, 0x43, 0x56, 0xB8, 0xD4, 0x4B, 0xA2, 0x29, 0x45, 0xD3, 0x13, 0xF0, 0xC2, 0x76, 0x9B, 0x01, 0xA0, 0x80, 0x6E, 0x93, 0x63, 0x5E, 0x87, 0x24, 0x20, 0x2A, 0xFF, 0xBB, 0x9F, 0xA8, 0x99, 0x6C, 0xA7, 0x9A, 0x00, 0xB9, 0x7D, 0xDA, 0x66, 0xC9, 0xC0, 0x72, 0x72, 0x22, 0x0F, 0x1A, 0xCC, 0x23, 0xD9, 0xB7, 0x5F, 0x1B }; DSA *dsa = DSA_new(); BIGNUM *p, *q, *g; if (dsa == NULL) return NULL; if (!DSA_set0_pqg(dsa, p = BN_bin2bn(dsap_2048, sizeof(dsap_2048), NULL), q = BN_bin2bn(dsaq_2048, sizeof(dsaq_2048), NULL), g = BN_bin2bn(dsag_2048, sizeof(dsag_2048), NULL))) { DSA_free(dsa); BN_free(p); BN_free(q); BN_free(g); return NULL; } return dsa; }