genrsa.cc
Go to the documentation of this file.
1 /* Copyright (c) 2015, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #include <openssl/bio.h>
16 #include <openssl/bn.h>
17 #include <openssl/err.h>
18 #include <openssl/pem.h>
19 #include <openssl/rsa.h>
20 
21 #include "internal.h"
22 
23 
24 static const struct argument kArguments[] = {
25  {
26  "-bits", kOptionalArgument,
27  "The number of bits in the modulus (default: 2048)",
28  },
29  {
30  "", kOptionalArgument, "",
31  },
32 };
33 
34 bool GenerateRSAKey(const std::vector<std::string> &args) {
35  std::map<std::string, std::string> args_map;
36 
37  if (!ParseKeyValueArguments(&args_map, args, kArguments)) {
39  return false;
40  }
41 
42  unsigned bits;
43  if (!GetUnsigned(&bits, "-bits", 2048, args_map)) {
45  return false;
46  }
47 
48  bssl::UniquePtr<RSA> rsa(RSA_new());
49  bssl::UniquePtr<BIGNUM> e(BN_new());
50  bssl::UniquePtr<BIO> bio(BIO_new_fp(stdout, BIO_NOCLOSE));
51 
52  if (!BN_set_word(e.get(), RSA_F4) ||
53  !RSA_generate_key_ex(rsa.get(), bits, e.get(), NULL) ||
54  !PEM_write_bio_RSAPrivateKey(bio.get(), rsa.get(), NULL /* cipher */,
55  NULL /* key */, 0 /* key len */,
56  NULL /* password callback */,
57  NULL /* callback arg */)) {
59  return false;
60  }
61 
62  return true;
63 }
ParseKeyValueArguments
bool ParseKeyValueArguments(std::map< std::string, std::string > *out_args, const std::vector< std::string > &args, const struct argument *templates)
Definition: args.cc:27
bn.h
BIO_new_fp
#define BIO_new_fp
Definition: boringssl_prefix_symbols.h:819
internal.h
demumble_test.stdout
stdout
Definition: demumble_test.py:38
RSA_F4
#define RSA_F4
Definition: rsa.h:607
PEM_write_bio_RSAPrivateKey
#define PEM_write_bio_RSAPrivateKey
Definition: boringssl_prefix_symbols.h:1995
bio.h
ERR_print_errors_fp
#define ERR_print_errors_fp
Definition: boringssl_prefix_symbols.h:1437
pem.h
RSA_generate_key_ex
#define RSA_generate_key_ex
Definition: boringssl_prefix_symbols.h:2091
BN_set_word
#define BN_set_word
Definition: boringssl_prefix_symbols.h:992
python_utils.port_server.stderr
stderr
Definition: port_server.py:51
kArguments
static const struct argument kArguments[]
Definition: genrsa.cc:24
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
argument
Definition: third_party/boringssl-with-bazel/src/tool/internal.h:108
GenerateRSAKey
bool GenerateRSAKey(const std::vector< std::string > &args)
Definition: genrsa.cc:34
bits
OPENSSL_EXPORT ASN1_BIT_STRING * bits
Definition: x509v3.h:482
err.h
rsa.h
RSA_new
#define RSA_new
Definition: boringssl_prefix_symbols.h:2110
PrintUsage
void PrintUsage(const struct argument *templates)
Definition: args.cc:75
GetUnsigned
bool GetUnsigned(unsigned *out, const std::string &arg_name, unsigned default_value, const std::map< std::string, std::string > &args)
Definition: args.cc:82
kOptionalArgument
@ kOptionalArgument
Definition: third_party/boringssl-with-bazel/src/tool/internal.h:104
BIO_NOCLOSE
#define BIO_NOCLOSE
Definition: bio.h:373
BN_new
#define BN_new
Definition: boringssl_prefix_symbols.h:971


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:28