📄 asn1.c
字号:
if (rc != CKR_OK){ st_err_log(84, __FILE__, __LINE__); goto cleanup; } offset += field_len; } // prime #1 // rc = ber_decode_INTEGER( buf+offset, &tmp, &len, &field_len ); if (rc != CKR_OK){ st_err_log(79, __FILE__, __LINE__); goto cleanup; } else { rc = build_attribute( CKA_PRIME_1, tmp, len, &p_attr ); if (rc != CKR_OK){ st_err_log(84, __FILE__, __LINE__); goto cleanup; } offset += field_len; } // prime #2 // rc = ber_decode_INTEGER( buf+offset, &tmp, &len, &field_len ); if (rc != CKR_OK){ st_err_log(79, __FILE__, __LINE__); goto cleanup; } else { rc = build_attribute( CKA_PRIME_2, tmp, len, &q_attr ); if (rc != CKR_OK){ st_err_log(84, __FILE__, __LINE__); goto cleanup; } offset += field_len; } // exponent #1 // rc = ber_decode_INTEGER( buf+offset, &tmp, &len, &field_len ); if (rc != CKR_OK){ st_err_log(79, __FILE__, __LINE__); goto cleanup; } else { rc = build_attribute( CKA_EXPONENT_1, tmp, len, &e1_attr ); if (rc != CKR_OK){ st_err_log(84, __FILE__, __LINE__); goto cleanup; } offset += field_len; } // exponent #2 // rc = ber_decode_INTEGER( buf+offset, &tmp, &len, &field_len ); if (rc != CKR_OK){ st_err_log(79, __FILE__, __LINE__); goto cleanup; } else { rc = build_attribute( CKA_EXPONENT_2, tmp, len, &e2_attr ); if (rc != CKR_OK){ st_err_log(84, __FILE__, __LINE__); goto cleanup; } offset += field_len; } // coefficient // rc = ber_decode_INTEGER( buf+offset, &tmp, &len, &field_len ); if (rc != CKR_OK){ st_err_log(79, __FILE__, __LINE__); goto cleanup; } else { rc = build_attribute( CKA_COEFFICIENT, tmp, len, &coeff_attr ); if (rc != CKR_OK){ st_err_log(84, __FILE__, __LINE__); goto cleanup; } offset += len; } *modulus = n_attr; *publ_exp = e_attr; *priv_exp = d_attr; *prime1 = p_attr; *prime2 = q_attr; *exponent1 = e1_attr; *exponent2 = e2_attr; *coeff = coeff_attr; return CKR_OK;cleanup: if (n_attr) free(n_attr); if (e_attr) free(e_attr); if (d_attr) free(d_attr); if (p_attr) free(p_attr); if (q_attr) free(q_attr); if (e1_attr) free(e1_attr); if (e2_attr) free(e2_attr); if (coeff_attr) free(coeff_attr); return rc;}// DSA is a little different from RSA//// DSAPrivateKey ::= INTEGER//// The 'parameters' field of the AlgorithmIdentifier are as follows://// DSSParameters ::= SEQUENCE {// prime1 INTEGER// prime2 INTEGER// base INTEGER// }//CK_RVber_encode_DSAPrivateKey( CK_BBOOL length_only, CK_BYTE ** data, CK_ULONG * data_len, CK_ATTRIBUTE * prime1, CK_ATTRIBUTE * prime2, CK_ATTRIBUTE * base, CK_ATTRIBUTE * priv_key ){ CK_BYTE *param = NULL; CK_BYTE *buf = NULL; CK_BYTE *tmp = NULL; CK_BYTE *alg = NULL; CK_ULONG offset, len, param_len; CK_ULONG alg_len; CK_RV rc; // build the DSS parameters first // offset = 0; rc = 0; rc |= ber_encode_INTEGER( TRUE, NULL, &len, NULL, prime1->ulValueLen ); offset += len; rc |= ber_encode_INTEGER( TRUE, NULL, &len, NULL, prime2->ulValueLen ); offset += len; rc |= ber_encode_INTEGER( TRUE, NULL, &len, NULL, base->ulValueLen ); offset += len; if (rc != CKR_OK){ st_err_log(4, __FILE__, __LINE__, __FUNCTION__); return CKR_FUNCTION_FAILED; } if (length_only == TRUE) { rc = ber_encode_SEQUENCE( TRUE, NULL, ¶m_len, NULL, offset ); if (rc != CKR_OK){ st_err_log(78, __FILE__, __LINE__); return rc; } rc = ber_encode_INTEGER( TRUE, NULL, &len, NULL, priv_key->ulValueLen ); if (rc != CKR_OK){ st_err_log(76, __FILE__, __LINE__); return rc; } rc = ber_encode_PrivateKeyInfo( TRUE, NULL, data_len, NULL, ber_idDSALen + param_len, NULL, len ); if (rc != CKR_OK){ st_err_log(82, __FILE__, __LINE__); } return rc; } // 'buf' will be the sequence data for the AlgorithmIdentifyer::parameter // buf = (CK_BYTE *)malloc(offset); if (!buf){ st_err_log(1, __FILE__, __LINE__); return CKR_HOST_MEMORY; } len = 0; offset = 0; rc = ber_encode_INTEGER( FALSE, &tmp, &len, (CK_BYTE *)prime1 + sizeof(CK_ATTRIBUTE), prime1->ulValueLen ); if (rc != CKR_OK){ st_err_log(76, __FILE__, __LINE__); goto error; } memcpy( buf+offset, tmp, len ); offset += len; free( tmp ); tmp = NULL; rc = ber_encode_INTEGER( FALSE, &tmp, &len, (CK_BYTE *)prime2 + sizeof(CK_ATTRIBUTE), prime2->ulValueLen ); if (rc != CKR_OK){ st_err_log(76, __FILE__, __LINE__); goto error; } memcpy( buf+offset, tmp, len ); offset += len; free( tmp ); tmp = NULL; rc = ber_encode_INTEGER( FALSE, &tmp, &len, (CK_BYTE *)base + sizeof(CK_ATTRIBUTE), base->ulValueLen ); if (rc != CKR_OK){ st_err_log(76, __FILE__, __LINE__); goto error; } memcpy( buf+offset, tmp, len ); offset += len; free( tmp ); tmp = NULL; rc = ber_encode_SEQUENCE( FALSE, ¶m, ¶m_len, buf, offset ); if (rc != CKR_OK) { st_err_log(78, __FILE__, __LINE__); free(buf); return rc; } free( buf ); buf = NULL; // Build the DSA AlgorithmIdentifier // // AlgorithmIdentifier ::= SEQUENCE { // algorithm OBJECT IDENTIFIER // parameters ANY DEFINED BY algorithm OPTIONAL // } // len = ber_idDSALen + param_len; buf = (CK_BYTE *)malloc( len ); if (!buf){ st_err_log(1, __FILE__, __LINE__); goto error; } memcpy( buf, ber_idDSA, ber_idDSALen ); memcpy( buf + ber_idDSALen, param, param_len ); free( param ); param = NULL; rc = ber_encode_SEQUENCE( FALSE, &alg, &alg_len, buf, len ); if (rc != CKR_OK){ st_err_log(78, __FILE__, __LINE__); goto error; } free( buf ); buf = NULL; // build the private key INTEGER // rc = ber_encode_INTEGER( FALSE, &buf, &len, (CK_BYTE *)priv_key + sizeof(CK_ATTRIBUTE), priv_key->ulValueLen ); if (rc != CKR_OK){ st_err_log(76, __FILE__, __LINE__); goto error; } rc = ber_encode_PrivateKeyInfo( FALSE, data, data_len, alg, alg_len, buf, len ); if (rc != CKR_OK){ st_err_log(82, __FILE__, __LINE__); goto error; }error: if (alg) free( alg ); if (buf) free( buf ); if (param) free( param ); if (tmp) free( tmp ); return rc;}////CK_RVber_decode_DSAPrivateKey( CK_BYTE * data, CK_ULONG data_len, CK_ATTRIBUTE ** prime, CK_ATTRIBUTE ** subprime, CK_ATTRIBUTE ** base, CK_ATTRIBUTE ** priv_key ){ CK_ATTRIBUTE *p_attr = NULL; CK_ATTRIBUTE *q_attr = NULL; CK_ATTRIBUTE *g_attr = NULL; CK_ATTRIBUTE *x_attr = NULL; CK_BYTE *alg = NULL; CK_BYTE *buf = NULL; CK_BYTE *dsakey = NULL; CK_BYTE *tmp = NULL; CK_ULONG buf_len, field_len, len, offset; CK_RV rc; rc = ber_decode_PrivateKeyInfo( data, data_len, &alg, &len, &dsakey ); if (rc != CKR_OK){ st_err_log(82, __FILE__, __LINE__); return rc; } // make sure we're dealing with a DSA key. just compare the OBJECT // IDENTIFIER // if (memcmp(alg, ber_idDSA, ber_idDSALen) != 0){ st_err_log(4, __FILE__, __LINE__, __FUNCTION__); return CKR_FUNCTION_FAILED; } // extract the parameter data into ATTRIBUTES // rc = ber_decode_SEQUENCE( alg + ber_idDSALen, &buf, &buf_len, &field_len ); if (rc != CKR_OK){ st_err_log(81, __FILE__, __LINE__); return rc; } offset = 0; // prime // rc = ber_decode_INTEGER( buf+offset, &tmp, &len, &field_len ); if (rc != CKR_OK){ st_err_log(79, __FILE__, __LINE__); goto cleanup; } offset += field_len; // subprime // rc = ber_decode_INTEGER( buf+offset, &tmp, &len, &field_len ); if (rc != CKR_OK){ st_err_log(79, __FILE__, __LINE__); goto cleanup; } offset += field_len; // base // rc = ber_decode_INTEGER( buf+offset, &tmp, &len, &field_len ); if (rc != CKR_OK){ st_err_log(79, __FILE__, __LINE__); goto cleanup; } offset += field_len; if (offset > buf_len){ st_err_log(4, __FILE__, __LINE__, __FUNCTION__); return CKR_FUNCTION_FAILED; } // // it looks okay. build the attributes // offset = 0; // prime // rc = ber_decode_INTEGER( buf+offset, &tmp, &len, &field_len ); if (rc != CKR_OK){ st_err_log(79, __FILE__, __LINE__); goto cleanup; } else { rc = build_attribute( CKA_PRIME, tmp, len, &p_attr ); if (rc != CKR_OK){ st_err_log(84, __FILE__, __LINE__); goto cleanup; } offset += field_len; } // subprime // rc = ber_decode_INTEGER( buf+offset, &tmp, &len, &field_len ); if (rc != CKR_OK){ st_err_log(79, __FILE__, __LINE__); goto cleanup; } else { rc = build_attribute( CKA_SUBPRIME, tmp, len, &q_attr ); if (rc != CKR_OK){ st_err_log(84, __FILE__, __LINE__); goto cleanup; } offset += field_len; } // base // rc = ber_decode_INTEGER( buf+offset, &tmp, &len, &field_len ); if (rc != CKR_OK){ st_err_log(79, __FILE__, __LINE__); goto cleanup; } else { rc = build_attribute( CKA_BASE, tmp, len, &g_attr ); if (rc != CKR_OK){ st_err_log(84, __FILE__, __LINE__); goto cleanup; } offset += field_len; } // now get the private key // rc = ber_decode_INTEGER( dsakey, &tmp, &len, &field_len ); if (rc != CKR_OK){ st_err_log(79, __FILE__, __LINE__); goto cleanup; } else { rc = build_attribute( CKA_VALUE, tmp, len, &x_attr ); if (rc != CKR_OK){ st_err_log(84, __FILE__, __LINE__); goto cleanup; } offset += field_len; } *prime = p_attr; *subprime = q_attr; *base = g_attr; *priv_key = x_attr; return CKR_OK;cleanup: if (p_attr) free(p_attr); if (q_attr) free(q_attr); if (g_attr) free(g_attr); if (x_attr) free(x_attr); return rc;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -