How to use makeCertificate method of x509 Package

Best K6 code snippet using x509.makeCertificate

insert_test.go

Source:insert_test.go Github

copy

Full Screen

...42 t.Fatal(err)43 }44 return45}46func makeCertificate() (serialNumber *big.Int, cert *x509.Certificate, pemBytes []byte, signer ocsp.Signer, err error) {47 privKey, err := rsa.GenerateKey(rand.Reader, 2048)48 if err != nil {49 return50 }51 serialNumberRange := new(big.Int).Lsh(big.NewInt(1), 128)52 serialNumber, err = rand.Int(rand.Reader, serialNumberRange)53 if err != nil {54 return55 }56 template := x509.Certificate{57 SerialNumber: serialNumber,58 Subject: pkix.Name{59 Organization: []string{"Cornell CS 5152"},60 },61 AuthorityKeyId: []byte{42, 42, 42, 42},62 }63 cert = &template64 issuerSerial, err := rand.Int(rand.Reader, serialNumberRange)65 if err != nil {66 return67 }68 responderSerial, err := rand.Int(rand.Reader, serialNumberRange)69 if err != nil {70 return71 }72 // Generate a CA certificate73 issuerTemplate := x509.Certificate{74 SerialNumber: issuerSerial,75 Subject: pkix.Name{76 Organization: []string{"Cornell CS 5152"},77 },78 AuthorityKeyId: []byte{42, 42, 42, 42},79 KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,80 IsCA: true,81 BasicConstraintsValid: true,82 }83 issuerBytes, err := x509.CreateCertificate(rand.Reader, &issuerTemplate, &issuerTemplate, &privKey.PublicKey, privKey)84 if err != nil {85 return86 }87 issuer, err := x509.ParseCertificate(issuerBytes)88 if err != nil {89 return90 }91 responderTemplate := x509.Certificate{92 SerialNumber: responderSerial,93 Subject: pkix.Name{94 Organization: []string{"Cornell CS 5152 Responder"},95 },96 AuthorityKeyId: []byte{42, 42, 42, 43},97 }98 responderBytes, err := x509.CreateCertificate(rand.Reader, &responderTemplate, &responderTemplate, &privKey.PublicKey, privKey)99 if err != nil {100 return101 }102 responder, err := x509.ParseCertificate(responderBytes)103 if err != nil {104 return105 }106 signer, err = ocsp.NewSigner(issuer, responder, privKey, time.Hour)107 if err != nil {108 return109 }110 derBytes, err := x509.CreateCertificate(rand.Reader, &template, issuer, &privKey.PublicKey, privKey)111 if err != nil {112 return113 }114 pemBytes = pem.EncodeToMemory(&pem.Block{115 Type: "CERTIFICATE",116 Bytes: derBytes,117 })118 return119}120func TestInsertValidCertificate(t *testing.T) {121 dbAccessor, err := prepDB()122 if err != nil {123 t.Fatal(err)124 }125 serialNumber, cert, pemBytes, signer, err := makeCertificate()126 if err != nil {127 t.Fatal(err)128 }129 resp, body := makeRequest(t, dbAccessor, signer, map[string]interface{}{130 "serial_number": serialNumber.Text(16),131 "authority_key_identifier": hex.EncodeToString(cert.AuthorityKeyId),132 "status": "good",133 "pem": string(pemBytes),134 })135 if resp.StatusCode != http.StatusOK {136 t.Fatal("Expected HTTP OK, got", resp.StatusCode, string(body))137 }138 var response map[string]interface{}139 if err = json.Unmarshal(body, &response); err != nil {140 t.Fatal("Could not parse response: ", err)141 }142 responseResult := response["result"].(map[string]interface{})143 encodedOcsp := responseResult["ocsp_response"].(string)144 rawOcsp, err := base64.StdEncoding.DecodeString(encodedOcsp)145 if err != nil {146 t.Fatal("Could not base64 decode response: ", err)147 }148 returnedOcsp, err := stdocsp.ParseResponse(rawOcsp, nil)149 if err != nil {150 t.Fatal("Could not parse returned OCSP response", err)151 }152 ocsps, err := dbAccessor.GetOCSP(serialNumber.Text(16), hex.EncodeToString(cert.AuthorityKeyId))153 if err != nil {154 t.Fatal(err)155 }156 if len(ocsps) != 1 {157 t.Fatal("Expected 1 OCSP record to be inserted, but found ", len(ocsps))158 }159 parsedOcsp, err := stdocsp.ParseResponse([]byte(ocsps[0].Body), nil)160 if err != nil {161 t.Fatal(err)162 }163 if parsedOcsp.SerialNumber.Cmp(returnedOcsp.SerialNumber) != 0 {164 t.Fatal("The returned and inserted OCSP response have different serial numbers: got ", returnedOcsp.SerialNumber, " but decoded ", parsedOcsp.SerialNumber)165 }166 if parsedOcsp.SerialNumber.Cmp(serialNumber) != 0 {167 t.Fatal("Got the wrong serial number: expected", serialNumber, "but got", parsedOcsp.SerialNumber)168 }169 if parsedOcsp.Status != stdocsp.Good {170 t.Fatal("Expected OCSP response status to be ", stdocsp.Good,171 " but found ", parsedOcsp.Status)172 }173}174func TestInsertMissingSerial(t *testing.T) {175 dbAccessor, err := prepDB()176 if err != nil {177 t.Fatal(err)178 }179 _, cert, pemBytes, signer, err := makeCertificate()180 if err != nil {181 t.Fatal(err)182 }183 resp, body := makeRequest(t, dbAccessor, signer, map[string]interface{}{184 "authority_key_identifier": hex.EncodeToString(cert.AuthorityKeyId),185 "status": "good",186 "pem": string(pemBytes),187 })188 if resp.StatusCode != http.StatusBadRequest {189 t.Fatal("Expected HTTP Bad Request", resp.StatusCode, string(body))190 }191}192func TestInsertMissingAKI(t *testing.T) {193 dbAccessor, err := prepDB()194 if err != nil {195 t.Fatal(err)196 }197 serialNumber, _, pemBytes, signer, err := makeCertificate()198 if err != nil {199 t.Fatal(err)200 }201 resp, body := makeRequest(t, dbAccessor, signer, map[string]interface{}{202 "serial_number": serialNumber.Text(16),203 "status": "good",204 "pem": string(pemBytes),205 })206 if resp.StatusCode != http.StatusBadRequest {207 t.Fatal("Expected HTTP Bad Request", resp.StatusCode, string(body))208 }209}210func TestInsertMissingPEM(t *testing.T) {211 dbAccessor, err := prepDB()212 if err != nil {213 t.Fatal(err)214 }215 serialNumber, cert, _, signer, err := makeCertificate()216 if err != nil {217 t.Fatal(err)218 }219 resp, body := makeRequest(t, dbAccessor, signer, map[string]interface{}{220 "serial_number": serialNumber.Text(16),221 "authority_key_identifier": hex.EncodeToString(cert.AuthorityKeyId),222 "status": "good",223 })224 if resp.StatusCode != http.StatusBadRequest {225 t.Fatal("Expected HTTP Bad Request", resp.StatusCode, string(body))226 }227}228func TestInsertInvalidSerial(t *testing.T) {229 dbAccessor, err := prepDB()230 if err != nil {231 t.Fatal(err)232 }233 _, cert, pemBytes, signer, err := makeCertificate()234 if err != nil {235 t.Fatal(err)236 }237 resp, body := makeRequest(t, dbAccessor, signer, map[string]interface{}{238 "serial_number": "this is not a serial number",239 "authority_key_identifier": hex.EncodeToString(cert.AuthorityKeyId),240 "status": "good",241 "pem": string(pemBytes),242 })243 if resp.StatusCode != http.StatusBadRequest {244 t.Fatal("Expected HTTP Bad Request", resp.StatusCode, string(body))245 }246}247func TestInsertInvalidAKI(t *testing.T) {248 dbAccessor, err := prepDB()249 if err != nil {250 t.Fatal(err)251 }252 serialNumber, _, pemBytes, signer, err := makeCertificate()253 if err != nil {254 t.Fatal(err)255 }256 resp, body := makeRequest(t, dbAccessor, signer, map[string]interface{}{257 "serial_number": serialNumber.Text(16),258 "authority_key_identifier": "this is not an AKI",259 "status": "good",260 "pem": string(pemBytes),261 })262 if resp.StatusCode != http.StatusBadRequest {263 t.Fatal("Expected HTTP Bad Request, got", resp.StatusCode, string(body))264 }265}266func TestInsertInvalidStatus(t *testing.T) {267 dbAccessor, err := prepDB()268 if err != nil {269 t.Fatal(err)270 }271 serialNumber, cert, pemBytes, signer, err := makeCertificate()272 if err != nil {273 t.Fatal(err)274 }275 resp, body := makeRequest(t, dbAccessor, signer, map[string]interface{}{276 "serial_number": serialNumber.Text(16),277 "authority_key_identifier": hex.EncodeToString(cert.AuthorityKeyId),278 "status": "invalid",279 "pem": string(pemBytes),280 })281 if resp.StatusCode != http.StatusBadRequest {282 t.Fatal("Expected HTTP Bad Request", resp.StatusCode, string(body))283 }284}285func TestInsertInvalidPEM(t *testing.T) {286 dbAccessor, err := prepDB()287 if err != nil {288 t.Fatal(err)289 }290 serialNumber, cert, _, signer, err := makeCertificate()291 if err != nil {292 t.Fatal(err)293 }294 resp, body := makeRequest(t, dbAccessor, signer, map[string]interface{}{295 "serial_number": serialNumber.Text(16),296 "authority_key_identifier": hex.EncodeToString(cert.AuthorityKeyId),297 "status": "good",298 "pem": "this is not a PEM certificate",299 })300 if resp.StatusCode != http.StatusBadRequest {301 t.Fatal("Expected HTTP Bad Request, got", resp.StatusCode, string(body))302 }303}304func TestInsertWrongSerial(t *testing.T) {305 dbAccessor, err := prepDB()306 if err != nil {307 t.Fatal(err)308 }309 _, cert, pemBytes, signer, err := makeCertificate()310 if err != nil {311 t.Fatal(err)312 }313 resp, body := makeRequest(t, dbAccessor, signer, map[string]interface{}{314 "serial_number": big.NewInt(1).Text(16),315 "authority_key_identifier": hex.EncodeToString(cert.AuthorityKeyId),316 "status": "good",317 "pem": string(pemBytes),318 })319 if resp.StatusCode != http.StatusBadRequest {320 t.Fatal("Expected HTTP Bad Request", resp.StatusCode, string(body))321 }322}323func TestInsertWrongAKI(t *testing.T) {324 dbAccessor, err := prepDB()325 if err != nil {326 t.Fatal(err)327 }328 serialNumber, _, pemBytes, signer, err := makeCertificate()329 if err != nil {330 t.Fatal(err)331 }332 resp, body := makeRequest(t, dbAccessor, signer, map[string]interface{}{333 "serial_number": serialNumber.Text(16),334 "authority_key_identifier": hex.EncodeToString([]byte{7, 7}),335 "status": "good",336 "pem": string(pemBytes),337 })338 if resp.StatusCode != http.StatusBadRequest {339 t.Fatal("Expected HTTP Bad Request", resp.StatusCode, string(body))340 }341}342func TestInsertRevokedCertificate(t *testing.T) {343 dbAccessor, err := prepDB()344 if err != nil {345 t.Fatal(err)346 }347 serialNumber, cert, pemBytes, signer, err := makeCertificate()348 if err != nil {349 t.Fatal(err)350 }351 resp, body := makeRequest(t, dbAccessor, signer, map[string]interface{}{352 "serial_number": serialNumber.Text(16),353 "authority_key_identifier": hex.EncodeToString(cert.AuthorityKeyId),354 "status": "revoked",355 "pem": string(pemBytes),356 "revoked_at": time.Now(),357 })358 if resp.StatusCode != http.StatusOK {359 t.Fatal("Expected HTTP OK", resp.StatusCode, string(body))360 }361 ocsps, err := dbAccessor.GetOCSP(serialNumber.Text(16), hex.EncodeToString(cert.AuthorityKeyId))362 if err != nil {363 t.Fatal(err)364 }365 if len(ocsps) != 1 {366 t.Fatal("Expected 1 OCSP record to be inserted, but found ", len(ocsps))367 }368 response, err := stdocsp.ParseResponse([]byte(ocsps[0].Body), nil)369 if err != nil {370 t.Fatal(err)371 }372 if response.Status != stdocsp.Revoked {373 t.Fatal("Expected OCSP response status to be ", stdocsp.Revoked,374 " but found ", response.Status)375 }376}377func TestInsertRevokedCertificateWithoutTime(t *testing.T) {378 dbAccessor, err := prepDB()379 if err != nil {380 t.Fatal(err)381 }382 serialNumber, cert, pemBytes, signer, err := makeCertificate()383 if err != nil {384 t.Fatal(err)385 }386 resp, body := makeRequest(t, dbAccessor, signer, map[string]interface{}{387 "serial_number": serialNumber.Text(16),388 "authority_key_identifier": hex.EncodeToString(cert.AuthorityKeyId),389 "status": "revoked",390 "pem": string(pemBytes),391 // Omit RevokedAt392 })393 if resp.StatusCode != http.StatusBadRequest {394 t.Fatal("Expected HTTP Bad Request", resp.StatusCode, string(body))395 }396}...

Full Screen

Full Screen

makeCertificate

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 priv, _ := rsa.GenerateKey(rand.Reader, 1024)4 cert := makeCertificate(priv)5 fmt.Println(cert)6}7func makeCertificate(priv *rsa.PrivateKey) *x509.Certificate {8 notBefore := time.Now()9 notAfter := notBefore.Add(365 * 24 * time.Hour)10 serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)11 serialNumber, _ := rand.Int(rand.Reader, serialNumberLimit)12 template := x509.Certificate{13 Subject: pkix.Name{14 Organization: []string{"My Company Name"},15 },16 ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},17 }18 derBytes, _ := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)19 cert, _ := x509.ParseCertificate(derBytes)20}

Full Screen

Full Screen

makeCertificate

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Generating RSA key pair")4 privateKey, err := rsa.GenerateKey(rand.Reader, 2048)5 if err != nil {6 fmt.Println(err)7 }8 fmt.Println("Creating certificate template")9 template := x509.Certificate{10 SerialNumber: big.NewInt(1),11 Subject: pkix.Name{12 Organization: []string{"Acme Co"},13 },14 NotBefore: time.Now(),15 NotAfter: time.Now().Add(time.Hour * 24 * 365),16 ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},17 }18 fmt.Println("Creating certificate")19 derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, publicKey, privateKey)20 if err != nil {21 fmt.Println(err)22 }23 fmt.Println("Saving certificate")24 certOut, err := os.Create("server.crt")25 if err != nil {26 fmt.Println(err)27 }28 pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})29 certOut.Close()30 fmt.Println("Saving private key")31 keyOut, err := os.OpenFile("server.key", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)32 if err != nil {33 fmt.Println(err)34 }35 pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)})36 keyOut.Close()37}

Full Screen

Full Screen

makeCertificate

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 template := x509.Certificate{4 SerialNumber: big.NewInt(1),5 Subject: pkix.Name{6 },7 NotBefore: time.Now(),8 NotAfter: time.Now().AddDate(1, 0, 0),9 }10 priv, err := rsa.GenerateKey(rand.Reader, 2048)11 if err != nil {12 fmt.Println(err)13 }14 cert, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)15 if err != nil {16 fmt.Println(err)17 }18 _ = ioutil.WriteFile("ca.crt", cert, 0644)19 _ = ioutil.WriteFile("ca.key", x509.MarshalPKCS1PrivateKey(priv), 0644)20}21func CreateCertificate(rand io.Reader, template, parent *Certificate, pub, priv interface{}) (cert []byte, err error)

Full Screen

Full Screen

makeCertificate

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 cert, err := makeCertificate()4 if err != nil {5 panic(err)6 }7}8func makeCertificate() (*x509.Certificate, error) {9 privateKey, err := rsa.GenerateKey(rand.Reader, 2048)10 if err != nil {11 }12 notBefore := time.Now()13 notAfter := notBefore.Add(365 * 24 * time.Hour)14 serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)15 serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)16 if err != nil {17 }18 template := x509.Certificate{19 Subject: pkix.Name{20 Organization: []string{"Acme Co"},21 OrganizationalUnit: []string{"Division 1"},22 Country: []string{"US"},23 Province: []string{"CA"},24 Locality: []string{"San Francisco"},25 StreetAddress: []string{"1234 Main Street"},26 PostalCode: []string{"94105"},27 },28 ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},29 IPAddresses: []net.IP{net.ParseIP("

Full Screen

Full Screen

makeCertificate

Using AI Code Generation

copy

Full Screen

1import (2func main() {3privatekey,err:=rsa.GenerateKey(rand.Reader,2048)4if err!=nil{5panic(err)6}7serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)8serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)9if err != nil {10panic(err)11}12template:=x509.Certificate{13Subject: pkix.Name{14Organization: []string{"My Company Name"},15OrganizationalUnit: []string{"My Organizational Unit"},16Locality: []string{"My Locality"},17Province: []string{"My Province"},18Country: []string{"My Country"},19},20NotBefore: time.Now(),21NotAfter: time.Now().AddDate(5, 5, 5),22ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},23}24cert,err:=x509.CreateCertificate(rand.Reader, &template, &template, publickey, privatekey)25if err!=nil{26panic(err)27}28err = ioutil.WriteFile("cert.pem", cert, 0644)29if err != nil {30panic(err)31}32}

Full Screen

Full Screen

makeCertificate

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 privateKey, _ := tls.X509KeyPair(certPEMBlock, keyPEMBlock)4 cert, _ := x509.ParseCertificate(privateKey.Certificate[0])5 fmt.Println(cert)6}7import (8func main() {9 privateKey, _ := tls.X509KeyPair(certPEMBlock, keyPEMBlock)10 cert, _ := x509.ParseCertificate(privateKey.Certificate[0])11 fmt.Println(cert)12}13import (14func main() {15 privateKey, _ := tls.X509KeyPair(certPEMBlock, keyPEMBlock)16 cert, _ := x509.ParseCertificate(privateKey.Certificate[0])17 fmt.Println(cert)18}19import (20func main() {21 privateKey, _ := tls.X509KeyPair(certPEMBlock, keyPEMBlock)22 cert, _ := x509.ParseCertificate(privateKey.Certificate[0])23 fmt.Println(cert)24}25import (26func main() {27 privateKey, _ := tls.X509KeyPair(certPEMBlock, keyPEMBlock)28 cert, _ := x509.ParseCertificate(privateKey.Certificate[0])29 fmt.Println(cert)30}31import (32func main() {33 privateKey, _ := tls.X509KeyPair(certPEMBlock, keyPEMBlock)34 cert, _ := x509.ParseCertificate(private

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful